




"""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    __lowercase							:


				'''simple docstring'''


				def __init__( self :  Dict   ,SCREAMING_SNAKE_CASE_ :  List[Any]   ,):
										snake_case__							:       List[Any]				      =       parent
										snake_case__							:       Optional[Any]				      =       1_3
										snake_case__							:       List[Any]				      =       7
										snake_case__							:       Union[str, Any]				      =       True
										snake_case__							:       List[Any]				      =       True
										snake_case__							:       str				      =       True
										snake_case__							:       str				      =       True
										snake_case__							:       Optional[int]				      =       True
										snake_case__							:       Tuple				      =       9_9
										snake_case__							:       List[Any]				      =       3_2
										snake_case__							:       Union[str, Any]				      =       2
										snake_case__							:       Tuple				      =       4
										snake_case__							:       Dict				      =       3_7
										snake_case__							:       Optional[int]				      =       '''gelu'''
										snake_case__							:       Any				      =       0.1
										snake_case__							:       Any				      =       0.1
										snake_case__							:       List[str]				      =       5_1_2
										snake_case__							:       Any				      =       1_6
										snake_case__							:       str				      =       2
										snake_case__							:       Optional[int]				      =       0.02
										snake_case__							:       Optional[int]				      =       3
										snake_case__							:       Union[str, Any]				      =       4
										snake_case__							:       Any				      =       None
										snake_case__							:       Optional[int]				      =       self.vocab_size - 1

				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       str				      =       ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size   )

										snake_case__							:       Any				      =       None
										if self.use_input_mask:
																snake_case__							:       int				      =       random_attention_mask([self.batch_size, self.seq_length]   )

										snake_case__							:       List[Any]				      =       None
										if self.use_token_type_ids:
																snake_case__							:       List[str]				      =       ids_tensor([self.batch_size, self.seq_length]   ,self.type_vocab_size   )

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

										snake_case__							:       Dict				      =       None
										snake_case__							:       str				      =       None
										snake_case__							:       Optional[int]				      =       None
										if self.use_labels:
																snake_case__							:       Tuple				      =       ids_tensor([self.batch_size]   ,self.type_sequence_label_size   )
																snake_case__							:       List[str]				      =       ids_tensor([self.batch_size, self.seq_length]   ,self.num_labels   )
																snake_case__							:       Optional[int]				      =       ids_tensor([self.batch_size]   ,self.num_choices   )

										snake_case__							:       Optional[int]				      =       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   ,)

										snake_case__							:       Optional[int]				      =       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    _lowerCamelCase       ( self :  Optional[int]   ,SCREAMING_SNAKE_CASE_ :  int   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]   ,SCREAMING_SNAKE_CASE_ :  str   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,SCREAMING_SNAKE_CASE_ :  List[Any]   ,*SCREAMING_SNAKE_CASE_ :  Union[str, Any]   ):
										snake_case__							:       Any				      =       TFOpenAIGPTModel(config=SCREAMING_SNAKE_CASE_   )
										snake_case__							:       Optional[Any]				      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case__							:       Dict				      =       model(SCREAMING_SNAKE_CASE_   )

										snake_case__							:       int				      =       [input_ids, input_mask]
										snake_case__							:       Union[str, Any]				      =       model(SCREAMING_SNAKE_CASE_   )

										snake_case__							:       Dict				      =       model(SCREAMING_SNAKE_CASE_   )

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

				def    _lowerCamelCase       ( self :  Optional[int]   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,SCREAMING_SNAKE_CASE_ :  List[Any]   ,SCREAMING_SNAKE_CASE_ :  str   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,*SCREAMING_SNAKE_CASE_ :  Optional[Any]   ):
										snake_case__							:       Tuple				      =       TFOpenAIGPTLMHeadModel(config=SCREAMING_SNAKE_CASE_   )
										snake_case__							:       Optional[int]				      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										snake_case__							:       Tuple				      =       model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.seq_length, self.vocab_size)   )

				def    _lowerCamelCase       ( self :  Union[str, Any]   ,SCREAMING_SNAKE_CASE_ :  str   ,SCREAMING_SNAKE_CASE_ :  List[str]   ,SCREAMING_SNAKE_CASE_ :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Any   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]   ,SCREAMING_SNAKE_CASE_ :  List[str]   ,*SCREAMING_SNAKE_CASE_ :  int   ):
										snake_case__							:       Dict				      =       TFOpenAIGPTDoubleHeadsModel(config=SCREAMING_SNAKE_CASE_   )

										snake_case__							:       Any				      =       tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_   ,1   )   ,(1, self.num_choices, 1)   )
										snake_case__							:       Any				      =       tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_   ,1   )   ,(1, self.num_choices, 1)   )
										snake_case__							:       Tuple				      =       tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE_   ,1   )   ,(1, self.num_choices, 1)   )

										snake_case__							:       int				      =       {
										    '''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,
										}
										snake_case__							:       int				      =       model(SCREAMING_SNAKE_CASE_   )
										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    _lowerCamelCase       ( self :  Tuple   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,SCREAMING_SNAKE_CASE_ :  Tuple   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]   ,SCREAMING_SNAKE_CASE_ :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Any   ,*SCREAMING_SNAKE_CASE_ :  List[str]   ):
										snake_case__							:       Any				      =       self.num_labels
										snake_case__							:       Optional[Any]				      =       ids_tensor([self.batch_size]   ,self.type_sequence_label_size   )
										snake_case__							:       int				      =       {
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										    '''token_type_ids''': token_type_ids,
										    '''labels''': sequence_labels,
										}
										snake_case__							:       Optional[Any]				      =       TFOpenAIGPTForSequenceClassification(SCREAMING_SNAKE_CASE_   )
										snake_case__							:       Optional[int]				      =       model(SCREAMING_SNAKE_CASE_   )
										self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)   )





				def    _lowerCamelCase       ( self :  List[str]   ):
										snake_case__							:       Any				      =       self.prepare_config_and_inputs()

										(
										    (
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						(
										    snake_case__
										)      ,						
										)							:       Tuple				      =       config_and_inputs

										snake_case__							:       List[str]				      =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict







@require_tf
class    __lowercase							(      snake_case_    , snake_case_    , unittest.TestCase						):


				'''simple docstring'''


				_lowerCAmelCase						      =(
				    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
				    if is_tf_available()
				    else ()
				)
				_lowerCAmelCase						      =(
				    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				_lowerCAmelCase						      =(
				    {
				        "feature-extraction": TFOpenAIGPTModel,
				        "text-classification": TFOpenAIGPTForSequenceClassification,
				        "text-generation": TFOpenAIGPTLMHeadModel,
				        "zero-shot": TFOpenAIGPTForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				_lowerCAmelCase						      =False
				_lowerCAmelCase						      =False

				def    _lowerCamelCase       ( self :  Optional[int]   ,SCREAMING_SNAKE_CASE_ :  Dict   ,SCREAMING_SNAKE_CASE_ :  str   ,SCREAMING_SNAKE_CASE_ :  Union[str, Any]   ,SCREAMING_SNAKE_CASE_ :  Dict   ,SCREAMING_SNAKE_CASE_ :  Tuple   ):
										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    _lowerCamelCase       ( self :  int   ):
										snake_case__							:       Union[str, Any]				      =       TFOpenAIGPTModelTester(self   )
										snake_case__							:       List[Any]				      =       ConfigTester(self   ,config_class=SCREAMING_SNAKE_CASE_   ,n_embd=3_7   )

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

				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       Union[str, Any]				      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*SCREAMING_SNAKE_CASE_   )

				def    _lowerCamelCase       ( self :  int   ):
										snake_case__							:       Dict				      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_lm_head(*SCREAMING_SNAKE_CASE_   )

				def    _lowerCamelCase       ( self :  Dict   ):
										snake_case__							:       Optional[int]				      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_double_head(*SCREAMING_SNAKE_CASE_   )

				def    _lowerCamelCase       ( self :  int   ):
										snake_case__							:       Optional[Any]				      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*SCREAMING_SNAKE_CASE_   )





				@slow
				def    _lowerCamelCase       ( self :  List[str]   ):
										for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																snake_case__							:       Optional[int]				      =       TFOpenAIGPTModel.from_pretrained(SCREAMING_SNAKE_CASE_   )
																self.assertIsNotNone(SCREAMING_SNAKE_CASE_   )







@require_tf
class    __lowercase							(      unittest.TestCase						):


				'''simple docstring'''


				@slow
				def    _lowerCamelCase       ( self :  Any   ):
										snake_case__							:       Union[str, Any]				      =       TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''   )
										snake_case__							:       Tuple				      =       tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]   ,dtype=tf.intaa   )  # the president is
										snake_case__							:       Optional[Any]				      =       [
										    4_8_1,
										    4_7_3_5,
										    5_4_4,
										    2_4_6,
										    9_6_3,
										    8_7_0,
										    7_6_2,
										    2_3_9,
										    2_4_4,
										    4_0_4_7_7,
										    2_4_4,
										    2_4_9,
										    7_1_9,
										    8_8_1,
										    4_8_7,
										    5_4_4,
										    2_4_0,
										    2_4_4,
										    6_0_3,
										    4_8_1,
										]  # the president is a very good man. " \n " i\'m sure he is, " said the

										snake_case__							:       List[str]				      =       model.generate(SCREAMING_SNAKE_CASE_   ,do_sample=SCREAMING_SNAKE_CASE_   )
										self.assertListEqual(output_ids[0].numpy().tolist()   ,SCREAMING_SNAKE_CASE_   )








"""simple docstring"""





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


_lowercase						   =     logging.get_logger(__name__)

_lowercase						   =     {
    """facebook/musicgen-small""": """https://huggingface.co/facebook/musicgen-small/resolve/main/config.json""",
    # See all Musicgen models at https://huggingface.co/models?filter=musicgen
}

