'''simple docstring'''





from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFCvtForImageClassification, TFCvtModel
       from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor


class 			UpperCAmelCase__    (		UpperCamelCase__					):



  """simple docstring"""






  def 	_UpperCAmelCase				(				self:			List[str]   ) -> int:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.config_class(**self.inputs_dict   )
    self.parent.assertTrue(hasattr(_a				,				"embed_dim"   )   )
    self.parent.assertTrue(hasattr(_a				,				"num_heads"   )   )






class 			UpperCAmelCase__    :



  """simple docstring"""






  def __init__(				self:			Union[str, Any]				,				__lowerCAmelCase:			List[str]				,				__lowerCAmelCase:			List[str]=13				,				__lowerCAmelCase:			Any=64				,				__lowerCAmelCase:			List[Any]=3				,				__lowerCAmelCase:			List[str]=[16, 48, 96]				,				__lowerCAmelCase:			Optional[Any]=[1, 3, 6]				,				__lowerCAmelCase:			List[Any]=[1, 2, 10]				,				__lowerCAmelCase:			Any=[7, 3, 3]				,				__lowerCAmelCase:			List[Any]=[4, 2, 2]				,				__lowerCAmelCase:			List[Any]=[2, 1, 1]				,				__lowerCAmelCase:			Dict=[2, 2, 2]				,				__lowerCAmelCase:			Union[str, Any]=[False, False, True]				,				__lowerCAmelCase:			Optional[Any]=[0.0, 0.0, 0.0]				,				__lowerCAmelCase:			Dict=0.02				,				__lowerCAmelCase:			Tuple=1E-12				,				__lowerCAmelCase:			Optional[Any]=True				,				__lowerCAmelCase:			Optional[Any]=True				,				__lowerCAmelCase:			Union[str, Any]=2				,				) -> Optional[int]:




    '''simple docstring'''




    __UpperCAmelCase						    =       parent
    __UpperCAmelCase						    =       batch_size
    __UpperCAmelCase						    =       image_size
    __UpperCAmelCase						    =       patch_sizes
    __UpperCAmelCase						    =       patch_stride
    __UpperCAmelCase						    =       patch_padding
    __UpperCAmelCase						    =       is_training
    __UpperCAmelCase						    =       use_labels
    __UpperCAmelCase						    =       num_labels
    __UpperCAmelCase						    =       num_channels
    __UpperCAmelCase						    =       embed_dim
    __UpperCAmelCase						    =       num_heads
    __UpperCAmelCase						    =       stride_kv
    __UpperCAmelCase						    =       depth
    __UpperCAmelCase						    =       cls_token
    __UpperCAmelCase						    =       attention_drop_rate
    __UpperCAmelCase						    =       initializer_range
    __UpperCAmelCase						    =       layer_norm_eps






  def 	_UpperCAmelCase				(				self:			str   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

    __UpperCAmelCase						    =       None
    if self.use_labels:
      # create a random int32 tensor of given shape
      __UpperCAmelCase						    =       ids_tensor([self.batch_size]				,				self.num_labels   )

    __UpperCAmelCase						    =       self.get_config()
    return config, pixel_values, labels






  def 	_UpperCAmelCase				(				self:			Dict   ) -> List[Any]:




    '''simple docstring'''




    return CvtConfig(
        image_size=self.image_size				,				num_labels=self.num_labels				,				num_channels=self.num_channels				,				embed_dim=self.embed_dim				,				num_heads=self.num_heads				,				patch_sizes=self.patch_sizes				,				patch_padding=self.patch_padding				,				patch_stride=self.patch_stride				,				stride_kv=self.stride_kv				,				depth=self.depth				,				cls_token=self.cls_token				,				attention_drop_rate=self.attention_drop_rate				,				initializer_range=self.initializer_range				,				)






  def 	_UpperCAmelCase				(				self:			Tuple				,				__lowerCAmelCase:			Tuple				,				__lowerCAmelCase:			Any				,				__lowerCAmelCase:			Dict   ) -> Optional[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFCvtModel(config=_a   )
    __UpperCAmelCase						    =       model(_a				,				training=_a   )
    __UpperCAmelCase						    =       (self.image_size, self.image_size)
    __UpperCAmelCase						    =       image_size[0], image_size[1]
    for i in range(len(self.depth   )   ):
      __UpperCAmelCase						    =       floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
      __UpperCAmelCase						    =       floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1   )
    self.parent.assertEqual(result.last_hidden_state.shape				,				(self.batch_size, self.embed_dim[-1], height, width)   )






  def 	_UpperCAmelCase				(				self:			Union[str, Any]				,				__lowerCAmelCase:			List[str]				,				__lowerCAmelCase:			List[Any]				,				__lowerCAmelCase:			Optional[int]   ) -> List[str]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.num_labels
    __UpperCAmelCase						    =       TFCvtForImageClassification(_a   )
    __UpperCAmelCase						    =       model(_a				,				labels=_a				,				training=_a   )
    self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels)   )






  def 	_UpperCAmelCase				(				self:			List[str]   ) -> Tuple:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.prepare_config_and_inputs()
    __UpperCAmelCase						    =       config_and_inputs
    __UpperCAmelCase						    =       {"""pixel_values""": pixel_values}
    return config, inputs_dict






