




'''simple docstring'''






import argparse

import fairseq
import torch
from torch import nn

from transformers import (
    MBartaaTokenizer,
    MBartConfig,
    MBartForCausalLM,
    SpeechEncoderDecoderConfig,
    SpeechEncoderDecoderModel,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaModel,
    logging,
)


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

A_			      =		{
    "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",
    "w2v_model.layer_norm": "feature_projection.layer_norm",
    "quantizer.weight_proj": "quantizer.weight_proj",
    "quantizer.vars": "quantizer.codevectors",
    "project_q": "project_q",
    "final_proj": "project_hid",
    "w2v_encoder.proj": "lm_head",
    "mask_emb": "masked_spec_embed",
}
A_			      =		[
    "lm_head",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
]






def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)    ->   Dict:
      for attribute in key.split('.'		):
            snake_case__       :   Any	      =    getattr(__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)

      if weight_type is not None:
            snake_case__       :   Dict	      =    getattr(__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		).shape
      else:
            snake_case__       :   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":
            snake_case__       :   int	      =    value
      elif weight_type == "weight_g":
            snake_case__       :   List[str]	      =    value
      elif weight_type == "weight_v":
            snake_case__       :   Union[str, Any]	      =    value
      elif weight_type == "bias":
            snake_case__       :   Any	      =    value
      else:
            snake_case__       :   Optional[int]	      =    value

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






def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)    ->   Optional[Any]:
      snake_case__       :   Optional[Any]	      =    []
      snake_case__       :   Optional[Any]	      =    fairseq_model.state_dict()

      snake_case__       :   Dict	      =    hf_model.feature_extractor
      snake_case__       :   Union[str, Any]	      =    hf_model.adapter

      for name, value in fairseq_dict.items():
            snake_case__       :   Union[str, Any]	      =    False
            if "conv_layers" in name:
                  load_conv_layer(
                      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      hf_model.config.feat_extract_norm == 'group'				,      )
                  snake_case__       :   Tuple	      =    True
            elif any(x in name for x in ['adaptor', 'w2v_encoder.proj.', 'w2v_proj_ln.']		):
                  load_adapter(__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)
                  snake_case__       :   Union[str, Any]	      =    True
            else:
                  for key, mapped_key in MAPPING.items():
                        if key in name or key.split('w2v_model.'		)[-1] == name.split('.'		)[0]:
                              snake_case__       :   List[str]	      =    True
                              if "*" in mapped_key:
                                    snake_case__       :   str	      =    name.split(__SCREAMING_SNAKE_CASE		)[0].split('.'		)[-2]
                                    snake_case__       :   Optional[Any]	      =    mapped_key.replace('*'				,      __SCREAMING_SNAKE_CASE		)
                              if "weight_g" in name:
                                    snake_case__       :   List[str]	      =    'weight_g'
                              elif "weight_v" in name:
                                    snake_case__       :   Dict	      =    'weight_v'
                              elif "bias" in name:
                                    snake_case__       :   Optional[int]	      =    'bias'
                              elif "weight" in name:
                                    snake_case__       :   Optional[Any]	      =    'weight'
                              else:
                                    snake_case__       :   Union[str, Any]	      =    None
                              set_recursively(__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)
                        continue
            if not is_used:
                  unused_weights.append(__SCREAMING_SNAKE_CASE		)

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






def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)    ->   Union[str, Any]:
      snake_case__       :   Tuple	      =    full_name.split('conv_layers.'		)[-1]
      snake_case__       :   Any	      =    name.split('.'		)
      snake_case__       :   Optional[int]	      =    int(items[0]		)
      snake_case__       :   Any	      =    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."
                  )
                  snake_case__       :   Union[str, Any]	      =    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."
                  )
                  snake_case__       :   List[str]	      =    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."
                  )
                  snake_case__       :   List[Any]	      =    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."
                  )
                  snake_case__       :   Any	      =    value
                  logger.info(f"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."		)
      else:
            unused_weights.append(__SCREAMING_SNAKE_CASE		)