class    __lowercase							(      snake_case_						):


				'''simple docstring'''


				_lowerCAmelCase						      ="musicgen_decoder"
				_lowerCAmelCase						      =["past_key_values"]

				def __init__( self :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Dict=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  List[str]=2_4   ,SCREAMING_SNAKE_CASE_ :  int=4_0_9_6   ,SCREAMING_SNAKE_CASE_ :  List[Any]=1_6   ,SCREAMING_SNAKE_CASE_ :  Dict=0.0   ,SCREAMING_SNAKE_CASE_ :  Any=True   ,SCREAMING_SNAKE_CASE_ :  Any="gelu"   ,SCREAMING_SNAKE_CASE_ :  Tuple=1_0_2_4   ,SCREAMING_SNAKE_CASE_ :  Any=0.1   ,SCREAMING_SNAKE_CASE_ :  List[str]=0.0   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=0.0   ,SCREAMING_SNAKE_CASE_ :  Tuple=0.02   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=False   ,SCREAMING_SNAKE_CASE_ :  List[str]=4   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=None   ,SCREAMING_SNAKE_CASE_ :  Tuple=False   ,**SCREAMING_SNAKE_CASE_ :  Union[str, Any]   ,):
										snake_case__							:       List[str]				      =       vocab_size
										snake_case__							:       List[str]				      =       max_position_embeddings
										snake_case__							:       List[Any]				      =       hidden_size
										snake_case__							:       Dict				      =       ffn_dim
										snake_case__							:       Union[str, Any]				      =       num_hidden_layers
										snake_case__							:       List[str]				      =       num_attention_heads
										snake_case__							:       Optional[Any]				      =       dropout
										snake_case__							:       Dict				      =       attention_dropout
										snake_case__							:       Union[str, Any]				      =       activation_dropout
										snake_case__							:       Dict				      =       activation_function
										snake_case__							:       Union[str, Any]				      =       initializer_factor
										snake_case__							:       List[str]				      =       layerdrop
										snake_case__							:       Any				      =       use_cache
										snake_case__							:       Tuple				      =       scale_embedding  # scale factor will be sqrt(d_model) if True
										snake_case__							:       Any				      =       num_codebooks
										super().__init__(
										    pad_token_id=SCREAMING_SNAKE_CASE_   ,bos_token_id=SCREAMING_SNAKE_CASE_   ,eos_token_id=SCREAMING_SNAKE_CASE_   ,tie_word_embeddings=SCREAMING_SNAKE_CASE_   ,**SCREAMING_SNAKE_CASE_   ,)







