

import os
import sys
import unittest


A											=     os.path.abspath(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.join(git_repo_path, 'utils'))

import check_dummies  # noqa: E402
from check_dummies import create_dummy_files, create_dummy_object, find_backend, read_init  # noqa: E402


# Align TRANSFORMERS_PATH in check_dummies with the current path
A											=     os.path.join(git_repo_path, 'src', 'diffusers')


class SCREAMING_SNAKE_CASE  (		unittest.TestCase							):






			"""simple docstring"""




			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= find_backend('    if not is_torch_available():'							)
				self.assertEqual(__UpperCamelCase ,				'torch'							)

				# backend_with_underscore = find_backend("    if not is_tensorflow_text_available():")
				# self.assertEqual(backend_with_underscore, "tensorflow_text")

				snake_case_      		= find_backend('    if not (is_torch_available() and is_transformers_available()):'							)
				self.assertEqual(__UpperCamelCase ,				'torch_and_transformers'							)

				# double_backend_with_underscore = find_backend(
				#    "    if not (is_sentencepiece_available() and is_tensorflow_text_available()):"
				# )
				# self.assertEqual(double_backend_with_underscore, "sentencepiece_and_tensorflow_text")

				snake_case_      		= find_backend(
				    '    if not (is_torch_available() and is_transformers_available() and is_onnx_available()):'							)
				self.assertEqual(__UpperCamelCase ,				'torch_and_transformers_and_onnx'							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= read_init()
				# We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
				self.assertIn('torch' ,				__UpperCamelCase							)
				self.assertIn('torch_and_transformers' ,				__UpperCamelCase							)
				self.assertIn('flax_and_transformers' ,				__UpperCamelCase							)
				self.assertIn('torch_and_transformers_and_onnx' ,				__UpperCamelCase							)

				# Likewise, we can't assert on the exact content of a key
				self.assertIn('UNet2DModel' ,				objects['torch']							)
				self.assertIn('FlaxUNet2DConditionModel' ,				objects['flax']							)
				self.assertIn('StableDiffusionPipeline' ,				objects['torch_and_transformers']							)
				self.assertIn('FlaxStableDiffusionPipeline' ,				objects['flax_and_transformers']							)
				self.assertIn('LMSDiscreteScheduler' ,				objects['torch_and_scipy']							)
				self.assertIn('OnnxStableDiffusionPipeline' ,				objects['torch_and_transformers_and_onnx']							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= create_dummy_object('CONSTANT' ,				'\'torch\''							)
				self.assertEqual(__UpperCamelCase ,				'\nCONSTANT = None\n'							)

				snake_case_      		= create_dummy_object('function' ,				'\'torch\''							)
				self.assertEqual(
				    __UpperCamelCase ,				'\ndef function(*args, **kwargs):\n    requires_backends(function, \'torch\')\n'							)

				snake_case_      		= '\nclass FakeClass(metaclass=DummyObject):\n    _backends = \'torch\'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, \'torch\')\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, \'torch\')\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, \'torch\')\n'
				snake_case_      		= create_dummy_object('FakeClass' ,				'\'torch\''							)
				self.assertEqual(__UpperCamelCase ,				__UpperCamelCase							)







			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= '# This file is autogenerated by the command `make fix-copies`, do not edit.\nfrom ..utils import DummyObject, requires_backends\n\n\nCONSTANT = None\n\n\ndef function(*args, **kwargs):\n    requires_backends(function, ["torch"])\n\n\nclass FakeClass(metaclass=DummyObject):\n    _backends = ["torch"]\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, ["torch"])\n\n    @classmethod\n    def from_config(cls, *args, **kwargs):\n        requires_backends(cls, ["torch"])\n\n    @classmethod\n    def from_pretrained(cls, *args, **kwargs):\n        requires_backends(cls, ["torch"])\n'
				snake_case_      		= create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']}							)
				self.assertEqual(dummy_files['torch'] ,				__UpperCamelCase							)



import unittest

import numpy as np
import torch

from diffusers import ScoreSdeVePipeline, ScoreSdeVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class SCREAMING_SNAKE_CASE  (		unittest.TestCase							):






			"""simple docstring"""




			@property
			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				torch.manual_seed(0							)
				snake_case_      		= UNetaDModel(
				    block_out_channels=(32, 64) ,				layers_per_block=2 ,				sample_size=32 ,				in_channels=3 ,				out_channels=3 ,				down_block_types=('DownBlock2D', 'AttnDownBlock2D') ,				up_block_types=('AttnUpBlock2D', 'UpBlock2D') ,				)
				return model







			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.dummy_uncond_unet
				snake_case_      		= ScoreSdeVeScheduler()

				snake_case_      		= ScoreSdeVePipeline(unet=__UpperCamelCase ,				scheduler=__UpperCamelCase							)
				sde_ve.to(__UpperCamelCase							)
				sde_ve.set_progress_bar_config(disable=__UpperCamelCase							)

				snake_case_      		= torch.manual_seed(0							)
				snake_case_      		= sde_ve(num_inference_steps=2 ,				output_type='numpy' ,				generator=__UpperCamelCase							).images

				snake_case_      		= torch.manual_seed(0							)
				snake_case_      		= sde_ve(num_inference_steps=2 ,				output_type='numpy' ,				generator=__UpperCamelCase ,				return_dict=__UpperCamelCase							)[
				    0
				]

				snake_case_      		= image[0, -3:, -3:, -1]
				snake_case_      		= image_from_tuple[0, -3:, -3:, -1]

				assert image.shape == (1, 32, 32, 3)
				snake_case_      		= np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]							)

				assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2
				assert np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1E-2







@slow
@require_torch
class SCREAMING_SNAKE_CASE  (		unittest.TestCase							):






			"""simple docstring"""




			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= 'google/ncsnpp-church-256'
				snake_case_      		= UNetaDModel.from_pretrained(__UpperCamelCase							)

				snake_case_      		= ScoreSdeVeScheduler.from_pretrained(__UpperCamelCase							)

				snake_case_      		= ScoreSdeVePipeline(unet=__UpperCamelCase ,				scheduler=__UpperCamelCase							)
				sde_ve.to(__UpperCamelCase							)
				sde_ve.set_progress_bar_config(disable=__UpperCamelCase							)

				snake_case_      		= torch.manual_seed(0							)
				snake_case_      		= sde_ve(num_inference_steps=10 ,				output_type='numpy' ,				generator=__UpperCamelCase							).images

				snake_case_      		= image[0, -3:, -3:, -1]

				assert image.shape == (1, 2_56, 2_56, 3)

				snake_case_      		= np.array([0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0]							)

				assert np.abs(image_slice.flatten() - expected_slice							).max() < 1E-2
