
import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor

class 		__magic_name__				( unittest.TestCase					):


  """simple docstring"""





  def     _UpperCAmelCase  (					self	,		a__ = False		):
    _lowerCamelCase      =						10

    if only_cross_attention:
      _lowerCamelCase      =						12
    else:
      # when only cross attention is not set, the cross attention dim must be the same as the query dim
      _lowerCamelCase      =						query_dim

    return {
        "query_dim": query_dim,
        "cross_attention_dim": cross_attention_dim,
        "heads": 2,
        "dim_head": 4,
        "added_kv_proj_dim": 6,
        "norm_num_groups": 1,
        "only_cross_attention": only_cross_attention,
        "processor": AttnAddedKVProcessor(),
    }


  def     _UpperCAmelCase  (					self	,		a__	,		a__		):
    _lowerCamelCase      =						2

    _lowerCamelCase      =						torch.rand(a__	,		a__	,		3	,		2		)
    _lowerCamelCase      =						torch.rand(a__	,		4	,		a__		)
    _lowerCamelCase      =						None

    return {
        "hidden_states": hidden_states,
        "encoder_hidden_states": encoder_hidden_states,
        "attention_mask": attention_mask,
    }


  def     _UpperCAmelCase  (					self		):
    # self and cross attention

    torch.manual_seed(0		)

    _lowerCamelCase      =						self.get_constructor_arguments(only_cross_attention=a__		)
    _lowerCamelCase      =						Attention(**a__		)

    self.assertTrue(attn.to_k is not None		)
    self.assertTrue(attn.to_v is not None		)

    _lowerCamelCase      =						self.get_forward_arguments(
        query_dim=constructor_args['''query_dim''']	,		added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']		)

    _lowerCamelCase      =						attn(**a__		)

    # only self attention

    torch.manual_seed(0		)

    _lowerCamelCase      =						self.get_constructor_arguments(only_cross_attention=a__		)
    _lowerCamelCase      =						Attention(**a__		)

    self.assertTrue(attn.to_k is None		)
    self.assertTrue(attn.to_v is None		)

    _lowerCamelCase      =						self.get_forward_arguments(
        query_dim=constructor_args['''query_dim''']	,		added_kv_proj_dim=constructor_args['''added_kv_proj_dim''']		)

    _lowerCamelCase      =						attn(**a__		)

    self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()		)







class 		__magic_name__				( unittest.TestCase					):


  """simple docstring"""





  def     _UpperCAmelCase  (					self		):
    _lowerCamelCase      =						DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''	,		safety_checker=a__		)

    _lowerCamelCase      =						pipe(
        '''foo'''	,		num_inference_steps=2	,		generator=torch.Generator('''cpu'''		).manual_seed(0		)	,		output_type='''np'''	,		).images

    # the initial conversion succeeds
    _lowerCamelCase      =						DiffusionPipeline.from_pretrained(
        '''hf-internal-testing/tiny-stable-diffusion-pipe'''	,		device_map='''sequential'''	,		safety_checker=a__		)

    _lowerCamelCase      =						pipe(
        '''foo'''	,		num_inference_steps=2	,		generator=torch.Generator('''cpu'''		).manual_seed(0		)	,		output_type='''np'''	,		).images

    with tempfile.TemporaryDirectory() as tmpdir:
      # save the converted model
      pipe.save_pretrained(a__		)

      # can also load the converted weights
      _lowerCamelCase      =						DiffusionPipeline.from_pretrained(a__	,		device_map='''sequential'''	,		safety_checker=a__		)

    _lowerCamelCase      =						pipe(
        '''foo'''	,		num_inference_steps=2	,		generator=torch.Generator('''cpu'''		).manual_seed(0		)	,		output_type='''np'''	,		).images

    self.assertTrue(np.allclose(a__	,		a__	,		atol=1E-5		)		)
    self.assertTrue(np.allclose(a__	,		a__	,		atol=1E-5		)		)






from __future__ import annotations

from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any

