



"""simple docstring"""
import unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM


@require_torch
class    SCREAMING_SNAKE_CASE__ :

    def __init__(				self			:				Tuple ,  lowerCAmelCase_			:				int ,  lowerCAmelCase_			:				int=9_9 ,  lowerCAmelCase_			:				Dict=1_3 ,  lowerCAmelCase_			:				int=1_6 ,  lowerCAmelCase_			:				Optional[Any]=7 ,  lowerCAmelCase_			:				str=True ,  lowerCAmelCase_			:				Optional[int]=True ,  lowerCAmelCase_			:				int=True ,  lowerCAmelCase_			:				Optional[Any]=False ,  lowerCAmelCase_			:				Any=True ,  lowerCAmelCase_			:				Union[str, Any]=2 ,  lowerCAmelCase_			:				str=3_2 ,  lowerCAmelCase_			:				Union[str, Any]=4 ,  lowerCAmelCase_			:				Union[str, Any]=4 ,  lowerCAmelCase_			:				List[Any]=3_0 ,  lowerCAmelCase_			:				int=0 ,  lowerCAmelCase_			:				List[str]=1 ,  lowerCAmelCase_			:				List[str]=2 ,  lowerCAmelCase_			:				Tuple=None ,  ):







      """simple docstring"""







      lowercase_								= parent
      lowercase_								= batch_size
      lowercase_								= decoder_seq_length
      # For common tests
      lowercase_								= self.decoder_seq_length
      lowercase_								= is_training
      lowercase_								= use_attention_mask
      lowercase_								= use_labels

      lowercase_								= vocab_size
      lowercase_								= d_model
      lowercase_								= d_model
      lowercase_								= decoder_layers
      lowercase_								= decoder_layers
      lowercase_								= decoder_ffn_dim
      lowercase_								= decoder_attention_heads
      lowercase_								= decoder_attention_heads
      lowercase_								= eos_token_id
      lowercase_								= bos_token_id
      lowercase_								= pad_token_id
      lowercase_								= decoder_start_token_id
      lowercase_								= use_cache
      lowercase_								= max_position_embeddings

      lowercase_								= None
      lowercase_								= decoder_seq_length
      lowercase_								= 2
      lowercase_								= 1

    def 					_UpperCAmelCase	(				self			:				Union[str, Any]):







      """simple docstring"""







      lowercase_								= ids_tensor([self.batch_size, self.decoder_seq_length] ,  self.vocab_size)

      lowercase_								= None
      if self.use_attention_mask:
        lowercase_								= ids_tensor([self.batch_size, self.decoder_seq_length] ,  vocab_size=2)

      lowercase_								= None
      if self.use_labels:
        lowercase_								= ids_tensor([self.batch_size, self.decoder_seq_length] ,  self.vocab_size)

      lowercase_								= TrOCRConfig(
          vocab_size=self.vocab_size ,  d_model=self.d_model ,  decoder_layers=self.decoder_layers ,  decoder_ffn_dim=self.decoder_ffn_dim ,  decoder_attention_heads=self.decoder_attention_heads ,  eos_token_id=self.eos_token_id ,  bos_token_id=self.bos_token_id ,  use_cache=self.use_cache ,  pad_token_id=self.pad_token_id ,  decoder_start_token_id=self.decoder_start_token_id ,  max_position_embeddings=self.max_position_embeddings ,  )

      return (config, input_ids, attention_mask, lm_labels)

    def 					_UpperCAmelCase	(				self			:				Dict ,  lowerCAmelCase_			:				Tuple ,  lowerCAmelCase_			:				Union[str, Any] ,  lowerCAmelCase_			:				Any ,  lowerCAmelCase_			:				Union[str, Any] ,  ):







      """simple docstring"""







      lowercase_								= True
      lowercase_								= TrOCRDecoder(config=lowerCAmelCase_).to(lowerCAmelCase_).eval()
      lowercase_								= input_ids[:2]

      input_ids[input_ids == 0] += 1
      # first forward pass
      lowercase_								= model(lowerCAmelCase_ ,  use_cache=lowerCAmelCase_)
      lowercase_								= model(lowerCAmelCase_)
      lowercase_								= model(lowerCAmelCase_ ,  use_cache=lowerCAmelCase_)

      self.parent.assertTrue(len(lowerCAmelCase_) == len(lowerCAmelCase_))
      self.parent.assertTrue(len(lowerCAmelCase_) == len(lowerCAmelCase_) + 1)

      lowercase_								= outputs["""past_key_values"""]

      # create hypothetical next token and extent to next_input_ids
      lowercase_								= ids_tensor((2, 1) ,  config.vocab_size - 1) + 1

      # append to next input_ids and
      lowercase_								= torch.cat([input_ids, next_tokens] ,  dim=-1)

      lowercase_								= model(lowerCAmelCase_)["""last_hidden_state"""]
      lowercase_								= model(lowerCAmelCase_ ,  past_key_values=lowerCAmelCase_)["""last_hidden_state"""]

      # select random slice
      lowercase_								= ids_tensor((1,) ,  output_from_past.shape[-1]).item()
      lowercase_								= output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
      lowercase_								= output_from_past[:, 0, random_slice_idx].detach()

      # test that outputs are equal for slice
      assert torch.allclose(lowerCAmelCase_ ,  lowerCAmelCase_ ,  atol=1E-3)




    def 					_UpperCAmelCase	(				self			:				List[str]):







      """simple docstring"""







      lowercase_								= self.prepare_config_and_inputs()
      lowercase_					,    lowercase_					,    lowercase_					,    lowercase_								= config_and_inputs

      lowercase_								= {"""input_ids""": input_ids, """attention_mask""": attention_mask}
      return config, inputs_dict