@require_tf
class 			UpperCAmelCase__    (		UpperCamelCase__		,	UpperCamelCase__		,	unittest.TestCase					):



  """simple docstring"""






  lowerCAmelCase__		:	int    		= (TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
  lowerCAmelCase__		:	int    		= (
      {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
      if is_tf_available()
      else {}
  )
  lowerCAmelCase__		:	Dict    		= False
  lowerCAmelCase__		:	Optional[int]    		= False
  lowerCAmelCase__		:	int    		= False
  lowerCAmelCase__		:	str    		= False
  lowerCAmelCase__		:	Tuple    		= False






  def 	_UpperCAmelCase				(				self:			List[Any]   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFCvtModelTester(self   )
    __UpperCAmelCase						    =       TFCvtConfigTester(self				,				config_class=_a				,				has_text_modality=_a				,				hidden_size=37   )






  def 	_UpperCAmelCase				(				self:			Dict   ) -> str:




    '''simple docstring'''




    self.config_tester.create_and_test_config_common_properties()
    self.config_tester.create_and_test_config_to_json_string()
    self.config_tester.create_and_test_config_to_json_file()
    self.config_tester.create_and_test_config_from_and_save_pretrained()
    self.config_tester.create_and_test_config_with_num_labels()
    self.config_tester.check_config_can_be_init_without_params()
    self.config_tester.check_config_arguments_init()






  @unittest.skip(reason="Cvt does not output attentions"   )
  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> List[str]:




    '''simple docstring'''




    pass






  @unittest.skip(reason="Cvt does not use inputs_embeds"   )
  def 	_UpperCAmelCase				(				self:			List[str]   ) -> List[Any]:




    '''simple docstring'''




    pass






  @unittest.skip(reason="Cvt does not support input and output embeddings"   )
  def 	_UpperCAmelCase				(				self:			List[Any]   ) -> str:




    '''simple docstring'''




    pass






  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices("GPU"   )   ) == 0				,				reason="TF does not support backprop for grouped convolutions on CPU."				,				)
  def 	_UpperCAmelCase				(				self:			Dict   ) -> Union[str, Any]:




    '''simple docstring'''




    super().test_dataset_conversion()






  @unittest.skipIf(
      not is_tf_available() or len(tf.config.list_physical_devices("GPU"   )   ) == 0				,				reason="TF does not support backprop for grouped convolutions on CPU."				,				)
  @slow
  def 	_UpperCAmelCase				(				self:			Tuple   ) -> List[str]:




    '''simple docstring'''




    super().test_keras_fit()






  @unittest.skip(reason="Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8"   )
  def 	_UpperCAmelCase				(				self:			Any   ) -> int:




    '''simple docstring'''




    __UpperCAmelCase						    =       tf.keras.mixed_precision.Policy("mixed_float16"   )
    tf.keras.mixed_precision.set_global_policy(_a   )
    super().test_keras_fit()
    tf.keras.mixed_precision.set_global_policy("float32"   )






  def 	_UpperCAmelCase				(				self:			str   ) -> Union[str, Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       model_class(_a   )
      __UpperCAmelCase						    =       inspect.signature(model.call   )
      # signature.parameters is an OrderedDict => so arg_names order is deterministic
      __UpperCAmelCase						    =       [*signature.parameters.keys()]

      __UpperCAmelCase						    =       ["""pixel_values"""]
      self.assertListEqual(arg_names[:1]				,				_a   )






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




    '''simple docstring'''




    def check_hidden_states_output(__lowerCAmelCase:			Any				,				__lowerCAmelCase:			Union[str, Any]				,				__lowerCAmelCase:			Union[str, Any]   ):
      __UpperCAmelCase						    =       model_class(_a   )

      __UpperCAmelCase						    =       model(**self._prepare_for_class(_a				,				_a   )   )
      __UpperCAmelCase						    =       outputs.hidden_states

      __UpperCAmelCase						    =       len(self.model_tester.depth   )
      self.assertEqual(len(_a   )				,				_a   )

      # verify the first hidden states (first block)
      self.assertListEqual(
          list(hidden_states[0].shape[-3:]   )				,				[
              self.model_tester.embed_dim[0],
              self.model_tester.image_size // 4,
              self.model_tester.image_size // 4,
          ]				,				)

    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
      __UpperCAmelCase						    =       True
      check_hidden_states_output(_a				,				_a				,				_a   )

      # check that output_hidden_states also work using config
      del inputs_dict["output_hidden_states"]
      __UpperCAmelCase						    =       True

      check_hidden_states_output(_a				,				_a				,				_a   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*_a   )






  def 	_UpperCAmelCase				(				self:			Optional[Any]   ) -> Tuple:




    '''simple docstring'''




    __UpperCAmelCase						    =       self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*_a   )






  @slow
  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> Any:




    '''simple docstring'''




    for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      __UpperCAmelCase						    =       TFCvtModel.from_pretrained(_a   )
      self.assertIsNotNone(_a   )






def       __lowerCAmelCase      (						)    ->  Dict:
  __UpperCAmelCase						    =       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"					)
  return image





@require_tf
@require_vision
class 			UpperCAmelCase__    (		unittest.TestCase					):



  """simple docstring"""






  @cached_property
  def 	_UpperCAmelCase				(				self:			Union[str, Any]   ) -> int:




    '''simple docstring'''




    return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )






  @slow
  def 	_UpperCAmelCase				(				self:			Any   ) -> Tuple:




    '''simple docstring'''




    __UpperCAmelCase						    =       TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]   )

    __UpperCAmelCase						    =       self.default_image_processor
    __UpperCAmelCase						    =       prepare_img()
    __UpperCAmelCase						    =       image_processor(images=_a				,				return_tensors="tf"   )

    # forward pass
    __UpperCAmelCase						    =       model(**_a   )

    # verify the logits
    __UpperCAmelCase						    =       tf.TensorShape((1, 1_000)   )
    self.assertEqual(outputs.logits.shape				,				_a   )

    __UpperCAmelCase						    =       tf.constant([0.9285, 0.9015, -0.3150]   )
    self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()				,				_a				,				atol=1E-4   )   )




