
'''simple docstring'''
import argparse
import json

import gdown
import numpy as np
import torch
from huggingface_hub import hf_hub_download

from transformers import (
    VideoMAEConfig,
    VideoMAEForPreTraining,
    VideoMAEForVideoClassification,
    VideoMAEImageProcessor,
)

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    snake_case_ :    Any     			=						VideoMAEConfig()

    set_architecture_configs(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       )

    if "finetuned" not in model_name:
        snake_case_ :    Dict     			=						False

    if "finetuned" in model_name:
        snake_case_ :    List[str]     			=						"huggingface/label-files"
        if "kinetics" in model_name:
            snake_case_ :    List[Any]     			=						4_0_0
            snake_case_ :    int     			=						"kinetics400-id2label.json"
        elif "ssv2" in model_name:
            snake_case_ :    Union[str, Any]     			=						1_7_4
            snake_case_ :    Any     			=						"something-something-v2-id2label.json"
        else:
            raise ValueError("Model name should either contain 'kinetics' or 'ssv2' in case it's fine-tuned."       )
        snake_case_ :    str     			=						json.load(open(hf_hub_download(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		repo_type="dataset"       ),		"r"       )       )
        snake_case_ :    List[str]     			=						{int(__SCREAMING_SNAKE_CASE       ): v for k, v in idalabel.items()}
        snake_case_ :    Optional[int]     			=						idalabel
        snake_case_ :    int     			=						{v: k for k, v in idalabel.items()}

    return config

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    if "small" in model_name:
        snake_case_ :    Optional[int]     			=						3_8_4
        snake_case_ :    Tuple     			=						1_5_3_6
        snake_case_ :    Dict     			=						1_2
        snake_case_ :    int     			=						1_6
        snake_case_ :    Optional[int]     			=						1_2
        snake_case_ :    Union[str, Any]     			=						3
        snake_case_ :    int     			=						1_9_2
        snake_case_ :    Optional[int]     			=						7_6_8
    elif "large" in model_name:
        snake_case_ :    Any     			=						1_0_2_4
        snake_case_ :    List[str]     			=						4_0_9_6
        snake_case_ :    Tuple     			=						2_4
        snake_case_ :    Tuple     			=						1_6
        snake_case_ :    Union[str, Any]     			=						1_2
        snake_case_ :    List[str]     			=						8
        snake_case_ :    Optional[int]     			=						5_1_2
        snake_case_ :    Optional[Any]     			=						2_0_4_8
    elif "huge" in model_name:
        snake_case_ :    List[Any]     			=						1_2_8_0
        snake_case_ :    Dict     			=						5_1_2_0
        snake_case_ :    List[Any]     			=						3_2
        snake_case_ :    Optional[int]     			=						1_6
        snake_case_ :    List[str]     			=						1_2
        snake_case_ :    List[Any]     			=						8
        snake_case_ :    Optional[int]     			=						6_4_0
        snake_case_ :    Any     			=						2_5_6_0
    elif "base" not in model_name:
        raise ValueError("Model name should include either \"small\", \"base\", \"large\", or \"huge\""       )

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    if "encoder." in name:
        snake_case_ :    int     			=						name.replace("encoder.",		""       )
    if "cls_token" in name:
        snake_case_ :    Dict     			=						name.replace("cls_token",		"videomae.embeddings.cls_token"       )
    if "decoder_pos_embed" in name:
        snake_case_ :    Tuple     			=						name.replace("decoder_pos_embed",		"decoder.decoder_pos_embed"       )
    if "pos_embed" in name and "decoder" not in name:
        snake_case_ :    Dict     			=						name.replace("pos_embed",		"videomae.embeddings.position_embeddings"       )
    if "patch_embed.proj" in name:
        snake_case_ :    List[Any]     			=						name.replace("patch_embed.proj",		"videomae.embeddings.patch_embeddings.projection"       )
    if "patch_embed.norm" in name:
        snake_case_ :    str     			=						name.replace("patch_embed.norm",		"videomae.embeddings.norm"       )
    if "decoder.blocks" in name:
        snake_case_ :    Optional[int]     			=						name.replace("decoder.blocks",		"decoder.decoder_layers"       )
    if "blocks" in name:
        snake_case_ :    int     			=						name.replace("blocks",		"videomae.encoder.layer"       )
    if "attn.proj" in name:
        snake_case_ :    Union[str, Any]     			=						name.replace("attn.proj",		"attention.output.dense"       )
    if "attn" in name and "bias" not in name:
        snake_case_ :    Tuple     			=						name.replace("attn",		"attention.self"       )
    if "attn" in name:
        snake_case_ :    Tuple     			=						name.replace("attn",		"attention.attention"       )
    if "norm1" in name:
        snake_case_ :    int     			=						name.replace("norm1",		"layernorm_before"       )
    if "norm2" in name:
        snake_case_ :    List[Any]     			=						name.replace("norm2",		"layernorm_after"       )
    if "mlp.fc1" in name:
        snake_case_ :    Any     			=						name.replace("mlp.fc1",		"intermediate.dense"       )
    if "mlp.fc2" in name:
        snake_case_ :    int     			=						name.replace("mlp.fc2",		"output.dense"       )
    if "decoder_embed" in name:
        snake_case_ :    List[str]     			=						name.replace("decoder_embed",		"decoder.decoder_embed"       )
    if "decoder_norm" in name:
        snake_case_ :    Union[str, Any]     			=						name.replace("decoder_norm",		"decoder.decoder_norm"       )
    if "decoder_pred" in name:
        snake_case_ :    Optional[Any]     			=						name.replace("decoder_pred",		"decoder.decoder_pred"       )
    if "norm.weight" in name and "decoder" not in name and "fc" not in name:
        snake_case_ :    List[str]     			=						name.replace("norm.weight",		"videomae.layernorm.weight"       )
    if "norm.bias" in name and "decoder" not in name and "fc" not in name:
        snake_case_ :    Optional[Any]     			=						name.replace("norm.bias",		"videomae.layernorm.bias"       )
    if "head" in name and "decoder" not in name:
        snake_case_ :    Optional[int]     			=						name.replace("head",		"classifier"       )

    return name

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    for key in orig_state_dict.copy().keys():
        snake_case_ :    Dict     			=						orig_state_dict.pop(__SCREAMING_SNAKE_CASE       )

        if key.startswith("encoder."       ):
            snake_case_ :    List[str]     			=						key.replace("encoder.",		""       )

        if "qkv" in key:
            snake_case_ :    Optional[Any]     			=						key.split("."       )
            if key.startswith("decoder.blocks"       ):
                snake_case_ :    str     			=						config.decoder_hidden_size
                snake_case_ :    str     			=						int(key_split[2]       )
                snake_case_ :    int     			=						"decoder.decoder_layers."
                if "weight" in key:
                    snake_case_ :    Any     			=						val[:dim, :]
                    snake_case_ :    Any     			=						val[dim : dim * 2, :]
                    snake_case_ :    Union[str, Any]     			=						val[-dim:, :]
            else:
                snake_case_ :    Dict     			=						config.hidden_size
                snake_case_ :    List[str]     			=						int(key_split[1]       )
                snake_case_ :    Dict     			=						"videomae.encoder.layer."
                if "weight" in key:
                    snake_case_ :    Optional[Any]     			=						val[:dim, :]
                    snake_case_ :    Dict     			=						val[dim : dim * 2, :]
                    snake_case_ :    Optional[Any]     			=						val[-dim:, :]
        else:
            snake_case_ :    Union[str, Any]     			=						val

    return orig_state_dict

