





'''simple docstring'''





import collections
import inspect
import unittest

from transformers import DonutSwinConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

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


if is_torch_available():
      import torch
      from torch import nn

      from transformers import DonutSwinModel
      from transformers.models.donut.modeling_donut_swin import DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST





class UpperCAmelCase_							:


  def __init__(       self   ,__snake_case   ,__snake_case=1_3   ,__snake_case=3_2   ,__snake_case=2   ,__snake_case=3   ,__snake_case=1_6   ,__snake_case=[1, 2, 1]   ,__snake_case=[2, 2, 4]   ,__snake_case=2   ,__snake_case=2.0   ,__snake_case=True   ,__snake_case=0.0   ,__snake_case=0.0   ,__snake_case=0.1   ,__snake_case="gelu"   ,__snake_case=False   ,__snake_case=True   ,__snake_case=0.02   ,__snake_case=1E-5   ,__snake_case=True   ,__snake_case=None   ,__snake_case=True   ,__snake_case=1_0   ,__snake_case=8   ,):



       """simple docstring"""





       A_					      =					parent
       A_					      =					batch_size
       A_					      =					image_size
       A_					      =					patch_size
       A_					      =					num_channels
       A_					      =					embed_dim
       A_					      =					depths
       A_					      =					num_heads
       A_					      =					window_size
       A_					      =					mlp_ratio
       A_					      =					qkv_bias
       A_					      =					hidden_dropout_prob
       A_					      =					attention_probs_dropout_prob
       A_					      =					drop_path_rate
       A_					      =					hidden_act
       A_					      =					use_absolute_embeddings
       A_					      =					patch_norm
       A_					      =					layer_norm_eps
       A_					      =					initializer_range
       A_					      =					is_training
       A_					      =					scope
       A_					      =					use_labels
       A_					      =					type_sequence_label_size
       A_					      =					encoder_stride


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

       A_					      =					None
       if self.use_labels:
            A_					      =					ids_tensor([self.batch_size]   ,self.type_sequence_label_size   )

       A_					      =					self.get_config()

       return config, pixel_values, labels


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       return DonutSwinConfig(
           image_size=self.image_size   ,patch_size=self.patch_size   ,num_channels=self.num_channels   ,embed_dim=self.embed_dim   ,depths=self.depths   ,num_heads=self.num_heads   ,window_size=self.window_size   ,mlp_ratio=self.mlp_ratio   ,qkv_bias=self.qkv_bias   ,hidden_dropout_prob=self.hidden_dropout_prob   ,attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,drop_path_rate=self.drop_path_rate   ,hidden_act=self.hidden_act   ,use_absolute_embeddings=self.use_absolute_embeddings   ,path_norm=self.patch_norm   ,layer_norm_eps=self.layer_norm_eps   ,initializer_range=self.initializer_range   ,encoder_stride=self.encoder_stride   ,)


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ):



       """simple docstring"""





       A_					      =					DonutSwinModel(config=__snake_case   )
       model.to(__snake_case   )
       model.eval()
       A_					      =					model(__snake_case   )

       A_					      =					((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths   ) - 1))
       A_					      =					int(config.embed_dim * 2 ** (len(config.depths   ) - 1)   )

       self.parent.assertEqual(result.last_hidden_state.shape   ,(self.batch_size, expected_seq_len, expected_dim)   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.prepare_config_and_inputs()
       (
           (
           A_						
       )      ,						(
           A_						
       )      ,						(
           A_						
       )      ,												
       )					      =					config_and_inputs
       A_					      =					{'''pixel_values''': pixel_values}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_							(   _SCREAMING_SNAKE_CASE    ,		_SCREAMING_SNAKE_CASE    ,		unittest.TestCase   ):
  __lowerCAmelCase      :	List[str]  	=      (DonutSwinModel,) if is_torch_available() else ()
  __lowerCAmelCase      :	Optional[int]  	=      {"""feature-extraction""": DonutSwinModel} if is_torch_available() else {}
  __lowerCAmelCase      :	str  	=      True

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


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					DonutSwinModelTester(self   )
       A_					      =					ConfigTester(self   ,config_class=__snake_case   ,embed_dim=3_7   )


  def 	__UpperCAmelCase    (       self   ):



       """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 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       return


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       pass


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_      ,						A_					      =					self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            A_					      =					model_class(__snake_case   )
            self.assertIsInstance(model.get_input_embeddings()   ,(nn.Module)   )
            A_					      =					model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(__snake_case   ,nn.Linear   )   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_      ,						A_					      =					self.model_tester.prepare_config_and_inputs_for_common()

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

            A_					      =					['''pixel_values''']
            self.assertListEqual(arg_names[:1]   ,__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





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

       for model_class in self.all_model_classes:
            A_					      =					True
            A_					      =					False
            A_					      =					True
            A_					      =					model_class(__snake_case   )
            model.to(__snake_case   )
            model.eval()
            with torch.no_grad():
                 A_					      =					model(**self._prepare_for_class(__snake_case   ,__snake_case   )   )
            A_					      =					outputs.attentions
            A_					      =					len(self.model_tester.depths   )
            self.assertEqual(len(__snake_case   )   ,__snake_case   )

            # check that output_attentions also work using config
            del inputs_dict["output_attentions"]
            A_					      =					True
            A_					      =					config.window_size**2
            A_					      =					model_class(__snake_case   )
            model.to(__snake_case   )
            model.eval()
            with torch.no_grad():
                 A_					      =					model(**self._prepare_for_class(__snake_case   ,__snake_case   )   )
            A_					      =					outputs.attentions
            self.assertEqual(len(__snake_case   )   ,__snake_case   )

            self.assertListEqual(
                list(attentions[0].shape[-3:]   )   ,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]   ,)
            A_					      =					len(__snake_case   )

            # Check attention is always last and order is fine
            A_					      =					True
            A_					      =					True
            A_					      =					model_class(__snake_case   )
            model.to(__snake_case   )
            model.eval()
            with torch.no_grad():
                 A_					      =					model(**self._prepare_for_class(__snake_case   ,__snake_case   )   )

            if hasattr(self.model_tester   ,'''num_hidden_states_types'''   ):
                 A_					      =					self.model_tester.num_hidden_states_types
            else:
                 # also another +1 for reshaped_hidden_states
                 A_					      =					2
            self.assertEqual(out_len + added_hidden_states   ,len(__snake_case   )   )

            A_					      =					outputs.attentions

            self.assertEqual(len(__snake_case   )   ,__snake_case   )

            self.assertListEqual(
                list(self_attentions[0].shape[-3:]   )   ,[self.model_tester.num_heads[0], window_size_squared, window_size_squared]   ,)


  def 	__UpperCAmelCase    (       self   ,__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   ):



       """simple docstring"""





       A_					      =					model_class(__snake_case   )
       model.to(__snake_case   )
       model.eval()

       with torch.no_grad():
            A_					      =					model(**self._prepare_for_class(__snake_case   ,__snake_case   )   )

       A_					      =					outputs.hidden_states

       A_					      =					getattr(
           self.model_tester   ,'''expected_num_hidden_layers'''   ,len(self.model_tester.depths   ) + 1   )
       self.assertEqual(len(__snake_case   )   ,__snake_case   )

       # DonutSwin has a different seq_length
       A_					      =					(
           config.patch_size
           if isinstance(config.patch_size   ,collections.abc.Iterable   )
           else (config.patch_size, config.patch_size)
       )

       A_					      =					(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

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

       A_					      =					outputs.reshaped_hidden_states
       self.assertEqual(len(__snake_case   )   ,__snake_case   )

       A_      ,						A_      ,						A_      ,						A_					      =					reshaped_hidden_states[0].shape
       A_					      =					(
           reshaped_hidden_states[0].view(__snake_case   ,__snake_case   ,height * width   ).permute(0   ,2   ,1   )
       )
       self.assertListEqual(
           list(reshaped_hidden_states.shape[-2:]   )   ,[num_patches, self.model_tester.embed_dim]   ,)


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_      ,						A_					      =					self.model_tester.prepare_config_and_inputs_for_common()

       A_					      =					(
           self.model_tester.image_size
           if isinstance(self.model_tester.image_size   ,collections.abc.Iterable   )
           else (self.model_tester.image_size, self.model_tester.image_size)
       )

       for model_class in self.all_model_classes:
            A_					      =					True
            self.check_hidden_states_output(__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   )

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

            self.check_hidden_states_output(__snake_case   ,__snake_case   ,__snake_case   ,__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_      ,						A_					      =					self.model_tester.prepare_config_and_inputs_for_common()
       A_					      =					3

       A_					      =					(
           self.model_tester.image_size
           if isinstance(self.model_tester.image_size   ,collections.abc.Iterable   )
           else (self.model_tester.image_size, self.model_tester.image_size)
       )
       A_					      =					(
           config.patch_size
           if isinstance(config.patch_size   ,collections.abc.Iterable   )
           else (config.patch_size, config.patch_size)
       )

       A_					      =					image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
       A_					      =					image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

       for model_class in self.all_model_classes:
            A_					      =					True
            self.check_hidden_states_output(__snake_case   ,__snake_case   ,__snake_case   ,(padded_height, padded_width)   )

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            A_					      =					True
            self.check_hidden_states_output(__snake_case   ,__snake_case   ,__snake_case   ,(padded_height, padded_width)   )


  @slow
  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            A_					      =					DonutSwinModel.from_pretrained(__snake_case   )
            self.assertIsNotNone(__snake_case   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_      ,						A_					      =					self.model_tester.prepare_config_and_inputs_for_common()

       A_					      =					_config_zero_init(__snake_case   )
       for model_class in self.all_model_classes:
            A_					      =					model_class(config=__snake_case   )
            for name, param in model.named_parameters():
                 if "embeddings" not in name and 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'   ,)










import shutil
import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import SpeechaTextTokenizer, is_speech_available
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


if is_speech_available():
      from transformers import SpeechaTextFeatureExtractor, SpeechaTextProcessor


a__			:    Optional[Any]			      =		get_tests_dir('fixtures/test_sentencepiece.model')





@require_torch
@require_torchaudio
@require_sentencepiece
class UpperCAmelCase_							(   unittest.TestCase   ):


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					tempfile.mkdtemp()

       A_					      =					['''<s>''', '''<pad>''', '''</s>''', '''<unk>''', '''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
       A_					      =					dict(zip(__snake_case   ,range(len(__snake_case   )   )   )   )
       A_					      =					Path(self.tmpdirname   )
       save_json(__snake_case   ,save_dir / VOCAB_FILES_NAMES['''vocab_file''']   )
       if not (save_dir / VOCAB_FILES_NAMES["spm_file"]).exists():
            copyfile(__snake_case   ,save_dir / VOCAB_FILES_NAMES['''spm_file''']   )

       A_					      =					SpeechaTextTokenizer.from_pretrained(self.tmpdirname   )
       tokenizer.save_pretrained(self.tmpdirname   )

       A_					      =					{
           '''feature_size''': 2_4,
           '''num_mel_bins''': 2_4,
           '''padding_value''': 0.0,
           '''sampling_rate''': 1_6_0_0_0,
           '''return_attention_mask''': False,
           '''do_normalize''': True,
       }
       save_json(__snake_case   ,save_dir / FEATURE_EXTRACTOR_NAME   )


  def 	__UpperCAmelCase    (       self   ,**__snake_case   ):



       """simple docstring"""





       return SpeechaTextTokenizer.from_pretrained(self.tmpdirname   ,**__snake_case   )


  def 	__UpperCAmelCase    (       self   ,**__snake_case   ):



       """simple docstring"""





       return SpeechaTextFeatureExtractor.from_pretrained(self.tmpdirname   ,**__snake_case   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       shutil.rmtree(self.tmpdirname   )


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_tokenizer()
       A_					      =					self.get_feature_extractor()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

       processor.save_pretrained(self.tmpdirname   )
       A_					      =					SpeechaTextProcessor.from_pretrained(self.tmpdirname   )

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

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


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					SpeechaTextProcessor(
           tokenizer=self.get_tokenizer()   ,feature_extractor=self.get_feature_extractor()   )
       processor.save_pretrained(self.tmpdirname   )

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

       A_					      =					SpeechaTextProcessor.from_pretrained(
           self.tmpdirname   ,bos_token='''(BOS)'''   ,eos_token='''(EOS)'''   ,do_normalize=__snake_case   ,padding_value=1.0   )

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

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


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

       A_					      =					floats_list((3, 1_0_0_0)   )

       A_					      =					feature_extractor(__snake_case   ,return_tensors='''np'''   )
       A_					      =					processor(__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 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

       A_					      =					'''This is a test string'''

       A_					      =					processor(text=__snake_case   )

       A_					      =					tokenizer(__snake_case   )

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


  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

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

       A_					      =					processor.batch_decode(__snake_case   )
       A_					      =					tokenizer.batch_decode(__snake_case   )

       self.assertListEqual(__snake_case   ,__snake_case   )




  def 	__UpperCAmelCase    (       self   ):



       """simple docstring"""





       A_					      =					self.get_feature_extractor()
       A_					      =					self.get_tokenizer()

       A_					      =					SpeechaTextProcessor(tokenizer=__snake_case   ,feature_extractor=__snake_case   )

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



