






'''simple docstring'''
import logging
import sys
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional, Union

import librosa
import torch
from datasets import DatasetDict, load_dataset
from packaging import version
from torch import nn

from transformers import (
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaForPreTraining,
    is_apex_available,
    trainer_utils,
)
from transformers.models.wavaveca.modeling_wavaveca import _compute_mask_indices


if is_apex_available():
  from apex import amp

if version.parse(version.parse(torch.__version__).base_version) >= version.parse("1.6"):
  a_											=						True
  from torch.cuda.amp import autocast


a_											=						logging.getLogger(__name__)






@dataclass
class    UpperCAmelCase_				:
 UpperCAmelCase_		    =						field(
     metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}					)
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}    ,						)
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """Whether to freeze the feature extractor layers of the model."""}					)
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """Whether to log verbose messages or not."""}    ,						)
 UpperCAmelCase_		    =						field(
     default=2.0    ,						metadata={"""help""": """Maximum temperature for gumbel softmax."""}					)
 UpperCAmelCase_		    =						field(
     default=0.5    ,						metadata={"""help""": """Minimum temperature for gumbel softmax."""}					)
 UpperCAmelCase_		    =						field(
     default=0.9_9_9_9_9_5    ,						metadata={"""help""": """Decay of gumbel temperature during training."""}					)


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






    logging.basicConfig(
        format="%(asctime)s - %(levelname)s - %(name)s - %(message)s",		datefmt="%m/%d/%Y %H:%M:%S",		handlers=[logging.StreamHandler(sys.stdout       )],		)
    snake_case_ :    Any     			=						logging.WARNING
    if model_args.verbose_logging:
        snake_case_ :    str     			=						logging.DEBUG
    elif trainer_utils.is_main_process(training_args.local_rank       ):
        snake_case_ :    List[str]     			=						logging.INFO
    logger.setLevel(__SCREAMING_SNAKE_CASE       )






@dataclass
class    UpperCAmelCase_				:
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """The name of the dataset to use (via the datasets library)."""}					)
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """The configuration name of the dataset to use (via the datasets library)."""}					)
 UpperCAmelCase_		    =						field(
     default="""train"""    ,						metadata={
         """help""": """The name of the training data set split to use (via the datasets library). Defaults to 'train'"""
     }    ,						)
 UpperCAmelCase_		    =						field(
     default="""validation"""    ,						metadata={
         """help""": (
             """The name of the validation data set split to use (via the datasets library). Defaults to 'validation'"""
         )
     }    ,						)
 UpperCAmelCase_		    =						field(
     default="""file"""    ,						metadata={"""help""": """Column in the dataset that contains speech file path. Defaults to 'file'"""}    ,						)
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """Overwrite the cached preprocessed datasets or not."""}					)
 UpperCAmelCase_		    =						field(
     default=1    ,						metadata={
         """help""": """The percentage of the train set used as validation set in case there's no validation split"""
     }    ,						)
 UpperCAmelCase_		    =						field(
     default=snake_case__    ,						metadata={"""help""": """The number of processes to use for the preprocessing."""}    ,						)
 UpperCAmelCase_		    =						field(
     default=2_0.0    ,						metadata={"""help""": """Filter audio files that are longer than `max_duration_in_seconds` seconds"""}					)






