




"""simple docstring"""

import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test





class     lowercase_	(					unittest.TestCase							):



 '''simple docstring'''

 @parameterized.expand([(None,), ('foo.json',)]		)
 def       lowerCAmelCase_							(			self	:	List[Any]   ,							_UpperCAmelCase	:	Optional[Any]		):
       _A        =       GenerationConfig(
           do_sample=_UpperCAmelCase   ,							temperature=0.7   ,							length_penalty=1.0   ,							bad_words_ids=[[1, 2, 3], [4, 5]]   ,							)
       with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(_UpperCAmelCase   ,							config_name=_UpperCAmelCase		)
             _A        =       GenerationConfig.from_pretrained(_UpperCAmelCase   ,							config_name=_UpperCAmelCase		)

       # Checks parameters that were specified
       self.assertEqual(loaded_config.do_sample   ,							_UpperCAmelCase		)
       self.assertEqual(loaded_config.temperature   ,							0.7		)
       self.assertEqual(loaded_config.length_penalty   ,							1.0		)
       self.assertEqual(loaded_config.bad_words_ids   ,							[[1, 2, 3], [4, 5]]		)

       # Checks parameters that were not specified (defaults)
       self.assertEqual(loaded_config.top_k   ,							50		)
       self.assertEqual(loaded_config.max_length   ,							20		)
       self.assertEqual(loaded_config.max_time   ,							_UpperCAmelCase		)

 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       _A        =       AutoConfig.from_pretrained('gpt2'		)
       _A        =       GenerationConfig.from_model_config(_UpperCAmelCase		)
       _A        =       GenerationConfig()

       # The generation config has loaded a few non-default parameters from the model config
       self.assertNotEqual(_UpperCAmelCase   ,							_UpperCAmelCase		)

       # One of those parameters is eos_token_id -- check if it matches
       self.assertNotEqual(generation_config_from_model.eos_token_id   ,							default_generation_config.eos_token_id		)
       self.assertEqual(generation_config_from_model.eos_token_id   ,							model_config.eos_token_id		)

 def       lowerCAmelCase_							(			self	:	Optional[Any]		):
       _A        =       GenerationConfig()
       _A        =       {
           'max_new_tokens': 1_024,
           'foo': 'bar',
       }
       _A        =       copy.deepcopy(_UpperCAmelCase		)
       _A        =       generation_config.update(**_UpperCAmelCase		)

       # update_kwargs was not modified (no side effects)
       self.assertEqual(_UpperCAmelCase   ,							_UpperCAmelCase		)

       # update_kwargs was used to update the config on valid attributes
       self.assertEqual(generation_config.max_new_tokens   ,							1_024		)

       # `.update()` returns a dictionary of unused kwargs
       self.assertEqual(_UpperCAmelCase   ,							{'foo': 'bar'}		)

 def       lowerCAmelCase_							(			self	:	str		):
       _A        =       GenerationConfig()
       _A        =       'bar'

       with tempfile.TemporaryDirectory('test-generation-config'		) as tmp_dir:
             generation_config.save_pretrained(_UpperCAmelCase		)

             _A        =       GenerationConfig.from_pretrained(_UpperCAmelCase		)
       # update_kwargs was used to update the config on valid attributes
       self.assertEqual(new_config.foo   ,							'bar'		)

       _A        =       GenerationConfig.from_model_config(_UpperCAmelCase		)
       assert not hasattr(_UpperCAmelCase   ,							'foo'		)  # no new kwargs should be initialized if from config



 def       lowerCAmelCase_							(			self	:	Union[str, Any]		):
       _A        =       GenerationConfig()
       self.assertEqual(default_config.temperature   ,							1.0		)
       self.assertEqual(default_config.do_sample   ,							_UpperCAmelCase		)
       self.assertEqual(default_config.num_beams   ,							1		)

       _A        =       GenerationConfig(
           do_sample=_UpperCAmelCase   ,							temperature=0.7   ,							length_penalty=1.0   ,							bad_words_ids=[[1, 2, 3], [4, 5]]   ,							)
       self.assertEqual(config.temperature   ,							0.7		)
       self.assertEqual(config.do_sample   ,							_UpperCAmelCase		)
       self.assertEqual(config.num_beams   ,							1		)

       with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(_UpperCAmelCase		)
             _A        =       GenerationConfig.from_pretrained(_UpperCAmelCase   ,							temperature=1.0		)

       self.assertEqual(loaded_config.temperature   ,							1.0		)
       self.assertEqual(loaded_config.do_sample   ,							_UpperCAmelCase		)
       self.assertEqual(loaded_config.num_beams   ,							1		)  # default value





