code: 
  
  
  
  
  """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
  
  
  __A 		=			random.Random()
  
  
  
  def   UpperCamelCase__     (		lowercase__  :     Tuple       ,     lowercase__  :     Tuple=1.0       ,     lowercase__  :     List[Any]=None       ,     lowercase__  :     List[str]=None   ):
  		if rng is None:
  				snake_case		:						Optional[int]								=				global_rng
  
  		snake_case		:						Any								=				[]
  		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 			lowerCamelCase__    (  unittest.TestCase			):
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Union[str, Any]								=				"facebook/musicgen-small"
  					snake_case		:						Union[str, Any]								=				tempfile.mkdtemp()
  
  
  			def 							lowerCamelCase_				(     self				, **SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return TaTokenizer.from_pretrained(self.checkpoint				, **SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self				, **SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					return EncodecFeatureExtractor.from_pretrained(self.checkpoint				, **SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					shutil.rmtree(self.tmpdirname							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						str								=				self.get_tokenizer()
  					snake_case		:						str								=				self.get_feature_extractor()
  
  					snake_case		:						List[Any]								=				MusicgenProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
  
  					processor.save_pretrained(self.tmpdirname							)
  					snake_case		:						str								=				MusicgenProcessor.from_pretrained(self.tmpdirname							)
  
  					self.assertEqual(processor.tokenizer.get_vocab()				, tokenizer.get_vocab()							)
  					self.assertIsInstance(processor.tokenizer				, SCREAMING_SNAKE_CASE							)
  
  					self.assertEqual(processor.feature_extractor.to_json_string()				, feature_extractor.to_json_string()							)
  					self.assertIsInstance(processor.feature_extractor				, SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Union[str, Any]								=				MusicgenProcessor(tokenizer=self.get_tokenizer()				, feature_extractor=self.get_feature_extractor()							)
  					processor.save_pretrained(self.tmpdirname							)
  
  					snake_case		:						Union[str, Any]								=				self.get_tokenizer(bos_token="(BOS)"				, eos_token="(EOS)"							)
  					snake_case		:						str								=				self.get_feature_extractor(do_normalize=SCREAMING_SNAKE_CASE				, padding_value=1.0							)
  
  					snake_case		:						str								=				MusicgenProcessor.from_pretrained(
  					    self.tmpdirname				, bos_token="(BOS)"				, eos_token="(EOS)"				, do_normalize=SCREAMING_SNAKE_CASE				, padding_value=1.0							)
  
  					self.assertEqual(processor.tokenizer.get_vocab()				, tokenizer_add_kwargs.get_vocab()							)
  					self.assertIsInstance(processor.tokenizer				, SCREAMING_SNAKE_CASE							)
  
  					self.assertEqual(processor.feature_extractor.to_json_string()				, feature_extractor_add_kwargs.to_json_string()							)
  					self.assertIsInstance(processor.feature_extractor				, SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Union[str, Any]								=				self.get_feature_extractor()
  					snake_case		:						Any								=				self.get_tokenizer()
  
  					snake_case		:						Dict								=				MusicgenProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Optional[Any]								=				floats_list((3, 1_000)							)
  
  					snake_case		:						Optional[Any]								=				feature_extractor(SCREAMING_SNAKE_CASE				, return_tensors="np"							)
  					snake_case		:						List[str]								=				processor(SCREAMING_SNAKE_CASE				, 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 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Tuple								=				self.get_feature_extractor()
  					snake_case		:						int								=				self.get_tokenizer()
  
  					snake_case		:						List[Any]								=				MusicgenProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Dict								=				"This is a test string"
  
  					snake_case		:						Any								=				processor(text=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Tuple								=				tokenizer(SCREAMING_SNAKE_CASE							)
  
  					for key in encoded_tok.keys():
  							self.assertListEqual(encoded_tok[key]				, encoded_processor[key]							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[int]								=				self.get_feature_extractor()
  					snake_case		:						int								=				self.get_tokenizer()
  
  					snake_case		:						Any								=				MusicgenProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						List[Any]								=				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]
  
  					snake_case		:						List[Any]								=				processor.batch_decode(sequences=SCREAMING_SNAKE_CASE							)
  					snake_case		:						Optional[int]								=				tokenizer.batch_decode(SCREAMING_SNAKE_CASE							)
  
  					self.assertListEqual(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Union[str, Any]								=				self.get_feature_extractor()
  					snake_case		:						Any								=				self.get_tokenizer()
  
  					snake_case		:						Tuple								=				MusicgenProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
  
  					self.assertListEqual(
  					    processor.model_input_names				, feature_extractor.model_input_names				, msg="`processor` and `feature_extractor` model input names do not match"				, )
  
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Any								=				self.get_feature_extractor(padding_side="left"							)
  					snake_case		:						Union[str, Any]								=				self.get_tokenizer()
  
  					snake_case		:						List[str]								=				MusicgenProcessor(tokenizer=SCREAMING_SNAKE_CASE				, feature_extractor=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						List[str]								=				[floats_list((1, x)							)[0] for x in range(5				, 20				, 5							)]
  					snake_case		:						str								=				processor(SCREAMING_SNAKE_CASE							).padding_mask
  
  					snake_case		:						Union[str, Any]								=				np.asarray(floats_list((3, 20)							)							)[:, None, :]
  					snake_case		:						Optional[Any]								=				processor.batch_decode(SCREAMING_SNAKE_CASE				, padding_mask=SCREAMING_SNAKE_CASE							)
  
  					self.assertIsInstance(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							)
  
  					for audio in decoded_audios:
  							self.assertIsInstance(SCREAMING_SNAKE_CASE				, 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)							)
  
  
style_context: 
  
  
  
  
  """simple docstring"""
  
  import unittest
  
  import numpy as np
  import PIL
  import torch
  
  from diffusers.image_processor import VaeImageProcessor
  class 			lowerCamelCase__    (  unittest.TestCase			):
  
  
  			@property
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						int								=				1
  					snake_case		:						Dict								=				3
  					snake_case		:						int								=				8
  					snake_case		:						int								=				8
  
  					snake_case		:						List[str]								=				torch.rand((batch_size, num_channels, height, width)							)
  
  					return sample
  
  
  			def 							lowerCamelCase_				(     self				, SCREAMING_SNAKE_CASE							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					if isinstance(image[0]				, PIL.Image.Image							):
  							return np.stack([np.array(SCREAMING_SNAKE_CASE							) for i in image]				, axis=0							)
  					elif isinstance(SCREAMING_SNAKE_CASE				, torch.Tensor							):
  							return image.cpu().numpy().transpose(0				, 2				, 3				, 1							)
  					return image
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[int]								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Any								=				self.dummy_sample
  					snake_case		:						Optional[Any]								=				self.to_np(SCREAMING_SNAKE_CASE							)
  
  					for output_type in ["pt", "np", "pil"]:
  							snake_case		:						Optional[Any]								=				image_processor.postprocess(
  							    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type=SCREAMING_SNAKE_CASE				, )
  							snake_case		:						Optional[Any]								=				self.to_np(SCREAMING_SNAKE_CASE							)
  							snake_case		:						Any								=				(input_np * 255).round() if output_type == "pil" else input_np
  							assert (
  							    np.abs(in_np - out_np							).max() < 1E-6
  							), F'''decoded output does not match input for output_type {output_type}'''
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Dict								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)
  					snake_case		:						Tuple								=				self.dummy_sample.cpu().numpy().transpose(0				, 2				, 3				, 1							)
  
  					for output_type in ["pt", "np", "pil"]:
  							snake_case		:						Dict								=				image_processor.postprocess(image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type=SCREAMING_SNAKE_CASE							)
  
  							snake_case		:						Union[str, Any]								=				self.to_np(SCREAMING_SNAKE_CASE							)
  							snake_case		:						Tuple								=				(input_np * 255).round() if output_type == "pil" else input_np
  							assert (
  							    np.abs(in_np - out_np							).max() < 1E-6
  							), F'''decoded output does not match input for output_type {output_type}'''
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[Any]								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						str								=				self.dummy_sample.cpu().numpy().transpose(0				, 2				, 3				, 1							)
  					snake_case		:						int								=				image_processor.numpy_to_pil(SCREAMING_SNAKE_CASE							)
  
  					for output_type in ["pt", "np", "pil"]:
  							snake_case		:						Dict								=				image_processor.postprocess(image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type=SCREAMING_SNAKE_CASE							)
  							for i, o in zip(SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE							):
  									snake_case		:						Dict								=				np.array(SCREAMING_SNAKE_CASE							)
  									snake_case		:						Tuple								=				self.to_np(SCREAMING_SNAKE_CASE							) if output_type == "pil" else (self.to_np(SCREAMING_SNAKE_CASE							) * 255).round()
  									assert (
  									    np.abs(in_np - out_np							).max() < 1E-6
  									), F'''decoded output does not match input for output_type {output_type}'''
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Tuple								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						List[str]								=				self.dummy_sample
  					snake_case		:						Optional[int]								=				input_pt_ad.squeeze(0							)
  
  					snake_case		:						Any								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  					snake_case		:						Union[str, Any]								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  
  					snake_case		:						Union[str, Any]								=				self.to_np(self.dummy_sample							)
  					snake_case		:						Tuple								=				input_np_ad.squeeze(0							)
  
  					snake_case		:						str								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  					snake_case		:						Union[str, Any]								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  
  					assert np.abs(out_pt_ad - out_pt_ad							).max() < 1E-6
  					assert np.abs(out_np_ad - out_np_ad							).max() < 1E-6
  
  
  
  			def 							lowerCamelCase_				(     self							):
  
  
  
  					"""simple docstring"""
  
  
  
  
  
  
  
  					snake_case		:						Optional[Any]								=				VaeImageProcessor(do_resize=SCREAMING_SNAKE_CASE				, do_normalize=SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						Union[str, Any]								=				self.dummy_sample
  					snake_case		:						List[str]								=				list(SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						str								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  
  					snake_case		:						Any								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  
  					snake_case		:						Dict								=				self.to_np(self.dummy_sample							)
  					list(SCREAMING_SNAKE_CASE							)
  
  					snake_case		:						str								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  
  					snake_case		:						Union[str, Any]								=				image_processor.postprocess(
  					    image_processor.preprocess(SCREAMING_SNAKE_CASE							)				, output_type="np"				, )
  
  					assert np.abs(out_pt_ad - out_pt_list							).max() < 1E-6
  					assert np.abs(out_np_ad - out_np_list							).max() < 1E-6
  
  