@dataclass
class    UpperCAmelCase_				:
 UpperCAmelCase_		    =						4_2
 UpperCAmelCase_		    =						4_2
 UpperCAmelCase_		    =						"""longest"""
 UpperCAmelCase_		    =						None
 UpperCAmelCase_		    =						None





 def __call__(    self ,       lowercase_):
     # reformat list to dict and set to pytorch format
     snake_case_ :    List[Any]     			=						self.feature_extractor.pad(
         lowercase_ ,       max_length=self.max_length ,       padding=self.padding ,       pad_to_multiple_of=self.pad_to_multiple_of ,       return_tensors="pt" ,       )
     snake_case_ :    Optional[int]     			=						self.model._get_feat_extract_output_lengths(batch["input_values"].shape[-1])

     snake_case_ :    Tuple     			=						batch["input_values"].shape[0]

     # make sure that no loss is computed on padded inputs
     if batch["attention_mask"] is not None:
         # compute real output lengths according to convolution formula
         snake_case_ :    Tuple     			=						self.model._get_feat_extract_output_lengths(batch["attention_mask"].sum(-1)).to(
             torch.long)

         snake_case_ :    Union[str, Any]     			=						torch.zeros(
             (batch_size, mask_indices_seq_length) ,       dtype=torch.long ,       device=batch["input_values"].device)

         # these two operations makes sure that all values
         # before the output lengths indices are attended to
         snake_case_ :    Optional[Any]     			=						1
         snake_case_ :    Dict     			=						attention_mask.flip([-1]).cumsum(-1).flip([-1]).bool()

     # sample randomly masked indices
     snake_case_ :    str     			=						_compute_mask_indices(
         (batch_size, mask_indices_seq_length) ,       self.model.config.mask_time_prob ,       self.model.config.mask_time_length ,       attention_mask=lowercase_ ,       min_masks=2 ,       )

     return batch






