

'''simple docstring'''

import os
import sys
import unittest


UpperCAmelCase					 =	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
UpperCAmelCase					 =	os.path.join(git_repo_path, '''src''', '''transformers''')

UpperCAmelCase					 =	'''
{0} = None
'''

UpperCAmelCase					 =	'''
class {0}(metaclass=DummyObject):
    _backends = {1}

    def __init__(self, *args, **kwargs):
        requires_backends(self, {1})
'''


UpperCAmelCase					 =	'''
def {0}(*args, **kwargs):
    requires_backends({0}, {1})
'''


class 	lowerCAmelCase  (   unittest.TestCase ):







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=find_backend('    _import_structure["models.albert"].append("AlbertTokenizerFast")'   )
            self.assertIsNone(__lowercase   )

            __lowercase      		=find_backend('    if not is_tokenizers_available():'   )
            self.assertEqual(__lowercase    ,   'tokenizers'   )

            __lowercase      		=find_backend('    if not is_tensorflow_text_available():'   )
            self.assertEqual(__lowercase    ,   'tensorflow_text'   )

            __lowercase      		=find_backend('    if not (is_sentencepiece_available() and is_tokenizers_available()):'   )
            self.assertEqual(__lowercase    ,   'sentencepiece_and_tokenizers'   )

            __lowercase      		=find_backend(
                '    if not (is_sentencepiece_available() and is_tensorflow_text_available()):'   )
            self.assertEqual(__lowercase    ,   'sentencepiece_and_tensorflow_text'   )

            __lowercase      		=find_backend(
                '    if not (is_sentencepiece_available() and is_tokenizers_available() and is_vision_available()):'   )
            self.assertEqual(__lowercase    ,   'sentencepiece_and_tokenizers_and_vision'   )







       def       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=read_init()
            # We don't assert on the exact list of keys to allow for smooth grow of backend-specific objects
            self.assertIn('torch'    ,   __lowercase   )
            self.assertIn('tensorflow_text'    ,   __lowercase   )
            self.assertIn('sentencepiece_and_tokenizers'    ,   __lowercase   )

            # Likewise, we can't assert on the exact content of a key
            self.assertIn('BertModel'    ,   objects['torch']   )
            self.assertIn('TFBertModel'    ,   objects['tf']   )
            self.assertIn('FlaxBertModel'    ,   objects['flax']   )
            self.assertIn('BertModel'    ,   objects['torch']   )
            self.assertIn('TFBertTokenizer'    ,   objects['tensorflow_text']   )
            self.assertIn('convert_slow_tokenizer'    ,   objects['sentencepiece_and_tokenizers']   )







       def       snake_case (				self      :			List[Any]   ):







            """simple docstring"""







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

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

            __lowercase      		='\nclass FakeClass(metaclass=DummyObject):\n    _backends = \'torch\'\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, \'torch\')\n'
            __lowercase      		=create_dummy_object('FakeClass'    ,   '\'torch\''   )
            self.assertEqual(__lowercase    ,   __lowercase   )







       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







            __lowercase      		='# 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'
            __lowercase      		=create_dummy_files({'torch': ['CONSTANT', 'function', 'FakeClass']}   )
            self.assertEqual(dummy_files['torch']    ,   __lowercase   )





'''simple docstring'''

import unittest

import numpy as np
import torch

from diffusers import PNDMPipeline, PNDMScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()


class 	lowerCAmelCase  (   unittest.TestCase ):







       @property
       def       snake_case (				self      :			str   ):







            """simple docstring"""







            torch.manual_seed(0   )
            __lowercase      		=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       snake_case (				self      :			int   ):







            """simple docstring"""







            __lowercase      		=self.dummy_uncond_unet
            __lowercase      		=PNDMScheduler()

            __lowercase      		=PNDMPipeline(unet=__lowercase    ,   scheduler=__lowercase   )
            pndm.to(__lowercase   )
            pndm.set_progress_bar_config(disable=__lowercase   )

            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   num_inference_steps=20    ,   output_type='numpy'   ).images

            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   num_inference_steps=20    ,   output_type='numpy'    ,   return_dict=__lowercase   )[0]

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

            assert image.shape == (1, 32, 32, 3)
            __lowercase      		=np.array([1.0, 1.0, 0.0, 1.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 	lowerCAmelCase  (   unittest.TestCase ):







       def       snake_case (				self      :			Union[str, Any]   ):







            """simple docstring"""







            __lowercase      		='google/ddpm-cifar10-32'

            __lowercase      		=UNetaDModel.from_pretrained(__lowercase   )
            __lowercase      		=PNDMScheduler()

            __lowercase      		=PNDMPipeline(unet=__lowercase    ,   scheduler=__lowercase   )
            pndm.to(__lowercase   )
            pndm.set_progress_bar_config(disable=__lowercase   )
            __lowercase      		=torch.manual_seed(0   )
            __lowercase      		=pndm(generator=__lowercase    ,   output_type='numpy'   ).images

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

            assert image.shape == (1, 32, 32, 3)
            __lowercase      		=np.array([0.1_5_6_4, 0.1_4_6_4_5, 0.1_4_0_6, 0.1_4_7_1_5, 0.1_2_4_2_5, 0.1_4_0_4_5, 0.1_3_1_1_5, 0.1_2_1_7_5, 0.1_2_5]   )

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


