



"""simple docstring"""
from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_torch_available,
)


__SCREAMING_SNAKE_CASE  :						int    							=      {
    '''configuration_wav2vec2''': ['''WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''Wav2Vec2Config'''],
    '''feature_extraction_wav2vec2''': ['''Wav2Vec2FeatureExtractor'''],
    '''processing_wav2vec2''': ['''Wav2Vec2Processor'''],
    '''tokenization_wav2vec2''': ['''Wav2Vec2CTCTokenizer''', '''Wav2Vec2Tokenizer'''],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__SCREAMING_SNAKE_CASE  :						Union[str, Any]    							=      [
			    '''WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''Wav2Vec2ForAudioFrameClassification''',
			    '''Wav2Vec2ForCTC''',
			    '''Wav2Vec2ForMaskedLM''',
			    '''Wav2Vec2ForPreTraining''',
			    '''Wav2Vec2ForSequenceClassification''',
			    '''Wav2Vec2ForXVector''',
			    '''Wav2Vec2Model''',
			    '''Wav2Vec2PreTrainedModel''',
			]

try:
			if not is_tf_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__SCREAMING_SNAKE_CASE  :						Tuple    							=      [
			    '''TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''TFWav2Vec2ForCTC''',
			    '''TFWav2Vec2Model''',
			    '''TFWav2Vec2PreTrainedModel''',
			    '''TFWav2Vec2ForSequenceClassification''',
			]

try:
			if not is_flax_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			__SCREAMING_SNAKE_CASE  :						Dict    							=      [
			    '''FlaxWav2Vec2ForCTC''',
			    '''FlaxWav2Vec2ForPreTraining''',
			    '''FlaxWav2Vec2Model''',
			    '''FlaxWav2Vec2PreTrainedModel''',
			]


if TYPE_CHECKING:
			from .configuration_wavaveca import WAV_2_VEC_2_PRETRAINED_CONFIG_ARCHIVE_MAP, WavaVecaConfig
			from .feature_extraction_wavaveca import WavaVecaFeatureExtractor
			from .processing_wavaveca import WavaVecaProcessor
			from .tokenization_wavaveca import WavaVecaCTCTokenizer, WavaVecaTokenizer

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_wavaveca import (
						    WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    WavaVecaForAudioFrameClassification,
						    WavaVecaForCTC,
						    WavaVecaForMaskedLM,
						    WavaVecaForPreTraining,
						    WavaVecaForSequenceClassification,
						    WavaVecaForXVector,
						    WavaVecaModel,
						    WavaVecaPreTrainedModel,
						)

			try:
						if not is_tf_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_wavaveca import (
						    TF_WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFWavaVecaForCTC,
						    TFWavaVecaForSequenceClassification,
						    TFWavaVecaModel,
						    TFWavaVecaPreTrainedModel,
						)

			try:
						if not is_flax_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_tf_wavaveca import (
						    FlaxWavaVecaForCTC,
						    FlaxWavaVecaForPreTraining,
						    FlaxWavaVecaModel,
						    FlaxWavaVecaPreTrainedModel,
						)


else:
			import sys

			__SCREAMING_SNAKE_CASE  :						Union[str, Any]    							=      _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





"""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 							lowerCamelCase_:





					'''simple docstring'''



					def __init__(    self				,       lowerCamelCase__				,       lowerCamelCase__=9_9				,       lowerCamelCase__=1_3				,       lowerCamelCase__=1_6				,       lowerCamelCase__=7				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=True				,       lowerCamelCase__=False				,       lowerCamelCase__=True				,       lowerCamelCase__=2				,       lowerCamelCase__=3_2				,       lowerCamelCase__=4				,       lowerCamelCase__=4				,       lowerCamelCase__=3_0				,       lowerCamelCase__=0				,       lowerCamelCase__=1				,       lowerCamelCase__=2				,       lowerCamelCase__=None				,       ):
									_lowerCamelCase        =				parent
									_lowerCamelCase        =				batch_size
									_lowerCamelCase        =				decoder_seq_length
									# For common tests
									_lowerCamelCase        =				self.decoder_seq_length
									_lowerCamelCase        =				is_training
									_lowerCamelCase        =				use_attention_mask
									_lowerCamelCase        =				use_labels

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

									_lowerCamelCase        =				None
									_lowerCamelCase        =				decoder_seq_length
									_lowerCamelCase        =				2
									_lowerCamelCase        =				1

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				ids_tensor([self.batch_size, self.decoder_seq_length]				,       self.vocab_size   )

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

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

									_lowerCamelCase        =				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 			snake_case__      (    self				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       lowerCamelCase__				,       ):
									_lowerCamelCase        =				True
									_lowerCamelCase        =				TrOCRDecoder(config=lowerCamelCase__   ).to(lowerCamelCase__   ).eval()
									_lowerCamelCase        =				input_ids[:2]

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

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

									_lowerCamelCase        =				outputs['''past_key_values''']

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

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

									_lowerCamelCase        =				model(lowerCamelCase__   )['''last_hidden_state''']
									_lowerCamelCase        =				model(lowerCamelCase__				,       past_key_values=lowerCamelCase__   )['''last_hidden_state''']

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

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





					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.prepare_config_and_inputs()
									_lowerCamelCase  ,     _lowerCamelCase  ,     _lowerCamelCase  ,     _lowerCamelCase        =				config_and_inputs

									_lowerCamelCase        =				{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
									return config, inputs_dict


@require_torch
class 							lowerCamelCase_(   A__,				A__,				A__,				unittest.TestCase    ):





					'''simple docstring'''



					lowercase__   :       int										= (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
					lowercase__   :       List[str]										= (TrOCRForCausalLM,) if is_torch_available() else ()
					lowercase__   :       Tuple										= {'text-generation': TrOCRForCausalLM} if is_torch_available() else {}
					lowercase__   :       Dict										= True
					lowercase__   :       Optional[Any]										= False

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				TrOCRStandaloneDecoderModelTester(self				,       is_training=lowerCamelCase__   )
									_lowerCamelCase        =				ConfigTester(self				,       config_class=lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									pass

					def 			snake_case__      (    self   ):
									pass

					def 			snake_case__      (    self   ):
									pass

					def 			snake_case__      (    self   ):
									self.config_tester.run_common_tests()

					def 			snake_case__      (    self   ):
									_lowerCamelCase        =				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_decoder_model_past(*lowerCamelCase__   )

					def 			snake_case__      (    self   ):
									return





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