






"""simple docstring"""







from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
  from transformers import (
      AutoConfig,
      BertConfig,
      GPTaConfig,
      TaConfig,
      TFAutoModel,
      TFAutoModelForCausalLM,
      TFAutoModelForMaskedLM,
      TFAutoModelForPreTraining,
      TFAutoModelForQuestionAnswering,
      TFAutoModelForSeqaSeqLM,
      TFAutoModelForSequenceClassification,
      TFAutoModelWithLMHead,
      TFBertForMaskedLM,
      TFBertForPreTraining,
      TFBertForQuestionAnswering,
      TFBertForSequenceClassification,
      TFBertModel,
      TFGPTaLMHeadModel,
      TFRobertaForMaskedLM,
      TFTaForConditionalGeneration,
  )
  from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
  from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
  from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
  from transformers import (
      AutoModel,
      AutoModelForCausalLM,
      AutoModelForMaskedLM,
      AutoModelForPreTraining,
      AutoModelForQuestionAnswering,
      AutoModelForSeqaSeqLM,
      AutoModelForSequenceClassification,
      AutoModelWithLMHead,
      BertForMaskedLM,
      BertForPreTraining,
      BertForQuestionAnswering,
      BertForSequenceClassification,
      BertModel,
      GPTaLMHeadModel,
      RobertaForMaskedLM,
      TaForConditionalGeneration,
  )







@is_pt_tf_cross_test
class  lowerCamelCase__ (	unittest.TestCase			):






    @slow
    def     _UpperCamelCase							(			self ):
          # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          for model_name in ["bert-base-uncased"]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModel.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModel.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          for model_name in ["bert-base-uncased"]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelForPreTraining.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelForPreTraining.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelForCausalLM.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                UpperCAmelCase      , UpperCAmelCase          =	TFAutoModelForCausalLM.from_pretrained(
                    lowerCAmelCase_     ,output_loading_info=lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelForCausalLM.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                UpperCAmelCase      , UpperCAmelCase          =	AutoModelForCausalLM.from_pretrained(
                    lowerCAmelCase_     ,output_loading_info=lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelForMaskedLM.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                UpperCAmelCase      , UpperCAmelCase          =	TFAutoModelForMaskedLM.from_pretrained(
                    lowerCAmelCase_     ,output_loading_info=lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelForMaskedLM.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                UpperCAmelCase      , UpperCAmelCase          =	AutoModelForMaskedLM.from_pretrained(
                    lowerCAmelCase_     ,output_loading_info=lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                UpperCAmelCase      , UpperCAmelCase          =	TFAutoModelForSeqaSeqLM.from_pretrained(
                    lowerCAmelCase_     ,output_loading_info=lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelForSeqaSeqLM.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                UpperCAmelCase      , UpperCAmelCase          =	AutoModelForSeqaSeqLM.from_pretrained(
                    lowerCAmelCase_     ,output_loading_info=lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          for model_name in ["bert-base-uncased"]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelForSequenceClassification.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    @slow
    def     _UpperCamelCase							(			self ):
          # for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          for model_name in ["bert-base-uncased"]:
                UpperCAmelCase          =	AutoConfig.from_pretrained(lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	TFAutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )

                UpperCAmelCase          =	AutoModelForQuestionAnswering.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
                self.assertIsNotNone(lowerCAmelCase_ )
                self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
          self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )
          self.assertEqual(model.num_parameters()     ,14_410 )
          self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_ )     ,14_410 )

          UpperCAmelCase          =	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
          self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )
          self.assertEqual(model.num_parameters()     ,14_410 )
          self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_ )     ,14_410 )






    def     _UpperCamelCase							(			self ):
          UpperCAmelCase          =	TFAutoModelWithLMHead.from_pretrained(lowerCAmelCase_     ,from_pt=lowerCAmelCase_ )
          self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )
          self.assertEqual(model.num_parameters()     ,14_410 )
          self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_ )     ,14_410 )

          UpperCAmelCase          =	AutoModelWithLMHead.from_pretrained(lowerCAmelCase_     ,from_tf=lowerCAmelCase_ )
          self.assertIsInstance(lowerCAmelCase_     ,lowerCAmelCase_ )
          self.assertEqual(model.num_parameters()     ,14_410 )
          self.assertEqual(model.num_parameters(only_trainable=lowerCAmelCase_ )     ,14_410 )











