'''simple docstring'''




import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import (
						    OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
						    OpenAIGPTConfig,
						    OpenAIGPTDoubleHeadsModel,
						    OpenAIGPTForSequenceClassification,
						    OpenAIGPTLMHeadModel,
						    OpenAIGPTModel,
						)






class 				a__:


			'''simple docstring'''






			def __init__(   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase=13       ,    __lowerCAmelCase=7       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=True       ,    __lowerCAmelCase=99       ,    __lowerCAmelCase=32       ,    __lowerCAmelCase=5       ,    __lowerCAmelCase=4       ,    __lowerCAmelCase=37       ,    __lowerCAmelCase="gelu"       ,    __lowerCAmelCase=0.1       ,    __lowerCAmelCase=0.1       ,    __lowerCAmelCase=512       ,    __lowerCAmelCase=16       ,    __lowerCAmelCase=2       ,    __lowerCAmelCase=0.02       ,    __lowerCAmelCase=3       ,    __lowerCAmelCase=4       ,    __lowerCAmelCase=None       ,    ):




					"""simple docstring"""


					lowerCAmelCase   					=  parent
					lowerCAmelCase   					=  batch_size
					lowerCAmelCase   					=  seq_length
					lowerCAmelCase   					=  is_training
					lowerCAmelCase   					=  use_token_type_ids
					lowerCAmelCase   					=  use_labels
					lowerCAmelCase   					=  vocab_size
					lowerCAmelCase   					=  hidden_size
					lowerCAmelCase   					=  num_hidden_layers
					lowerCAmelCase   					=  num_attention_heads
					lowerCAmelCase   					=  intermediate_size
					lowerCAmelCase   					=  hidden_act
					lowerCAmelCase   					=  hidden_dropout_prob
					lowerCAmelCase   					=  attention_probs_dropout_prob
					lowerCAmelCase   					=  max_position_embeddings
					lowerCAmelCase   					=  type_vocab_size
					lowerCAmelCase   					=  type_sequence_label_size
					lowerCAmelCase   					=  initializer_range
					lowerCAmelCase   					=  num_labels
					lowerCAmelCase   					=  num_choices
					lowerCAmelCase   					=  scope
					lowerCAmelCase   					=  self.vocab_size - 1



			def 							a_       (   self):




					"""simple docstring"""


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

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

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

					lowerCAmelCase   					=  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       ,    )

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

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



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    *__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  OpenAIGPTModel(config=__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()

					lowerCAmelCase   					=  model(__lowerCAmelCase       ,    token_type_ids=__lowerCAmelCase       ,    head_mask=__lowerCAmelCase)
					lowerCAmelCase   					=  model(__lowerCAmelCase       ,    token_type_ids=__lowerCAmelCase)
					lowerCAmelCase   					=  model(__lowerCAmelCase)

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



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    *__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  OpenAIGPTLMHeadModel(__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()

					lowerCAmelCase   					=  model(__lowerCAmelCase       ,    token_type_ids=__lowerCAmelCase       ,    labels=__lowerCAmelCase)
					self.parent.assertEqual(result.loss.shape       ,    ())
					self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size))



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    *__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  OpenAIGPTDoubleHeadsModel(__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()

					lowerCAmelCase   					=  model(__lowerCAmelCase       ,    token_type_ids=__lowerCAmelCase       ,    labels=__lowerCAmelCase)
					self.parent.assertEqual(result.loss.shape       ,    ())
					self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.seq_length, self.vocab_size))



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    *__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  self.num_labels
					lowerCAmelCase   					=  OpenAIGPTForSequenceClassification(__lowerCAmelCase)
					model.to(__lowerCAmelCase)
					model.eval()

					lowerCAmelCase   					=  ids_tensor([self.batch_size]       ,    self.type_sequence_label_size)
					lowerCAmelCase   					=  model(__lowerCAmelCase       ,    token_type_ids=__lowerCAmelCase       ,    labels=__lowerCAmelCase)
					self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.num_labels))







			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.prepare_config_and_inputs()
					(
					    (
					    lowerCAmelCase
					)					,     (
					    lowerCAmelCase
					)					,     (
					    lowerCAmelCase
					)					,     (
					    lowerCAmelCase
					)					,     (
					    lowerCAmelCase
					)					,     (
					    lowerCAmelCase
					)					,     (
					    lowerCAmelCase
					)					,     
					)   					=  config_and_inputs
					lowerCAmelCase   					=  {
					    """input_ids""": input_ids,
					    """token_type_ids""": token_type_ids,
					    """head_mask""": head_mask,
					}

					return config, inputs_dict







