




import unittest

import numpy as np
import timeout_decorator  # noqa

from transformers import OPTConfig, is_flax_available
from transformers.testing_utils import require_flax, require_sentencepiece, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor


if is_flax_available():
   import os

   # The slow tests are often failing with OOM error on GPU
   # This makes JAX allocate exactly what is needed on demand, and deallocate memory that is no longer needed
   # but will be slower as stated here https://jax.readthedocs.io/en/latest/gpu_memory_allocation.html
   UpperCAmelCase__						:       List[str]							=    """platform"""

   import jax
   import jax.numpy as jnp

   from transformers import FlaxOPTForCausalLM, FlaxOPTModel, GPTaTokenizer


def 		_lowerCamelCase				(					UpperCamelCase					,     UpperCamelCase					,     UpperCamelCase=None					,     UpperCamelCase=None							)			->		Union[str, Any]:





  """simple docstring"""





  if attention_mask is None:
    _lowercase: Dict						     =    np.where(input_ids != config.pad_token_id					,     1					,     0							)
  return {
      "input_ids": input_ids,
      "attention_mask": attention_mask,
  }

@require_flax
class      UpperCAmelCase						:






   def __init__(	self   :       Optional[Any]						,					UpperCamelCase_   :       Optional[Any]						,					UpperCamelCase_   :       Optional[int]=13						,					UpperCamelCase_   :       str=7						,					UpperCamelCase_   :       Union[str, Any]=True						,					UpperCamelCase_   :       Tuple=False						,					UpperCamelCase_   :       Union[str, Any]=99						,					UpperCamelCase_   :       Optional[Any]=16						,					UpperCamelCase_   :       Optional[Any]=2						,					UpperCamelCase_   :       int=4						,					UpperCamelCase_   :       Optional[Any]=4						,					UpperCamelCase_   :       Dict="gelu"						,					UpperCamelCase_   :       List[Any]=0.1						,					UpperCamelCase_   :       Any=0.1						,					UpperCamelCase_   :       Union[str, Any]=20						,					UpperCamelCase_   :       str=2						,					UpperCamelCase_   :       Tuple=1						,					UpperCamelCase_   :       int=0						,					UpperCamelCase_   :       Tuple=16						,					UpperCamelCase_   :       List[str]=16						,					UpperCamelCase_   :       Tuple=0.02						,					)->			List[str]:
     _lowercase: Tuple						     =    parent
     _lowercase: int						     =    batch_size
     _lowercase: Dict						     =    seq_length
     _lowercase: Optional[int]						     =    is_training
     _lowercase: List[str]						     =    use_labels
     _lowercase: Optional[int]						     =    vocab_size
     _lowercase: Optional[Any]						     =    hidden_size
     _lowercase: str						     =    num_hidden_layers
     _lowercase: List[Any]						     =    num_attention_heads
     _lowercase: Any						     =    intermediate_size
     _lowercase: Any						     =    hidden_act
     _lowercase: List[Any]						     =    hidden_dropout_prob
     _lowercase: Optional[int]						     =    attention_probs_dropout_prob
     _lowercase: List[Any]						     =    max_position_embeddings
     _lowercase: Tuple						     =    eos_token_id
     _lowercase: Optional[int]						     =    pad_token_id
     _lowercase: Tuple						     =    bos_token_id
     _lowercase: int						     =    embed_dim
     _lowercase: Any						     =    word_embed_proj_dim
     _lowercase: str						     =    initializer_range
     _lowercase: Optional[Any]						     =    False






   def  _a						(	self   :       Dict     )->			Dict:
     _lowercase: Optional[Any]						     =    np.clip(ids_tensor([self.batch_size, self.seq_length - 1]						,					self.vocab_size     )						,					3						,					self.vocab_size     )
     _lowercase: int						     =    np.concatenate((input_ids, 2 * np.ones((self.batch_size, 1)						,					dtype=np.intaa     ))						,					-1     )

     _lowercase: Union[str, Any]						     =    OPTConfig(
         vocab_size=self.vocab_size						,					hidden_size=self.hidden_size						,					num_hidden_layers=self.num_hidden_layers						,					num_attention_heads=self.num_attention_heads						,					ffn_dim=self.intermediate_size						,					dropout=self.hidden_dropout_prob						,					attention_dropout=self.attention_probs_dropout_prob						,					max_position_embeddings=self.max_position_embeddings						,					eos_token_id=self.eos_token_id						,					bos_token_id=self.bos_token_id						,					pad_token_id=self.pad_token_id						,					embed_dim=self.embed_dim						,					is_encoder_decoder=lowerCamelCase__						,					word_embed_proj_dim=self.word_embed_proj_dim						,					initializer_range=self.initializer_range						,					use_cache=lowerCamelCase__						,					)
     _lowercase: Any						     =    prepare_opt_inputs_dict(lowerCamelCase__						,					lowerCamelCase__     )
     return config, inputs_dict






   def  _a						(	self   :       Dict     )->			Union[str, Any]:
     _lowercase: Optional[int]						     =    self.prepare_config_and_inputs()
     return config, inputs_dict






   def  _a						(	self   :       Any						,					UpperCamelCase_   :       Optional[int]						,					UpperCamelCase_   :       Dict						,					UpperCamelCase_   :       Optional[int]     )->			Dict:
     _lowercase: Tuple						     =    20
     _lowercase: Any						     =    model_class_name(lowerCamelCase__     )

     _lowercase: str						     =    inputs_dict["input_ids"]
     _lowercase: str						     =    inputs_dict["attention_mask"]

     _lowercase: List[str]						     =    model.init_cache(input_ids.shape[0]						,					lowerCamelCase__     )
     _lowercase: Optional[int]						     =    jnp.ones((input_ids.shape[0], max_length)						,					dtype='i4'     )

     _lowercase: Dict						     =    jnp.broadcast_to(
         jnp.arange(input_ids.shape[-1] - 1     )[None, :]						,					(input_ids.shape[0], input_ids.shape[-1] - 1)						,					)
     _lowercase: List[Any]						     =    model(
         input_ids[:, :-1]						,					attention_mask=lowerCamelCase__						,					past_key_values=lowerCamelCase__						,					position_ids=lowerCamelCase__						,					)

     _lowercase: Tuple						     =    jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]						,					dtype='i4'     )
     _lowercase: Any						     =    model(
         input_ids[:, -1:]						,					attention_mask=lowerCamelCase__						,					past_key_values=outputs_cache.past_key_values						,					position_ids=lowerCamelCase__						,					)

     _lowercase: str						     =    model(lowerCamelCase__     )

     _lowercase: List[str]						     =    np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])     )     )
     self.parent.assertTrue(diff < 1E-3						,					msg=f'Max diff is {diff}'     )






   def  _a						(	self   :       str						,					UpperCamelCase_   :       Dict						,					UpperCamelCase_   :       List[Any]						,					UpperCamelCase_   :       List[Any]     )->			Union[str, Any]:
     _lowercase: Optional[Any]						     =    20
     _lowercase: Tuple						     =    model_class_name(lowerCamelCase__     )

     _lowercase: Any						     =    (
         inputs_dict["input_ids"],
         inputs_dict["attention_mask"],
     )

     _lowercase: int						     =    jnp.concatenate(
         [
             attention_mask,
             jnp.zeros((attention_mask.shape[0], max_length - attention_mask.shape[1])     ),
         ]						,					axis=-1						,					)

     _lowercase: Tuple						     =    model.init_cache(input_ids.shape[0]						,					lowerCamelCase__     )
     _lowercase: str						     =    jnp.broadcast_to(
         jnp.arange(input_ids.shape[-1] - 1     )[None, :]						,					(input_ids.shape[0], input_ids.shape[-1] - 1)						,					)

     _lowercase: Optional[int]						     =    model(
         input_ids[:, :-1]						,					attention_mask=lowerCamelCase__						,					past_key_values=lowerCamelCase__						,					position_ids=lowerCamelCase__						,					)
     _lowercase: Tuple						     =    jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]						,					dtype='i4'     )
     _lowercase: Dict						     =    model(
         input_ids[:, -1:]						,					past_key_values=outputs_cache.past_key_values						,					attention_mask=lowerCamelCase__						,					position_ids=lowerCamelCase__						,					)

     _lowercase: int						     =    model(lowerCamelCase__						,					attention_mask=lowerCamelCase__     )

     _lowercase: List[Any]						     =    np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])     )     )
     self.parent.assertTrue(diff < 1E-3						,					msg=f'Max diff is {diff}'     )

