




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__:
      def __init__(				self: str    ,       UpperCamelCase_: Optional[Any]    ,       UpperCamelCase_: Optional[Any]=99    ,       UpperCamelCase_: List[str]=13    ,       UpperCamelCase_: int=16    ,       UpperCamelCase_: Tuple=7    ,       UpperCamelCase_: List[str]=True    ,       UpperCamelCase_: List[str]=True    ,       UpperCamelCase_: Optional[int]=True    ,       UpperCamelCase_: Dict=False    ,       UpperCamelCase_: str=True    ,       UpperCamelCase_: Union[str, Any]=2    ,       UpperCamelCase_: Union[str, Any]=32    ,       UpperCamelCase_: Any=4    ,       UpperCamelCase_: Dict=4    ,       UpperCamelCase_: str=30    ,       UpperCamelCase_: List[str]=0    ,       UpperCamelCase_: Dict=1    ,       UpperCamelCase_: Optional[Any]=2    ,       UpperCamelCase_: Dict=None    ,       ):
             __lowerCamelCase   =						parent
             __lowerCamelCase   =						batch_size
             __lowerCamelCase   =						decoder_seq_length
             # For common tests
             __lowerCamelCase   =						self.decoder_seq_length
             __lowerCamelCase   =						is_training
             __lowerCamelCase   =						use_attention_mask
             __lowerCamelCase   =						use_labels

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

             __lowerCamelCase   =						None
             __lowerCamelCase   =						decoder_seq_length
             __lowerCamelCase   =						2
             __lowerCamelCase   =						1
      def 			lowerCAmelCase__						(				self: int   ):
             __lowerCamelCase   =						ids_tensor([self.batch_size, self.decoder_seq_length]    ,       self.vocab_size   )

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

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

             __lowerCamelCase   =						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: List[Any]    ,       UpperCamelCase_: Optional[int]    ,       UpperCamelCase_: Optional[int]    ,       UpperCamelCase_: Optional[Any]    ,       UpperCamelCase_: Dict    ,       ):
             __lowerCamelCase   =						True
             __lowerCamelCase   =						TrOCRDecoder(config=UpperCamelCase_   ).to(UpperCamelCase_   ).eval()
             __lowerCamelCase   =						input_ids[:2]

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

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

             __lowerCamelCase   =						outputs["""past_key_values"""]

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

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

             __lowerCamelCase   =						model(UpperCamelCase_   )["""last_hidden_state"""]
             __lowerCamelCase   =						model(UpperCamelCase_    ,       past_key_values=UpperCamelCase_   )["""last_hidden_state"""]

             # select random slice
             __lowerCamelCase   =						ids_tensor((1,)    ,       output_from_past.shape[-1]   ).item()
             __lowerCamelCase   =						output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
             __lowerCamelCase   =						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: Union[str, Any]   ):
             __lowerCamelCase   =						self.prepare_config_and_inputs()
             __lowerCamelCase,   __lowerCamelCase,   __lowerCamelCase,   __lowerCamelCase   =						config_and_inputs

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





