
import inspect
import unittest

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

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


if is_torch_available():
							import torch

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


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class    _UpperCAmelCase      :

				"""simple docstring"""




				def __init__(  self		:					Optional[Any],						lowerCamelCase		:					int,						lowerCamelCase		:					Union[str, Any]=13,						lowerCamelCase		:					Dict=32,						lowerCamelCase		:					Tuple=3,						lowerCamelCase		:					List[str]=4,						lowerCamelCase		:					int=[10, 20, 30, 40],						lowerCamelCase		:					int=[2, 2, 3, 2],						lowerCamelCase		:					List[str]=True,						lowerCamelCase		:					int=True,						lowerCamelCase		:					List[Any]=37,						lowerCamelCase		:					Optional[int]="gelu",						lowerCamelCase		:					Optional[Any]=10,						lowerCamelCase		:					Optional[Any]=0.02,						lowerCamelCase		:					Union[str, Any]=["stage2", "stage3", "stage4"],						lowerCamelCase		:					int=[2, 3, 4],						lowerCamelCase		:					Optional[Any]=None,						):

								'''simple docstring'''
								lowercase__     				=    parent
								lowercase__     				=    batch_size
								lowercase__     				=    image_size
								lowercase__     				=    num_channels
								lowercase__     				=    num_stages
								lowercase__     				=    hidden_sizes
								lowercase__     				=    depths
								lowercase__     				=    is_training
								lowercase__     				=    use_labels
								lowercase__     				=    intermediate_size
								lowercase__     				=    hidden_act
								lowercase__     				=    num_labels
								lowercase__     				=    initializer_range
								lowercase__     				=    out_features
								lowercase__     				=    out_indices
								lowercase__     				=    scope




				def        lowercase__     (  self		:					List[Any]						):

								'''simple docstring'''
								lowercase__     				=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

								lowercase__     				=    None
								if self.use_labels:
												lowercase__     				=    ids_tensor([self.batch_size],						self.num_labels						)

								lowercase__     				=    self.get_config()
								return config, pixel_values, labels




				def        lowercase__     (  self		:					Union[str, Any]						):

								'''simple docstring'''
								return ConvNextConfig(
								    num_channels=self.num_channels,						hidden_sizes=self.hidden_sizes,						depths=self.depths,						num_stages=self.num_stages,						hidden_act=self.hidden_act,						is_decoder=_a,						initializer_range=self.initializer_range,						out_features=self.out_features,						out_indices=self.out_indices,						num_labels=self.num_labels,						)




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					List[str],						lowerCamelCase		:					str,						lowerCamelCase		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    ConvNextModel(config=_a						)
								model.to(_a						)
								model.eval()
								lowercase__     				=    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        lowercase__     (  self		:					int,						lowerCamelCase		:					int,						lowerCamelCase		:					Any,						lowerCamelCase		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    ConvNextForImageClassification(_a						)
								model.to(_a						)
								model.eval()
								lowercase__     				=    model(_a,						labels=_a						)
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)						)




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					Any,						lowerCamelCase		:					Optional[Any],						lowerCamelCase		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    ConvNextBackbone(config=_a						)
								model.to(_a						)
								model.eval()
								lowercase__     				=    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__     				=    None
								lowercase__     				=    ConvNextBackbone(config=_a						)
								model.to(_a						)
								model.eval()
								lowercase__     				=    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        lowercase__     (  self		:					str						):

								'''simple docstring'''
								lowercase__     				=    self.prepare_config_and_inputs()
								lowercase__     				=    config_and_inputs
								lowercase__     				=    {"""pixel_values""": pixel_values}
								return config, inputs_dict



