






"""simple docstring"""



import argparse

import torch
from saprl.hub import distilhubert

from transformers import HubertConfig, HubertModel, WavaVecaFeatureExtractor, logging


logging.set_verbosity_info()
_lowerCAmelCase  :  str   =			logging.get_logger(__name__)

_lowerCAmelCase  :  int   =			{
    """post_extract_proj""": """feature_projection.projection""",
    """encoder.pos_conv.0""": """encoder.pos_conv_embed.conv""",
    """self_attn.k_proj""": """encoder.layers.*.attention.k_proj""",
    """self_attn.v_proj""": """encoder.layers.*.attention.v_proj""",
    """self_attn.q_proj""": """encoder.layers.*.attention.q_proj""",
    """self_attn.out_proj""": """encoder.layers.*.attention.out_proj""",
    """self_attn_layer_norm""": """encoder.layers.*.layer_norm""",
    """fc1""": """encoder.layers.*.feed_forward.intermediate_dense""",
    """fc2""": """encoder.layers.*.feed_forward.output_dense""",
    """final_layer_norm""": """encoder.layers.*.final_layer_norm""",
    """encoder.layer_norm""": """encoder.layer_norm""",
    """mask_emb""": """masked_spec_embed""",
}


def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Tuple ,				snake_case					:   Dict ,				snake_case					:   int ,				snake_case					:   int ,				snake_case					:   int							)->		Union[str, Any]:






     '''simple docstring'''



     for attribute in key.split("."							):
          UpperCAmelCase__	:						Any       =       getattr(snake_case ,				snake_case							)

     if weight_type is not None:
          UpperCAmelCase__	:						Dict       =       getattr(snake_case ,				snake_case							).shape
     else:
          UpperCAmelCase__	:						str       =       hf_pointer.shape

     assert hf_shape == value.shape, (
         f'Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be'
         f' {value.shape} for {full_name}'
     )

     if weight_type == "weight":
          UpperCAmelCase__	:						Optional[Any]       =       value
     elif weight_type == "weight_g":
          UpperCAmelCase__	:						Tuple       =       value
     elif weight_type == "weight_v":
          UpperCAmelCase__	:						Tuple       =       value
     elif weight_type == "bias":
          UpperCAmelCase__	:						str       =       value
     else:
          UpperCAmelCase__	:						int       =       value

     logger.info(f'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.'							)


def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Union[str, Any] ,				snake_case					:   Optional[int]							)->		Dict:






     '''simple docstring'''



     UpperCAmelCase__	:						int       =       []
     UpperCAmelCase__	:						str       =       fairseq_model.state_dict()

     UpperCAmelCase__	:						int       =       hf_model.feature_extractor

     for name, value in fairseq_dict.items():
          UpperCAmelCase__	:						Optional[int]       =       False
          if "conv_layers" in name:
               load_conv_layer(
                   snake_case ,				snake_case ,				snake_case ,				snake_case ,				hf_model.config.feat_extract_norm == "group" ,				)
               UpperCAmelCase__	:						Union[str, Any]       =       True
          else:
               for key, mapped_key in MAPPING.items():
                    UpperCAmelCase__	:						Optional[Any]       =       mapped_key

                    if key in name:
                         UpperCAmelCase__	:						str       =       True
                         if "*" in mapped_key:
                              UpperCAmelCase__	:						Any       =       name.split(snake_case							)[0].split("."							)[-2]
                              UpperCAmelCase__	:						str       =       mapped_key.replace("*" ,				snake_case							)
                         if "weight_g" in name:
                              UpperCAmelCase__	:						List[str]       =       "weight_g"
                         elif "weight_v" in name:
                              UpperCAmelCase__	:						Optional[Any]       =       "weight_v"
                         elif "weight" in name:
                              UpperCAmelCase__	:						List[Any]       =       "weight"
                         elif "bias" in name:
                              UpperCAmelCase__	:						int       =       "bias"
                         else:
                              UpperCAmelCase__	:						Tuple       =       None
                         set_recursively(snake_case ,				snake_case ,				snake_case ,				snake_case ,				snake_case							)
                    continue
          if not is_used:
               unused_weights.append(snake_case							)

     logger.warning(f'Unused weights: {unused_weights}'							)