@require_torch
class     lowerCamelCase__(					__lowerCamelCase ,     __lowerCamelCase ,     __lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	Tuple    		=   (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
      UpperCAmelCase__      :	Dict    		=   (TrOCRForCausalLM,) if is_torch_available() else ()
      UpperCAmelCase__      :	Union[str, Any]    		=   {'text-generation': TrOCRForCausalLM} if is_torch_available() else {}
      UpperCAmelCase__      :	Union[str, Any]    		=   True
      UpperCAmelCase__      :	Dict    		=   False
      def 			lowerCAmelCase__						(				self: str   ):
             __lowerCamelCase   =						TrOCRStandaloneDecoderModelTester(self    ,       is_training=UpperCamelCase_   )
             __lowerCamelCase   =						ConfigTester(self    ,       config_class=UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: str   ):
             pass
      def 			lowerCAmelCase__						(				self: List[str]   ):
             pass
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             pass
      def 			lowerCAmelCase__						(				self: List[str]   ):
             self.config_tester.run_common_tests()
      def 			lowerCAmelCase__						(				self: Dict   ):
             __lowerCamelCase   =						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_decoder_model_past(*UpperCamelCase_   )
      def 			lowerCAmelCase__						(				self: str   ):
             return




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







import gc
import random
import unittest

import numpy as np
import torch
from transformers import CLIPImageProcessor, CLIPVisionConfig, CLIPVisionModel

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEImgaImgPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import floats_tensor, load_image, load_numpy, slow
from diffusers.utils.testing_utils import require_torch_gpu, torch_device

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference





class     lowerCamelCase__(					__lowerCamelCase ,     unittest.TestCase):
      UpperCAmelCase__      :	Tuple    		=   ShapEImgaImgPipeline
      UpperCAmelCase__      :	Optional[Any]    		=   ['image']
      UpperCAmelCase__      :	int    		=   ['image']
      UpperCAmelCase__      :	Any    		=   [
          'num_images_per_prompt',
          'num_inference_steps',
          'generator',
          'latents',
          'guidance_scale',
          'frame_size',
          'output_type',
          'return_dict',
      ]
      UpperCAmelCase__      :	int    		=   False
      @property
      def 			lowerCAmelCase__						(				self: int   ):
             return 32
      @property
      def 			lowerCAmelCase__						(				self: List[str]   ):
             return 32
      @property
      def 			lowerCAmelCase__						(				self: Any   ):
             return self.time_input_dim * 4
      @property
      def 			lowerCAmelCase__						(				self: Dict   ):
             return 8
      @property
      def 			lowerCAmelCase__						(				self: int   ):
             torch.manual_seed(0   )
             __lowerCamelCase   =						CLIPVisionConfig(
                 hidden_size=self.text_embedder_hidden_size    ,       image_size=64    ,       projection_dim=self.text_embedder_hidden_size    ,       intermediate_size=37    ,       num_attention_heads=4    ,       num_channels=3    ,       num_hidden_layers=5    ,       patch_size=1    ,       )

             __lowerCamelCase   =						CLIPVisionModel(UpperCamelCase_   )
             return model
      @property
      def 			lowerCAmelCase__						(				self: Dict   ):
             __lowerCamelCase   =						CLIPImageProcessor(
                 crop_size=2_24    ,       do_center_crop=UpperCamelCase_    ,       do_normalize=UpperCamelCase_    ,       do_resize=UpperCamelCase_    ,       image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]    ,       image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]    ,       resample=3    ,       size=2_24    ,       )

             return image_processor
      @property
      def 			lowerCAmelCase__						(				self: Tuple   ):
             torch.manual_seed(0   )

             __lowerCamelCase   =						{
                 """num_attention_heads""": 2,
                 """attention_head_dim""": 16,
                 """embedding_dim""": self.time_input_dim,
                 """num_embeddings""": 32,
                 """embedding_proj_dim""": self.text_embedder_hidden_size,
                 """time_embed_dim""": self.time_embed_dim,
                 """num_layers""": 1,
                 """clip_embed_dim""": self.time_input_dim * 2,
                 """additional_embeddings""": 0,
                 """time_embed_act_fn""": """gelu""",
                 """norm_in_type""": """layer""",
                 """embedding_proj_norm_type""": """layer""",
                 """encoder_hid_proj_type""": None,
                 """added_emb_type""": None,
             }

             __lowerCamelCase   =						PriorTransformer(**UpperCamelCase_   )
             return model
      @property
      def 			lowerCAmelCase__						(				self: List[Any]   ):
             torch.manual_seed(0   )

             __lowerCamelCase   =						{
                 """param_shapes""": (
                     (self.renderer_dim, 93),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                     (self.renderer_dim, 8),
                 ),
                 """d_latent""": self.time_input_dim,
                 """d_hidden""": self.renderer_dim,
                 """n_output""": 12,
                 """background""": (
                     0.1,
                     0.1,
                     0.1,
                 ),
             }
             __lowerCamelCase   =						ShapERenderer(**UpperCamelCase_   )
             return model
      def 			lowerCAmelCase__						(				self: List[str]   ):
             __lowerCamelCase   =						self.dummy_prior
             __lowerCamelCase   =						self.dummy_image_encoder
             __lowerCamelCase   =						self.dummy_image_processor
             __lowerCamelCase   =						self.dummy_renderer

             __lowerCamelCase   =						HeunDiscreteScheduler(
                 beta_schedule="""exp"""    ,       num_train_timesteps=10_24    ,       prediction_type="""sample"""    ,       use_karras_sigmas=UpperCamelCase_    ,       clip_sample=UpperCamelCase_    ,       clip_sample_range=1.0    ,       )
             __lowerCamelCase   =						{
                 """prior""": prior,
                 """image_encoder""": image_encoder,
                 """image_processor""": image_processor,
                 """renderer""": renderer,
                 """scheduler""": scheduler,
             }

             return components
      def 			lowerCAmelCase__						(				self: int    ,       UpperCamelCase_: List[Any]    ,       UpperCamelCase_: Dict=0   ):
             __lowerCamelCase   =						floats_tensor((1, 3, 64, 64)    ,       rng=random.Random(UpperCamelCase_   )   ).to(UpperCamelCase_   )

             if str(UpperCamelCase_   ).startswith("""mps"""   ):
                    __lowerCamelCase   =						torch.manual_seed(UpperCamelCase_   )
             else:
                    __lowerCamelCase   =						torch.Generator(device=UpperCamelCase_   ).manual_seed(UpperCamelCase_   )
             __lowerCamelCase   =						{
                 """image""": input_image,
                 """generator""": generator,
                 """num_inference_steps""": 1,
                 """frame_size""": 32,
                 """output_type""": """np""",
             }
             return inputs
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						"""cpu"""

             __lowerCamelCase   =						self.get_dummy_components()

             __lowerCamelCase   =						self.pipeline_class(**UpperCamelCase_   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )

             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						pipe(**self.get_dummy_inputs(UpperCamelCase_   )   )
             __lowerCamelCase   =						output.images[0]
             __lowerCamelCase   =						image[0, -3:, -3:, -1]

             assert image.shape == (20, 32, 32, 3)

             __lowerCamelCase   =						np.array(
                 [
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                     0.0003_9216,
                 ]   )

             assert np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
      def 			lowerCAmelCase__						(				self: List[str]   ):
             # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
             self._test_inference_batch_consistent(batch_sizes=[1, 2]   )
      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						torch_device == """cpu"""
             __lowerCamelCase   =						True
             self._test_inference_batch_single_identical(
                 batch_size=2    ,       test_max_difference=UpperCamelCase_    ,       relax_max_difference=UpperCamelCase_    ,       )




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						self.get_dummy_components()
             __lowerCamelCase   =						self.pipeline_class(**UpperCamelCase_   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )
             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						1
             __lowerCamelCase   =						2

             __lowerCamelCase   =						self.get_dummy_inputs(UpperCamelCase_   )

             for key in inputs.keys():
                    if key in self.batch_params:
                           __lowerCamelCase   =						batch_size * [inputs[key]]

             __lowerCamelCase   =						pipe(**UpperCamelCase_    ,       num_images_per_prompt=UpperCamelCase_   )[0]

             assert images.shape[0] == batch_size * num_images_per_prompt





