

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 A__    ( _lowerCAmelCase ):



 """simple docstring"""







 def    __magic_name__       (		self :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    self.config_class(**self.inputs_dict       )
      self.parent.assertTrue(hasattr(_lowerCAmelCase     ,						"embed_dim"       )       )
      self.parent.assertTrue(hasattr(_lowerCAmelCase     ,						"num_heads"       )       )





class A__    :



 """simple docstring"""







 def __init__(		self :	int     ,						A_ :	Any     ,						A_ :	List[str]=1_3     ,						A_ :	Optional[Any]=6_4     ,						A_ :	Optional[Any]=3     ,						A_ :	Optional[int]=[1_6, 4_8, 9_6]     ,						A_ :	int=[1, 3, 6]     ,						A_ :	int=[1, 2, 1_0]     ,						A_ :	Optional[Any]=[7, 3, 3]     ,						A_ :	Union[str, Any]=[4, 2, 2]     ,						A_ :	Optional[int]=[2, 1, 1]     ,						A_ :	List[Any]=[2, 2, 2]     ,						A_ :	Optional[Any]=[False, False, True]     ,						A_ :	Optional[Any]=[0.0, 0.0, 0.0]     ,						A_ :	Dict=0.02     ,						A_ :	Union[str, Any]=1E-12     ,						A_ :	Tuple=True     ,						A_ :	Any=True     ,						A_ :	Tuple=2     ,						):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    parent
      _lowerCAmelCase   :					List[Any]        =    batch_size
      _lowerCAmelCase   :					Optional[Any]        =    image_size
      _lowerCAmelCase   :					Optional[Any]        =    patch_sizes
      _lowerCAmelCase   :					Optional[Any]        =    patch_stride
      _lowerCAmelCase   :					List[str]        =    patch_padding
      _lowerCAmelCase   :					Dict        =    is_training
      _lowerCAmelCase   :					str        =    use_labels
      _lowerCAmelCase   :					Optional[int]        =    num_labels
      _lowerCAmelCase   :					List[Any]        =    num_channels
      _lowerCAmelCase   :					List[str]        =    embed_dim
      _lowerCAmelCase   :					int        =    num_heads
      _lowerCAmelCase   :					Union[str, Any]        =    stride_kv
      _lowerCAmelCase   :					Union[str, Any]        =    depth
      _lowerCAmelCase   :					Any        =    cls_token
      _lowerCAmelCase   :					Optional[int]        =    attention_drop_rate
      _lowerCAmelCase   :					Optional[int]        =    initializer_range
      _lowerCAmelCase   :					List[Any]        =    layer_norm_eps

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

      _lowerCAmelCase   :					Optional[Any]        =    self.get_config()
      return config, pixel_values, labels

 def    __magic_name__       (		self :	Tuple       ):






      '''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    __magic_name__       (		self :	List[Any]     ,						A_ :	Union[str, Any]     ,						A_ :	List[str]     ,						A_ :	List[str]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Dict        =    TFCvtModel(config=_lowerCAmelCase       )
      _lowerCAmelCase   :					Optional[Any]        =    model(_lowerCAmelCase     ,						training=_lowerCAmelCase       )
      _lowerCAmelCase   :					Tuple        =    (self.image_size, self.image_size)
      _lowerCAmelCase      ,			_lowerCAmelCase   :					Union[str, Any]        =    image_size[0], image_size[1]
      for i in range(len(self.depth       )       ):
           _lowerCAmelCase   :					int        =    floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1       )
           _lowerCAmelCase   :					Union[str, Any]        =    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    __magic_name__       (		self :	Tuple     ,						A_ :	Any     ,						A_ :	Tuple     ,						A_ :	Optional[Any]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Any        =    self.num_labels
      _lowerCAmelCase   :					int        =    TFCvtForImageClassification(_lowerCAmelCase       )
      _lowerCAmelCase   :					Tuple        =    model(_lowerCAmelCase     ,						labels=_lowerCAmelCase     ,						training=_lowerCAmelCase       )
      self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels)       )


 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[int]        =    self.prepare_config_and_inputs()
      _lowerCAmelCase      ,			_lowerCAmelCase      ,			_lowerCAmelCase   :					Union[str, Any]        =    config_and_inputs
      _lowerCAmelCase   :					int        =    {"pixel_values": pixel_values}
      return config, inputs_dict





@require_tf
class A__    ( _lowerCAmelCase  ,		_lowerCAmelCase  ,		unittest.TestCase ):



 """simple docstring"""







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

 def    __magic_name__       (		self :	str       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    TFCvtModelTester(self       )
      _lowerCAmelCase   :					Union[str, Any]        =    TFCvtConfigTester(self     ,						config_class=_lowerCAmelCase     ,						has_text_modality=_lowerCAmelCase     ,						hidden_size=3_7       )

 def    __magic_name__       (		self :	Dict       ):






      '''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    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      pass

 @unittest.skip(reason="Cvt does not use inputs_embeds"       )
 def    __magic_name__       (		self :	Union[str, Any]       ):






      '''simple docstring'''




      pass

 @unittest.skip(reason="Cvt does not support input and output embeddings"       )
 def    __magic_name__       (		self :	Optional[Any]       ):






      '''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    __magic_name__       (		self :	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    __magic_name__       (		self :	Tuple       ):






      '''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    __magic_name__       (		self :	Any       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    tf.keras.mixed_precision.Policy("mixed_float16"       )
      tf.keras.mixed_precision.set_global_policy(_lowerCAmelCase       )
      super().test_keras_fit()
      tf.keras.mixed_precision.set_global_policy("float32"       )

 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      _lowerCAmelCase      ,			_lowerCAmelCase   :					Any        =    self.model_tester.prepare_config_and_inputs_for_common()

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

           _lowerCAmelCase   :					Any        =    ["pixel_values"]
           self.assertListEqual(arg_names[:1]     ,						_lowerCAmelCase       )

 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      def check_hidden_states_output(A_ :	List[Any]     ,						A_ :	List[Any]     ,						A_ :	Any       ):
           _lowerCAmelCase   :					Union[str, Any]        =    model_class(_lowerCAmelCase       )

           _lowerCAmelCase   :					Optional[Any]        =    model(**self._prepare_for_class(_lowerCAmelCase     ,						_lowerCAmelCase       )       )
           _lowerCAmelCase   :					Optional[Any]        =    outputs.hidden_states

           _lowerCAmelCase   :					Dict        =    len(self.model_tester.depth       )
           self.assertEqual(len(_lowerCAmelCase       )     ,						_lowerCAmelCase       )

           # 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,
               ]     ,						)

      _lowerCAmelCase      ,			_lowerCAmelCase   :					Dict        =    self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           _lowerCAmelCase   :					Union[str, Any]        =    True
           check_hidden_states_output(_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase       )

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           _lowerCAmelCase   :					Optional[Any]        =    True

           check_hidden_states_output(_lowerCAmelCase     ,						_lowerCAmelCase     ,						_lowerCAmelCase       )

 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Union[str, Any]        =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_lowerCAmelCase       )

 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      _lowerCAmelCase   :					int        =    self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase       )


 @slow
 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




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