@require_torch
class    _UpperCAmelCase      ( UpperCamelCase__	,UpperCamelCase__	,unittest.TestCase			):

				"""simple docstring"""




				lowercase__       							= (
				    (
				        ConvNextModel,
				        ConvNextForImageClassification,
				        ConvNextBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				lowercase__       							= (
				    {"""feature-extraction""": ConvNextModel, """image-classification""": ConvNextForImageClassification}
				    if is_torch_available()
				    else {}
				)

				lowercase__       							= True
				lowercase__       							= False
				lowercase__       							= False
				lowercase__       							= False
				lowercase__       							= False




				def        lowercase__     (  self		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    ConvNextModelTester(self						)
								lowercase__     				=    ConfigTester(self,						config_class=_a,						has_text_modality=_a,						hidden_size=37						)




				def        lowercase__     (  self		:					Optional[int]						):

								'''simple docstring'''
								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        lowercase__     (  self		:					List[str]						):

								'''simple docstring'''
								return




				@unittest.skip(reason='''ConvNext does not use inputs_embeds'''						)
				def        lowercase__     (  self		:					int						):

								'''simple docstring'''
								pass




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

								'''simple docstring'''
								pass




				@unittest.skip(reason='''ConvNext does not use feedforward chunking'''						)
				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								pass




				def        lowercase__     (  self		:					Union[str, Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()

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

												lowercase__     				=    ["""pixel_values"""]
												self.assertListEqual(arg_names[:1],						_a						)




				def        lowercase__     (  self		:					Union[str, Any]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_a						)




				def        lowercase__     (  self		:					Optional[int]						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*_a						)




				def        lowercase__     (  self		:					Dict						):

								'''simple docstring'''
								def check_hidden_states_output(lowerCamelCase		:					int,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					int						):
												lowercase__     				=    model_class(_a						)
												model.to(_a						)
												model.eval()

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

												lowercase__     				=    outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												lowercase__     				=    self.model_tester.num_stages
												self.assertEqual(len(_a						),						expected_num_stages + 1						)

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

								lowercase__     				=    self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												lowercase__     				=    True
												check_hidden_states_output(_a,						_a,						_a						)

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												lowercase__     				=    True

												check_hidden_states_output(_a,						_a,						_a						)




				def        lowercase__     (  self		:					Any						):

								'''simple docstring'''
								lowercase__     				=    self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_a						)




				@slow
				def        lowercase__     (  self		:					List[str]						):

								'''simple docstring'''
								for model_name in CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowercase__     				=    ConvNextModel.from_pretrained(_a						)
												self.assertIsNotNone(_a						)






def 						a    (		):






				'''simple docstring'''
				lowercase__     				=    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
				return image
@require_torch
@require_vision
class    _UpperCAmelCase      ( unittest.TestCase			):

				"""simple docstring"""




				@cached_property
				def        lowercase__     (  self		:					str						):

								'''simple docstring'''
								return AutoImageProcessor.from_pretrained('''facebook/convnext-tiny-224'''						) if is_vision_available() else None




				@slow
				def        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    ConvNextForImageClassification.from_pretrained('''facebook/convnext-tiny-224'''						).to(_a						)

								lowercase__     				=    self.default_image_processor
								lowercase__     				=    prepare_img()
								lowercase__     				=    image_processor(images=_a,						return_tensors='''pt'''						).to(_a						)

								# forward pass
								with torch.no_grad():
												lowercase__     				=    model(**_a						)

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

								lowercase__     				=    torch.tensor([-0.0260, -0.4739, 0.1911]						).to(_a						)

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



@require_torch
class    _UpperCAmelCase      ( unittest.TestCase	,UpperCamelCase__			):

				"""simple docstring"""




				lowercase__       							= (ConvNextBackbone,) if is_torch_available() else ()
				lowercase__       							= ConvNextConfig

				lowercase__       							= False




				def        lowercase__     (  self		:					List[str]						):

								'''simple docstring'''
								lowercase__     				=    ConvNextModelTester(self						)












import copy
from typing import Any, Dict, List, Optional, Union

import numpy as np
import torch

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, logging


A__			:    Tuple         =				logging.get_logger(__name__)
class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				lowercase__       							= ["""input_features""", """is_longer"""]




				def __init__(  self		:					Optional[int],						lowerCamelCase		:					int=64,						lowerCamelCase		:					Union[str, Any]=48_000,						lowerCamelCase		:					str=480,						lowerCamelCase		:					Tuple=10,						lowerCamelCase		:					List[Any]=1_024,						lowerCamelCase		:					Optional[int]=0.0,						lowerCamelCase		:					Optional[Any]=False,						lowerCamelCase		:					float = 0,						lowerCamelCase		:					float = 14_000,						lowerCamelCase		:					int = None,						lowerCamelCase		:					str = "fusion",						lowerCamelCase		:					str = "repeatpad",						**lowerCamelCase		:					Dict,						):

								'''simple docstring'''
								super().__init__(
								    feature_size=lowerCamelCase,						sampling_rate=lowerCamelCase,						padding_value=lowerCamelCase,						return_attention_mask=lowerCamelCase,						**lowerCamelCase,						)
								lowercase__     				=    top_db
								lowercase__     				=    truncation
								lowercase__     				=    padding
								lowercase__     				=    fft_window_size
								lowercase__     				=    (fft_window_size >> 1) + 1
								lowercase__     				=    hop_length
								lowercase__     				=    max_length_s
								lowercase__     				=    max_length_s * sampling_rate
								lowercase__     				=    sampling_rate
								lowercase__     				=    frequency_min
								lowercase__     				=    frequency_max
								lowercase__     				=    mel_filter_bank(
								    num_frequency_bins=self.nb_frequency_bins,						num_mel_filters=lowerCamelCase,						min_frequency=lowerCamelCase,						max_frequency=lowerCamelCase,						sampling_rate=lowerCamelCase,						norm=lowerCamelCase,						mel_scale='''htk''',						)
								lowercase__     				=    mel_filter_bank(
								    num_frequency_bins=self.nb_frequency_bins,						num_mel_filters=lowerCamelCase,						min_frequency=lowerCamelCase,						max_frequency=lowerCamelCase,						sampling_rate=lowerCamelCase,						norm='''slaney''',						mel_scale='''slaney''',						)




				def        lowercase__     (  self		:					Tuple						):

								'''simple docstring'''
								lowercase__     				=    copy.deepcopy(self.__dict__						)
								lowercase__     				=    self.__class__.__name__
								if "mel_filters" in output:
												del output["mel_filters"]
								if "mel_filters_slaney" in output:
												del output["mel_filters_slaney"]
								return output




				def        lowercase__     (  self		:					Optional[int],						lowerCamelCase		:					np.array,						lowerCamelCase		:					Optional[np.array] = None						):

								'''simple docstring'''
								lowercase__     				=    spectrogram(
								    lowerCamelCase,						window_function(self.fft_window_size,						'''hann'''						),						frame_length=self.fft_window_size,						hop_length=self.hop_length,						power=2.0,						mel_filters=lowerCamelCase,						log_mel='''dB''',						)
								return log_mel_spectrogram.T




				def        lowercase__     (  self		:					int,						lowerCamelCase		:					str,						lowerCamelCase		:					List[str],						lowerCamelCase		:					Optional[Any]						):

								'''simple docstring'''
								lowercase__     				=    np.array_split(list(range(0,						total_frames - chunk_frames + 1						)						),						3						)
								if len(ranges[1]						) == 0:
												# if the audio is too short, we just use the first chunk
												lowercase__     				=    [0]
								if len(ranges[2]						) == 0:
												# if the audio is too short, we just use the first chunk
												lowercase__     				=    [0]
								# randomly choose index for each part
								lowercase__     				=    np.random.choice(ranges[0]						)
								lowercase__     				=    np.random.choice(ranges[1]						)
								lowercase__     				=    np.random.choice(ranges[2]						)

								lowercase__     				=    mel[idx_front : idx_front + chunk_frames, :]
								lowercase__     				=    mel[idx_middle : idx_middle + chunk_frames, :]
								lowercase__     				=    mel[idx_back : idx_back + chunk_frames, :]

								lowercase__     				=    torch.tensor(mel[None, None, :]						)
								lowercase__     				=    torch.nn.functional.interpolate(
								    lowerCamelCase,						size=[chunk_frames, 64],						mode='''bilinear''',						align_corners=lowerCamelCase						)
								lowercase__     				=    mel_shrink[0][0].numpy()
								lowercase__     				=    np.stack([mel_shrink, mel_chunk_front, mel_chunk_middle, mel_chunk_back],						axis=0						)
								return mel_fusion




				def        lowercase__     (  self		:					List[str],						lowerCamelCase		:					np.array,						lowerCamelCase		:					int,						lowerCamelCase		:					Dict,						lowerCamelCase		:					Union[str, Any]						):

								'''simple docstring'''
								if waveform.shape[0] > max_length:
												if truncation == "rand_trunc":
																lowercase__     				=    True
																# random crop to max_length (for compatibility) -> this should be handled by self.pad
																lowercase__     				=    len(lowerCamelCase						) - max_length
																lowercase__     				=    np.random.randint(0,						overflow + 1						)
																lowercase__     				=    waveform[idx : idx + max_length]
																lowercase__     				=    self._np_extract_fbank_features(lowerCamelCase,						self.mel_filters_slaney						)[None, :]
												elif truncation == "fusion":
																lowercase__     				=    self._np_extract_fbank_features(lowerCamelCase,						self.mel_filters						)
																lowercase__     				=    max_length // self.hop_length + 1  # the +1 related to how the spectrogram is computed
																lowercase__     				=    mel.shape[0]
																if chunk_frames == total_frames:
																				# there is a corner case where the audio length is larger than max_length but smaller than max_length+hop_length.
																				# In this case, we just use the whole audio.
																				lowercase__     				=    np.stack([mel, mel, mel, mel],						axis=0						)
																				lowercase__     				=    False
																else:
																				lowercase__     				=    self._random_mel_fusion(lowerCamelCase,						lowerCamelCase,						lowerCamelCase						)
																				lowercase__     				=    True
												else:
																raise NotImplementedError(F"""data_truncating {truncation} not implemented"""						)

								else:
												lowercase__     				=    False
												# only use repeat as a new possible value for padding. you repeat the audio before applying the usual max_length padding
												if waveform.shape[0] < max_length:
																if padding == "repeat":
																				lowercase__     				=    int(max_length / len(lowerCamelCase						)						)
																				lowercase__     				=    np.stack(np.tile(lowerCamelCase,						n_repeat + 1						)						)[:max_length]
																if padding == "repeatpad":
																				lowercase__     				=    int(max_length / len(lowerCamelCase						)						)
																				lowercase__     				=    np.stack(np.tile(lowerCamelCase,						lowerCamelCase						)						)
																lowercase__     				=    np.pad(lowerCamelCase,						(0, max_length - waveform.shape[0]),						mode='''constant''',						constant_values=0						)

												if truncation == "fusion":
																lowercase__     				=    self._np_extract_fbank_features(lowerCamelCase,						self.mel_filters						)
																lowercase__     				=    np.stack([input_mel, input_mel, input_mel, input_mel],						axis=0						)
												else:
																lowercase__     				=    self._np_extract_fbank_features(lowerCamelCase,						self.mel_filters_slaney						)[None, :]

								return input_mel, longer




				def __call__(  self		:					Union[str, Any],						lowerCamelCase		:					Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]],						lowerCamelCase		:					str = None,						lowerCamelCase		:					Optional[str] = None,						lowerCamelCase		:					Optional[int] = None,						lowerCamelCase		:					Optional[int] = None,						lowerCamelCase		:					Optional[Union[str, TensorType]] = None,						**lowerCamelCase		:					List[str],						):

								'''simple docstring'''
								lowercase__     				=    truncation if truncation is not None else self.truncation
								lowercase__     				=    padding if padding else self.padding

								if sampling_rate is not None:
												if sampling_rate != self.sampling_rate:
																raise ValueError(
																    F"""The model corresponding to this feature extractor: {self.__class__.__name__} was trained using a"""
																    F""" sampling rate of {self.sampling_rate}. Please make sure that the provided `raw_speech` input"""
																    F""" was sampled with {self.sampling_rate} and not {sampling_rate}."""						)
								else:
												logger.warning(
												    '''It is strongly recommended to pass the `sampling_rate` argument to this function. '''
												    '''Failing to do so can result in silent errors that might be hard to debug.'''						)

								lowercase__     				=    isinstance(lowerCamelCase,						np.ndarray						) and len(raw_speech.shape						) > 1
								if is_batched_numpy and len(raw_speech.shape						) > 2:
												raise ValueError(F"""Only mono-channel audio is supported for input to {self}"""						)
								lowercase__     				=    is_batched_numpy or (
								    isinstance(lowerCamelCase,						(list, tuple)						) and (isinstance(raw_speech[0],						(np.ndarray, tuple, list)						))
								)

								if is_batched:
												lowercase__     				=    [np.asarray(lowerCamelCase,						dtype=np.floataa						) for speech in raw_speech]
								elif not is_batched and not isinstance(lowerCamelCase,						np.ndarray						):
												lowercase__     				=    np.asarray(lowerCamelCase,						dtype=np.floataa						)
								elif isinstance(lowerCamelCase,						np.ndarray						) and raw_speech.dtype is np.dtype(np.floataa						):
												lowercase__     				=    raw_speech.astype(np.floataa						)

								# always return batch
								if not is_batched:
												lowercase__     				=    [np.asarray(lowerCamelCase						)]

								# convert to mel spectrogram, truncate and pad if needed.
								lowercase__     				=    [
								    self._get_input_mel(lowerCamelCase,						max_length if max_length else self.nb_max_samples,						lowerCamelCase,						lowerCamelCase						)
								    for waveform in raw_speech
								]

								lowercase__     				=    []
								lowercase__     				=    []
								for mel, longer in padded_inputs:
												input_mel.append(lowerCamelCase						)
												is_longer.append(lowerCamelCase						)

								if truncation == "fusion" and sum(lowerCamelCase						) == 0:
												# if no audio is longer than 10s, then randomly select one audio to be longer
												lowercase__     				=    np.random.randint(0,						len(lowerCamelCase						)						)
												lowercase__     				=    True

								if isinstance(input_mel[0],						lowerCamelCase						):
												lowercase__     				=    [np.asarray(lowerCamelCase,						dtype=np.floataa						) for feature in input_mel]

								# is_longer is a list of bool
								lowercase__     				=    [[longer] for longer in is_longer]

								lowercase__     				=    {'''input_features''': input_mel, '''is_longer''': is_longer}
								lowercase__     				=    BatchFeature(lowerCamelCase						)

								if return_tensors is not None:
												lowercase__     				=    input_features.convert_to_tensors(lowerCamelCase						)

								return input_features




