



import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionAttendAndExcitePipeline,
    UNetaDConditionModel,
)
from diffusers.utils import load_numpy, skip_mps, slow
from diffusers.utils.testing_utils import require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


a_      :      Optional[int]    =     False
@skip_mps
class    __UpperCamelCase			(       _lowercase	,	_lowercase	,	_lowercase	,	unittest.TestCase				):

 """simple docstring"""



 _lowercase   :    List[Any]      						=					StableDiffusionAttendAndExcitePipeline
 _lowercase   :    str      						=					False
 _lowercase   :    List[str]      						=					TEXT_TO_IMAGE_PARAMS
 _lowercase   :    Optional[int]      						=					TEXT_TO_IMAGE_BATCH_PARAMS.union({'''token_indices'''}				)
 _lowercase   :    Union[str, Any]      						=					TEXT_TO_IMAGE_IMAGE_PARAMS
 _lowercase   :    List[Any]      						=					TEXT_TO_IMAGE_IMAGE_PARAMS

 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			Dict:
  super().setUpClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			Tuple:
  super().tearDownClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  torch.manual_seed(0      )
  a__												= UNetaDConditionModel(
      block_out_channels=(3_2, 6_4)		,				layers_per_block=1		,				sample_size=3_2		,				in_channels=4		,				out_channels=4		,				down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')		,				up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')		,				cross_attention_dim=3_2		,				attention_head_dim=(2, 4)		,				use_linear_projection=SCREAMING_SNAKE_CASE		,				)
  a__												= DDIMScheduler(
      beta_start=0.0_00_85		,				beta_end=0.0_12		,				beta_schedule='''scaled_linear'''		,				clip_sample=SCREAMING_SNAKE_CASE		,				set_alpha_to_one=SCREAMING_SNAKE_CASE		,				)
  torch.manual_seed(0      )
  a__												= 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      )
  a__												= CLIPTextConfig(
      bos_token_id=0		,				eos_token_id=2		,				hidden_size=3_2		,				intermediate_size=3_7		,				layer_norm_eps=1e-0_5		,				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		,				)
  a__												= CLIPTextModel(SCREAMING_SNAKE_CASE      )
  a__												= CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''      )

  a__												= {
      '''unet''': unet,
      '''scheduler''': scheduler,
      '''vae''': vae,
      '''text_encoder''': text_encoder,
      '''tokenizer''': tokenizer,
      '''safety_checker''': None,
      '''feature_extractor''': None,
  }

  return components

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=0      ) ->			Dict:
  if str(SCREAMING_SNAKE_CASE      ).startswith('''mps'''      ):
   a__												= torch.manual_seed(SCREAMING_SNAKE_CASE      )
  else:
   a__												= torch.Generator(device=SCREAMING_SNAKE_CASE      ).manual_seed(SCREAMING_SNAKE_CASE      )
  a__												= a__												= {
      '''prompt''': '''a cat and a frog''',
      '''token_indices''': [2, 5],
      '''generator''': generator,
      '''num_inference_steps''': 1,
      '''guidance_scale''': 6.0,
      '''output_type''': '''numpy''',
      '''max_iter_to_alter''': 2,
      '''thresholds''': {0: 0.7},
  }
  return inputs

 def 						_UpperCAmelCase       (	self      ) ->			str:
  a__												= '''cpu'''

  a__												= self.get_dummy_components()
  a__												= self.pipeline_class(**SCREAMING_SNAKE_CASE      )
  pipe.to(SCREAMING_SNAKE_CASE      )
  pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE      )

  a__												= self.get_dummy_inputs(SCREAMING_SNAKE_CASE      )
  a__												= pipe(**SCREAMING_SNAKE_CASE      ).images
  a__												= image[0, -3:, -3:, -1]

  self.assertEqual(image.shape		,				(1, 6_4, 6_4, 3)      )
  a__												= np.array(
      [0.63_90_53_64, 0.62_89_73_07, 0.48_59_90_17, 0.5_13_36_24, 0.5_55_00_48, 0.45_76_95_16, 0.50_32_69_73, 0.5_02_31_39, 0.45_38_44_96]      )
  a__												= np.abs(image_slice.flatten() - expected_slice      ).max()
  self.assertLessEqual(SCREAMING_SNAKE_CASE		,				1e-3      )

 def 						_UpperCAmelCase       (	self      ) ->			str:
  super().test_cpu_offload_forward_pass(expected_max_diff=5e-4      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  # NOTE: Larger batch sizes cause this test to timeout, only test on smaller batches
  self._test_inference_batch_consistent(batch_sizes=[1, 2]      )

 def 						_UpperCAmelCase       (	self      ) ->			List[Any]:
  self._test_inference_batch_single_identical(batch_size=2		,				expected_max_diff=7e-4      )

 def 						_UpperCAmelCase       (	self      ) ->			int:
  super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3      )

 def 						_UpperCAmelCase       (	self      ) ->			Optional[Any]:
  super().test_pt_np_pil_outputs_equivalent(expected_max_diff=5e-4      )

 def 						_UpperCAmelCase       (	self      ) ->			Dict:
  super().test_save_load_local(expected_max_difference=5e-4      )



 def 						_UpperCAmelCase       (	self      ) ->			str:
  super().test_save_load_optional_components(expected_max_difference=4e-4      )





