



import inspect
import unittest

import numpy as np

from tests.test_modeling_common import floats_tensor
from transformers import DetrConfig, MaskFormerConfig, SwinConfig, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch

			from transformers import MaskFormerForInstanceSegmentation, MaskFormerModel

			if is_vision_available():
						from transformers import MaskFormerImageProcessor

if is_vision_available():
			from PIL import Image

class        UpperCAmelCase_  :

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A=2				,							_A=True				,							_A=False				,							_A=10				,							_A=3				,							_A=32 * 4				,							_A=32 * 6				,							_A=4				,							_A=32				,							):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= parent
													__SCREAMING_SNAKE_CASE									= batch_size
													__SCREAMING_SNAKE_CASE									= is_training
													__SCREAMING_SNAKE_CASE									= use_auxiliary_loss
													__SCREAMING_SNAKE_CASE									= num_queries
													__SCREAMING_SNAKE_CASE									= num_channels
													__SCREAMING_SNAKE_CASE									= min_size
													__SCREAMING_SNAKE_CASE									= max_size
													__SCREAMING_SNAKE_CASE									= num_labels
													__SCREAMING_SNAKE_CASE									= mask_feature_size






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= floats_tensor([self.batch_size, self.num_channels, self.min_size, self.max_size]		).to(
													    _A		)

													__SCREAMING_SNAKE_CASE									= torch.ones([self.batch_size, self.min_size, self.max_size]				,							device=_A		)

													__SCREAMING_SNAKE_CASE									= (
													    torch.rand([self.batch_size, self.num_labels, self.min_size, self.max_size]				,							device=_A		) > 0.5
													).float()
													__SCREAMING_SNAKE_CASE									= (torch.rand((self.batch_size, self.num_labels)				,							device=_A		) > 0.5).long()

													__SCREAMING_SNAKE_CASE									= self.get_config()
													return config, pixel_values, pixel_mask, mask_labels, class_labels






						def 	_A     (   self		):







													'''simple docstring'''

													return MaskFormerConfig.from_backbone_and_decoder_configs(
													    backbone_config=SwinConfig(
													        depths=[1, 1, 1, 1]				,							)				,							decoder_config=DetrConfig(
													        decoder_ffn_dim=128				,							num_queries=self.num_queries				,							decoder_attention_heads=2				,							d_model=self.mask_feature_size				,							)				,							mask_feature_size=self.mask_feature_size				,							fpn_feature_size=self.mask_feature_size				,							num_channels=self.num_channels				,							num_labels=self.num_labels				,							)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.prepare_config_and_inputs()
													__SCREAMING_SNAKE_CASE									= {'pixel_values': pixel_values, 'pixel_mask': pixel_mask}
													return config, inputs_dict






						def 	_A     (   self				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= output.encoder_hidden_states
													__SCREAMING_SNAKE_CASE									= output.pixel_decoder_hidden_states
													__SCREAMING_SNAKE_CASE									= output.transformer_decoder_hidden_states

													self.parent.assertTrue(len(_A		)				,							len(config.backbone_config.depths		)		)
													self.parent.assertTrue(len(_A		)				,							len(config.backbone_config.depths		)		)
													self.parent.assertTrue(len(_A		)				,							config.decoder_config.decoder_layers		)






						def 	_A     (   self				,							_A				,							_A				,							_A				,							_A=False		):







													'''simple docstring'''

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= MaskFormerModel(config=_A		)
																				model.to(_A		)
																				model.eval()

																				__SCREAMING_SNAKE_CASE									= model(pixel_values=_A				,							pixel_mask=_A		)
																				__SCREAMING_SNAKE_CASE									= model(_A				,							output_hidden_states=_A		)
													# the correct shape of output.transformer_decoder_hidden_states ensure the correcteness of the
													# encoder and pixel decoder
													self.parent.assertEqual(
													    output.transformer_decoder_last_hidden_state.shape				,							(self.batch_size, self.num_queries, self.mask_feature_size)				,							)
													# let's ensure the other two hidden state exists
													self.parent.assertTrue(output.pixel_decoder_last_hidden_state is not None		)
													self.parent.assertTrue(output.encoder_last_hidden_state is not None		)

													if output_hidden_states:
																				self.check_output_hidden_state(_A				,							_A		)






						def 	_A     (   self				,							_A				,							_A				,							_A				,							_A				,							_A		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MaskFormerForInstanceSegmentation(config=_A		)
													model.to(_A		)
													model.eval()

													def comm_check_on_output(_A		):
																				# let's still check that all the required stuff is there
																				self.parent.assertTrue(result.transformer_decoder_last_hidden_state is not None		)
																				self.parent.assertTrue(result.pixel_decoder_last_hidden_state is not None		)
																				self.parent.assertTrue(result.encoder_last_hidden_state is not None		)
																				# okay, now we need to check the logits shape
																				# due to the encoder compression, masks have a //4 spatial size
																				self.parent.assertEqual(
																				    result.masks_queries_logits.shape				,							(self.batch_size, self.num_queries, self.min_size // 4, self.max_size // 4)				,							)
																				# + 1 for null class
																				self.parent.assertEqual(
																				    result.class_queries_logits.shape				,							(self.batch_size, self.num_queries, self.num_labels + 1)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(pixel_values=_A				,							pixel_mask=_A		)
																				__SCREAMING_SNAKE_CASE									= model(_A		)

																				comm_check_on_output(_A		)

																				__SCREAMING_SNAKE_CASE									= model(
																				    pixel_values=_A				,							pixel_mask=_A				,							mask_labels=_A				,							class_labels=_A		)

													comm_check_on_output(_A		)

													self.parent.assertTrue(result.loss is not None		)
													self.parent.assertEqual(result.loss.shape				,							torch.Size([1]		)		)







@require_torch
class        UpperCAmelCase_  (       snake_case__   ,   snake_case__   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			List[str]      =  (MaskFormerModel, MaskFormerForInstanceSegmentation) if is_torch_available() else ()
						UpperCamelCase__      :			str      =  (
						    {"""feature-extraction""": MaskFormerModel, """image-segmentation""": MaskFormerForInstanceSegmentation}
						    if is_torch_available()
						    else {}
						)

						UpperCamelCase__      :			int      =  False
						UpperCamelCase__      :			str      =  False
						UpperCamelCase__      :			Optional[int]      =  False
						UpperCamelCase__      :			Optional[int]      =  False






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MaskFormerModelTester(self		)
													__SCREAMING_SNAKE_CASE									= ConfigTester(self				,							config_class=_A				,							has_text_modality=_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													self.config_tester.run_common_tests()






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskformer_model(_A				,							**_A				,							output_hidden_states=_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_maskformer_instance_segmentation_head_model(*_A		)






						@unittest.skip(reason='MaskFormer does not use inputs_embeds'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip(reason='MaskFormer does not have a get_input_embeddings method'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip(reason='MaskFormer is not a generative model'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip(reason='MaskFormer does not use token embeddings'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@require_torch_multi_gpu
						@unittest.skip(
						    reason='MaskFormer has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'		)
						def 	_A     (   self		):







													'''simple docstring'''

													pass






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

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

																				__SCREAMING_SNAKE_CASE									= ['pixel_values']
																				self.assertListEqual(arg_names[:1]				,							_A		)






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

													for model_name in ["facebook/maskformer-swin-small-coco"]:
																				__SCREAMING_SNAKE_CASE									= MaskFormerModel.from_pretrained(_A		)
																				self.assertIsNotNone(_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (self.model_tester.min_size,) * 2
													__SCREAMING_SNAKE_CASE									= {
													    'pixel_values': torch.randn((2, 3, *size)				,							device=_A		),
													    'mask_labels': torch.randn((2, 10, *size)				,							device=_A		),
													    'class_labels': torch.zeros(2				,							10				,							device=_A		).long(),
													}

													__SCREAMING_SNAKE_CASE									= MaskFormerForInstanceSegmentation(MaskFormerConfig()		).to(_A		)
													__SCREAMING_SNAKE_CASE									= model(**_A		)
													self.assertTrue(outputs.loss is not None		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()
													self.model_tester.create_and_check_maskformer_model(_A				,							**_A				,							output_hidden_states=_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs_for_common()

													for model_class in self.all_model_classes:
																				__SCREAMING_SNAKE_CASE									= model_class(_A		).to(_A		)
																				__SCREAMING_SNAKE_CASE									= model(**_A				,							output_attentions=_A		)
																				self.assertTrue(outputs.attentions is not None		)






						def 	_A     (   self		):







													'''simple docstring'''

													if not self.model_tester.is_training:
																				return
													# only MaskFormerForInstanceSegmentation has the loss
													__SCREAMING_SNAKE_CASE									= self.all_model_classes[1]
													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()

													__SCREAMING_SNAKE_CASE									= model_class(_A		)
													model.to(_A		)
													model.train()

													__SCREAMING_SNAKE_CASE									= model(_A				,							mask_labels=_A				,							class_labels=_A		).loss
													loss.backward()






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.all_model_classes[1]
													__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE							,	__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													__SCREAMING_SNAKE_CASE									= True
													__SCREAMING_SNAKE_CASE									= True

													__SCREAMING_SNAKE_CASE									= model_class(_A		)
													model.to(_A		)
													model.train()

													__SCREAMING_SNAKE_CASE									= model(_A				,							mask_labels=_A				,							class_labels=_A		)

													__SCREAMING_SNAKE_CASE									= outputs.encoder_hidden_states[0]
													encoder_hidden_states.retain_grad()

													__SCREAMING_SNAKE_CASE									= outputs.pixel_decoder_hidden_states[0]
													pixel_decoder_hidden_states.retain_grad()
													# we requires_grad=True in inputs_embeds (line 2152), the original implementation don't
													__SCREAMING_SNAKE_CASE									= outputs.transformer_decoder_hidden_states[0]
													transformer_decoder_hidden_states.retain_grad()

													__SCREAMING_SNAKE_CASE									= outputs.attentions[0]
													attentions.retain_grad()

													outputs.loss.backward(retain_graph=_A		)

													self.assertIsNotNone(encoder_hidden_states.grad		)
													self.assertIsNotNone(pixel_decoder_hidden_states.grad		)
													self.assertIsNotNone(transformer_decoder_hidden_states.grad		)
													self.assertIsNotNone(attentions.grad		)







lowerCAmelCase__		:					Optional[Any]							=1E-4






def      __lowercase       (			)    ->			str:
							__SCREAMING_SNAKE_CASE									= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
							return image

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

						'''simple docstring'''




						@cached_property
						def 	_A     (   self		):







													'''simple docstring'''

													return (
													    MaskFormerImageProcessor.from_pretrained('facebook/maskformer-swin-small-coco'		)
													    if is_vision_available()
													    else None
													)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= MaskFormerModel.from_pretrained('facebook/maskformer-swin-small-coco'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(_A				,							return_tensors='pt'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= inputs['pixel_values'].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
													# check size
													self.assertEqual(_A				,							(1, 3, 800, 1_088)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[-0.0_4_8_2, 0.9_2_2_8, 0.4_9_5_1], [-0.2_5_4_7, 0.8_0_1_7, 0.8_5_2_7], [-0.0_0_6_9, 0.3_3_8_5, -0.0_0_8_9]]		).to(_A		)
													self.assertTrue(
													    torch.allclose(
													        outputs.encoder_last_hidden_state[0, 0, :3, :3]				,							_A				,							atol=_A		)		)

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[-0.8_4_2_2, -0.8_4_3_4, -0.9_7_1_8], [-1.0_1_4_4, -0.5_5_6_5, -0.4_1_9_5], [-1.0_0_3_8, -0.4_4_8_4, -0.1_9_6_1]]		).to(_A		)
													self.assertTrue(
													    torch.allclose(
													        outputs.pixel_decoder_last_hidden_state[0, 0, :3, :3]				,							_A				,							atol=_A		)		)

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[0.2_8_5_2, -0.0_1_5_9, 0.9_7_3_5], [0.6_2_5_4, 0.1_8_5_8, 0.8_5_2_9], [-0.0_6_8_0, -0.4_1_1_6, 1.8_4_1_3]]		).to(_A		)
													self.assertTrue(
													    torch.allclose(
													        outputs.transformer_decoder_last_hidden_state[0, :3, :3]				,							_A				,							atol=_A		)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (
													    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'		)
													    .to(_A		)
													    .eval()
													)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(_A				,							return_tensors='pt'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= inputs['pixel_values'].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
													# check size
													self.assertEqual(_A				,							(1, 3, 800, 1_088)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)
													# masks_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.masks_queries_logits
													self.assertEqual(
													    masks_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)				,							)
													__SCREAMING_SNAKE_CASE									= [
													    [-1.3_7_3_7_1_2_4, -1.7_7_2_4_9_3_7, -1.9_3_6_4_2_3_3],
													    [-1.5_9_7_7_2_8_1, -1.9_8_6_7_9_3_9, -2.1_5_2_3_6_9_5],
													    [-1.5_7_9_5_3_9_8, -1.9_2_6_9_8_3_2, -2.0_9_3_9_4_2],
													]
													__SCREAMING_SNAKE_CASE									= torch.tensor(_A		).to(_A		)
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]				,							_A				,							atol=_A		)		)
													# class_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.class_queries_logits
													self.assertEqual(
													    class_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [
													        [1.6512e00, -5.2572e00, -3.3519e00],
													        [3.6169e-02, -5.9025e00, -2.9313e00],
													        [1.0766e-04, -7.7630e00, -5.1263e00],
													    ]		).to(_A		)
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]				,							_A				,							atol=_A		)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (
													    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-resnet101-coco-stuff'		)
													    .to(_A		)
													    .eval()
													)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor
													__SCREAMING_SNAKE_CASE									= prepare_img()
													__SCREAMING_SNAKE_CASE									= image_processor(_A				,							return_tensors='pt'		).to(_A		)
													__SCREAMING_SNAKE_CASE									= inputs['pixel_values'].shape
													# check size is divisible by 32
													self.assertTrue((inputs_shape[-1] % 32) == 0 and (inputs_shape[-2] % 32) == 0		)
													# check size
													self.assertEqual(_A				,							(1, 3, 800, 1_088)		)

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)
													# masks_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.masks_queries_logits
													self.assertEqual(
													    masks_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, inputs_shape[-2] // 4, inputs_shape[-1] // 4)				,							)
													__SCREAMING_SNAKE_CASE									= [[-0.9_0_4_6, -2.6_3_6_6, -4.6_0_6_2], [-3.4_1_7_9, -5.7_8_9_0, -8.8_0_5_7], [-4.9_1_7_9, -7.6_5_6_0, -1_0.7_7_1_1]]
													__SCREAMING_SNAKE_CASE									= torch.tensor(_A		).to(_A		)
													self.assertTrue(torch.allclose(masks_queries_logits[0, 0, :3, :3]				,							_A				,							atol=_A		)		)
													# class_queries_logits
													__SCREAMING_SNAKE_CASE									= outputs.class_queries_logits
													self.assertEqual(
													    class_queries_logits.shape				,							(1, model.config.decoder_config.num_queries, model.config.num_labels + 1)		)
													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [[4.7_1_8_8, -3.2_5_8_5, -2.8_8_5_7], [6.6_8_7_1, -2.9_1_8_1, -1.2_4_8_7], [7.2_4_4_9, -2.2_7_6_4, -2.1_8_7_4]]		).to(_A		)
													self.assertTrue(torch.allclose(outputs.class_queries_logits[0, :3, :3]				,							_A				,							atol=_A		)		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= (
													    MaskFormerForInstanceSegmentation.from_pretrained('facebook/maskformer-swin-small-coco'		)
													    .to(_A		)
													    .eval()
													)
													__SCREAMING_SNAKE_CASE									= self.default_image_processor

													__SCREAMING_SNAKE_CASE									= image_processor(
													    [np.zeros((3, 800, 1_333)		), np.zeros((3, 800, 1_333)		)]				,							segmentation_maps=[np.zeros((384, 384)		).astype(np.floataa		), np.zeros((384, 384)		).astype(np.floataa		)]				,							return_tensors='pt'				,							)

													__SCREAMING_SNAKE_CASE									= inputs['pixel_values'].to(_A		)
													__SCREAMING_SNAKE_CASE									= [el.to(_A		) for el in inputs['mask_labels']]
													__SCREAMING_SNAKE_CASE									= [el.to(_A		) for el in inputs['class_labels']]

													with torch.no_grad():
																				__SCREAMING_SNAKE_CASE									= model(**_A		)

													self.assertTrue(outputs.loss is not None		)











import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


lowerCAmelCase__		:					Dict							=random.Random()

if is_torch_available():
			import torch






def      __lowercase       (			a__							,       a__=1.0							,       a__=None							,       a__=None       )    ->			Any:
							if rng is None:
														__SCREAMING_SNAKE_CASE									= global_rng

							__SCREAMING_SNAKE_CASE									= []
							for batch_idx in range(shape[0]       ):
														values.append([]       )
														for _ in range(shape[1]       ):
																					values[-1].append(rng.random() * scale       )

							return values

class        UpperCAmelCase_  (       unittest.TestCase	):

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A=7				,							_A=400				,							_A=2_000				,							_A=1				,							_A=0.0				,							_A=16_000				,							_A=True				,							_A=True				,							):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= parent
													__SCREAMING_SNAKE_CASE									= batch_size
													__SCREAMING_SNAKE_CASE									= min_seq_length
													__SCREAMING_SNAKE_CASE									= max_seq_length
													__SCREAMING_SNAKE_CASE									= (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
													__SCREAMING_SNAKE_CASE									= feature_size
													__SCREAMING_SNAKE_CASE									= padding_value
													__SCREAMING_SNAKE_CASE									= sampling_rate
													__SCREAMING_SNAKE_CASE									= return_attention_mask
													__SCREAMING_SNAKE_CASE									= do_normalize






						def 	_A     (   self		):







													'''simple docstring'''

													return {
													    "feature_size": self.feature_size,
													    "padding_value": self.padding_value,
													    "sampling_rate": self.sampling_rate,
													    "return_attention_mask": self.return_attention_mask,
													    "do_normalize": self.do_normalize,
													}






						def 	_A     (   self				,							_A=False				,							_A=False		):







													'''simple docstring'''

													def _flatten(_A		):
																				return list(itertools.chain(*_A		)		)

													if equal_length:
																				__SCREAMING_SNAKE_CASE									= floats_list((self.batch_size, self.max_seq_length)		)
													else:
																				# make sure that inputs increase in size
																				__SCREAMING_SNAKE_CASE									= [
																				    _flatten(floats_list((x, self.feature_size)		)		)
																				    for x in range(self.min_seq_length				,							self.max_seq_length				,							self.seq_length_diff		)
																				]

													if numpify:
																				__SCREAMING_SNAKE_CASE									= [np.asarray(_A		) for x in speech_inputs]

													return speech_inputs







@require_torch
@require_torchaudio
class        UpperCAmelCase_  (       UpperCamelCase_   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			Optional[int]      =  ASTFeatureExtractor






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= ASTFeatureExtractionTester(self		)






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
													# create three inputs of length 800, 1000, and 1200
													__SCREAMING_SNAKE_CASE									= [floats_list((1, x)		)[0] for x in range(800				,							1_400				,							200		)]
													__SCREAMING_SNAKE_CASE									= [np.asarray(_A		) for speech_input in speech_inputs]

													# Test not batched input
													__SCREAMING_SNAKE_CASE									= feat_extract(speech_inputs[0]				,							return_tensors='np'		).input_values
													__SCREAMING_SNAKE_CASE									= feat_extract(np_speech_inputs[0]				,							return_tensors='np'		).input_values
													self.assertTrue(np.allclose(_A				,							_A				,							atol=1e-3		)		)

													# Test batched
													__SCREAMING_SNAKE_CASE									= feat_extract(_A				,							padding=_A				,							return_tensors='np'		).input_values
													__SCREAMING_SNAKE_CASE									= feat_extract(_A				,							padding=_A				,							return_tensors='np'		).input_values
													for enc_seq_a, enc_seq_a in zip(_A				,							_A		):
																				self.assertTrue(np.allclose(_A				,							_A				,							atol=1e-3		)		)

													# Test 2-D numpy arrays are batched.
													__SCREAMING_SNAKE_CASE									= [floats_list((1, x)		)[0] for x in (800, 800, 800)]
													__SCREAMING_SNAKE_CASE									= np.asarray(_A		)
													__SCREAMING_SNAKE_CASE									= feat_extract(_A				,							return_tensors='np'		).input_values
													__SCREAMING_SNAKE_CASE									= feat_extract(_A				,							return_tensors='np'		).input_values
													for enc_seq_a, enc_seq_a in zip(_A				,							_A		):
																				self.assertTrue(np.allclose(_A				,							_A				,							atol=1e-3		)		)






						@require_torch
						def 	_A     (   self		):







													'''simple docstring'''

													import torch

													__SCREAMING_SNAKE_CASE									= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()		)
													__SCREAMING_SNAKE_CASE									= np.random.rand(100		).astype(np.floataa		)
													__SCREAMING_SNAKE_CASE									= np_speech_inputs.tolist()

													for inputs in [py_speech_inputs, np_speech_inputs]:
																				__SCREAMING_SNAKE_CASE									= feature_extractor.pad([{'input_values': inputs}]				,							return_tensors='np'		)
																				self.assertTrue(np_processed.input_values.dtype == np.floataa		)
																				__SCREAMING_SNAKE_CASE									= feature_extractor.pad([{'input_values': inputs}]				,							return_tensors='pt'		)
																				self.assertTrue(pt_processed.input_values.dtype == torch.floataa		)






						def 	_A     (   self				,							_A		):







													'''simple docstring'''

													from datasets import load_dataset

													__SCREAMING_SNAKE_CASE									= load_dataset('hf-internal-testing/librispeech_asr_dummy'				,							'clean'				,							split='validation'		)
													# automatic decoding with librispeech
													__SCREAMING_SNAKE_CASE									= ds.sort('id'		).select(range(_A		)		)[:num_samples]['audio']

													return [x["array"] for x in speech_samples]






						@require_torch
						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= torch.tensor(
													    [-0.9_8_9_4, -1.2_7_7_6, -0.9_0_6_6, -1.2_7_7_6, -0.9_3_4_9, -1.2_6_0_9, -1.0_3_8_6, -1.2_7_7_6,
													     -1.1_5_6_1, -1.2_7_7_6, -1.2_0_5_2, -1.2_7_2_3, -1.2_1_9_0, -1.2_1_3_2, -1.2_7_7_6, -1.1_1_3_3,
													     -1.1_9_5_3, -1.1_3_4_3, -1.1_5_8_4, -1.2_2_0_3, -1.1_7_7_0, -1.2_4_7_4, -1.2_3_8_1, -1.1_9_3_6,
													     -0.9_2_7_0, -0.8_3_1_7, -0.8_0_4_9, -0.7_7_0_6, -0.7_5_6_5, -0.7_8_6_9]		)
													# fmt: on

													__SCREAMING_SNAKE_CASE									= self._load_datasamples(1		)
													__SCREAMING_SNAKE_CASE									= ASTFeatureExtractor()
													__SCREAMING_SNAKE_CASE									= feature_extractor(_A				,							return_tensors='pt'		).input_values
													self.assertEquals(input_values.shape				,							(1, 1_024, 128)		)
													self.assertTrue(torch.allclose(input_values[0, 0, :30]				,							_A				,							atol=1e-4		)		)