def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Any ,				snake_case					:   Dict ,				snake_case					:   Dict ,				snake_case					:   List[str] ,				snake_case					:   int							)->		int:






     '''simple docstring'''



     UpperCAmelCase__	:						Any       =       full_name.split("conv_layers."							)[-1]
     UpperCAmelCase__	:						Optional[int]       =       name.split("."							)
     UpperCAmelCase__	:						Dict       =       int(items[0]							)
     UpperCAmelCase__	:						Dict       =       int(items[1]							)

     if type_id == 0:
          if "bias" in name:
               assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
                   f'{full_name} has size {value.shape}, but'
                   f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'
               )
               UpperCAmelCase__	:						str       =       value
               logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.'							)
          elif "weight" in name:
               assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
                   f'{full_name} has size {value.shape}, but'
                   f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'
               )
               UpperCAmelCase__	:						Tuple       =       value
               logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.'							)
     elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
          if "bias" in name:
               assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
                   f'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'
                   " found."
               )
               UpperCAmelCase__	:						str       =       value
               logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'							)
          elif "weight" in name:
               assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
                   f'{full_name} has size {value.shape}, but'
                   f' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'
               )
               UpperCAmelCase__	:						Optional[int]       =       value
               logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'							)
     else:
          unused_weights.append(snake_case							)


def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Tuple							)->		Union[str, Any]:






     '''simple docstring'''



     UpperCAmelCase__	:						Optional[int]       =       HubertConfig()
     UpperCAmelCase__	:						Optional[Any]       =       model.config

     UpperCAmelCase__	:						Optional[int]       =       fs_config.activation_dropout
     UpperCAmelCase__	:						List[str]       =       False
     UpperCAmelCase__	:						Union[str, Any]       =       fs_config.attention_dropout
     UpperCAmelCase__	:						Optional[int]       =       False
     UpperCAmelCase__	:						Tuple       =       eval(fs_config.extractor_conv_feature_layers							)
     UpperCAmelCase__	:						Tuple       =       [x[0] for x in conv_layers]
     UpperCAmelCase__	:						Optional[Any]       =       [x[1] for x in conv_layers]
     UpperCAmelCase__	:						Any       =       [x[2] for x in conv_layers]
     UpperCAmelCase__	:						Union[str, Any]       =       "gelu"
     UpperCAmelCase__	:						List[str]       =       "layer" if fs_config.extractor_mode == "layer_norm" else "group"
     UpperCAmelCase__	:						Any       =       False
     UpperCAmelCase__	:						List[Any]       =       0.0
     UpperCAmelCase__	:						Any       =       0.0
     UpperCAmelCase__	:						Dict       =       fs_config.activation_fn
     UpperCAmelCase__	:						int       =       fs_config.dropout
     UpperCAmelCase__	:						Tuple       =       fs_config.encoder_embed_dim
     UpperCAmelCase__	:						List[Any]       =       0.02
     UpperCAmelCase__	:						List[Any]       =       fs_config.encoder_ffn_embed_dim
     UpperCAmelCase__	:						Optional[Any]       =       1E-5
     UpperCAmelCase__	:						Optional[int]       =       0.0
     UpperCAmelCase__	:						List[Any]       =       fs_config.encoder_attention_heads
     UpperCAmelCase__	:						str       =       fs_config.conv_pos_groups
     UpperCAmelCase__	:						List[str]       =       fs_config.conv_pos
     UpperCAmelCase__	:						str       =       len(snake_case							)
     UpperCAmelCase__	:						Optional[Any]       =       fs_config.encoder_layers

     return config


@torch.no_grad()
def   SCREAMING_SNAKE_CASE__					(     snake_case					:   Union[str, Any] ,				snake_case					:   Union[str, Any]=None							)->		List[str]:






     '''simple docstring'''



     UpperCAmelCase__	:						str       =       distilhubert().model.model

     if config_path is not None:
          UpperCAmelCase__	:						Optional[int]       =       HubertConfig.from_pretrained(snake_case							)
     else:
          UpperCAmelCase__	:						int       =       convert_config(snake_case							)
     UpperCAmelCase__	:						Any       =       model.eval()

     UpperCAmelCase__	:						List[str]       =       WavaVecaFeatureExtractor(
         feature_size=1 ,				sampling_rate=1_6000 ,				padding_value=0 ,				do_normalize=snake_case ,				return_attention_mask=snake_case ,				)
     UpperCAmelCase__	:						List[Any]       =       HubertModel(snake_case							)

     recursively_load_weights(snake_case ,				snake_case							)

     feature_extractor.save_pretrained(snake_case							)
     hf_model.save_pretrained(snake_case							)