def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		)    ->   Dict:
      snake_case__       :   Dict	      =    full_name.split('adaptor.'		)[-1]
      snake_case__       :   Optional[Any]	      =    name.split('.'		)

      if items[1].isdigit():
            snake_case__       :   List[Any]	      =    int(items[1]		)
      else:
            snake_case__       :   List[str]	      =    None

      if "adaptor" not in full_name:
            if "proj_ln" in full_name:
                  # has to be layer norm
                  if "bias" in name:
                        assert (
                            value.shape == adapter.proj_layer_norm.bias.data.shape
                        ), f"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.bias.data.shape} was found."
                        snake_case__       :   Union[str, Any]	      =    value
                        logger.info(f"Adapter proj layer norm bias was initialized from {full_name}."		)
                  if "weight" in name:
                        assert (
                            value.shape == adapter.proj_layer_norm.weight.data.shape
                        ), f"{full_name} has size {value.shape}, but {adapter.proj_layer_norm.weight.data.shape} was found."
                        snake_case__       :   List[str]	      =    value
            else:
                  # has to be projection layer
                  if "bias" in name:
                        assert (
                            value.shape == adapter.proj.bias.data.shape
                        ), f"{full_name} has size {value.shape}, but {adapter.proj.bias.data.shape} was found."
                        snake_case__       :   Any	      =    value
                        logger.info(f"Adapter proj layer bias was initialized from {full_name}."		)
                  if "weight" in name:
                        assert (
                            value.shape == adapter.proj.weight.data.shape
                        ), f"{full_name} has size {value.shape}, but {adapter.proj.weight.data.shape} was found."
                        snake_case__       :   int	      =    value
                        logger.info(f"Adapter proj layer weight was initialized from {full_name}."		)
      elif isinstance(__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE		):
            if "bias" in name:
                  assert (
                      value.shape == adapter.layers[layer_id].conv.bias.data.shape
                  ), f"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.bias.data.shape} was found."
                  snake_case__       :   Union[str, Any]	      =    value
                  logger.info(f"Adapter layer {layer_id} bias was initialized from {full_name}."		)
            elif "weight" in name:
                  assert (
                      value.shape == adapter.layers[layer_id].conv.weight.data.shape
                  ), f"{full_name} has size {value.shape}, but {adapter.layers[layer_id].conv.weight.data.shape} was found."
                  snake_case__       :   Dict	      =    value
                  logger.info(f"Adapter layer {layer_id} bias was initialized from {full_name}."		)
      else:
            unused_weights.append(__SCREAMING_SNAKE_CASE		)






def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE		)    ->   List[str]:
      snake_case__      ,							snake_case__       :   Optional[Any]	      =    emb.weight.shape
      snake_case__       :   Union[str, Any]	      =    nn.Linear(__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      bias=__SCREAMING_SNAKE_CASE		)
      snake_case__       :   Optional[Any]	      =    emb.weight.data
      return lin_layer






