




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   :

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

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

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

										__magic_name__						:						Any										=None
										__magic_name__						:						Dict										=decoder_seq_length
										__magic_name__						:						Union[str, Any]										=2
										__magic_name__						:						List[str]										=1

						def      A__    (		self :Optional[Any]			):
										'''simple docstring'''

										__magic_name__						:						Union[str, Any]										=ids_tensor([self.batch_size, self.decoder_seq_length]     ,			self.vocab_size			)

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

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

										__magic_name__						:						Tuple										=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      A__    (		self :Union[str, Any]     ,			__snake_case :Any     ,			__snake_case :int     ,			__snake_case :Dict     ,			__snake_case :Dict     ,			):
										'''simple docstring'''

										__magic_name__						:						Dict										=True
										__magic_name__						:						List[Any]										=TrOCRDecoder(config=__snake_case			).to(__snake_case			).eval()
										__magic_name__						:						List[Any]										=input_ids[:2]

										input_ids[input_ids == 0] += 1
										# first forward pass
										__magic_name__						:						str										=model(__snake_case     ,			use_cache=__snake_case			)
										__magic_name__						:						Optional[Any]										=model(__snake_case			)
										__magic_name__						:						str										=model(__snake_case     ,			use_cache=__snake_case			)

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

										__magic_name__						:						str										=outputs["""past_key_values"""]

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

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

										__magic_name__						:						Dict										=model(__snake_case			)["""last_hidden_state"""]
										__magic_name__						:						Union[str, Any]										=model(__snake_case     ,			past_key_values=__snake_case			)["""last_hidden_state"""]

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

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


						def      A__    (		self :Tuple			):
										'''simple docstring'''

										__magic_name__						:						Tuple										=self.prepare_config_and_inputs()
										__magic_name__					,			__magic_name__					,			__magic_name__					,			__magic_name__						:						int										=config_and_inputs

										__magic_name__						:						Optional[int]										={"""input_ids""": input_ids, """attention_mask""": attention_mask}
										return config, inputs_dict




@require_torch
class 		__A   (      UpperCamelCase__       ,    UpperCamelCase__       ,    UpperCamelCase__       ,    unittest.TestCase      ):
						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      A__    (		self :Dict			):
										'''simple docstring'''

										__magic_name__						:						Dict										=TrOCRStandaloneDecoderModelTester(self     ,			is_training=__snake_case			)
										__magic_name__						:						Optional[int]										=ConfigTester(self     ,			config_class=__snake_case			)

						def      A__    (		self :Optional[int]			):
										'''simple docstring'''

										pass

						def      A__    (		self :Tuple			):
										'''simple docstring'''

										pass

						def      A__    (		self :int			):
										'''simple docstring'''

										pass

						def      A__    (		self :Union[str, Any]			):
										'''simple docstring'''

										self.config_tester.run_common_tests()

						def      A__    (		self :List[Any]			):
										'''simple docstring'''

										__magic_name__						:						Dict										=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_decoder_model_past(*__snake_case			)

						def      A__    (		self :Union[str, Any]			):
										'''simple docstring'''

										return


						@unittest.skip("""The model doesn't support left padding"""			)  # and it's not used enough to be worth fixing :)
						def      A__    (		self :Optional[int]			):
										'''simple docstring'''

										pass












"""simple docstring"""
import argparse
import copy


def 							A						(				__snake_case: Union[str, Any]  )    ->		Tuple:

       """simple docstring"""





       __magic_name__      				=    {}

       with open(__snake_case  ) as f:
              for line in f:
                     if line.split()[0] not in dict_of_neighbours:
                            __magic_name__      				=    []
                            _list.append([line.split()[1], line.split()[2]]  )
                            __magic_name__      				=    _list
                     else:
                            dict_of_neighbours[line.split()[0]].append(
                                [line.split()[1], line.split()[2]]  )
                     if line.split()[1] not in dict_of_neighbours:
                            __magic_name__      				=    []
                            _list.append([line.split()[0], line.split()[2]]  )
                            __magic_name__      				=    _list
                     else:
                            dict_of_neighbours[line.split()[1]].append(
                                [line.split()[0], line.split()[2]]  )

       return dict_of_neighbours


def 							A						(				__snake_case: Optional[Any] ,     __snake_case: str  )    ->		List[str]:

       """simple docstring"""





       with open(__snake_case  ) as f:
              __magic_name__      				=    f.read(1  )
       __magic_name__      				=    start_node

       __magic_name__      				=    []

       __magic_name__      				=    start_node

       __magic_name__      				=    0
       while visiting not in first_solution:
              __magic_name__      				=    1_0_0_0_0
              for k in dict_of_neighbours[visiting]:
                     if int(k[1]  ) < int(__snake_case  ) and k[0] not in first_solution:
                            __magic_name__      				=    k[1]
                            __magic_name__      				=    k[0]

              first_solution.append(__snake_case  )
              __magic_name__      				=    distance_of_first_solution + int(__snake_case  )
              __magic_name__      				=    best_node

       first_solution.append(__snake_case  )

       __magic_name__      				=    0
       for k in dict_of_neighbours[first_solution[-2]]:
              if k[0] == start_node:
                     break
              position += 1

       __magic_name__      				=    (
           distance_of_first_solution
           + int(dict_of_neighbours[first_solution[-2]][position][1]  )
           - 1_0_0_0_0
       )
       return first_solution, distance_of_first_solution


