import gc
import unittest

import numpy as np
import torch
import torch.nn.functional as F
from transformers import (
    ClapTextConfig,
    ClapTextModelWithProjection,
    RobertaTokenizer,
    SpeechTaHifiGan,
    SpeechTaHifiGanConfig,
)

from diffusers import (
    AudioLDMPipeline,
    AutoencoderKL,
    DDIMScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class       UpperCAmelCase__  (       __lowerCamelCase					,     unittest.TestCase			):






   """simple docstring"""
   A							:       Optional[Any]     =    AudioLDMPipeline
   A							:       Tuple     =    TEXT_TO_AUDIO_PARAMS
   A							:       Optional[Any]     =    TEXT_TO_AUDIO_BATCH_PARAMS
   A							:       List[str]     =    frozenset(
       [
           '''num_inference_steps''',
           '''num_waveforms_per_prompt''',
           '''generator''',
           '''latents''',
           '''output_type''',
           '''return_dict''',
           '''callback''',
           '''callback_steps''',
       ]			)
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        torch.manual_seed(0				)
        lowercase_					:       str     				=					UNetaDConditionModel(
            block_out_channels=(32, 64)       ,				layers_per_block=2       ,				sample_size=32       ,				in_channels=4       ,				out_channels=4       ,				down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')       ,				up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')       ,				cross_attention_dim=(32, 64)       ,				class_embed_type='simple_projection'       ,				projection_class_embeddings_input_dim=32       ,				class_embeddings_concat=a_       ,				)
        lowercase_					:       Optional[Any]     				=					DDIMScheduler(
            beta_start=0.0_00_85       ,				beta_end=0.0_12       ,				beta_schedule='scaled_linear'       ,				clip_sample=a_       ,				set_alpha_to_one=a_       ,				)
        torch.manual_seed(0				)
        lowercase_					:       str     				=					AutoencoderKL(
            block_out_channels=[32, 64]       ,				in_channels=1       ,				out_channels=1       ,				down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']       ,				up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']       ,				latent_channels=4       ,				)
        torch.manual_seed(0				)
        lowercase_					:       Dict     				=					ClapTextConfig(
            bos_token_id=0       ,				eos_token_id=2       ,				hidden_size=32       ,				intermediate_size=37       ,				layer_norm_eps=1e-05       ,				num_attention_heads=4       ,				num_hidden_layers=5       ,				pad_token_id=1       ,				vocab_size=1_000       ,				projection_dim=32       ,				)
        lowercase_					:       str     				=					ClapTextModelWithProjection(a_				)
        lowercase_					:       Any     				=					RobertaTokenizer.from_pretrained('hf-internal-testing/tiny-random-roberta'       ,				model_max_length=77				)

        lowercase_					:       Optional[Any]     				=					SpeechTaHifiGanConfig(
            model_in_dim=8       ,				sampling_rate=16_000       ,				upsample_initial_channel=16       ,				upsample_rates=[2, 2]       ,				upsample_kernel_sizes=[4, 4]       ,				resblock_kernel_sizes=[3, 7]       ,				resblock_dilation_sizes=[[1, 3, 5], [1, 3, 5]]       ,				normalize_before=a_       ,				)

        lowercase_					:       str     				=					SpeechTaHifiGan(a_				)

        lowercase_					:       Tuple     				=					{
            "unet": unet,
            "scheduler": scheduler,
            "vae": vae,
            "text_encoder": text_encoder,
            "tokenizer": tokenizer,
            "vocoder": vocoder,
        }
        return components
   def 		_lowerCamelCase (self       ,				_a       ,				_a=0				)    ->     Optional[int]:
        if str(a_				).startswith('mps'				):
             lowercase_					:       str     				=					torch.manual_seed(a_				)
        else:
             lowercase_					:       List[Any]     				=					torch.Generator(device=a_				).manual_seed(a_				)
        lowercase_					:       int     				=					{
            "prompt": "A hammer hitting a wooden surface",
            "generator": generator,
            "num_inference_steps": 2,
            "guidance_scale": 6.0,
        }
        return inputs
   def 		_lowerCamelCase (self				)    ->     Tuple:
        lowercase_					:       Optional[int]     				=					"cpu"  # ensure determinism for the device-dependent torch.Generator

        lowercase_					:       List[Any]     				=					self.get_dummy_components()
        lowercase_					:       Union[str, Any]     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       Any     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       Optional[int]     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       Optional[int]     				=					audioldm_pipe(**a_				)
        lowercase_					:       List[str]     				=					output.audios[0]

        assert audio.ndim == 1
        assert len(a_				) == 256

        lowercase_					:       Union[str, Any]     				=					audio[:10]
        lowercase_					:       Union[str, Any]     				=					np.array(
            [-0.00_50, 0.00_50, -0.00_60, 0.00_33, -0.00_26, 0.00_33, -0.00_27, 0.00_33, -0.00_28, 0.00_33]				)

        assert np.abs(audio_slice - expected_slice				).max() < 1e-2
   def 		_lowerCamelCase (self				)    ->     Any:
        lowercase_					:       List[str]     				=					self.get_dummy_components()
        lowercase_					:       Optional[Any]     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       List[Any]     				=					audioldm_pipe.to(a_				)
        lowercase_					:       Dict     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       List[Any]     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       Tuple     				=					3 * [inputs["prompt"]]

        # forward
        lowercase_					:       Tuple     				=					audioldm_pipe(**a_				)
        lowercase_					:       Optional[int]     				=					output.audios[0]

        lowercase_					:       List[Any]     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       Optional[Any]     				=					3 * [inputs.pop('prompt'				)]

        lowercase_					:       Optional[Any]     				=					audioldm_pipe.tokenizer(
            a_       ,				padding='max_length'       ,				max_length=audioldm_pipe.tokenizer.model_max_length       ,				truncation=a_       ,				return_tensors='pt'       ,				)
        lowercase_					:       Optional[Any]     				=					text_inputs["input_ids"].to(a_				)

        lowercase_					:       Union[str, Any]     				=					audioldm_pipe.text_encoder(
            a_       ,				)
        lowercase_					:       str     				=					prompt_embeds.text_embeds
        # additional L_2 normalization over each hidden-state
        lowercase_					:       Optional[Any]     				=					F.normalize(a_       ,				dim=-1				)

        lowercase_					:       Optional[Any]     				=					prompt_embeds

        # forward
        lowercase_					:       List[Any]     				=					audioldm_pipe(**a_				)
        lowercase_					:       Tuple     				=					output.audios[0]

        assert np.abs(audio_a - audio_a				).max() < 1e-2
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        lowercase_					:       List[Any]     				=					self.get_dummy_components()
        lowercase_					:       Tuple     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       Dict     				=					audioldm_pipe.to(a_				)
        lowercase_					:       Any     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       Union[str, Any]     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       Dict     				=					3 * ["this is a negative prompt"]
        lowercase_					:       Optional[int]     				=					negative_prompt
        lowercase_					:       List[str]     				=					3 * [inputs["prompt"]]

        # forward
        lowercase_					:       Optional[Any]     				=					audioldm_pipe(**a_				)
        lowercase_					:       Dict     				=					output.audios[0]

        lowercase_					:       Dict     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       int     				=					3 * [inputs.pop('prompt'				)]

        lowercase_					:       Dict     				=					[]
        for p in [prompt, negative_prompt]:
             lowercase_					:       Dict     				=					audioldm_pipe.tokenizer(
                 a_       ,				padding='max_length'       ,				max_length=audioldm_pipe.tokenizer.model_max_length       ,				truncation=a_       ,				return_tensors='pt'       ,				)
             lowercase_					:       str     				=					text_inputs["input_ids"].to(a_				)

             lowercase_					:       Optional[Any]     				=					audioldm_pipe.text_encoder(
                 a_       ,				)
             lowercase_					:       int     				=					text_embeds.text_embeds
             # additional L_2 normalization over each hidden-state
             lowercase_					:       List[str]     				=					F.normalize(a_       ,				dim=-1				)

             embeds.append(a_				)

        lowercase_					:       List[str]     				=					embeds

        # forward
        lowercase_					:       Dict     				=					audioldm_pipe(**a_				)
        lowercase_					:       Optional[Any]     				=					output.audios[0]

        assert np.abs(audio_a - audio_a				).max() < 1e-2
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        lowercase_					:       int     				=					"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowercase_					:       int     				=					self.get_dummy_components()
        lowercase_					:       Any     				=					PNDMScheduler(skip_prk_steps=a_				)
        lowercase_					:       List[Any]     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       Union[str, Any]     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       Union[str, Any]     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       List[str]     				=					"egg cracking"
        lowercase_					:       Tuple     				=					audioldm_pipe(**a_       ,				negative_prompt=a_				)
        lowercase_					:       Tuple     				=					output.audios[0]

        assert audio.ndim == 1
        assert len(a_				) == 256

        lowercase_					:       Optional[Any]     				=					audio[:10]
        lowercase_					:       List[str]     				=					np.array(
            [-0.00_51, 0.00_50, -0.00_60, 0.00_34, -0.00_26, 0.00_33, -0.00_27, 0.00_33, -0.00_28, 0.00_32]				)

        assert np.abs(audio_slice - expected_slice				).max() < 1e-2
   def 		_lowerCamelCase (self				)    ->     str:
        lowercase_					:       Tuple     				=					"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowercase_					:       Optional[Any]     				=					self.get_dummy_components()
        lowercase_					:       int     				=					PNDMScheduler(skip_prk_steps=a_				)
        lowercase_					:       int     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       Dict     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       List[str]     				=					"A hammer hitting a wooden surface"

        # test num_waveforms_per_prompt=1 (default)
        lowercase_					:       Any     				=					audioldm_pipe(a_       ,				num_inference_steps=2				).audios

        assert audios.shape == (1, 256)

        # test num_waveforms_per_prompt=1 (default) for batch of prompts
        lowercase_					:       List[Any]     				=					2
        lowercase_					:       Any     				=					audioldm_pipe([prompt] * batch_size       ,				num_inference_steps=2				).audios

        assert audios.shape == (batch_size, 256)

        # test num_waveforms_per_prompt for single prompt
        lowercase_					:       List[str]     				=					2
        lowercase_					:       Any     				=					audioldm_pipe(a_       ,				num_inference_steps=2       ,				num_waveforms_per_prompt=a_				).audios

        assert audios.shape == (num_waveforms_per_prompt, 256)

        # test num_waveforms_per_prompt for batch of prompts
        lowercase_					:       Any     				=					2
        lowercase_					:       Tuple     				=					audioldm_pipe(
            [prompt] * batch_size       ,				num_inference_steps=2       ,				num_waveforms_per_prompt=a_				).audios

        assert audios.shape == (batch_size * num_waveforms_per_prompt, 256)
   def 		_lowerCamelCase (self				)    ->     Tuple:
        lowercase_					:       Tuple     				=					"cpu"  # ensure determinism for the device-dependent torch.Generator
        lowercase_					:       List[str]     				=					self.get_dummy_components()
        lowercase_					:       Optional[Any]     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       List[str]     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)
        lowercase_					:       Optional[Any]     				=					audioldm_pipe.vocoder.config.sampling_rate

        lowercase_					:       Union[str, Any]     				=					self.get_dummy_inputs(a_				)
        lowercase_					:       Union[str, Any]     				=					audioldm_pipe(audio_length_in_s=0.0_16       ,				**a_				)
        lowercase_					:       Union[str, Any]     				=					output.audios[0]

        assert audio.ndim == 1
        assert len(a_				) / vocoder_sampling_rate == 0.0_16

        lowercase_					:       Dict     				=					audioldm_pipe(audio_length_in_s=0.0_32       ,				**a_				)
        lowercase_					:       List[Any]     				=					output.audios[0]

        assert audio.ndim == 1
        assert len(a_				) / vocoder_sampling_rate == 0.0_32
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       List[str]     				=					self.get_dummy_components()
        lowercase_					:       Tuple     				=					AudioLDMPipeline(**a_				)
        lowercase_					:       Tuple     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       Dict     				=					["hey"]

        lowercase_					:       int     				=					audioldm_pipe(a_       ,				num_inference_steps=1				)
        lowercase_					:       str     				=					output.audios.shape
        assert audio_shape == (1, 256)

        lowercase_					:       Optional[int]     				=					audioldm_pipe.vocoder.config
        config.model_in_dim *= 2
        lowercase_					:       Tuple     				=					SpeechTaHifiGan(a_				).to(a_				)
        lowercase_					:       Optional[int]     				=					audioldm_pipe(a_       ,				num_inference_steps=1				)
        lowercase_					:       Any     				=					output.audios.shape
        # waveform shape is unchanged, we just have 2x the number of mel channels in the spectrogram
        assert audio_shape == (1, 256)
   def 		_lowerCamelCase (self				)    ->     List[str]:
        self._test_attention_slicing_forward_pass(test_mean_pixel_difference=a_				)
   def 		_lowerCamelCase (self				)    ->     str:
        self._test_inference_batch_single_identical(test_mean_pixel_difference=a_				)





   @unittest.skipIf(
       torch_device != 'cuda' or not is_xformers_available()       ,				reason='XFormers attention is only available with CUDA and `xformers` installed'       ,				)
   def 		_lowerCamelCase (self				)    ->     int:
        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=a_				)