def 						UpperCamelCase_     (	):
    """simple docstring"""






    snake_case_ :    List[str]     			=						hf_hub_download(
        repo_id="hf-internal-testing/spaghetti-video",		filename="eating_spaghetti.npy",		repo_type="dataset"       )
    snake_case_ :    Union[str, Any]     			=						np.load(__SCREAMING_SNAKE_CASE       )
    return list(__SCREAMING_SNAKE_CASE       )

def 						UpperCamelCase_     (	__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       ):
    """simple docstring"""






    snake_case_ :    List[str]     			=						get_videomae_config(__SCREAMING_SNAKE_CASE       )

    if "finetuned" in model_name:
        snake_case_ :    int     			=						VideoMAEForVideoClassification(__SCREAMING_SNAKE_CASE       )
    else:
        snake_case_ :    str     			=						VideoMAEForPreTraining(__SCREAMING_SNAKE_CASE       )

    # download original checkpoint, hosted on Google Drive
    snake_case_ :    Dict     			=						"pytorch_model.bin"
    gdown.cached_download(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		quiet=__SCREAMING_SNAKE_CASE       )
    snake_case_ :    str     			=						torch.load(__SCREAMING_SNAKE_CASE,		map_location="cpu"       )
    if "model" in files:
        snake_case_ :    str     			=						files["model"]
    else:
        snake_case_ :    Optional[Any]     			=						files["module"]
    snake_case_ :    Any     			=						convert_state_dict(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       )

    model.load_state_dict(__SCREAMING_SNAKE_CASE       )
    model.eval()

    # verify model on basic input
    snake_case_ :    Tuple     			=						VideoMAEImageProcessor(image_mean=[0.5, 0.5, 0.5],		image_std=[0.5, 0.5, 0.5]       )
    snake_case_ :    Union[str, Any]     			=						prepare_video()
    snake_case_ :    str     			=						image_processor(__SCREAMING_SNAKE_CASE,		return_tensors="pt"       )

    if "finetuned" not in model_name:
        snake_case_ :    Tuple     			=						hf_hub_download(repo_id="hf-internal-testing/bool-masked-pos",		filename="bool_masked_pos.pt"       )
        snake_case_ :    List[str]     			=						torch.load(__SCREAMING_SNAKE_CASE       )

    snake_case_ :    int     			=						model(**__SCREAMING_SNAKE_CASE       )
    snake_case_ :    int     			=						outputs.logits

    snake_case_ :    Dict     			=						[
        "videomae-small-finetuned-kinetics",
        "videomae-small-finetuned-ssv2",
        # Kinetics-400 checkpoints (short = pretrained only for 800 epochs instead of 1600)
        "videomae-base-short",
        "videomae-base-short-finetuned-kinetics",
        "videomae-base",
        "videomae-base-finetuned-kinetics",
        "videomae-large",
        "videomae-large-finetuned-kinetics",
        "videomae-huge-finetuned-kinetics",
        # Something-Something-v2 checkpoints (short = pretrained only for 800 epochs instead of 2400)
        "videomae-base-short-ssv2",
        "videomae-base-short-finetuned-ssv2",
        "videomae-base-ssv2",
        "videomae-base-finetuned-ssv2",
    ]

    # NOTE: logits were tested with image_mean and image_std equal to [0.5, 0.5, 0.5] and [0.5, 0.5, 0.5]
    if model_name == "videomae-small-finetuned-kinetics":
        snake_case_ :    Tuple     			=						torch.Size([1, 4_0_0]       )
        snake_case_ :    Dict     			=						torch.tensor([-0.9291, -0.4061, -0.9307]       )
    elif model_name == "videomae-small-finetuned-ssv2":
        snake_case_ :    Union[str, Any]     			=						torch.Size([1, 1_7_4]       )
        snake_case_ :    str     			=						torch.tensor([0.2671, -0.4689, -0.8235]       )
    elif model_name == "videomae-base":
        snake_case_ :    List[str]     			=						torch.Size([1, 1_4_0_8, 1_5_3_6]       )
        snake_case_ :    Dict     			=						torch.tensor([[0.7739, 0.7968, 0.7089], [0.6701, 0.7487, 0.6209], [0.4287, 0.5158, 0.4773]]       )
    elif model_name == "videomae-base-short":
        snake_case_ :    Any     			=						torch.Size([1, 1_4_0_8, 1_5_3_6]       )
        snake_case_ :    Any     			=						torch.tensor([[0.7994, 0.9612, 0.8508], [0.7401, 0.8958, 0.8302], [0.5862, 0.7468, 0.7325]]       )
        # we verified the loss both for normalized and unnormalized targets for this one
        snake_case_ :    Tuple     			=						torch.tensor([0.5142]       ) if config.norm_pix_loss else torch.tensor([0.6469]       )
    elif model_name == "videomae-large":
        snake_case_ :    Union[str, Any]     			=						torch.Size([1, 1_4_0_8, 1_5_3_6]       )
        snake_case_ :    Any     			=						torch.tensor([[0.7149, 0.7997, 0.6966], [0.6768, 0.7869, 0.6948], [0.5139, 0.6221, 0.5605]]       )
    elif model_name == "videomae-large-finetuned-kinetics":
        snake_case_ :    Tuple     			=						torch.Size([1, 4_0_0]       )
        snake_case_ :    Any     			=						torch.tensor([0.0771, 0.0011, -0.3625]       )
    elif model_name == "videomae-huge-finetuned-kinetics":
        snake_case_ :    Optional[Any]     			=						torch.Size([1, 4_0_0]       )
        snake_case_ :    str     			=						torch.tensor([0.2433, 0.1632, -0.4894]       )
    elif model_name == "videomae-base-short-finetuned-kinetics":
        snake_case_ :    Optional[int]     			=						torch.Size([1, 4_0_0]       )
        snake_case_ :    List[Any]     			=						torch.tensor([0.6588, 0.0990, -0.2493]       )
    elif model_name == "videomae-base-finetuned-kinetics":
        snake_case_ :    Optional[int]     			=						torch.Size([1, 4_0_0]       )
        snake_case_ :    Dict     			=						torch.tensor([0.3669, -0.0688, -0.2421]       )
    elif model_name == "videomae-base-short-ssv2":
        snake_case_ :    List[Any]     			=						torch.Size([1, 1_4_0_8, 1_5_3_6]       )
        snake_case_ :    Optional[Any]     			=						torch.tensor([[0.4712, 0.5296, 0.5786], [0.2278, 0.2729, 0.4026], [0.0352, 0.0730, 0.2506]]       )
    elif model_name == "videomae-base-short-finetuned-ssv2":
        snake_case_ :    List[Any]     			=						torch.Size([1, 1_7_4]       )
        snake_case_ :    Union[str, Any]     			=						torch.tensor([-0.0537, -0.1539, -0.3266]       )
    elif model_name == "videomae-base-ssv2":
        snake_case_ :    List[str]     			=						torch.Size([1, 1_4_0_8, 1_5_3_6]       )
        snake_case_ :    Optional[Any]     			=						torch.tensor([[0.8131, 0.8727, 0.8546], [0.7366, 0.9377, 0.8870], [0.5935, 0.8874, 0.8564]]       )
    elif model_name == "videomae-base-finetuned-ssv2":
        snake_case_ :    int     			=						torch.Size([1, 1_7_4]       )
        snake_case_ :    Union[str, Any]     			=						torch.tensor([0.1961, -0.8337, -0.6389]       )
    else:
        raise ValueError(f'Model name not supported. Should be one of {model_names}'       )

    # verify logits
    assert logits.shape == expected_shape
    if "finetuned" in model_name:
        assert torch.allclose(logits[0, :3],		__SCREAMING_SNAKE_CASE,		atol=1E-4       )
    else:
        print("Logits:",		logits[0, :3, :3]       )
        assert torch.allclose(logits[0, :3, :3],		__SCREAMING_SNAKE_CASE,		atol=1E-4       )
    print("Logits ok!"       )

    # verify loss, if applicable
    if model_name == "videomae-base-short":
        snake_case_ :    int     			=						outputs.loss
        assert torch.allclose(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE,		atol=1E-4       )
        print("Loss ok!"       )

    if pytorch_dump_folder_path is not None:
        print(f'Saving model and image processor to {pytorch_dump_folder_path}'       )
        image_processor.save_pretrained(__SCREAMING_SNAKE_CASE       )
        model.save_pretrained(__SCREAMING_SNAKE_CASE       )

    if push_to_hub:
        print("Pushing to the hub..."       )
        model.push_to_hub(__SCREAMING_SNAKE_CASE,		organization="nielsr"       )