def 							A						(				__snake_case: Union[str, Any] ,     __snake_case: Optional[Any]  )    ->		Dict:

       """simple docstring"""





       __magic_name__      				=    []

       for n in solution[1:-1]:
              __magic_name__      				=    solution.index(__snake_case  )
              for kn in solution[1:-1]:
                     __magic_name__      				=    solution.index(__snake_case  )
                     if n == kn:
                            continue

                     __magic_name__      				=    copy.deepcopy(__snake_case  )
                     __magic_name__      				=    kn
                     __magic_name__      				=    n

                     __magic_name__      				=    0

                     for k in _tmp[:-1]:
                            __magic_name__      				=    _tmp[_tmp.index(__snake_case  ) + 1]
                            for i in dict_of_neighbours[k]:
                                   if i[0] == next_node:
                                          __magic_name__      				=    distance + int(i[1]  )
                     _tmp.append(__snake_case  )

                     if _tmp not in neighborhood_of_solution:
                            neighborhood_of_solution.append(_tmp  )

       __magic_name__      				=    len(neighborhood_of_solution[0]  ) - 1

       neighborhood_of_solution.sort(key=lambda __snake_case  : x[index_of_last_item_in_the_list]  )
       return neighborhood_of_solution


def 							A						(				__snake_case: List[str] ,     __snake_case: Union[str, Any] ,     __snake_case: List[str] ,     __snake_case: Optional[int] ,     __snake_case: List[str]  )    ->		Dict:

       """simple docstring"""





       __magic_name__      				=    1
       __magic_name__      				=    first_solution
       __magic_name__      				=    []
       __magic_name__      				=    distance_of_first_solution
       __magic_name__      				=    solution

       while count <= iters:
              __magic_name__      				=    find_neighborhood(__snake_case ,     __snake_case  )
              __magic_name__      				=    0
              __magic_name__      				=    neighborhood[index_of_best_solution]
              __magic_name__      				=    len(__snake_case  ) - 1

              __magic_name__      				=    False
              while not found:
                     __magic_name__      				=    0
                     while i < len(__snake_case  ):
                            if best_solution[i] != solution[i]:
                                   __magic_name__      				=    best_solution[i]
                                   __magic_name__      				=    solution[i]
                                   break
                            __magic_name__      				=    i + 1

                     if [first_exchange_node, second_exchange_node] not in tabu_list and [
                         second_exchange_node,
                         first_exchange_node,
                     ] not in tabu_list:
                            tabu_list.append([first_exchange_node, second_exchange_node]  )
                            __magic_name__      				=    True
                            __magic_name__      				=    best_solution[:-1]
                            __magic_name__      				=    neighborhood[index_of_best_solution][best_cost_index]
                            if cost < best_cost:
                                   __magic_name__      				=    cost
                                   __magic_name__      				=    solution
                     else:
                            __magic_name__      				=    index_of_best_solution + 1
                            __magic_name__      				=    neighborhood[index_of_best_solution]

              if len(__snake_case  ) >= size:
                     tabu_list.pop(0  )

              __magic_name__      				=    count + 1

       return best_solution_ever, best_cost


def 							A						(				__snake_case: Union[str, Any]=None  )    ->		Tuple:

       """simple docstring"""





       __magic_name__      				=    generate_neighbours(args.File  )

       __magic_name__				,  __magic_name__      				=    generate_first_solution(
           args.File ,     __snake_case  )

       __magic_name__				,  __magic_name__      				=    tabu_search(
           __snake_case ,     __snake_case ,     __snake_case ,     args.Iterations ,     args.Size ,     )

       print(F"""Best solution: {best_sol}, with total distance: {best_cost}."""  )


if __name__ == "__main__":
 snake_case : str     		=		argparse.ArgumentParser(description="""Tabu Search""")
 parser.add_argument(
     """-f""",
     """--File""",
     type=str,
     help="""Path to the file containing the data""",
     required=True,
 )
 parser.add_argument(
     """-i""",
     """--Iterations""",
     type=int,
     help="""How many iterations the algorithm should perform""",
     required=True,
 )
 parser.add_argument(
     """-s""", """--Size""", type=int, help="""Size of the tabu list""", required=True
 )

 # Pass the arguments to main method
 main(parser.parse_args())