



from __future__ import annotations

import unittest

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

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


if is_tf_available():
						import tensorflow as tf

						from transformers import (
						    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFXLMForMultipleChoice,
						    TFXLMForQuestionAnsweringSimple,
						    TFXLMForSequenceClassification,
						    TFXLMForTokenClassification,
						    TFXLMModel,
						    TFXLMWithLMHeadModel,
						    XLMConfig,
						)



class     __snake_case						:




							def __init__(	self					:	List[str]    ,    A_					:	Dict    ,    ):
									lowerCAmelCase_      :					int     =			parent
									lowerCAmelCase_      :					Any     =			1_3
									lowerCAmelCase_      :					Optional[int]     =			7
									lowerCAmelCase_      :					Tuple     =			True
									lowerCAmelCase_      :					Optional[int]     =			True
									lowerCAmelCase_      :					Optional[Any]     =			True
									lowerCAmelCase_      :					List[Any]     =			True
									lowerCAmelCase_      :					str     =			True
									lowerCAmelCase_      :					Union[str, Any]     =			False
									lowerCAmelCase_      :					List[str]     =			False
									lowerCAmelCase_      :					Dict     =			False
									lowerCAmelCase_      :					Dict     =			2
									lowerCAmelCase_      :					Union[str, Any]     =			9_9
									lowerCAmelCase_      :					str     =			0
									lowerCAmelCase_      :					Optional[int]     =			3_2
									lowerCAmelCase_      :					Optional[Any]     =			2
									lowerCAmelCase_      :					int     =			4
									lowerCAmelCase_      :					Tuple     =			0.1
									lowerCAmelCase_      :					Union[str, Any]     =			0.1
									lowerCAmelCase_      :					List[Any]     =			5_1_2
									lowerCAmelCase_      :					Any     =			1_6
									lowerCAmelCase_      :					Tuple     =			2
									lowerCAmelCase_      :					Tuple     =			0.02
									lowerCAmelCase_      :					Dict     =			3
									lowerCAmelCase_      :					Any     =			4
									lowerCAmelCase_      :					List[str]     =			'''last'''
									lowerCAmelCase_      :					Union[str, Any]     =			True
									lowerCAmelCase_      :					Dict     =			None
									lowerCAmelCase_      :					int     =			0




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					Union[str, Any]     =			ids_tensor([self.batch_size, self.seq_length]    ,    self.vocab_size)
									lowerCAmelCase_      :					Tuple     =			random_attention_mask([self.batch_size, self.seq_length]    ,    dtype=tf.floataa)

									lowerCAmelCase_      :					Optional[Any]     =			None
									if self.use_input_lengths:
											lowerCAmelCase_      :					Optional[int]     =			(
											    ids_tensor([self.batch_size]    ,    vocab_size=2) + self.seq_length - 2
											)  # small variation of seq_length

									lowerCAmelCase_      :					Any     =			None
									if self.use_token_type_ids:
											lowerCAmelCase_      :					Any     =			ids_tensor([self.batch_size, self.seq_length]    ,    self.n_langs)

									lowerCAmelCase_      :					str     =			None
									lowerCAmelCase_      :					Dict     =			None
									lowerCAmelCase_      :					List[Any]     =			None
									if self.use_labels:
											lowerCAmelCase_      :					Any     =			ids_tensor([self.batch_size]    ,    self.type_sequence_label_size)
											lowerCAmelCase_      :					List[Any]     =			ids_tensor([self.batch_size, self.seq_length]    ,    self.num_labels)
											lowerCAmelCase_      :					Dict     =			ids_tensor([self.batch_size]    ,    2    ,    dtype=tf.floataa)
											lowerCAmelCase_      :					str     =			ids_tensor([self.batch_size]    ,    self.num_choices)

									lowerCAmelCase_      :					Optional[int]     =			XLMConfig(
									    vocab_size=self.vocab_size    ,    n_special=self.n_special    ,    emb_dim=self.hidden_size    ,    n_layers=self.num_hidden_layers    ,    n_heads=self.num_attention_heads    ,    dropout=self.hidden_dropout_prob    ,    attention_dropout=self.attention_probs_dropout_prob    ,    gelu_activation=self.gelu_activation    ,    sinusoidal_embeddings=self.sinusoidal_embeddings    ,    asm=self.asm    ,    causal=self.causal    ,    n_langs=self.n_langs    ,    max_position_embeddings=self.max_position_embeddings    ,    initializer_range=self.initializer_range    ,    summary_type=self.summary_type    ,    use_proj=self.use_proj    ,    bos_token_id=self.bos_token_id    ,    )

									return (
									    config,
									    input_ids,
									    token_type_ids,
									    input_lengths,
									    sequence_labels,
									    token_labels,
									    is_impossible_labels,
									    choice_labels,
									    input_mask,
									)




							def        UpperCAmelCase__      (	self					:	Union[str, Any]    ,    A_					:	Optional[int]    ,    A_					:	Dict    ,    A_					:	List[str]    ,    A_					:	Tuple    ,    A_					:	Any    ,    A_					:	str    ,    A_					:	List[str]    ,    A_					:	str    ,    A_					:	Optional[int]    ,    ):
									lowerCAmelCase_      :					Union[str, Any]     =			TFXLMModel(config=A_)
									lowerCAmelCase_      :					str     =			{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
									lowerCAmelCase_      :					Dict     =			model(A_)

									lowerCAmelCase_      :					str     =			[input_ids, input_mask]
									lowerCAmelCase_      :					Tuple     =			model(A_)
									self.parent.assertEqual(result.last_hidden_state.shape    ,    (self.batch_size, self.seq_length, self.hidden_size))




							def        UpperCAmelCase__      (	self					:	Any    ,    A_					:	List[str]    ,    A_					:	List[Any]    ,    A_					:	Tuple    ,    A_					:	str    ,    A_					:	Union[str, Any]    ,    A_					:	Union[str, Any]    ,    A_					:	Any    ,    A_					:	Dict    ,    A_					:	List[str]    ,    ):
									lowerCAmelCase_      :					List[str]     =			TFXLMWithLMHeadModel(A_)

									lowerCAmelCase_      :					str     =			{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
									lowerCAmelCase_      :					Dict     =			model(A_)

									lowerCAmelCase_      :					int     =			outputs

									self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.seq_length, self.vocab_size))




							def        UpperCAmelCase__      (	self					:	List[str]    ,    A_					:	Optional[Any]    ,    A_					:	Dict    ,    A_					:	Any    ,    A_					:	Optional[Any]    ,    A_					:	List[str]    ,    A_					:	Optional[int]    ,    A_					:	List[str]    ,    A_					:	str    ,    A_					:	Any    ,    ):
									lowerCAmelCase_      :					Any     =			TFXLMForQuestionAnsweringSimple(A_)

									lowerCAmelCase_      :					Tuple     =			{'''input_ids''': input_ids, '''lengths''': input_lengths}

									lowerCAmelCase_      :					Any     =			model(A_)

									self.parent.assertEqual(result.start_logits.shape    ,    (self.batch_size, self.seq_length))
									self.parent.assertEqual(result.end_logits.shape    ,    (self.batch_size, self.seq_length))




							def        UpperCAmelCase__      (	self					:	List[str]    ,    A_					:	List[str]    ,    A_					:	Optional[int]    ,    A_					:	Dict    ,    A_					:	Union[str, Any]    ,    A_					:	Dict    ,    A_					:	Optional[Any]    ,    A_					:	Union[str, Any]    ,    A_					:	str    ,    A_					:	Optional[int]    ,    ):
									lowerCAmelCase_      :					Optional[Any]     =			TFXLMForSequenceClassification(A_)

									lowerCAmelCase_      :					Optional[int]     =			{'''input_ids''': input_ids, '''lengths''': input_lengths}

									lowerCAmelCase_      :					List[str]     =			model(A_)

									self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.type_sequence_label_size))




							def        UpperCAmelCase__      (	self					:	List[str]    ,    A_					:	str    ,    A_					:	Optional[Any]    ,    A_					:	Tuple    ,    A_					:	List[str]    ,    A_					:	Tuple    ,    A_					:	List[Any]    ,    A_					:	Optional[Any]    ,    A_					:	Tuple    ,    A_					:	List[str]    ,    ):
									lowerCAmelCase_      :					str     =			self.num_labels
									lowerCAmelCase_      :					Optional[int]     =			TFXLMForTokenClassification(config=A_)
									lowerCAmelCase_      :					Optional[int]     =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
									lowerCAmelCase_      :					Union[str, Any]     =			model(A_)
									self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.seq_length, self.num_labels))




							def        UpperCAmelCase__      (	self					:	List[str]    ,    A_					:	List[Any]    ,    A_					:	int    ,    A_					:	Optional[Any]    ,    A_					:	Union[str, Any]    ,    A_					:	Any    ,    A_					:	str    ,    A_					:	Union[str, Any]    ,    A_					:	List[Any]    ,    A_					:	Tuple    ,    ):
									lowerCAmelCase_      :					Union[str, Any]     =			self.num_choices
									lowerCAmelCase_      :					Union[str, Any]     =			TFXLMForMultipleChoice(config=A_)
									lowerCAmelCase_      :					List[Any]     =			tf.tile(tf.expand_dims(A_    ,    1)    ,    (1, self.num_choices, 1))
									lowerCAmelCase_      :					Optional[int]     =			tf.tile(tf.expand_dims(A_    ,    1)    ,    (1, self.num_choices, 1))
									lowerCAmelCase_      :					Optional[Any]     =			tf.tile(tf.expand_dims(A_    ,    1)    ,    (1, self.num_choices, 1))
									lowerCAmelCase_      :					Dict     =			{
									    '''input_ids''': multiple_choice_inputs_ids,
									    '''attention_mask''': multiple_choice_input_mask,
									    '''token_type_ids''': multiple_choice_token_type_ids,
									}
									lowerCAmelCase_      :					Optional[int]     =			model(A_)
									self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.num_choices))







							def        UpperCAmelCase__      (	self					:	Optional[int]):
									lowerCAmelCase_      :					Optional[int]     =			self.prepare_config_and_inputs()
									(
									    (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  (
									    lowerCAmelCase_
									) ,  
									)      :					Optional[int]     =			config_and_inputs
									lowerCAmelCase_      :					List[str]     =			{
									    '''input_ids''': input_ids,
									    '''token_type_ids''': token_type_ids,
									    '''langs''': token_type_ids,
									    '''lengths''': input_lengths,
									}
									return config, inputs_dict





@require_tf
class     __snake_case						(					UpperCamelCase_						,UpperCamelCase_						,unittest.TestCase						):
							_a      				=			(
							    (
							        TFXLMModel,
							        TFXLMWithLMHeadModel,
							        TFXLMForSequenceClassification,
							        TFXLMForQuestionAnsweringSimple,
							        TFXLMForTokenClassification,
							        TFXLMForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							_a      				=			(
							    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Check other models whether language generation is also applicable
							_a      				=			(
							    {
							        '''feature-extraction''': TFXLMModel,
							        '''fill-mask''': TFXLMWithLMHeadModel,
							        '''question-answering''': TFXLMForQuestionAnsweringSimple,
							        '''text-classification''': TFXLMForSequenceClassification,
							        '''text-generation''': TFXLMWithLMHeadModel,
							        '''token-classification''': TFXLMForTokenClassification,
							        '''zero-shot''': TFXLMForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							_a      				=			False
							_a      				=			False




							def        UpperCAmelCase__      (	self					:	List[str]    ,    A_					:	Dict    ,    A_					:	Optional[int]    ,    A_					:	List[str]    ,    A_					:	Any    ,    A_					:	Tuple):
									if (
									    pipeline_test_casse_name == "QAPipelineTests"
									    and tokenizer_name is not None
									    and not tokenizer_name.endswith('''Fast''')
									):
											# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
											# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
											# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
											return True

									return False




							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_      :					Dict     =			TFXLMModelTester(self)
									lowerCAmelCase_      :					int     =			ConfigTester(self    ,    config_class=A_    ,    emb_dim=3_7)




							def        UpperCAmelCase__      (	self					:	int):
									self.config_tester.run_common_tests()




							def        UpperCAmelCase__      (	self					:	str):
									lowerCAmelCase_      :					Dict     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_model(*A_)




							def        UpperCAmelCase__      (	self					:	Any):
									lowerCAmelCase_      :					List[Any]     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_lm_head(*A_)




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					List[Any]     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_qa(*A_)




							def        UpperCAmelCase__      (	self					:	List[str]):
									lowerCAmelCase_      :					Union[str, Any]     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_sequence_classif(*A_)




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					str     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_for_token_classification(*A_)




							def        UpperCAmelCase__      (	self					:	List[Any]):
									lowerCAmelCase_      :					Optional[int]     =			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_for_multiple_choice(*A_)







							@slow
							def        UpperCAmelCase__      (	self					:	Any):
									for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCAmelCase_      :					List[str]     =			TFXLMModel.from_pretrained(A_)
											self.assertIsNotNone(A_)





@require_tf
class     __snake_case						(					unittest.TestCase						):




							@slow
							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					List[str]     =			TFXLMWithLMHeadModel.from_pretrained('''xlm-mlm-en-2048''')
									lowerCAmelCase_      :					Union[str, Any]     =			tf.convert_to_tensor([[1_4, 4_4_7]]    ,    dtype=tf.intaa)  # the president
									lowerCAmelCase_      :					int     =			[
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									    1_4,
									    4_4_7,
									]  # the president the president the president the president the president the president the president the president the president the president
									# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
									lowerCAmelCase_      :					Dict     =			model.generate(A_    ,    do_sample=A_)
									self.assertListEqual(output_ids[0].numpy().tolist()    ,    A_)










from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__  :  Tuple       = logging.get_logger(__name__)

A__  :  Tuple       = {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}



class     __snake_case						(					UpperCamelCase_						):
							_a      				=			'''opt'''
							_a      				=			['''past_key_values''']




							def __init__(	self					:	Optional[int]    ,    A_					:	Any=5_0_2_7_2    ,    A_					:	Tuple=7_6_8    ,    A_					:	Optional[Any]=1_2    ,    A_					:	str=3_0_7_2    ,    A_					:	Dict=2_0_4_8    ,    A_					:	List[Any]=True    ,    A_					:	Optional[int]=False    ,    A_					:	Optional[int]=None    ,    A_					:	Dict=0.1    ,    A_					:	str=0.0    ,    A_					:	Union[str, Any]=1_2    ,    A_					:	List[str]="relu"    ,    A_					:	Dict=0.0    ,    A_					:	Union[str, Any]=0.02    ,    A_					:	Optional[Any]=True    ,    A_					:	Optional[Any]=1    ,    A_					:	List[Any]=2    ,    A_					:	Dict=2    ,    A_					:	Optional[int]=True    ,    A_					:	Optional[int]=True    ,    **A_					:	List[Any]    ,    ):
									super().__init__(
									    pad_token_id=A_    ,    bos_token_id=A_    ,    eos_token_id=A_    ,    **A_    ,    )
									lowerCAmelCase_      :					List[Any]     =			vocab_size
									lowerCAmelCase_      :					Union[str, Any]     =			max_position_embeddings
									lowerCAmelCase_      :					Optional[int]     =			num_attention_heads
									lowerCAmelCase_      :					int     =			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
									lowerCAmelCase_      :					List[Any]     =			ffn_dim
									lowerCAmelCase_      :					Any     =			hidden_size
									lowerCAmelCase_      :					Union[str, Any]     =			num_hidden_layers
									lowerCAmelCase_      :					str     =			dropout
									lowerCAmelCase_      :					List[str]     =			attention_dropout
									lowerCAmelCase_      :					Any     =			activation_function
									lowerCAmelCase_      :					Union[str, Any]     =			init_std
									lowerCAmelCase_      :					int     =			layerdrop
									lowerCAmelCase_      :					Optional[int]     =			use_cache
									lowerCAmelCase_      :					int     =			do_layer_norm_before
									# We keep these variables at `True` for backward compatibility.
									lowerCAmelCase_      :					Optional[Any]     =			enable_bias
									lowerCAmelCase_      :					Optional[int]     =			layer_norm_elementwise_affine

									# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
									# with checkpoints that have been fine-tuned before transformers v4.20.1
									# see https://github.com/facebookresearch/metaseq/pull/164
									lowerCAmelCase_      :					Optional[Any]     =			_remove_final_layer_norm