if __name__ == "__main__":
  a_											=						argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      "--checkpoint_url",
      default="https://drive.google.com/u/1/uc?id=1tEhLyskjb755TJ65ptsrafUG2llSwQE1&amp;export=download&amp;confirm=t&amp;uuid=aa3276eb-fb7e-482a-adec-dc7171df14c4",
      type=str,
      help=(
          "URL of the original PyTorch checkpoint (on Google Drive) you'd like to convert. Should be a direct"
          " download link."
      ),
  )
  parser.add_argument(
      "--pytorch_dump_folder_path",
      default="/Users/nielsrogge/Documents/VideoMAE/Test",
      type=str,
      help="Path to the output PyTorch model directory.",
  )
  parser.add_argument("--model_name", default="videomae-base", type=str, help="Name of the model.")
  parser.add_argument(
      "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub."
  )

  a_											=						parser.parse_args()
  convert_videomae_checkpoint(args.checkpoint_url, args.pytorch_dump_folder_path, args.model_name, args.push_to_hub)






'''simple docstring'''
import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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_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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
  from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor






class    UpperCAmelCase_				:





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





 def        snake_case__		(    self):
     snake_case_ :    Any     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

     snake_case_ :    Tuple     			=						None
     if self.use_labels:
         snake_case_ :    Any     			=						ids_tensor([self.batch_size] ,       self.num_labels)

     snake_case_ :    int     			=						self.get_config()

     return config, pixel_values, labels





 def        snake_case__		(    self):
     return ConvNextVaConfig(
         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=lowercase_ ,       initializer_range=self.initializer_range ,       out_features=self.out_features ,       out_indices=self.out_indices ,       num_labels=self.num_labels ,       )





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    Dict     			=						ConvNextVaModel(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    Dict     			=						model(lowercase_)
     # 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        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    Tuple     			=						ConvNextVaForImageClassification(lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    int     			=						model(lowercase_ ,       labels=lowercase_)
     self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels))





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_ ,       lowercase_):
     snake_case_ :    Tuple     			=						ConvNextVaBackbone(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    Any     			=						model(lowercase_)

     # 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_ :    Dict     			=						None
     snake_case_ :    str     			=						ConvNextVaBackbone(config=lowercase_)
     model.to(lowercase_)
     model.eval()
     snake_case_ :    List[str]     			=						model(lowercase_)

     # 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        snake_case__		(    self):
     snake_case_ :    Optional[int]     			=						self.prepare_config_and_inputs()
     snake_case_			, snake_case_			, snake_case_ :    str     			=						config_and_inputs
     snake_case_ :    int     			=						{"pixel_values": pixel_values}
     return config, inputs_dict





 def        snake_case__		(    self):
     snake_case_ :    Union[str, Any]     			=						self.prepare_config_and_inputs()
     snake_case_			, snake_case_			, snake_case_ :    Dict     			=						config_and_inputs
     snake_case_ :    Union[str, Any]     			=						{"pixel_values": pixel_values, "labels": labels}
     return config, inputs_dict






@require_torch
class    UpperCAmelCase_				(	snake_case__    ,						snake_case__    ,						unittest.TestCase					):
 UpperCAmelCase_		    =						(
     (
         ConvNextVaModel,
         ConvNextVaForImageClassification,
         ConvNextVaBackbone,
     )
     if is_torch_available()
     else ()
 )
 UpperCAmelCase_		    =						(
     {"""feature-extraction""": ConvNextVaModel, """image-classification""": ConvNextVaForImageClassification}
     if is_torch_available()
     else {}
 )

 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False
 UpperCAmelCase_		    =						False





 def        snake_case__		(    self):
     snake_case_ :    List[Any]     			=						ConvNextVaModelTester(self)
     snake_case_ :    str     			=						ConfigTester(self ,       config_class=lowercase_ ,       has_text_modality=lowercase_ ,       hidden_size=37)





 def        snake_case__		(    self):
     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        snake_case__		(    self):
     return





 @unittest.skip(reason="ConvNextV2 does not use inputs_embeds")
 def        snake_case__		(    self):
     pass





 @unittest.skip(reason="ConvNextV2 does not support input and output embeddings")
 def        snake_case__		(    self):
     pass





 @unittest.skip(reason="ConvNextV2 does not use feedforward chunking")
 def        snake_case__		(    self):
     pass





 def        snake_case__		(    self):
     if not self.model_tester.is_training:
         return

     for model_class in self.all_model_classes:
         snake_case_			, snake_case_ :    Optional[Any]     			=						self.model_tester.prepare_config_and_inputs_with_labels()
         snake_case_ :    Union[str, Any]     			=						True

         if model_class.__name__ in [
             *get_values(lowercase_),
             *get_values(lowercase_),
         ]:
             continue

         snake_case_ :    List[Any]     			=						model_class(lowercase_)
         model.to(lowercase_)
         model.train()
         snake_case_ :    int     			=						self._prepare_for_class(lowercase_ ,       lowercase_ ,       return_labels=lowercase_)
         snake_case_ :    Union[str, Any]     			=						model(**lowercase_).loss
         loss.backward()





 def        snake_case__		(    self):
     if not self.model_tester.is_training:
         return

     for model_class in self.all_model_classes:
         snake_case_			, snake_case_ :    List[str]     			=						self.model_tester.prepare_config_and_inputs_with_labels()
         snake_case_ :    Dict     			=						False
         snake_case_ :    Any     			=						True

         if (
             model_class.__name__
             in [*get_values(lowercase_), *get_values(lowercase_)]
             or not model_class.supports_gradient_checkpointing
         ):
             continue

         snake_case_ :    Any     			=						model_class(lowercase_)
         model.to(lowercase_)
         model.gradient_checkpointing_enable()
         model.train()
         snake_case_ :    Any     			=						self._prepare_for_class(lowercase_ ,       lowercase_ ,       return_labels=lowercase_)
         snake_case_ :    Optional[int]     			=						model(**lowercase_).loss
         loss.backward()





 def        snake_case__		(    self):
     snake_case_			, snake_case_ :    Dict     			=						self.model_tester.prepare_config_and_inputs_for_common()

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

         snake_case_ :    str     			=						["pixel_values"]
         self.assertListEqual(arg_names[:1] ,       lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    Tuple     			=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_)





 def        snake_case__		(    self):
     def check_hidden_states_output(lowercase_ ,       lowercase_ ,       lowercase_):
         snake_case_ :    int     			=						model_class(lowercase_)
         model.to(lowercase_)
         model.eval()

         with torch.no_grad():
             snake_case_ :    int     			=						model(**self._prepare_for_class(lowercase_ ,       lowercase_))

         snake_case_ :    int     			=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

         snake_case_ :    Dict     			=						self.model_tester.num_stages
         self.assertEqual(len(lowercase_) ,       expected_num_stages + 1)

         # ConvNextV2'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_ :    Any     			=						self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
         snake_case_ :    Tuple     			=						True
         check_hidden_states_output(lowercase_ ,       lowercase_ ,       lowercase_)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case_ :    Union[str, Any]     			=						True

         check_hidden_states_output(lowercase_ ,       lowercase_ ,       lowercase_)





 def        snake_case__		(    self):
     snake_case_ :    Union[str, Any]     			=						self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowercase_)





 @slow
 def        snake_case__		(    self):
     for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_ :    Any     			=						ConvNextVaModel.from_pretrained(lowercase_)
         self.assertIsNotNone(lowercase_)


