





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			:





			def __init__( self					,			snake_case					,			snake_case=99					,			snake_case=13					,			snake_case=16					,			snake_case=7					,			snake_case=True					,			snake_case=True					,			snake_case=True					,			snake_case=False					,			snake_case=True					,			snake_case=2					,			snake_case=32					,			snake_case=4					,			snake_case=4					,			snake_case=30					,			snake_case=0					,			snake_case=1					,			snake_case=2					,			snake_case=None					,			)				->  int:
						"""simple docstring"""





						a__	:						int      		=  parent
						a__	:						Union[str, Any]      		=  batch_size
						a__	:						Dict      		=  decoder_seq_length
						# For common tests
						a__	:						Optional[Any]      		=  self.decoder_seq_length
						a__	:						Union[str, Any]      		=  is_training
						a__	:						List[Any]      		=  use_attention_mask
						a__	:						int      		=  use_labels

						a__	:						Optional[int]      		=  vocab_size
						a__	:						List[str]      		=  d_model
						a__	:						List[Any]      		=  d_model
						a__	:						Optional[Any]      		=  decoder_layers
						a__	:						Optional[int]      		=  decoder_layers
						a__	:						List[str]      		=  decoder_ffn_dim
						a__	:						int      		=  decoder_attention_heads
						a__	:						Union[str, Any]      		=  decoder_attention_heads
						a__	:						List[str]      		=  eos_token_id
						a__	:						Tuple      		=  bos_token_id
						a__	:						Tuple      		=  pad_token_id
						a__	:						List[Any]      		=  decoder_start_token_id
						a__	:						str      		=  use_cache
						a__	:						int      		=  max_position_embeddings

						a__	:						Any      		=  None
						a__	:						List[str]      		=  decoder_seq_length
						a__	:						Any      		=  2
						a__	:						List[str]      		=  1





			def 	_snake_case ( self      )				->  List[str]:
						"""simple docstring"""





						a__	:						Optional[Any]      		=  ids_tensor([self.batch_size, self.decoder_seq_length]					,			self.vocab_size      )

						a__	:						int      		=  None
						if self.use_attention_mask:
									a__	:						Union[str, Any]      		=  ids_tensor([self.batch_size, self.decoder_seq_length]					,			vocab_size=2      )

						a__	:						Dict      		=  None
						if self.use_labels:
									a__	:						Union[str, Any]      		=  ids_tensor([self.batch_size, self.decoder_seq_length]					,			self.vocab_size      )

						a__	:						Optional[Any]      		=  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					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			)				->  Optional[Any]:
						"""simple docstring"""





						a__	:						Dict      		=  True
						a__	:						List[str]      		=  TrOCRDecoder(config=lowercase__      ).to(lowercase__      ).eval()
						a__	:						Optional[int]      		=  input_ids[:2]

						input_ids[input_ids == 0] += 1
						# first forward pass
						a__	:						Union[str, Any]      		=  model(lowercase__					,			use_cache=lowercase__      )
						a__	:						List[str]      		=  model(lowercase__      )
						a__	:						int      		=  model(lowercase__					,			use_cache=lowercase__      )

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

						a__	:						List[Any]      		=  outputs["past_key_values"]

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

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

						a__	:						Union[str, Any]      		=  model(lowercase__      )["last_hidden_state"]
						a__	:						Tuple      		=  model(lowercase__					,			past_key_values=lowercase__      )["last_hidden_state"]

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

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





			def 	_snake_case ( self      )				->  List[str]:
						"""simple docstring"""





						a__	:						Union[str, Any]      		=  self.prepare_config_and_inputs()
						a__	:						Tuple      		=  config_and_inputs

						a__	:						Optional[Any]      		=  {"input_ids": input_ids, "attention_mask": attention_mask}
						return config, inputs_dict







@require_torch
class   __lowerCAmelCase			(					_UpperCAmelCase		,_UpperCAmelCase		,_UpperCAmelCase		,unittest.TestCase  ):
			_UpperCamelCase :						Optional[Any]     		=				(TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
			_UpperCamelCase :						Any     		=				(TrOCRForCausalLM,) if is_torch_available() else ()
			_UpperCamelCase :						List[str]     		=				{"""text-generation""": TrOCRForCausalLM} if is_torch_available() else {}
			_UpperCamelCase :						Dict     		=				True
			_UpperCamelCase :						Union[str, Any]     		=				False





			def 	_snake_case ( self      )				->  Union[str, Any]:
						"""simple docstring"""





						a__	:						int      		=  TrOCRStandaloneDecoderModelTester(self					,			is_training=lowercase__      )
						a__	:						Union[str, Any]      		=  ConfigTester(self					,			config_class=lowercase__      )





			def 	_snake_case ( self      )				->  Union[str, Any]:
						"""simple docstring"""





						pass





			def 	_snake_case ( self      )				->  Dict:
						"""simple docstring"""





						pass





			def 	_snake_case ( self      )				->  Dict:
						"""simple docstring"""





						pass





			def 	_snake_case ( self      )				->  Optional[int]:
						"""simple docstring"""





						self.config_tester.run_common_tests()





			def 	_snake_case ( self      )				->  str:
						"""simple docstring"""





						a__	:						Any      		=  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_decoder_model_past(*lowercase__      )





			def 	_snake_case ( self      )				->  List[Any]:
						"""simple docstring"""





						return





			@unittest.skip("The model doesn't support left padding"      )  # and it's not used enough to be worth fixing :)
			def 	_snake_case ( self      )				->  Union[str, Any]:
						"""simple docstring"""





						pass








from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE__       :			Any			      =						{"""configuration_sew""": ["""SEW_PRETRAINED_CONFIG_ARCHIVE_MAP""", """SEWConfig"""]}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  SCREAMING_SNAKE_CASE__       :			Union[str, Any]			      =						[
      """SEW_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """SEWForCTC""",
      """SEWForSequenceClassification""",
      """SEWModel""",
      """SEWPreTrainedModel""",
  ]

if TYPE_CHECKING:
  from .configuration_sew import SEW_PRETRAINED_CONFIG_ARCHIVE_MAP, SEWConfig

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_sew import (
        SEW_PRETRAINED_MODEL_ARCHIVE_LIST,
        SEWForCTC,
        SEWForSequenceClassification,
        SEWModel,
        SEWPreTrainedModel,
    )


else:
  import sys

  SCREAMING_SNAKE_CASE__       :			Optional[int]			      =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

