






import argparse
import os
from functools import reduce

import fairseq
import torch
from datasets import load_dataset

from transformers import WavaVecaProcessor, logging
from transformers.models.dataavec.configuration_dataavec_audio import DataaVecAudioConfig

# Copied from https://github.com/pytorch/fairseq/blob/main/examples/data2vec/models/data2vec_audio.py
from transformers.models.dataavec.dataavec_audio import DataaVecAudioModel as Dummy  # noqa: F401
from transformers.models.dataavec.modeling_dataavec_audio import DataaVecAudioForCTC, DataaVecAudioModel


logging.set_verbosity_info()
_UpperCAmelCase					:						Optional[int]         =     logging.get_logger(__name__)

_UpperCAmelCase					:						Optional[Any]         =     {
    'post_extract_proj': 'feature_projection.projection',
    'models.0.layer_norm': 'feature_projection.layer_norm',
    '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',
    'w2v_model.layer_norm': 'feature_projection.layer_norm',
    'w2v_encoder.proj': 'lm_head',
    'mask_emb': 'masked_spec_embed',
}
_UpperCAmelCase					:						Optional[Any]         =     [
    'lm_head',
]






def  UpperCAmelCase__						(    lowerCamelCase	:			List[Any]			,lowerCamelCase	:			List[str]			,lowerCamelCase	:			Optional[int]			,lowerCamelCase	:			Union[str, Any]			,lowerCamelCase	:			Tuple    ):
   for attribute in key.split("""."""    ):
      lowerCamelCase									=getattr(lowerCamelCase			,lowerCamelCase    )

   if weight_type is not None:
      lowerCamelCase									=getattr(lowerCamelCase			,lowerCamelCase    ).shape
   else:
      lowerCamelCase									=hf_pointer.shape

   if hf_shape != value.shape:
      raise ValueError(
          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":
      lowerCamelCase									=value
   elif weight_type == "weight_g":
      lowerCamelCase									=value
   elif weight_type == "weight_v":
      lowerCamelCase									=value
   elif weight_type == "bias":
      lowerCamelCase									=value
   else:
      lowerCamelCase									=value

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






def  UpperCAmelCase__						(    lowerCamelCase	:			List[Any]			,lowerCamelCase	:			Union[str, Any]			,lowerCamelCase	:			Union[str, Any]    ):
   lowerCamelCase									=[]
   lowerCamelCase									=fairseq_model.state_dict()

   if not is_headless:
      lowerCamelCase									=hf_model.dataavec_audio.feature_extractor
      lowerCamelCase									=hf_model.dataavec_audio.encoder.pos_conv_embed

   else:
      lowerCamelCase									=hf_model.feature_extractor
      lowerCamelCase									=hf_model.encoder.pos_conv_embed

   for name, value in fairseq_dict.items():
      lowerCamelCase									=False
      if "conv_layers" in name:
         load_conv_layer(
             lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,)
         lowerCamelCase									=True
      elif "pos_conv" in name:
         load_pos_conv_layer(
             lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,)
         lowerCamelCase									=True
      else:
         for key, mapped_key in MAPPING.items():
            if not is_headless:
               lowerCamelCase									="""data2vec_audio.""" + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
            if key in name or key.split("""w2v_model."""    )[-1] == name.split("""."""    )[0]:
               lowerCamelCase									=True
               if "*" in mapped_key:
                  lowerCamelCase									=name.split(lowerCamelCase    )[0].split("""."""    )[-2]
                  lowerCamelCase									=mapped_key.replace("""*"""			,lowerCamelCase    )
               if "weight_g" in name:
                  lowerCamelCase									="""weight_g"""
               elif "weight_v" in name:
                  lowerCamelCase									="""weight_v"""
               elif "bias" in name:
                  lowerCamelCase									="""bias"""
               elif "weight" in name:
                  # TODO: don't match quantizer.weight_proj
                  lowerCamelCase									="""weight"""
               else:
                  lowerCamelCase									=None
               set_recursively(lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,lowerCamelCase    )
            continue
      if not is_used:
         unused_weights.append(lowerCamelCase    )

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






def  UpperCAmelCase__						(    lowerCamelCase	:			Dict			,lowerCamelCase	:			Union[str, Any]    ):
   lowerCamelCase									=path.split("""."""    )
   return reduce(lowerCamelCase			,lowerCamelCase			,lowerCamelCase    )






def  UpperCAmelCase__						(    lowerCamelCase	:			Any			,lowerCamelCase	:			Tuple			,lowerCamelCase	:			List[Any]			,lowerCamelCase	:			Any    ):
   lowerCamelCase									=access_by_string(lowerCamelCase			,lowerCamelCase    )
   lowerCamelCase									=hf_weight.data

   if fsq_value.shape != hf_value.shape:
      raise ValueError(f'''{full_name} has size {fsq_value.shape}, but {hf_value.shape} was found.'''    )
   lowerCamelCase									=fsq_value
   logger.info(f'''{full_name} was correctly initialized from {hf_weight_path}.'''    )






def  UpperCAmelCase__						(    lowerCamelCase	:			Any			,lowerCamelCase	:			str			,lowerCamelCase	:			Any			,lowerCamelCase	:			List[str]    ):
   lowerCamelCase									=full_name.split("""conv_layers."""    )[-1]
   lowerCamelCase									=name.split("""."""    )
   lowerCamelCase									=int(items[0]    )
   lowerCamelCase									=int(items[1]    )

   lowerCamelCase									=name.split("""."""    )[-1]
   if type_id == 0:
      lowerCamelCase									="""conv"""
   elif type_id == 2:
      lowerCamelCase									="""layer_norm"""
   else:
      unused_weights.append(lowerCamelCase    )
      return

   set_weights(lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,f'''conv_layers.{layer_id}.{layer_type}.{weight_type}'''    )






def  UpperCAmelCase__						(    lowerCamelCase	:			int			,lowerCamelCase	:			Dict			,lowerCamelCase	:			List[Any]			,lowerCamelCase	:			Union[str, Any]    ):
   lowerCamelCase									=full_name.split("""pos_conv."""    )[-1]
   lowerCamelCase									=name.split("""."""    )
   lowerCamelCase									=int(items[0]    )
   lowerCamelCase									=int(items[1]    )

   lowerCamelCase									=name.split("""."""    )[-1]
   if type_id != 0:
      unused_weights.append(lowerCamelCase    )
      return
   else:
      lowerCamelCase									="""conv"""

   set_weights(lowerCamelCase			,lowerCamelCase			,lowerCamelCase			,f'''layers.{layer_id}.{layer_type}.{weight_type}'''    )






@torch.no_grad()
def  UpperCAmelCase__						(    lowerCamelCase	:			Dict			,lowerCamelCase	:			Union[str, Any]			,lowerCamelCase	:			Dict=None			,lowerCamelCase	:			Optional[int]=None			,lowerCamelCase	:			Optional[int]=True    ):
   if config_path is not None:
      lowerCamelCase									=DataaVecAudioConfig.from_pretrained(lowerCamelCase    )
   else:
      lowerCamelCase									=DataaVecAudioConfig()

   if not is_finetuned:
      # Modify final_proj layer name
      lowerCamelCase									=DataaVecAudioModel(lowerCamelCase    )
      lowerCamelCase									=os.path.dirname(lowerCamelCase    )

      lowerCamelCase									=torch.load(lowerCamelCase    )
      lowerCamelCase									=state_dict["""model"""].pop("""final_proj.0.weight"""    )
      lowerCamelCase									=state_dict["""model"""].pop("""final_proj.0.bias"""    )
      lowerCamelCase									=os.path.join(lowerCamelCase			,"""converted.pt"""    )
      torch.save(lowerCamelCase			,lowerCamelCase    )
   else:
      lowerCamelCase									=DataaVecAudioForCTC(lowerCamelCase    )
      lowerCamelCase									=checkpoint_path

   def load_dataavec(lowerCamelCase	:			Optional[Any]    ):
      lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=fairseq.checkpoint_utils.load_model_ensemble_and_task([path]    )
      return model[0].eval()

   lowerCamelCase									=load_dataavec(lowerCamelCase    )

   recursively_load_weights(lowerCamelCase			,lowerCamelCase			,not is_finetuned    )

   lowerCamelCase									=WavaVecaProcessor.from_pretrained("""facebook/wav2vec2-large-lv60"""    )

   lowerCamelCase									=load_dataset("""patrickvonplaten/librispeech_asr_dummy"""			,"""clean"""			,split="""validation"""    )
   lowerCamelCase									=[x["""array"""] for x in ds[:4]["""audio"""]]

   lowerCamelCase									=processor(lowerCamelCase			,return_tensors="""pt"""			,padding=lowerCamelCase    )

   lowerCamelCase									=inputs.input_values
   lowerCamelCase									=inputs.attention_mask
   #    input_values = inputs.input_values[:, :-1]
   #    attention_mask = inputs.attention_mask[:, :-1]

   hf_wavavec.eval()
   model.eval()
   if is_finetuned:
      lowerCamelCase									=model(source=lowerCamelCase			,padding_mask=(1 - attention_mask)			,mask=lowerCamelCase			,features_only=lowerCamelCase    )[
          """encoder_out"""
      ].transpose(0			,1    )
      lowerCamelCase									=hf_wavavec(lowerCamelCase			,attention_mask=lowerCamelCase    )["""logits"""]

      lowerCamelCase									=torch.argmax(lowerCamelCase			,dim=-1    )
      lowerCamelCase									=processor.batch_decode(lowerCamelCase    )

      print(f'''Expected Output: {ds[:4]["text"]}, Pred: {output_string}'''    )
   else:
      lowerCamelCase									=model(source=lowerCamelCase			,padding_mask=(1 - attention_mask)			,mask=lowerCamelCase			,features_only=lowerCamelCase    )[
          """layer_results"""
      ][-1][0].transpose(0			,1    )
      lowerCamelCase									=hf_wavavec(lowerCamelCase			,attention_mask=lowerCamelCase    )["""last_hidden_state"""]

   print(our_output.shape			,their_output.shape    )
   lowerCamelCase									=torch.max(torch.abs(our_output - their_output    )    ).item()
   print(f'''max_absolute_diff = {max_absolute_diff}'''    )  # ~ 1e-7
   lowerCamelCase									=torch.allclose(lowerCamelCase			,lowerCamelCase			,atol=1E-3    )
   print("""Do both models output the same tensors?"""			,"""🔥""" if success else """💩"""    )
   if not success:
      raise Exception("""Something went wRoNg"""    )

   hf_wavavec.save_pretrained(lowerCamelCase    )

   if is_finetuned:
      processor.save_pretrained(lowerCamelCase    )
   else:
      processor.feature_extractor.save_pretrained(lowerCamelCase    )


if __name__ == "__main__":
   _UpperCAmelCase					:						Union[str, Any]         =     argparse.ArgumentParser()
   parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
   parser.add_argument('--checkpoint_path', default=None, type=str, help='Path to fairseq checkpoint')
   parser.add_argument('--dict_path', default=None, type=str, help='Path to dict of fine-tuned model')
   parser.add_argument('--config_path', default=None, type=str, help='Path to hf config.json of model to convert')
   parser.add_argument(
       '--not_finetuned', action='store_true', help='Whether the model to convert is a fine-tuned model or not'
   )
   _UpperCAmelCase					:						Any         =     parser.parse_args()
   convert_wavaveca_checkpoint(
       args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
   )







import inspect
import unittest

from transformers import DataaVecVisionConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
   import torch
   from torch import nn

   from transformers import (
       MODEL_MAPPING,
       DataaVecVisionForImageClassification,
       DataaVecVisionForSemanticSegmentation,
       DataaVecVisionModel,
   )
   from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import BeitImageProcessor


class 		lowerCAmelCase_				:





     def __init__(	self							, _UpperCAmelCase							, _UpperCAmelCase=100							, _UpperCAmelCase=13							, _UpperCAmelCase=30							, _UpperCAmelCase=2							, _UpperCAmelCase=3							, _UpperCAmelCase=True							, _UpperCAmelCase=True							, _UpperCAmelCase=32							, _UpperCAmelCase=4							, _UpperCAmelCase=4							, _UpperCAmelCase=37							, _UpperCAmelCase="gelu"							, _UpperCAmelCase=0.1							, _UpperCAmelCase=0.1							, _UpperCAmelCase=10							, _UpperCAmelCase=0.02							, _UpperCAmelCase=3							, _UpperCAmelCase=None							, _UpperCAmelCase=[0, 1, 2, 3]							, )		->				Dict:
        lowerCamelCase									=parent
        lowerCamelCase									=100
        lowerCamelCase									=batch_size
        lowerCamelCase									=image_size
        lowerCamelCase									=patch_size
        lowerCamelCase									=num_channels
        lowerCamelCase									=is_training
        lowerCamelCase									=use_labels
        lowerCamelCase									=hidden_size
        lowerCamelCase									=num_hidden_layers
        lowerCamelCase									=num_attention_heads
        lowerCamelCase									=intermediate_size
        lowerCamelCase									=hidden_act
        lowerCamelCase									=hidden_dropout_prob
        lowerCamelCase									=attention_probs_dropout_prob
        lowerCamelCase									=type_sequence_label_size
        lowerCamelCase									=initializer_range
        lowerCamelCase									=scope
        lowerCamelCase									=out_indices
        lowerCamelCase									=num_labels

        # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        lowerCamelCase									=(image_size // patch_size) ** 2
        lowerCamelCase									=num_patches + 1





     def 						lowerCamelCase							(	self  )		->				List[Any]:
        lowerCamelCase									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

        lowerCamelCase									=None
        lowerCamelCase									=None
        if self.use_labels:
           lowerCamelCase									=ids_tensor([self.batch_size]							, self.type_sequence_label_size  )
           lowerCamelCase									=ids_tensor([self.batch_size, self.image_size, self.image_size]							, self.num_labels  )

        lowerCamelCase									=self.get_config()

        return config, pixel_values, labels, pixel_labels





     def 						lowerCamelCase							(	self  )		->				str:
        return DataaVecVisionConfig(
            vocab_size=self.vocab_size							, image_size=self.image_size							, patch_size=self.patch_size							, num_channels=self.num_channels							, hidden_size=self.hidden_size							, num_hidden_layers=self.num_hidden_layers							, num_attention_heads=self.num_attention_heads							, intermediate_size=self.intermediate_size							, hidden_act=self.hidden_act							, hidden_dropout_prob=self.hidden_dropout_prob							, attention_probs_dropout_prob=self.attention_probs_dropout_prob							, is_decoder=_UpperCAmelCase							, initializer_range=self.initializer_range							, out_indices=self.out_indices							, )





     def 						lowerCamelCase							(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )		->				Union[str, Any]:
        lowerCamelCase									=DataaVecVisionModel(config=_UpperCAmelCase  )
        model.to(_UpperCAmelCase  )
        model.eval()
        lowerCamelCase									=model(_UpperCAmelCase  )
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        lowerCamelCase									=(self.image_size // self.patch_size) ** 2
        self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, num_patches + 1, self.hidden_size)  )





     def 						lowerCamelCase							(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )		->				Any:
        lowerCamelCase									=self.type_sequence_label_size
        lowerCamelCase									=DataaVecVisionForImageClassification(_UpperCAmelCase  )
        model.to(_UpperCAmelCase  )
        model.eval()
        lowerCamelCase									=model(_UpperCAmelCase							, labels=_UpperCAmelCase  )
        self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.type_sequence_label_size)  )





     def 						lowerCamelCase							(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )		->				str:
        lowerCamelCase									=self.num_labels
        lowerCamelCase									=DataaVecVisionForSemanticSegmentation(_UpperCAmelCase  )
        model.to(_UpperCAmelCase  )
        model.eval()
        lowerCamelCase									=model(_UpperCAmelCase  )
        self.parent.assertEqual(
            result.logits.shape							, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)  )
        lowerCamelCase									=model(_UpperCAmelCase							, labels=_UpperCAmelCase  )
        self.parent.assertEqual(
            result.logits.shape							, (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)  )





     def 						lowerCamelCase							(	self  )		->				List[Any]:
        lowerCamelCase									=self.prepare_config_and_inputs()
        lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=config_and_inputs
        lowerCamelCase									={"""pixel_values""": pixel_values}
        return config, inputs_dict




