import tempfile
import unittest

import numpy as np
import torch

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







   def 	lowerCamelCase_  (    self	:	Union[str, Any]      ,      lowerCamelCase_	:	bool = False							):





    """simple docstring"""


    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 	lowerCamelCase_  (    self	:	Optional[int]      ,      lowerCamelCase_	:	List[str]      ,      lowerCamelCase_	:	str							):





    """simple docstring"""


    UpperCamelCase      =			2

    UpperCamelCase      =			torch.rand(lowerCamelCase_      ,      lowerCamelCase_      ,      3      ,      2							)
    UpperCamelCase      =			torch.rand(lowerCamelCase_      ,      4      ,      lowerCamelCase_							)
    UpperCamelCase      =			None

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







   def 	lowerCamelCase_  (    self	:	List[Any]							):





    """simple docstring"""


    torch.manual_seed(0							)

    UpperCamelCase      =			self.get_constructor_arguments(only_cross_attention=lowerCamelCase_							)
    UpperCamelCase      =			Attention(**lowerCamelCase_							)

    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(**lowerCamelCase_							)

    # only self attention

    torch.manual_seed(0							)

    UpperCamelCase      =			self.get_constructor_arguments(only_cross_attention=lowerCamelCase_							)
    UpperCamelCase      =			Attention(**lowerCamelCase_							)

    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(**lowerCamelCase_							)

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





class SCREAMING_SNAKE_CASE_		(			unittest.TestCase		):







   def 	lowerCamelCase_  (    self	:	List[str]							):





    """simple docstring"""


    UpperCamelCase      =			DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=lowerCamelCase_							)

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

    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(lowerCamelCase_							)

     # can also load the converted weights
     UpperCamelCase      =			DiffusionPipeline.from_pretrained(lowerCamelCase_      ,      device_map="""sequential"""      ,      safety_checker=lowerCamelCase_							)

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

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

import unittest

from datasets import load_dataset

from transformers import load_tool

from .test_tools_common import ToolTesterMixin
class SCREAMING_SNAKE_CASE_		(			unittest.TestCase     ,    __lowerCAmelCase		):







   def 	lowerCamelCase_  (    self	:	Optional[Any]							):





    """simple docstring"""


    UpperCamelCase      =			load_tool("""document-question-answering"""							)
    self.tool.setup()
    UpperCamelCase      =			load_tool("""document-question-answering"""      ,      remote=lowerCamelCase_							)







   def 	lowerCamelCase_  (    self	:	Optional[Any]							):





    """simple docstring"""


    UpperCamelCase      =			load_dataset("""hf-internal-testing/example-documents"""      ,      split="""test"""							)
    UpperCamelCase      =			dataset[0]["""image"""]

    UpperCamelCase      =			self.tool(lowerCamelCase_      ,      """When is the coffee break?"""							)
    self.assertEqual(lowerCamelCase_      ,      """11-14 to 11:39 a.m."""							)







   def 	lowerCamelCase_  (    self	:	Union[str, Any]							):





    """simple docstring"""


    UpperCamelCase      =			load_dataset("""hf-internal-testing/example-documents"""      ,      split="""test"""							)
    UpperCamelCase      =			dataset[0]["""image"""]

    UpperCamelCase      =			self.remote_tool(lowerCamelCase_      ,      """When is the coffee break?"""							)
    self.assertEqual(lowerCamelCase_      ,      """11-14 to 11:39 a.m."""							)







   def 	lowerCamelCase_  (    self	:	int							):





    """simple docstring"""


    UpperCamelCase      =			load_dataset("""hf-internal-testing/example-documents"""      ,      split="""test"""							)
    UpperCamelCase      =			dataset[0]["""image"""]

    self.tool(document=lowerCamelCase_      ,      question="""When is the coffee break?"""							)







   def 	lowerCamelCase_  (    self	:	Any							):





    """simple docstring"""


    UpperCamelCase      =			load_dataset("""hf-internal-testing/example-documents"""      ,      split="""test"""							)
    UpperCamelCase      =			dataset[0]["""image"""]

    UpperCamelCase      =			self.remote_tool(document=lowerCamelCase_      ,      question="""When is the coffee break?"""							)
    self.assertEqual(lowerCamelCase_      ,      """11-14 to 11:39 a.m."""							)