class    __lowercase							(      snake_case_						):


				'''simple docstring'''


				_lowerCAmelCase						      ="musicgen"
				_lowerCAmelCase						      =True

				def __init__( self :  Optional[Any]   ,**SCREAMING_SNAKE_CASE_ :  Dict   ):
										super().__init__(**SCREAMING_SNAKE_CASE_   )
										if "text_encoder" not in kwargs or "audio_encoder" not in kwargs or "decoder" not in kwargs:
																raise ValueError('''Config has to be initialized with text_encoder, audio_encoder and decoder config'''   )

										snake_case__							:       Optional[int]				      =       kwargs.pop('''text_encoder'''   )
										snake_case__							:       Optional[Any]				      =       text_encoder_config.pop('''model_type'''   )

										snake_case__							:       Union[str, Any]				      =       kwargs.pop('''audio_encoder'''   )
										snake_case__							:       Dict				      =       audio_encoder_config.pop('''model_type'''   )

										snake_case__							:       Optional[Any]				      =       kwargs.pop('''decoder'''   )

										snake_case__							:       Union[str, Any]				      =       AutoConfig.for_model(SCREAMING_SNAKE_CASE_   ,**SCREAMING_SNAKE_CASE_   )
										snake_case__							:       List[Any]				      =       AutoConfig.for_model(SCREAMING_SNAKE_CASE_   ,**SCREAMING_SNAKE_CASE_   )
										snake_case__							:       List[str]				      =       MusicgenDecoderConfig(**SCREAMING_SNAKE_CASE_   )
										snake_case__							:       Union[str, Any]				      =       True

				@classmethod
				def    _lowerCamelCase       ( cls :  Dict   ,SCREAMING_SNAKE_CASE_ :  PretrainedConfig   ,SCREAMING_SNAKE_CASE_ :  PretrainedConfig   ,SCREAMING_SNAKE_CASE_ :  MusicgenDecoderConfig   ,**SCREAMING_SNAKE_CASE_ :  str   ,):
										return cls(
										    text_encoder=text_encoder_config.to_dict()   ,audio_encoder=audio_encoder_config.to_dict()   ,decoder=decoder_config.to_dict()   ,**SCREAMING_SNAKE_CASE_   ,)





				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       Optional[Any]				      =       copy.deepcopy(self.__dict__   )
										snake_case__							:       Any				      =       self.text_encoder.to_dict()
										snake_case__							:       Dict				      =       self.audio_encoder.to_dict()
										snake_case__							:       Dict				      =       self.decoder.to_dict()
										snake_case__							:       Optional[int]				      =       self.__class__.model_type
										return output