import gc
import unittest

import numpy as np
import torch

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, TransformeraDModel
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
    CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 			UpperCAmelCase__    (		snake_case		,	unittest.TestCase					):



  """simple docstring"""






  lowerCAmelCase__		:	int    		= DiTPipeline
  lowerCAmelCase__		:	Any    		= CLASS_CONDITIONED_IMAGE_GENERATION_PARAMS
  lowerCAmelCase__		:	Optional[Any]    		= PipelineTesterMixin.required_optional_params - {
      'latents',
      'num_images_per_prompt',
      'callback',
      'callback_steps',
  }
  lowerCAmelCase__		:	List[Any]    		= CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS
  lowerCAmelCase__		:	Optional[int]    		= False






  def 	_UpperCAmelCase				(				self:			str   ) -> List[Any]:




    '''simple docstring'''




    torch.manual_seed(0   )
    __UpperCAmelCase						    =       TransformeraDModel(
        sample_size=16				,				num_layers=2				,				patch_size=4				,				attention_head_dim=8				,				num_attention_heads=2				,				in_channels=4				,				out_channels=8				,				attention_bias=__lowerCAmelCase				,				activation_fn="gelu-approximate"				,				num_embeds_ada_norm=1_000				,				norm_type="ada_norm_zero"				,				norm_elementwise_affine=__lowerCAmelCase				,				)
    __UpperCAmelCase						    =       AutoencoderKL()
    __UpperCAmelCase						    =       DDIMScheduler()
    __UpperCAmelCase						    =       {"transformer": transformer.eval(), "vae": vae.eval(), "scheduler": scheduler}
    return components






  def 	_UpperCAmelCase				(				self:			str				,				__lowerCAmelCase:			str				,				__lowerCAmelCase:			Optional[int]=0   ) -> int:




    '''simple docstring'''




    if str(__lowerCAmelCase   ).startswith("mps"   ):
      __UpperCAmelCase						    =       torch.manual_seed(__lowerCAmelCase   )
    else:
      __UpperCAmelCase						    =       torch.Generator(device=__lowerCAmelCase   ).manual_seed(__lowerCAmelCase   )
    __UpperCAmelCase						    =       {
        "class_labels": [1],
        "generator": generator,
        "num_inference_steps": 2,
        "output_type": "numpy",
    }
    return inputs






  def 	_UpperCAmelCase				(				self:			Tuple   ) -> Dict:




    '''simple docstring'''




    __UpperCAmelCase						    =       "cpu"

    __UpperCAmelCase						    =       self.get_dummy_components()
    __UpperCAmelCase						    =       self.pipeline_class(**__lowerCAmelCase   )
    pipe.to(__lowerCAmelCase   )
    pipe.set_progress_bar_config(disable=__lowerCAmelCase   )

    __UpperCAmelCase						    =       self.get_dummy_inputs(__lowerCAmelCase   )
    __UpperCAmelCase						    =       pipe(**__lowerCAmelCase   ).images
    __UpperCAmelCase						    =       image[0, -3:, -3:, -1]

    self.assertEqual(image.shape				,				(1, 16, 16, 3)   )
    __UpperCAmelCase						    =       np.array([0.2946, 0.6601, 0.4329, 0.3296, 0.4144, 0.5319, 0.7273, 0.5013, 0.4457]   )
    __UpperCAmelCase						    =       np.abs(image_slice.flatten() - expected_slice   ).max()
    self.assertLessEqual(__lowerCAmelCase				,				1E-3   )






  def 	_UpperCAmelCase				(				self:			int   ) -> str:




    '''simple docstring'''




    self._test_inference_batch_single_identical(relax_max_difference=__lowerCAmelCase				,				expected_max_diff=1E-3   )






  @unittest.skipIf(
      torch_device != "cuda" or not is_xformers_available()				,				reason="XFormers attention is only available with CUDA and `xformers` installed"				,				)
  def 	_UpperCAmelCase				(				self:			Optional[int]   ) -> str:




    '''simple docstring'''




    self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=1E-3   )






