


'''simple docstring'''



from __future__ import annotations

import unittest

import numpy as np

from transformers import OPTConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
      import tensorflow as tf

      from transformers import GPTaTokenizer, TFOPTForCausalLM, TFOPTModel




def 			_a			(		_lowercase      :			Tuple    ,      _lowercase      :			Dict    ,      _lowercase      :			Optional[Any]=None    ,      _lowercase      :			Dict=None						):





     '''simple docstring'''






     if attention_mask is None:
          __UpperCAmelCase			:				Dict													=    tf.cast(tf.math.not_equal(_lowercase    ,      config.pad_token_id						)    ,      tf.inta						)
     return {"input_ids": input_ids, "attention_mask": attention_mask}



@require_tf
class 			a       :


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Union[str, Any]							     =     OPTConfig
     SCREAMING_SNAKE_CASE :				str							     =     {}
     SCREAMING_SNAKE_CASE :				Union[str, Any]							     =     "gelu"

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

     def 				lowerCamelCase__				(       self  :	Any    )		->							Any:
          __UpperCAmelCase			:				List[Any]													=    ids_tensor([self.batch_size, self.seq_length - 1]							,				self.vocab_size    )
          __UpperCAmelCase			:				str													=    tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size    )							,				1    )
          __UpperCAmelCase			:				List[str]													=    tf.concat([input_ids, eos_tensor]							,				axis=1    )

          __UpperCAmelCase			:				str													=    self.config_cls(
              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							,				word_embed_proj_dim=self.word_embed_proj_dim							,				is_encoder_decoder=snake_case							,				**self.config_updates							,				)
          __UpperCAmelCase			:				Any													=    prepare_opt_inputs_dict(snake_case							,				snake_case    )
          return config, inputs_dict






     def 				lowerCamelCase__				(       self  :	Any							,				snake_case  :	List[str]							,				snake_case  :	Optional[Any]    )		->							Dict:
          __UpperCAmelCase			:				Optional[Any]													=    TFOPTModel(config=snake_case    )
          __UpperCAmelCase			:				str													=    inputs_dict['''input_ids''']

          __UpperCAmelCase			:				int													=    input_ids[:1, :]
          __UpperCAmelCase			:				Union[str, Any]													=    inputs_dict['''attention_mask'''][:1, :]
          __UpperCAmelCase			:				Optional[int]													=    1

          # first forward pass
          __UpperCAmelCase			:				Any													=    model(snake_case							,				attention_mask=snake_case							,				use_cache=snake_case    )

          __UpperCAmelCase							,     __UpperCAmelCase			:				str													=    outputs.to_tuple()

          # create hypothetical next token and extent to next_input_ids
          __UpperCAmelCase			:				Union[str, Any]													=    ids_tensor((self.batch_size, 3)							,				config.vocab_size    )
          __UpperCAmelCase			:				Union[str, Any]													=    tf.cast(ids_tensor((self.batch_size, 3)							,				2    )							,				tf.inta    )

          # append to next input_ids and
          __UpperCAmelCase			:				Optional[int]													=    tf.concat([input_ids, next_tokens]							,				axis=-1    )
          __UpperCAmelCase			:				Optional[Any]													=    tf.concat([attention_mask, next_attn_mask]							,				axis=-1    )

          __UpperCAmelCase			:				int													=    model(snake_case							,				attention_mask=snake_case    )[0]
          __UpperCAmelCase			:				Optional[Any]													=    model(snake_case							,				attention_mask=snake_case							,				past_key_values=snake_case    )[0]

          self.parent.assertEqual(next_tokens.shape[1]							,				output_from_past.shape[1]    )

          # select random slice
          __UpperCAmelCase			:				Optional[Any]													=    int(ids_tensor((1,)							,				output_from_past.shape[-1]    )    )
          __UpperCAmelCase			:				List[Any]													=    output_from_no_past[:, -3:, random_slice_idx]
          __UpperCAmelCase			:				Optional[Any]													=    output_from_past[:, :, random_slice_idx]

          # test that outputs are equal for slice
          tf.debugging.assert_near(snake_case							,				snake_case							,				rtol=1E-3    )