@require_torch
class 				a__(					lowerCAmelCase__ ,						lowerCAmelCase__ ,						lowerCAmelCase__ ,						unittest.TestCase	):


			'''simple docstring'''






			UpperCAmelCase_			:     Optional[Any]      =       (
			    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
			    if is_torch_available()
			    else ()
			)
			UpperCAmelCase_			:     Optional[int]      =       (
			    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
			)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
			UpperCAmelCase_			:     Optional[int]      =       (
			    {
			        '''feature-extraction''': OpenAIGPTModel,
			        '''text-classification''': OpenAIGPTForSequenceClassification,
			        '''text-generation''': OpenAIGPTLMHeadModel,
			        '''zero-shot''': OpenAIGPTForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)



			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




					"""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 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase       ,    __lowerCAmelCase=False):




					"""simple docstring"""


					lowerCAmelCase   					=  super()._prepare_for_class(__lowerCAmelCase       ,    __lowerCAmelCase       ,    return_labels=__lowerCAmelCase)

					if return_labels:
							if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
									lowerCAmelCase   					=  torch.zeros(
									    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)       ,    dtype=torch.long       ,    device=__lowerCAmelCase       ,    )
									lowerCAmelCase   					=  inputs_dict["""labels"""]
									lowerCAmelCase   					=  inputs_dict["""labels"""]
									lowerCAmelCase   					=  torch.zeros(
									    (self.model_tester.batch_size, self.model_tester.num_choices)       ,    dtype=torch.long       ,    device=__lowerCAmelCase       ,    )
									lowerCAmelCase   					=  torch.zeros(
									    self.model_tester.batch_size       ,    dtype=torch.long       ,    device=__lowerCAmelCase)
					return inputs_dict



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  OpenAIGPTModelTester(self)
					lowerCAmelCase   					=  ConfigTester(self       ,    config_class=__lowerCAmelCase       ,    n_embd=37)



			def 							a_       (   self):




					"""simple docstring"""


					self.config_tester.run_common_tests()



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_lm_head_model(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_double_lm_head_model(*__lowerCAmelCase)



			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__lowerCAmelCase)







			@slow
			def 							a_       (   self):




					"""simple docstring"""


					for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							lowerCAmelCase   					=  OpenAIGPTModel.from_pretrained(__lowerCAmelCase)
							self.assertIsNotNone(__lowerCAmelCase)







@require_torch
class 				a__(					unittest.TestCase	):


			'''simple docstring'''






			@slow
			def 							a_       (   self):




					"""simple docstring"""


					lowerCAmelCase   					=  OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt""")
					model.to(__lowerCAmelCase)
					lowerCAmelCase   					=  torch.tensor([[481, 4735, 544]]       ,    dtype=torch.long       ,    device=__lowerCAmelCase)  # the president is
					lowerCAmelCase   					=  [
					    481,
					    4735,
					    544,
					    246,
					    963,
					    870,
					    762,
					    239,
					    244,
					    40477,
					    244,
					    249,
					    719,
					    881,
					    487,
					    544,
					    240,
					    244,
					    603,
					    481,
					]  # the president is a very good man. " \n " i\'m sure he is, " said the

					lowerCAmelCase   					=  model.generate(__lowerCAmelCase       ,    do_sample=__lowerCAmelCase)
					self.assertListEqual(output_ids[0].tolist()       ,    __lowerCAmelCase)





'''simple docstring'''




from typing import List, Optional

import numpy as np

from ...processing_utils import ProcessorMixin
from ...utils import to_numpy






