

from typing import List, Optional, Union

import torch
from transformers import (
    XLMRobertaTokenizer,
)

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDIMScheduler, DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .text_encoder import MultilingualCLIP


a_						     =    logging.get_logger(__name__)  # pylint: disable=invalid-name

a_						     =    '\n    Examples:\n        ```py\n        >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline\n        >>> import torch\n\n        >>> pipe_prior = KandinskyPriorPipeline.from_pretrained("kandinsky-community/Kandinsky-2-1-prior")\n        >>> pipe_prior.to("cuda")\n\n        >>> prompt = "red cat, 4k photo"\n        >>> out = pipe_prior(prompt)\n        >>> image_emb = out.image_embeds\n        >>> negative_image_emb = out.negative_image_embeds\n\n        >>> pipe = KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1")\n        >>> pipe.to("cuda")\n\n        >>> image = pipe(\n        ...     prompt,\n        ...     image_embeds=image_emb,\n        ...     negative_image_embeds=negative_image_emb,\n        ...     height=768,\n        ...     width=768,\n        ...     num_inference_steps=100,\n        ... ).images\n\n        >>> image[0].save("cat.png")\n        ```\n'

def  lowerCamelCase__ (					_a							,			_a							,			_a=8):
 SCREAMING_SNAKE_CASE   :      List[Any]				     =       h // scale_factor**2
 if h % scale_factor**2 != 0:
  new_h += 1
 SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       w // scale_factor**2
 if w % scale_factor**2 != 0:
  new_w += 1
 return new_h * scale_factor, new_w * scale_factor