if __name__ == "__main__":
 _lowerCAmelCase  :  List[str]   =			argparse.ArgumentParser()
 parser.add_argument("""--pytorch_dump_folder_path""", default=None, type=str, help="""Path to the output PyTorch model.""")
 parser.add_argument("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
 _lowerCAmelCase  :  List[str]   =			parser.parse_args()
 convert_hubert_checkpoint(args.pytorch_dump_folder_path, args.config_path)










"""simple docstring"""



import inspect
import unittest

import requests

from transformers import MgpstrConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch
 from torch import nn

 from transformers import MgpstrForSceneTextRecognition


if is_vision_available():
 from PIL import Image

 from transformers import MgpstrProcessor




class  lowerCAmelCase__ :
   def __init__(		self			:  int       , snake_case__			:  Tuple       , snake_case__			:  Any=False       , snake_case__			:  Optional[Any]=1_3       , snake_case__			:  str=(3_2, 1_2_8)       , snake_case__			:  int=4       , snake_case__			:  int=3       , snake_case__			:  int=2_7       , snake_case__			:  Tuple=3_8       , snake_case__			:  Dict=9_9       , snake_case__			:  Union[str, Any]=9_9       , snake_case__			:  Union[str, Any]=3_2       , snake_case__			:  Optional[Any]=5       , snake_case__			:  Optional[Any]=4       , snake_case__			:  str=4.0       , snake_case__			:  List[Any]=2_5_7       , snake_case__			:  int=None       , ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       parent
        UpperCAmelCase__	:						Union[str, Any]       =       is_training
        UpperCAmelCase__	:						Tuple       =       batch_size
        UpperCAmelCase__	:						List[Any]       =       image_size
        UpperCAmelCase__	:						str       =       patch_size
        UpperCAmelCase__	:						List[str]       =       num_channels
        UpperCAmelCase__	:						Dict       =       max_token_length
        UpperCAmelCase__	:						List[Any]       =       num_character_labels
        UpperCAmelCase__	:						List[Any]       =       num_bpe_labels
        UpperCAmelCase__	:						List[Any]       =       num_wordpiece_labels
        UpperCAmelCase__	:						List[Any]       =       hidden_size
        UpperCAmelCase__	:						Optional[int]       =       num_hidden_layers
        UpperCAmelCase__	:						Union[str, Any]       =       num_attention_heads
        UpperCAmelCase__	:						Union[str, Any]       =       mlp_ratio
        UpperCAmelCase__	:						int       =       patch_embeds_hidden_size
        UpperCAmelCase__	:						int       =       output_hidden_states
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]       )
        UpperCAmelCase__	:						Optional[int]       =       self.get_config()
        return config, pixel_values
   def 			__a       (		self			:  List[Any]       ):


        '''simple docstring'''
        return MgpstrConfig(
            image_size=self.image_size       , patch_size=self.patch_size       , num_channels=self.num_channels       , max_token_length=self.max_token_length       , num_character_labels=self.num_character_labels       , num_bpe_labels=self.num_bpe_labels       , num_wordpiece_labels=self.num_wordpiece_labels       , hidden_size=self.hidden_size       , num_hidden_layers=self.num_hidden_layers       , num_attention_heads=self.num_attention_heads       , mlp_ratio=self.mlp_ratio       , output_hidden_states=self.output_hidden_states       , )
   def 			__a       (		self			:  Any       , snake_case__			:  int       , snake_case__			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       MgpstrForSceneTextRecognition(snake_case__       )
        model.to(snake_case__       )
        model.eval()
        with torch.no_grad():
             UpperCAmelCase__	:						Any       =       model(snake_case__       )
        self.parent.assertEqual(
            generated_ids[0][0].shape       , (self.batch_size, self.max_token_length, self.num_character_labels)       )




   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       self.prepare_config_and_inputs()
        UpperCAmelCase__       ,							UpperCAmelCase__	:						int       =       config_and_inputs
        UpperCAmelCase__	:						List[str]       =       {"pixel_values": pixel_values}
        return config, inputs_dict




@require_torch
class  lowerCAmelCase__ (						__magic_name__       ,	__magic_name__       ,	unittest.TestCase      ):
   SCREAMING_SNAKE_CASE_         =(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
   SCREAMING_SNAKE_CASE_         ={'''feature-extraction''': MgpstrForSceneTextRecognition} if is_torch_available() else {}
   SCREAMING_SNAKE_CASE_         =False

   SCREAMING_SNAKE_CASE_         =False
   SCREAMING_SNAKE_CASE_         =False
   SCREAMING_SNAKE_CASE_         =False
   SCREAMING_SNAKE_CASE_         =False
   def 			__a       (		self			:  str       ):


        '''simple docstring'''
        UpperCAmelCase__	:						int       =       MgpstrModelTester(self       )
        UpperCAmelCase__	:						List[str]       =       ConfigTester(self       , config_class=snake_case__       , has_text_modality=snake_case__       )
   def 			__a       (		self			:  Optional[Any]       ):


        '''simple docstring'''
        self.config_tester.run_common_tests()
   def 			__a       (		self			:  int       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case__       )
   @unittest.skip(reason="MgpstrModel does not use inputs_embeds"       )
   def 			__a       (		self			:  str       ):


        '''simple docstring'''
        pass
   def 			__a       (		self			:  Union[str, Any]       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						Optional[int]       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						Dict       =       model_class(snake_case__       )
             self.assertIsInstance(model.get_input_embeddings()       , (nn.Module)       )
             UpperCAmelCase__	:						Optional[Any]       =       model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(snake_case__       , nn.Linear       )       )
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						Any       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						List[str]       =       model_class(snake_case__       )
             UpperCAmelCase__	:						int       =       inspect.signature(model.forward       )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             UpperCAmelCase__	:						Any       =       [*signature.parameters.keys()]

             UpperCAmelCase__	:						Optional[Any]       =       ["pixel_values"]
             self.assertListEqual(arg_names[:1]       , snake_case__       )
   @unittest.skip(reason="MgpstrModel does not support feedforward chunking"       )
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        pass
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						List[str]       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             if not model_class.supports_gradient_checkpointing:
                  continue

             UpperCAmelCase__	:						Union[str, Any]       =       True
             UpperCAmelCase__	:						Union[str, Any]       =       model_class(snake_case__       )
             self.assertTrue(model.is_gradient_checkpointing       )
   def 			__a       (		self			:  Any       ):


        '''simple docstring'''
        def check_hidden_states_output(snake_case__			:  List[Any]       , snake_case__			:  List[Any]       , snake_case__			:  List[str]       ):
             UpperCAmelCase__	:						int       =       model_class(snake_case__       )
             model.to(snake_case__       )
             model.eval()

             with torch.no_grad():
                  UpperCAmelCase__	:						Tuple       =       model(**self._prepare_for_class(snake_case__       , snake_case__       )       )

             UpperCAmelCase__	:						List[str]       =       outputs.hidden_states

             UpperCAmelCase__	:						Tuple       =       getattr(
                 self.model_tester       , "expected_num_hidden_layers"       , self.model_tester.num_hidden_layers + 1       )
             self.assertEqual(len(snake_case__       )       , snake_case__       )

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]       )       , [self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]       , )

        UpperCAmelCase__       ,							UpperCAmelCase__	:						Dict       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						Tuple       =       True
             check_hidden_states_output(snake_case__       , snake_case__       , snake_case__       )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             UpperCAmelCase__	:						Dict       =       True

             check_hidden_states_output(snake_case__       , snake_case__       , snake_case__       )
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						Tuple       =       self.model_tester.prepare_config_and_inputs_for_common()

        UpperCAmelCase__	:						Dict       =       _config_zero_init(snake_case__       )
        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						int       =       model_class(config=snake_case__       )
             for name, param in model.named_parameters():
                  if isinstance(snake_case__       , (nn.Linear, nn.Convad, nn.LayerNorm)       ):
                       if param.requires_grad:
                            self.assertIn(
                                ((param.data.mean() * 1e9).round() / 1e9).item()       , [0.0, 1.0]       , msg=f'Parameter {name} of model {model_class} seems not properly initialized'       , )




   @unittest.skip(reason="Retain_grad is tested in individual model tests"       )
   def 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        pass




