'''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   UpperCamelCase					(       unittest.TestCase       ):







    """simple docstring"""





    def    SCREAMING_SNAKE_CASE_     (					self     :						Optional[int]							,							UpperCAmelCase_     :						bool = False):


         """simple docstring"""







         a					:							Union[str, Any]							= 1_0

         if only_cross_attention:
              a					:							Tuple							= 1_2
         else:
              # when only cross attention is not set, the cross attention dim must be the same as the query dim
              a					:							Any							= 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    SCREAMING_SNAKE_CASE_     (					self     :						Optional[Any]							,							UpperCAmelCase_     :						Tuple							,							UpperCAmelCase_     :						Any):


         """simple docstring"""







         a					:							int							= 2

         a					:							Dict							= torch.rand(UpperCAmelCase_							,							UpperCAmelCase_							,							3							,							2)
         a					:							Optional[int]							= torch.rand(UpperCAmelCase_							,							4							,							UpperCAmelCase_)
         a					:							Tuple							= None

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







    def    SCREAMING_SNAKE_CASE_     (					self     :						Any):


         """simple docstring"""







         torch.manual_seed(0)

         a					:							int							= self.get_constructor_arguments(only_cross_attention=UpperCAmelCase_)
         a					:							Optional[Any]							= Attention(**UpperCAmelCase_)

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

         a					:							Any							= self.get_forward_arguments(
             query_dim=constructor_args['query_dim']							,							added_kv_proj_dim=constructor_args['added_kv_proj_dim'])

         a					:							List[str]							= attn(**UpperCAmelCase_)

         # only self attention

         torch.manual_seed(0)

         a					:							Dict							= self.get_constructor_arguments(only_cross_attention=UpperCAmelCase_)
         a					:							List[str]							= Attention(**UpperCAmelCase_)

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

         a					:							List[str]							= self.get_forward_arguments(
             query_dim=constructor_args['query_dim']							,							added_kv_proj_dim=constructor_args['added_kv_proj_dim'])

         a					:							int							= attn(**UpperCAmelCase_)

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






class   UpperCamelCase					(       unittest.TestCase       ):







    """simple docstring"""





    def    SCREAMING_SNAKE_CASE_     (					self     :						Optional[Any]):


         """simple docstring"""







         a					:							str							= DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'							,							safety_checker=UpperCAmelCase_)

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

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

         a					:							Optional[Any]							= 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(UpperCAmelCase_)

              # can also load the converted weights
              a					:							Any							= DiffusionPipeline.from_pretrained(UpperCAmelCase_							,							device_map='sequential'							,							safety_checker=UpperCAmelCase_)

         a					:							Optional[Any]							= pipe(
             'foo'							,							num_inference_steps=2							,							generator=torch.Generator('cpu').manual_seed(0)							,							output_type='np'							,							).images

         self.assertTrue(np.allclose(UpperCAmelCase_							,							UpperCAmelCase_							,							atol=1e-5))
         self.assertTrue(np.allclose(UpperCAmelCase_							,							UpperCAmelCase_							,							atol=1e-5))


'''simple docstring'''





from typing import TYPE_CHECKING

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


UpperCamelCase       :     Tuple			=    {
    """configuration_markuplm""": ["""MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MarkupLMConfig"""],
    """feature_extraction_markuplm""": ["""MarkupLMFeatureExtractor"""],
    """processing_markuplm""": ["""MarkupLMProcessor"""],
    """tokenization_markuplm""": ["""MarkupLMTokenizer"""],
}

try:
      if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase       :     int			=    ["""MarkupLMTokenizerFast"""]

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      UpperCamelCase       :     Any			=    [
          """MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST""",
          """MarkupLMForQuestionAnswering""",
          """MarkupLMForSequenceClassification""",
          """MarkupLMForTokenClassification""",
          """MarkupLMModel""",
          """MarkupLMPreTrainedModel""",
      ]


if TYPE_CHECKING:
      from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
      from .feature_extraction_markuplm import MarkupLMFeatureExtractor
      from .processing_markuplm import MarkupLMProcessor
      from .tokenization_markuplm import MarkupLMTokenizer

      try:
            if not is_tokenizers_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .tokenization_markuplm_fast import MarkupLMTokenizerFast

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_markuplm import (
                MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
                MarkupLMForQuestionAnswering,
                MarkupLMForSequenceClassification,
                MarkupLMForTokenClassification,
                MarkupLMModel,
                MarkupLMPreTrainedModel,
            )


else:
      import sys

      UpperCamelCase       :     Dict			=    _LazyModule(__name__, globals()["""__file__"""], _import_structure)