@is_staging_test
class     lowercase_	(					unittest.TestCase							):



 '''simple docstring'''

 @classmethod
 def       lowerCAmelCase_							(			cls	:	Dict		):
       _A        =       TOKEN
       HfFolder.save_token(_UpperCAmelCase		)

 @classmethod
 def       lowerCAmelCase_							(			cls	:	List[Any]		):
       try:
             delete_repo(token=cls._token   ,							repo_id='test-generation-config'		)
       except HTTPError:
             pass

       try:
             delete_repo(token=cls._token   ,							repo_id='valid_org/test-generation-config-org'		)
       except HTTPError:
             pass

 def       lowerCAmelCase_							(			self	:	List[Any]		):
       _A        =       GenerationConfig(
           do_sample=_UpperCAmelCase   ,							temperature=0.7   ,							length_penalty=1.0   ,							)
       config.push_to_hub('test-generation-config'   ,							use_auth_token=self._token		)

       _A        =       GenerationConfig.from_pretrained(F'''{USER}/test-generation-config'''		)
       for k, v in config.to_dict().items():
             if k != "transformers_version":
                   self.assertEqual(_UpperCAmelCase   ,							getattr(_UpperCAmelCase   ,							_UpperCAmelCase		)		)

        # Reset repo
       delete_repo(token=self._token   ,							repo_id='test-generation-config'		)

       # Push to hub via save_pretrained
       with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(
                 _UpperCAmelCase   ,							repo_id='test-generation-config'   ,							push_to_hub=_UpperCAmelCase   ,							use_auth_token=self._token		)

       _A        =       GenerationConfig.from_pretrained(F'''{USER}/test-generation-config'''		)
       for k, v in config.to_dict().items():
             if k != "transformers_version":
                   self.assertEqual(_UpperCAmelCase   ,							getattr(_UpperCAmelCase   ,							_UpperCAmelCase		)		)



 def       lowerCAmelCase_							(			self	:	Any		):
       _A        =       GenerationConfig(
           do_sample=_UpperCAmelCase   ,							temperature=0.7   ,							length_penalty=1.0   ,							)
       config.push_to_hub('valid_org/test-generation-config-org'   ,							use_auth_token=self._token		)

       _A        =       GenerationConfig.from_pretrained('valid_org/test-generation-config-org'		)
       for k, v in config.to_dict().items():
             if k != "transformers_version":
                   self.assertEqual(_UpperCAmelCase   ,							getattr(_UpperCAmelCase   ,							_UpperCAmelCase		)		)

        # Reset repo
       delete_repo(token=self._token   ,							repo_id='valid_org/test-generation-config-org'		)

       # Push to hub via save_pretrained
       with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(
                 _UpperCAmelCase   ,							repo_id='valid_org/test-generation-config-org'   ,							push_to_hub=_UpperCAmelCase   ,							use_auth_token=self._token		)

       _A        =       GenerationConfig.from_pretrained('valid_org/test-generation-config-org'		)
       for k, v in config.to_dict().items():
             if k != "transformers_version":
                   self.assertEqual(_UpperCAmelCase   ,							getattr(_UpperCAmelCase   ,							_UpperCAmelCase		)		)







"""simple docstring"""

import argparse
import os

import torch

from transformers import (
    XLNetConfig,
    XLNetForQuestionAnswering,
    XLNetForSequenceClassification,
    XLNetLMHeadModel,
    load_tf_weights_in_xlnet,
)
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


a         =      {
    '''cola''': 2,
    '''mnli''': 3,
    '''mrpc''': 2,
    '''sst-2''': 2,
    '''sts-b''': 1,
    '''qqp''': 2,
    '''qnli''': 2,
    '''rte''': 2,
    '''wnli''': 2,
}


logging.set_verbosity_info()






def 						_snake_case					( _snake_case							: str    ,  _snake_case							: Optional[int]    ,  _snake_case							: List[Any]    ,  _snake_case							: Tuple=None							)   -> List[str]:






      '''simple docstring'''


      _A        =       XLNetConfig.from_json_file(_snake_case							)

      _A        =       finetuning_task.lower() if finetuning_task is not None else ''
      if finetuning_task in GLUE_TASKS_NUM_LABELS:
            print(F'''Building PyTorch XLNetForSequenceClassification model from configuration: {config}'''							)
            _A        =       finetuning_task
            _A        =       GLUE_TASKS_NUM_LABELS[finetuning_task]
            _A        =       XLNetForSequenceClassification(_snake_case							)
      elif "squad" in finetuning_task:
            _A        =       finetuning_task
            _A        =       XLNetForQuestionAnswering(_snake_case							)
      else:
            _A        =       XLNetLMHeadModel(_snake_case							)

      # Load weights from tf checkpoint
      load_tf_weights_in_xlnet(_snake_case    ,  _snake_case    ,  _snake_case							)

      # Save pytorch-model
      _A        =       os.path.join(_snake_case    ,  _snake_case							)
      _A        =       os.path.join(_snake_case    ,  _snake_case							)
      print(F'''Save PyTorch model to {os.path.abspath(_snake_case							)}'''							)
      torch.save(model.state_dict()    ,  _snake_case							)
      print(F'''Save configuration file to {os.path.abspath(_snake_case							)}'''							)
      with open(_snake_case    ,  'w'    ,  encoding='utf-8'							) as f:
            f.write(config.to_json_string()							)


if __name__ == "__main__":
       a         =      argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '''--tf_checkpoint_path''', default=None, type=str, required=True, help='''Path to the TensorFlow checkpoint path.'''
       )
       parser.add_argument(
           '''--xlnet_config_file''',
           default=None,
           type=str,
           required=True,
           help=(
               '''The config json file corresponding to the pre-trained XLNet model. \n'''
               '''This specifies the model architecture.'''
           ),
       )
       parser.add_argument(
           '''--pytorch_dump_folder_path''',
           default=None,
           type=str,
           required=True,
           help='''Path to the folder to store the PyTorch model or dataset/vocab.''',
       )
       parser.add_argument(
           '''--finetuning_task''',
           default=None,
           type=str,
           help='''Name of a task on which the XLNet TensorFlow model was fine-tuned''',
       )
       a         =      parser.parse_args()
       print(args)

       convert_xlnet_checkpoint_to_pytorch(
           args.tf_checkpoint_path, args.xlnet_config_file, args.pytorch_dump_folder_path, args.finetuning_task
       )