@require_tf
class 			a       (	_a			,						_a			,						unittest.TestCase ):


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Optional[int]							     =     (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
     SCREAMING_SNAKE_CASE :				Dict							     =     (TFOPTForCausalLM,) if is_tf_available() else ()
     SCREAMING_SNAKE_CASE :				Any							     =     (
         {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
     )
     SCREAMING_SNAKE_CASE :				Optional[int]							     =     False
     SCREAMING_SNAKE_CASE :				str							     =     False
     SCREAMING_SNAKE_CASE :				Tuple							     =     False
     SCREAMING_SNAKE_CASE :				str							     =     1_0

     def 				lowerCamelCase__				(       self  :	int    )		->							str:
          __UpperCAmelCase			:				Dict													=    TFOPTModelTester(self    )
          __UpperCAmelCase			:				int													=    ConfigTester(self							,				config_class=snake_case    )

     def 				lowerCamelCase__				(       self  :	Tuple    )		->							Any:
          self.config_tester.run_common_tests()

     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							List[str]:
          __UpperCAmelCase			:				Union[str, Any]													=    self.model_tester.prepare_config_and_inputs_for_common()
          self.model_tester.check_decoder_model_past_large_inputs(*snake_case    )






     def 				lowerCamelCase__				(       self  :	Any    )		->							Tuple:
          __UpperCAmelCase							,     __UpperCAmelCase			:				Any													=    self.model_tester.prepare_config_and_inputs_for_common()

          def _get_word_embedding_weight(snake_case  :	Optional[int]							,				snake_case  :	int    ):
               if hasattr(snake_case							,				'''weight'''    ):
                    return embedding_layer.weight
               else:
                    # Here we build the word embeddings weights if not exists.
                    # And then we retry to get the attribute once built.
                    model.build()
                    if hasattr(snake_case							,				'''weight'''    ):
                         return embedding_layer.weight
                    else:
                         return None

          for model_class in self.all_model_classes:
               for size in [config.vocab_size - 10, config.vocab_size + 10]:
                    # build the embeddings
                    __UpperCAmelCase			:				str													=    model_class(config=snake_case    )
                    __UpperCAmelCase			:				Tuple													=    _get_word_embedding_weight(snake_case							,				model.get_input_embeddings()    )
                    __UpperCAmelCase			:				int													=    _get_word_embedding_weight(snake_case							,				model.get_output_embeddings()    )

                    # reshape the embeddings
                    model.resize_token_embeddings(snake_case    )
                    __UpperCAmelCase			:				str													=    _get_word_embedding_weight(snake_case							,				model.get_input_embeddings()    )
                    __UpperCAmelCase			:				List[str]													=    _get_word_embedding_weight(snake_case							,				model.get_output_embeddings()    )

                    # check that the resized embeddings size matches the desired size.
                    __UpperCAmelCase			:				List[Any]													=    size if size is not None else config.vocab_size

                    self.assertEqual(new_input_embeddings.shape[0]							,				snake_case    )

                    # check that weights remain the same after resizing
                    __UpperCAmelCase			:				str													=    True
                    for pa, pa in zip(old_input_embeddings.value()							,				new_input_embeddings.value()    ):
                         if tf.math.reduce_sum(tf.math.abs(pa - pa    )    ) > 0:
                              __UpperCAmelCase			:				Optional[Any]													=    False
                    self.assertTrue(snake_case    )

                    if old_output_embeddings is not None and new_output_embeddings is not None:
                         self.assertEqual(new_output_embeddings.shape[0]							,				snake_case    )

                         __UpperCAmelCase			:				Union[str, Any]													=    True
                         for pa, pa in zip(old_output_embeddings.value()							,				new_output_embeddings.value()    ):
                              if tf.math.reduce_sum(tf.math.abs(pa - pa    )    ) > 0:
                                   __UpperCAmelCase			:				Dict													=    False
                         self.assertTrue(snake_case    )




def 			_a			(		_lowercase      :			Union[str, Any]						):





     '''simple docstring'''






     return tf.constant(_lowercase    ,      dtype=tf.intaa						)



@require_tf
class 			a       (	unittest.TestCase ):


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Any							     =     9_9

     def 				lowerCamelCase__				(       self  :	List[str]    )		->							Dict:
          __UpperCAmelCase			:				str													=    tf.ones((4, 1)							,				dtype=tf.intaa    ) * 2
          __UpperCAmelCase			:				Union[str, Any]													=    tf.concat([ids_tensor((4, 6)							,				self.vocab_size - 3    ) + 3, eos_column_vector]							,				axis=1    )
          __UpperCAmelCase			:				Optional[Any]													=    input_ids.shape[0]
          __UpperCAmelCase			:				Optional[int]													=    OPTConfig(
              vocab_size=self.vocab_size							,				hidden_size=24							,				num_hidden_layers=2							,				num_attention_heads=2							,				ffn_dim=32							,				max_position_embeddings=48							,				eos_token_id=2							,				pad_token_id=1							,				bos_token_id=0							,				)
          return config, input_ids, batch_size



@require_sentencepiece
@require_tf
class 			a       (	unittest.TestCase ):


     """simple docstring"""






     @slow
     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							str:
          __UpperCAmelCase			:				Union[str, Any]													=    TFOPTModel.from_pretrained('''facebook/opt-350m'''    )
          __UpperCAmelCase			:				Any													=    _long_tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]    )
          __UpperCAmelCase			:				int													=    tf.not_equal(snake_case							,				model.config.pad_token_id    )
          with tf.GradientTape():
               __UpperCAmelCase			:				Union[str, Any]													=    model(input_ids=snake_case							,				attention_mask=snake_case    ).last_hidden_state
          __UpperCAmelCase			:				Dict													=    (1, 11, 512)
          self.assertEqual(output.shape							,				snake_case    )
          __UpperCAmelCase			:				Union[str, Any]													=    tf.constant(
              [[-0.2_873, -1.9_218, -0.3_033], [-1.2_710, -0.1_338, -0.1_902], [0.4_095, 0.1_214, -1.3_121]]    )
          self.assertTrue(np.allclose(output[:, :3, :3]							,				snake_case							,				atol=4E-3    )    )

          __UpperCAmelCase			:				List[Any]													=    tf.function(snake_case							,				jit_compile=snake_case    )
          __UpperCAmelCase			:				Any													=    xla_generate(snake_case							,				snake_case    )[0]
          self.assertTrue(np.allclose(output[:, :3, :3]							,				snake_case							,				atol=4E-2    )    )



