


from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


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

def        lowerCAmelCase__ (			a__:		Union[str, Any]       )  ->		List[int]:


							'''simple docstring'''





							if isinstance(lowerCamelCase_			,  np.ndarray       ):
														return list(tensor.shape       )

							_UpperCAmelCase 		=			tf.shape(lowerCamelCase_       )

							if tensor.shape == tf.TensorShape(lowerCamelCase_       ):
														return dynamic

							_UpperCAmelCase 		=			tensor.shape.as_list()

							return [dynamic[i] if s is None else s for i, s in enumerate(lowerCamelCase_       )]

def        lowerCAmelCase__ (			a__:		List[Any]			,  a__:		Any = None			,  a__:		Union[str, Any] = None       )  ->		tf.Tensor:


							'''simple docstring'''





							return tf.nn.softmax(logits=logits + 1e-9			,  axis=lowerCamelCase_			,  name=lowerCamelCase_       )

def        lowerCAmelCase__ (			a__:		Any			,  a__:		Union[str, Any]			,  a__:		Optional[int]			,  a__:		Union[str, Any]=1e-5			,  a__:		Optional[int]=-1       )  ->		Tuple:


							'''simple docstring'''





							if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(lowerCamelCase_			,  lowerCamelCase_       ):
														raise NotImplementedError('Only 1D weight and bias tensors are supported for now, with only a single axis.'       )

							# Get mean and variance on the axis to be normalized
							_UpperCAmelCase 		=			tf.nn.moments(lowerCamelCase_			,  axes=[axis]			,  keepdims=lowerCamelCase_       )

							if axis != -1:
														# Reshape scale and weight to have the same rank as inputs, but with 1 dimensions
														# on every dimension except axis
														_UpperCAmelCase 		=			[1] * inputs.shape.rank
														_UpperCAmelCase 		=			shape_list(lowerCamelCase_       )[axis]
														_UpperCAmelCase 		=			tf.reshape(lowerCamelCase_			,  lowerCamelCase_       )
														_UpperCAmelCase 		=			tf.reshape(lowerCamelCase_			,  lowerCamelCase_       )

							# Compute layer normalization using the batch_normalization
							# function.
							_UpperCAmelCase 		=			tf.nn.batch_normalization(
							    lowerCamelCase_			,  lowerCamelCase_			,  lowerCamelCase_			,  offset=lowerCamelCase_			,  scale=lowerCamelCase_			,  variance_epsilon=lowerCamelCase_			,  )
							return outputs

def        lowerCAmelCase__ (			a__:		List[Any]			,  a__:		Optional[Any]=0			,  a__:		Optional[Any]=-1       )  ->		Optional[Any]:


							'''simple docstring'''





							if end_dim < 0:
														end_dim += input.shape.rank
							if start_dim < 0:
														start_dim += input.shape.rank

							if start_dim == end_dim:
														return input

							_UpperCAmelCase 		=			tf.shape(lowerCamelCase_       )
							_UpperCAmelCase 		=			tf.math.reduce_prod(in_shape[start_dim : end_dim + 1]       )
							_UpperCAmelCase 		=			tf.concat([in_shape[:start_dim], [flattened_dim], in_shape[end_dim + 1 :]]			,  axis=0       )
							return tf.reshape(lowerCamelCase_			,  lowerCamelCase_       )

def        lowerCAmelCase__ (			a__:		List[Any]       )  ->		tf.Tensor:


							'''simple docstring'''





							if not isinstance(lowerCamelCase_			,  tf.Tensor       ):
														_UpperCAmelCase 		=			tf.convert_to_tensor(lowerCamelCase_       )  # Catches stray NumPy inputs
							if encoder_attention_mask.shape.rank == 3:
														_UpperCAmelCase 		=			encoder_attention_mask[:, None, :, :]
							if encoder_attention_mask.shape.rank == 2:
														_UpperCAmelCase 		=			encoder_attention_mask[:, None, None, :]
							# T5 has a mask that can compare sequence ids, we can simulate this here with this transposition
							# Cf. https://github.com/tensorflow/mesh/blob/8d2465e9bc93129b913b5ccc6a59aa97abd96ec6/mesh_tensorflow
							# /transformer/transformer_layers.py#L270
							# encoder_extended_attention_mask = (encoder_extended_attention_mask ==
							# encoder_extended_attention_mask.transpose(-1, -2))
							_UpperCAmelCase 		=			(
							    tf.cast(1			,  encoder_attention_mask.dtype       ) - encoder_extended_attention_mask
							) * encoder_extended_attention_mask.dtype.min

							return encoder_extended_attention_mask

