





"""simple docstring"""




import os
import tempfile
import unittest

import numpy as np
import torch

from diffusers.models import ModelMixin, UNetaDConditionModel
from diffusers.models.attention_processor import AttnProcessor, LoRAAttnProcessor
from diffusers.utils import (
    floats_tensor,
    logging,
    skip_mps,
    torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()

lowerCamelCase_	:		Tuple         =   logging.get_logger(__name__)





def 		UpperCAmelCase__						(	_UpperCAmelCase					,   _UpperCAmelCase = True   ):






  """simple docstring"""





  A_    :			Tuple									=    {}
  for name in model.attn_processors.keys():
    A_    :			Optional[Any]									=    name.endswith('attn2.processor'   ) and not (
        name.startswith('transformer_in'   ) or 'temp_attentions' in name.split('.'   )
    )
    A_    :			int									=    model.config.cross_attention_dim if has_cross_attention else None
    if name.startswith('mid_block'   ):
      A_    :			Tuple									=    model.config.block_out_channels[-1]
    elif name.startswith('up_blocks'   ):
      A_    :			Tuple									=    int(name[len('up_blocks.'   )]   )
      A_    :			Optional[Any]									=    list(reversed(model.config.block_out_channels   )   )[block_id]
    elif name.startswith('down_blocks'   ):
      A_    :			List[str]									=    int(name[len('down_blocks.'   )]   )
      A_    :			int									=    model.config.block_out_channels[block_id]
    elif name.startswith('transformer_in'   ):
      # Note that the `8 * ...` comes from: https://github.com/huggingface/diffusers/blob/7139f0e874f10b2463caa8cbd585762a309d12d6/src/diffusers/models/unet_3d_condition.py#L148
      A_    :			Tuple									=    8 * model.config.attention_head_dim

    A_    :			Tuple									=    LoRAAttnProcessor(hidden_size=_UpperCAmelCase					,   cross_attention_dim=_UpperCAmelCase   )
    A_    :			Optional[int]									=    lora_attn_procs[name].to(model.device   )

    if mock_weights:
      # add 1 to weights to mock trained weights
      with torch.no_grad():
        lora_attn_procs[name].to_q_lora.up.weight += 1
        lora_attn_procs[name].to_k_lora.up.weight += 1
        lora_attn_procs[name].to_v_lora.up.weight += 1
        lora_attn_procs[name].to_out_lora.up.weight += 1

  return lora_attn_procs





@skip_mps
class 					_UpperCAmelCase    (  UpperCAmelCase__     ,  UpperCAmelCase__     ,  unittest.TestCase						):



      '''simple docstring'''
      lowercase_ :					Optional[Any]             =  UNetaDConditionModel
      lowercase_ :					Optional[Any]             =  """sample"""

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Optional[int]									=    4
        A_    :			Optional[int]									=    4
        A_    :			str									=    4
        A_    :			List[str]									=    (3_2, 3_2)

        A_    :			Tuple									=    floats_tensor((batch_size, num_channels, num_frames) + sizes    ).to(snake_case_    )
        A_    :			List[str]									=    torch.tensor([1_0]    ).to(snake_case_    )
        A_    :			Tuple									=    floats_tensor((batch_size, 4, 3_2)    ).to(snake_case_    )

        return {"sample": noise, "timestep": time_step, "encoder_hidden_states": encoder_hidden_states}

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (4, 4, 3_2, 3_2)

      @property
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        return (4, 4, 3_2, 3_2)

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_    :			Dict									=    {
            'block_out_channels': (3_2, 6_4),
            'down_block_types': (
                'CrossAttnDownBlock3D',
                'DownBlock3D',
            ),
            'up_block_types': ('UpBlock3D', 'CrossAttnUpBlock3D'),
            'cross_attention_dim': 3_2,
            'attention_head_dim': 8,
            'out_channels': 4,
            'in_channels': 4,
            'layers_per_block': 1,
            'sample_size': 3_2,
        }
        A_    :			List[Any]									=    self.dummy_input
        return init_dict, inputs_dict

      @unittest.skipIf(
          torch_device != 'cuda' or not is_xformers_available()   , reason='XFormers attention is only available with CUDA and `xformers` installed'   , )
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Dict									=    self.prepare_init_args_and_inputs_for_common()
        A_    :			str									=    self.model_class(**snake_case_    )

        model.enable_xformers_memory_efficient_attention()

        assert (
            model.mid_block.attentions[0].transformer_blocks[0].attna.processor.__class__.__name__
            == "XFormersAttnProcessor"
        ), "xformers is not enabled"

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Optional[Any]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Optional[int]									=    3_2

        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_    )

          if isinstance(snake_case_   , snake_case_    ):
            A_    :			Any									=    output.sample

        self.assertIsNotNone(snake_case_    )
        A_    :			Any									=    inputs_dict['sample'].shape
        self.assertEqual(output.shape   , snake_case_   , 'Input and output shapes do not match'    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Dict									=    self.prepare_init_args_and_inputs_for_common()
        A_    :			Optional[int]									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        with torch.no_grad():
          # Warmup pass when using mps (see #372)
          if torch_device == "mps" and isinstance(snake_case_   , snake_case_    ):
            model(**self.dummy_input    )

          A_    :			List[str]									=    model(**snake_case_    )
          if isinstance(snake_case_   , snake_case_    ):
            A_    :			str									=    first.sample

          A_    :			Dict									=    model(**snake_case_    )
          if isinstance(snake_case_   , snake_case_    ):
            A_    :			int									=    second.sample

        A_    :			Optional[Any]									=    first.cpu().numpy()
        A_    :			Any									=    second.cpu().numpy()
        A_    :			str									=    out_a[~np.isnan(snake_case_    )]
        A_    :			Tuple									=    out_a[~np.isnan(snake_case_    )]
        A_    :			List[str]									=    np.amax(np.abs(out_a - out_a    )    )
        self.assertLessEqual(snake_case_   , 1E-5    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Dict									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Optional[Any]									=    8

        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        model.set_attention_slice('auto'    )
        with torch.no_grad():
          A_    :			List[str]									=    model(**snake_case_    )
        assert output is not None

        model.set_attention_slice('max'    )
        with torch.no_grad():
          A_    :			Optional[int]									=    model(**snake_case_    )
        assert output is not None

        model.set_attention_slice(2    )
        with torch.no_grad():
          A_    :			Any									=    model(**snake_case_    )
        assert output is not None

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			List[str]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			List[str]									=    8

        A_    :			Any									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Optional[Any]									=    model(**snake_case_    ).sample

        A_    :			Optional[Any]									=    create_lora_layers(snake_case_    )

        # make sure we can set a list of attention processors
        model.set_attn_processor(snake_case_    )
        model.to(snake_case_    )

        # test that attn processors can be set to itself
        model.set_attn_processor(model.attn_processors    )

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.0}    ).sample
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample
          A_    :			str									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        assert (samplea - samplea).abs().max() < 3E-3
        assert (samplea - samplea).abs().max() < 3E-3

        # sample 2 and sample 3 should be different
        assert (samplea - samplea).abs().max() > 3E-3

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Any									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Union[str, Any]									=    8

        torch.manual_seed(0    )
        A_    :			Optional[int]									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Tuple									=    model(**snake_case_    ).sample

        A_    :			int									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.bin'    )    )    )
          torch.manual_seed(0    )
          A_    :			List[str]									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          new_model.load_attn_procs(snake_case_    )

        with torch.no_grad():
          A_    :			int									=    new_model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        assert (sample - new_sample).abs().max() < 1E-3

        # LoRA and no LoRA should NOT be the same
        assert (sample - old_sample).abs().max() > 1E-4

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Optional[Any]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Dict									=    8

        torch.manual_seed(0    )
        A_    :			Optional[Any]									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Union[str, Any]									=    model(**snake_case_    ).sample

        A_    :			Optional[int]									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        with torch.no_grad():
          A_    :			List[str]									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_   , safe_serialization=snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.safetensors'    )    )    )
          torch.manual_seed(0    )
          A_    :			Tuple									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          new_model.load_attn_procs(snake_case_    )

        with torch.no_grad():
          A_    :			List[str]									=    new_model(**snake_case_   , cross_attention_kwargs={'scale': 0.5}    ).sample

        assert (sample - new_sample).abs().max() < 3E-3

        # LoRA and no LoRA should NOT be the same
        assert (sample - old_sample).abs().max() > 1E-4

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Any									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Union[str, Any]									=    8

        torch.manual_seed(0    )
        A_    :			Any									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        A_    :			Optional[int]									=    create_lora_layers(snake_case_   , mock_weights=snake_case_    )
        model.set_attn_processor(snake_case_    )
        # Saving as torch, properly reloads with directly filename
        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.bin'    )    )    )
          torch.manual_seed(0    )
          A_    :			List[str]									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          new_model.load_attn_procs(snake_case_   , weight_name='pytorch_lora_weights.bin'    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Tuple									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Dict									=    8

        torch.manual_seed(0    )
        A_    :			Dict									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        A_    :			Any									=    create_lora_layers(snake_case_   , mock_weights=snake_case_    )
        model.set_attn_processor(snake_case_    )
        # Saving as torch, properly reloads with directly filename
        with tempfile.TemporaryDirectory() as tmpdirname:
          model.save_attn_procs(snake_case_    )
          self.assertTrue(os.path.isfile(os.path.join(snake_case_   , 'pytorch_lora_weights.bin'    )    )    )
          torch.manual_seed(0    )
          A_    :			Optional[int]									=    self.model_class(**snake_case_    )
          new_model.to(snake_case_    )
          with self.assertRaises(snake_case_    ) as e:
            new_model.load_attn_procs(snake_case_   , use_safetensors=snake_case_    )
          self.assertIn('Error no file named pytorch_lora_weights.safetensors'   , str(e.exception    )    )

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			Optional[int]									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			int									=    8

        torch.manual_seed(0    )
        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )

        with torch.no_grad():
          A_    :			Optional[int]									=    model(**snake_case_    ).sample

        A_    :			Tuple									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_   , cross_attention_kwargs={'scale': 0.0}    ).sample

        model.set_attn_processor(AttnProcessor()    )

        with torch.no_grad():
          A_    :			List[Any]									=    model(**snake_case_    ).sample

        assert (sample - new_sample).abs().max() < 1E-4
        assert (sample - old_sample).abs().max() < 3E-3

      @unittest.skipIf(
          torch_device != 'cuda' or not is_xformers_available()   , reason='XFormers attention is only available with CUDA and `xformers` installed'   , )
      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			str									=    self.prepare_init_args_and_inputs_for_common()

        A_    :			Union[str, Any]									=    4

        torch.manual_seed(0    )
        A_    :			int									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        A_    :			Optional[Any]									=    create_lora_layers(snake_case_    )
        model.set_attn_processor(snake_case_    )

        # default
        with torch.no_grad():
          A_    :			Any									=    model(**snake_case_    ).sample

          model.enable_xformers_memory_efficient_attention()
          A_    :			Optional[Any]									=    model(**snake_case_    ).sample

          model.disable_xformers_memory_efficient_attention()
          A_    :			List[Any]									=    model(**snake_case_    ).sample

        assert (sample - on_sample).abs().max() < 1E-4
        assert (sample - off_sample).abs().max() < 1E-4

      def    lowerCamelCase_     (	self    ):

        """simple docstring"""

        A_		,			A_    :			List[Any]									=    self.prepare_init_args_and_inputs_for_common()
        A_    :			Any									=    3_2

        A_    :			str									=    self.model_class(**snake_case_    )
        model.to(snake_case_    )
        model.eval()

        with torch.no_grad():
          A_    :			Dict									=    model(**snake_case_    )[0]

        model.enable_forward_chunking()
        with torch.no_grad():
          A_    :			Tuple									=    model(**snake_case_    )[0]

        self.assertEqual(output.shape   , output_a.shape   , 'Shape doesn\'t match'    )
        assert np.abs(output.cpu() - output_a.cpu()    ).max() < 1E-2


