

'''simple docstring'''







from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, 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.models.openai.modeling_tf_openai import (
							    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFOpenAIGPTDoubleHeadsModel,
							    TFOpenAIGPTForSequenceClassification,
							    TFOpenAIGPTLMHeadModel,
							    TFOpenAIGPTModel,
							)
class 					__a   :

						def __init__(		self	:						Dict    ,			lowercase__	:						Dict    ,			)						->Optional[Any]:

							"""simple docstring"""



							_lowercase							=     parent
							_lowercase							=     13
							_lowercase							=     7
							_lowercase							=     True
							_lowercase							=     True
							_lowercase							=     True
							_lowercase							=     True
							_lowercase							=     True
							_lowercase							=     99
							_lowercase							=     32
							_lowercase							=     2
							_lowercase							=     4
							_lowercase							=     37
							_lowercase							=     """gelu"""
							_lowercase							=     0.1
							_lowercase							=     0.1
							_lowercase							=     5_12
							_lowercase							=     16
							_lowercase							=     2
							_lowercase							=     0.02
							_lowercase							=     3
							_lowercase							=     4
							_lowercase							=     None
							_lowercase							=     self.vocab_size - 1

						def 	_UpperCAmelCase							(		self	:						Dict)						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size)

							_lowercase							=     None
							if self.use_input_mask:
								_lowercase							=     random_attention_mask([self.batch_size, self.seq_length])

							_lowercase							=     None
							if self.use_token_type_ids:
								_lowercase							=     ids_tensor([self.batch_size, self.seq_length]    ,			self.type_vocab_size)

							_lowercase							=     None
							if self.use_mc_token_ids:
								_lowercase							=     ids_tensor([self.batch_size, self.num_choices]    ,			self.seq_length)

							_lowercase							=     None
							_lowercase							=     None
							_lowercase							=     None
							if self.use_labels:
								_lowercase							=     ids_tensor([self.batch_size]    ,			self.type_sequence_label_size)
								_lowercase							=     ids_tensor([self.batch_size, self.seq_length]    ,			self.num_labels)
								_lowercase							=     ids_tensor([self.batch_size]    ,			self.num_choices)

							_lowercase							=     OpenAIGPTConfig(
							    vocab_size=self.vocab_size    ,			n_embd=self.hidden_size    ,			n_layer=self.num_hidden_layers    ,			n_head=self.num_attention_heads    ,			n_positions=self.max_position_embeddings    ,			pad_token_id=self.pad_token_id    ,			)

							_lowercase							=     ids_tensor([self.num_hidden_layers, self.num_attention_heads]    ,			2)

							return (
							    config,
							    input_ids,
							    input_mask,
							    head_mask,
							    token_type_ids,
							    mc_token_ids,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							)

						def 	_UpperCAmelCase							(		self	:						Optional[int]    ,			lowercase__	:						Union[str, Any]    ,			lowercase__	:						Union[str, Any]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						str    ,			lowercase__	:						Any    ,			*lowercase__	:						str)						->str:

							"""simple docstring"""



							_lowercase							=     TFOpenAIGPTModel(config=lowercase__)
							_lowercase							=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
							_lowercase							=     model(lowercase__)

							_lowercase							=     [input_ids, input_mask]
							_lowercase							=     model(lowercase__)

							_lowercase							=     model(lowercase__)

							self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size))

						def 	_UpperCAmelCase							(		self	:						Tuple    ,			lowercase__	:						str    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						Optional[Any]    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Union[str, Any]    ,			*lowercase__	:						str)						->List[Any]:

							"""simple docstring"""



							_lowercase							=     TFOpenAIGPTLMHeadModel(config=lowercase__)
							_lowercase							=     {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
							_lowercase							=     model(lowercase__)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size))

						def 	_UpperCAmelCase							(		self	:						str    ,			lowercase__	:						Dict    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						int    ,			lowercase__	:						Any    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Optional[int]    ,			*lowercase__	:						Dict)						->int:

							"""simple docstring"""



							_lowercase							=     TFOpenAIGPTDoubleHeadsModel(config=lowercase__)

							_lowercase							=     tf.tile(tf.expand_dims(lowercase__    ,			1)    ,			(1, self.num_choices, 1))
							_lowercase							=     tf.tile(tf.expand_dims(lowercase__    ,			1)    ,			(1, self.num_choices, 1))
							_lowercase							=     tf.tile(tf.expand_dims(lowercase__    ,			1)    ,			(1, self.num_choices, 1))

							_lowercase							=     {
							    """input_ids""": multiple_choice_inputs_ids,
							    """mc_token_ids""": mc_token_ids,
							    """attention_mask""": multiple_choice_input_mask,
							    """token_type_ids""": multiple_choice_token_type_ids,
							}
							_lowercase							=     model(lowercase__)
							self.parent.assertEqual(
							    result.logits.shape    ,			(self.batch_size, self.num_choices, self.seq_length, self.vocab_size))
							self.parent.assertEqual(result.mc_logits.shape    ,			(self.batch_size, self.num_choices))

						def 	_UpperCAmelCase							(		self	:						Optional[Any]    ,			lowercase__	:						Any    ,			lowercase__	:						Tuple    ,			lowercase__	:						List[str]    ,			lowercase__	:						str    ,			lowercase__	:						str    ,			*lowercase__	:						int)						->Union[str, Any]:

							"""simple docstring"""



							_lowercase							=     self.num_labels
							_lowercase							=     ids_tensor([self.batch_size]    ,			self.type_sequence_label_size)
							_lowercase							=     {
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							    """labels""": sequence_labels,
							}
							_lowercase							=     TFOpenAIGPTForSequenceClassification(lowercase__)
							_lowercase							=     model(lowercase__)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels))


						def 	_UpperCAmelCase							(		self	:						Optional[int])						->Any:

							"""simple docstring"""



							_lowercase							=     self.prepare_config_and_inputs()

							(
							    (
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,		(
							    _lowercase	
							)		,			
							)							=     config_and_inputs

							_lowercase							=     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
							return config, inputs_dict






@require_tf
class 					__a   (		_snake_case							,_snake_case							,unittest.TestCase  ):
						__SCREAMING_SNAKE_CASE     :					Any  =      (
						    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
						    if is_tf_available()
						    else ()
						)
						__SCREAMING_SNAKE_CASE     :					Optional[Any]  =      (
						    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
						)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
						__SCREAMING_SNAKE_CASE     :					Dict  =      (
						    {
						        'feature-extraction': TFOpenAIGPTModel,
						        'text-classification': TFOpenAIGPTForSequenceClassification,
						        'text-generation': TFOpenAIGPTLMHeadModel,
						        'zero-shot': TFOpenAIGPTForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						__SCREAMING_SNAKE_CASE     :					Tuple  =      False
						__SCREAMING_SNAKE_CASE     :					int  =      False

						def 	_UpperCAmelCase							(		self	:						List[str]    ,			lowercase__	:						List[Any]    ,			lowercase__	:						Optional[int]    ,			lowercase__	:						List[str]    ,			lowercase__	:						int    ,			lowercase__	:						int)						->Optional[Any]:

							"""simple docstring"""



							if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
								# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
								# `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
								# tiny config could not be created.
								return True

							return False

						def 	_UpperCAmelCase							(		self	:						Union[str, Any])						->Optional[int]:

							"""simple docstring"""



							_lowercase							=     TFOpenAIGPTModelTester(self)
							_lowercase							=     ConfigTester(self    ,			config_class=lowercase__    ,			n_embd=37)

						def 	_UpperCAmelCase							(		self	:						List[str])						->List[str]:

							"""simple docstring"""



							self.config_tester.run_common_tests()

						def 	_UpperCAmelCase							(		self	:						List[Any])						->Any:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						str)						->str:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_lm_head(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						Optional[Any])						->Optional[Any]:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_double_head(*lowercase__)

						def 	_UpperCAmelCase							(		self	:						Optional[Any])						->List[str]:

							"""simple docstring"""



							_lowercase							=     self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase__)


						@slow
						def 	_UpperCAmelCase							(		self	:						Dict)						->str:

							"""simple docstring"""



							for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								_lowercase							=     TFOpenAIGPTModel.from_pretrained(lowercase__)
								self.assertIsNotNone(lowercase__)






@require_tf
class 					__a   (		unittest.TestCase  ):

						@slow
						def 	_UpperCAmelCase							(		self	:						List[str])						->Dict:

							"""simple docstring"""



							_lowercase							=     TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt""")
							_lowercase							=     tf.convert_to_tensor([[4_81, 47_35, 5_44]]    ,			dtype=tf.intaa)  # the president is
							_lowercase							=     [
							    4_81,
							    47_35,
							    5_44,
							    2_46,
							    9_63,
							    8_70,
							    7_62,
							    2_39,
							    2_44,
							    4_04_77,
							    2_44,
							    2_49,
							    7_19,
							    8_81,
							    4_87,
							    5_44,
							    2_40,
							    2_44,
							    6_03,
							    4_81,
							]  # the president is a very good man. " \n " i\'m sure he is, " said the

							_lowercase							=     model.generate(lowercase__    ,			do_sample=lowercase__)
							self.assertListEqual(output_ids[0].numpy().tolist()    ,			lowercase__)






"""simple docstring"""




import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
					pass

if is_speech_available():
					from transformers import EncodecFeatureExtractor, MusicgenProcessor


__lowerCAmelCase							:  Union[str, Any] 				=		random.Random()

def __snake_case (      UpperCamelCase						,					UpperCamelCase=1.0						,					UpperCamelCase=None						,					UpperCamelCase=None    )       ->    Tuple:


		"""simple docstring"""





		if rng is None:
				a__   		=     global_rng

		a__   		=     []
		for batch_idx in range(shape[0]    ):
				values.append([]    )
				for _ in range(shape[1]    ):
						values[-1].append(rng.random() * scale    )

		return values




@require_torch
@require_sentencepiece
class SCREAMING_SNAKE_CASE       (      unittest.TestCase					):






	'''simple docstring'''







	def    _UpperCamelCase							(  self :Optional[int]			)			->   List[str]:

			'''simple docstring'''






			a__   		=     '''facebook/musicgen-small'''
			a__   		=     tempfile.mkdtemp()


	def    _UpperCamelCase							(  self :Union[str, Any]       ,    **__magic_name__ :Union[str, Any]			)			->   str:

			'''simple docstring'''






			return TaTokenizer.from_pretrained(self.checkpoint       ,    **__magic_name__			)


	def    _UpperCamelCase							(  self :Optional[int]       ,    **__magic_name__ :Any			)			->   Optional[int]:

			'''simple docstring'''






			return EncodecFeatureExtractor.from_pretrained(self.checkpoint       ,    **__magic_name__			)


	def    _UpperCamelCase							(  self :Optional[int]			)			->   Optional[int]:

			'''simple docstring'''






			shutil.rmtree(self.tmpdirname			)


	def    _UpperCamelCase							(  self :Tuple			)			->   Any:

			'''simple docstring'''






			a__   		=     self.get_tokenizer()
			a__   		=     self.get_feature_extractor()

			a__   		=     MusicgenProcessor(tokenizer=__magic_name__       ,    feature_extractor=__magic_name__			)

			processor.save_pretrained(self.tmpdirname			)
			a__   		=     MusicgenProcessor.from_pretrained(self.tmpdirname			)

			self.assertEqual(processor.tokenizer.get_vocab()       ,    tokenizer.get_vocab()			)
			self.assertIsInstance(processor.tokenizer       ,    __magic_name__			)

			self.assertEqual(processor.feature_extractor.to_json_string()       ,    feature_extractor.to_json_string()			)
			self.assertIsInstance(processor.feature_extractor       ,    __magic_name__			)


	def    _UpperCamelCase							(  self :Dict			)			->   Optional[int]:

			'''simple docstring'''






			a__   		=     MusicgenProcessor(tokenizer=self.get_tokenizer()       ,    feature_extractor=self.get_feature_extractor()			)
			processor.save_pretrained(self.tmpdirname			)

			a__   		=     self.get_tokenizer(bos_token='''(BOS)'''       ,    eos_token='''(EOS)'''			)
			a__   		=     self.get_feature_extractor(do_normalize=__magic_name__       ,    padding_value=1.0			)

			a__   		=     MusicgenProcessor.from_pretrained(
			    self.tmpdirname       ,    bos_token='''(BOS)'''       ,    eos_token='''(EOS)'''       ,    do_normalize=__magic_name__       ,    padding_value=1.0			)

			self.assertEqual(processor.tokenizer.get_vocab()       ,    tokenizer_add_kwargs.get_vocab()			)
			self.assertIsInstance(processor.tokenizer       ,    __magic_name__			)

			self.assertEqual(processor.feature_extractor.to_json_string()       ,    feature_extractor_add_kwargs.to_json_string()			)
			self.assertIsInstance(processor.feature_extractor       ,    __magic_name__			)


	def    _UpperCamelCase							(  self :Any			)			->   Tuple:

			'''simple docstring'''






			a__   		=     self.get_feature_extractor()
			a__   		=     self.get_tokenizer()

			a__   		=     MusicgenProcessor(tokenizer=__magic_name__       ,    feature_extractor=__magic_name__			)

			a__   		=     floats_list((3, 1000)			)

			a__   		=     feature_extractor(__magic_name__       ,    return_tensors='''np'''			)
			a__   		=     processor(__magic_name__       ,    return_tensors='''np'''			)

			for key in input_feat_extract.keys():
					self.assertAlmostEqual(input_feat_extract[key].sum()       ,    input_processor[key].sum()       ,    delta=1e-2			)


	def    _UpperCamelCase							(  self :Tuple			)			->   Dict:

			'''simple docstring'''






			a__   		=     self.get_feature_extractor()
			a__   		=     self.get_tokenizer()

			a__   		=     MusicgenProcessor(tokenizer=__magic_name__       ,    feature_extractor=__magic_name__			)

			a__   		=     '''This is a test string'''

			a__   		=     processor(text=__magic_name__			)

			a__   		=     tokenizer(__magic_name__			)

			for key in encoded_tok.keys():
					self.assertListEqual(encoded_tok[key]       ,    encoded_processor[key]			)


	def    _UpperCamelCase							(  self :Tuple			)			->   Optional[Any]:

			'''simple docstring'''






			a__   		=     self.get_feature_extractor()
			a__   		=     self.get_tokenizer()

			a__   		=     MusicgenProcessor(tokenizer=__magic_name__       ,    feature_extractor=__magic_name__			)

			a__   		=     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

			a__   		=     processor.batch_decode(sequences=__magic_name__			)
			a__   		=     tokenizer.batch_decode(__magic_name__			)

			self.assertListEqual(__magic_name__       ,    __magic_name__			)


	def    _UpperCamelCase							(  self :Optional[Any]			)			->   int:

			'''simple docstring'''






			a__   		=     self.get_feature_extractor()
			a__   		=     self.get_tokenizer()

			a__   		=     MusicgenProcessor(tokenizer=__magic_name__       ,    feature_extractor=__magic_name__			)

			self.assertListEqual(
			    processor.model_input_names       ,    feature_extractor.model_input_names       ,    msg='''`processor` and `feature_extractor` model input names do not match'''       ,    )






	def    _UpperCamelCase							(  self :Any			)			->   List[str]:

			'''simple docstring'''






			a__   		=     self.get_feature_extractor(padding_side='''left'''			)
			a__   		=     self.get_tokenizer()

			a__   		=     MusicgenProcessor(tokenizer=__magic_name__       ,    feature_extractor=__magic_name__			)

			a__   		=     [floats_list((1, x)			)[0] for x in range(5       ,    20       ,    5			)]
			a__   		=     processor(__magic_name__			).padding_mask

			a__   		=     np.asarray(floats_list((3, 20)			)			)[:, None, :]
			a__   		=     processor.batch_decode(__magic_name__       ,    padding_mask=__magic_name__			)

			self.assertIsInstance(__magic_name__       ,    __magic_name__			)

			for audio in decoded_audios:
					self.assertIsInstance(__magic_name__       ,    np.ndarray			)

			self.assertTrue(decoded_audios[0].shape == (1, 10)			)
			self.assertTrue(decoded_audios[1].shape == (1, 15)			)
			self.assertTrue(decoded_audios[2].shape == (1, 20)			)




