





import tempfile
import unittest

import numpy as np
import torch

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

class 	__snake_case					(      unittest.TestCase ):






    '''simple docstring'''





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

      """simple docstring"""





      __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 						A		( self    :		Optional[Any]      ,    snake_case    :		int      ,    snake_case    :		str				):

      """simple docstring"""





      __lowerCAmelCase								=					2

      __lowerCAmelCase								=					torch.rand(snake_case      ,    snake_case      ,    3      ,    2				)
      __lowerCAmelCase								=					torch.rand(snake_case      ,    4      ,    snake_case				)
      __lowerCAmelCase								=					None

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





    def 						A		( self    :		List[Any]				):

      """simple docstring"""





      torch.manual_seed(0				)

      __lowerCAmelCase								=					self.get_constructor_arguments(only_cross_attention=snake_case				)
      __lowerCAmelCase								=					Attention(**snake_case				)

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

      # only self attention

      torch.manual_seed(0				)

      __lowerCAmelCase								=					self.get_constructor_arguments(only_cross_attention=snake_case				)
      __lowerCAmelCase								=					Attention(**snake_case				)

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

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


class 	__snake_case					(      unittest.TestCase ):






    '''simple docstring'''





    def 						A		( self    :		List[Any]				):

      """simple docstring"""





      __lowerCAmelCase								=					DiffusionPipeline.from_pretrained('''hf-internal-testing/tiny-stable-diffusion-pipe'''      ,    safety_checker=snake_case				)

      __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=snake_case				)

      __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(snake_case				)

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

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

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







import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


__lowercase				    =      (30, 487)
__lowercase				    =      (36, 1_024)
__lowercase				    =      (100, 100)
__lowercase				    =      100

__lowercase				    =      datasets.Features(
    {"""text""": ArrayaD(SHAPE_TEST_1, dtype="""float32"""), """image""": ArrayaD(SHAPE_TEST_2, dtype="""float32""")}
)

__lowercase    ,							__lowercase				    =      os.path.split(__file__)
__lowercase				    =      os.path.join(RESULTS_BASEPATH, """results""", RESULTS_FILENAME.replace(""".py""", """.json"""))


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Any  ,				__lowercase:		Optional[Any]  ,				__lowercase:		List[Any]):



  '''simple docstring'''






  with ArrowWriter(features=__lowercase  ,				path=os.path.join(__lowercase  ,				'''beta.arrow''')) as writer:
    for key, record in dummy_data:
      __lowerCAmelCase								=					my_features.encode_example(__lowercase)
      writer.write(__lowercase)
    __lowerCAmelCase						,    __lowerCAmelCase								=					writer.finalize()


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		str  ,				__lowercase:		str):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  for _ in dataset:
    pass


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Tuple  ,				__lowercase:		str):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  dataset.set_format('''numpy''')
  for _ in dataset:
    pass


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		str  ,				__lowercase:		Dict):



  '''simple docstring'''






  __lowerCAmelCase								=					10
  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  for i in range(0  ,				len(__lowercase)  ,				__lowercase):
    __lowerCAmelCase								=					dataset[i : i + batch_size]


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		List[Any]  ,				__lowercase:		Union[str, Any]):



  '''simple docstring'''






  __lowerCAmelCase								=					10
  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  dataset.set_format('''numpy''')
  for i in range(0  ,				len(__lowercase)  ,				__lowercase):
    __lowerCAmelCase								=					dataset[i : i + batch_size]


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Union[str, Any]  ,				__lowercase:		str):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  for col in feats:
    __lowerCAmelCase								=					dataset[col]


@get_duration
def        __SCREAMING_SNAKE_CASE	(				__lowercase:		Optional[int]  ,				__lowercase:		Optional[Any]):



  '''simple docstring'''






  __lowerCAmelCase								=					datasets.Dataset.from_file(
      filename=os.path.join(__lowercase  ,				'''beta.arrow''')  ,				info=datasets.DatasetInfo(features=__lowercase))
  dataset.set_format('''numpy''')
  for col in feats:
    __lowerCAmelCase								=					dataset[col]


def        __SCREAMING_SNAKE_CASE	(				):



  '''simple docstring'''






  __lowerCAmelCase								=					{}
  __lowerCAmelCase								=					(
      read_unformated,
      read_formatted_as_numpy,
      read_batch_unformated,
      read_batch_formatted_as_numpy,
      read_col_unformated,
      read_col_formatted_as_numpy,
  )
  with tempfile.TemporaryDirectory() as tmp_dir:
    __lowerCAmelCase								=					datasets.Features({'''image''': ArrayaD(__lowercase  ,				dtype='''float32''')})
    __lowerCAmelCase								=					generate_examples(features=__lowercase  ,				num_examples=__lowercase)
    __lowerCAmelCase								=					write(__lowercase  ,				__lowercase  ,				__lowercase)
    for read_func in read_functions:
      __lowerCAmelCase								=					read_func(__lowercase  ,				__lowercase)

  with tempfile.TemporaryDirectory() as tmp_dir:
    # don't use fixed length for fair comparison
    # feats = datasets.Features(
    #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
    # )
    __lowerCAmelCase								=					datasets.Features({'''image''': datasets.Sequence(datasets.Sequence(datasets.Value('''float32''')))})
    __lowerCAmelCase								=					generate_examples(
        features=__lowercase  ,				num_examples=__lowercase  ,				seq_shapes={'''image''': SPEED_TEST_SHAPE})
    __lowerCAmelCase								=					write(__lowercase  ,				__lowercase  ,				__lowercase)
    for read_func in read_functions:
      __lowerCAmelCase								=					read_func(__lowercase  ,				__lowercase)

  with tempfile.TemporaryDirectory() as tmp_dir:
    # don't use fixed length for fair comparison
    # feats = datasets.Features(
    #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
    # )
    __lowerCAmelCase								=					datasets.Features({'''image''': datasets.Sequence(datasets.Value('''float32'''))})
    __lowerCAmelCase								=					generate_examples(
        features=__lowercase  ,				num_examples=__lowercase  ,				seq_shapes={'''image''': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}  ,				)
    __lowerCAmelCase								=					write(__lowercase  ,				__lowercase  ,				__lowercase)
    for read_func in read_functions:
      __lowerCAmelCase								=					read_func(__lowercase  ,				__lowercase)

  with open(__lowercase  ,				'''wb''') as f:
    f.write(json.dumps(__lowercase).encode('''utf-8'''))


if __name__ == "__main__":  # useful to run the profiler
      benchmark_array_xd()
