
"""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 					A__			(      unittest.TestCase):

		def 				__lowerCamelCase				( self			,						_SCREAMING_SNAKE_CASE = False					):
			__lowerCAmelCase     :		int		     = 10

			if only_cross_attention:
				__lowerCAmelCase     :		Optional[Any]		     = 12
			else:
				# when only cross attention is not set, the cross attention dim must be the same as the query dim
				__lowerCAmelCase     :		Tuple		     = 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			,						_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE					):
			__lowerCAmelCase     :		int		     = 2

			__lowerCAmelCase     :		Tuple		     = torch.rand(_SCREAMING_SNAKE_CASE			,						_SCREAMING_SNAKE_CASE			,						3			,						2					)
			__lowerCAmelCase     :		int		     = torch.rand(_SCREAMING_SNAKE_CASE			,						4			,						_SCREAMING_SNAKE_CASE					)
			__lowerCAmelCase     :		Tuple		     = None

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







		def 				__lowerCamelCase				( self					):
			# self and cross attention

			torch.manual_seed(0					)

			__lowerCAmelCase     :		str		     = self.get_constructor_arguments(only_cross_attention=_SCREAMING_SNAKE_CASE					)
			__lowerCAmelCase     :		int		     = Attention(**_SCREAMING_SNAKE_CASE					)

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

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

			__lowerCAmelCase     :		Any		     = attn(**_SCREAMING_SNAKE_CASE					)

			# only self attention

			torch.manual_seed(0					)

			__lowerCAmelCase     :		Dict		     = self.get_constructor_arguments(only_cross_attention=_SCREAMING_SNAKE_CASE					)
			__lowerCAmelCase     :		int		     = Attention(**_SCREAMING_SNAKE_CASE					)

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

			__lowerCAmelCase     :		Optional[int]		     = self.get_forward_arguments(
			    query_dim=constructor_args['query_dim']			,						added_kv_proj_dim=constructor_args['added_kv_proj_dim']					)

			__lowerCAmelCase     :		Union[str, Any]		     = attn(**_SCREAMING_SNAKE_CASE					)

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




class 					A__			(      unittest.TestCase):

		def 				__lowerCamelCase				( self					):
			__lowerCAmelCase     :		str		     = DiffusionPipeline.from_pretrained('hf-internal-testing/tiny-stable-diffusion-pipe'			,						safety_checker=_SCREAMING_SNAKE_CASE					)

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

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

			__lowerCAmelCase     :		str		     = 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(_SCREAMING_SNAKE_CASE					)

				# can also load the converted weights
				__lowerCAmelCase     :		Optional[Any]		     = DiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE			,						device_map='sequential'			,						safety_checker=_SCREAMING_SNAKE_CASE					)

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

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

"""simple docstring"""







def        __lowerCAmelCase							(_UpperCamelCase = 10**6						):

	if n <= 0:
		raise ValueError('Please enter an integer greater than 0'						)

	__lowerCAmelCase     :		Optional[Any]		     = list(range(n + 1						)						)
	for number in range(2			,    n + 1						):
		if phi[number] == number:
			phi[number] -= 1
			for multiple in range(number * 2			,    n + 1			,    _UpperCamelCase						):
				__lowerCAmelCase     :		Tuple		     = (phi[multiple] // number) * (number - 1)

	__lowerCAmelCase     :		Any		     = 1
	for number in range(1			,    n + 1						):
		if (answer / phi[answer]) < (number / phi[number]):
			__lowerCAmelCase     :		List[Any]		     = number

	return answer


if __name__ == "__main__":
							print(solution())