code: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import posixpath
  from pathlib import Path
  from unittest.mock import patch
  
  import pytest
  from fsspec.implementations.local import AbstractFileSystem, LocalFileSystem, stringify_path
  from fsspec.registry import _registry as _fsspec_registry
  
  
  
  class 							__UpperCAmelCase  (       A__						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    '''mock'''
  
  
  
  
  
  
  
  		def __init__(self						:      List[Any]      ,      *_lowerCAmelCase						:      Tuple      ,      _lowerCAmelCase						:      List[str]      ,      **_lowerCAmelCase						:      str      ):
  			super().__init__()
  			A							  =      LocalFileSystem(*a_      ,      **a_      )
  			A							  =      Path(a_      ).resolve().as_posix() + """/"""
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ,      _lowerCAmelCase						:      Tuple      ,      *_lowerCAmelCase						:      Union[str, Any]      ,      **_lowerCAmelCase						:      Union[str, Any]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.mkdir(a_      ,      *a_      ,      **a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      Union[str, Any]      ,      _lowerCAmelCase						:      Union[str, Any]      ,      *_lowerCAmelCase						:      Tuple      ,      **_lowerCAmelCase						:      Optional[Any]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.makedirs(a_      ,      *a_      ,      **a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      List[Any]      ,      _lowerCAmelCase						:      Optional[int]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.rmdir(a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ,      _lowerCAmelCase						:      Optional[Any]      ,      _lowerCAmelCase						:      Optional[int]=True      ,      *_lowerCAmelCase						:      Union[str, Any]      ,      **_lowerCAmelCase						:      Dict      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			A							  =      self._fs.ls(a_      ,      detail=a_      ,      *a_      ,      **a_      )
  			if detail:
  				return [{**info, "name": info["name"][len(self.local_root_dir      ) :]} for info in out]
  			else:
  				return [name[len(self.local_root_dir      ) :] for name in out]
  
  
  
  
  
  
  
  		def        A     (self						:      List[Any]      ,      _lowerCAmelCase						:      Optional[int]      ,      *_lowerCAmelCase						:      List[Any]      ,      **_lowerCAmelCase						:      Optional[int]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			A							  =      dict(self._fs.info(a_      ,      *a_      ,      **a_      )      )
  			A							  =      out["""name"""][len(self.local_root_dir      ) :]
  			return out
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      List[Any]      ,      *_lowerCAmelCase						:      List[Any]      ,      **_lowerCAmelCase						:      Dict      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.cp_file(a_      ,      a_      ,      *a_      ,      **a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      Tuple      ,      _lowerCAmelCase						:      str      ,      *_lowerCAmelCase						:      str      ,      **_lowerCAmelCase						:      int      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.rm_file(a_      ,      *a_      ,      **a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      Tuple      ,      _lowerCAmelCase						:      List[Any]      ,      *_lowerCAmelCase						:      Any      ,      **_lowerCAmelCase						:      List[Any]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.rm(a_      ,      *a_      ,      **a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      List[Any]      ,      *_lowerCAmelCase						:      List[Any]      ,      **_lowerCAmelCase						:      List[Any]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs._open(a_      ,      *a_      ,      **a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      Tuple      ,      _lowerCAmelCase						:      str      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.created(a_      )
  
  
  
  
  
  
  
  		def        A     (self						:      int      ,      _lowerCAmelCase						:      List[str]      ):
  			A							  =      posixpath.join(self.local_root_dir      ,      self._strip_protocol(a_      )      )
  			return self._fs.modified(a_      )
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (cls						:      Dict      ,      _lowerCAmelCase						:      int      ):
  			A							  =      stringify_path(a_      )
  			if path.startswith("""mock://"""      ):
  				A							  =      path[7:]
  			return path
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       A__						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    '''tmp'''
  		__lowerCAmelCase 				=    None
  
  
  
  
  
  
  
  		def __init__(self						:      Optional[int]      ,      *_lowerCAmelCase						:      str      ,      **_lowerCAmelCase						:      str      ):
  			assert self.tmp_dir is not None, "TmpDirFileSystem.tmp_dir is not set"
  			super().__init__(*a_      ,      **a_      ,      local_root_dir=self.tmp_dir      ,      auto_mkdir=a_      )
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (cls						:      int      ,      _lowerCAmelCase						:      Any      ):
  			A							  =      stringify_path(a_      )
  			if path.startswith("""tmp://"""      ):
  				A							  =      path[6:]
  			return path
  
  
  
  
  
  
  @pytest.fixture
  def    __a (     )      ->Optional[Any]:
  
  
  
  	"""simple docstring"""
  
  
  
  
  
  
  
  	A							  =      MockFileSystem
  	A							  =      TmpDirFileSystem
  	yield
  	del _fsspec_registry["mock"]
  	del _fsspec_registry["tmp"]
  
  
  
  
  @pytest.fixture
  def    __a (     UpperCAmelCase       ,			UpperCAmelCase    )      ->Optional[int]:
  
  
  
  	"""simple docstring"""
  
  
  
  
  
  
  
  	A							  =      tmp_path_factory.mktemp("""mockfs"""    )
  	return MockFileSystem(local_root_dir=UpperCamelCase__       ,			auto_mkdir=UpperCamelCase__    )
  
  
  
  
  @pytest.fixture
  def    __a (     UpperCAmelCase       ,			UpperCAmelCase    )      ->Dict:
  
  
  
  	"""simple docstring"""
  
  
  
  
  
  
  
  	A							  =      tmp_path_factory.mktemp("""tmpfs"""    )
  	with patch.object(UpperCamelCase__       ,			"""tmp_dir"""       ,			UpperCamelCase__    ):
  		yield TmpDirFileSystem()
  
  
style_context: 
  
  
  
  
  
  '''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        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
  			A							  =      10
  
  			if only_cross_attention:
  				A							  =      12
  			else:
  				# when only cross attention is not set, the cross attention dim must be the same as the query dim
  				A							  =      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[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
  			A							  =      2
  
  			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
  			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
  			A							  =      None
  
  			return {
  			    "hidden_states": hidden_states,
  			    "encoder_hidden_states": encoder_hidden_states,
  			    "attention_mask": attention_mask,
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ):
  			# self and cross attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is not None      )
  			self.assertTrue(attn.to_v is not None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			# only self attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is None      )
  			self.assertTrue(attn.to_v is None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ):
  			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			# the initial conversion succeeds
  			A							  =      DiffusionPipeline.from_pretrained(
  			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      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
  				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      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      )      )
  
  