@slow
class       UpperCAmelCase__  (       unittest.TestCase			):






   """simple docstring"""
   def 		_lowerCamelCase (self				)    ->     int:
        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()
   def 		_lowerCamelCase (self       ,				_a       ,				_a="cpu"       ,				_a=torch.floataa       ,				_a=0				)    ->     int:
        lowercase_					:       Tuple     				=					torch.Generator(device=a_				).manual_seed(a_				)
        lowercase_					:       Optional[Any]     				=					np.random.RandomState(a_				).standard_normal((1, 8, 128, 16)				)
        lowercase_					:       Dict     				=					torch.from_numpy(a_				).to(device=a_       ,				dtype=a_				)
        lowercase_					:       Optional[Any]     				=					{
            "prompt": "A hammer hitting a wooden surface",
            "latents": latents,
            "generator": generator,
            "num_inference_steps": 3,
            "guidance_scale": 2.5,
        }
        return inputs
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        lowercase_					:       str     				=					AudioLDMPipeline.from_pretrained('cvssp/audioldm'				)
        lowercase_					:       Any     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       Tuple     				=					self.get_inputs(a_				)
        lowercase_					:       Dict     				=					25
        lowercase_					:       Optional[Any]     				=					audioldm_pipe(**a_				).audios[0]

        assert audio.ndim == 1
        assert len(a_				) == 81_920

        lowercase_					:       Tuple     				=					audio[77_230:77_240]
        lowercase_					:       str     				=					np.array(
            [-0.48_84, -0.46_07, 0.00_23, 0.50_07, 0.58_96, 0.51_51, 0.38_13, -0.02_08, -0.36_87, -0.43_15]				)
        lowercase_					:       int     				=					np.abs(expected_slice - audio_slice				).max()
        assert max_diff < 1e-2





   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        lowercase_					:       int     				=					AudioLDMPipeline.from_pretrained('cvssp/audioldm'				)
        lowercase_					:       Tuple     				=					LMSDiscreteScheduler.from_config(audioldm_pipe.scheduler.config				)
        lowercase_					:       List[str]     				=					audioldm_pipe.to(a_				)
        audioldm_pipe.set_progress_bar_config(disable=a_				)

        lowercase_					:       int     				=					self.get_inputs(a_				)
        lowercase_					:       str     				=					audioldm_pipe(**a_				).audios[0]

        assert audio.ndim == 1
        assert len(a_				) == 81_920

        lowercase_					:       Optional[int]     				=					audio[27_780:27_790]
        lowercase_					:       Union[str, Any]     				=					np.array([-0.21_31, -0.08_73, -0.01_24, -0.01_89, 0.05_69, 0.13_73, 0.18_83, 0.28_86, 0.32_97, 0.22_12]				)
        lowercase_					:       str     				=					np.abs(expected_slice - audio_slice				).max()
        assert max_diff < 3e-2