@slow
@require_torch_gpu
class     lowerCamelCase__(					unittest.TestCase):
      def 			lowerCAmelCase__						(				self: Union[str, Any]   ):
             # clean up the VRAM after each test
             super().tearDown()
             gc.collect()
             torch.cuda.empty_cache()




      def 			lowerCAmelCase__						(				self: Any   ):
             __lowerCamelCase   =						load_image(
                 """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/shap_e/corgi.png"""   )
             __lowerCamelCase   =						load_numpy(
                 """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
                 """/shap_e/test_shap_e_img2img_out.npy"""   )
             __lowerCamelCase   =						ShapEImgaImgPipeline.from_pretrained("""openai/shap-e-img2img"""   )
             __lowerCamelCase   =						pipe.to(UpperCamelCase_   )
             pipe.set_progress_bar_config(disable=UpperCamelCase_   )

             __lowerCamelCase   =						torch.Generator(device=UpperCamelCase_   ).manual_seed(0   )

             __lowerCamelCase   =						pipe(
                 UpperCamelCase_    ,       generator=UpperCamelCase_    ,       guidance_scale=3.0    ,       num_inference_steps=64    ,       frame_size=64    ,       output_type="""np"""    ,       ).images[0]

             assert images.shape == (20, 64, 64, 3)

             assert_mean_pixel_difference(UpperCamelCase_    ,       UpperCamelCase_   )

