



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 SCREAMING_SNAKE_CASE__					:




    '''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,		):
        A			:	Optional[Any]										=       parent
        A			:	str										=       batch_size
        A			:	Dict										=       seq_length
        A			:	Dict										=       is_training
        A			:	List[str]										=       use_token_type_ids
        A			:	Tuple										=       use_labels
        A			:	Tuple										=       vocab_size
        A			:	Optional[Any]										=       hidden_size
        A			:	List[Any]										=       num_hidden_layers
        A			:	Optional[Any]										=       num_attention_heads
        A			:	Optional[Any]										=       intermediate_size
        A			:	Tuple										=       hidden_act
        A			:	Tuple										=       hidden_dropout_prob
        A			:	List[str]										=       attention_probs_dropout_prob
        A			:	Dict										=       max_position_embeddings
        A			:	Union[str, Any]										=       type_vocab_size
        A			:	int										=       type_sequence_label_size
        A			:	str										=       initializer_range
        A			:	List[Any]										=       num_labels
        A			:	Tuple										=       num_choices
        A			:	Optional[Any]										=       scope
        A			:	int										=       self.vocab_size - 1






    def       _lowerCAmelCase (	self		):
        A			:	List[Any]										=       ids_tensor([self.batch_size, self.seq_length],		self.vocab_size		)

        A			:	List[str]										=       None
        if self.use_token_type_ids:
            A			:	Optional[int]										=       ids_tensor([self.batch_size, self.seq_length],		self.type_vocab_size		)

        A			:	Tuple										=       None
        A			:	Any										=       None
        A			:	List[Any]										=       None
        if self.use_labels:
            A			:	List[str]										=       ids_tensor([self.batch_size],		self.type_sequence_label_size		)
            A			:	Union[str, Any]										=       ids_tensor([self.batch_size, self.seq_length],		self.num_labels		)
            A			:	List[str]										=       ids_tensor([self.batch_size],		self.num_choices		)

        A			:	List[Any]										=       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,		)

        A			:	Optional[Any]										=       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       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		*lowerCamelCase__		):
        A			:	Optional[int]										=       OpenAIGPTModel(config=lowerCamelCase__		)
        model.to(lowerCamelCase__		)
        model.eval()

        A			:	Union[str, Any]										=       model(lowerCamelCase__,		token_type_ids=lowerCamelCase__,		head_mask=lowerCamelCase__		)
        A			:	Union[str, Any]										=       model(lowerCamelCase__,		token_type_ids=lowerCamelCase__		)
        A			:	Dict										=       model(lowerCamelCase__		)

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






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		*lowerCamelCase__		):
        A			:	Optional[int]										=       OpenAIGPTLMHeadModel(lowerCamelCase__		)
        model.to(lowerCamelCase__		)
        model.eval()

        A			:	Union[str, Any]										=       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       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		*lowerCamelCase__		):
        A			:	List[Any]										=       OpenAIGPTDoubleHeadsModel(lowerCamelCase__		)
        model.to(lowerCamelCase__		)
        model.eval()

        A			:	Optional[int]										=       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       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		*lowerCamelCase__		):
        A			:	List[str]										=       self.num_labels
        A			:	Dict										=       OpenAIGPTForSequenceClassification(lowerCamelCase__		)
        model.to(lowerCamelCase__		)
        model.eval()

        A			:	Dict										=       ids_tensor([self.batch_size],		self.type_sequence_label_size		)
        A			:	Dict										=       model(lowerCamelCase__,		token_type_ids=lowerCamelCase__,		labels=lowerCamelCase__		)
        self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)		)






    def       _lowerCAmelCase (	self		):
        A			:	int										=       self.prepare_config_and_inputs()
        (
            (
            A
        )					,							(
            A
        )					,							(
            A
        )					,							(
            A
        )					,							(
            A
        )					,							(
            A
        )					,							(
            A
        )					,							
        )			:	Any										=       config_and_inputs
        A			:	Optional[int]										=       {
            """input_ids""": input_ids,
            """token_type_ids""": token_type_ids,
            """head_mask""": head_mask,
        }

        return config, inputs_dict






@require_torch
class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						SCREAMING_SNAKE_CASE__			,						unittest.TestCase   ):




    '''simple docstring'''






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






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__		):
        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,		lowerCamelCase__,		lowerCamelCase__,		lowerCamelCase__=False		):
        A			:	List[Any]										=       super()._prepare_for_class(lowerCamelCase__,		lowerCamelCase__,		return_labels=lowerCamelCase__		)

        if return_labels:
            if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
                A			:	List[str]										=       torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length),		dtype=torch.long,		device=lowerCamelCase__,		)
                A			:	str										=       inputs_dict["""labels"""]
                A			:	Any										=       inputs_dict["""labels"""]
                A			:	Any										=       torch.zeros(
                    (self.model_tester.batch_size, self.model_tester.num_choices),		dtype=torch.long,		device=lowerCamelCase__,		)
                A			:	List[Any]										=       torch.zeros(
                    self.model_tester.batch_size,		dtype=torch.long,		device=lowerCamelCase__		)
        return inputs_dict






    def       _lowerCAmelCase (	self		):
        A			:	int										=       OpenAIGPTModelTester(self		)
        A			:	str										=       ConfigTester(self,		config_class=lowerCamelCase__,		n_embd=37		)






    def       _lowerCAmelCase (	self		):
        self.config_tester.run_common_tests()






    def       _lowerCAmelCase (	self		):
        A			:	Any										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*lowerCamelCase__		)






    def       _lowerCAmelCase (	self		):
        A			:	Any										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(*lowerCamelCase__		)






    def       _lowerCAmelCase (	self		):
        A			:	List[Any]										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_double_lm_head_model(*lowerCamelCase__		)






    def       _lowerCAmelCase (	self		):
        A			:	str										=       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCamelCase__		)






    @slow
    def       _lowerCAmelCase (	self		):
        for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A			:	Union[str, Any]										=       OpenAIGPTModel.from_pretrained(lowerCamelCase__		)
            self.assertIsNotNone(lowerCamelCase__		)






@require_torch
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    @slow
    def       _lowerCAmelCase (	self		):
        A			:	Optional[int]										=       OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""		)
        model.to(lowerCamelCase__		)
        A			:	Any										=       torch.tensor([[481, 4735, 544]],		dtype=torch.long,		device=lowerCamelCase__		)  # the president is
        A			:	Tuple										=       [
            481,
            4735,
            544,
            246,
            963,
            870,
            762,
            239,
            244,
            4_0477,
            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

        A			:	List[str]										=       model.generate(lowerCamelCase__,		do_sample=lowerCamelCase__		)
        self.assertListEqual(output_ids[0].tolist(),		lowerCamelCase__		)







from random import randint, random




def      __UpperCamelCase     (  _lowerCAmelCase				,					_lowerCAmelCase				,					_lowerCAmelCase				,					_lowerCAmelCase = False				,					_lowerCAmelCase = False				,					_lowerCAmelCase = 5				,					)	->  list:



    """simple docstring"""






    A			:	Any										=       [[-1] * number_of_cells]  # Create a highway without any car
    A			:	Tuple										=       0
    A			:	Dict										=       max(_lowerCAmelCase				,					0		)
    while i < number_of_cells:
        A			:	Any										=       (
            randint(0				,					_lowerCAmelCase		) if random_speed else initial_speed
        )  # Place the cars
        i += (
            randint(1				,					max_speed * 2		) if random_frequency else frequency
        )  # Arbitrary number, may need tuning
    return highway




def      __UpperCamelCase     (  _lowerCAmelCase				,					_lowerCAmelCase		)	->  int:



    """simple docstring"""






    A			:	List[str]										=       0
    A			:	Dict										=       highway_now[car_index + 1 :]
    for cell in range(len(_lowerCAmelCase		)		):  # May need a better name for this
        if cells[cell] != -1:  # If the cell is not empty then
            return distance  # we have the distance we wanted
        distance += 1
    # Here if the car is near the end of the highway
    return distance + get_distance(_lowerCAmelCase				,					-1		)




def      __UpperCamelCase     (  _lowerCAmelCase				,					_lowerCAmelCase				,					_lowerCAmelCase		)	->  list:



    """simple docstring"""






    A			:	str										=       len(_lowerCAmelCase		)
    # Beforce calculations, the highway is empty
    A			:	Any										=       [-1] * number_of_cells

    for car_index in range(_lowerCAmelCase		):
        if highway_now[car_index] != -1:
            # Add 1 to the current speed of the car and cap the speed
            A			:	str										=       min(highway_now[car_index] + 1				,					_lowerCAmelCase		)
            # Number of empty cell before the next car
            A			:	Optional[int]										=       get_distance(_lowerCAmelCase				,					_lowerCAmelCase		) - 1
            # We can't have the car causing an accident
            A			:	Any										=       min(next_highway[car_index]				,					_lowerCAmelCase		)
            if random() < probability:
                # Randomly, a driver will slow down
                A			:	Optional[Any]										=       max(next_highway[car_index] - 1				,					0		)
    return next_highway




def      __UpperCamelCase     (  _lowerCAmelCase				,					_lowerCAmelCase				,					_lowerCAmelCase				,					_lowerCAmelCase		)	->  list:



    """simple docstring"""






    A			:	Any										=       len(highway[0]		)

    for i in range(_lowerCAmelCase		):
        A			:	Optional[int]										=       update(highway[i]				,					_lowerCAmelCase				,					_lowerCAmelCase		)
        A			:	Tuple										=       [-1] * number_of_cells

        for car_index in range(_lowerCAmelCase		):
            A			:	Dict										=       next_speeds_calculated[car_index]
            if speed != -1:
                # Change the position based on the speed (with % to create the loop)
                A			:	Optional[int]										=       (car_index + speed) % number_of_cells
                # Commit the change of position
                A			:	Dict										=       speed
        highway.append(_lowerCAmelCase		)

    return highway


if __name__ == "__main__":
     import doctest

     doctest.testmod()