@require_torch_gpu
@slow
class 			UpperCAmelCase__    (		unittest.TestCase					):



  """simple docstring"""






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




    '''simple docstring'''




    super().tearDown()
    gc.collect()
    torch.cuda.empty_cache()






  def 	_UpperCAmelCase				(				self:			Dict   ) -> List[Any]:




    '''simple docstring'''




    __UpperCAmelCase						    =       torch.manual_seed(0   )

    __UpperCAmelCase						    =       DiTPipeline.from_pretrained("facebook/DiT-XL-2-256"   )
    pipe.to("cuda"   )

    __UpperCAmelCase						    =       ["vase", "umbrella", "white shark", "white wolf"]
    __UpperCAmelCase						    =       pipe.get_label_ids(__lowerCAmelCase   )

    __UpperCAmelCase						    =       pipe(__lowerCAmelCase				,				generator=__lowerCAmelCase				,				num_inference_steps=40				,				output_type="np"   ).images

    for word, image in zip(__lowerCAmelCase				,				__lowerCAmelCase   ):
      __UpperCAmelCase						    =       load_numpy(
          F'''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/dit/{word}.npy'''   )
      assert np.abs((expected_image - image).max()   ) < 1E-2






  def 	_UpperCAmelCase				(				self:			Any   ) -> List[str]:




    '''simple docstring'''




    __UpperCAmelCase						    =       DiTPipeline.from_pretrained("facebook/DiT-XL-2-512"   )
    __UpperCAmelCase						    =       DPMSolverMultistepScheduler.from_config(pipe.scheduler.config   )
    pipe.to("cuda"   )

    __UpperCAmelCase						    =       ["vase", "umbrella"]
    __UpperCAmelCase						    =       pipe.get_label_ids(__lowerCAmelCase   )

    __UpperCAmelCase						    =       torch.manual_seed(0   )
    __UpperCAmelCase						    =       pipe(__lowerCAmelCase				,				generator=__lowerCAmelCase				,				num_inference_steps=25				,				output_type="np"   ).images

    for word, image in zip(__lowerCAmelCase				,				__lowerCAmelCase   ):
      __UpperCAmelCase						    =       load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
          F'''/dit/{word}_512.npy'''   )

      assert np.abs((expected_image - image).max()   ) < 1E-1



