"""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_						(					lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase=None					,			lowerCamelCase=None       ):
				if attention_mask is None:
								UpperCAmelCase__             =							tf.cast(tf.math.not_equal(lowerCAmelCase_					,			config.pad_token_id       )					,			tf.inta       )
				return {"input_ids": input_ids, "attention_mask": attention_mask}
@require_tf
class 				snake_case       :




					"""simple docstring"""

					snake_case__				    = OPTConfig
					snake_case__				    = {}
					snake_case__				    = "gelu"







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







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

									UpperCAmelCase__             =							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=lowerCAmelCase_    ,**self.config_updates    ,)
									UpperCAmelCase__             =							prepare_opt_inputs_dict(lowerCAmelCase_    ,lowerCAmelCase_			)
									return config, inputs_dict







					def 		__lowerCAmelCase						(				self   :	Any    ,lowerCamelCase__   :	Optional[Any]    ,lowerCamelCase__   :	Dict			):
									UpperCAmelCase__             =							TFOPTModel(config=lowerCAmelCase_			)
									UpperCAmelCase__             =							inputs_dict['input_ids']

									UpperCAmelCase__             =							input_ids[:1, :]
									UpperCAmelCase__             =							inputs_dict['attention_mask'][:1, :]
									UpperCAmelCase__             =							1

									# first forward pass
									UpperCAmelCase__             =							model(lowerCAmelCase_    ,attention_mask=lowerCAmelCase_    ,use_cache=lowerCAmelCase_			)

									UpperCAmelCase__      ,	UpperCAmelCase__             =							outputs.to_tuple()

									# create hypothetical next token and extent to next_input_ids
									UpperCAmelCase__             =							ids_tensor((self.batch_size, 3)    ,config.vocab_size			)
									UpperCAmelCase__             =							tf.cast(ids_tensor((self.batch_size, 3)    ,2			)    ,tf.inta			)

									# append to next input_ids and
									UpperCAmelCase__             =							tf.concat([input_ids, next_tokens]    ,axis=-1			)
									UpperCAmelCase__             =							tf.concat([attention_mask, next_attn_mask]    ,axis=-1			)

									UpperCAmelCase__             =							model(lowerCAmelCase_    ,attention_mask=lowerCAmelCase_			)[0]
									UpperCAmelCase__             =							model(lowerCAmelCase_    ,attention_mask=lowerCAmelCase_    ,past_key_values=lowerCAmelCase_			)[0]

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

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

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






@require_tf
class 				snake_case       ( _UpperCamelCase      ,    _UpperCamelCase      ,    unittest.TestCase				):




					"""simple docstring"""

					snake_case__				    = (TFOPTModel, TFOPTForCausalLM) if is_tf_available() else ()
					snake_case__				    = (TFOPTForCausalLM,) if is_tf_available() else ()
					snake_case__				    = (
					    {"feature-extraction": TFOPTModel, "text-generation": TFOPTForCausalLM} if is_tf_available() else {}
					)
					snake_case__				    = False
					snake_case__				    = False
					snake_case__				    = False
					snake_case__				    = 10







					def 		__lowerCAmelCase						(				self   :	List[str]			):
									UpperCAmelCase__             =							TFOPTModelTester(self			)
									UpperCAmelCase__             =							ConfigTester(self    ,config_class=lowerCAmelCase_			)







					def 		__lowerCAmelCase						(				self   :	Tuple			):
									self.config_tester.run_common_tests()







					def 		__lowerCAmelCase						(				self   :	Tuple			):
									UpperCAmelCase__             =							self.model_tester.prepare_config_and_inputs_for_common()
									self.model_tester.check_decoder_model_past_large_inputs(*lowerCAmelCase_			)







					def 		__lowerCAmelCase						(				self   :	Union[str, Any]			):
									UpperCAmelCase__      ,	UpperCAmelCase__             =							self.model_tester.prepare_config_and_inputs_for_common()

									def _get_word_embedding_weight(lowerCamelCase__   :	str    ,lowerCamelCase__   :	Union[str, Any]			):
													if hasattr(lowerCAmelCase_    ,'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(lowerCAmelCase_    ,'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__             =							model_class(config=lowerCAmelCase_			)
																	UpperCAmelCase__             =							_get_word_embedding_weight(lowerCAmelCase_    ,model.get_input_embeddings()			)
																	UpperCAmelCase__             =							_get_word_embedding_weight(lowerCAmelCase_    ,model.get_output_embeddings()			)

																	# reshape the embeddings
																	model.resize_token_embeddings(lowerCAmelCase_			)
																	UpperCAmelCase__             =							_get_word_embedding_weight(lowerCAmelCase_    ,model.get_input_embeddings()			)
																	UpperCAmelCase__             =							_get_word_embedding_weight(lowerCAmelCase_    ,model.get_output_embeddings()			)

																	# check that the resized embeddings size matches the desired size.
																	UpperCAmelCase__             =							size if size is not None else config.vocab_size

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

																	# check that weights remain the same after resizing
																	UpperCAmelCase__             =							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__             =							False
																	self.assertTrue(lowerCAmelCase_			)

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

																					UpperCAmelCase__             =							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__             =							False
																					self.assertTrue(lowerCAmelCase_			)






def   a_						(					lowerCamelCase       ):
				return tf.constant(lowerCAmelCase_					,			dtype=tf.intaa       )
@require_tf
class 				snake_case       ( unittest.TestCase				):




					"""simple docstring"""

					snake_case__				    = 99







					def 		__lowerCAmelCase						(				self   :	Optional[int]			):
									UpperCAmelCase__             =							tf.ones((4, 1)    ,dtype=tf.intaa			) * 2
									UpperCAmelCase__             =							tf.concat([ids_tensor((4, 6)    ,self.vocab_size - 3			) + 3, eos_column_vector]    ,axis=1			)
									UpperCAmelCase__             =							input_ids.shape[0]
									UpperCAmelCase__             =							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 				snake_case       ( unittest.TestCase				):




					"""simple docstring"""

					@slow
					def 		__lowerCAmelCase						(				self   :	str			):
									UpperCAmelCase__             =							TFOPTModel.from_pretrained('facebook/opt-350m'			)
									UpperCAmelCase__             =							_long_tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]			)
									UpperCAmelCase__             =							tf.not_equal(lowerCAmelCase_    ,model.config.pad_token_id			)
									with tf.GradientTape():
													UpperCAmelCase__             =							model(input_ids=lowerCAmelCase_    ,attention_mask=lowerCAmelCase_			).last_hidden_state
									UpperCAmelCase__             =							(1, 11, 512)
									self.assertEqual(output.shape    ,lowerCAmelCase_			)
									UpperCAmelCase__             =							tf.constant(
									    [[-0.2_8_7_3, -1.9_2_1_8, -0.3_0_3_3], [-1.2_7_1_0, -0.1_3_3_8, -0.1_9_0_2], [0.4_0_9_5, 0.1_2_1_4, -1.3_1_2_1]]			)
									self.assertTrue(np.allclose(output[:, :3, :3]    ,lowerCAmelCase_    ,atol=4e-3			)			)

									UpperCAmelCase__             =							tf.function(lowerCAmelCase_    ,jit_compile=lowerCAmelCase_			)
									UpperCAmelCase__             =							xla_generate(lowerCAmelCase_    ,lowerCAmelCase_			)[0]
									self.assertTrue(np.allclose(output[:, :3, :3]    ,lowerCAmelCase_    ,atol=4e-2			)			)






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




					"""simple docstring"""

					def 		__lowerCAmelCase						(				self   :	int			):
									super().setUp()
									UpperCAmelCase__             =							'facebook/opt-350m'







					def 		__lowerCAmelCase						(				self   :	Dict			):
									UpperCAmelCase__             =							TFOPTForCausalLM.from_pretrained(self.path_model			)
									UpperCAmelCase__             =							GPTaTokenizer.from_pretrained(self.path_model			)

									UpperCAmelCase__             =							[
									    '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__             =							tokenizer(lowerCAmelCase_    ,return_tensors='tf'    ,padding=lowerCAmelCase_    ,add_special_tokens=lowerCAmelCase_			)
									UpperCAmelCase__             =							tf.math.reduce_mean(model(inputs.input_ids    ,attention_mask=inputs.attention_mask			)[0]    ,axis=-1			)
									UpperCAmelCase__             =							tf.constant(
									    [
									        [1.3_8_5_1, -1_3.8_9_2_3, -1_0.5_2_2_9, -1_0.7_5_3_3, -0.2_3_0_9, -1_0.2_3_8_4, -0.5_3_6_5, -9.0_9_4_7, -5.1_6_7_0],
									        [-4.7_0_7_3, -1_0.6_2_7_6, -3.9_4_1_5, -2_1.5_2_4_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2, -0.2_8_2_2],
									        [0.6_2_4_7, -3.4_2_2_9, -8.9_1_7_9, -1.4_2_9_7, -1_4.1_6_5_0, 1.4_1_4_6, -9.0_2_1_8, -0.2_7_0_3, -0.2_7_0_3],
									        [6.4_7_8_3, -1.9_9_1_3, -1_0.7_9_2_6, -2.3_3_3_6, 1.5_0_9_2, -0.9_9_7_4, -6.8_2_1_3, 1.3_4_7_7, 1.3_4_7_7],
									    ]			)
									self.assertTrue(np.allclose(lowerCAmelCase_    ,lowerCAmelCase_    ,atol=1e-4			)			)

									UpperCAmelCase__             =							tf.function(lowerCAmelCase_    ,jit_compile=lowerCAmelCase_			)
									UpperCAmelCase__             =							tf.math.reduce_mean(xla_generate(inputs.input_ids    ,attention_mask=inputs.attention_mask			)[0]    ,axis=-1			)
									self.assertTrue(np.allclose(lowerCAmelCase_    ,lowerCAmelCase_    ,atol=1e-4			)			)






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




					"""simple docstring"""

					@property
					def 		__lowerCAmelCase						(				self   :	Optional[int]			):
									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   :	int			):
									UpperCAmelCase__             =							'facebook/opt-125m'

									UpperCAmelCase__             =							[
									    '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__             =							[]
									UpperCAmelCase__             =							GPTaTokenizer.from_pretrained(lowerCAmelCase_			)
									UpperCAmelCase__             =							TFOPTForCausalLM.from_pretrained(lowerCAmelCase_			)

									for prompt in self.prompts:
													UpperCAmelCase__             =							tokenizer(lowerCAmelCase_    ,return_tensors='tf'			).input_ids

													UpperCAmelCase__             =							model.generate(lowerCAmelCase_    ,max_length=10			)

													UpperCAmelCase__             =							tokenizer.batch_decode(lowerCAmelCase_    ,skip_special_tokens=lowerCAmelCase_			)
													predicted_outputs += generated_string

									self.assertListEqual(lowerCAmelCase_    ,lowerCAmelCase_			)







					def 		__lowerCAmelCase						(				self   :	Optional[Any]			):
									UpperCAmelCase__             =							'facebook/opt-350m'

									UpperCAmelCase__             =							GPTaTokenizer.from_pretrained(lowerCAmelCase_			)
									UpperCAmelCase__             =							TFOPTForCausalLM.from_pretrained(lowerCAmelCase_			)

									UpperCAmelCase__             =							'left'

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

									UpperCAmelCase__             =							tokenizer(lowerCAmelCase_    ,return_tensors='tf'    ,padding=lowerCAmelCase_			)
									UpperCAmelCase__             =							inputs['input_ids']

									UpperCAmelCase__             =							model.generate(input_ids=lowerCAmelCase_    ,attention_mask=inputs['attention_mask']			)

									UpperCAmelCase__             =							tokenizer(sentences[0]    ,return_tensors='tf'			).input_ids
									UpperCAmelCase__             =							model.generate(input_ids=lowerCAmelCase_			)

									UpperCAmelCase__             =							inputs_non_padded.shape[-1] - tf.math.reduce_sum(
									    tf.cast(inputs['attention_mask'][-1]    ,tf.intaa			)			)
									UpperCAmelCase__             =							tokenizer(sentences[1]    ,return_tensors='tf'			).input_ids
									UpperCAmelCase__             =							model.generate(input_ids=lowerCAmelCase_    ,max_length=model.config.max_length - num_paddings			)

									UpperCAmelCase__             =							tokenizer.batch_decode(lowerCAmelCase_    ,skip_special_tokens=lowerCAmelCase_			)
									UpperCAmelCase__             =							tokenizer.decode(output_non_padded[0]    ,skip_special_tokens=lowerCAmelCase_			)
									UpperCAmelCase__             =							tokenizer.decode(output_padded[0]    ,skip_special_tokens=lowerCAmelCase_			)

									UpperCAmelCase__             =							[
									    '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]			)







					def 		__lowerCAmelCase						(				self   :	List[Any]			):
									UpperCAmelCase__             =							'facebook/opt-350m'

									UpperCAmelCase__             =							[
									    '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__             =							[]
									UpperCAmelCase__             =							GPTaTokenizer.from_pretrained(lowerCAmelCase_			)
									UpperCAmelCase__             =							TFOPTForCausalLM.from_pretrained(lowerCAmelCase_			)

									for prompt in self.prompts:
													UpperCAmelCase__             =							tokenizer(lowerCAmelCase_    ,return_tensors='tf'			).input_ids

													UpperCAmelCase__             =							model.generate(lowerCAmelCase_    ,max_length=10			)

													UpperCAmelCase__             =							tokenizer.batch_decode(lowerCAmelCase_    ,skip_special_tokens=lowerCAmelCase_			)
													predicted_outputs += generated_string

									self.assertListEqual(lowerCAmelCase_    ,lowerCAmelCase_			)










from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


_snake_case  :    List[Any]						    =  {
    'configuration_biogpt': ['BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP', 'BioGptConfig'],
    'tokenization_biogpt': ['BioGptTokenizer'],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Optional[int]						    =  [
         'BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST',
         'BioGptForCausalLM',
         'BioGptForTokenClassification',
         'BioGptForSequenceClassification',
         'BioGptModel',
         'BioGptPreTrainedModel',
     ]


if TYPE_CHECKING:
     from .configuration_biogpt import BIOGPT_PRETRAINED_CONFIG_ARCHIVE_MAP, BioGptConfig
     from .tokenization_biogpt import BioGptTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_biogpt import (
              BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST,
              BioGptForCausalLM,
              BioGptForSequenceClassification,
              BioGptForTokenClassification,
              BioGptModel,
              BioGptPreTrainedModel,
          )


else:
     import sys

     _snake_case  :    List[str]						    =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

