





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						:







 '''simple docstring'''

 def __init__(self    :       int	,			UpperCAmelCase_    :       List[Any]	,			UpperCAmelCase_    :       List[Any]=99	,			UpperCAmelCase_    :       Tuple=13	,			UpperCAmelCase_    :       List[Any]=16	,			UpperCAmelCase_    :       Optional[Any]=7	,			UpperCAmelCase_    :       List[str]=True	,			UpperCAmelCase_    :       Optional[int]=True	,			UpperCAmelCase_    :       str=True	,			UpperCAmelCase_    :       List[Any]=False	,			UpperCAmelCase_    :       Dict=True	,			UpperCAmelCase_    :       List[Any]=2	,			UpperCAmelCase_    :       Tuple=32	,			UpperCAmelCase_    :       List[Any]=4	,			UpperCAmelCase_    :       str=4	,			UpperCAmelCase_    :       Union[str, Any]=30	,			UpperCAmelCase_    :       Union[str, Any]=0	,			UpperCAmelCase_    :       Any=1	,			UpperCAmelCase_    :       Tuple=2	,			UpperCAmelCase_    :       Dict=None	,			)	->str:



   '''simple docstring'''



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

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

   lowerCamelCase__:				Any        =None
   lowerCamelCase__:				str        =decoder_seq_length
   lowerCamelCase__:				int        =2
   lowerCamelCase__:				Optional[int]        =1

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				List[str]        =ids_tensor([self.batch_size, self.decoder_seq_length]	,			self.vocab_size)

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

   lowerCamelCase__:				List[str]        =None
   if self.use_labels:
     lowerCamelCase__:				Optional[Any]        =ids_tensor([self.batch_size, self.decoder_seq_length]	,			self.vocab_size)

   lowerCamelCase__:				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 							SCREAMING_SNAKE_CASE_ (self    :       Any	,			UpperCAmelCase_    :       Any	,			UpperCAmelCase_    :       Dict	,			UpperCAmelCase_    :       List[str]	,			UpperCAmelCase_    :       Optional[int]	,			)	->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:				int        =True
   lowerCamelCase__:				List[str]        =TrOCRDecoder(config=UpperCAmelCase_).to(UpperCAmelCase_).eval()
   lowerCamelCase__:				List[str]        =input_ids[:2]

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

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

   lowerCamelCase__:				Dict        =outputs["past_key_values"]

   # create hypothetical next token and extent to next_input_ids
   lowerCamelCase__:				Optional[int]        =ids_tensor((2, 1)	,			config.vocab_size - 1) + 1

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

   lowerCamelCase__:				Optional[Any]        =model(UpperCAmelCase_)["last_hidden_state"]
   lowerCamelCase__:				Tuple        =model(UpperCAmelCase_	,			past_key_values=UpperCAmelCase_)["last_hidden_state"]

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

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







 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:				str        =self.prepare_config_and_inputs()
   lowerCamelCase__:				int        =config_and_inputs

   lowerCamelCase__:				Dict        ={"input_ids": input_ids, "attention_mask": attention_mask}
   return config, inputs_dict



@require_torch
class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ,							__SCREAMING_SNAKE_CASE   ,							__SCREAMING_SNAKE_CASE   ,							unittest.TestCase   ):







 '''simple docstring'''
 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 							SCREAMING_SNAKE_CASE_ (self    :       int)	->int:



   '''simple docstring'''



   lowerCamelCase__:				Dict        =TrOCRStandaloneDecoderModelTester(self	,			is_training=UpperCAmelCase_)
   lowerCamelCase__:				Any        =ConfigTester(self	,			config_class=UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Dict:



   '''simple docstring'''



   pass

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->Optional[Any]:



   '''simple docstring'''



   pass

 def 							SCREAMING_SNAKE_CASE_ (self    :       int)	->str:



   '''simple docstring'''



   pass

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



   '''simple docstring'''



   self.config_tester.run_common_tests()

 def 							SCREAMING_SNAKE_CASE_ (self    :       Tuple)	->Tuple:



   '''simple docstring'''



   lowerCamelCase__:				str        =self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[Any])	->Any:



   '''simple docstring'''



   return







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



   '''simple docstring'''



   pass








import enum
import os
from hashlib import shaaaa
from typing import Optional

from .. import config
from .logging import get_logger


__A     =    get_logger(__name__)



class        _SCREAMING_SNAKE_CASE						(     enum.Enum   ):







 '''simple docstring'''
 lowercase_		=      "all_checks"
 lowercase_		=      "basic_checks"
 lowercase_		=      "no_checks"



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a=None		)					->		Optional[int]:







  """simple docstring"""





  if expected_checksums is None:
    logger.info("Unable to verify checksums."		)
    return
  if len(set(__a		) - set(__a		)		) > 0:
    raise ExpectedMoreDownloadedFiles(str(set(__a		) - set(__a		)		)		)
  if len(set(__a		) - set(__a		)		) > 0:
    raise UnexpectedDownloadedFile(str(set(__a		) - set(__a		)		)		)
  lowerCamelCase__:				List[Any]        =[url for url in expected_checksums if expected_checksums[url] != recorded_checksums[url]]
  lowerCamelCase__:				Union[str, Any]        =" for " + verification_name if verification_name is not None else ""
  if len(__a		) > 0:
    raise NonMatchingChecksumError(
        F"""Checksums didn't match{for_verification_name}:\n"""
        F"""{bad_urls}\n"""
        "Set `verification_mode='no_checks'` to skip checksums verification and ignore this error"		)
  logger.info("All the checksums matched successfully" + for_verification_name		)



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''





def 					lowerCAmelCase_			(	__a     ,   __a		)					->		Any:







  """simple docstring"""





  if expected_splits is None:
    logger.info("Unable to verify splits sizes."		)
    return
  if len(set(__a		) - set(__a		)		) > 0:
    raise ExpectedMoreSplits(str(set(__a		) - set(__a		)		)		)
  if len(set(__a		) - set(__a		)		) > 0:
    raise UnexpectedSplits(str(set(__a		) - set(__a		)		)		)
  lowerCamelCase__:				Optional[int]        =[
      {"expected": expected_splits[name], "recorded": recorded_splits[name]}
      for name in expected_splits
      if expected_splits[name].num_examples != recorded_splits[name].num_examples
  ]
  if len(__a		) > 0:
    raise NonMatchingSplitsSizesError(str(__a		)		)
  logger.info("All the splits matched successfully."		)





def 					lowerCAmelCase_			(	__a     ,   __a = True		)					->		dict:







  """simple docstring"""





  if record_checksum:
    lowerCamelCase__:				str        =shaaaa()
    with open(__a     ,   "rb"		) as f:
      for chunk in iter(lambda: f.read(1 << 20		)     ,   b""		):
        m.update(__a		)
      lowerCamelCase__:				Dict        =m.hexdigest()
  else:
    lowerCamelCase__:				List[str]        =None
  return {"num_bytes": os.path.getsize(__a		), "checksum": checksum}






def 					lowerCAmelCase_			(	__a		)					->		int:







  """simple docstring"""





  if dataset_size and config.IN_MEMORY_MAX_SIZE:
    return dataset_size < config.IN_MEMORY_MAX_SIZE
  else:
    return False