@require_flax
class      UpperCAmelCase						(			_a   ,					unittest.TestCase   ,					_a   ):
   __SCREAMING_SNAKE_CASE		:							Optional[int]							=					(FlaxOPTModel, FlaxOPTForCausalLM) if is_flax_available() else ()
   __SCREAMING_SNAKE_CASE		:							Tuple							=					() if is_flax_available() else ()






   def  _a						(	self   :       Optional[Any]     )->			Optional[int]:
     _lowercase: List[Any]						     =    FlaxOPTModelTester(self     )






   def  _a						(	self   :       str     )->			str:
     _lowercase: Tuple						     =    self.model_tester.prepare_config_and_inputs()
     for model_class in self.all_model_classes:
       self.model_tester.check_use_cache_forward(lowerCamelCase__						,					lowerCamelCase__						,					lowerCamelCase__     )






   def  _a						(	self   :       Any     )->			List[Any]:
     _lowercase: int						     =    self.model_tester.prepare_config_and_inputs()
     for model_class in self.all_model_classes:
       self.model_tester.check_use_cache_forward_with_attn_mask(lowerCamelCase__						,					lowerCamelCase__						,					lowerCamelCase__     )






   @slow
   def  _a						(	self   :       Union[str, Any]     )->			List[Any]:
     for model_class_name in self.all_model_classes:
       _lowercase: Union[str, Any]						     =    model_class_name.from_pretrained('facebook/opt-125m'     )
       _lowercase: int						     =    np.ones((1, 1)     ) * model.config.eos_token_id
       _lowercase: Optional[int]						     =    model(lowerCamelCase__     )
       self.assertIsNotNone(lowerCamelCase__     )