@torch.no_grad()
def 		UpperCamelCase__      (		__SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      __SCREAMING_SNAKE_CASE				,      )    ->   Dict:
      snake_case__       :   str	      =    WavaVecaConfig.from_pretrained(
          __SCREAMING_SNAKE_CASE				,      add_adapter=__SCREAMING_SNAKE_CASE				,      adapter_stride=__SCREAMING_SNAKE_CASE				,      adapter_kernel_size=__SCREAMING_SNAKE_CASE				,      use_auth_token=__SCREAMING_SNAKE_CASE				,      output_hidden_size=__SCREAMING_SNAKE_CASE				,      )
      snake_case__       :   Dict	      =    MBartConfig.from_pretrained(__SCREAMING_SNAKE_CASE		)

      # load model
      snake_case__      ,							snake_case__      ,							snake_case__       :   Optional[Any]	      =    fairseq.checkpoint_utils.load_model_ensemble_and_task(
          [checkpoint_path]				,      arg_overrides={
              'config_yaml': config_yaml_path,
              'data': '/'.join(dict_path.split('/'		)[:-1]		),
              'w2v_path': checkpoint_path,
              'load_pretrained_decoder_from': None,
          }				,      )
      snake_case__       :   Any	      =    model[0].eval()

      # load feature extractor
      snake_case__       :   List[Any]	      =    WavaVecaFeatureExtractor.from_pretrained(__SCREAMING_SNAKE_CASE				,      use_auth_token=__SCREAMING_SNAKE_CASE		)

      # set weights for wav2vec2 encoder
      snake_case__       :   Union[str, Any]	      =    WavaVecaModel(__SCREAMING_SNAKE_CASE		)

      recursively_load_weights_wavaveca(model.encoder				,      __SCREAMING_SNAKE_CASE		)

      # load decoder weights
      snake_case__       :   Optional[Any]	      =    MBartForCausalLM(__SCREAMING_SNAKE_CASE		)
      snake_case__      ,							snake_case__       :   Union[str, Any]	      =    hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict()				,      strict=__SCREAMING_SNAKE_CASE		)
      logger.warning(f"The following keys are missing when loading the decoder weights: {missing_keys}"		)
      logger.warning(f"The following keys are unexpected when loading the decoder weights: {unexpected_keys}"		)

      snake_case__       :   Optional[int]	      =    SpeechEncoderDecoderModel(encoder=__SCREAMING_SNAKE_CASE				,      decoder=__SCREAMING_SNAKE_CASE		)
      snake_case__       :   List[Any]	      =    False

      snake_case__       :   List[str]	      =    MBartaaTokenizer(__SCREAMING_SNAKE_CASE		)
      tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE		)

      snake_case__       :   List[Any]	      =    hf_wavavec.config.to_dict()
      snake_case__       :   Tuple	      =    tokenizer.pad_token_id
      snake_case__       :   Any	      =    tokenizer.bos_token_id
      snake_case__       :   Optional[int]	      =    tokenizer.eos_token_id
      snake_case__       :   Dict	      =    'mbart50'
      snake_case__       :   Dict	      =    'wav2vec2'

      snake_case__       :   Tuple	      =    tokenizer.eos_token_id
      snake_case__       :   Optional[Any]	      =    25_0004
      snake_case__       :   List[str]	      =    tokenizer.eos_token_id

      snake_case__       :   str	      =    SpeechEncoderDecoderConfig.from_dict(__SCREAMING_SNAKE_CASE		)

      hf_wavavec.save_pretrained(__SCREAMING_SNAKE_CASE		)
      feature_extractor.save_pretrained(__SCREAMING_SNAKE_CASE		)


if __name__ == "__main__":
      A_			      =		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_yaml_path", default=None, type=str, help="Path to yaml file of fine-tuned model")
      parser.add_argument(
          "--encoder_config_path",
          default="facebook/wav2vec2-xls-r-1b",
          type=str,
          help="Path to hf encoder wav2vec2 checkpoint config",
      )
      parser.add_argument(
          "--decoder_config_path",
          default="facebook/mbart-large-50-one-to-many-mmt",
          type=str,
          help="Path to hf decoder checkpoint config",
      )
      parser.add_argument("--add_adapter", default=True, type=bool, help="whethere to add model adapter layers")
      parser.add_argument("--adapter_stride", default=2, type=int, help="stride of adapter layers")
      parser.add_argument("--adapter_kernel_size", default=3, type=int, help="kernel size of adapter layers")
      parser.add_argument("--encoder_output_dim", default=1024, type=int, help="encoder output dim")
      parser.add_argument("--start_token_id", default=250004, type=int, help="`decoder_start_token_id` of model config")

      A_			      =		parser.parse_args()
      convert_wavaveca_checkpoint(
          args.checkpoint_path,
          args.pytorch_dump_folder_path,
          args.dict_path,
          args.config_yaml_path,
          encoder_config_path=args.encoder_config_path,
          decoder_config_path=args.decoder_config_path,
          add_adapter=args.add_adapter,
          adapter_kernel_size=args.adapter_kernel_size,
          adapter_stride=args.adapter_stride,
          decoder_start_token_id=args.start_token_id,
          encoder_output_dim=args.encoder_output_dim,
      )






