


'''simple docstring'''






import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
		pass

if is_speech_available():
		from transformers import EncodecFeatureExtractor, MusicgenProcessor


__UpperCAmelCase    				=							random.Random()


def 					_snake_case   (     A ,    A=1.0 ,    A=None ,    A=None       )						->    Tuple:
							if rng is None:
														lowerCAmelCase__          =	global_rng

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

							return values






@require_torch
@require_sentencepiece
class 		a__				(							unittest.TestCase  ):





							'''simple docstring'''






							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														lowerCAmelCase__          =	'''facebook/musicgen-small'''
														lowerCAmelCase__          =	tempfile.mkdtemp()







							def   __SCREAMING_SNAKE_CASE							(		self					,    **lowerCamelCase_   )       ->							int:
														return TaTokenizer.from_pretrained(self.checkpoint					,    **lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self					,    **lowerCamelCase_   )       ->							int:
														return EncodecFeatureExtractor.from_pretrained(self.checkpoint					,    **lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														shutil.rmtree(self.tmpdirname   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[Any]:
														lowerCAmelCase__          =	self.get_tokenizer()
														lowerCAmelCase__          =	self.get_feature_extractor()

														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=lowerCamelCase_					,    feature_extractor=lowerCamelCase_   )

														processor.save_pretrained(self.tmpdirname   )
														lowerCAmelCase__          =	MusicgenProcessor.from_pretrained(self.tmpdirname   )

														self.assertEqual(processor.tokenizer.get_vocab()					,    tokenizer.get_vocab()   )
														self.assertIsInstance(processor.tokenizer					,    lowerCamelCase_   )

														self.assertEqual(processor.feature_extractor.to_json_string()					,    feature_extractor.to_json_string()   )
														self.assertIsInstance(processor.feature_extractor					,    lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							str:
														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=self.get_tokenizer()					,    feature_extractor=self.get_feature_extractor()   )
														processor.save_pretrained(self.tmpdirname   )

														lowerCAmelCase__          =	self.get_tokenizer(bos_token='''(BOS)'''					,    eos_token='''(EOS)'''   )
														lowerCAmelCase__          =	self.get_feature_extractor(do_normalize=lowerCamelCase_					,    padding_value=1.0   )

														lowerCAmelCase__          =	MusicgenProcessor.from_pretrained(
														    self.tmpdirname					,    bos_token='''(BOS)'''					,    eos_token='''(EOS)'''					,    do_normalize=lowerCamelCase_					,    padding_value=1.0   )

														self.assertEqual(processor.tokenizer.get_vocab()					,    tokenizer_add_kwargs.get_vocab()   )
														self.assertIsInstance(processor.tokenizer					,    lowerCamelCase_   )

														self.assertEqual(processor.feature_extractor.to_json_string()					,    feature_extractor_add_kwargs.to_json_string()   )
														self.assertIsInstance(processor.feature_extractor					,    lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[str]:
														lowerCAmelCase__          =	self.get_feature_extractor()
														lowerCAmelCase__          =	self.get_tokenizer()

														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=lowerCamelCase_					,    feature_extractor=lowerCamelCase_   )

														lowerCAmelCase__          =	floats_list((3, 10_00)   )

														lowerCAmelCase__          =	feature_extractor(lowerCamelCase_					,    return_tensors='''np'''   )
														lowerCAmelCase__          =	processor(lowerCamelCase_					,    return_tensors='''np'''   )

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()					,    input_processor[key].sum()					,    delta=1e-2   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[str]:
														lowerCAmelCase__          =	self.get_feature_extractor()
														lowerCAmelCase__          =	self.get_tokenizer()

														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=lowerCamelCase_					,    feature_extractor=lowerCamelCase_   )

														lowerCAmelCase__          =	'''This is a test string'''

														lowerCAmelCase__          =	processor(text=lowerCamelCase_   )

														lowerCAmelCase__          =	tokenizer(lowerCamelCase_   )

														for key in encoded_tok.keys():
																					self.assertListEqual(encoded_tok[key]					,    encoded_processor[key]   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Tuple:
														lowerCAmelCase__          =	self.get_feature_extractor()
														lowerCAmelCase__          =	self.get_tokenizer()

														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=lowerCamelCase_					,    feature_extractor=lowerCamelCase_   )

														lowerCAmelCase__          =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

														lowerCAmelCase__          =	processor.batch_decode(sequences=lowerCamelCase_   )
														lowerCAmelCase__          =	tokenizer.batch_decode(lowerCamelCase_   )

														self.assertListEqual(lowerCamelCase_					,    lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Optional[int]:
														lowerCAmelCase__          =	self.get_feature_extractor()
														lowerCAmelCase__          =	self.get_tokenizer()

														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=lowerCamelCase_					,    feature_extractor=lowerCamelCase_   )

														self.assertListEqual(
														    processor.model_input_names					,    feature_extractor.model_input_names					,    msg='''`processor` and `feature_extractor` model input names do not match'''					,    )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														lowerCAmelCase__          =	self.get_feature_extractor(padding_side='''left'''   )
														lowerCAmelCase__          =	self.get_tokenizer()

														lowerCAmelCase__          =	MusicgenProcessor(tokenizer=lowerCamelCase_					,    feature_extractor=lowerCamelCase_   )

														lowerCAmelCase__          =	[floats_list((1, x)   )[0] for x in range(5					,    20					,    5   )]
														lowerCAmelCase__          =	processor(lowerCamelCase_   ).padding_mask

														lowerCAmelCase__          =	np.asarray(floats_list((3, 20)   )   )[:, None, :]
														lowerCAmelCase__          =	processor.batch_decode(lowerCamelCase_					,    padding_mask=lowerCamelCase_   )

														self.assertIsInstance(lowerCamelCase_					,    lowerCamelCase_   )

														for audio in decoded_audios:
																					self.assertIsInstance(lowerCamelCase_					,    np.ndarray   )

														self.assertTrue(decoded_audios[0].shape == (1, 10)   )
														self.assertTrue(decoded_audios[1].shape == (1, 15)   )
														self.assertTrue(decoded_audios[2].shape == (1, 20)   )