# (todo: sayakpaul) implement SLOW tests.


from __future__ import annotations






def        lowerCamelCase_  (				UpperCAmelCase_				:  int						)    ->					list[int]:




	'''simple docstring'''




	_UpperCamelCase :		Any								=    [0] * max_one

	for i in range(0				,							UpperCAmelCase_						):
		_UpperCamelCase :		List[Any]								=    i

	for i in range(2				,							UpperCAmelCase_						):
		if totients[i] == i:
			for j in range(UpperCAmelCase_				,							UpperCAmelCase_				,							UpperCAmelCase_						):
				totients[j] -= totients[j] // i

	return totients






def        lowerCamelCase_  (				UpperCAmelCase_				:  int				,							UpperCAmelCase_				:  int						)    ->					bool:




	'''simple docstring'''




	return sorted(str(UpperCAmelCase_						)						) == sorted(str(UpperCAmelCase_						)						)






def        lowerCamelCase_  (				UpperCAmelCase_				:  int = 1_0_0_0_0_0_0_0						)    ->					int:




	'''simple docstring'''




	_UpperCamelCase :		List[Any]								=    1  # i
	_UpperCamelCase :		List[str]								=    0  # φ(i)
	_UpperCamelCase :		str								=    get_totients(max_n + 1						)

	for i in range(2				,							max_n + 1						):
		_UpperCamelCase :		List[str]								=    totients[i]

		if i * min_denominator < min_numerator * t and has_same_digits(UpperCAmelCase_				,							UpperCAmelCase_						):
			_UpperCamelCase :		Optional[int]								=    i
			_UpperCamelCase :		Tuple								=    t

	return min_numerator


if __name__ == "__main__":
							print(f'{solution() = }')




