


'''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   ,					__magic_name__   ,					)       ->				Dict:
      _a         =				parent
      _a         =				13
      _a         =				7
      _a         =				True
      _a         =				True
      _a         =				True
      _a         =				True
      _a         =				True
      _a         =				99
      _a         =				32
      _a         =				2
      _a         =				4
      _a         =				37
      _a         =				'gelu'
      _a         =				0.1
      _a         =				0.1
      _a         =				5_12
      _a         =				16
      _a         =				2
      _a         =				0.0_2
      _a         =				3
      _a         =				4
      _a         =				None
      _a         =				self.vocab_size - 1
 def 				__UpperCAmelCase		(			self    )       ->				Any:
      _a         =				ids_tensor([self.batch_size, self.seq_length]   ,					self.vocab_size    )

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

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

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

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

      _a         =				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         =				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   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					*__magic_name__    )       ->				str:
      _a         =				TFOpenAIGPTModel(config=__magic_name__    )
      _a         =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      _a         =				model(__magic_name__    )

      _a         =				[input_ids, input_mask]
      _a         =				model(__magic_name__    )

      _a         =				model(__magic_name__    )

      self.parent.assertEqual(result.last_hidden_state.shape   ,					(self.batch_size, self.seq_length, self.hidden_size)    )
 def 				__UpperCAmelCase		(			self   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					*__magic_name__    )       ->				List[Any]:
      _a         =				TFOpenAIGPTLMHeadModel(config=__magic_name__    )
      _a         =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      _a         =				model(__magic_name__    )
      self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.seq_length, self.vocab_size)    )
 def 				__UpperCAmelCase		(			self   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					*__magic_name__    )       ->				int:
      _a         =				TFOpenAIGPTDoubleHeadsModel(config=__magic_name__    )

      _a         =				tf.tile(tf.expand_dims(__magic_name__   ,					1    )   ,					(1, self.num_choices, 1)    )
      _a         =				tf.tile(tf.expand_dims(__magic_name__   ,					1    )   ,					(1, self.num_choices, 1)    )
      _a         =				tf.tile(tf.expand_dims(__magic_name__   ,					1    )   ,					(1, self.num_choices, 1)    )

      _a         =				{
          '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,
      }
      _a         =				model(__magic_name__    )
      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   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					*__magic_name__    )       ->				str:
      _a         =				self.num_labels
      _a         =				ids_tensor([self.batch_size]   ,					self.type_sequence_label_size    )
      _a         =				{
          'input_ids': input_ids,
          'attention_mask': input_mask,
          'token_type_ids': token_type_ids,
          'labels': sequence_labels,
      }
      _a         =				TFOpenAIGPTForSequenceClassification(__magic_name__    )
      _a         =				model(__magic_name__    )
      self.parent.assertEqual(result.logits.shape   ,					(self.batch_size, self.num_labels)    )




 def 				__UpperCAmelCase		(			self    )       ->				Optional[Any]:
      _a         =				self.prepare_config_and_inputs()

      (
          _a	
      )         =				config_and_inputs

      _a         =				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
      return config, inputs_dict





@require_tf
class 			a  (  _SCREAMING_SNAKE_CASE	,					_SCREAMING_SNAKE_CASE	,					unittest.TestCase							):
 _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 				__UpperCAmelCase		(			self   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__   ,					__magic_name__    )       ->				int:




      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    )       ->				int:
      _a         =				TFOpenAIGPTModelTester(self    )
      _a         =				ConfigTester(self   ,					config_class=__magic_name__   ,					n_embd=37    )
 def 				__UpperCAmelCase		(			self    )       ->				List[str]:
      self.config_tester.run_common_tests()
 def 				__UpperCAmelCase		(			self    )       ->				Dict:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_model(*__magic_name__    )
 def 				__UpperCAmelCase		(			self    )       ->				int:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_lm_head(*__magic_name__    )
 def 				__UpperCAmelCase		(			self    )       ->				List[str]:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_double_head(*__magic_name__    )
 def 				__UpperCAmelCase		(			self    )       ->				Union[str, Any]:
      _a         =				self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__magic_name__    )




 @slow
 def 				__UpperCAmelCase		(			self    )       ->				int:




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





@require_tf
class 			a  (  unittest.TestCase							):
 @slow
 def 				__UpperCAmelCase		(			self    )       ->				List[str]:
      _a         =				TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt'    )
      _a         =				tf.convert_to_tensor([[4_81, 47_35, 5_44]]   ,					dtype=tf.intaa    )  # the president is
      _a         =				[
          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

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









"""simple docstring"""


# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# 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.

from pathlib import Path

from .config_args import default_config_file, load_config_from_file
from .config_utils import SubcommandHelpFormatter


_UpperCamelCase							:       int       					=		'Update an existing config file with the latest defaults while maintaining the old configuration.'




def      snake_case    (A_ :str ):







  '''simple docstring'''

  a	:     int							=  args.config_file
  if config_file is None and Path(A_ ).exists():
    a	:     List[Any]							=  default_config_file
  elif not Path(A_ ).exists():
    raise ValueError(f'''The passed config file located at {config_file} doesn\'t exist.''' )
  a	:     Dict							=  load_config_from_file(A_ )

  if config_file.endswith('.json' ):
    config.to_json_file(A_ )
  else:
    config.to_yaml_file(A_ )
  return config_file




def      snake_case    (A_ :List[str]      ,     A_ :Union[str, Any] ):







  '''simple docstring'''

  a	:     int							=  parser.add_parser('update'      ,     parents=A_      ,     help=A_      ,     formatter_class=A_ )
  parser.add_argument(
      '--config_file'      ,     default=A_      ,     help=(
          'The path to the config file to update. Will default to a file named default_config.yaml in the cache '
          'location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '
          'such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '
          'with \'huggingface\'.'
      )      ,     )

  parser.set_defaults(func=A_ )
  return parser






def      snake_case    (A_ :List[str] ):







  '''simple docstring'''

  a	:     List[str]							=  update_config(A_ )
  print(f'''Sucessfully updated the configuration file at {config_file}.''' )




