"""simple docstring"""




import argparse

import torch

from transformers import BertForMaskedLM


if __name__ == "__main__":
     _a   :	Optional[Any] 					=     argparse.ArgumentParser(
         description=(
             'Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned'
             ' Distillation'
         )
     )
     parser.add_argument('--model_type', default='bert', choices=['bert'])
     parser.add_argument('--model_name', default='bert-base-uncased', type=str)
     parser.add_argument('--dump_checkpoint', default='serialization_dir/tf_bert-base-uncased_0247911.pth', type=str)
     parser.add_argument('--vocab_transform', action='store_true')
     _a   :	Any 					=     parser.parse_args()

     if args.model_type == "bert":
          _a   :	Any 					=     BertForMaskedLM.from_pretrained(args.model_name)
          _a   :	str 					=     'bert'
     else:
          raise ValueError('args.model_type should be "bert".')

     _a   :	Tuple 					=     model.state_dict()
     _a   :	Optional[int] 					=     {}

     for w in ["word_embeddings", "position_embeddings"]:
          _a   :	str 					=     state_dict[F"""{prefix}.embeddings.{w}.weight"""]
     for w in ["weight", "bias"]:
          _a   :	Any 					=     state_dict[F"""{prefix}.embeddings.LayerNorm.{w}"""]

     _a   :	List[str] 					=     0
     for teacher_idx in [0, 2, 4, 7, 9, 11]:
          for w in ["weight", "bias"]:
               _a   :	Dict 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}"""
               ]
               _a   :	Tuple 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}"""
               ]
               _a   :	Optional[Any] 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}"""
               ]

               _a   :	Optional[int] 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}"""
               ]
               _a   :	List[str] 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}"""
               ]

               _a   :	str 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}"""
               ]
               _a   :	Dict 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}"""
               ]
               _a   :	Optional[int] 					=     state_dict[
                   F"""{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}"""
               ]
          std_idx += 1

     _a   :	int 					=     state_dict['cls.predictions.decoder.weight']
     _a   :	List[str] 					=     state_dict['cls.predictions.bias']
     if args.vocab_transform:
          for w in ["weight", "bias"]:
               _a   :	Optional[int] 					=     state_dict[F"""cls.predictions.transform.dense.{w}"""]
               _a   :	Tuple 					=     state_dict[F"""cls.predictions.transform.LayerNorm.{w}"""]

     print(F"""N layers selected for distillation: {std_idx}""")
     print(F"""Number of params transferred for distillation: {len(compressed_sd.keys())}""")

     print(F"""Save transferred checkpoint to {args.dump_checkpoint}.""")
     torch.save(compressed_sd, args.dump_checkpoint)





"""simple docstring"""




import unittest

import numpy as np

from transformers import MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING, TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
from transformers.pipelines import AudioClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torchaudio,
    slow,
)

from .test_pipelines_common import ANY
@is_pipeline_test
class 		__A							(				unittest.TestCase	):
     _UpperCamelCase							:    str	      =	MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING
     _UpperCamelCase							:    Any	      =	TF_MODEL_FOR_AUDIO_CLASSIFICATION_MAPPING






     def 					__A						(	self       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: List[Any]						    =  AudioClassificationPipeline(model=a__       ,  feature_extractor=a__						)

       # test with a raw waveform
       _lowerCAmelCase		: Optional[int]						    =  np.zeros((34000,)						)
       _lowerCAmelCase		: Optional[Any]						    =  np.zeros((14000,)						)
       return audio_classifier, [audioa, audio]






     def 					__A						(	self       ,  a__       ,  a__						):
       _lowerCAmelCase    ,			_lowerCAmelCase		: List[Any]						    =  examples
       _lowerCAmelCase		: List[Any]						    =  audio_classifier(a__						)
       # by default a model is initialized with num_labels=2
       self.assertEqual(
           a__       ,  [
               {"""score""": ANY(a__						), """label""": ANY(a__						)},
               {"""score""": ANY(a__						), """label""": ANY(a__						)},
           ]       ,  )
       _lowerCAmelCase		: Tuple						    =  audio_classifier(a__       ,  top_k=1						)
       self.assertEqual(
           a__       ,  [
               {"""score""": ANY(a__						), """label""": ANY(a__						)},
           ]       ,  )

       self.run_torchaudio(a__						)






     @require_torchaudio
     def 					__A						(	self       ,  a__						):
       import datasets

       # test with a local file
       _lowerCAmelCase		: int						    =  datasets.load_dataset("""hf-internal-testing/librispeech_asr_dummy"""       ,  """clean"""       ,  split="""validation"""						)
       _lowerCAmelCase		: List[Any]						    =  dataset[0]["""audio"""]["""array"""]
       _lowerCAmelCase		: str						    =  audio_classifier(a__						)
       self.assertEqual(
           a__       ,  [
               {"""score""": ANY(a__						), """label""": ANY(a__						)},
               {"""score""": ANY(a__						), """label""": ANY(a__						)},
           ]       ,  )






     @require_torch
     def 					__A						(	self						):
       _lowerCAmelCase		: int						    =  """anton-l/wav2vec2-random-tiny-classifier"""

       _lowerCAmelCase		: Optional[Any]						    =  pipeline("""audio-classification"""       ,  model=a__						)

       _lowerCAmelCase		: Any						    =  np.ones((8000,)						)
       _lowerCAmelCase		: List[str]						    =  audio_classifier(a__       ,  top_k=4						)

       _lowerCAmelCase		: List[str]						    =  [
           {"""score""": 0.0_8_4_2, """label""": """no"""},
           {"""score""": 0.0_8_3_8, """label""": """up"""},
           {"""score""": 0.0_8_3_7, """label""": """go"""},
           {"""score""": 0.0_8_3_4, """label""": """right"""},
       ]
       _lowerCAmelCase		: str						    =  [
           {"""score""": 0.0_8_4_5, """label""": """stop"""},
           {"""score""": 0.0_8_4_4, """label""": """on"""},
           {"""score""": 0.0_8_4_1, """label""": """right"""},
           {"""score""": 0.0_8_3_4, """label""": """left"""},
       ]
       self.assertIn(nested_simplify(a__       ,  decimals=4						)       ,  [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]						)

       _lowerCAmelCase		: int						    =  {"""array""": np.ones((8000,)						), """sampling_rate""": audio_classifier.feature_extractor.sampling_rate}
       _lowerCAmelCase		: int						    =  audio_classifier(a__       ,  top_k=4						)
       self.assertIn(nested_simplify(a__       ,  decimals=4						)       ,  [EXPECTED_OUTPUT, EXPECTED_OUTPUT_PT_2]						)






     @require_torch
     @slow
     def 					__A						(	self						):
       import datasets

       _lowerCAmelCase		: Optional[Any]						    =  """superb/wav2vec2-base-superb-ks"""

       _lowerCAmelCase		: List[str]						    =  pipeline("""audio-classification"""       ,  model=a__						)
       _lowerCAmelCase		: str						    =  datasets.load_dataset("""anton-l/superb_dummy"""       ,  """ks"""       ,  split="""test"""						)

       _lowerCAmelCase		: Optional[Any]						    =  np.array(dataset[3]["""speech"""]       ,  dtype=np.floataa						)
       _lowerCAmelCase		: List[str]						    =  audio_classifier(a__       ,  top_k=4						)
       self.assertEqual(
           nested_simplify(a__       ,  decimals=3						)       ,  [
               {"""score""": 0.9_8_1, """label""": """go"""},
               {"""score""": 0.0_0_7, """label""": """up"""},
               {"""score""": 0.0_0_6, """label""": """_unknown_"""},
               {"""score""": 0.0_0_1, """label""": """down"""},
           ]       ,  )






     @require_tf
     @unittest.skip("""Audio classification is not implemented for TF"""						)
     def 					__A						(	self						):
       pass