@require_torch
class 		lowerCAmelCase_				(  UpperCamelCase				,     UpperCamelCase				,     unittest.TestCase ):
     __lowerCamelCase       :    Optional[int] 	=     (
         (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     __lowerCamelCase       :    str 	=     (
         {
             '''feature-extraction''': DataaVecVisionModel,
             '''image-classification''': DataaVecVisionForImageClassification,
             '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     __lowerCamelCase       :    Any 	=     False
     __lowerCamelCase       :    List[str] 	=     False
     __lowerCamelCase       :    int 	=     False





     def 						lowerCamelCase							(	self  )		->				Any:
        lowerCamelCase									=DataaVecVisionModelTester(self  )
        lowerCamelCase									=ConfigTester(
            self							, config_class=_UpperCAmelCase							, has_text_modality=_UpperCAmelCase							, hidden_size=37  )





     def 						lowerCamelCase							(	self  )		->				int:
        self.config_tester.run_common_tests()





     def 						lowerCamelCase							(	self  )		->				Dict:
        # Data2VecVision does not use inputs_embeds
        pass





     @require_torch_multi_gpu
     @unittest.skip(
         reason="""Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""  )
     def 						lowerCamelCase							(	self  )		->				Dict:
        pass





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowerCamelCase									=model_class(_UpperCAmelCase  )
           self.assertIsInstance(model.get_input_embeddings()							, (nn.Module)  )
           lowerCamelCase									=model.get_output_embeddings()
           self.assertTrue(x is None or isinstance(_UpperCAmelCase							, nn.Linear  )  )





     def 						lowerCamelCase							(	self  )		->				Union[str, Any]:
        lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
           lowerCamelCase									=model_class(_UpperCAmelCase  )
           lowerCamelCase									=inspect.signature(model.forward  )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           lowerCamelCase									=[*signature.parameters.keys()]

           lowerCamelCase									=["""pixel_values"""]
           self.assertListEqual(arg_names[:1]							, _UpperCAmelCase  )





     def 						lowerCamelCase							(	self  )		->				str:
        lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_UpperCAmelCase  )





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*_UpperCAmelCase  )





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        if not self.model_tester.is_training:
           return

        lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs_for_common()
        lowerCamelCase									=True

        for model_class in self.all_model_classes:
           if model_class in [*get_values(_UpperCAmelCase  )]:
              continue

           lowerCamelCase									=model_class(_UpperCAmelCase  )
           model.to(_UpperCAmelCase  )
           model.train()
           lowerCamelCase									=self._prepare_for_class(_UpperCAmelCase							, _UpperCAmelCase							, return_labels=_UpperCAmelCase  )
           lowerCamelCase									=model(**_UpperCAmelCase  ).loss
           loss.backward()





     def 						lowerCamelCase							(	self  )		->				int:
        lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
           return

        lowerCamelCase									=False
        lowerCamelCase									=True

        for model_class in self.all_model_classes:
           if model_class in [*get_values(_UpperCAmelCase  )] or not model_class.supports_gradient_checkpointing:
              continue
           # TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
           # this can then be incorporated into _prepare_for_class in test_modeling_common.py
           elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
              lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase				,       lowerCamelCase									=inputs_dict["""pixel_values"""].shape
              lowerCamelCase									=torch.zeros(
                  [self.model_tester.batch_size, height, width]							, device=_UpperCAmelCase  ).long()
           lowerCamelCase									=model_class(_UpperCAmelCase  )
           model.gradient_checkpointing_enable()
           model.to(_UpperCAmelCase  )
           model.train()
           lowerCamelCase									=self._prepare_for_class(_UpperCAmelCase							, _UpperCAmelCase							, return_labels=_UpperCAmelCase  )
           lowerCamelCase									=model(**_UpperCAmelCase  ).loss
           loss.backward()





     def 						lowerCamelCase							(	self  )		->				Optional[Any]:
        lowerCamelCase				,       lowerCamelCase									=self.model_tester.prepare_config_and_inputs_for_common()

        lowerCamelCase									=_config_zero_init(_UpperCAmelCase  )
        for model_class in self.all_model_classes:
           lowerCamelCase									=model_class(config=_UpperCAmelCase  )
           for name, param in model.named_parameters():
              # we skip lambda parameters as these require special initial values
              # determined by config.layer_scale_init_value
              if "lambda" in name:
                 continue
              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'''							, )





     def 						lowerCamelCase							(	self							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase=2E-4							, _UpperCAmelCase="outputs"							, _UpperCAmelCase=None  )		->				int:
        # We override with a slightly higher tol value, as semseg models tend to diverge a bit more
        super().check_pt_tf_outputs(_UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase							, _UpperCAmelCase  )





     def 						lowerCamelCase							(	self  )		->				Dict:
        lowerCamelCase									=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_UpperCAmelCase  )





     @slow
     def 						lowerCamelCase							(	self  )		->				List[Any]:
        for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowerCamelCase									=DataaVecVisionModel.from_pretrained(_UpperCAmelCase  )
           self.assertIsNotNone(_UpperCAmelCase  )






def  UpperCAmelCase__						(    ):
   lowerCamelCase									=Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""    )
   return image







@require_torch
@require_vision
class 		lowerCAmelCase_				(  unittest.TestCase ):





     @cached_property
     def 						lowerCamelCase							(	self  )		->				List[str]:
        return (
            BeitImageProcessor.from_pretrained("""facebook/data2vec-vision-base-ft1k"""  ) if is_vision_available() else None
        )





     @slow
     def 						lowerCamelCase							(	self  )		->				Optional[int]:
        lowerCamelCase									=DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""  ).to(
            _UpperCAmelCase  )

        lowerCamelCase									=self.default_image_processor
        lowerCamelCase									=prepare_img()
        lowerCamelCase									=image_processor(images=_UpperCAmelCase							, return_tensors="""pt"""  ).to(_UpperCAmelCase  )

        # forward pass
        with torch.no_grad():
           lowerCamelCase									=model(**_UpperCAmelCase  )
        lowerCamelCase									=outputs.logits

        # verify the logits
        lowerCamelCase									=torch.Size((1, 1000)  )
        self.assertEqual(logits.shape							, _UpperCAmelCase  )

        lowerCamelCase									=torch.tensor([0.3277, -0.1395, 0.0911]  ).to(_UpperCAmelCase  )

        self.assertTrue(torch.allclose(logits[0, :3]							, _UpperCAmelCase							, atol=1E-4  )  )

        lowerCamelCase									=[model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
        self.assertEqual(logits[0].topk(2  ).indices.cpu().tolist()							, _UpperCAmelCase  )