
class  lowercase__:



	"""simple docstring"""




	def __init__(       self  :			int       ,							SCREAMING_SNAKE_CASE_  :			int       ,							SCREAMING_SNAKE_CASE_  :			List[str]=None       ,							SCREAMING_SNAKE_CASE_  :			Union[str, Any]=None       )       ->     List[str]:
					lowercase_       =					data
					lowercase_       =					previous
					lowercase_       =					next_node
	def __str__(       self  :			Tuple       )       ->     str:
					return f'''{self.data}'''
	def 						_lowercase  (       self  :			Any       )       ->     int:
					return self.data
	def 						_lowercase  (       self  :			Union[str, Any]       )       ->     Optional[int]:
					return self.next




	def 						_lowercase  (       self  :			Tuple       )       ->     Any:
					return self.previous

class  lowercase__:



	"""simple docstring"""




	def __init__(       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     str:
					lowercase_       =					head
	def __iter__(       self  :			Union[str, Any]       )       ->     str:
					return self




	def 						_lowercase  (       self  :			List[str]       )       ->     Tuple:
					if not self.current:
									raise StopIteration
					else:
									lowercase_       =					self.current.get_data()
									lowercase_       =					self.current.get_next()
									return value

class  lowercase__:



	"""simple docstring"""




	def __init__(       self  :			Union[str, Any]       )       ->     Any:
					lowercase_       =					None  # First node in list
					lowercase_       =					None  # Last node in list
	def __str__(       self  :			Union[str, Any]       )       ->     Union[str, Any]:
					lowercase_       =					self.head
					lowercase_       =					[]
					while current is not None:
									nodes.append(current.get_data()       )
									lowercase_       =					current.get_next()
					return " ".join(str(SCREAMING_SNAKE_CASE_       ) for node in nodes       )
	def __contains__(       self  :			Optional[Any]       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     List[Any]:
					lowercase_       =					self.head
					while current:
									if current.get_data() == value:
													return True
									lowercase_       =					current.get_next()
					return False
	def __iter__(       self  :			Dict       )       ->     Tuple:
					return LinkedListIterator(self.head       )
	def 						_lowercase  (       self  :			Any       )       ->     Optional[Any]:
					if self.head:
									return self.head.get_data()
					return None
	def 						_lowercase  (       self  :			Any       )       ->     Any:
					if self.tail:
									return self.tail.get_data()
					return None
	def 						_lowercase  (       self  :			Dict       ,							SCREAMING_SNAKE_CASE_  :			Node       )       ->     None:
					if self.head is None:
									lowercase_       =					node
									lowercase_       =					node
					else:
									self.insert_before_node(self.head       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Node       )       ->     None:
					if self.head is None:
									self.set_head(SCREAMING_SNAKE_CASE_       )
					else:
									self.insert_after_node(self.tail       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     None:
					lowercase_       =					Node(SCREAMING_SNAKE_CASE_       )
					if self.head is None:
									self.set_head(SCREAMING_SNAKE_CASE_       )
					else:
									self.set_tail(SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Optional[int]       ,							SCREAMING_SNAKE_CASE_  :			Node       ,							SCREAMING_SNAKE_CASE_  :			Node       )       ->     None:
					lowercase_       =					node
					lowercase_       =					node.previous

					if node.get_previous() is None:
									lowercase_       =					node_to_insert
					else:
									lowercase_       =					node_to_insert

					lowercase_       =					node_to_insert
	def 						_lowercase  (       self  :			List[str]       ,							SCREAMING_SNAKE_CASE_  :			Node       ,							SCREAMING_SNAKE_CASE_  :			Node       )       ->     None:
					lowercase_       =					node
					lowercase_       =					node.next

					if node.get_next() is None:
									lowercase_       =					node_to_insert
					else:
									lowercase_       =					node_to_insert

					lowercase_       =					node_to_insert
	def 						_lowercase  (       self  :			Tuple       ,							SCREAMING_SNAKE_CASE_  :			int       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     None:
					lowercase_       =					1
					lowercase_       =					Node(SCREAMING_SNAKE_CASE_       )
					lowercase_       =					self.head
					while node:
									if current_position == position:
													self.insert_before_node(SCREAMING_SNAKE_CASE_       ,							SCREAMING_SNAKE_CASE_       )
													return
									current_position += 1
									lowercase_       =					node.next
					self.insert_after_node(self.tail       ,							SCREAMING_SNAKE_CASE_       )
	def 						_lowercase  (       self  :			Dict       ,							SCREAMING_SNAKE_CASE_  :			int       )       ->     Node:
					lowercase_       =					self.head
					while node:
									if node.get_data() == item:
													return node
									lowercase_       =					node.get_next()
					raise Exception('''Node not found'''       )
	def 						_lowercase  (       self  :			List[Any]       ,							SCREAMING_SNAKE_CASE_  :			str       )       ->     List[Any]:
					if (node := self.get_node(SCREAMING_SNAKE_CASE_       )) is not None:
									if node == self.head:
													lowercase_       =					self.head.get_next()

									if node == self.tail:
													lowercase_       =					self.tail.get_previous()

									self.remove_node_pointers(SCREAMING_SNAKE_CASE_       )
	@staticmethod
	def 						_lowercase  (       SCREAMING_SNAKE_CASE_  :			Node       )       ->     None:
					if node.get_next():
									lowercase_       =					node.previous

					if node.get_previous():
									lowercase_       =					node.next

					lowercase_       =					None
					lowercase_       =					None




	def 						_lowercase  (       self  :			Tuple       )       ->     Tuple:
					return self.head is None




def 				a						(							):


				'''simple docstring'''


if __name__ == "__main__":
						import doctest

						doctest.testmod()








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





      '''simple docstring'''






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

             '''simple docstring'''







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

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

             lowercase__      			= None
             lowercase__      			= decoder_seq_length
             lowercase__      			= 2
             lowercase__      			= 1



      def       UpperCamelCase__		(self    :    str ):

             '''simple docstring'''







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

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

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

             lowercase__      			= 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    :    Tuple   ,						UpperCamelCase    :    List[Any]   ,						UpperCamelCase    :    Optional[Any]   ,						UpperCamelCase    :    Tuple   ,						UpperCamelCase    :    List[str]   ,						):

             '''simple docstring'''







             lowercase__      			= True
             lowercase__      			= TrOCRDecoder(config=UpperCamelCase ).to(UpperCamelCase ).eval()
             lowercase__      			= input_ids[:2]

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

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

             lowercase__      			= outputs['''past_key_values''']

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

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

             lowercase__      			= model(UpperCamelCase )['''last_hidden_state''']
             lowercase__      			= model(UpperCamelCase   ,						past_key_values=UpperCamelCase )['''last_hidden_state''']

             # select random slice
             lowercase__      			= ids_tensor((1,)   ,						output_from_past.shape[-1] ).item()
             lowercase__      			= output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
             lowercase__      			= 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    :    Optional[Any] ):

             '''simple docstring'''







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

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






@require_torch
class   __lowerCAmelCase						(lowercase_  ,     lowercase_  ,     lowercase_  ,     unittest.TestCase     ):





      '''simple docstring'''






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



      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             lowercase__      			= TrOCRStandaloneDecoderModelTester(self   ,						is_training=UpperCamelCase )
             lowercase__      			= ConfigTester(self   ,						config_class=UpperCamelCase )



      def       UpperCamelCase__		(self    :    List[str] ):

             '''simple docstring'''







             pass



      def       UpperCamelCase__		(self    :    Optional[int] ):

             '''simple docstring'''







             pass



      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             pass



      def       UpperCamelCase__		(self    :    Any ):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def       UpperCamelCase__		(self    :    Union[str, Any] ):

             '''simple docstring'''







             lowercase__      			= self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase )



      def       UpperCamelCase__		(self    :    Optional[int] ):

             '''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    :    List[str] ):

             '''simple docstring'''







             pass




