



import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    IMAGE_PROCESSOR_MAPPING,
    AutoConfig,
    AutoImageProcessor,
    CLIPConfig,
    CLIPImageProcessor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER


sys.path.append(str(Path(__file__).parent.parent.parent.parent / '''utils'''))

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_image_processing import CustomImageProcessor  # noqa E402



class     lowerCAmelCase_		(  unittest.TestCase							):
       def  snake_case_	(      self	)					->		str:
          UpperCamelCase   :  Optional[int]						     =     0
       def  snake_case_	(      self	)					->		Tuple:
          UpperCamelCase   :  Dict						     =     AutoImageProcessor.from_pretrained('openai/clip-vit-base-patch32'	)
          self.assertIsInstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self	)					->		Optional[Any]:
          with tempfile.TemporaryDirectory() as tmpdirname:
             UpperCamelCase   :  List[Any]						     =     Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
             UpperCamelCase   :  List[str]						     =     Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
             json.dump(
                 {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'},      open(SCREAMING_SNAKE_CASE_,      'w'	),      )
             json.dump({'model_type': 'clip'},      open(SCREAMING_SNAKE_CASE_,      'w'	)	)

             UpperCamelCase   :  Any						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self	)					->		Union[str, Any]:
          # Ensure we can load the image processor from the feature extractor config
          with tempfile.TemporaryDirectory() as tmpdirname:
             UpperCamelCase   :  Tuple						     =     Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
             UpperCamelCase   :  Union[str, Any]						     =     Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
             json.dump(
                 {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'},      open(SCREAMING_SNAKE_CASE_,      'w'	),      )
             json.dump({'model_type': 'clip'},      open(SCREAMING_SNAKE_CASE_,      'w'	)	)

             UpperCamelCase   :  Any						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self	)					->		List[str]:
          with tempfile.TemporaryDirectory() as tmpdirname:
             UpperCamelCase   :  Dict						     =     CLIPConfig()

             # Create a dummy config file with image_proceesor_type
             UpperCamelCase   :  Any						     =     Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
             UpperCamelCase   :  Optional[Any]						     =     Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
             json.dump(
                 {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'},      open(SCREAMING_SNAKE_CASE_,      'w'	),      )
             json.dump({'model_type': 'clip'},      open(SCREAMING_SNAKE_CASE_,      'w'	)	)

             # remove image_processor_type to make sure config.json alone is enough to load image processor locally
             UpperCamelCase   :  List[Any]						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	).to_dict()

             config_dict.pop('image_processor_type'	)
             UpperCamelCase   :  str						     =     CLIPImageProcessor(**SCREAMING_SNAKE_CASE_	)

             # save in new folder
             model_config.save_pretrained(SCREAMING_SNAKE_CASE_	)
             config.save_pretrained(SCREAMING_SNAKE_CASE_	)

             UpperCamelCase   :  Tuple						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)

             # make sure private variable is not incorrectly saved
             UpperCamelCase   :  str						     =     json.loads(config.to_json_string()	)
             self.assertTrue('_processor_class' not in dict_as_saved	)

          self.assertIsInstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self	)					->		List[str]:
          with tempfile.TemporaryDirectory() as tmpdirname:
             UpperCamelCase   :  List[str]						     =     Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
             json.dump(
                 {'image_processor_type': 'CLIPImageProcessor', 'processor_class': 'CLIPProcessor'},      open(SCREAMING_SNAKE_CASE_,      'w'	),      )

             UpperCamelCase   :  Any						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
             self.assertIsInstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
       def  snake_case_	(      self	)					->		Optional[Any]:
          with self.assertRaisesRegex(
              SCREAMING_SNAKE_CASE_,      'clip-base is not a local folder and is not a valid model identifier'	):
             UpperCamelCase   :  Optional[int]						     =     AutoImageProcessor.from_pretrained('clip-base'	)
       def  snake_case_	(      self	)					->		Optional[int]:
          with self.assertRaisesRegex(
              SCREAMING_SNAKE_CASE_,      R'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'	):
             UpperCamelCase   :  Any						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_,      revision='aaaaaa'	)
       def  snake_case_	(      self	)					->		str:
          with self.assertRaisesRegex(
              SCREAMING_SNAKE_CASE_,      'hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json.',      ):
             UpperCamelCase   :  Union[str, Any]						     =     AutoImageProcessor.from_pretrained('hf-internal-testing/config-no-model'	)
       def  snake_case_	(      self	)					->		int:
          # If remote code is not set, we will time out when asking whether to load the model.
          with self.assertRaises(SCREAMING_SNAKE_CASE_	):
             UpperCamelCase   :  Tuple						     =     AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'	)
          # If remote code is disabled, we can't load this config.
          with self.assertRaises(SCREAMING_SNAKE_CASE_	):
             UpperCamelCase   :  str						     =     AutoImageProcessor.from_pretrained(
                 'hf-internal-testing/test_dynamic_image_processor',      trust_remote_code=SCREAMING_SNAKE_CASE_	)

          UpperCamelCase   :  Dict						     =     AutoImageProcessor.from_pretrained(
              'hf-internal-testing/test_dynamic_image_processor',      trust_remote_code=SCREAMING_SNAKE_CASE_	)
          self.assertEqual(image_processor.__class__.__name__,      'NewImageProcessor'	)

          # Test image processor can be reloaded.
          with tempfile.TemporaryDirectory() as tmp_dir:
             image_processor.save_pretrained(SCREAMING_SNAKE_CASE_	)
             UpperCamelCase   :  Optional[int]						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_,      trust_remote_code=SCREAMING_SNAKE_CASE_	)
          self.assertEqual(reloaded_image_processor.__class__.__name__,      'NewImageProcessor'	)
       def  snake_case_	(      self	)					->		str:
          try:
             AutoConfig.register('custom',      SCREAMING_SNAKE_CASE_	)
             AutoImageProcessor.register(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
             # Trying to register something existing in the Transformers library will raise an error
             with self.assertRaises(SCREAMING_SNAKE_CASE_	):
                AutoImageProcessor.register(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)

             with tempfile.TemporaryDirectory() as tmpdirname:
                UpperCamelCase   :  Tuple						     =     Path(SCREAMING_SNAKE_CASE_	) / 'preprocessor_config.json'
                UpperCamelCase   :  Optional[Any]						     =     Path(SCREAMING_SNAKE_CASE_	) / 'config.json'
                json.dump(
                    {'feature_extractor_type': 'CLIPFeatureExtractor', 'processor_class': 'CLIPProcessor'},      open(SCREAMING_SNAKE_CASE_,      'w'	),      )
                json.dump({'model_type': 'clip'},      open(SCREAMING_SNAKE_CASE_,      'w'	)	)

                UpperCamelCase   :  Tuple						     =     CustomImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)

             # Now that the config is registered, it can be used as any other config with the auto-API
             with tempfile.TemporaryDirectory() as tmp_dir:
                image_processor.save_pretrained(SCREAMING_SNAKE_CASE_	)
                UpperCamelCase   :  List[str]						     =     AutoImageProcessor.from_pretrained(SCREAMING_SNAKE_CASE_	)
                self.assertIsInstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)

          finally:
             if "custom" in CONFIG_MAPPING._extra_content:
                del CONFIG_MAPPING._extra_content["custom"]
             if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
                del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]






       def  snake_case_	(      self	)					->		Optional[Any]:



          class     lowerCAmelCase_		(  a__							):
                 UpperCAmelCase__    : int       					=  True

          try:
             AutoConfig.register('custom',      SCREAMING_SNAKE_CASE_	)
             AutoImageProcessor.register(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)
             # If remote code is not set, the default is to use local
             UpperCamelCase   :  str						     =     AutoImageProcessor.from_pretrained('hf-internal-testing/test_dynamic_image_processor'	)
             self.assertEqual(image_processor.__class__.__name__,      'NewImageProcessor'	)
             self.assertTrue(image_processor.is_local	)

             # If remote code is disabled, we load the local one.
             UpperCamelCase   :  List[str]						     =     AutoImageProcessor.from_pretrained(
                 'hf-internal-testing/test_dynamic_image_processor',      trust_remote_code=SCREAMING_SNAKE_CASE_	)
             self.assertEqual(image_processor.__class__.__name__,      'NewImageProcessor'	)
             self.assertTrue(image_processor.is_local	)

             # If remote is enabled, we load from the Hub
             UpperCamelCase   :  int						     =     AutoImageProcessor.from_pretrained(
                 'hf-internal-testing/test_dynamic_image_processor',      trust_remote_code=SCREAMING_SNAKE_CASE_	)
             self.assertEqual(image_processor.__class__.__name__,      'NewImageProcessor'	)
             self.assertTrue(not hasattr(SCREAMING_SNAKE_CASE_,      'is_local'	)	)

          finally:
             if "custom" in CONFIG_MAPPING._extra_content:
                del CONFIG_MAPPING._extra_content["custom"]
             if CustomConfig in IMAGE_PROCESSOR_MAPPING._extra_content:
                del IMAGE_PROCESSOR_MAPPING._extra_content[CustomConfig]