'''simple docstring'''






import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
		from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import PoolFormerImageProcessor




class 		a__				(							a__  ):





							'''simple docstring'''






							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														lowerCAmelCase__          =	self.config_class(**self.inputs_dict   )
														self.parent.assertTrue(hasattr(lowerCamelCase_					,    '''hidden_sizes'''   )   )
														self.parent.assertTrue(hasattr(lowerCamelCase_					,    '''num_encoder_blocks'''   )   )




class 		a__				:





							'''simple docstring'''






							def __init__(		self					,    lowerCamelCase_					,    lowerCamelCase_=13					,    lowerCamelCase_=64					,    lowerCamelCase_=3					,    lowerCamelCase_=4					,    lowerCamelCase_=[2, 2, 2, 2]					,    lowerCamelCase_=[8, 4, 2, 1]					,    lowerCamelCase_=[16, 32, 64, 1_28]					,    lowerCamelCase_=[1, 4, 8, 16]					,    lowerCamelCase_=False					,    lowerCamelCase_=True					,    lowerCamelCase_="gelu"					,    lowerCamelCase_=0.1					,    lowerCamelCase_=0.02					,    lowerCamelCase_=3					,    lowerCamelCase_=None					,    )       ->							Any:
														lowerCAmelCase__          =	parent
														lowerCAmelCase__          =	batch_size
														lowerCAmelCase__          =	image_size
														lowerCAmelCase__          =	num_channels
														lowerCAmelCase__          =	num_encoder_blocks
														lowerCAmelCase__          =	sr_ratios
														lowerCAmelCase__          =	depths
														lowerCAmelCase__          =	hidden_sizes
														lowerCAmelCase__          =	downsampling_rates
														lowerCAmelCase__          =	is_training
														lowerCAmelCase__          =	use_labels
														lowerCAmelCase__          =	hidden_act
														lowerCAmelCase__          =	hidden_dropout_prob
														lowerCAmelCase__          =	initializer_range
														lowerCAmelCase__          =	num_labels
														lowerCAmelCase__          =	scope







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Optional[int]:
														lowerCAmelCase__          =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

														lowerCAmelCase__          =	PoolFormerConfig(
														    image_size=self.image_size					,    num_channels=self.num_channels					,    num_encoder_blocks=self.num_encoder_blocks					,    depths=self.depths					,    hidden_sizes=self.hidden_sizes					,    hidden_act=self.hidden_act					,    hidden_dropout_prob=self.hidden_dropout_prob					,    initializer_range=self.initializer_range					,    )

														return config, pixel_values, labels







							def   __SCREAMING_SNAKE_CASE							(		self					,    lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )       ->							Any:
														lowerCAmelCase__          =	PoolFormerModel(config=lowerCamelCase_   )
														model.to(lowerCamelCase_   )
														model.eval()
														lowerCAmelCase__          =	model(lowerCamelCase_   )
														lowerCAmelCase__          =	lowerCAmelCase__          =	self.image_size // 32.0
														self.parent.assertEqual(
														    result.last_hidden_state.shape					,    (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							str:
														lowerCAmelCase__          =	self.prepare_config_and_inputs()
														lowerCAmelCase__   ,					lowerCAmelCase__   ,					lowerCAmelCase__          =	config_and_inputs
														lowerCAmelCase__          =	{'''pixel_values''': pixel_values}
														return config, inputs_dict




@require_torch
class 		a__				(							a__				,				a__				,				unittest.TestCase  ):





							'''simple docstring'''






							lowercase__			:   Dict							=      (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
							lowercase__			:   Any							=      (
							    {"feature-extraction": PoolFormerModel, "image-classification": PoolFormerForImageClassification}
							    if is_torch_available()
							    else {}
							)

							lowercase__			:   Optional[int]							=      False
							lowercase__			:   Optional[int]							=      False
							lowercase__			:   Union[str, Any]							=      False
							lowercase__			:   List[Any]							=      False
							lowercase__			:   Tuple							=      False







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Union[str, Any]:
														lowerCAmelCase__          =	PoolFormerModelTester(self   )
														lowerCAmelCase__          =	PoolFormerConfigTester(self					,    config_class=lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[Any]:
														self.config_tester.run_common_tests()







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							List[Any]:
														lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_   )







							@unittest.skip('''PoolFormer does not use inputs_embeds'''   )
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							str:
														pass







							@unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''   )
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Tuple:
														pass







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														def check_hidden_states_output(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   ):
																					lowerCAmelCase__          =	model_class(lowerCamelCase_   )
																					model.to(lowerCamelCase_   )
																					model.eval()

																					with torch.no_grad():
																												lowerCAmelCase__          =	model(**self._prepare_for_class(lowerCamelCase_					,    lowerCamelCase_   )   )

																					lowerCAmelCase__          =	outputs.hidden_states

																					lowerCAmelCase__          =	self.model_tester.num_encoder_blocks
																					self.assertEqual(len(lowerCamelCase_   )					,    lowerCamelCase_   )

																					# verify the first hidden states (first block)
																					self.assertListEqual(
																					    list(hidden_states[0].shape[-3:]   )					,    [
																					        self.model_tester.hidden_sizes[0],
																					        self.model_tester.image_size // 4,
																					        self.model_tester.image_size // 4,
																					    ]					,    )

														lowerCAmelCase__   ,					lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs_for_common()

														for model_class in self.all_model_classes:
																					lowerCAmelCase__          =	True
																					check_hidden_states_output(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )

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

																					check_hidden_states_output(lowerCamelCase_					,    lowerCamelCase_					,    lowerCamelCase_   )







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														if not self.model_tester.is_training:
																					return

														lowerCAmelCase__   ,					lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs_for_common()
														lowerCAmelCase__          =	True

														for model_class in self.all_model_classes:
																					if model_class in get_values(lowerCamelCase_   ):
																												continue
																					lowerCAmelCase__          =	model_class(lowerCamelCase_   )
																					model.to(lowerCamelCase_   )
																					model.train()
																					lowerCAmelCase__          =	self._prepare_for_class(lowerCamelCase_					,    lowerCamelCase_					,    return_labels=lowerCamelCase_   )
																					lowerCAmelCase__          =	model(**lowerCamelCase_   ).loss
																					loss.backward()







							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														lowerCAmelCase__   ,					lowerCAmelCase__          =	self.model_tester.prepare_config_and_inputs_for_common()

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

																					lowerCAmelCase__          =	['''pixel_values''']
																					self.assertListEqual(arg_names[:1]					,    lowerCamelCase_   )







							@slow
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							int:
														for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					lowerCAmelCase__          =	PoolFormerModel.from_pretrained(lowerCamelCase_   )
																					self.assertIsNotNone(lowerCamelCase_   )


def 					_snake_case   (     )						->    Optional[int]:
							lowerCAmelCase__          =	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
							return image






@require_torch
class 		a__				(							unittest.TestCase  ):





							'''simple docstring'''






							@slow
							def   __SCREAMING_SNAKE_CASE							(		self   )       ->							Dict:
														lowerCAmelCase__          =	PoolFormerImageProcessor()
														lowerCAmelCase__          =	PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''   ).to(lowerCamelCase_   )

														lowerCAmelCase__          =	image_processor(images=prepare_img()					,    return_tensors='''pt'''   ).to(lowerCamelCase_   )

														# forward pass
														with torch.no_grad():
																					lowerCAmelCase__          =	model(**lowerCamelCase_   )

														# verify the logits
														lowerCAmelCase__          =	torch.Size((1, 10_00)   )
														self.assertEqual(outputs.logits.shape					,    lowerCamelCase_   )

														lowerCAmelCase__          =	torch.tensor([-0.6_113, 0.1_685, -0.0_492]   ).to(lowerCamelCase_   )
														self.assertTrue(torch.allclose(outputs.logits[0, :3]					,    lowerCamelCase_					,    atol=1e-4   )   )