class 		__magic_name__				:


  """simple docstring"""





  _UpperCamelCase						 = ("_front", "_back", "_len")

  @dataclass
  class 		__magic_name__				:


    """simple docstring"""





    _UpperCamelCase						 = None
    _UpperCamelCase						 = None
    _UpperCamelCase						 = None







  class 		__magic_name__				:


    """simple docstring"""





    _UpperCamelCase						 = ("_cur",)


    def __init__(					self	,		a__		):
      _lowerCamelCase      =						cur


    def __iter__(					self		):
      return self


    def     _UpperCAmelCase  (					self		):
      if self._cur is None:
        # finished iterating
        raise StopIteration
      _lowerCamelCase      =						self._cur.val
      _lowerCamelCase      =						self._cur.next_node

      return val







  def __init__(					self	,		a__ = None		):
    _lowerCamelCase      =						None
    _lowerCamelCase      =						None
    _lowerCamelCase      =						0

    if iterable is not None:
      # append every value to the deque
      for val in iterable:
        self.append(a__		)


  def     _UpperCAmelCase  (					self	,		a__		):
    _lowerCamelCase      =						self._Node(a__	,		a__	,		a__		)
    if self.is_empty():
      # front = back
      _lowerCamelCase      =						_lowerCamelCase      =						node
      _lowerCamelCase      =						1
    else:
      # connect nodes
      _lowerCamelCase      =						node
      _lowerCamelCase      =						self._back
      _lowerCamelCase      =						node  # assign new back to the new node

      self._len += 1

      # make sure there were no errors
      assert not self.is_empty(), "Error on appending value."


  def     _UpperCAmelCase  (					self	,		a__		):
    _lowerCamelCase      =						self._Node(a__	,		a__	,		a__		)
    if self.is_empty():
      # front = back
      _lowerCamelCase      =						_lowerCamelCase      =						node
      _lowerCamelCase      =						1
    else:
      # connect nodes
      _lowerCamelCase      =						self._front
      _lowerCamelCase      =						node
      _lowerCamelCase      =						node  # assign new front to the new node

      self._len += 1

      # make sure there were no errors
      assert not self.is_empty(), "Error on appending value."


  def     _UpperCAmelCase  (					self	,		a__		):
    for val in iterable:
      self.append(a__		)


  def     _UpperCAmelCase  (					self	,		a__		):
    for val in iterable:
      self.appendleft(a__		)


  def     _UpperCAmelCase  (					self		):
    assert not self.is_empty(), "Deque is empty."

    _lowerCamelCase      =						self._back
    _lowerCamelCase      =						self._back.prev_node  # set new back
    # drop the last node - python will deallocate memory automatically
    _lowerCamelCase      =						None

    self._len -= 1

    return topop.val


  def     _UpperCAmelCase  (					self		):
    assert not self.is_empty(), "Deque is empty."

    _lowerCamelCase      =						self._front
    _lowerCamelCase      =						self._front.next_node  # set new front and drop the first node
    _lowerCamelCase      =						None

    self._len -= 1

    return topop.val


  def     _UpperCAmelCase  (					self		):
    return self._front is None


  def __len__(					self		):
    return self._len


  def __eq__(					self	,		a__		):

    if not isinstance(a__	,		a__		):
      return NotImplemented

    _lowerCamelCase      =						self._front
    _lowerCamelCase      =						other._front

    # if the length of the dequeues are not the same, they are not equal
    if len(self		) != len(a__		):
      return False

    while me is not None and oth is not None:
      # compare every value
      if me.val != oth.val:
        return False
      _lowerCamelCase      =						me.next_node
      _lowerCamelCase      =						oth.next_node

    return True


  def __iter__(					self		):
    return Deque._Iterator(self._front		)


  def __repr__(					self		):
    _lowerCamelCase      =						[]
    _lowerCamelCase      =						self._front
    while aux is not None:
      # append the values in a list to display
      values_list.append(aux.val		)
      _lowerCamelCase      =						aux.next_node

    return f'''[{', '.join(repr(a__		) for val in values_list		)}]'''


if __name__ == "__main__":
  import doctest

  doctest.testmod()