class    UpperCAmelCase_				(	snake_case__					):





 def __init__(    self ,       *lowercase_ ,       lowercase_=1 ,       lowercase_=0 ,       lowercase_=1.0 ,       **lowercase_):
     super().__init__(*lowercase_ ,       **lowercase_)
     snake_case_ :    Union[str, Any]     			=						0
     snake_case_ :    int     			=						max_gumbel_temp
     snake_case_ :    Union[str, Any]     			=						min_gumbel_temp
     snake_case_ :    List[str]     			=						gumbel_temp_decay





 def        snake_case__		(    self ,       lowercase_ ,       lowercase_):
     model.train()
     snake_case_ :    Dict     			=						self._prepare_inputs(lowercase_)

     if self.use_amp:
         with autocast():
             snake_case_ :    Dict     			=						self.compute_loss(lowercase_ ,       lowercase_)
     else:
         snake_case_ :    Tuple     			=						self.compute_loss(lowercase_ ,       lowercase_)

     if self.args.n_gpu > 1 or self.deepspeed:
         if model.module.config.ctc_loss_reduction == "mean":
             snake_case_ :    Any     			=						loss.mean()
         elif model.module.config.ctc_loss_reduction == "sum":
             snake_case_ :    List[Any]     			=						loss.sum() / (inputs["mask_time_indices"]).sum()
         else:
             raise ValueError(F'{model.config.ctc_loss_reduction} is not valid. Choose one of [\'mean\', \'sum\']')

     if self.args.gradient_accumulation_steps > 1:
         snake_case_ :    int     			=						loss / self.args.gradient_accumulation_steps

     if self.use_amp:
         self.scaler.scale(lowercase_).backward()
     elif self.use_apex:
         with amp.scale_loss(lowercase_ ,       self.optimizer) as scaled_loss:
             scaled_loss.backward()
     elif self.deepspeed:
         self.deepspeed.backward(lowercase_)
     else:
         loss.backward()

     self.num_update_step += 1
     # make sure gumbel softmax temperature is decayed
     if self.args.n_gpu > 1 or self.deepspeed:
         model.module.set_gumbel_temperature(
             max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step ,       self.min_gumbel_temp))
     else:
         model.set_gumbel_temperature(
             max(self.max_gumbel_temp * self.gumbel_temp_decay**self.num_update_step ,       self.min_gumbel_temp))

     return loss.detach()


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






    snake_case_ :    str     			=						HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)       )

    snake_case_ :    str     			=						parser.parse_args_into_dataclasses()
    configure_logger(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       )

    # Downloading and loading a dataset from the hub.
    snake_case_ :    Optional[Any]     			=						load_dataset(data_args.dataset_name,		data_args.dataset_config_name,		cache_dir=model_args.cache_dir       )

    if "validation" not in datasets.keys():
        # make sure only "validation" and "train" keys remain"
        snake_case_ :    Optional[int]     			=						DatasetDict()
        snake_case_ :    Optional[Any]     			=						load_dataset(
            data_args.dataset_name,		data_args.dataset_config_name,		split=f'{data_args.train_split_name}[:{data_args.validation_split_percentage}%]',		cache_dir=model_args.cache_dir,		)
        snake_case_ :    Tuple     			=						load_dataset(
            data_args.dataset_name,		data_args.dataset_config_name,		split=f'{data_args.train_split_name}[{data_args.validation_split_percentage}%:]',		cache_dir=model_args.cache_dir,		)
    else:
        # make sure only "validation" and "train" keys remain"
        snake_case_ :    Tuple     			=						DatasetDict()
        snake_case_ :    Dict     			=						load_dataset(
            data_args.dataset_name,		data_args.dataset_config_name,		split="validation",		cache_dir=model_args.cache_dir,		)
        snake_case_ :    Union[str, Any]     			=						load_dataset(
            data_args.dataset_name,		data_args.dataset_config_name,		split=f'{data_args.train_split_name}',		cache_dir=model_args.cache_dir,		)

    # only normalized-inputs-training is supported
    snake_case_ :    List[str]     			=						WavaVecaFeatureExtractor.from_pretrained(
        model_args.model_name_or_path,		cache_dir=model_args.cache_dir,		do_normalize=__SCREAMING_SNAKE_CASE       )

    def prepare_dataset(__SCREAMING_SNAKE_CASE       ):
        # check that all files have the correct sampling rate
        snake_case_ :    Optional[Any]     			=						librosa.load(batch[data_args.speech_file_column],		sr=feature_extractor.sampling_rate       )
        return batch

    # load audio files into numpy arrays
    snake_case_ :    Optional[int]     			=						datasets.map(
        __SCREAMING_SNAKE_CASE,		num_proc=data_args.preprocessing_num_workers,		remove_columns=datasets["train"].column_names       )

    # filter audio files that are too long
    snake_case_ :    Any     			=						vectorized_datasets.filter(
        lambda __SCREAMING_SNAKE_CASE       : len(data["speech"]       ) < int(data_args.max_duration_in_seconds * feature_extractor.sampling_rate       )       )

    def normalize(__SCREAMING_SNAKE_CASE       ):
        return feature_extractor(batch["speech"],		sampling_rate=feature_extractor.sampling_rate       )

    # normalize and transform to `BatchFeatures`
    snake_case_ :    Optional[Any]     			=						vectorized_datasets.map(
        __SCREAMING_SNAKE_CASE,		batched=__SCREAMING_SNAKE_CASE,		num_proc=data_args.preprocessing_num_workers,		load_from_cache_file=not data_args.overwrite_cache,		remove_columns=vectorized_datasets["train"].column_names,		)

    # pretraining is only supported for "newer" stable layer norm architecture
    # apply_spec_augment has to be True, mask_feature_prob has to be 0.0
    snake_case_ :    Optional[int]     			=						WavaVecaConfig.from_pretrained(
        model_args.model_name_or_path,		cache_dir=model_args.cache_dir,		gradient_checkpointing=training_args.gradient_checkpointing,		)

    if not config.do_stable_layer_norm or config.feat_extract_norm != "layer":
        raise ValueError(
            "PreTraining is only supported for ``config.do_stable_layer_norm=True`` and"
            " ``config.feat_extract_norm='layer'"       )

    snake_case_ :    int     			=						WavaVecaForPreTraining(__SCREAMING_SNAKE_CASE       )

    snake_case_ :    Dict     			=						DataCollatorForWavaVecaPretraining(model=__SCREAMING_SNAKE_CASE,		feature_extractor=__SCREAMING_SNAKE_CASE       )

    snake_case_ :    str     			=						WavaVecaPreTrainer(
        model=__SCREAMING_SNAKE_CASE,		data_collator=__SCREAMING_SNAKE_CASE,		args=__SCREAMING_SNAKE_CASE,		train_dataset=vectorized_datasets["train"],		eval_dataset=vectorized_datasets["validation"],		tokenizer=__SCREAMING_SNAKE_CASE,		max_gumbel_temp=model_args.max_gumbel_temperature,		min_gumbel_temp=model_args.min_gumbel_temperature,		gumbel_temp_decay=model_args.gumbel_temperature_decay,		)
    trainer.train()


if __name__ == "__main__":
  main()






'''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))




