'''simple docstring'''





from typing import List, Optional, Union

import numpy as np
import tensorflow as tf

from .utils import logging


SCREAMING_SNAKE_CASE :	int							     =		logging.get_logger(__name__)



def   __A		(			_A					):



 """simple docstring"""


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

 __a       =  tf.shape(_A					)

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

 __a       =  tensor.shape.as_list()

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



def   __A		(			_A							,				_A = None							,				_A = None					):



 """simple docstring"""


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



def   __A		(			_A							,				_A							,				_A							,				_A=1E-5							,				_A=-1					):



 """simple docstring"""


 if weight.shape.rank != 1 or bias.shape.rank != 1 or not isinstance(_A							,				_A					):
  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
 __a       ,     __a       =  tf.nn.moments(_A							,				axes=[axis]							,				keepdims=_A					)

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

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



def   __A		(			_A							,				_A=0							,				_A=-1					):



 """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

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



def   __A		(			_A					):



 """simple docstring"""


 if not isinstance(_A							,				tf.Tensor					):
  __a       =  tf.convert_to_tensor(_A					)  # Catches stray NumPy inputs
 if encoder_attention_mask.shape.rank == 3:
  __a       =  encoder_attention_mask[:, None, :, :]
 if encoder_attention_mask.shape.rank == 2:
  __a       =  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))
 __a       =  (
     tf.cast(1							,				encoder_attention_mask.dtype					) - encoder_extended_attention_mask
 ) * encoder_extended_attention_mask.dtype.min

 return encoder_extended_attention_mask



def   __A		(			_A							,				_A							,				_A = "input_ids"					):



 """simple docstring"""


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



def   __A		(			_A							,				_A							,				_A					):



 """simple docstring"""


 __a       =  6_4512
 # 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.
 __a       =  [x for x in data if len(_A					) > 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}"""					)

 __a       =  np.asarray(_A					)

 __a       =  1
 __a       =  np.array_split(_A							,				_A					)

 # 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
  __a       =  np.array_split(_A							,				_A					)

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



def   __A		(			_A							,				_A					):



 """simple docstring"""


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






def   __A		(			_A					):



 """simple docstring"""


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

 return tf.nest.map_structure(_expand_single_ad_tensor							,				_A					)



import unittest

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

from diffusers import DDIMScheduler, LDMPipeline, UNetaDModel, VQModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class    A_ (       unittest.TestCase ):

  @property
  def 		_UpperCAmelCase   (		self :      str       ):
   torch.manual_seed(0       )
   __a       =  UNetaDModel(
       block_out_channels=(32, 64)     ,							layers_per_block=2     ,							sample_size=32     ,							in_channels=3     ,							out_channels=3     ,							down_block_types=("DownBlock2D", "AttnDownBlock2D")     ,							up_block_types=("AttnUpBlock2D", "UpBlock2D")     ,							)
   return model

  @property
  def 		_UpperCAmelCase   (		self :      Dict       ):
   torch.manual_seed(0       )
   __a       =  VQModel(
       block_out_channels=[32, 64]     ,							in_channels=3     ,							out_channels=3     ,							down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]     ,							up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]     ,							latent_channels=3     ,							)
   return model

  @property
  def 		_UpperCAmelCase   (		self :      Optional[Any]       ):
   torch.manual_seed(0       )
   __a       =  CLIPTextConfig(
       bos_token_id=0     ,							eos_token_id=2     ,							hidden_size=32     ,							intermediate_size=37     ,							layer_norm_eps=1E-05     ,							num_attention_heads=4     ,							num_hidden_layers=5     ,							pad_token_id=1     ,							vocab_size=10_00     ,							)
   return CLIPTextModel(__SCREAMING_SNAKE_CASE       )

  def 		_UpperCAmelCase   (		self :      Dict       ):
   __a       =  self.dummy_uncond_unet
   __a       =  DDIMScheduler()
   __a       =  self.dummy_vq_model

   __a       =  LDMPipeline(unet=__SCREAMING_SNAKE_CASE     ,							vqvae=__SCREAMING_SNAKE_CASE     ,							scheduler=__SCREAMING_SNAKE_CASE       )
   ldm.to(__SCREAMING_SNAKE_CASE       )
   ldm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

   __a       =  torch.manual_seed(0       )
   __a       =  ldm(generator=__SCREAMING_SNAKE_CASE     ,							num_inference_steps=2     ,							output_type="numpy"       ).images

   __a       =  torch.manual_seed(0       )
   __a       =  ldm(generator=__SCREAMING_SNAKE_CASE     ,							num_inference_steps=2     ,							output_type="numpy"     ,							return_dict=__SCREAMING_SNAKE_CASE       )[0]

   __a       =  image[0, -3:, -3:, -1]
   __a       =  image_from_tuple[0, -3:, -3:, -1]

   assert image.shape == (1, 64, 64, 3)
   __a       =  np.array([0.85_12, 0.8_18, 0.64_11, 0.68_08, 0.44_65, 0.56_18, 0.46, 0.62_31, 0.51_72]       )
   __a       =  1E-2 if torch_device != "mps" else 3E-2

   assert np.abs(image_slice.flatten() - expected_slice       ).max() < tolerance
   assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < tolerance
@slow
@require_torch
class    A_ (       unittest.TestCase ):

  def 		_UpperCAmelCase   (		self :      str       ):
   __a       =  LDMPipeline.from_pretrained("CompVis/ldm-celebahq-256"       )
   ldm.to(__SCREAMING_SNAKE_CASE       )
   ldm.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE       )

   __a       =  torch.manual_seed(0       )
   __a       =  ldm(generator=__SCREAMING_SNAKE_CASE     ,							num_inference_steps=5     ,							output_type="numpy"       ).images

   __a       =  image[0, -3:, -3:, -1]

   assert image.shape == (1, 2_56, 2_56, 3)
   __a       =  np.array([0.43_99, 0.4_49_75, 0.4_68_25, 0.4_74, 0.43_59, 0.45_81, 0.4_50_95, 0.43_41, 0.44_47]       )
   __a       =  1E-2 if torch_device != "mps" else 3E-2

   assert np.abs(image_slice.flatten() - expected_slice       ).max() < tolerance