def        lowerCAmelCase__ (			a__:		Union[str, Any]			,  a__:		Optional[Any]			,  a__:		List[str] = "input_ids"       )  ->		None:


							'''simple docstring'''





							tf.debugging.assert_less(
							    lowerCamelCase_			,  tf.cast(lowerCamelCase_			,  dtype=tensor.dtype       )			,  message=(
							        F'''The maximum value of {tensor_name} ({tf.math.reduce_max(lowerCamelCase_       )}) must be smaller than the embedding '''
							        F'''layer\'s input dimension ({embed_dim}). The likely cause is some problem at tokenization time.'''
							    )			,  )

def        lowerCAmelCase__ (			a__:		Any			,  a__:		Tuple			,  a__:		List[Any]       )  ->		Optional[int]:


							'''simple docstring'''





							_UpperCAmelCase 		=			6_4_5_1_2
							# Check that no item in `data` is larger than `HDF5_OBJECT_HEADER_LIMIT`
							# because in that case even chunking the array would not make the saving
							# possible.
							_UpperCAmelCase 		=			[x for x in data if len(lowerCamelCase_       ) > HDF5_OBJECT_HEADER_LIMIT]

							# Expecting this to never be true.
							if bad_attributes:
														raise RuntimeError(
														    'The following attributes cannot be saved to HDF5 file because '
														    F'''they are larger than {HDF5_OBJECT_HEADER_LIMIT} '''
														    F'''bytes: {bad_attributes}'''       )

							_UpperCAmelCase 		=			np.asarray(lowerCamelCase_       )

							_UpperCAmelCase 		=			1
							_UpperCAmelCase 		=			np.array_split(lowerCamelCase_			,  lowerCamelCase_       )

							# This will never loop forever thanks to the test above.
							while any(x.nbytes > HDF5_OBJECT_HEADER_LIMIT for x in chunked_data       ):
														num_chunks += 1
														_UpperCAmelCase 		=			np.array_split(lowerCamelCase_			,  lowerCamelCase_       )

							if num_chunks > 1:
														for chunk_id, chunk_data in enumerate(lowerCamelCase_       ):
																					_UpperCAmelCase 		=			chunk_data
							else:
														_UpperCAmelCase 		=			data

def        lowerCAmelCase__ (			a__:		str			,  a__:		str       )  ->		List[Any]:


							'''simple docstring'''





							if name in group.attrs:
														_UpperCAmelCase 		=			[n.decode('utf8'       ) if hasattr(lowerCamelCase_			,  'decode'       ) else n for n in group.attrs[name]]
							else:
														_UpperCAmelCase 		=			[]
														_UpperCAmelCase 		=			0
														while "%s%d" % (name, chunk_id) in group.attrs:
																					data.extend(
																					    [n.decode('utf8'       ) if hasattr(lowerCamelCase_			,  'decode'       ) else n for n in group.attrs['%s%d' % (name, chunk_id)]]       )
																					chunk_id += 1
							return data







def        lowerCAmelCase__ (			a__:		Union[str, Any]       )  ->		Optional[Any]:


							'''simple docstring'''





							def _expand_single_ad_tensor(a__:		int       ):
														if isinstance(lowerCamelCase_			,  tf.Tensor       ) and t.shape.rank == 1:
																					return tf.expand_dims(lowerCamelCase_			,  axis=-1       )
														return t

							return tf.nest.map_structure(_expand_single_ad_tensor			,  lowerCamelCase_       )




import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizer

from diffusers import (
    AltDiffusionImgaImgPipeline,
    AutoencoderKL,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.pipelines.alt_diffusion.modeling_roberta_series import (
    RobertaSeriesConfig,
    RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()






class _lowerCamelCase(						unittest.TestCase   ):
       def 			UpperCamelCase     (					self)       ->   Optional[Any]:




           """simple docstring"""

           super().tearDown()
           gc.collect()
           torch.cuda.empty_cache()
       @property
       def 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           _lowercase       :							Optional[Any]      		=		1
           _lowercase       :							Any      		=		3
           _lowercase       :							Tuple      		=		(32, 32)

           _lowercase       :							Tuple      		=		floats_tensor((batch_size, num_channels) + sizes,      rng=random.Random(0)).to(lowerCamelCase)
           return image
       @property
       def 			UpperCamelCase     (					self)       ->   str:




           """simple docstring"""

           torch.manual_seed(0)
           _lowercase       :							Dict      		=		UNetaDConditionModel(
               block_out_channels=(32, 64),      layers_per_block=2,      sample_size=32,      in_channels=4,      out_channels=4,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D'),      up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D'),      cross_attention_dim=32,      )
           return model
       @property
       def 			UpperCamelCase     (					self)       ->   List[Any]:




           """simple docstring"""

           torch.manual_seed(0)
           _lowercase       :							str      		=		AutoencoderKL(
               block_out_channels=[32, 64],      in_channels=3,      out_channels=3,      down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'],      up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'],      latent_channels=4,      )
           return model
       @property
       def 			UpperCamelCase     (					self)       ->   Optional[Any]:




           """simple docstring"""

           torch.manual_seed(0)
           _lowercase       :							Optional[int]      		=		RobertaSeriesConfig(
               hidden_size=32,      project_dim=32,      intermediate_size=37,      layer_norm_eps=1E-05,      num_attention_heads=4,      num_hidden_layers=5,      pad_token_id=1,      vocab_size=50_06,      )
           return RobertaSeriesModelWithTransformation(lowerCamelCase)
       @property
       def 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           def extract(*lowerCamelCase,      **lowerCamelCase):






               class _lowerCamelCase:
                      def __init__(					self)       ->   Optional[Any]:




                          """simple docstring"""

                          _lowercase       :							Optional[int]      		=		torch.ones([0])




                      def 			UpperCamelCase     (					self,      lowerCamelCase)       ->   int:




                          """simple docstring"""

                          self.pixel_values.to(lowerCamelCase)
                          return self


               return Out()

           return extract
       def 			UpperCamelCase     (					self)       ->   Tuple:




           """simple docstring"""

           _lowercase       :							Any      		=		'cpu'  # ensure determinism for the device-dependent torch.Generator
           _lowercase       :							List[Any]      		=		self.dummy_cond_unet
           _lowercase       :							Union[str, Any]      		=		PNDMScheduler(skip_prk_steps=lowerCamelCase)
           _lowercase       :							Optional[Any]      		=		self.dummy_vae
           _lowercase       :							List[Any]      		=		self.dummy_text_encoder
           _lowercase       :							Any      		=		XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta')
           _lowercase       :							Tuple      		=		77

           _lowercase       :							int      		=		self.dummy_image.to(lowerCamelCase)
           _lowercase       :							int      		=		init_image / 2 + 0.5

           # make sure here that pndm scheduler skips prk
           _lowercase       :							Union[str, Any]      		=		AltDiffusionImgaImgPipeline(
               unet=lowerCamelCase,      scheduler=lowerCamelCase,      vae=lowerCamelCase,      text_encoder=lowerCamelCase,      tokenizer=lowerCamelCase,      safety_checker=lowerCamelCase,      feature_extractor=self.dummy_extractor,      )
           _lowercase       :							List[Any]      		=		VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor,      do_normalize=lowerCamelCase)
           _lowercase       :							Optional[int]      		=		alt_pipe.to(lowerCamelCase)
           alt_pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							Optional[Any]      		=		'A painting of a squirrel eating a burger'
           _lowercase       :							Dict      		=		torch.Generator(device=lowerCamelCase).manual_seed(0)
           _lowercase       :							Any      		=		alt_pipe(
               [prompt],      generator=lowerCamelCase,      guidance_scale=6.0,      num_inference_steps=2,      output_type='np',      image=lowerCamelCase,      )

           _lowercase       :							Optional[int]      		=		output.images

           _lowercase       :							Optional[Any]      		=		torch.Generator(device=lowerCamelCase).manual_seed(0)
           _lowercase       :							Optional[Any]      		=		alt_pipe(
               [prompt],      generator=lowerCamelCase,      guidance_scale=6.0,      num_inference_steps=2,      output_type='np',      image=lowerCamelCase,      return_dict=lowerCamelCase,      )[0]

           _lowercase       :							Optional[int]      		=		image[0, -3:, -3:, -1]
           _lowercase       :							Dict      		=		image_from_tuple[0, -3:, -3:, -1]

           assert image.shape == (1, 32, 32, 3)
           _lowercase       :							int      		=		np.array([0.4_4_2_7, 0.3_7_3_1, 0.4_2_4_9, 0.4_9_4_1, 0.4_5_4_6, 0.4_1_4_8, 0.4_1_9_3, 0.4_6_6_6, 0.4_4_9_9])

           assert np.abs(image_slice.flatten() - expected_slice).max() < 5E-3
           assert np.abs(image_from_tuple_slice.flatten() - expected_slice).max() < 5E-3
       @unittest.skipIf(torch_device != 'cuda',      'This test requires a GPU')
       def 			UpperCamelCase     (					self)       ->   str:




           """simple docstring"""

           _lowercase       :							List[Any]      		=		self.dummy_cond_unet
           _lowercase       :							Tuple      		=		PNDMScheduler(skip_prk_steps=lowerCamelCase)
           _lowercase       :							str      		=		self.dummy_vae
           _lowercase       :							Optional[Any]      		=		self.dummy_text_encoder
           _lowercase       :							Optional[Any]      		=		XLMRobertaTokenizer.from_pretrained('hf-internal-testing/tiny-xlm-roberta')
           _lowercase       :							Optional[Any]      		=		77

           _lowercase       :							str      		=		self.dummy_image.to(lowerCamelCase)

           # put models in fp16
           _lowercase       :							List[str]      		=		unet.half()
           _lowercase       :							List[Any]      		=		vae.half()
           _lowercase       :							Any      		=		bert.half()

           # make sure here that pndm scheduler skips prk
           _lowercase       :							Union[str, Any]      		=		AltDiffusionImgaImgPipeline(
               unet=lowerCamelCase,      scheduler=lowerCamelCase,      vae=lowerCamelCase,      text_encoder=lowerCamelCase,      tokenizer=lowerCamelCase,      safety_checker=lowerCamelCase,      feature_extractor=self.dummy_extractor,      )
           _lowercase       :							List[str]      		=		VaeImageProcessor(vae_scale_factor=alt_pipe.vae_scale_factor,      do_normalize=lowerCamelCase)
           _lowercase       :							Any      		=		alt_pipe.to(lowerCamelCase)
           alt_pipe.set_progress_bar_config(disable=lowerCamelCase)

           _lowercase       :							int      		=		'A painting of a squirrel eating a burger'
           _lowercase       :							Optional[Any]      		=		torch.manual_seed(0)
           _lowercase       :							Union[str, Any]      		=		alt_pipe(
               [prompt],      generator=lowerCamelCase,      num_inference_steps=2,      output_type='np',      image=lowerCamelCase,      ).images

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




       @unittest.skipIf(torch_device != 'cuda',      'This test requires a GPU')
       def 			UpperCamelCase     (					self)       ->   Optional[int]:




           """simple docstring"""

           _lowercase       :							int      		=		load_image(
               'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
               '/img2img/sketch-mountains-input.jpg')
           # resize to resolution that is divisible by 8 but not 16 or 32
           _lowercase       :							str      		=		init_image.resize((7_60, 5_04))

           _lowercase       :							Optional[int]      		=		'BAAI/AltDiffusion'
           _lowercase       :							str      		=		AltDiffusionImgaImgPipeline.from_pretrained(
               lowerCamelCase,      safety_checker=lowerCamelCase,      )
           pipe.to(lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)
           pipe.enable_attention_slicing()

           _lowercase       :							List[str]      		=		'A fantasy landscape, trending on artstation'

           _lowercase       :							Any      		=		torch.manual_seed(0)
           _lowercase       :							Dict      		=		pipe(
               prompt=lowerCamelCase,      image=lowerCamelCase,      strength=0.7_5,      guidance_scale=7.5,      generator=lowerCamelCase,      output_type='np',      )
           _lowercase       :							List[str]      		=		output.images[0]

           _lowercase       :							Tuple      		=		image[2_55:2_58, 3_83:3_86, -1]

           assert image.shape == (5_04, 7_60, 3)
           _lowercase       :							Optional[Any]      		=		np.array([0.9_3_5_8, 0.9_3_9_7, 0.9_5_9_9, 0.9_9_0_1, 1.0_0_0_0, 1.0_0_0_0, 0.9_8_8_2, 1.0_0_0_0, 1.0_0_0_0])

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