@require_torch_gpu
@slow
class    __UpperCamelCase			(       unittest.TestCase				):

 """simple docstring"""



 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			Optional[int]:
  super().setUpClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 @classmethod
 def 						_UpperCAmelCase       (	cls      ) ->			str:
  super().tearDownClass()
  torch.use_deterministic_algorithms(SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  super().tearDown()
  gc.collect()
  torch.cuda.empty_cache()



 def 						_UpperCAmelCase       (	self      ) ->			str:
  a__												= torch.manual_seed(5_1      )

  a__												= StableDiffusionAttendAndExcitePipeline.from_pretrained(
      '''CompVis/stable-diffusion-v1-4'''		,				safety_checker=SCREAMING_SNAKE_CASE		,				torch_dtype=torch.floataa      )
  pipe.to('''cuda'''      )

  a__												= '''a painting of an elephant with glasses'''
  a__												= [5, 7]

  a__												= pipe(
      prompt=SCREAMING_SNAKE_CASE		,				token_indices=SCREAMING_SNAKE_CASE		,				guidance_scale=7.5		,				generator=SCREAMING_SNAKE_CASE		,				num_inference_steps=5		,				max_iter_to_alter=5		,				output_type='''numpy'''		,				).images[0]

  a__												= load_numpy(
      '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/attend-and-excite/elephant_glasses.npy'''      )
  assert np.abs((expected_image - image).max()      ) < 5e-1










import torch
from torch import nn
class    __UpperCamelCase			(       nn.Module				):

 """simple docstring"""



 def __init__(	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=1		,				SCREAMING_SNAKE_CASE=False      ) ->			Tuple:
  super().__init__()

  a__												= n_token
  a__												= d_embed
  a__												= d_proj

  a__												= cutoffs + [n_token]
  a__												= [0] + self.cutoffs
  a__												= div_val

  a__												= self.cutoffs[0]
  a__												= len(self.cutoffs      ) - 1
  a__												= self.shortlist_size + self.n_clusters

  if self.n_clusters > 0:
   a__												= nn.Parameter(torch.zeros(self.n_clusters		,				self.d_embed      )      )
   a__												= nn.Parameter(torch.zeros(self.n_clusters      )      )

  a__												= nn.ModuleList()
  a__												= nn.ParameterList()

  if div_val == 1:
   for i in range(len(self.cutoffs      )      ):
    if d_proj != d_embed:
     self.out_projs.append(nn.Parameter(torch.FloatTensor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )      )
    else:
     self.out_projs.append(SCREAMING_SNAKE_CASE      )

   self.out_layers.append(nn.Linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )
  else:
   for i in range(len(self.cutoffs      )      ):
    a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
    a__												= d_embed // (div_val**i)

    self.out_projs.append(nn.Parameter(torch.FloatTensor(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )      )      )

    self.out_layers.append(nn.Linear(SCREAMING_SNAKE_CASE		,				r_idx - l_idx      )      )

  a__												= keep_order

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      ) ->			str:
  if proj is None:
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				bias=SCREAMING_SNAKE_CASE      )
  else:
   # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				proj.t().contiguous()      )
   a__												= nn.functional.linear(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				bias=SCREAMING_SNAKE_CASE      )
   # else:
   #     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
   #     if bias is not None:
   #         logit = logit + bias

  return logit

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=False      ) ->			Optional[int]:

  if labels is not None:
   # Shift so that tokens < n predict n
   a__												= hidden[..., :-1, :].contiguous()
   a__												= labels[..., 1:].contiguous()
   a__												= hidden.view(-1		,				hidden.size(-1      )      )
   a__												= labels.view(-1      )
   if hidden.size(0      ) != labels.size(0      ):
    raise RuntimeError('''Input and labels should have the same size in the batch dimension.'''      )
  else:
   a__												= hidden.view(-1		,				hidden.size(-1      )      )

  if self.n_clusters == 0:
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				self.out_layers[0].weight		,				self.out_layers[0].bias		,				self.out_projs[0]      )
   if labels is not None:
    a__												= labels != -1_0_0
    a__												= torch.zeros_like(SCREAMING_SNAKE_CASE		,				dtype=hidden.dtype		,				device=hidden.device      )
    a__												= (
        -nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )[mask].gather(1		,				labels[mask].unsqueeze(1      )      ).squeeze(1      )
    )
   else:
    a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )
  else:
   # construct weights and biases
   a__							,			a__												= [], []
   for i in range(len(self.cutoffs      )      ):
    if self.div_val == 1:
     a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
     a__												= self.out_layers[0].weight[l_idx:r_idx]
     a__												= self.out_layers[0].bias[l_idx:r_idx]
    else:
     a__												= self.out_layers[i].weight
     a__												= self.out_layers[i].bias

    if i == 0:
     a__												= torch.cat([weight_i, self.cluster_weight]		,				dim=0      )
     a__												= torch.cat([bias_i, self.cluster_bias]		,				dim=0      )

    weights.append(SCREAMING_SNAKE_CASE      )
    biases.append(SCREAMING_SNAKE_CASE      )

   a__							,			a__							,			a__												= weights[0], biases[0], self.out_projs[0]

   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
   a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

   if labels is None:
    a__												= hidden.new_empty((head_logit.size(0      ), self.n_token)      )
   else:
    a__												= torch.zeros_like(SCREAMING_SNAKE_CASE		,				dtype=hidden.dtype		,				device=hidden.device      )

   a__												= 0
   a__												= [0] + self.cutoffs
   for i in range(len(SCREAMING_SNAKE_CASE      ) - 1      ):
    a__							,			a__												= cutoff_values[i], cutoff_values[i + 1]

    if labels is not None:
     a__												= (labels >= l_idx) & (labels < r_idx)
     a__												= mask_i.nonzero().squeeze()

     if indices_i.numel() == 0:
      continue

     a__												= labels.index_select(0		,				SCREAMING_SNAKE_CASE      ) - l_idx
     a__												= head_logprob.index_select(0		,				SCREAMING_SNAKE_CASE      )
     a__												= hidden.index_select(0		,				SCREAMING_SNAKE_CASE      )
    else:
     a__												= hidden

    if i == 0:
     if labels is not None:
      a__												= head_logprob_i.gather(1		,				target_i[:, None]      ).squeeze(1      )
     else:
      a__												= head_logprob[:, : self.cutoffs[0]]
    else:
     a__							,			a__							,			a__												= weights[i], biases[i], self.out_projs[i]

     a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
     a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )
     a__												= self.cutoffs[0] + i - 1  # No probability for the head cluster
     if labels is not None:
      a__												= head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
          1		,				target_i[:, None]      ).squeeze(1      )
     else:
      a__												= head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
      a__												= logprob_i

    if labels is not None:
     if (hasattr(self		,				'''keep_order'''      ) and self.keep_order) or keep_order:
      out.index_copy_(0		,				SCREAMING_SNAKE_CASE		,				-logprob_i      )
     else:
      out[offset : offset + logprob_i.size(0      )].copy_(-logprob_i      )
     offset += logprob_i.size(0      )

  return out



 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE      ) ->			List[str]:
  if self.n_clusters == 0:
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				self.out_layers[0].weight		,				self.out_layers[0].bias		,				self.out_projs[0]      )
   return nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=-1      )
  else:
   # construct weights and biases
   a__							,			a__												= [], []
   for i in range(len(self.cutoffs      )      ):
    if self.div_val == 1:
     a__							,			a__												= self.cutoff_ends[i], self.cutoff_ends[i + 1]
     a__												= self.out_layers[0].weight[l_idx:r_idx]
     a__												= self.out_layers[0].bias[l_idx:r_idx]
    else:
     a__												= self.out_layers[i].weight
     a__												= self.out_layers[i].bias

    if i == 0:
     a__												= torch.cat([weight_i, self.cluster_weight]		,				dim=0      )
     a__												= torch.cat([bias_i, self.cluster_bias]		,				dim=0      )

    weights.append(SCREAMING_SNAKE_CASE      )
    biases.append(SCREAMING_SNAKE_CASE      )

   a__							,			a__							,			a__												= weights[0], biases[0], self.out_projs[0]
   a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )

   a__												= hidden.new_empty((head_logit.size(0      ), self.n_token)      )
   a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

   a__												= [0] + self.cutoffs
   for i in range(len(SCREAMING_SNAKE_CASE      ) - 1      ):
    a__							,			a__												= cutoff_values[i], cutoff_values[i + 1]

    if i == 0:
     a__												= head_logprob[:, : self.cutoffs[0]]
    else:
     a__							,			a__							,			a__												= weights[i], biases[i], self.out_projs[i]

     a__												= self._compute_logit(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
     a__												= nn.functional.log_softmax(SCREAMING_SNAKE_CASE		,				dim=1      )

     a__												= head_logprob[:, -i] + tail_logprob_i
     a__												= logprob_i

   return out