'''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__  :






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

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

        lowercase_					:       str     				=					self.get_config()

        return config, pixel_values, labels
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        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=_a       ,				initializer_range=self.initializer_range       ,				out_features=self.out_features       ,				out_indices=self.out_indices       ,				num_labels=self.num_labels       ,				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a				)    ->     Tuple:
        lowercase_					:       Optional[int]     				=					ConvNextVaModel(config=_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       List[Any]     				=					model(_a				)
        # 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       ,				_a       ,				_a       ,				_a				)    ->     Any:
        lowercase_					:       int     				=					ConvNextVaForImageClassification(_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       Dict     				=					model(_a       ,				labels=_a				)
        self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels)				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a				)    ->     Optional[Any]:
        lowercase_					:       Any     				=					ConvNextVaBackbone(config=_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       Union[str, Any]     				=					model(_a				)

        # 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
        lowercase_					:       int     				=					None
        lowercase_					:       List[str]     				=					ConvNextVaBackbone(config=_a				)
        model.to(_a				)
        model.eval()
        lowercase_					:       Union[str, Any]     				=					model(_a				)

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





   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        lowercase_					:       Optional[Any]     				=					self.prepare_config_and_inputs()
        lowercase_				,lowercase_				,lowercase_					:       Optional[Any]     				=					config_and_inputs
        lowercase_					:       Dict     				=					{'pixel_values': pixel_values, 'labels': labels}
        return config, inputs_dict



@require_torch
class       UpperCAmelCase__  (       _snake_case					,     _snake_case					,     unittest.TestCase			):






   """simple docstring"""
   A							:       List[str]     =    (
       (
           ConvNextVaModel,
           ConvNextVaForImageClassification,
           ConvNextVaBackbone,
       )
       if is_torch_available()
       else ()
   )
   A							:       Optional[int]     =    (
       {'''feature-extraction''': ConvNextVaModel, '''image-classification''': ConvNextVaForImageClassification}
       if is_torch_available()
       else {}
   )

   A							:       Optional[int]     =    False
   A							:       str     =    False
   A							:       int     =    False
   A							:       Dict     =    False
   A							:       Optional[int]     =    False
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        lowercase_					:       Dict     				=					ConvNextVaModelTester(self				)
        lowercase_					:       List[str]     				=					ConfigTester(self       ,				config_class=_a       ,				has_text_modality=_a       ,				hidden_size=37				)
   def 		_lowerCamelCase (self				)    ->     int:
        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				)    ->     List[Any]:
        return
   @unittest.skip(reason='ConvNextV2 does not use inputs_embeds'				)
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        pass
   @unittest.skip(reason='ConvNextV2 does not support input and output embeddings'				)
   def 		_lowerCamelCase (self				)    ->     Tuple:
        pass
   @unittest.skip(reason='ConvNextV2 does not use feedforward chunking'				)
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        pass
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        if not self.model_tester.is_training:
             return

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

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

             lowercase_					:       str     				=					model_class(_a				)
             model.to(_a				)
             model.train()
             lowercase_					:       Tuple     				=					self._prepare_for_class(_a       ,				_a       ,				return_labels=_a				)
             lowercase_					:       List[Any]     				=					model(**_a				).loss
             loss.backward()
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        if not self.model_tester.is_training:
             return

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

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

             lowercase_					:       Union[str, Any]     				=					model_class(_a				)
             model.to(_a				)
             model.gradient_checkpointing_enable()
             model.train()
             lowercase_					:       List[Any]     				=					self._prepare_for_class(_a       ,				_a       ,				return_labels=_a				)
             lowercase_					:       Optional[int]     				=					model(**_a				).loss
             loss.backward()
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        lowercase_				,lowercase_					:       int     				=					self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase_					:       List[str]     				=					['pixel_values']
             self.assertListEqual(arg_names[:1]       ,				_a				)
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        lowercase_					:       List[Any]     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_a				)
   def 		_lowerCamelCase (self				)    ->     Any:
        def check_hidden_states_output(_a       ,				_a       ,				_a				):
             lowercase_					:       Tuple     				=					model_class(_a				)
             model.to(_a				)
             model.eval()

             with torch.no_grad():
                  lowercase_					:       str     				=					model(**self._prepare_for_class(_a       ,				_a				)				)

             lowercase_					:       str     				=					outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

             lowercase_					:       Optional[int]     				=					self.model_tester.num_stages
             self.assertEqual(len(_a				)       ,				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]       ,				)

        lowercase_				,lowercase_					:       Optional[int]     				=					self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_					:       Optional[Any]     				=					True
             check_hidden_states_output(_a       ,				_a       ,				_a				)

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

             check_hidden_states_output(_a       ,				_a       ,				_a				)
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       Dict     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_a				)





   @slow
   def 		_lowerCamelCase (self				)    ->     Any:
        for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_					:       Optional[int]     				=					ConvNextVaModel.from_pretrained(_a				)
             self.assertIsNotNone(_a				)



def _UpperCamelCase   (       ):
     lowercase_					:       Union[str, Any]     				=					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
     return image






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






   """simple docstring"""
   @cached_property
   def 		_lowerCamelCase (self				)    ->     str:
        return AutoImageProcessor.from_pretrained('facebook/convnextv2-tiny-1k-224'				) if is_vision_available() else None





   @slow
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       List[Any]     				=					ConvNextVaForImageClassification.from_pretrained('facebook/convnextv2-tiny-1k-224'				).to(_a				)

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

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

        # verify the logits
        lowercase_					:       str     				=					torch.Size((1, 1_000)				)
        self.assertEqual(outputs.logits.shape       ,				_a				)

        lowercase_					:       Tuple     				=					torch.tensor([0.99_96, 0.19_66, -0.43_86]				).to(_a				)
        self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,				_a       ,				atol=1e-4				)				)