class 				a__(					lowerCAmelCase__	):


			'''simple docstring'''






			UpperCAmelCase_			:     str      =       '''EncodecFeatureExtractor'''
			UpperCAmelCase_			:     Dict      =       ('''T5Tokenizer''', '''T5TokenizerFast''')



			def __init__(   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase):




					"""simple docstring"""


					super().__init__(__lowerCAmelCase       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  self.feature_extractor
					lowerCAmelCase   					=  False



			def 							a_       (   self       ,    __lowerCAmelCase=None       ,    __lowerCAmelCase=None       ,    __lowerCAmelCase=True):




					"""simple docstring"""


					return self.tokenizer.get_decoder_prompt_ids(task=__lowerCAmelCase       ,    language=__lowerCAmelCase       ,    no_timestamps=__lowerCAmelCase)



			def __call__(   self       ,    *__lowerCAmelCase       ,    **__lowerCAmelCase):




					"""simple docstring"""


					if self._in_target_context_manager:
							return self.current_processor(*__lowerCAmelCase       ,    **__lowerCAmelCase)

					lowerCAmelCase   					=  kwargs.pop("""audio"""       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  kwargs.pop("""sampling_rate"""       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  kwargs.pop("""text"""       ,    __lowerCAmelCase)
					if len(__lowerCAmelCase) > 0:
							lowerCAmelCase   					=  args[0]
							lowerCAmelCase   					=  args[1:]

					if audio is None and text is None:
							raise ValueError("""You need to specify either an `audio` or `text` input to process.""")

					if text is not None:
							lowerCAmelCase   					=  self.tokenizer(__lowerCAmelCase       ,    **__lowerCAmelCase)

					if audio is not None:
							lowerCAmelCase   					=  self.feature_extractor(__lowerCAmelCase       ,    *__lowerCAmelCase       ,    sampling_rate=__lowerCAmelCase       ,    **__lowerCAmelCase)

					if audio is None:
							return inputs

					elif text is None:
							return audio_inputs

					else:
							lowerCAmelCase   					=  audio_inputs["""input_values"""]
							if "padding_mask" in audio_inputs:
									lowerCAmelCase   					=  audio_inputs["""padding_mask"""]
							return inputs



			def 							a_       (   self       ,    *__lowerCAmelCase       ,    **__lowerCAmelCase):




					"""simple docstring"""


					lowerCAmelCase   					=  kwargs.pop("""audio"""       ,    __lowerCAmelCase)
					lowerCAmelCase   					=  kwargs.pop("""padding_mask"""       ,    __lowerCAmelCase)

					if len(__lowerCAmelCase) > 0:
							lowerCAmelCase   					=  args[0]
							lowerCAmelCase   					=  args[1:]

					if audio_values is not None:
							return self._decode_audio(__lowerCAmelCase       ,    padding_mask=__lowerCAmelCase)
					else:
							return self.tokenizer.batch_decode(*__lowerCAmelCase       ,    **__lowerCAmelCase)



			def 							a_       (   self       ,    *__lowerCAmelCase       ,    **__lowerCAmelCase):




					"""simple docstring"""


					return self.tokenizer.decode(*__lowerCAmelCase       ,    **__lowerCAmelCase)







			def 							a_       (   self       ,    __lowerCAmelCase       ,    __lowerCAmelCase = None):




					"""simple docstring"""


					lowerCAmelCase   					=  to_numpy(__lowerCAmelCase)
					lowerCAmelCase					,     lowerCAmelCase					,     lowerCAmelCase   					=  audio_values.shape

					if padding_mask is None:
							return list(__lowerCAmelCase)

					lowerCAmelCase   					=  to_numpy(__lowerCAmelCase)

					# match the sequence length of the padding mask to the generated audio arrays by padding with the **non-padding**
					# token (so that the generated audio values are **not** treated as padded tokens)
					lowerCAmelCase   					=  seq_len - padding_mask.shape[-1]
					lowerCAmelCase   					=  1 - self.feature_extractor.padding_value
					lowerCAmelCase   					=  np.pad(__lowerCAmelCase       ,    ((0, 0), (0, difference))       ,    """constant"""       ,    constant_values=__lowerCAmelCase)

					lowerCAmelCase   					=  audio_values.tolist()
					for i in range(__lowerCAmelCase):
							lowerCAmelCase   					=  np.asarray(audio_values[i])[
							    padding_mask[i][None, :] != self.feature_extractor.padding_value
							]
							lowerCAmelCase   					=  sliced_audio.reshape(__lowerCAmelCase       ,    -1)

					return audio_values




