


'''simple docstring'''







import tempfile
import unittest

import numpy as np
import torch

from diffusers import DiffusionPipeline
from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
class  lowercase_					(   unittest.TestCase       ):


    def        __a							(					self  , a = False   ):
          UpperCamelCase__  =    10

          if only_cross_attention:
                UpperCamelCase__  =    12
          else:
                # when only cross attention is not set, the cross attention dim must be the same as the query dim
                UpperCamelCase__  =    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        __a							(					self  , a  , a   ):
          UpperCamelCase__  =    2

          UpperCamelCase__  =    torch.rand(a  , a  , 3  , 2   )
          UpperCamelCase__  =    torch.rand(a  , 4  , a   )
          UpperCamelCase__  =    None

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


    def        __a							(					self   ):
          # self and cross attention

          torch.manual_seed(0   )

          UpperCamelCase__  =    self.get_constructor_arguments(only_cross_attention=a   )
          UpperCamelCase__  =    Attention(**a   )

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

          UpperCamelCase__  =    self.get_forward_arguments(
              query_dim=constructor_args["query_dim"]  , added_kv_proj_dim=constructor_args["added_kv_proj_dim"]   )

          UpperCamelCase__  =    attn(**a   )

          # only self attention

          torch.manual_seed(0   )

          UpperCamelCase__  =    self.get_constructor_arguments(only_cross_attention=a   )
          UpperCamelCase__  =    Attention(**a   )

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

          UpperCamelCase__  =    self.get_forward_arguments(
              query_dim=constructor_args["query_dim"]  , added_kv_proj_dim=constructor_args["added_kv_proj_dim"]   )

          UpperCamelCase__  =    attn(**a   )

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

class  lowercase_					(   unittest.TestCase       ):


    def        __a							(					self   ):
          UpperCamelCase__  =    DiffusionPipeline.from_pretrained("hf-internal-testing/tiny-stable-diffusion-pipe"  , safety_checker=a   )

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

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

          UpperCamelCase__  =    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
                UpperCamelCase__  =    DiffusionPipeline.from_pretrained(a  , device_map="sequential"  , safety_checker=a   )

          UpperCamelCase__  =    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   )   )










'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


a__  : Any				=	{
    'configuration_codegen': ['CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'CodeGenConfig', 'CodeGenOnnxConfig'],
    'tokenization_codegen': ['CodeGenTokenizer'],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a__  : Tuple				=	['CodeGenTokenizerFast']

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a__  : Dict				=	[
      'CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST',
      'CodeGenForCausalLM',
      'CodeGenModel',
      'CodeGenPreTrainedModel',
  ]

if TYPE_CHECKING:
  from .configuration_codegen import CODEGEN_PRETRAINED_CONFIG_ARCHIVE_MAP, CodeGenConfig, CodeGenOnnxConfig
  from .tokenization_codegen import CodeGenTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_codegen_fast import CodeGenTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_codegen import (
        CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST,
        CodeGenForCausalLM,
        CodeGenModel,
        CodeGenPreTrainedModel,
    )

else:
  import sys

  a__  : Optional[int]				=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)