@slow
@require_torch_gpu
class _lowerCamelCase(						unittest.TestCase   ):
       def 			UpperCamelCase     (					self)       ->   Optional[Any]:




           """simple docstring"""

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




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




           """simple docstring"""

           _lowercase       :							Union[str, Any]      		=		load_image(
               'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
               '/img2img/sketch-mountains-input.jpg')
           _lowercase       :							str      		=		init_image.resize((7_68, 5_12))
           _lowercase       :							Any      		=		load_numpy(
               'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/img2img/fantasy_landscape_alt.npy')

           _lowercase       :							str      		=		'BAAI/AltDiffusion'
           _lowercase       :							Optional[Any]      		=		AltDiffusionImgaImgPipeline.from_pretrained(
               lowerCamelCase,      safety_checker=lowerCamelCase,      )
           pipe.to(lowerCamelCase)
           pipe.set_progress_bar_config(disable=lowerCamelCase)
           pipe.enable_attention_slicing()

           _lowercase       :							int      		=		'A fantasy landscape, trending on artstation'

           _lowercase       :							List[Any]      		=		torch.manual_seed(0)
           _lowercase       :							int      		=		pipe(
               prompt=lowerCamelCase,      image=lowerCamelCase,      strength=0.7_5,      guidance_scale=7.5,      generator=lowerCamelCase,      output_type='np',      )
           _lowercase       :							Union[str, Any]      		=		output.images[0]

           assert image.shape == (5_12, 7_68, 3)
           # img2img is flaky across GPUs even in fp32, so using MAE here
           assert np.abs(expected_image - image).max() < 1E-2