def   SCREAMING_SNAKE_CASE__					(     )->		List[str]:






     '''simple docstring'''



     UpperCAmelCase__	:						List[str]       =       "https://i.postimg.cc/ZKwLg2Gw/367-14.png"
     UpperCAmelCase__	:						Union[str, Any]       =       Image.open(requests.get(snake_case ,				stream=snake_case							).raw							).convert("RGB"							)
     return im




@require_vision
@require_torch
class  lowerCAmelCase__ (						unittest.TestCase      ):
   @slow
   def 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[int]       =       "alibaba-damo/mgp-str-base"
        UpperCAmelCase__	:						Dict       =       MgpstrForSceneTextRecognition.from_pretrained(snake_case__       ).to(snake_case__       )
        UpperCAmelCase__	:						int       =       MgpstrProcessor.from_pretrained(snake_case__       )

        UpperCAmelCase__	:						Dict       =       prepare_img()
        UpperCAmelCase__	:						List[str]       =       processor(images=snake_case__       , return_tensors="pt"       ).pixel_values.to(snake_case__       )

        # forward pass
        with torch.no_grad():
             UpperCAmelCase__	:						List[str]       =       model(snake_case__       )

        # verify the logits
        self.assertEqual(outputs.logits[0].shape       , torch.Size((1, 2_7, 3_8)       )       )

        UpperCAmelCase__	:						List[str]       =       processor.batch_decode(outputs.logits       )
        UpperCAmelCase__	:						Dict       =       "ticket"

        self.assertEqual(out_strs["generated_text"][0]       , snake_case__       )

        UpperCAmelCase__	:						int       =       torch.tensor(
            [[[-39.5397, -44.4024, -36.1844], [-61.4709, -63.8639, -58.3454], [-74.0225, -68.5494, -71.2164]]]       , device=snake_case__       , )

        self.assertTrue(torch.allclose(outputs.logits[0][:, 1:4, 1:4]       , snake_case__       , atol=1e-4       )       )