def 	_snake_case		(    )       ->      int:

     """simple docstring"""







     _lowerCAmelCase   :					Union[str, Any]        =    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
     return image





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



 """simple docstring"""







 @cached_property
 def    __magic_name__       (		self :	Optional[int]       ):






      '''simple docstring'''




      return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )


 @slow
 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      _lowerCAmelCase   :					str        =    TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]       )

      _lowerCAmelCase   :					int        =    self.default_image_processor
      _lowerCAmelCase   :					int        =    prepare_img()
      _lowerCAmelCase   :					Tuple        =    image_processor(images=_lowerCAmelCase     ,						return_tensors="tf"       )

      # forward pass
      _lowerCAmelCase   :					Union[str, Any]        =    model(**_lowerCAmelCase       )

      # verify the logits
      _lowerCAmelCase   :					Tuple        =    tf.TensorShape((1, 1_0_0_0)       )
      self.assertEqual(outputs.logits.shape     ,						_lowerCAmelCase       )

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









import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()





@skip_mps
class A__    ( A  ,		unittest.TestCase ):



 """simple docstring"""







 _lowercase     :					str      =						TextToVideoSDPipeline
 _lowercase     :					str      =						TEXT_TO_IMAGE_PARAMS
 _lowercase     :					Dict      =						TEXT_TO_IMAGE_BATCH_PARAMS
 # No `output_type`.
 _lowercase     :					Union[str, Any]      =						frozenset(
     [
         '''num_inference_steps''',
         '''generator''',
         '''latents''',
         '''return_dict''',
         '''callback''',
         '''callback_steps''',
     ] )

 def    __magic_name__       (		self :	Any       ):






      '''simple docstring'''




      torch.manual_seed(0       )
      _lowerCAmelCase   :					Union[str, Any]        =    UNetaDConditionModel(
          block_out_channels=(3_2, 6_4, 6_4, 6_4)     ,						layers_per_block=2     ,						sample_size=3_2     ,						in_channels=4     ,						out_channels=4     ,						down_block_types=("CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "CrossAttnDownBlock3D", "DownBlock3D")     ,						up_block_types=("UpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D", "CrossAttnUpBlock3D")     ,						cross_attention_dim=3_2     ,						attention_head_dim=4     ,						)
      _lowerCAmelCase   :					Any        =    DDIMScheduler(
          beta_start=0.00085     ,						beta_end=0.012     ,						beta_schedule="scaled_linear"     ,						clip_sample=A_     ,						set_alpha_to_one=A_     ,						)
      torch.manual_seed(0       )
      _lowerCAmelCase   :					Dict        =    AutoencoderKL(
          block_out_channels=[3_2, 6_4]     ,						in_channels=3     ,						out_channels=3     ,						down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]     ,						up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]     ,						latent_channels=4     ,						sample_size=1_2_8     ,						)
      torch.manual_seed(0       )
      _lowerCAmelCase   :					Union[str, Any]        =    CLIPTextConfig(
          bos_token_id=0     ,						eos_token_id=2     ,						hidden_size=3_2     ,						intermediate_size=3_7     ,						layer_norm_eps=1E-05     ,						num_attention_heads=4     ,						num_hidden_layers=5     ,						pad_token_id=1     ,						vocab_size=1_0_0_0     ,						hidden_act="gelu"     ,						projection_dim=5_1_2     ,						)
      _lowerCAmelCase   :					Any        =    CLIPTextModel(A_       )
      _lowerCAmelCase   :					Union[str, Any]        =    CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"       )

      _lowerCAmelCase   :					Dict        =    {
          "unet": unet,
          "scheduler": scheduler,
          "vae": vae,
          "text_encoder": text_encoder,
          "tokenizer": tokenizer,
      }
      return components

 def    __magic_name__       (		self :	str     ,						A_ :	Union[str, Any]     ,						A_ :	List[str]=0       ):






      '''simple docstring'''




      if str(A_       ).startswith("mps"       ):
           _lowerCAmelCase   :					Any        =    torch.manual_seed(A_       )
      else:
           _lowerCAmelCase   :					Optional[int]        =    torch.Generator(device=A_       ).manual_seed(A_       )
      _lowerCAmelCase   :					List[str]        =    {
          "prompt": "A painting of a squirrel eating a burger",
          "generator": generator,
          "num_inference_steps": 2,
          "guidance_scale": 6.0,
          "output_type": "pt",
      }
      return inputs

 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Tuple        =    "cpu"  # ensure determinism for the device-dependent torch.Generator
      _lowerCAmelCase   :					Dict        =    self.get_dummy_components()
      _lowerCAmelCase   :					int        =    TextToVideoSDPipeline(**A_       )
      _lowerCAmelCase   :					Dict        =    sd_pipe.to(A_       )
      sd_pipe.set_progress_bar_config(disable=A_       )

      _lowerCAmelCase   :					Optional[Any]        =    self.get_dummy_inputs(A_       )
      _lowerCAmelCase   :					Any        =    "np"
      _lowerCAmelCase   :					List[str]        =    sd_pipe(**A_       ).frames
      _lowerCAmelCase   :					Optional[int]        =    frames[0][-3:, -3:, -1]

      assert frames[0].shape == (6_4, 6_4, 3)
      _lowerCAmelCase   :					str        =    np.array([158.0, 160.0, 153.0, 125.0, 100.0, 121.0, 111.0, 93.0, 113.0]       )

      assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2

 def    __magic_name__       (		self :	List[Any]       ):






      '''simple docstring'''




      self._test_attention_slicing_forward_pass(test_mean_pixel_difference=A_     ,						expected_max_diff=3E-3       )

 @unittest.skipIf(
     torch_device != "cuda" or not is_xformers_available()     ,						reason="XFormers attention is only available with CUDA and `xformers` installed"     ,						)
 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=A_     ,						expected_max_diff=1E-2       )

 @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline."       )
 def    __magic_name__       (		self :	List[str]       ):






      '''simple docstring'''




      pass

 @unittest.skip(reason="Batching needs to be properly figured out first for this pipeline."       )
 def    __magic_name__       (		self :	int       ):






      '''simple docstring'''




      pass

 @unittest.skip(reason="`num_images_per_prompt` argument is not supported for this pipeline."       )
 def    __magic_name__       (		self :	str       ):






      '''simple docstring'''




      pass


 def    __magic_name__       (		self :	Optional[Any]       ):






      '''simple docstring'''




      return super().test_progress_bar()