'''simple docstring'''






import inspect
import unittest

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

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


if is_torch_available():
      import torch

      from transformers import ConvNextBackbone, ConvNextForImageClassification, ConvNextModel
      from transformers.models.convnext.modeling_convnext import CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor






class        lowercase_      :





     def __init__(     self     :					Tuple     ,							__lowerCamelCase     :					Optional[Any]     ,							__lowerCamelCase     :					Optional[Any]=13     ,							__lowerCamelCase     :					List[Any]=32     ,							__lowerCamelCase     :					List[str]=3     ,							__lowerCamelCase     :					Any=4     ,							__lowerCamelCase     :					str=[10, 20, 30, 40]     ,							__lowerCamelCase     :					Any=[2, 2, 3, 2]     ,							__lowerCamelCase     :					List[Any]=True     ,							__lowerCamelCase     :					List[Any]=True     ,							__lowerCamelCase     :					List[Any]=37     ,							__lowerCamelCase     :					int="gelu"     ,							__lowerCamelCase     :					List[Any]=10     ,							__lowerCamelCase     :					Tuple=0.0_2     ,							__lowerCamelCase     :					str=["stage2", "stage3", "stage4"]     ,							__lowerCamelCase     :					Optional[Any]=[2, 3, 4]     ,							__lowerCamelCase     :					Dict=None     ,							):
           snake_case__       :   List[str]	      =    parent
           snake_case__       :   Optional[Any]	      =    batch_size
           snake_case__       :   int	      =    image_size
           snake_case__       :   Tuple	      =    num_channels
           snake_case__       :   Any	      =    num_stages
           snake_case__       :   Any	      =    hidden_sizes
           snake_case__       :   Optional[Any]	      =    depths
           snake_case__       :   Optional[Any]	      =    is_training
           snake_case__       :   Dict	      =    use_labels
           snake_case__       :   Any	      =    intermediate_size
           snake_case__       :   int	      =    hidden_act
           snake_case__       :   Any	      =    num_labels
           snake_case__       :   Optional[int]	      =    initializer_range
           snake_case__       :   Union[str, Any]	      =    out_features
           snake_case__       :   Optional[Any]	      =    out_indices
           snake_case__       :   Optional[int]	      =    scope





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   Union[str, Any]	      =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

           snake_case__       :   Optional[int]	      =    None
           if self.use_labels:
                 snake_case__       :   Tuple	      =    ids_tensor([self.batch_size]     ,							self.num_labels			)

           snake_case__       :   Any	      =    self.get_config()
           return config, pixel_values, labels





     def 			_lowerCAmelCase      (     self     :					Tuple			):
           return ConvNextConfig(
               num_channels=self.num_channels     ,							hidden_sizes=self.hidden_sizes     ,							depths=self.depths     ,							num_stages=self.num_stages     ,							hidden_act=self.hidden_act     ,							is_decoder=__lowerCamelCase     ,							initializer_range=self.initializer_range     ,							out_features=self.out_features     ,							out_indices=self.out_indices     ,							num_labels=self.num_labels     ,							)





     def 			_lowerCAmelCase      (     self     :					List[Any]     ,							__lowerCamelCase     :					List[str]     ,							__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					Tuple			):
           snake_case__       :   Optional[int]	      =    ConvNextModel(config=__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   Union[str, Any]	      =    model(__lowerCamelCase			)
           # expected last hidden states: B, C, H // 32, W // 32
           self.parent.assertEqual(
               result.last_hidden_state.shape     ,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)     ,							)





     def 			_lowerCAmelCase      (     self     :					Any     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					List[Any]			):
           snake_case__       :   Optional[Any]	      =    ConvNextForImageClassification(__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   Optional[Any]	      =    model(__lowerCamelCase     ,							labels=__lowerCamelCase			)
           self.parent.assertEqual(result.logits.shape     ,							(self.batch_size, self.num_labels)			)





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							__lowerCamelCase     :					int     ,							__lowerCamelCase     :					Dict     ,							__lowerCamelCase     :					int			):
           snake_case__       :   Dict	      =    ConvNextBackbone(config=__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   str	      =    model(__lowerCamelCase			)

           # verify hidden states
           self.parent.assertEqual(len(result.feature_maps			)     ,							len(config.out_features			)			)
           self.parent.assertListEqual(list(result.feature_maps[0].shape			)     ,							[self.batch_size, self.hidden_sizes[1], 4, 4]			)

           # verify channels
           self.parent.assertEqual(len(model.channels			)     ,							len(config.out_features			)			)
           self.parent.assertListEqual(model.channels     ,							config.hidden_sizes[1:]			)

           # verify backbone works with out_features=None
           snake_case__       :   List[str]	      =    None
           snake_case__       :   Union[str, Any]	      =    ConvNextBackbone(config=__lowerCamelCase			)
           model.to(__lowerCamelCase			)
           model.eval()
           snake_case__       :   int	      =    model(__lowerCamelCase			)

           # verify feature maps
           self.parent.assertEqual(len(result.feature_maps			)     ,							1			)
           self.parent.assertListEqual(list(result.feature_maps[0].shape			)     ,							[self.batch_size, self.hidden_sizes[-1], 1, 1]			)

           # verify channels
           self.parent.assertEqual(len(model.channels			)     ,							1			)
           self.parent.assertListEqual(model.channels     ,							[config.hidden_sizes[-1]]			)





     def 			_lowerCAmelCase      (     self     :					Tuple			):
           snake_case__       :   Dict	      =    self.prepare_config_and_inputs()
           snake_case__      ,							snake_case__      ,							snake_case__       :   Optional[Any]	      =    config_and_inputs
           snake_case__       :   Optional[int]	      =    {'pixel_values': pixel_values}
           return config, inputs_dict






@require_torch
class        lowercase_      (						lowerCAmelCase_						,       lowerCAmelCase_						,       unittest.TestCase							):
     A_											=			(
         (
             ConvNextModel,
             ConvNextForImageClassification,
             ConvNextBackbone,
         )
         if is_torch_available()
         else ()
     )
     A_											=			(
         {"feature-extraction": ConvNextModel, "image-classification": ConvNextForImageClassification}
         if is_torch_available()
         else {}
     )

     A_											=			True
     A_											=			False
     A_											=			False
     A_											=			False
     A_											=			False





     def 			_lowerCAmelCase      (     self     :					Any			):
           snake_case__       :   Tuple	      =    ConvNextModelTester(self			)
           snake_case__       :   Optional[int]	      =    ConfigTester(self     ,							config_class=__lowerCamelCase     ,							has_text_modality=__lowerCamelCase     ,							hidden_size=37			)





     def 			_lowerCAmelCase      (     self     :					Optional[Any]			):
           self.create_and_test_config_common_properties()
           self.config_tester.create_and_test_config_to_json_string()
           self.config_tester.create_and_test_config_to_json_file()
           self.config_tester.create_and_test_config_from_and_save_pretrained()
           self.config_tester.create_and_test_config_with_num_labels()
           self.config_tester.check_config_can_be_init_without_params()
           self.config_tester.check_config_arguments_init()





     def 			_lowerCAmelCase      (     self     :					Dict			):
           return





     @unittest.skip(reason='ConvNext does not use inputs_embeds'			)
     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           pass





     @unittest.skip(reason='ConvNext does not support input and output embeddings'			)
     def 			_lowerCAmelCase      (     self     :					Tuple			):
           pass





     @unittest.skip(reason='ConvNext does not use feedforward chunking'			)
     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           pass





     def 			_lowerCAmelCase      (     self     :					str			):
           snake_case__      ,							snake_case__       :   Any	      =    self.model_tester.prepare_config_and_inputs_for_common()

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

                 snake_case__       :   str	      =    ['pixel_values']
                 self.assertListEqual(arg_names[:1]     ,							__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					int			):
           snake_case__       :   List[Any]	      =    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					int			):
           snake_case__       :   List[Any]	      =    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					str			):
           def check_hidden_states_output(__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					Any     ,							__lowerCamelCase     :					Tuple			):
                 snake_case__       :   int	      =    model_class(__lowerCamelCase			)
                 model.to(__lowerCamelCase			)
                 model.eval()

                 with torch.no_grad():
                       snake_case__       :   int	      =    model(**self._prepare_for_class(__lowerCamelCase     ,							__lowerCamelCase			)			)

                 snake_case__       :   Optional[Any]	      =    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                 snake_case__       :   Optional[Any]	      =    self.model_tester.num_stages
                 self.assertEqual(len(__lowerCamelCase			)     ,							expected_num_stages + 1			)

                 # ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:]			)     ,							[self.model_tester.image_size // 4, self.model_tester.image_size // 4]     ,							)

           snake_case__      ,							snake_case__       :   List[Any]	      =    self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                 snake_case__       :   Dict	      =    True
                 check_hidden_states_output(__lowerCamelCase     ,							__lowerCamelCase     ,							__lowerCamelCase			)

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

                 check_hidden_states_output(__lowerCamelCase     ,							__lowerCamelCase     ,							__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   Any	      =    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase			)





     @slow
     def 			_lowerCAmelCase      (     self     :					str			):
           for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 snake_case__       :   Optional[int]	      =    ConvNextModel.from_pretrained(__lowerCamelCase			)
                 self.assertIsNotNone(__lowerCamelCase			)






