




"""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      __snake_case (				_lowercase			,_lowercase			,_lowercase=None			,_lowercase=None    ):


    """simple docstring"""







    if attention_mask is None:
        UpperCamelCase              =	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 				snake_case_			:







   """simple docstring"""




   A_				      =       OPTConfig
   A_				      =       {}
   A_				      =       '''gelu'''







   def __init__(		self       ,					lowerCamelCase_       ,					lowerCamelCase_=1_3       ,					lowerCamelCase_=7       ,					lowerCamelCase_=True       ,					lowerCamelCase_=False       ,					lowerCamelCase_=9_9       ,					lowerCamelCase_=1_6       ,					lowerCamelCase_=2       ,					lowerCamelCase_=4       ,					lowerCamelCase_=4       ,					lowerCamelCase_="gelu"       ,					lowerCamelCase_=0.1       ,					lowerCamelCase_=0.1       ,					lowerCamelCase_=2_0       ,					lowerCamelCase_=2       ,					lowerCamelCase_=1       ,					lowerCamelCase_=0       ,					lowerCamelCase_=1_6       ,					lowerCamelCase_=1_6       ,					)				->   Union[str, Any]:
       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   UpperCAmelCase__			(		self)				->   Any:
       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   UpperCAmelCase__			(		self       ,					lowerCamelCase_       ,					lowerCamelCase_)				->   Any:
       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_			(					lowerCamelCase_	, lowerCamelCase_	, unittest.TestCase			):







   """simple docstring"""




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







   def   UpperCAmelCase__			(		self)				->   List[Any]:
       UpperCamelCase              =	TFOPTModelTester(self)
       UpperCamelCase              =	ConfigTester(self       ,					config_class=lowerCamelCase_)







   def   UpperCAmelCase__			(		self)				->   Union[str, Any]:
       self.config_tester.run_common_tests()







   def   UpperCAmelCase__			(		self)				->   Any:
       UpperCamelCase              =	self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_decoder_model_past_large_inputs(*lowerCamelCase_)







   def   UpperCAmelCase__			(		self)				->   str:
       UpperCamelCase       ,	UpperCamelCase              =	self.model_tester.prepare_config_and_inputs_for_common()

       def _get_word_embedding_weight(lowerCamelCase_       ,					lowerCamelCase_):
           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 - 1_0, config.vocab_size + 1_0]:
               # 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      __snake_case (				_lowercase    ):


    """simple docstring"""







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

@require_tf
class 				snake_case_			(					unittest.TestCase			):







   """simple docstring"""




   A_				      =       99







   def   UpperCAmelCase__			(		self)				->   Tuple:
       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=2_4       ,					num_hidden_layers=2       ,					num_attention_heads=2       ,					ffn_dim=3_2       ,					max_position_embeddings=4_8       ,					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   UpperCAmelCase__			(		self)				->   Any:
       UpperCamelCase              =	TFOPTModel.from_pretrained('''facebook/opt-350m''')
       UpperCamelCase              =	_long_tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 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, 1_1, 5_1_2)
       self.assertEqual(output.shape       ,					lowerCamelCase_)
       UpperCamelCase              =	tf.constant(
           [[-0.2873, -1.9218, -0.3033], [-1.2710, -0.1338, -0.1902], [0.4095, 0.1214, -1.3121]])
       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   UpperCAmelCase__			(		self)				->   Union[str, Any]:
       super().setUp()
       UpperCamelCase              =	'''facebook/opt-350m'''







   def   UpperCAmelCase__			(		self)				->   int:
       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.3851, -13.8923, -10.5229, -10.7533, -0.2309, -10.2384, -0.5365, -9.0947, -5.1670],
               [-4.7073, -10.6276, -3.9415, -21.5242, -0.2822, -0.2822, -0.2822, -0.2822, -0.2822],
               [0.6247, -3.4229, -8.9179, -1.4297, -14.1650, 1.4146, -9.0218, -0.2703, -0.2703],
               [6.4783, -1.9913, -10.7926, -2.3336, 1.5092, -0.9974, -6.8213, 1.3477, 1.3477],
           ])
       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   UpperCAmelCase__			(		self)				->   Tuple:
       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   UpperCAmelCase__			(		self)				->   Union[str, Any]:
       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=1_0)

           UpperCamelCase              =	tokenizer.batch_decode(lowerCamelCase_       ,					skip_special_tokens=lowerCamelCase_)
           predicted_outputs += generated_string

       self.assertListEqual(lowerCamelCase_       ,					lowerCamelCase_)







   def   UpperCAmelCase__			(		self)				->   Tuple:
       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   UpperCAmelCase__			(		self)				->   Union[str, 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=1_0)

           UpperCamelCase              =	tokenizer.batch_decode(lowerCamelCase_       ,					skip_special_tokens=lowerCamelCase_)
           predicted_outputs += generated_string

       self.assertListEqual(lowerCamelCase_       ,					lowerCamelCase_)






'''simple docstring'''




import unittest

from transformers.testing_utils import require_bsa
from transformers.utils import is_bsa_available

from ...test_feature_extraction_common import FeatureExtractionSavingTestMixin


if is_bsa_available():
							from transformers import MarkupLMFeatureExtractor




class      UpperCamelCase__					(					unittest.TestCase			):




					"""simple docstring"""


					def __init__(						self				:			str      ,						lowerCamelCase_				:			str						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				List[str]		=			parent



					def lowerCamelCase_			(						self				:			Union[str, Any]						):

							'''simple docstring'''



							return {}







def 		__A		(  ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE :				Any		=			"""<HTML>

    <HEAD>
    <TITLE>sample document</TITLE>
    </HEAD>

    <BODY BGCOLOR=\"FFFFFF\">
    <HR>
    <a href=\"http://google.com\">Goog</a>
    <H1>This is one header</H1>
    <H2>This is a another Header</H2>
    <P>Travel from
        <P>
        <B>SFO to JFK</B>
        <BR>
        <B><I>on May 2, 2015 at 2:00 pm. For details go to confirm.com </I></B>
        <HR>
        <div style=\"color:#0000FF\">
            <h3>Traveler <b> name </b> is
            <p> John Doe </p>
        </div>"""

		SCREAMING_SNAKE_CASE :				int		=			"""
    <!DOCTYPE html>
    <html>
    <body>

    <h1>My First Heading</h1>
    <p>My first paragraph.</p>

    </body>
    </html>
    """

		return [html_string_a, html_string_a]




@require_bsa
class      UpperCamelCase__					(					lowercase_						,					unittest.TestCase			):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__				=	MarkupLMFeatureExtractor if is_bsa_available() else None


					def lowerCamelCase_			(						self				:			Optional[int]						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				int		=			MarkupLMFeatureExtractionTester(self						)


					@property
					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							return self.feature_extract_tester.prepare_feat_extract_dict()



					def lowerCamelCase_			(						self				:			int						):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE :				Union[str, Any]		=			self.feature_extraction_class()

							# Test not batched input
							SCREAMING_SNAKE_CASE :				Optional[int]		=			get_html_strings()[0]
							SCREAMING_SNAKE_CASE :				List[Any]		=			feature_extractor(lowerCamelCase_						)

							# fmt: off
							SCREAMING_SNAKE_CASE :				Optional[int]		=			[["""sample document""", """Goog""", """This is one header""", """This is a another Header""", """Travel from""", """SFO to JFK""", """on May 2, 2015 at 2:00 pm. For details go to confirm.com""", """Traveler""", """name""", """is""", """John Doe"""]]
							SCREAMING_SNAKE_CASE :				List[str]		=			[["""/html/head/title""", """/html/body/a""", """/html/body/h1""", """/html/body/h2""", """/html/body/p""", """/html/body/p/p/b[1]""", """/html/body/p/p/b[2]/i""", """/html/body/p/p/div/h3""", """/html/body/p/p/div/h3/b""", """/html/body/p/p/div/h3""", """/html/body/p/p/div/h3/p"""]]
							# fmt: on

							self.assertEqual(encoding.nodes      ,						lowerCamelCase_						)
							self.assertEqual(encoding.xpaths      ,						lowerCamelCase_						)

							# Test batched
							SCREAMING_SNAKE_CASE :				str		=			get_html_strings()
							SCREAMING_SNAKE_CASE :				str		=			feature_extractor(lowerCamelCase_						)

							# fmt: off
							SCREAMING_SNAKE_CASE :				int		=			expected_nodes + [["""My First Heading""", """My first paragraph."""]]
							SCREAMING_SNAKE_CASE :				List[Any]		=			expected_xpaths + [["""/html/body/h1""", """/html/body/p"""]]

							self.assertEqual(len(encoding.nodes						)      ,						2						)
							self.assertEqual(len(encoding.xpaths						)      ,						2						)

							self.assertEqual(encoding.nodes      ,						lowerCamelCase_						)
							self.assertEqual(encoding.xpaths      ,						lowerCamelCase_						)