@slow
@skip_mps
class A__    ( unittest.TestCase ):



 """simple docstring"""







 def    __magic_name__       (		self :	Tuple       ):






      '''simple docstring'''




      _lowerCAmelCase   :					Optional[Any]        =    load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy"       )

      _lowerCAmelCase   :					Dict        =    TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"       )
      _lowerCAmelCase   :					Optional[Any]        =    DPMSolverMultistepScheduler.from_config(pipe.scheduler.config       )
      _lowerCAmelCase   :					Optional[Any]        =    pipe.to("cuda"       )

      _lowerCAmelCase   :					Union[str, Any]        =    "Spiderman is surfing"
      _lowerCAmelCase   :					Any        =    torch.Generator(device="cpu"       ).manual_seed(0       )

      _lowerCAmelCase   :					Union[str, Any]        =    pipe(A_     ,						generator=A_     ,						num_inference_steps=2_5     ,						output_type="pt"       ).frames
      _lowerCAmelCase   :					Optional[Any]        =    video_frames.cpu().numpy()

      assert np.abs(expected_video - video       ).mean() < 5E-2


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






      '''simple docstring'''




      _lowerCAmelCase   :					Dict        =    load_numpy(
          "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy"       )

      _lowerCAmelCase   :					Optional[Any]        =    TextToVideoSDPipeline.from_pretrained("damo-vilab/text-to-video-ms-1.7b"       )
      _lowerCAmelCase   :					Optional[int]        =    pipe.to("cuda"       )

      _lowerCAmelCase   :					List[Any]        =    "Spiderman is surfing"
      _lowerCAmelCase   :					List[str]        =    torch.Generator(device="cpu"       ).manual_seed(0       )

      _lowerCAmelCase   :					List[Any]        =    pipe(A_     ,						generator=A_     ,						num_inference_steps=2     ,						output_type="pt"       ).frames
      _lowerCAmelCase   :					str        =    video_frames.cpu().numpy()

      assert np.abs(expected_video - video       ).mean() < 5E-2