@require_sentencepiece
@require_flax
class      UpperCAmelCase						(			unittest.TestCase   ):






   @slow
   def  _a						(	self   :       Any     )->			Union[str, Any]:
     _lowercase: List[Any]						     =    FlaxOPTModel.from_pretrained('facebook/opt-350m'     )
     _lowercase: Tuple						     =    jnp.array([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]     )
     _lowercase: int						     =    model(input_ids=lowerCamelCase__     ).last_hidden_state
     _lowercase: Optional[int]						     =    (1, 11, 5_12)
     self.assertEqual(output.shape						,					lowerCamelCase__     )
     _lowercase: List[Any]						     =    jnp.array(
         [[-0.2_867, -1.9_256, -0.3_062], [-1.2_711, -0.1_337, -0.1_897], [0.4_109, 0.1_187, -1.3_142]]     )
     self.assertTrue(jnp.allclose(output[:, :3, :3]						,					lowerCamelCase__						,					atol=4E-2     )     )

@require_flax
@slow
class      UpperCAmelCase						(			unittest.TestCase   ):






   def  _a						(	self   :       Optional[Any]     )->			int:
     super().setUp()
     _lowercase: Optional[Any]						     =    "facebook/opt-350m"






   def  _a						(	self   :       List[Any]     )->			Union[str, Any]:
     _lowercase: Union[str, Any]						     =    FlaxOPTForCausalLM.from_pretrained(self.path_model     )
     _lowercase: Dict						     =    GPTaTokenizer.from_pretrained(self.path_model     )

     _lowercase: List[Any]						     =    [
         "Today is a beautiful day and I want to",
         "In the city of",
         "Paris is the capital of France and",
         "Computers and mobile phones have taken",
     ]
     # verify that prompt without BOS token is identical to Metaseq -> add_special_tokens=False
     _lowercase: List[Any]						     =    tokenizer(lowerCamelCase__						,					return_tensors='jax'						,					padding=lowerCamelCase__						,					add_special_tokens=lowerCamelCase__     )
     _lowercase: List[Any]						     =    model(inputs.input_ids						,					attention_mask=inputs.attention_mask     )[0].mean(axis=-1     )
     _lowercase: Dict						     =    jnp.array(
         [
             [1.3_851, -13.8_923, -10.5_229, -10.7_533, -0.2_309, -10.2_384, -0.5_365, -9.0_947, -5.1_670],
             [-4.7_073, -10.6_276, -3.9_415, -21.5_242, -0.2_822, -0.2_822, -0.2_822, -0.2_822, -0.2_822],
             [0.6_247, -3.4_229, -8.9_179, -1.4_297, -14.1_650, 1.4_146, -9.0_218, -0.2_703, -0.2_703],
             [6.4_783, -1.9_913, -10.7_926, -2.3_336, 1.5_092, -0.9_974, -6.8_213, 1.3_477, 1.3_477],
         ]     )
     self.assertTrue(jnp.allclose(lowerCamelCase__						,					lowerCamelCase__						,					atol=4E-2     )     )

     _lowercase: Optional[Any]						     =    jax.jit(lowerCamelCase__     )
     _lowercase: List[Any]						     =    model(inputs.input_ids						,					attention_mask=inputs.attention_mask     )[0].mean(axis=-1     )
     self.assertTrue(jnp.allclose(lowerCamelCase__						,					lowerCamelCase__						,					atol=4E-2     )     )

