




from typing import Optional

import numpy as np
import torch
from torch import nn
from transformers import GPTaConfig, GPTaLMHeadModel
from transformers.modeling_utils import ModuleUtilsMixin

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class   __a(					_a    ,						_a    ,						_a       ):






    """simple docstring"""





    lowerCAmelCase        =				[R'''h\.\d+\.attn\.bias''', R'''h\.\d+\.attn\.masked_bias''']





    @register_to_config
    def __init__(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE = None       ,_SCREAMING_SNAKE_CASE = 50_257       ,_SCREAMING_SNAKE_CASE = 1_024       ,_SCREAMING_SNAKE_CASE = 768       ,_SCREAMING_SNAKE_CASE = 12       ,_SCREAMING_SNAKE_CASE = 12       ,_SCREAMING_SNAKE_CASE = None       ,_SCREAMING_SNAKE_CASE = "gelu_new"       ,_SCREAMING_SNAKE_CASE = 0.1       ,_SCREAMING_SNAKE_CASE = 0.1       ,_SCREAMING_SNAKE_CASE = 0.1       ,_SCREAMING_SNAKE_CASE = 1e-5       ,_SCREAMING_SNAKE_CASE = 0.02       ,_SCREAMING_SNAKE_CASE = True       ,_SCREAMING_SNAKE_CASE = True       ,_SCREAMING_SNAKE_CASE = False       ,_SCREAMING_SNAKE_CASE = False       ,)			->		Tuple:
         super().__init__()

         UpperCAmelCase_      :							List[str]       =      prefix_length

         if prefix_inner_dim != n_embd and prefix_hidden_dim is None:
              raise ValueError(
                  f'''`prefix_hidden_dim` cannot be `None` when `prefix_inner_dim`: {prefix_hidden_dim} and'''
                  f''' `n_embd`: {n_embd} are not equal.'''  )

         UpperCAmelCase_      :							Optional[int]       =      prefix_inner_dim
         UpperCAmelCase_      :							Any       =      prefix_hidden_dim

         UpperCAmelCase_      :							Dict       =      (
             nn.Linear(self.prefix_inner_dim       ,self.prefix_hidden_dim  )
             if self.prefix_hidden_dim is not None
             else nn.Identity()
         )
         UpperCAmelCase_      :							Optional[int]       =      (
             nn.Linear(self.prefix_hidden_dim       ,_SCREAMING_SNAKE_CASE  ) if self.prefix_hidden_dim is not None else nn.Identity()
         )

         UpperCAmelCase_      :							Any       =      GPTaConfig(
             vocab_size=_SCREAMING_SNAKE_CASE       ,n_positions=_SCREAMING_SNAKE_CASE       ,n_embd=_SCREAMING_SNAKE_CASE       ,n_layer=_SCREAMING_SNAKE_CASE       ,n_head=_SCREAMING_SNAKE_CASE       ,n_inner=_SCREAMING_SNAKE_CASE       ,activation_function=_SCREAMING_SNAKE_CASE       ,resid_pdrop=_SCREAMING_SNAKE_CASE       ,embd_pdrop=_SCREAMING_SNAKE_CASE       ,attn_pdrop=_SCREAMING_SNAKE_CASE       ,layer_norm_epsilon=_SCREAMING_SNAKE_CASE       ,initializer_range=_SCREAMING_SNAKE_CASE       ,scale_attn_weights=_SCREAMING_SNAKE_CASE       ,use_cache=_SCREAMING_SNAKE_CASE       ,scale_attn_by_inverse_layer_idx=_SCREAMING_SNAKE_CASE       ,reorder_and_upcast_attn=_SCREAMING_SNAKE_CASE       ,)
         UpperCAmelCase_      :							int       =      GPTaLMHeadModel(_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE = None       ,_SCREAMING_SNAKE_CASE = None       ,)			->		Union[str, Any]:
         UpperCAmelCase_      :							Optional[int]       =      self.transformer.transformer.wte(_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							int       =      self.encode_prefix(_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Dict       =      self.decode_prefix(_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Union[str, Any]       =      torch.cat((prefix_embeds, embedding_text)       ,dim=1  )

         if labels is not None:
              UpperCAmelCase_      :							Tuple       =      self.get_dummy_token(input_ids.shape[0]       ,input_ids.device  )
              UpperCAmelCase_      :							Optional[int]       =      torch.cat((dummy_token, input_ids)       ,dim=1  )
         UpperCAmelCase_      :							Optional[int]       =      self.transformer(inputs_embeds=_SCREAMING_SNAKE_CASE       ,labels=_SCREAMING_SNAKE_CASE       ,attention_mask=_SCREAMING_SNAKE_CASE  )
         if self.prefix_hidden_dim is not None:
              return out, hidden
         else:
              return out





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		torch.Tensor:
         return torch.zeros(_SCREAMING_SNAKE_CASE       ,self.prefix_length       ,dtype=torch.intaa       ,device=_SCREAMING_SNAKE_CASE  )





    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE  )			->		List[str]:
         return self.encode_prefix(_SCREAMING_SNAKE_CASE  )





    @torch.no_grad()
    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE       ,_SCREAMING_SNAKE_CASE  )			->		Optional[int]:
         UpperCAmelCase_      :							List[str]       =      torch.split(_SCREAMING_SNAKE_CASE       ,1       ,dim=0  )
         UpperCAmelCase_      :							Tuple       =      []
         UpperCAmelCase_      :							Optional[int]       =      []
         for feature in features:
              UpperCAmelCase_      :							Any       =      self.decode_prefix(feature.to(_SCREAMING_SNAKE_CASE  )  )  # back to the clip feature
              # Only support beam search for now
              UpperCAmelCase_,   UpperCAmelCase_      :							int       =      self.generate_beam(
                  input_embeds=_SCREAMING_SNAKE_CASE       ,device=_SCREAMING_SNAKE_CASE       ,eos_token_id=_SCREAMING_SNAKE_CASE  )
              generated_tokens.append(output_tokens[0]  )
              generated_seq_lengths.append(seq_lengths[0]  )
         UpperCAmelCase_      :							Any       =      torch.stack(_SCREAMING_SNAKE_CASE  )
         UpperCAmelCase_      :							Dict       =      torch.stack(_SCREAMING_SNAKE_CASE  )
         return generated_tokens, generated_seq_lengths





    @torch.no_grad()
    def 							a__				(				self       ,_SCREAMING_SNAKE_CASE=None       ,_SCREAMING_SNAKE_CASE=None       ,_SCREAMING_SNAKE_CASE=None       ,_SCREAMING_SNAKE_CASE = 5       ,_SCREAMING_SNAKE_CASE = 67       ,_SCREAMING_SNAKE_CASE = 1.0       ,_SCREAMING_SNAKE_CASE = None       ,)			->		Dict:
         UpperCAmelCase_      :							List[Any]       =      eos_token_id
         UpperCAmelCase_      :							str       =      None
         UpperCAmelCase_      :							List[str]       =      None
         UpperCAmelCase_      :							int       =      torch.ones(_SCREAMING_SNAKE_CASE       ,device=_SCREAMING_SNAKE_CASE       ,dtype=torch.int  )
         UpperCAmelCase_      :							Any       =      torch.zeros(_SCREAMING_SNAKE_CASE       ,device=_SCREAMING_SNAKE_CASE       ,dtype=torch.bool  )

         if input_embeds is not None:
              UpperCAmelCase_      :							Tuple       =      input_embeds
         else:
              UpperCAmelCase_      :							Dict       =      self.transformer.transformer.wte(_SCREAMING_SNAKE_CASE  )

         for i in range(_SCREAMING_SNAKE_CASE  ):
              UpperCAmelCase_      :							Tuple       =      self.transformer(inputs_embeds=_SCREAMING_SNAKE_CASE  )
              UpperCAmelCase_      :							List[str]       =      outputs.logits
              UpperCAmelCase_      :							List[str]       =      logits[:, -1, :] / (temperature if temperature > 0 else 1.0)
              UpperCAmelCase_      :							int       =      logits.softmax(-1  ).log()

              if scores is None:
                   UpperCAmelCase_,   UpperCAmelCase_      :							str       =      logits.topk(_SCREAMING_SNAKE_CASE       ,-1  )
                   UpperCAmelCase_      :							str       =      generated.expand(_SCREAMING_SNAKE_CASE       ,*generated.shape[1:]  )
                   UpperCAmelCase_,   UpperCAmelCase_      :							List[Any]       =      next_tokens.permute(1       ,0  ), scores.squeeze(0  )
                   if tokens is None:
                        UpperCAmelCase_      :							Dict       =      next_tokens
                   else:
                        UpperCAmelCase_      :							Any       =      tokens.expand(_SCREAMING_SNAKE_CASE       ,*tokens.shape[1:]  )
                        UpperCAmelCase_      :							Any       =      torch.cat((tokens, next_tokens)       ,dim=1  )
              else:
                   UpperCAmelCase_      :							Dict       =      -float(np.inf  )
                   UpperCAmelCase_      :							int       =      0
                   UpperCAmelCase_      :							Dict       =      scores[:, None] + logits
                   seq_lengths[~is_stopped] += 1
                   UpperCAmelCase_      :							List[str]       =      scores_sum / seq_lengths[:, None]
                   UpperCAmelCase_,   UpperCAmelCase_      :							List[Any]       =      scores_sum_average.view(-1  ).topk(_SCREAMING_SNAKE_CASE       ,-1  )
                   UpperCAmelCase_      :							str       =      next_tokens // scores_sum.shape[1]
                   UpperCAmelCase_      :							Dict       =      seq_lengths[next_tokens_source]
                   UpperCAmelCase_      :							Optional[Any]       =      next_tokens % scores_sum.shape[1]
                   UpperCAmelCase_      :							Union[str, Any]       =      next_tokens.unsqueeze(1  )
                   UpperCAmelCase_      :							str       =      tokens[next_tokens_source]
                   UpperCAmelCase_      :							Optional[Any]       =      torch.cat((tokens, next_tokens)       ,dim=1  )
                   UpperCAmelCase_      :							Any       =      generated[next_tokens_source]
                   UpperCAmelCase_      :							Dict       =      scores_sum_average * seq_lengths
                   UpperCAmelCase_      :							Dict       =      is_stopped[next_tokens_source]

              UpperCAmelCase_      :							Dict       =      self.transformer.transformer.wte(next_tokens.squeeze()  ).view(generated.shape[0]       ,1       ,-1  )
              UpperCAmelCase_      :							List[Any]       =      torch.cat((generated, next_token_embed)       ,dim=1  )
              UpperCAmelCase_      :							List[str]       =      is_stopped + next_tokens.eq(_SCREAMING_SNAKE_CASE  ).squeeze()
              if is_stopped.all():
                   break

         UpperCAmelCase_      :							int       =      scores / seq_lengths
         UpperCAmelCase_      :							List[Any]       =      scores.argsort(descending=_SCREAMING_SNAKE_CASE  )
         # tokens tensors are already padded to max_seq_length
         UpperCAmelCase_      :							List[str]       =      [tokens[i] for i in order]
         UpperCAmelCase_      :							Optional[Any]       =      torch.stack(_SCREAMING_SNAKE_CASE       ,dim=0  )
         UpperCAmelCase_      :							int       =      torch.tensor([seq_lengths[i] for i in order]       ,dtype=seq_lengths.dtype  )
         return output_texts, seq_lengths





from __future__ import annotations

from fractions import Fraction







def lowerCamelCase__       (      _lowercase			,						_lowercase				):

     '''simple docstring'''


     return (
         num != den and num % 10 == den // 10 and (num // 10) / (den % 10) == num / den
     )







def lowerCamelCase__       (      _lowercase				):

     '''simple docstring'''


     UpperCAmelCase_      :							Optional[int]       =      []
     UpperCAmelCase_      :							Tuple       =      11
     UpperCAmelCase_      :							int       =      int('''1''' + '''0''' * digit_len				)
     for num in range(_lowercase			,						_lowercase				):
          while den <= 99:
               if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
                    if is_digit_cancelling(_lowercase			,						_lowercase				):
                         solutions.append(f'''{num}/{den}'''				)
               den += 1
          num += 1
          UpperCAmelCase_      :							Any       =      10
     return solutions







def lowerCamelCase__       (      _lowercase = 2				):

     '''simple docstring'''


     UpperCAmelCase_      :							Tuple       =      1.0
     for fraction in fraction_list(_lowercase				):
          UpperCAmelCase_      :							Optional[Any]       =      Fraction(_lowercase				)
          result *= frac.denominator / frac.numerator
     return int(_lowercase				)


if __name__ == "__main__":
     print(solution())