"""simple docstring"""







import argparse

import torch

from transformers import (
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaForAudioFrameClassification,
    WavaVecaForSequenceClassification,
    WavaVecaForXVector,
    logging,
)


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

def _a			(		_snake_case				,	_snake_case				,	_snake_case							):







      """simple docstring"""






      UpperCAmelCase          =	WavaVecaForSequenceClassification.from_pretrained(_snake_case				,	config=_snake_case							)
      UpperCAmelCase          =	downstream_dict["""projector.weight"""]
      UpperCAmelCase          =	downstream_dict["""projector.bias"""]
      UpperCAmelCase          =	downstream_dict["""model.post_net.linear.weight"""]
      UpperCAmelCase          =	downstream_dict["""model.post_net.linear.bias"""]
      return model

def _a			(		_snake_case				,	_snake_case				,	_snake_case							):







      """simple docstring"""






      UpperCAmelCase          =	WavaVecaForAudioFrameClassification.from_pretrained(_snake_case				,	config=_snake_case							)
      UpperCAmelCase          =	downstream_dict["""model.linear.weight"""]
      UpperCAmelCase          =	downstream_dict["""model.linear.bias"""]
      return model

def _a			(		_snake_case				,	_snake_case				,	_snake_case							):







      """simple docstring"""






      UpperCAmelCase          =	WavaVecaForXVector.from_pretrained(_snake_case				,	config=_snake_case							)
      UpperCAmelCase          =	downstream_dict["""connector.weight"""]
      UpperCAmelCase          =	downstream_dict["""connector.bias"""]
      for i, kernel_size in enumerate(hf_config.tdnn_kernel							):
            UpperCAmelCase          =	downstream_dict[
                F'''model.framelevel_feature_extractor.module.{i}.kernel.weight'''
            ]
            UpperCAmelCase          =	downstream_dict[F'''model.framelevel_feature_extractor.module.{i}.kernel.bias''']

      UpperCAmelCase          =	downstream_dict["""model.utterancelevel_feature_extractor.linear1.weight"""]
      UpperCAmelCase          =	downstream_dict["""model.utterancelevel_feature_extractor.linear1.bias"""]
      UpperCAmelCase          =	downstream_dict["""model.utterancelevel_feature_extractor.linear2.weight"""]
      UpperCAmelCase          =	downstream_dict["""model.utterancelevel_feature_extractor.linear2.bias"""]
      UpperCAmelCase          =	downstream_dict["""objective.W"""]
      return model

@torch.no_grad()
def _a			(		_snake_case				,	_snake_case				,	_snake_case				,	_snake_case							):







      """simple docstring"""






      UpperCAmelCase          =	torch.load(_snake_case				,	map_location="""cpu"""							)

      UpperCAmelCase          =	checkpoint["""Downstream"""]

      UpperCAmelCase          =	WavaVecaConfig.from_pretrained(_snake_case							)
      UpperCAmelCase          =	WavaVecaFeatureExtractor.from_pretrained(
          _snake_case				,	return_attention_mask=_snake_case				,	do_normalize=_snake_case							)

      UpperCAmelCase          =	hf_config.architectures[0]
      if arch.endswith("""ForSequenceClassification"""							):
            UpperCAmelCase          =	convert_classification(_snake_case				,	_snake_case				,	_snake_case							)
      elif arch.endswith("""ForAudioFrameClassification"""							):
            UpperCAmelCase          =	convert_diarization(_snake_case				,	_snake_case				,	_snake_case							)
      elif arch.endswith("""ForXVector"""							):
            UpperCAmelCase          =	convert_xvector(_snake_case				,	_snake_case				,	_snake_case							)
      else:
            raise NotImplementedError(F'''S3PRL weights conversion is not supported for {arch}'''							)

      if hf_config.use_weighted_layer_sum:
            UpperCAmelCase          =	checkpoint["""Featurizer"""]["""weights"""]

      hf_feature_extractor.save_pretrained(_snake_case							)
      hf_model.save_pretrained(_snake_case							)


if __name__ == "__main__":
  _UpperCamelCase					      =  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.""")
  _UpperCamelCase					      =  parser.parse_args()
  convert_saprl_checkpoint(args.base_model_name, args.config_path, args.checkpoint_path, args.model_dump_path)



