



'''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 				lowercase			:


 """simple docstring"""


 def __init__(				self    ,a_    ,)						->  Tuple:
        _UpperCAmelCase    :		Union[str, Any]     	=					parent
        _UpperCAmelCase    :		int     	=					13
        _UpperCAmelCase    :		str     	=					7
        _UpperCAmelCase    :		Tuple     	=					True
        _UpperCAmelCase    :		List[Any]     	=					True
        _UpperCAmelCase    :		Union[str, Any]     	=					True
        _UpperCAmelCase    :		List[str]     	=					True
        _UpperCAmelCase    :		Tuple     	=					True
        _UpperCAmelCase    :		List[str]     	=					99
        _UpperCAmelCase    :		Dict     	=					32
        _UpperCAmelCase    :		List[str]     	=					2
        _UpperCAmelCase    :		Union[str, Any]     	=					4
        _UpperCAmelCase    :		Dict     	=					37
        _UpperCAmelCase    :		Any     	=					"""gelu"""
        _UpperCAmelCase    :		Tuple     	=					0.1
        _UpperCAmelCase    :		str     	=					0.1
        _UpperCAmelCase    :		str     	=					512
        _UpperCAmelCase    :		Optional[Any]     	=					16
        _UpperCAmelCase    :		Optional[int]     	=					2
        _UpperCAmelCase    :		Any     	=					0.02
        _UpperCAmelCase    :		str     	=					3
        _UpperCAmelCase    :		Dict     	=					4
        _UpperCAmelCase    :		int     	=					None
        _UpperCAmelCase    :		str     	=					self.vocab_size - 1







 def _snake_case					(				self				)						->  List[Any]:
        _UpperCAmelCase    :		List[str]     	=					ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size				)

        _UpperCAmelCase    :		List[str]     	=					None
        if self.use_input_mask:
               _UpperCAmelCase    :		int     	=					random_attention_mask([self.batch_size, self.seq_length]				)

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

        _UpperCAmelCase    :		Union[str, Any]     	=					None
        if self.use_mc_token_ids:
               _UpperCAmelCase    :		Optional[Any]     	=					ids_tensor([self.batch_size, self.num_choices]    ,self.seq_length				)

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

        _UpperCAmelCase    :		Optional[int]     	=					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    ,)

        _UpperCAmelCase    :		str     	=					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 _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  Optional[Any]:
        _UpperCAmelCase    :		str     	=					TFOpenAIGPTModel(config=a_				)
        _UpperCAmelCase    :		Optional[Any]     	=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
        _UpperCAmelCase    :		Any     	=					model(a_				)

        _UpperCAmelCase    :		Dict     	=					[input_ids, input_mask]
        _UpperCAmelCase    :		Optional[Any]     	=					model(a_				)

        _UpperCAmelCase    :		Tuple     	=					model(a_				)

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







 def _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  int:
        _UpperCAmelCase    :		int     	=					TFOpenAIGPTLMHeadModel(config=a_				)
        _UpperCAmelCase    :		Any     	=					{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}
        _UpperCAmelCase    :		Union[str, Any]     	=					model(a_				)
        self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)				)







 def _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  List[str]:
        _UpperCAmelCase    :		Any     	=					TFOpenAIGPTDoubleHeadsModel(config=a_				)

        _UpperCAmelCase    :		Dict     	=					tf.tile(tf.expand_dims(a_    ,1				)    ,(1, self.num_choices, 1)				)
        _UpperCAmelCase    :		int     	=					tf.tile(tf.expand_dims(a_    ,1				)    ,(1, self.num_choices, 1)				)
        _UpperCAmelCase    :		Optional[int]     	=					tf.tile(tf.expand_dims(a_    ,1				)    ,(1, self.num_choices, 1)				)

        _UpperCAmelCase    :		Dict     	=					{
            """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,
        }
        _UpperCAmelCase    :		Dict     	=					model(a_				)
        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 _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_    ,*a_				)						->  List[Any]:
        _UpperCAmelCase    :		List[str]     	=					self.num_labels
        _UpperCAmelCase    :		Any     	=					ids_tensor([self.batch_size]    ,self.type_sequence_label_size				)
        _UpperCAmelCase    :		Union[str, Any]     	=					{
            """input_ids""": input_ids,
            """attention_mask""": input_mask,
            """token_type_ids""": token_type_ids,
            """labels""": sequence_labels,
        }
        _UpperCAmelCase    :		Union[str, Any]     	=					TFOpenAIGPTForSequenceClassification(a_				)
        _UpperCAmelCase    :		Dict     	=					model(a_				)
        self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)				)







 def _snake_case					(				self				)						->  Optional[Any]:
        _UpperCAmelCase    :		Any     	=					self.prepare_config_and_inputs()

        (
            (
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,(
            _UpperCAmelCase
        )	,
        )    :		Any     	=					config_and_inputs

        _UpperCAmelCase    :		List[str]     	=					{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
        return config, inputs_dict







@require_tf
class 				lowercase			(    _lowerCamelCase   ,   _lowerCamelCase   ,   unittest.TestCase							):


 """simple docstring"""


 UpperCAmelCase		      =    (
     (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
     if is_tf_available()
     else ()
 )
 UpperCAmelCase		      =    (
     (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 UpperCAmelCase		      =    (
     {
         """feature-extraction""": TFOpenAIGPTModel,
         """text-classification""": TFOpenAIGPTForSequenceClassification,
         """text-generation""": TFOpenAIGPTLMHeadModel,
         """zero-shot""": TFOpenAIGPTForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 UpperCAmelCase		      =    False
 UpperCAmelCase		      =    False







 def _snake_case					(				self    ,a_    ,a_    ,a_    ,a_    ,a_				)						->  Optional[Any]:
        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 _snake_case					(				self				)						->  Union[str, Any]:
        _UpperCAmelCase    :		Any     	=					TFOpenAIGPTModelTester(self				)
        _UpperCAmelCase    :		int     	=					ConfigTester(self    ,config_class=a_    ,n_embd=37				)







 def _snake_case					(				self				)						->  Optional[Any]:
        self.config_tester.run_common_tests()







 def _snake_case					(				self				)						->  List[str]:
        _UpperCAmelCase    :		List[Any]     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*a_				)







 def _snake_case					(				self				)						->  Tuple:
        _UpperCAmelCase    :		List[str]     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*a_				)







 def _snake_case					(				self				)						->  List[Any]:
        _UpperCAmelCase    :		int     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*a_				)







 def _snake_case					(				self				)						->  int:
        _UpperCAmelCase    :		Dict     	=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a_				)







 @slow
 def _snake_case					(				self				)						->  Optional[Any]:
        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _UpperCAmelCase    :		Tuple     	=					TFOpenAIGPTModel.from_pretrained(a_				)
               self.assertIsNotNone(a_				)







@require_tf
class 				lowercase			(    unittest.TestCase							):


 """simple docstring"""


 @slow
 def _snake_case					(				self				)						->  int:
        _UpperCAmelCase    :		List[str]     	=					TFOpenAIGPTLMHeadModel.from_pretrained("""openai-gpt"""				)
        _UpperCAmelCase    :		Dict     	=					tf.convert_to_tensor([[481, 4_735, 544]]    ,dtype=tf.intaa				)  # the president is
        _UpperCAmelCase    :		Any     	=					[
            481,
            4_735,
            544,
            246,
            963,
            870,
            762,
            239,
            244,
            40_477,
            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

        _UpperCAmelCase    :		List[Any]     	=					model.generate(a_    ,do_sample=a_				)
        self.assertListEqual(output_ids[0].numpy().tolist()    ,a_				)




'''simple docstring'''




import argparse

import torch

from transformers import HubertConfig, HubertForSequenceClassification, WavaVecaFeatureExtractor, logging


logging.set_verbosity_info()
_A										= logging.get_logger(__name__)

_A										= ['UtteranceLevel']

@torch.no_grad()
def _UpperCamelCase (							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_				):
     lowercase_       :							List[str]  					=       torch.load(SCREAMING_SNAKE_CASE_	,							map_location='cpu'				)
     if checkpoint["Config"]["downstream_expert"]["modelrc"]["select"] not in SUPPORTED_MODELS:
          raise NotImplementedError(f'''The supported s3prl models are {SUPPORTED_MODELS}'''				)

     lowercase_       :							Dict  					=       checkpoint['Downstream']

     lowercase_       :							List[Any]  					=       HubertConfig.from_pretrained(SCREAMING_SNAKE_CASE_				)
     lowercase_       :							List[Any]  					=       HubertForSequenceClassification.from_pretrained(SCREAMING_SNAKE_CASE_	,							config=SCREAMING_SNAKE_CASE_				)
     lowercase_       :							List[str]  					=       WavaVecaFeatureExtractor.from_pretrained(
         SCREAMING_SNAKE_CASE_	,							return_attention_mask=SCREAMING_SNAKE_CASE_	,							do_normalize=SCREAMING_SNAKE_CASE_				)

     if hf_congfig.use_weighted_layer_sum:
          lowercase_       :							List[str]  					=       checkpoint['Featurizer']['weights']

     lowercase_       :							Tuple  					=       downstream_dict['projector.weight']
     lowercase_       :							List[Any]  					=       downstream_dict['projector.bias']
     lowercase_       :							List[Any]  					=       downstream_dict['model.post_net.linear.weight']
     lowercase_       :							str  					=       downstream_dict['model.post_net.linear.bias']

     hf_feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE_				)
     hf_model.save_pretrained(SCREAMING_SNAKE_CASE_				)


if __name__ == "__main__":
 _A										= argparse.ArgumentParser()
 parser.add_argument(
     '--base_model_name', default=None, type=str, help='Name of the huggingface pretrained base model.'
 )
 parser.add_argument('--config_path', default=None, type=str, help='Path to the huggingface classifier config.')
 parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to the s3prl checkpoint.')
 parser.add_argument('--model_dump_path', default=None, type=str, help='Path to the final converted model.')
 _A										= parser.parse_args()
 convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)




