






"""simple docstring"""
import warnings
from dataclasses import dataclass, field
from typing import Optional, Tuple

from .training_args import TrainingArguments
from .utils import cached_property, is_tf_available, logging, requires_backends


A_     :					Optional[int]							=     logging.get_logger(__name__)

if is_tf_available():
      import tensorflow as tf



@dataclass
class 						a_ (      snake_case_					):







      '''simple docstring'''

      lowerCamelCase__			:							Union[str, Any]					=      'tf'
      lowerCamelCase__			:							Optional[str]					=      field(
          default=snake_case_    ,		metadata={'help': 'Name of TPU'}    ,		)

      lowerCamelCase__			:							Optional[str]					=      field(
          default=snake_case_    ,		metadata={'help': 'Zone of TPU'}    ,		)

      lowerCamelCase__			:							Optional[str]					=      field(
          default=snake_case_    ,		metadata={'help': 'Name of Cloud TPU-enabled project'}    ,		)

      lowerCamelCase__			:							float					=      field(
          default=1.0    ,		metadata={'help': 'Power for the Polynomial decay LR scheduler.'}    ,		)

      lowerCamelCase__			:							bool					=      field(default=snake_case_    ,		metadata={'help': 'Whether to activate the XLA compilation or not'}					)




      @cached_property
      def      a__				(self   ):

       '''simple docstring'''



       requires_backends(self,	['tf']   )
       logger.info('Tensorflow: setting up strategy'   )

       lowerCamelCase__	:     Dict								=		tf.config.list_physical_devices('GPU'   )

       # Set to float16 at first
       if self.fpaa:
        tf.keras.mixed_precision.set_global_policy('mixed_float16'   )

       if self.no_cuda:
        lowerCamelCase__	:     List[Any]								=		tf.distribute.OneDeviceStrategy(device='/cpu:0'   )
       else:
        try:
         if self.tpu_name:
          lowerCamelCase__	:     Dict								=		tf.distribute.cluster_resolver.TPUClusterResolver(
              self.tpu_name,	zone=self.tpu_zone,	project=self.gcp_project   )
         else:
          lowerCamelCase__	:     Any								=		tf.distribute.cluster_resolver.TPUClusterResolver()
        except ValueError:
         if self.tpu_name:
          raise RuntimeError(f'''Couldn\'t connect to TPU {self.tpu_name}!'''   )
         else:
          lowerCamelCase__	:     List[str]								=		None

        if tpu:
         # Set to bfloat16 in case of TPU
         if self.fpaa:
          tf.keras.mixed_precision.set_global_policy('mixed_bfloat16'   )

         tf.config.experimental_connect_to_cluster(lowerCamelCase_   )
         tf.tpu.experimental.initialize_tpu_system(lowerCamelCase_   )

         lowerCamelCase__	:     Optional[Any]								=		tf.distribute.TPUStrategy(lowerCamelCase_   )

        elif len(lowerCamelCase_   ) == 0:
         lowerCamelCase__	:     Any								=		tf.distribute.OneDeviceStrategy(device='/cpu:0'   )
        elif len(lowerCamelCase_   ) == 1:
         lowerCamelCase__	:     List[str]								=		tf.distribute.OneDeviceStrategy(device='/gpu:0'   )
        elif len(lowerCamelCase_   ) > 1:
         # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
         lowerCamelCase__	:     Dict								=		tf.distribute.MirroredStrategy()
        else:
         raise ValueError('Cannot find the proper strategy, please check your environment properties.'   )

       return strategy




      @property
      def      a__				(self   ):

       '''simple docstring'''



       requires_backends(self,	['tf']   )
       return self._setup_strategy




      @property
      def      a__				(self   ):

       '''simple docstring'''



       requires_backends(self,	['tf']   )
       return self._setup_strategy.num_replicas_in_sync




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return False  # TF Logging is handled by Keras not the Trainer




      @property
      def      a__				(self   ):

       '''simple docstring'''



       if self.per_gpu_train_batch_size:
        logger.warning(
            'Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future '
            'version. Using `--per_device_train_batch_size` is preferred.'   )
       lowerCamelCase__	:     Union[str, Any]								=		self.per_gpu_train_batch_size or self.per_device_train_batch_size
       return per_device_batch_size * self.n_replicas




      @property
      def      a__				(self   ):

       '''simple docstring'''



       if self.per_gpu_eval_batch_size:
        logger.warning(
            'Using deprecated `--per_gpu_eval_batch_size` argument which will be removed in a future '
            'version. Using `--per_device_eval_batch_size` is preferred.'   )
       lowerCamelCase__	:     Union[str, Any]								=		self.per_gpu_eval_batch_size or self.per_device_eval_batch_size
       return per_device_batch_size * self.n_replicas





      @property
      def      a__				(self   ):

       '''simple docstring'''



       requires_backends(self,	['tf']   )
       warnings.warn(
           'The n_gpu argument is deprecated and will be removed in a future version, use n_replicas instead.',	lowerCamelCase_,	)
       return self._setup_strategy.num_replicas_in_sync









"""simple docstring"""
import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()



