
import inspect
import unittest

import requests

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

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import MgpstrForSceneTextRecognition


if is_vision_available():
    from PIL import Image

    from transformers import MgpstrProcessor




class 	UpperCAmelCase_      :


  '''simple docstring'''







  def __init__(			self      :			List[str]      ,							UpperCamelCase__      :			Any      ,							UpperCamelCase__      :			Union[str, Any]=False      ,							UpperCamelCase__      :			List[str]=13      ,							UpperCamelCase__      :			Optional[int]=(32, 128)      ,							UpperCamelCase__      :			Optional[int]=4      ,							UpperCamelCase__      :			Tuple=3      ,							UpperCamelCase__      :			List[str]=27      ,							UpperCamelCase__      :			Union[str, Any]=38      ,							UpperCamelCase__      :			Optional[int]=99      ,							UpperCamelCase__      :			Dict=99      ,							UpperCamelCase__      :			Union[str, Any]=32      ,							UpperCamelCase__      :			Any=5      ,							UpperCamelCase__      :			List[str]=4      ,							UpperCamelCase__      :			Any=4.0      ,							UpperCamelCase__      :			Optional[Any]=257      ,							UpperCamelCase__      :			str=None      ,							)      ->     str:







    """simple docstring"""






    __magic_name__    					=	parent
    __magic_name__    					=	is_training
    __magic_name__    					=	batch_size
    __magic_name__    					=	image_size
    __magic_name__    					=	patch_size
    __magic_name__    					=	num_channels
    __magic_name__    					=	max_token_length
    __magic_name__    					=	num_character_labels
    __magic_name__    					=	num_bpe_labels
    __magic_name__    					=	num_wordpiece_labels
    __magic_name__    					=	hidden_size
    __magic_name__    					=	num_hidden_layers
    __magic_name__    					=	num_attention_heads
    __magic_name__    					=	mlp_ratio
    __magic_name__    					=	patch_embeds_hidden_size
    __magic_name__    					=	output_hidden_states
  def 							_lowercase							(			self      :			Any   )      ->     Optional[Any]:







    """simple docstring"""






    __magic_name__    					=	floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]   )
    __magic_name__    					=	self.get_config()
    return config, pixel_values
  def 							_lowercase							(			self      :			Optional[Any]   )      ->     Dict:







    """simple docstring"""






    return MgpstrConfig(
        image_size=self.image_size      ,							patch_size=self.patch_size      ,							num_channels=self.num_channels      ,							max_token_length=self.max_token_length      ,							num_character_labels=self.num_character_labels      ,							num_bpe_labels=self.num_bpe_labels      ,							num_wordpiece_labels=self.num_wordpiece_labels      ,							hidden_size=self.hidden_size      ,							num_hidden_layers=self.num_hidden_layers      ,							num_attention_heads=self.num_attention_heads      ,							mlp_ratio=self.mlp_ratio      ,							output_hidden_states=self.output_hidden_states      ,							)
  def 							_lowercase							(			self      :			str      ,							UpperCamelCase__      :			int      ,							UpperCamelCase__      :			Optional[int]   )      ->     List[Any]:







    """simple docstring"""






    __magic_name__    					=	MgpstrForSceneTextRecognition(UpperCamelCase__   )
    model.to(UpperCamelCase__   )
    model.eval()
    with torch.no_grad():
      __magic_name__    					=	model(UpperCamelCase__   )
    self.parent.assertEqual(
        generated_ids[0][0].shape      ,							(self.batch_size, self.max_token_length, self.num_character_labels)   )





  def 							_lowercase							(			self      :			str   )      ->     Any:







    """simple docstring"""






    __magic_name__    					=	self.prepare_config_and_inputs()
    __magic_name__					,   __magic_name__    					=	config_and_inputs
    __magic_name__    					=	{"""pixel_values""": pixel_values}
    return config, inputs_dict







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


  '''simple docstring'''







  a__   	=		(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
  a__   	=		{"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
  a__   	=		False

  a__   	=		False
  a__   	=		False
  a__   	=		False
  a__   	=		False
  def 							_lowercase							(			self      :			str   )      ->     str:







    """simple docstring"""






    __magic_name__    					=	MgpstrModelTester(self   )
    __magic_name__    					=	ConfigTester(self      ,							config_class=UpperCamelCase__      ,							has_text_modality=UpperCamelCase__   )
  def 							_lowercase							(			self      :			Optional[int]   )      ->     str:







    """simple docstring"""






    self.config_tester.run_common_tests()
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     str:







    """simple docstring"""






    __magic_name__    					=	self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*UpperCamelCase__   )
  @unittest.skip(reason="""MgpstrModel does not use inputs_embeds"""   )
  def 							_lowercase							(			self      :			Optional[Any]   )      ->     int:







    """simple docstring"""






    pass
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     List[str]:







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __magic_name__    					=	model_class(UpperCamelCase__   )
      self.assertIsInstance(model.get_input_embeddings()      ,							(nn.Module)   )
      __magic_name__    					=	model.get_output_embeddings()
      self.assertTrue(x is None or isinstance(UpperCamelCase__      ,							nn.Linear   )   )
  def 							_lowercase							(			self      :			Tuple   )      ->     int:







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs_for_common()

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

      __magic_name__    					=	["""pixel_values"""]
      self.assertListEqual(arg_names[:1]      ,							UpperCamelCase__   )
  @unittest.skip(reason="""MgpstrModel does not support feedforward chunking"""   )
  def 							_lowercase							(			self      :			str   )      ->     Any:







    """simple docstring"""






    pass
  def 							_lowercase							(			self      :			Tuple   )      ->     Dict:







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      if not model_class.supports_gradient_checkpointing:
        continue

      __magic_name__    					=	True
      __magic_name__    					=	model_class(UpperCamelCase__   )
      self.assertTrue(model.is_gradient_checkpointing   )
  def 							_lowercase							(			self      :			Optional[int]   )      ->     Union[str, Any]:







    """simple docstring"""






    def check_hidden_states_output(UpperCamelCase__      :			Union[str, Any]      ,							UpperCamelCase__      :			List[Any]      ,							UpperCamelCase__      :			Union[str, Any]   ):
      __magic_name__    					=	model_class(UpperCamelCase__   )
      model.to(UpperCamelCase__   )
      model.eval()

      with torch.no_grad():
        __magic_name__    					=	model(**self._prepare_for_class(UpperCamelCase__      ,							UpperCamelCase__   )   )

      __magic_name__    					=	outputs.hidden_states

      __magic_name__    					=	getattr(
          self.model_tester      ,							"""expected_num_hidden_layers"""      ,							self.model_tester.num_hidden_layers + 1   )
      self.assertEqual(len(UpperCamelCase__   )      ,							UpperCamelCase__   )

      self.assertListEqual(
          list(hidden_states[0].shape[-2:]   )      ,							[self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]      ,							)

    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __magic_name__    					=	True
      check_hidden_states_output(UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )

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

      check_hidden_states_output(UpperCamelCase__      ,							UpperCamelCase__      ,							UpperCamelCase__   )
  def 							_lowercase							(			self      :			Dict   )      ->     Any:







    """simple docstring"""






    __magic_name__					,   __magic_name__    					=	self.model_tester.prepare_config_and_inputs_for_common()

    __magic_name__    					=	_config_zero_init(UpperCamelCase__   )
    for model_class in self.all_model_classes:
      __magic_name__    					=	model_class(config=UpperCamelCase__   )
      for name, param in model.named_parameters():
        if isinstance(UpperCamelCase__      ,							(nn.Linear, nn.Convad, nn.LayerNorm)   ):
          if param.requires_grad:
            self.assertIn(
                ((param.data.mean() * 1E9).round() / 1E9).item()      ,							[0.0, 1.0]      ,							msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''      ,							)





  @unittest.skip(reason="""Retain_grad is tested in individual model tests"""   )
  def 							_lowercase							(			self      :			List[str]   )      ->     List[Any]:







    """simple docstring"""






    pass







def a__   (	):






  '''simple docstring'''





  __magic_name__    					=	"""https://i.postimg.cc/ZKwLg2Gw/367-14.png"""
  __magic_name__    					=	Image.open(requests.get(A_,					stream=A_ ).raw ).convert("""RGB""" )
  return im





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


  '''simple docstring'''







  @slow
  def 							_lowercase							(			self      :			Tuple   )      ->     List[str]:







    """simple docstring"""






    __magic_name__    					=	"""alibaba-damo/mgp-str-base"""
    __magic_name__    					=	MgpstrForSceneTextRecognition.from_pretrained(UpperCamelCase__   ).to(UpperCamelCase__   )
    __magic_name__    					=	MgpstrProcessor.from_pretrained(UpperCamelCase__   )

    __magic_name__    					=	prepare_img()
    __magic_name__    					=	processor(images=UpperCamelCase__      ,							return_tensors="""pt"""   ).pixel_values.to(UpperCamelCase__   )

    # forward pass
    with torch.no_grad():
      __magic_name__    					=	model(UpperCamelCase__   )

    # verify the logits
    self.assertEqual(outputs.logits[0].shape      ,							torch.Size((1, 27, 38)   )   )

    __magic_name__    					=	processor.batch_decode(outputs.logits   )
    __magic_name__    					=	"""ticket"""

    self.assertEqual(out_strs["""generated_text"""][0]      ,							UpperCamelCase__   )

    __magic_name__    					=	torch.tensor(
        [[[-39.5397, -44.4024, -36.1844], [-61.4709, -63.8639, -58.3454], [-74.0225, -68.5494, -71.2164]]]      ,							device=UpperCamelCase__      ,							)

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



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


__lowerCAmelCase      :					Dict   	=       random.Random()

def a__   (	A_,					A_=1.0,					A_=None,					A_=None ):






  '''simple docstring'''





  if rng is None:
    __magic_name__    					=	global_rng

  __magic_name__    					=	[]
  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 	UpperCAmelCase_      (			unittest.TestCase					):


  '''simple docstring'''







  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     List[str]:







    """simple docstring"""






    __magic_name__    					=	"""facebook/musicgen-small"""
    __magic_name__    					=	tempfile.mkdtemp()
  def 							_lowercase							(			self      :			Tuple      ,							**UpperCamelCase__      :			str   )      ->     Any:







    """simple docstring"""






    return TaTokenizer.from_pretrained(self.checkpoint      ,							**UpperCamelCase__   )
  def 							_lowercase							(			self      :			Optional[int]      ,							**UpperCamelCase__      :			List[str]   )      ->     List[str]:







    """simple docstring"""






    return EncodecFeatureExtractor.from_pretrained(self.checkpoint      ,							**UpperCamelCase__   )
  def 							_lowercase							(			self      :			Union[str, Any]   )      ->     List[Any]:







    """simple docstring"""






    shutil.rmtree(self.tmpdirname   )
  def 							_lowercase							(			self      :			Optional[Any]   )      ->     Optional[int]:







    """simple docstring"""






    __magic_name__    					=	self.get_tokenizer()
    __magic_name__    					=	self.get_feature_extractor()

    __magic_name__    					=	MusicgenProcessor(tokenizer=UpperCamelCase__      ,							feature_extractor=UpperCamelCase__   )

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

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

    self.assertEqual(processor.feature_extractor.to_json_string()      ,							feature_extractor.to_json_string()   )
    self.assertIsInstance(processor.feature_extractor      ,							UpperCamelCase__   )
  def 							_lowercase							(			self      :			Optional[Any]   )      ->     List[str]:







    """simple docstring"""






    __magic_name__    					=	MusicgenProcessor(tokenizer=self.get_tokenizer()      ,							feature_extractor=self.get_feature_extractor()   )
    processor.save_pretrained(self.tmpdirname   )

    __magic_name__    					=	self.get_tokenizer(bos_token="""(BOS)"""      ,							eos_token="""(EOS)"""   )
    __magic_name__    					=	self.get_feature_extractor(do_normalize=UpperCamelCase__      ,							padding_value=1.0   )

    __magic_name__    					=	MusicgenProcessor.from_pretrained(
        self.tmpdirname      ,							bos_token="""(BOS)"""      ,							eos_token="""(EOS)"""      ,							do_normalize=UpperCamelCase__      ,							padding_value=1.0   )

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

    self.assertEqual(processor.feature_extractor.to_json_string()      ,							feature_extractor_add_kwargs.to_json_string()   )
    self.assertIsInstance(processor.feature_extractor      ,							UpperCamelCase__   )
  def 							_lowercase							(			self      :			Dict   )      ->     Optional[Any]:







    """simple docstring"""






    __magic_name__    					=	self.get_feature_extractor()
    __magic_name__    					=	self.get_tokenizer()

    __magic_name__    					=	MusicgenProcessor(tokenizer=UpperCamelCase__      ,							feature_extractor=UpperCamelCase__   )

    __magic_name__    					=	floats_list((3, 1000)   )

    __magic_name__    					=	feature_extractor(UpperCamelCase__      ,							return_tensors="""np"""   )
    __magic_name__    					=	processor(UpperCamelCase__      ,							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 							_lowercase							(			self      :			Optional[Any]   )      ->     str:







    """simple docstring"""






    __magic_name__    					=	self.get_feature_extractor()
    __magic_name__    					=	self.get_tokenizer()

    __magic_name__    					=	MusicgenProcessor(tokenizer=UpperCamelCase__      ,							feature_extractor=UpperCamelCase__   )

    __magic_name__    					=	"""This is a test string"""

    __magic_name__    					=	processor(text=UpperCamelCase__   )

    __magic_name__    					=	tokenizer(UpperCamelCase__   )

    for key in encoded_tok.keys():
      self.assertListEqual(encoded_tok[key]      ,							encoded_processor[key]   )
  def 							_lowercase							(			self      :			Dict   )      ->     int:







    """simple docstring"""






    __magic_name__    					=	self.get_feature_extractor()
    __magic_name__    					=	self.get_tokenizer()

    __magic_name__    					=	MusicgenProcessor(tokenizer=UpperCamelCase__      ,							feature_extractor=UpperCamelCase__   )

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

    __magic_name__    					=	processor.batch_decode(sequences=UpperCamelCase__   )
    __magic_name__    					=	tokenizer.batch_decode(UpperCamelCase__   )

    self.assertListEqual(UpperCamelCase__      ,							UpperCamelCase__   )
  def 							_lowercase							(			self      :			Tuple   )      ->     int:







    """simple docstring"""






    __magic_name__    					=	self.get_feature_extractor()
    __magic_name__    					=	self.get_tokenizer()

    __magic_name__    					=	MusicgenProcessor(tokenizer=UpperCamelCase__      ,							feature_extractor=UpperCamelCase__   )

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





  def 							_lowercase							(			self      :			List[str]   )      ->     int:







    """simple docstring"""






    __magic_name__    					=	self.get_feature_extractor(padding_side="""left"""   )
    __magic_name__    					=	self.get_tokenizer()

    __magic_name__    					=	MusicgenProcessor(tokenizer=UpperCamelCase__      ,							feature_extractor=UpperCamelCase__   )

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

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

    self.assertIsInstance(UpperCamelCase__      ,							UpperCamelCase__   )

    for audio in decoded_audios:
      self.assertIsInstance(UpperCamelCase__      ,							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)   )