@require_flax
@slow
class      UpperCAmelCase						(			unittest.TestCase   ):






   @property
   def  _a						(	self   :       Any     )->			str:
     return [
         "Today is a beautiful day and I want",
         "In the city of",
         "Paris is the capital of France and",
         "Computers and mobile phones have taken",
     ]






   def  _a						(	self   :       int     )->			Dict:
     _lowercase: List[Any]						     =    "facebook/opt-125m"

     _lowercase: Tuple						     =    [
         "Today is a beautiful day and I want to",
         "In the city of New York, the city",
         "Paris is the capital of France and the capital",
         "Computers and mobile phones have taken over the",
     ]

     _lowercase: Tuple						     =    []

     _lowercase: int						     =    FlaxOPTForCausalLM.from_pretrained(lowerCamelCase__     )
     _lowercase: int						     =    GPTaTokenizer.from_pretrained(lowerCamelCase__     )

     for prompt in self.prompts:
       _lowercase: Tuple						     =    tokenizer(lowerCamelCase__						,					return_tensors='jax'     ).input_ids

       _lowercase: Optional[Any]						     =    model.generate(lowerCamelCase__						,					max_length=10     )
       _lowercase: Optional[int]						     =    generated_ids[0]

       _lowercase: List[str]						     =    tokenizer.batch_decode(lowerCamelCase__						,					skip_special_tokens=lowerCamelCase__     )
       predicted_outputs += generated_string

     self.assertListEqual(lowerCamelCase__						,					lowerCamelCase__     )






   def  _a						(	self   :       Union[str, Any]     )->			Tuple:
     _lowercase: Union[str, Any]						     =    "facebook/opt-350m"

     _lowercase: List[str]						     =    [
         "Today is a beautiful day and I want to",
         "In the city of San Francisco, the city",
         "Paris is the capital of France and the capital",
         "Computers and mobile phones have taken over the",
     ]

     _lowercase: Tuple						     =    []
     _lowercase: List[Any]						     =    FlaxOPTForCausalLM.from_pretrained(lowerCamelCase__     )
     _lowercase: Any						     =    GPTaTokenizer.from_pretrained(lowerCamelCase__     )

     for prompt in self.prompts:
       _lowercase: Dict						     =    tokenizer(lowerCamelCase__						,					return_tensors='jax'     ).input_ids

       _lowercase: Dict						     =    model.generate(lowerCamelCase__						,					max_length=10     )
       _lowercase: str						     =    generated_ids[0]

       _lowercase: str						     =    tokenizer.batch_decode(lowerCamelCase__						,					skip_special_tokens=lowerCamelCase__     )
       predicted_outputs += generated_string

     self.assertListEqual(lowerCamelCase__						,					lowerCamelCase__     )






   def  _a						(	self   :       Optional[Any]     )->			str:
     _lowercase: Dict						     =    "facebook/opt-125m"
     _lowercase: Optional[Any]						     =    [
         "Today is a beautiful day and I want to thank",
         "In the city of Rome Canaver Canaver Canaver Canaver",
     ]
     _lowercase: int						     =    FlaxOPTForCausalLM.from_pretrained(lowerCamelCase__     )
     _lowercase: List[Any]						     =    GPTaTokenizer.from_pretrained(lowerCamelCase__     )
     _lowercase: List[str]						     =    tokenizer(
         [
             'Today is a beautiful day and I want to',
             'In the city of',
         ]						,					return_tensors='jax'						,					padding=lowerCamelCase__						,					)

     _lowercase: int						     =    jax.jit(model.generate     )

     _lowercase: str						     =    jit_generate(inputs['input_ids']						,					attention_mask=inputs['attention_mask']     ).sequences

     _lowercase: Optional[int]						     =    tokenizer.batch_decode(lowerCamelCase__						,					skip_special_tokens=lowerCamelCase__     )

     self.assertIsNotNone(lowerCamelCase__						,					lowerCamelCase__     )






   def  _a						(	self   :       List[Any]     )->			int:
     _lowercase: Tuple						     =    "facebook/opt-350m"

     _lowercase: List[Any]						     =    GPTaTokenizer.from_pretrained(lowerCamelCase__     )
     _lowercase: Union[str, Any]						     =    FlaxOPTForCausalLM.from_pretrained(lowerCamelCase__     )

     _lowercase: List[str]						     =    "left"

     # use different length sentences to test batching
     _lowercase: Tuple						     =    [
         "Hello, my dog is a little",
         "Today, I",
     ]

     _lowercase: int						     =    tokenizer(lowerCamelCase__						,					return_tensors='jax'						,					padding=lowerCamelCase__     )
     _lowercase: List[str]						     =    inputs["input_ids"]

     _lowercase: Tuple						     =    model.generate(input_ids=lowerCamelCase__						,					attention_mask=inputs['attention_mask']						,					trace=lowerCamelCase__     )

     _lowercase: List[Any]						     =    tokenizer(sentences[0]						,					return_tensors='jax'     ).input_ids
     _lowercase: Union[str, Any]						     =    model.generate(input_ids=lowerCamelCase__     )

     _lowercase: Tuple						     =    inputs_non_padded.shape[-1] - inputs["attention_mask"][-1].sum()
     _lowercase: Dict						     =    tokenizer(sentences[1]						,					return_tensors='jax'     ).input_ids
     _lowercase: int						     =    model.generate(input_ids=lowerCamelCase__						,					max_length=model.config.max_length - num_paddings     )

     _lowercase: List[str]						     =    tokenizer.batch_decode(outputs[0]						,					skip_special_tokens=lowerCamelCase__     )
     _lowercase: Tuple						     =    tokenizer.decode(output_non_padded[0][0]						,					skip_special_tokens=lowerCamelCase__     )
     _lowercase: Tuple						     =    tokenizer.decode(output_padded[0][0]						,					skip_special_tokens=lowerCamelCase__     )

     _lowercase: Union[str, Any]						     =    [
         "Hello, my dog is a little bit of a dork.\nI'm a little bit",
         "Today, I was in the middle of a conversation with a friend about the",
     ]
     self.assertListEqual(lowerCamelCase__						,					lowerCamelCase__     )
     self.assertListEqual(lowerCamelCase__						,					[non_padded_sentence, padded_sentence]     )






import math


def 		_lowerCamelCase				(					UpperCamelCase							)			->		int:





  """simple docstring"""





  return sum(int(UpperCamelCase							) for digit in str(UpperCamelCase							)							)


def 		_lowerCamelCase				(					UpperCamelCase = 30							)			->		int:





  """simple docstring"""





  _lowercase: int						     =    []
  for digit in range(2					,     100							):
    for power in range(2					,     100							):
      _lowercase: int						     =    int(math.pow(UpperCamelCase					,     UpperCamelCase							)							)
      if digit == digit_sum(UpperCamelCase							):
        digit_to_powers.append(UpperCamelCase							)

  digit_to_powers.sort()
  return digit_to_powers[n - 1]


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