




'''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 			_snake_case		:




			def __init__( self    ,							a__    ,							a__=99    ,							a__=13    ,							a__=16    ,							a__=7    ,							a__=True    ,							a__=True    ,							a__=True    ,							a__=False    ,							a__=True    ,							a__=2    ,							a__=32    ,							a__=4    ,							a__=4    ,							a__=30    ,							a__=0    ,							a__=1    ,							a__=2    ,							a__=None    ,							)    ->    Tuple:

										'''simple docstring'''




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

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

										snake_case_      =				None
										snake_case_      =				decoder_seq_length
										snake_case_      =				2
										snake_case_      =				1




			def        lowerCAmelCase__    ( self							)    ->    List[Any]:

										'''simple docstring'''




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

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

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

										snake_case_      =				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        lowerCAmelCase__    ( self    ,							a__    ,							a__    ,							a__    ,							a__    ,							)    ->    Union[str, Any]:

										'''simple docstring'''




										snake_case_      =				True
										snake_case_      =				TrOCRDecoder(config=a__							).to(a__							).eval()
										snake_case_      =				input_ids[:2]

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

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

										snake_case_      =				outputs["past_key_values"]

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

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

										snake_case_      =				model(a__							)["last_hidden_state"]
										snake_case_      =				model(a__    ,							past_key_values=a__							)["last_hidden_state"]

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

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




			def        lowerCAmelCase__    ( self							)    ->    Tuple:

										'''simple docstring'''




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

										snake_case_      =				{"input_ids": input_ids, "attention_mask": attention_mask}
										return config, inputs_dict




@require_torch
class 			_snake_case		(     lowercase_						,						lowercase_						,						lowercase_						,						unittest.TestCase							):
			lowerCAmelCase_					:				Any							=   (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
			lowerCAmelCase_					:				List[Any]							=   (TrOCRForCausalLM,) if is_torch_available() else ()
			lowerCAmelCase_					:				Dict							=   {"text-generation": TrOCRForCausalLM} if is_torch_available() else {}
			lowerCAmelCase_					:				List[str]							=   True
			lowerCAmelCase_					:				str							=   False




			def        lowerCAmelCase__    ( self							)    ->    str:

										'''simple docstring'''




										snake_case_      =				TrOCRStandaloneDecoderModelTester(self    ,							is_training=a__							)
										snake_case_      =				ConfigTester(self    ,							config_class=a__							)




			def        lowerCAmelCase__    ( self							)    ->    List[Any]:

										'''simple docstring'''




										pass




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

										'''simple docstring'''




										pass




			def        lowerCAmelCase__    ( self							)    ->    str:

										'''simple docstring'''




										pass




			def        lowerCAmelCase__    ( self							)    ->    Optional[int]:

										'''simple docstring'''




										self.config_tester.run_common_tests()




			def        lowerCAmelCase__    ( self							)    ->    Optional[int]:

										'''simple docstring'''




										snake_case_      =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_decoder_model_past(*a__							)




			def        lowerCAmelCase__    ( 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        lowerCAmelCase__    ( self							)    ->    str:

										'''simple docstring'''




										pass










'''simple docstring'''





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin




class 			_snake_case		(     unittest.TestCase						,						lowercase_							):




			def        lowerCAmelCase__    ( self							)    ->    Optional[int]:

										'''simple docstring'''




										snake_case_      =				load_tool("text-classification"							)
										self.tool.setup()
										snake_case_      =				load_tool("text-classification"    ,							remote=a__							)




			def        lowerCAmelCase__    ( self							)    ->    str:

										'''simple docstring'''




										snake_case_      =				self.tool("That's quite cool"    ,							["positive", "negative"]							)
										self.assertEqual(a__    ,							"positive"							)




			def        lowerCAmelCase__    ( self							)    ->    Optional[int]:

										'''simple docstring'''




										snake_case_      =				self.remote_tool("That's quite cool"    ,							["positive", "negative"]							)
										self.assertEqual(a__    ,							"positive"							)




			def        lowerCAmelCase__    ( self							)    ->    int:

										'''simple docstring'''




										snake_case_      =				self.tool(text="That's quite cool"    ,							labels=["positive", "negative"]							)
										self.assertEqual(a__    ,							"positive"							)




			def        lowerCAmelCase__    ( self							)    ->    Any:

										'''simple docstring'''




										snake_case_      =				self.remote_tool(text="That's quite cool"    ,							labels=["positive", "negative"]							)
										self.assertEqual(a__    ,							"positive"							)