class _UpperCamelCase							(							__A   ):
   '''simple docstring'''







   def __init__(			self							: Dict      ,      a							: MultilingualCLIP      ,      a							: XLMRobertaTokenizer      ,      a							: UNetaDConditionModel      ,      a							: Union[DDIMScheduler, DDPMScheduler]      ,      a							: VQModel      ,      )							->       str:

    """simple docstring"""

    super().__init__()

    self.register_modules(
        text_encoder=a      ,      tokenizer=a      ,      unet=a      ,      scheduler=a      ,      movq=a      ,      )
    SCREAMING_SNAKE_CASE   :      Any				     =       2 ** (len(self.movq.config.block_out_channels	) - 1)
   def 							__UpperCamelCase (			self							: List[str]      ,      a							: List[str]      ,      a							: List[str]      ,      a							: Optional[int]      ,      a							: Optional[Any]      ,      a							: Tuple      ,      a							: Dict	)							->       Optional[Any]:

    """simple docstring"""

    if latents is None:
     SCREAMING_SNAKE_CASE   :      List[str]				     =       randn_tensor(a      ,      generator=a      ,      device=a      ,      dtype=a	)
    else:
     if latents.shape != shape:
      raise ValueError(F"Unexpected latents shape, got {latents.shape}, expected {shape}"	)
     SCREAMING_SNAKE_CASE   :      Tuple				     =       latents.to(a	)

    SCREAMING_SNAKE_CASE   :      str				     =       latents * scheduler.init_noise_sigma
    return latents
   def 							__UpperCamelCase (			self							: List[str]      ,      a							: Dict      ,      a							: Dict      ,      a							: List[str]      ,      a							: Union[str, Any]      ,      a							: str=None      ,      )							->       Optional[int]:

    """simple docstring"""

    SCREAMING_SNAKE_CASE   :      Dict				     =       len(a	) if isinstance(a      ,      a	) else 1
    # get prompt text embeddings
    SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.tokenizer(
        a      ,      padding="max_length"      ,      truncation=a      ,      max_length=77      ,      return_attention_mask=a      ,      add_special_tokens=a      ,      return_tensors="pt"      ,      )

    SCREAMING_SNAKE_CASE   :      Dict				     =       text_inputs.input_ids
    SCREAMING_SNAKE_CASE   :      int				     =       self.tokenizer(a      ,      padding="longest"      ,      return_tensors="pt"	).input_ids

    if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(a      ,      a	):
     SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]	)
     logger.warning(
         "The following part of your input was truncated because CLIP can only handle sequences up to"
         F" {self.tokenizer.model_max_length} tokens: {removed_text}"	)

    SCREAMING_SNAKE_CASE   :      str				     =       text_input_ids.to(a	)
    SCREAMING_SNAKE_CASE   :      List[Any]				     =       text_inputs.attention_mask.to(a	)

    SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.text_encoder(
        input_ids=a      ,      attention_mask=a	)

    SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       prompt_embeds.repeat_interleave(a      ,      dim=0	)
    SCREAMING_SNAKE_CASE   :      Any				     =       text_encoder_hidden_states.repeat_interleave(a      ,      dim=0	)
    SCREAMING_SNAKE_CASE   :      Tuple				     =       text_mask.repeat_interleave(a      ,      dim=0	)

    if do_classifier_free_guidance:
     SCREAMING_SNAKE_CASE   :      List[str]
     if negative_prompt is None:
      SCREAMING_SNAKE_CASE   :      int				     =       [""] * batch_size
     elif type(a	) is not type(a	):
      raise TypeError(
          F"`negative_prompt` should be the same type to `prompt`, but got {type(a	)} !="
          F" {type(a	)}."	)
     elif isinstance(a      ,      a	):
      SCREAMING_SNAKE_CASE   :      Tuple				     =       [negative_prompt]
     elif batch_size != len(a	):
      raise ValueError(
          F"`negative_prompt`: {negative_prompt} has batch size {len(a	)}, but `prompt`:"
          F" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"
          " the batch size of `prompt`."	)
     else:
      SCREAMING_SNAKE_CASE   :      Any				     =       negative_prompt

     SCREAMING_SNAKE_CASE   :      Tuple				     =       self.tokenizer(
         a      ,      padding="max_length"      ,      max_length=77      ,      truncation=a      ,      return_attention_mask=a      ,      add_special_tokens=a      ,      return_tensors="pt"      ,      )
     SCREAMING_SNAKE_CASE   :      Tuple				     =       uncond_input.input_ids.to(a	)
     SCREAMING_SNAKE_CASE   :      str				     =       uncond_input.attention_mask.to(a	)

     SCREAMING_SNAKE_CASE   :      Dict				     =       self.text_encoder(
         input_ids=a      ,      attention_mask=a	)

     # duplicate unconditional embeddings for each generation per prompt, using mps friendly method

     SCREAMING_SNAKE_CASE   :      Optional[int]				     =       negative_prompt_embeds.shape[1]
     SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       negative_prompt_embeds.repeat(1      ,      a	)
     SCREAMING_SNAKE_CASE   :      str				     =       negative_prompt_embeds.view(batch_size * num_images_per_prompt      ,      a	)

     SCREAMING_SNAKE_CASE   :      str				     =       uncond_text_encoder_hidden_states.shape[1]
     SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       uncond_text_encoder_hidden_states.repeat(1      ,      a      ,      1	)
     SCREAMING_SNAKE_CASE   :      Tuple				     =       uncond_text_encoder_hidden_states.view(
         batch_size * num_images_per_prompt      ,      a      ,      -1	)
     SCREAMING_SNAKE_CASE   :      Dict				     =       uncond_text_mask.repeat_interleave(a      ,      dim=0	)

     # done duplicates

     # For classifier free guidance, we need to do two forward passes.
     # Here we concatenate the unconditional and text embeddings into a single batch
     # to avoid doing two forward passes
     SCREAMING_SNAKE_CASE   :      Dict				     =       torch.cat([negative_prompt_embeds, prompt_embeds]	)
     SCREAMING_SNAKE_CASE   :      List[str]				     =       torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]	)

     SCREAMING_SNAKE_CASE   :      str				     =       torch.cat([uncond_text_mask, text_mask]	)

    return prompt_embeds, text_encoder_hidden_states, text_mask
   def 							__UpperCamelCase (			self							: List[Any]      ,      a							: Any=0	)							->       Any:

    """simple docstring"""

    if is_accelerate_available():
     from accelerate import cpu_offload
    else:
     raise ImportError("Please install accelerate via `pip install accelerate`"	)

    SCREAMING_SNAKE_CASE   :      List[str]				     =       torch.device(F"cuda:{gpu_id}"	)

    SCREAMING_SNAKE_CASE   :      Any				     =       [
        self.unet,
        self.text_encoder,
        self.movq,
    ]
    for cpu_offloaded_model in models:
     if cpu_offloaded_model is not None:
      cpu_offload(a      ,      a	)
   def 							__UpperCamelCase (			self							: Union[str, Any]      ,      a							: List[Any]=0	)							->       Optional[Any]:

    """simple docstring"""

    if is_accelerate_available() and is_accelerate_version(">="      ,      "0.17.0.dev0"	):
     from accelerate import cpu_offload_with_hook
    else:
     raise ImportError("`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."	)

    SCREAMING_SNAKE_CASE   :      List[str]				     =       torch.device(F"cuda:{gpu_id}"	)

    if self.device.type != "cpu":
     self.to("cpu"      ,      silence_dtype_warnings=a	)
     torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

    SCREAMING_SNAKE_CASE   :      List[Any]				     =       None
    for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
     SCREAMING_SNAKE_CASE   :      int				     =       cpu_offload_with_hook(a      ,      a      ,      prev_module_hook=a	)

    if self.safety_checker is not None:
     SCREAMING_SNAKE_CASE   :      Tuple				     =       cpu_offload_with_hook(self.safety_checker      ,      a      ,      prev_module_hook=a	)

    # We'll offload the last model manually.
    SCREAMING_SNAKE_CASE   :      Dict				     =       hook
   @property
   # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
   def 							__UpperCamelCase (			self							: Tuple	)							->       Dict:

    """simple docstring"""

    if not hasattr(self.unet      ,      "_hf_hook"	):
     return self.device
    for module in self.unet.modules():
     if (
         hasattr(a      ,      "_hf_hook"	)
         and hasattr(module._hf_hook      ,      "execution_device"	)
         and module._hf_hook.execution_device is not None
     ):
      return torch.device(module._hf_hook.execution_device	)
    return self.device






   @torch.no_grad()
   @replace_example_docstring(a	)
   def __call__(			self							: Optional[int]      ,      a							: Union[str, List[str]]      ,      a							: Union[torch.FloatTensor, List[torch.FloatTensor]]      ,      a							: Union[torch.FloatTensor, List[torch.FloatTensor]]      ,      a							: Optional[Union[str, List[str]]] = None      ,      a							: int = 512      ,      a							: int = 512      ,      a							: int = 100      ,      a							: float = 4.0      ,      a							: int = 1      ,      a							: Optional[Union[torch.Generator, List[torch.Generator]]] = None      ,      a							: Optional[torch.FloatTensor] = None      ,      a							: Optional[str] = "pil"      ,      a							: bool = True      ,      )							->       Tuple:

    """simple docstring"""

    if isinstance(a      ,      a	):
     SCREAMING_SNAKE_CASE   :      Dict				     =       1
    elif isinstance(a      ,      a	):
     SCREAMING_SNAKE_CASE   :      List[str]				     =       len(a	)
    else:
     raise ValueError(F"`prompt` has to be of type `str` or `list` but is {type(a	)}"	)

    SCREAMING_SNAKE_CASE   :      Tuple				     =       self._execution_device

    SCREAMING_SNAKE_CASE   :      Any				     =       batch_size * num_images_per_prompt
    SCREAMING_SNAKE_CASE   :      Dict				     =       guidance_scale > 1.0

    SCREAMING_SNAKE_CASE   :      Dict				     =       self._encode_prompt(
        a      ,      a      ,      a      ,      a      ,      a	)

    if isinstance(a      ,      a	):
     SCREAMING_SNAKE_CASE   :      Any				     =       torch.cat(a      ,      dim=0	)
    if isinstance(a      ,      a	):
     SCREAMING_SNAKE_CASE   :      str				     =       torch.cat(a      ,      dim=0	)

    if do_classifier_free_guidance:
     SCREAMING_SNAKE_CASE   :      Any				     =       image_embeds.repeat_interleave(a      ,      dim=0	)
     SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       negative_image_embeds.repeat_interleave(a      ,      dim=0	)

    SCREAMING_SNAKE_CASE   :      str				     =       torch.cat([negative_image_embeds, image_embeds]      ,      dim=0	).to(
        dtype=prompt_embeds.dtype      ,      device=a	)

    self.scheduler.set_timesteps(a      ,      device=a	)
    SCREAMING_SNAKE_CASE   :      List[Any]				     =       self.scheduler.timesteps

    SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.unet.config.in_channels

    SCREAMING_SNAKE_CASE   :      List[Any]				     =       get_new_h_w(a      ,      a      ,      self.movq_scale_factor	)

    # create initial latent
    SCREAMING_SNAKE_CASE   :      str				     =       self.prepare_latents(
        (batch_size, num_channels_latents, height, width)      ,      text_encoder_hidden_states.dtype      ,      a      ,      a      ,      a      ,      self.scheduler      ,      )

    for i, t in enumerate(self.progress_bar(a	)	):
     # expand the latents if we are doing classifier free guidance
     SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       torch.cat([latents] * 2	) if do_classifier_free_guidance else latents

     SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       {"text_embeds": prompt_embeds, "image_embeds": image_embeds}
     SCREAMING_SNAKE_CASE   :      Dict				     =       self.unet(
         sample=a      ,      timestep=a      ,      encoder_hidden_states=a      ,      added_cond_kwargs=a      ,      return_dict=a      ,      )[0]

     if do_classifier_free_guidance:
      SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       noise_pred.split(latents.shape[1]      ,      dim=1	)
      SCREAMING_SNAKE_CASE   :      Tuple				     =       noise_pred.chunk(2	)
      SCREAMING_SNAKE_CASE   :      List[Any]				     =       variance_pred.chunk(2	)
      SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
      SCREAMING_SNAKE_CASE   :      str				     =       torch.cat([noise_pred, variance_pred_text]      ,      dim=1	)

     if not (
         hasattr(self.scheduler.config      ,      "variance_type"	)
         and self.scheduler.config.variance_type in ["learned", "learned_range"]
     ):
      SCREAMING_SNAKE_CASE   :      Optional[int]				     =       noise_pred.split(latents.shape[1]      ,      dim=1	)

     # compute the previous noisy sample x_t -> x_t-1
     SCREAMING_SNAKE_CASE   :      Any				     =       self.scheduler.step(
         a      ,      a      ,      a      ,      generator=a      ,      ).prev_sample
    # post-processing
    SCREAMING_SNAKE_CASE   :      Dict				     =       self.movq.decode(a      ,      force_not_quantize=a	)["sample"]

    if output_type not in ["pt", "np", "pil"]:
     raise ValueError(F"Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}"	)

    if output_type in ["np", "pil"]:
     SCREAMING_SNAKE_CASE   :      int				     =       image * 0.5 + 0.5
     SCREAMING_SNAKE_CASE   :      Tuple				     =       image.clamp(0      ,      1	)
     SCREAMING_SNAKE_CASE   :      Optional[int]				     =       image.cpu().permute(0      ,      2      ,      3      ,      1	).float().numpy()

    if output_type == "pil":
     SCREAMING_SNAKE_CASE   :      Tuple				     =       self.numpy_to_pil(a	)

    if not return_dict:
     return (image,)

    return ImagePipelineOutput(images=a	)


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							(							__A   ):
			'''simple docstring'''







			def 							__UpperCamelCase (			self							: Any	)							->       Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       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							: Dict      ,      a							: int      ,      a							: List[str]=13      ,      a							: Optional[int]=64      ,      a							: List[str]=3      ,      a							: Dict=[16, 48, 96]      ,      a							: List[Any]=[1, 3, 6]      ,      a							: int=[1, 2, 10]      ,      a							: List[str]=[7, 3, 3]      ,      a							: List[Any]=[4, 2, 2]      ,      a							: Tuple=[2, 1, 1]      ,      a							: Union[str, Any]=[2, 2, 2]      ,      a							: List[str]=[False, False, True]      ,      a							: int=[0.0, 0.0, 0.0]      ,      a							: Dict=0.02      ,      a							: List[Any]=1e-12      ,      a							: Tuple=True      ,      a							: Optional[int]=True      ,      a							: Union[str, Any]=2      ,      )							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       parent
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       batch_size
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       image_size
				SCREAMING_SNAKE_CASE   :      Dict				     =       patch_sizes
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       patch_stride
				SCREAMING_SNAKE_CASE   :      str				     =       patch_padding
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       is_training
				SCREAMING_SNAKE_CASE   :      Dict				     =       use_labels
				SCREAMING_SNAKE_CASE   :      List[str]				     =       num_labels
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       num_channels
				SCREAMING_SNAKE_CASE   :      Any				     =       embed_dim
				SCREAMING_SNAKE_CASE   :      str				     =       num_heads
				SCREAMING_SNAKE_CASE   :      Any				     =       stride_kv
				SCREAMING_SNAKE_CASE   :      Tuple				     =       depth
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       cls_token
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       attention_drop_rate
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       initializer_range
				SCREAMING_SNAKE_CASE   :      Tuple				     =       layer_norm_eps
			def 							__UpperCamelCase (			self							: str	)							->       Tuple:

				"""simple docstring"""

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

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

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.get_config()
				return config, pixel_values, labels
			def 							__UpperCamelCase (			self							: Optional[Any]	)							->       Optional[int]:

				"""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      ,      a							: str      ,      a							: List[str]      ,      a							: str	)							->       Dict:

				"""simple docstring"""

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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       self.num_labels
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       TFCvtForImageClassification(a	)
				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       model(a      ,      labels=a      ,      training=a	)
				self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.num_labels)	)






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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      int				     =       self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      List[str]				     =       config_and_inputs
				SCREAMING_SNAKE_CASE   :      str				     =       {"pixel_values": pixel_values}
				return config, inputs_dict






@require_tf
class _UpperCamelCase							(							__A							,	__A							,	unittest.TestCase   ):
			'''simple docstring'''







			lowerCamelCase__           =(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
			lowerCamelCase__           =(
			    {'feature-extraction': TFCvtModel, 'image-classification': TFCvtForImageClassification}
			    if is_tf_available()
			    else {}
			)
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			lowerCamelCase__           =False
			def 							__UpperCamelCase (			self							: List[Any]	)							->       int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[Any]				     =       TFCvtModelTester(self	)
				SCREAMING_SNAKE_CASE   :      List[Any]				     =       TFCvtConfigTester(self      ,      config_class=a      ,      has_text_modality=a      ,      hidden_size=37	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""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							: str	)							->       Optional[int]:

				"""simple docstring"""

				pass
			@unittest.skip(reason="Cvt does not use inputs_embeds"	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Optional[int]:

				"""simple docstring"""

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

				"""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							: int	)							->       Tuple:

				"""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							: List[str]	)							->       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							: Optional[int]	)							->       Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Tuple				     =       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							: List[str]	)							->       Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      Optional[int]				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      List[Any]				     =       model_class(a	)
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       inspect.signature(model.call	)
					# signature.parameters is an OrderedDict => so arg_names order is deterministic
					SCREAMING_SNAKE_CASE   :      Tuple				     =       [*signature.parameters.keys()]

					SCREAMING_SNAKE_CASE   :      Tuple				     =       ["pixel_values"]
					self.assertListEqual(arg_names[:1]      ,      a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       str:

				"""simple docstring"""

				def check_hidden_states_output(a							: List[str]      ,      a							: Union[str, Any]      ,      a							: List[str]	):
					SCREAMING_SNAKE_CASE   :      Tuple				     =       model_class(a	)

					SCREAMING_SNAKE_CASE   :      Optional[int]				     =       model(**self._prepare_for_class(a      ,      a	)	)
					SCREAMING_SNAKE_CASE   :      List[str]				     =       outputs.hidden_states

					SCREAMING_SNAKE_CASE   :      int				     =       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,
					    ]      ,      )

				SCREAMING_SNAKE_CASE     ,SCREAMING_SNAKE_CASE   :      List[str]				     =       self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
					SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       True
					check_hidden_states_output(a      ,      a      ,      a	)

					# check that output_hidden_states also work using config
					del inputs_dict["output_hidden_states"]
					SCREAMING_SNAKE_CASE   :      str				     =       True

					check_hidden_states_output(a      ,      a      ,      a	)
			def 							__UpperCamelCase (			self							: Any	)							->       Union[str, Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Dict				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a	)
			def 							__UpperCamelCase (			self							: Optional[int]	)							->       Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      List[str]				     =       self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*a	)






			@slow
			def 							__UpperCamelCase (			self							: Tuple	)							->       Optional[int]:

				"""simple docstring"""

				for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE   :      Tuple				     =       TFCvtModel.from_pretrained(a	)
					self.assertIsNotNone(a	)

def  lowerCamelCase__ (					):
	SCREAMING_SNAKE_CASE   :      Optional[int]				     =       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							: Tuple	)							->       Tuple:

				"""simple docstring"""

				return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)






			@slow
			def 							__UpperCamelCase (			self							: Dict	)							->       Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

				SCREAMING_SNAKE_CASE   :      Optional[Any]				     =       self.default_image_processor
				SCREAMING_SNAKE_CASE   :      Optional[int]				     =       prepare_img()
				SCREAMING_SNAKE_CASE   :      Union[str, Any]				     =       image_processor(images=a      ,      return_tensors="tf"	)

				# forward pass
				SCREAMING_SNAKE_CASE   :      List[str]				     =       model(**a	)

				# verify the logits
				SCREAMING_SNAKE_CASE   :      Dict				     =       tf.TensorShape((1, 1000)	)
				self.assertEqual(outputs.logits.shape      ,      a	)

				SCREAMING_SNAKE_CASE   :      Tuple				     =       tf.constant([0.9285, 0.9015, -0.3150]	)
				self.assertTrue(np.allclose(outputs.logits[0, :3].numpy()      ,      a      ,      atol=1e-4	)	)