import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import HeunDiscreteScheduler, PriorTransformer, ShapEPipeline
from diffusers.pipelines.shap_e import ShapERenderer
from diffusers.utils import 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_		(  a__	,   unittest.TestCase							):
       UpperCAmelCase__    : Union[str, Any]       					=  ShapEPipeline
       UpperCAmelCase__    : List[Any]       					=  ["prompt"]
       UpperCAmelCase__    : List[str]       					=  ["prompt"]
       UpperCAmelCase__    : int       					=  [
           "num_images_per_prompt",
           "num_inference_steps",
           "generator",
           "latents",
           "guidance_scale",
           "frame_size",
           "output_type",
           "return_dict",
       ]
       UpperCAmelCase__    : Union[str, Any]       					=  False
       @property
       def  snake_case_	(      self	)					->		Union[str, Any]:
          return 32
       @property
       def  snake_case_	(      self	)					->		List[str]:
          return 32
       @property
       def  snake_case_	(      self	)					->		int:
          return self.time_input_dim * 4
       @property
       def  snake_case_	(      self	)					->		Optional[int]:
          return 8
       @property
       def  snake_case_	(      self	)					->		str:
          UpperCamelCase   :  Union[str, Any]						     =     CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'	)
          return tokenizer
       @property
       def  snake_case_	(      self	)					->		Optional[Any]:
          torch.manual_seed(0	)
          UpperCamelCase   :  Dict						     =     CLIPTextConfig(
              bos_token_id=0,      eos_token_id=2,      hidden_size=self.text_embedder_hidden_size,      projection_dim=self.text_embedder_hidden_size,      intermediate_size=37,      layer_norm_eps=1e-05,      num_attention_heads=4,      num_hidden_layers=5,      pad_token_id=1,      vocab_size=1000,      )
          return CLIPTextModelWithProjection(SCREAMING_SNAKE_CASE_	)
       @property
       def  snake_case_	(      self	)					->		Dict:
          torch.manual_seed(0	)

          UpperCamelCase   :  int						     =     {
              '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',
              'encoder_hid_proj_type': None,
              'added_emb_type': None,
          }

          UpperCamelCase   :  Any						     =     PriorTransformer(**SCREAMING_SNAKE_CASE_	)
          return model
       @property
       def  snake_case_	(      self	)					->		Tuple:
          torch.manual_seed(0	)

          UpperCamelCase   :  Any						     =     {
              '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,
              ),
          }
          UpperCamelCase   :  Dict						     =     ShapERenderer(**SCREAMING_SNAKE_CASE_	)
          return model
       def  snake_case_	(      self	)					->		Tuple:
          UpperCamelCase   :  List[Any]						     =     self.dummy_prior
          UpperCamelCase   :  int						     =     self.dummy_text_encoder
          UpperCamelCase   :  Dict						     =     self.dummy_tokenizer
          UpperCamelCase   :  List[str]						     =     self.dummy_renderer

          UpperCamelCase   :  str						     =     HeunDiscreteScheduler(
              beta_schedule='exp',      num_train_timesteps=1024,      prediction_type='sample',      use_karras_sigmas=SCREAMING_SNAKE_CASE_,      clip_sample=SCREAMING_SNAKE_CASE_,      clip_sample_range=1.0,      )
          UpperCamelCase   :  List[str]						     =     {
              'prior': prior,
              'text_encoder': text_encoder,
              'tokenizer': tokenizer,
              'renderer': renderer,
              'scheduler': scheduler,
          }

          return components
       def  snake_case_	(      self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_=0	)					->		Any:
          if str(SCREAMING_SNAKE_CASE_	).startswith('mps'	):
             UpperCamelCase   :  Optional[int]						     =     torch.manual_seed(SCREAMING_SNAKE_CASE_	)
          else:
             UpperCamelCase   :  Any						     =     torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(SCREAMING_SNAKE_CASE_	)
          UpperCamelCase   :  str						     =     {
              'prompt': 'horse',
              'generator': generator,
              'num_inference_steps': 1,
              'frame_size': 32,
              'output_type': 'np',
          }
          return inputs
       def  snake_case_	(      self	)					->		int:
          UpperCamelCase   :  str						     =     'cpu'

          UpperCamelCase   :  Optional[int]						     =     self.get_dummy_components()

          UpperCamelCase   :  int						     =     self.pipeline_class(**SCREAMING_SNAKE_CASE_	)
          UpperCamelCase   :  Optional[Any]						     =     pipe.to(SCREAMING_SNAKE_CASE_	)

          pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

          UpperCamelCase   :  str						     =     pipe(**self.get_dummy_inputs(SCREAMING_SNAKE_CASE_	)	)
          UpperCamelCase   :  Optional[int]						     =     output.images[0]
          UpperCamelCase   :  Union[str, Any]						     =     image[0, -3:, -3:, -1]

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

          UpperCamelCase   :  List[str]						     =     np.array(
              [
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
                  0.00_03_92_16,
              ]	)

          assert np.abs(image_slice.flatten() - expected_slice	).max() < 1e-2
       def  snake_case_	(      self	)					->		int:
          # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
          self._test_inference_batch_consistent(batch_sizes=[1, 2]	)
       def  snake_case_	(      self	)					->		str:
          UpperCamelCase   :  str						     =     torch_device == 'cpu'
          UpperCamelCase   :  Optional[int]						     =     True

          self._test_inference_batch_single_identical(
              batch_size=2,      test_max_difference=SCREAMING_SNAKE_CASE_,      relax_max_difference=SCREAMING_SNAKE_CASE_,      )






       def  snake_case_	(      self	)					->		List[Any]:
          UpperCamelCase   :  List[Any]						     =     self.get_dummy_components()
          UpperCamelCase   :  str						     =     self.pipeline_class(**SCREAMING_SNAKE_CASE_	)
          UpperCamelCase   :  Optional[int]						     =     pipe.to(SCREAMING_SNAKE_CASE_	)
          pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

          UpperCamelCase   :  Optional[Any]						     =     1
          UpperCamelCase   :  Union[str, Any]						     =     2

          UpperCamelCase   :  Union[str, Any]						     =     self.get_dummy_inputs(SCREAMING_SNAKE_CASE_	)

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

          UpperCamelCase   :  int						     =     pipe(**SCREAMING_SNAKE_CASE_,      num_images_per_prompt=SCREAMING_SNAKE_CASE_	)[0]

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






@slow
@require_torch_gpu
class     lowerCAmelCase_		(  unittest.TestCase							):
       def  snake_case_	(      self	)					->		str:
          # clean up the VRAM after each test
          super().tearDown()
          gc.collect()
          torch.cuda.empty_cache()






       def  snake_case_	(      self	)					->		Tuple:
          UpperCamelCase   :  Optional[int]						     =     load_numpy(
              'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
              '/shap_e/test_shap_e_np_out.npy'	)
          UpperCamelCase   :  Optional[int]						     =     ShapEPipeline.from_pretrained('openai/shap-e'	)
          UpperCamelCase   :  Optional[int]						     =     pipe.to(SCREAMING_SNAKE_CASE_	)
          pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_	)

          UpperCamelCase   :  str						     =     torch.Generator(device=SCREAMING_SNAKE_CASE_	).manual_seed(0	)

          UpperCamelCase   :  Dict						     =     pipe(
              'a shark',      generator=SCREAMING_SNAKE_CASE_,      guidance_scale=15.0,      num_inference_steps=64,      frame_size=64,      output_type='np',      ).images[0]

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

          assert_mean_pixel_difference(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_	)