def 		UpperCamelCase__      (		)    ->   Optional[Any]:
      snake_case__       :   Any	      =    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'		)
      return image






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





     @cached_property
     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           return AutoImageProcessor.from_pretrained('facebook/convnext-tiny-224'			) if is_vision_available() else None





     @slow
     def 			_lowerCAmelCase      (     self     :					Optional[int]			):
           snake_case__       :   str	      =    ConvNextForImageClassification.from_pretrained('facebook/convnext-tiny-224'			).to(__lowerCamelCase			)

           snake_case__       :   str	      =    self.default_image_processor
           snake_case__       :   List[str]	      =    prepare_img()
           snake_case__       :   List[Any]	      =    image_processor(images=__lowerCamelCase     ,							return_tensors='pt'			).to(__lowerCamelCase			)

           # forward pass
           with torch.no_grad():
                 snake_case__       :   int	      =    model(**__lowerCamelCase			)

           # verify the logits
           snake_case__       :   Tuple	      =    torch.Size((1, 1000)			)
           self.assertEqual(outputs.logits.shape     ,							__lowerCamelCase			)

           snake_case__       :   int	      =    torch.tensor([-0.0_2_6_0, -0.4_7_3_9, 0.1_9_1_1]			).to(__lowerCamelCase			)

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






@require_torch
class        lowercase_      (						unittest.TestCase						,       lowerCAmelCase_							):
     A_											=			(ConvNextBackbone,) if is_torch_available() else ()
     A_											=			ConvNextConfig

     A_											=			False





     def 			_lowerCAmelCase      (     self     :					List[Any]			):
           snake_case__       :   List[Any]	      =    ConvNextModelTester(self			)