class 						a_ (      snake_case_    ,		unittest.TestCase					):







      '''simple docstring'''

      lowerCamelCase__			:							Optional[Any]					=      KandinskyPriorPipeline
      lowerCamelCase__			:							Any					=      ['prompt']
      lowerCamelCase__			:							Any					=      ['prompt', 'negative_prompt']
      lowerCamelCase__			:							str					=      [
          'num_images_per_prompt',
          'generator',
          'num_inference_steps',
          'latents',
          'negative_prompt',
          'guidance_scale',
          'output_type',
          'return_dict',
      ]
      lowerCamelCase__			:							Dict					=      False




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return 3_2




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return 3_2




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return self.time_input_dim




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return self.time_input_dim * 4




      @property
      def      a__				(self   ):

       '''simple docstring'''



       return 1_0_0




      @property
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Any								=		CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'   )
       return tokenizer




      @property
      def      a__				(self   ):

       '''simple docstring'''



       torch.manual_seed(0   )
       lowerCamelCase__	:     int								=		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=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,	)
       return CLIPTextModelWithProjection(lowerCamelCase_   )




      @property
      def      a__				(self   ):

       '''simple docstring'''



       torch.manual_seed(0   )

       lowerCamelCase__	:     Optional[int]								=		{
           'num_attention_heads': 2,
           'attention_head_dim': 1_2,
           'embedding_dim': self.text_embedder_hidden_size,
           'num_layers': 1,
       }

       lowerCamelCase__	:     Optional[int]								=		PriorTransformer(**lowerCamelCase_   )
       # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
       lowerCamelCase__	:     Dict								=		nn.Parameter(torch.ones(model.clip_std.shape   )   )
       return model




      @property
      def      a__				(self   ):

       '''simple docstring'''



       torch.manual_seed(0   )
       lowerCamelCase__	:     List[str]								=		CLIPVisionConfig(
           hidden_size=self.text_embedder_hidden_size,	image_size=2_2_4,	projection_dim=self.text_embedder_hidden_size,	intermediate_size=3_7,	num_attention_heads=4,	num_channels=3,	num_hidden_layers=5,	patch_size=1_4,	)

       lowerCamelCase__	:     int								=		CLIPVisionModelWithProjection(lowerCamelCase_   )
       return model




      @property
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Union[str, Any]								=		CLIPImageProcessor(
           crop_size=2_2_4,	do_center_crop=lowerCamelCase_,	do_normalize=lowerCamelCase_,	do_resize=lowerCamelCase_,	image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073],	image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711],	resample=3,	size=2_2_4,	)

       return image_processor




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		self.dummy_prior
       lowerCamelCase__	:     Optional[Any]								=		self.dummy_image_encoder
       lowerCamelCase__	:     Optional[Any]								=		self.dummy_text_encoder
       lowerCamelCase__	:     Union[str, Any]								=		self.dummy_tokenizer
       lowerCamelCase__	:     List[Any]								=		self.dummy_image_processor

       lowerCamelCase__	:     List[Any]								=		UnCLIPScheduler(
           variance_type='fixed_small_log',	prediction_type='sample',	num_train_timesteps=1_0_0_0,	clip_sample=lowerCamelCase_,	clip_sample_range=10.0,	)

       lowerCamelCase__	:     Union[str, Any]								=		{
           'prior': prior,
           'image_encoder': image_encoder,
           'text_encoder': text_encoder,
           'tokenizer': tokenizer,
           'scheduler': scheduler,
           'image_processor': image_processor,
       }

       return components




      def      a__				(self,	lowerCamelCase_,	lowerCamelCase_=0   ):

       '''simple docstring'''



       if str(lowerCamelCase_   ).startswith('mps'   ):
        lowerCamelCase__	:     List[Any]								=		torch.manual_seed(lowerCamelCase_   )
       else:
        lowerCamelCase__	:     Optional[int]								=		torch.Generator(device=lowerCamelCase_   ).manual_seed(lowerCamelCase_   )
       lowerCamelCase__	:     List[Any]								=		{
           'prompt': 'horse',
           'generator': generator,
           'guidance_scale': 4.0,
           'num_inference_steps': 2,
           'output_type': 'np',
       }
       return inputs




      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     List[str]								=		'cpu'

       lowerCamelCase__	:     str								=		self.get_dummy_components()

       lowerCamelCase__	:     Any								=		self.pipeline_class(**lowerCamelCase_   )
       lowerCamelCase__	:     Dict								=		pipe.to(lowerCamelCase_   )

       pipe.set_progress_bar_config(disable=lowerCamelCase_   )

       lowerCamelCase__	:     Tuple								=		pipe(**self.get_dummy_inputs(lowerCamelCase_   )   )
       lowerCamelCase__	:     Dict								=		output.image_embeds

       lowerCamelCase__	:     Optional[Any]								=		pipe(
           **self.get_dummy_inputs(lowerCamelCase_   ),	return_dict=lowerCamelCase_,	)[0]

       lowerCamelCase__	:     Dict								=		image[0, -1_0:]
       lowerCamelCase__	:     Tuple								=		image_from_tuple[0, -1_0:]

       assert image.shape == (1, 3_2)

       lowerCamelCase__	:     Dict								=		np.array(
           [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]   )

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




      @skip_mps
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     Optional[int]								=		torch_device == 'cpu'
       lowerCamelCase__	:     Any								=		True
       lowerCamelCase__	:     List[Any]								=		False

       self._test_inference_batch_single_identical(
           test_max_difference=lowerCamelCase_,	relax_max_difference=lowerCamelCase_,	test_mean_pixel_difference=lowerCamelCase_,	)





      @skip_mps
      def      a__				(self   ):

       '''simple docstring'''



       lowerCamelCase__	:     str								=		torch_device == 'cpu'
       lowerCamelCase__	:     int								=		False

       self._test_attention_slicing_forward_pass(
           test_max_difference=lowerCamelCase_,	test_mean_pixel_difference=lowerCamelCase_,	)