def 						UpperCamelCase_     (	):
    """simple docstring"""






    snake_case_ :    List[Any]     			=						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"       )
    return image






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





 @cached_property
 def        snake_case__		(    self):
     return AutoImageProcessor.from_pretrained("facebook/convnextv2-tiny-1k-224") if is_vision_available() else None





 @slow
 def        snake_case__		(    self):
     snake_case_ :    List[str]     			=						ConvNextVaForImageClassification.from_pretrained("facebook/convnextv2-tiny-1k-224").to(lowercase_)

     snake_case_ :    List[Any]     			=						self.default_image_processor
     snake_case_ :    str     			=						prepare_img()
     snake_case_ :    Any     			=						preprocessor(images=lowercase_ ,       return_tensors="pt").to(lowercase_)

     # forward pass
     with torch.no_grad():
         snake_case_ :    List[str]     			=						model(**lowercase_)

     # verify the logits
     snake_case_ :    str     			=						torch.Size((1, 10_00))
     self.assertEqual(outputs.logits.shape ,       lowercase_)

     snake_case_ :    str     			=						torch.tensor([0.9_996, 0.1_966, -0.4_386]).to(lowercase_)
     self.assertTrue(torch.allclose(outputs.logits[0, :3] ,       lowercase_ ,       atol=1E-4))




