

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 							UpperCAmelCase_						:
							"""simple docstring"""







							def __init__(     self			,				_a			,				_a=9_9			,				_a=1_3			,				_a=1_6			,				_a=7			,				_a=True			,				_a=True			,				_a=True			,				_a=False			,				_a=True			,				_a=2			,				_a=3_2			,				_a=4			,				_a=4			,				_a=3_0			,				_a=0			,				_a=1			,				_a=2			,				_a=None			,				)       ->       Any:
												_a      :       int   =						parent
												_a      :       List[Any]   =						batch_size
												_a      :       int   =						decoder_seq_length
												# For common tests
												_a      :       List[Any]   =						self.decoder_seq_length
												_a      :       Tuple   =						is_training
												_a      :       str   =						use_attention_mask
												_a      :       Tuple   =						use_labels

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

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


							def 							__lowercase							(     self			)       ->       Dict:
												_a      :       Any   =						ids_tensor([self.batch_size, self.decoder_seq_length]			,				self.vocab_size			)

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

												_a      :       str   =						None
												if self.use_labels:
																	_a      :       List[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 							__lowercase							(     self			,				_a			,				_a			,				_a			,				_a			,				)       ->       List[Any]:
												_a      :       List[Any]   =						True
												_a      :       str   =						TrOCRDecoder(config=_a			).to(_a			).eval()
												_a      :       Optional[Any]   =						input_ids[:2]

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

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

												_a      :       Tuple   =						outputs['''past_key_values''']

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

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

												_a      :       Any   =						model(_a			)['''last_hidden_state''']
												_a      :       Optional[Any]   =						model(_a			,				past_key_values=_a			)['''last_hidden_state''']

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

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




							def 							__lowercase							(     self			)       ->       Optional[Any]:
												_a      :       Dict   =						self.prepare_config_and_inputs()
												_a ,   _a ,   _a ,   _a      :       Union[str, Any]   =						config_and_inputs

												_a      :       Optional[Any]   =						{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
												return config, inputs_dict



@require_torch
class 							UpperCAmelCase_						(     __lowercase							,			__lowercase							,			__lowercase							,			unittest.TestCase  ):
							"""simple docstring"""







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


							def 							__lowercase							(     self			)       ->       Tuple:
												_a      :       Tuple   =						TrOCRStandaloneDecoderModelTester(self			,				is_training=_a			)
												_a      :       Dict   =						ConfigTester(self			,				config_class=_a			)


							def 							__lowercase							(     self			)       ->       Optional[Any]:
												pass


							def 							__lowercase							(     self			)       ->       Optional[Any]:
												pass


							def 							__lowercase							(     self			)       ->       List[Any]:
												pass


							def 							__lowercase							(     self			)       ->       Union[str, Any]:
												self.config_tester.run_common_tests()


							def 							__lowercase							(     self			)       ->       Union[str, Any]:
												_a      :       int   =						self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_decoder_model_past(*_a			)


							def 							__lowercase							(     self			)       ->       List[str]:
												return




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











"""simple docstring"""
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mobilebert import MobileBertTokenizer


snake_case : Optional[Any]     		=		logging.get_logger(__name__)

snake_case : str     		=		{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

snake_case : List[Any]     		=		{
    """vocab_file""": {"""mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"""},
    """tokenizer_file""": {
        """mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json"""
    },
}

snake_case : Tuple     		=		{"""mobilebert-uncased""": 5_1_2}


snake_case : List[str]     		=		{}



class        UpperCamelCase__ (				a_):







    """simple docstring"""
    __UpperCAmelCase           = VOCAB_FILES_NAMES
    __UpperCAmelCase           = PRETRAINED_VOCAB_FILES_MAP
    __UpperCAmelCase           = PRETRAINED_INIT_CONFIGURATION
    __UpperCAmelCase           = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __UpperCAmelCase           = MobileBertTokenizer







    def __init__(		self :	Optional[int]				,    UpperCamelCase_ :	Optional[int]=None				,    UpperCamelCase_ :	str=None				,    UpperCamelCase_ :	Tuple=True				,    UpperCamelCase_ :	Any="[UNK]"				,    UpperCamelCase_ :	str="[SEP]"				,    UpperCamelCase_ :	Any="[PAD]"				,    UpperCamelCase_ :	Dict="[CLS]"				,    UpperCamelCase_ :	str="[MASK]"				,    UpperCamelCase_ :	Union[str, Any]=True				,    UpperCamelCase_ :	Dict=None				,    **UpperCamelCase_ :	Dict				,    ):





           '''simple docstring'''

           super().__init__(
               UpperCamelCase_				,    tokenizer_file=UpperCamelCase_				,    do_lower_case=UpperCamelCase_				,    unk_token=UpperCamelCase_				,    sep_token=UpperCamelCase_				,    pad_token=UpperCamelCase_				,    cls_token=UpperCamelCase_				,    mask_token=UpperCamelCase_				,    tokenize_chinese_chars=UpperCamelCase_				,    strip_accents=UpperCamelCase_				,    **UpperCamelCase_				,    )

           __magic_name__      				=    json.loads(self.backend_tokenizer.normalizer.__getstate__() )
           if (
               normalizer_state.get('lowercase'				,    UpperCamelCase_ ) != do_lower_case
               or normalizer_state.get('strip_accents'				,    UpperCamelCase_ ) != strip_accents
               or normalizer_state.get('handle_chinese_chars'				,    UpperCamelCase_ ) != tokenize_chinese_chars
           ):
                  __magic_name__      				=    getattr(UpperCamelCase_				,    normalizer_state.pop('type' ) )
                  __magic_name__      				=    do_lower_case
                  __magic_name__      				=    strip_accents
                  __magic_name__      				=    tokenize_chinese_chars
                  __magic_name__      				=    normalizer_class(**UpperCamelCase_ )

           __magic_name__      				=    do_lower_case







    def 				a__			(		self :	Dict				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	List[Any]=None ):





           '''simple docstring'''

           __magic_name__      				=    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

           if token_ids_a:
                  output += token_ids_a + [self.sep_token_id]

           return output







    def 				a__			(		self :	List[Any]				,    UpperCamelCase_ :	List[int]				,    UpperCamelCase_ :	Optional[List[int]] = None ):





           '''simple docstring'''

           __magic_name__      				=    [self.sep_token_id]
           __magic_name__      				=    [self.cls_token_id]
           if token_ids_a is None:
                  return len(cls + token_ids_a + sep ) * [0]
           return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]







    def 				a__			(		self :	List[str]				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Optional[str] = None ):





           '''simple docstring'''

           __magic_name__      				=    self._tokenizer.model.save(UpperCamelCase_				,    name=UpperCamelCase_ )
           return tuple(UpperCamelCase_ )