





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    _UpperCAmelCase							:
      def __init__(		self      ,       a__      ,       a__=13      ,       a__=7      ,       a__=True      ,       a__=True      ,       a__=True      ,       a__=99      ,       a__=32      ,       a__=5      ,       a__=4      ,       a__=37      ,       a__="gelu"      ,       a__=0.1      ,       a__=0.1      ,       a__=512      ,       a__=16      ,       a__=2      ,       a__=0.02      ,       a__=3      ,       a__=4      ,       a__=None      ,       ):
         A_     :				Tuple			      =	parent
         A_     :				Tuple			      =	batch_size
         A_     :				Optional[Any]			      =	seq_length
         A_     :				int			      =	is_training
         A_     :				int			      =	use_token_type_ids
         A_     :				Dict			      =	use_labels
         A_     :				str			      =	vocab_size
         A_     :				Optional[Any]			      =	hidden_size
         A_     :				str			      =	num_hidden_layers
         A_     :				Dict			      =	num_attention_heads
         A_     :				Tuple			      =	intermediate_size
         A_     :				Union[str, Any]			      =	hidden_act
         A_     :				Tuple			      =	hidden_dropout_prob
         A_     :				str			      =	attention_probs_dropout_prob
         A_     :				List[Any]			      =	max_position_embeddings
         A_     :				str			      =	type_vocab_size
         A_     :				Optional[Any]			      =	type_sequence_label_size
         A_     :				Dict			      =	initializer_range
         A_     :				str			      =	num_labels
         A_     :				Any			      =	num_choices
         A_     :				List[str]			      =	scope
         A_     :				Tuple			      =	self.vocab_size - 1
      def 		_lowerCamelCase      (		self       ):
         A_     :				List[Any]			      =	ids_tensor([self.batch_size, self.seq_length]      ,       self.vocab_size       )

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

         A_     :				str			      =	None
         A_     :				Optional[Any]			      =	None
         A_     :				int			      =	None
         if self.use_labels:
            A_     :				Union[str, Any]			      =	ids_tensor([self.batch_size]      ,       self.type_sequence_label_size       )
            A_     :				Optional[Any]			      =	ids_tensor([self.batch_size, self.seq_length]      ,       self.num_labels       )
            A_     :				int			      =	ids_tensor([self.batch_size]      ,       self.num_choices       )

         A_     :				List[str]			      =	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_     :				Union[str, 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      ,       a__      ,       a__      ,       a__      ,       a__      ,       *a__       ):
         A_     :				List[Any]			      =	OpenAIGPTModel(config=a__       )
         model.to(a__       )
         model.eval()

         A_     :				Tuple			      =	model(a__      ,       token_type_ids=a__      ,       head_mask=a__       )
         A_     :				List[Any]			      =	model(a__      ,       token_type_ids=a__       )
         A_     :				Optional[Any]			      =	model(a__       )

         self.parent.assertEqual(result.last_hidden_state.shape      ,       (self.batch_size, self.seq_length, self.hidden_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       *a__       ):
         A_     :				List[Any]			      =	OpenAIGPTLMHeadModel(a__       )
         model.to(a__       )
         model.eval()

         A_     :				Optional[Any]			      =	model(a__      ,       token_type_ids=a__      ,       labels=a__       )
         self.parent.assertEqual(result.loss.shape      ,       ()       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       *a__       ):
         A_     :				List[str]			      =	OpenAIGPTDoubleHeadsModel(a__       )
         model.to(a__       )
         model.eval()

         A_     :				Optional[Any]			      =	model(a__      ,       token_type_ids=a__      ,       labels=a__       )
         self.parent.assertEqual(result.loss.shape      ,       ()       )
         self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.seq_length, self.vocab_size)       )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       *a__       ):
         A_     :				List[Any]			      =	self.num_labels
         A_     :				int			      =	OpenAIGPTForSequenceClassification(a__       )
         model.to(a__       )
         model.eval()

         A_     :				List[Any]			      =	ids_tensor([self.batch_size]      ,       self.type_sequence_label_size       )
         A_     :				List[str]			      =	model(a__      ,       token_type_ids=a__      ,       labels=a__       )
         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_
         )      ,				
         )     :				Optional[Any]			      =	config_and_inputs
         A_     :				List[str]			      =	{
             """input_ids""": input_ids,
             """token_type_ids""": token_type_ids,
             """head_mask""": head_mask,
         }

         return config, inputs_dict






