






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       ,    UpperCAmelCase       ,    UpperCAmelCase=99       ,    UpperCAmelCase=13       ,    UpperCAmelCase=16       ,    UpperCAmelCase=7       ,    UpperCAmelCase=True       ,    UpperCAmelCase=True       ,    UpperCAmelCase=True       ,    UpperCAmelCase=False       ,    UpperCAmelCase=True       ,    UpperCAmelCase=2       ,    UpperCAmelCase=32       ,    UpperCAmelCase=4       ,    UpperCAmelCase=4       ,    UpperCAmelCase=30       ,    UpperCAmelCase=0       ,    UpperCAmelCase=1       ,    UpperCAmelCase=2       ,    UpperCAmelCase=None       ,    ):
							"""simple docstring"""







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

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

							_UpperCAmelCase									=			None
							_UpperCAmelCase									=			decoder_seq_length
							_UpperCAmelCase									=			2
							_UpperCAmelCase									=			1

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







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

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

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

							_UpperCAmelCase									=			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       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    UpperCAmelCase       ,    ):
							"""simple docstring"""







							_UpperCAmelCase									=			True
							_UpperCAmelCase									=			TrOCRDecoder(config=UpperCAmelCase    ).to(UpperCAmelCase    ).eval()
							_UpperCAmelCase									=			input_ids[:2]

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

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

							_UpperCAmelCase									=			outputs['past_key_values']

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

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

							_UpperCAmelCase									=			model(UpperCAmelCase    )['last_hidden_state']
							_UpperCAmelCase									=			model(UpperCAmelCase       ,    past_key_values=UpperCAmelCase    )['last_hidden_state']

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

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

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







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

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





@require_torch
class __lowerCamelCase (      snake_case__     ,       snake_case__     ,       snake_case__     ,       unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__								=      (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
	UpperCamelCase__								=      (TrOCRForCausalLM,) if is_torch_available() else ()
	UpperCamelCase__								=      {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
	UpperCamelCase__								=      True
	UpperCamelCase__								=      False

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			TrOCRStandaloneDecoderModelTester(self       ,    is_training=UpperCAmelCase    )
							_UpperCAmelCase									=			ConfigTester(self       ,    config_class=UpperCAmelCase    )

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							pass

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							pass

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							pass

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							self.config_tester.run_common_tests()

	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*UpperCAmelCase    )

	def        UpperCamelCase    (							self    ):
							"""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    ):
							"""simple docstring"""







							pass








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_a      	=  logging.get_logger(__name__)

_a      	=  {
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}





class __lowerCamelCase (      snake_case__):





	"""simple docstring"""






	UpperCamelCase__								=      "distilbert"
	UpperCamelCase__								=      {
	    "hidden_size": "dim",
	    "num_attention_heads": "n_heads",
	    "num_hidden_layers": "n_layers",
	}

	def __init__(							self       ,    UpperCAmelCase=3_0522       ,    UpperCAmelCase=512       ,    UpperCAmelCase=False       ,    UpperCAmelCase=6       ,    UpperCAmelCase=12       ,    UpperCAmelCase=768       ,    UpperCAmelCase=4 * 768       ,    UpperCAmelCase=0.1       ,    UpperCAmelCase=0.1       ,    UpperCAmelCase="gelu"       ,    UpperCAmelCase=0.02       ,    UpperCAmelCase=0.1       ,    UpperCAmelCase=0.2       ,    UpperCAmelCase=0       ,    **UpperCAmelCase       ,    ):
							"""simple docstring"""







							_UpperCAmelCase									=			vocab_size
							_UpperCAmelCase									=			max_position_embeddings
							_UpperCAmelCase									=			sinusoidal_pos_embds
							_UpperCAmelCase									=			n_layers
							_UpperCAmelCase									=			n_heads
							_UpperCAmelCase									=			dim
							_UpperCAmelCase									=			hidden_dim
							_UpperCAmelCase									=			dropout
							_UpperCAmelCase									=			attention_dropout
							_UpperCAmelCase									=			activation
							_UpperCAmelCase									=			initializer_range
							_UpperCAmelCase									=			qa_dropout
							_UpperCAmelCase									=			seq_classif_dropout
							super().__init__(**UpperCAmelCase       ,    pad_token_id=UpperCAmelCase    )





class __lowerCamelCase (      snake_case__):





	"""simple docstring"""






	@property
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							if self.task == "multiple-choice":
													_UpperCAmelCase									=			{0: 'batch', 1: 'choice', 2: 'sequence'}
							else:
													_UpperCAmelCase									=			{0: 'batch', 1: 'sequence'}
							return OrderedDict(
							    [
							        ('input_ids', dynamic_axis),
							        ('attention_mask', dynamic_axis),
							    ]    )