@require_torch
class    SCREAMING_SNAKE_CASE__ (							__UpperCAmelCase      ,  __UpperCAmelCase      ,  __UpperCAmelCase      ,  unittest.TestCase   ):
    lowercase__      =   (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
    lowercase__      =   (TrOCRForCausalLM,) if is_torch_available() else ()
    lowercase__      =   {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
    lowercase__      =   True
    lowercase__      =   False

    def 					_UpperCAmelCase	(				self			:				Tuple):







      """simple docstring"""







      lowercase_								= TrOCRStandaloneDecoderModelTester(self ,  is_training=lowerCAmelCase_)
      lowercase_								= ConfigTester(self ,  config_class=lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				List[str]):







      """simple docstring"""







      pass

    def 					_UpperCAmelCase	(				self			:				Optional[Any]):







      """simple docstring"""







      pass

    def 					_UpperCAmelCase	(				self			:				Optional[Any]):







      """simple docstring"""







      pass

    def 					_UpperCAmelCase	(				self			:				int):







      """simple docstring"""







      self.config_tester.run_common_tests()

    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      lowercase_								= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_decoder_model_past(*lowerCAmelCase_)

    def 					_UpperCAmelCase	(				self			:				Any):







      """simple docstring"""







      return




    @unittest.skip("""The model doesn't support left padding""")  # and it's not used enough to be worth fixing :)
    def 					_UpperCAmelCase	(				self			:				str):







      """simple docstring"""







      pass






"""simple docstring"""
from typing import List

import datasets
from datasets.tasks import AudioClassification

from ..folder_based_builder import folder_based_builder


UpperCAmelCase						:		Union[str, Any]			= datasets.utils.logging.get_logger(__name__)


class    SCREAMING_SNAKE_CASE__ (							folder_based_builder.FolderBasedBuilderConfig   ):
    lowercase__      =   None
    lowercase__      =   None







class    SCREAMING_SNAKE_CASE__ (							folder_based_builder.FolderBasedBuilder   ):
    lowercase__      =   datasets.Audio()
    lowercase__      =   "audio"
    lowercase__      =   AudioFolderConfig
    lowercase__      =   42  # definition at the bottom of the script
    lowercase__      =   AudioClassification(audio_column="audio"      ,  label_column="label"   )







UpperCAmelCase						:		Tuple			= [
    ".aiff",
    ".au",
    ".avr",
    ".caf",
    ".flac",
    ".htk",
    ".svx",
    ".mat4",
    ".mat5",
    ".mpc2k",
    ".ogg",
    ".paf",
    ".pvf",
    ".raw",
    ".rf64",
    ".sd2",
    ".sds",
    ".ircam",
    ".voc",
    ".w64",
    ".wav",
    ".nist",
    ".wavex",
    ".wve",
    ".xi",
    ".mp3",
    ".opus",
]
UpperCAmelCase						:		Any			= AUDIO_EXTENSIONS