@require_torch
class    _UpperCAmelCase							(		_lowerCamelCase			,       _lowerCamelCase			,       _lowerCamelCase			,       unittest.TestCase     ):
      a						       =							(
          (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
          if is_torch_available()
          else ()
      )
      a						       =							(
          (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
      a						       =							(
          {
              '''feature-extraction''': OpenAIGPTModel,
              '''text-classification''': OpenAIGPTForSequenceClassification,
              '''text-generation''': OpenAIGPTLMHeadModel,
              '''zero-shot''': OpenAIGPTForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      def 		_lowerCamelCase      (		self      ,       a__      ,       a__      ,       a__      ,       a__      ,       a__       ):
         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      ,       a__      ,       a__      ,       a__=False       ):
         A_     :				List[str]			      =	super()._prepare_for_class(a__      ,       a__      ,       return_labels=a__       )

         if return_labels:
            if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
               A_     :				Optional[Any]			      =	torch.zeros(
                   (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)      ,       dtype=torch.long      ,       device=a__      ,       )
               A_     :				int			      =	inputs_dict["""labels"""]
               A_     :				Optional[Any]			      =	inputs_dict["""labels"""]
               A_     :				Union[str, Any]			      =	torch.zeros(
                   (self.model_tester.batch_size, self.model_tester.num_choices)      ,       dtype=torch.long      ,       device=a__      ,       )
               A_     :				Any			      =	torch.zeros(
                   self.model_tester.batch_size      ,       dtype=torch.long      ,       device=a__       )
         return inputs_dict
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	OpenAIGPTModelTester(self       )
         A_     :				str			      =	ConfigTester(self      ,       config_class=a__      ,       n_embd=37       )
      def 		_lowerCamelCase      (		self       ):
         self.config_tester.run_common_tests()
      def 		_lowerCamelCase      (		self       ):
         A_     :				int			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_model(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				Tuple			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_lm_head_model(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				Dict			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_double_lm_head_model(*a__       )
      def 		_lowerCamelCase      (		self       ):
         A_     :				Union[str, Any]			      =	self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a__       )






      @slow
      def 		_lowerCamelCase      (		self       ):
         for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_     :				Union[str, Any]			      =	OpenAIGPTModel.from_pretrained(a__       )
            self.assertIsNotNone(a__       )






@require_torch
class    _UpperCAmelCase							(		unittest.TestCase     ):
      @slow
      def 		_lowerCamelCase      (		self       ):
         A_     :				str			      =	OpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""       )
         model.to(a__       )
         A_     :				Optional[int]			      =	torch.tensor([[481, 4735, 544]]      ,       dtype=torch.long      ,       device=a__       )  # the president is
         A_     :				Tuple			      =	[
             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

         A_     :				int			      =	model.generate(a__      ,       do_sample=a__       )
         self.assertListEqual(output_ids[0].tolist()      ,       a__       )













# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


_lowerCAmelCase 				=     """3"""

print("""Python version:""", sys.version)
print("""transformers version:""", transformers.__version__)

try:
      import torch

      print("""Torch version:""", torch.__version__)
      print("""Cuda available:""", torch.cuda.is_available())
      print("""Cuda version:""", torch.version.cuda)
      print("""CuDNN version:""", torch.backends.cudnn.version())
      print("""Number of GPUs available:""", torch.cuda.device_count())
      print("""NCCL version:""", torch.cuda.nccl.version())
except ImportError:
      print("""Torch version:""", None)

try:
      import deepspeed

      print("""DeepSpeed version:""", deepspeed.__version__)
except ImportError:
      print("""DeepSpeed version:""", None)

try:
      import tensorflow as tf

      print("""TensorFlow version:""", tf.__version__)
      print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU""")))
      print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU""")))
except ImportError:
      print("""TensorFlow version:""", None)






