



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







       '''simple docstring'''


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


             '''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   lowercase__       (							self    :						Dict					)				->		List[Any]:


             '''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   lowercase__       (							self    :						str	,       _UpperCAmelCase    :						List[Any]	,       _UpperCAmelCase    :						Optional[int]	,       _UpperCAmelCase    :						str	,       _UpperCAmelCase    :						Tuple	,       )				->		Dict:


             '''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   lowercase__       (							self    :						Union[str, Any]					)				->		Optional[int]:


             '''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    lowercase__     (					SCREAMING_SNAKE_CASE       ,      SCREAMING_SNAKE_CASE       ,      SCREAMING_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   lowercase__       (							self    :						List[Any]					)				->		int:


             '''simple docstring'''




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



       def   lowercase__       (							self    :						str					)				->		List[str]:


             '''simple docstring'''




             pass



       def   lowercase__       (							self    :						Union[str, Any]					)				->		Optional[int]:


             '''simple docstring'''




             pass



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


             '''simple docstring'''




             pass



       def   lowercase__       (							self    :						Optional[Any]					)				->		List[Any]:


             '''simple docstring'''




             self.config_tester.run_common_tests()



       def   lowercase__       (							self    :						str					)				->		Tuple:


             '''simple docstring'''




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



       def   lowercase__       (							self    :						Any					)				->		int:


             '''simple docstring'''




             return







       @unittest.skip("The model doesn't support left padding"					)  # and it's not used enough to be worth fixing :)
       def   lowercase__       (							self    :						Union[str, Any]					)				->		List[Any]:


             '''simple docstring'''




             pass









"""simple docstring"""





import colorsys

from PIL import Image  # type: ignore
def      a__			(   lowerCAmelCase__			,    lowerCAmelCase__			,    lowerCAmelCase__   ):
      UpperCAmelCase_    				=							x
      UpperCAmelCase_    				=							y
      for step in range(lowerCAmelCase__   ):  # noqa: B007
            UpperCAmelCase_    				=							a * a - b * b + x
            UpperCAmelCase_    				=							2 * a * b + y
            UpperCAmelCase_    				=							a_new

            # divergence happens for all complex number with an absolute value
            # greater than 4
            if a * a + b * b > 4:
                  break
      return step / (max_step - 1)
def      a__			(   lowerCAmelCase__   ):
      if distance == 1:
            return (0, 0, 0)
      else:
            return (255, 255, 255)
def      a__			(   lowerCAmelCase__   ):
      if distance == 1:
            return (0, 0, 0)
      else:
            return tuple(round(i * 255   ) for i in colorsys.hsv_to_rgb(lowerCAmelCase__			,    1			,    1   )   )
def      a__			(   lowerCAmelCase__ = 800			,    lowerCAmelCase__ = 600			,    lowerCAmelCase__ = -0.6			,    lowerCAmelCase__ = 0			,    lowerCAmelCase__ = 3.2			,    lowerCAmelCase__ = 50			,    lowerCAmelCase__ = True			,    ):
      UpperCAmelCase_    				=							Image.new("RGB"			,    (image_width, image_height)   )
      UpperCAmelCase_    				=							img.load()

      # loop through the image-coordinates
      for image_x in range(lowerCAmelCase__   ):
            for image_y in range(lowerCAmelCase__   ):
                  # determine the figure-coordinates based on the image-coordinates
                  UpperCAmelCase_    				=							figure_width / image_width * image_height
                  UpperCAmelCase_    				=							figure_center_x + (image_x / image_width - 0.5) * figure_width
                  UpperCAmelCase_    				=							figure_center_y + (image_y / image_height - 0.5) * figure_height

                  UpperCAmelCase_    				=							get_distance(lowerCAmelCase__			,    lowerCAmelCase__			,    lowerCAmelCase__   )

                  # color the corresponding pixel based on the selected coloring-function
                  if use_distance_color_coding:
                        UpperCAmelCase_    				=							get_color_coded_rgb(lowerCAmelCase__   )
                  else:
                        UpperCAmelCase_    				=							get_black_and_white_rgb(lowerCAmelCase__   )

      return img


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  # colored version, full figure
  lowerCamelCase      =		get_image()

  # uncomment for colored version, different section, zoomed in
  # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
  # figure_width = 0.8)

  # uncomment for black and white version, full figure
  # img = get_image(use_distance_color_coding = False)

  # uncomment to save the image
  # img.save("mandelbrot.png")

  img.show()




