


"""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        __A       :





							'''simple docstring'''

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



														"""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							: Union[str, Any]    )   ->Dict:



														"""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							: Optional[Any]						,    UpperCAmelCase_							: Optional[Any]						,    UpperCAmelCase_							: Tuple						,    UpperCAmelCase_							: List[Any]						,    UpperCAmelCase_							: List[Any]						,    )   ->int:



														"""simple docstring"""



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

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

														self.parent.assertTrue(len(UpperCAmelCase_    ) == len(UpperCAmelCase_    )    )
														self.parent.assertTrue(len(UpperCAmelCase_    ) == len(UpperCAmelCase_    ) + 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(UpperCAmelCase_    )["""last_hidden_state"""]
														snake_case_          =						model(UpperCAmelCase_						,    past_key_values=UpperCAmelCase_    )["""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(UpperCAmelCase_						,    UpperCAmelCase_						,    atol=1E-3    )



							def 						lowerCAmelCase    (			self							: str    )   ->Any:



														"""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        __A       (snake_case__  ,						snake_case__  ,						snake_case__  ,						unittest.TestCase):





							'''simple docstring'''

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



							def 						lowerCAmelCase    (			self							: Dict    )   ->Optional[Any]:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Any    )   ->List[str]:



														"""simple docstring"""



														pass



							def 						lowerCAmelCase    (			self							: List[str]    )   ->List[Any]:



														"""simple docstring"""



														pass



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



														"""simple docstring"""



														pass



							def 						lowerCAmelCase    (			self							: Any    )   ->List[str]:



														"""simple docstring"""



														self.config_tester.run_common_tests()



							def 						lowerCAmelCase    (			self							: List[str]    )   ->Tuple:



														"""simple docstring"""



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



							def 						lowerCAmelCase    (			self							: Tuple    )   ->List[str]:



														"""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							: Tuple    )   ->Tuple:



														"""simple docstring"""



														pass







"""simple docstring"""
import math





class        __A       :





							'''simple docstring'''

							def __init__(			self							: List[str]						,    UpperCAmelCase_							: Tuple=0    )   ->Optional[int]:  # a graph with Node 0,1,...,N-1



														"""simple docstring"""



														snake_case_          =						n
														snake_case_          =						[
														    [math.inf for j in range(0						,    UpperCAmelCase_    )] for i in range(0						,    UpperCAmelCase_    )
														]  # adjacency matrix for weight
														snake_case_          =						[
														    [math.inf for j in range(0						,    UpperCAmelCase_    )] for i in range(0						,    UpperCAmelCase_    )
														]  # dp[i][j] stores minimum distance from i to j



							def 						lowerCAmelCase    (			self							: List[str]						,    UpperCAmelCase_							: Any						,    UpperCAmelCase_							: List[str]						,    UpperCAmelCase_							: Optional[Any]    )   ->Optional[int]:



														"""simple docstring"""



														snake_case_          =						w



							def 						lowerCAmelCase    (			self							: Optional[int]    )   ->Any:



														"""simple docstring"""



														for k in range(0						,    self.n    ):
																					for i in range(0						,    self.n    ):
																												for j in range(0						,    self.n    ):
																																			snake_case_          =						min(self.dp[i][j]						,    self.dp[i][k] + self.dp[k][j]    )



							def 						lowerCAmelCase    (			self							: Optional[int]						,    UpperCAmelCase_							: Dict						,    UpperCAmelCase_							: Optional[Any]    )   ->Union[str, Any]:



														"""simple docstring"""



														return self.dp[u][v]


if __name__ == "__main__":
				__SCREAMING_SNAKE_CASE							:      Optional[int]						=      Graph(5)
				graph.add_edge(0, 2, 9)
				graph.add_edge(0, 4, 10)
				graph.add_edge(1, 3, 5)
				graph.add_edge(2, 3, 7)
				graph.add_edge(3, 0, 10)
				graph.add_edge(3, 1, 2)
				graph.add_edge(3, 2, 1)
				graph.add_edge(3, 4, 6)
				graph.add_edge(4, 1, 3)
				graph.add_edge(4, 2, 4)
				graph.add_edge(4, 3, 9)
				graph.floyd_warshall()
				graph.show_min(1, 4)
				graph.show_min(0, 3)