@require_tf
@slow
class 			a       (	unittest.TestCase ):


     """simple docstring"""






     def 				lowerCamelCase__				(       self  :	Dict    )		->							List[Any]:
          super().setUp()
          __UpperCAmelCase			:				List[Any]													=    '''facebook/opt-350m'''






     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							Optional[int]:
          __UpperCAmelCase			:				Union[str, Any]													=    TFOPTForCausalLM.from_pretrained(self.path_model    )
          __UpperCAmelCase			:				List[str]													=    GPTaTokenizer.from_pretrained(self.path_model    )

          __UpperCAmelCase			:				Optional[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
          __UpperCAmelCase			:				Optional[Any]													=    tokenizer(snake_case							,				return_tensors='''tf'''							,				padding=snake_case							,				add_special_tokens=snake_case    )
          __UpperCAmelCase			:				Any													=    tf.math.reduce_mean(model(inputs.input_ids							,				attention_mask=inputs.attention_mask    )[0]							,				axis=-1    )
          __UpperCAmelCase			:				Any													=    tf.constant(
              [
                  [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(np.allclose(snake_case							,				snake_case							,				atol=1E-4    )    )

          __UpperCAmelCase			:				str													=    tf.function(snake_case							,				jit_compile=snake_case    )
          __UpperCAmelCase			:				Optional[int]													=    tf.math.reduce_mean(xla_generate(inputs.input_ids							,				attention_mask=inputs.attention_mask    )[0]							,				axis=-1    )
          self.assertTrue(np.allclose(snake_case							,				snake_case							,				atol=1E-4    )    )



@require_tf
@slow
class 			a       (	unittest.TestCase ):


     """simple docstring"""






     @property
     def 				lowerCamelCase__				(       self  :	int    )		->							Dict:
          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 				lowerCamelCase__				(       self  :	Any    )		->							Tuple:
          __UpperCAmelCase			:				List[Any]													=    '''facebook/opt-125m'''

          __UpperCAmelCase			:				str													=    [
              '''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''',
          ]

          __UpperCAmelCase			:				Dict													=    []
          __UpperCAmelCase			:				List[str]													=    GPTaTokenizer.from_pretrained(snake_case    )
          __UpperCAmelCase			:				Optional[Any]													=    TFOPTForCausalLM.from_pretrained(snake_case    )

          for prompt in self.prompts:
               __UpperCAmelCase			:				List[Any]													=    tokenizer(snake_case							,				return_tensors='''tf'''    ).input_ids

               __UpperCAmelCase			:				str													=    model.generate(snake_case							,				max_length=10    )

               __UpperCAmelCase			:				int													=    tokenizer.batch_decode(snake_case							,				skip_special_tokens=snake_case    )
               predicted_outputs += generated_string

          self.assertListEqual(snake_case							,				snake_case    )

     def 				lowerCamelCase__				(       self  :	List[Any]    )		->							Any:
          __UpperCAmelCase			:				Union[str, Any]													=    '''facebook/opt-350m'''

          __UpperCAmelCase			:				List[Any]													=    GPTaTokenizer.from_pretrained(snake_case    )
          __UpperCAmelCase			:				List[str]													=    TFOPTForCausalLM.from_pretrained(snake_case    )

          __UpperCAmelCase			:				Dict													=    '''left'''

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

          __UpperCAmelCase			:				Any													=    tokenizer(snake_case							,				return_tensors='''tf'''							,				padding=snake_case    )
          __UpperCAmelCase			:				List[Any]													=    inputs['''input_ids''']

          __UpperCAmelCase			:				Dict													=    model.generate(input_ids=snake_case							,				attention_mask=inputs['''attention_mask''']    )

          __UpperCAmelCase			:				int													=    tokenizer(sentences[0]							,				return_tensors='''tf'''    ).input_ids
          __UpperCAmelCase			:				str													=    model.generate(input_ids=snake_case    )

          __UpperCAmelCase			:				Optional[Any]													=    inputs_non_padded.shape[-1] - tf.math.reduce_sum(
              tf.cast(inputs['''attention_mask'''][-1]							,				tf.intaa    )    )
          __UpperCAmelCase			:				List[str]													=    tokenizer(sentences[1]							,				return_tensors='''tf'''    ).input_ids
          __UpperCAmelCase			:				List[str]													=    model.generate(input_ids=snake_case							,				max_length=model.config.max_length - num_paddings    )

          __UpperCAmelCase			:				List[str]													=    tokenizer.batch_decode(snake_case							,				skip_special_tokens=snake_case    )
          __UpperCAmelCase			:				Tuple													=    tokenizer.decode(output_non_padded[0]							,				skip_special_tokens=snake_case    )
          __UpperCAmelCase			:				List[Any]													=    tokenizer.decode(output_padded[0]							,				skip_special_tokens=snake_case    )

          __UpperCAmelCase			:				str													=    [
              '''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(snake_case							,				snake_case    )
          self.assertListEqual(snake_case							,				[non_padded_sentence, padded_sentence]    )






     def 				lowerCamelCase__				(       self  :	Optional[int]    )		->							List[str]:
          __UpperCAmelCase			:				Union[str, Any]													=    '''facebook/opt-350m'''

          __UpperCAmelCase			:				Union[str, Any]													=    [
              '''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''',
          ]

          __UpperCAmelCase			:				Optional[Any]													=    []
          __UpperCAmelCase			:				List[Any]													=    GPTaTokenizer.from_pretrained(snake_case    )
          __UpperCAmelCase			:				List[str]													=    TFOPTForCausalLM.from_pretrained(snake_case    )

          for prompt in self.prompts:
               __UpperCAmelCase			:				int													=    tokenizer(snake_case							,				return_tensors='''tf'''    ).input_ids

               __UpperCAmelCase			:				Any													=    model.generate(snake_case							,				max_length=10    )

               __UpperCAmelCase			:				str													=    tokenizer.batch_decode(snake_case							,				skip_special_tokens=snake_case    )
               predicted_outputs += generated_string

          self.assertListEqual(snake_case							,				snake_case    )



'''simple docstring'''



from __future__ import annotations




def 			_a			(		_lowercase      :			int						):





     '''simple docstring'''






     __UpperCAmelCase			:				str													=    [True] * limit
     __UpperCAmelCase			:				Tuple													=    False
     __UpperCAmelCase			:				List[str]													=    False
     __UpperCAmelCase			:				List[Any]													=    True

     for i in range(3    ,      int(limit**0.5 + 1						)    ,      2						):
          __UpperCAmelCase			:				Dict													=    i * 2
          while index < limit:
               __UpperCAmelCase			:				Optional[int]													=    False
               __UpperCAmelCase			:				Union[str, Any]													=    index + i

     __UpperCAmelCase			:				Optional[Any]													=    [2]

     for i in range(3    ,      _lowercase    ,      2						):
          if is_prime[i]:
               primes.append(_lowercase						)

     return primes




def 			_a			(		_lowercase      :			int = 1000000						):





     '''simple docstring'''






     __UpperCAmelCase			:				List[str]													=    prime_sieve(_lowercase						)
     __UpperCAmelCase			:				Optional[Any]													=    0
     __UpperCAmelCase			:				int													=    0

     for i in range(len(_lowercase						)						):
          for j in range(i + length    ,      len(_lowercase						)						):
               __UpperCAmelCase			:				Optional[Any]													=    sum(primes[i:j]						)
               if sol >= ceiling:
                    break

               if sol in primes:
                    __UpperCAmelCase			:				Any													=    j - i
                    __UpperCAmelCase			:				List[Any]													=    sol

     return largest


if __name__ == "__main__":
      print(f"""{solution() = }""")