





import tempfile
import unittest

from diffusers import (
    DDIMScheduler,
    DDPMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    PNDMScheduler,
    logging,
)
from diffusers.configuration_utils import ConfigMixin, register_to_config
from diffusers.utils.testing_utils import CaptureLogger

class 			__A(    __UpperCAmelCase			):
     __A							=							"""config.json"""

     @register_to_config
     def __init__(   self,							A=2,							A=5,							A=(2, 5),							A="for diffusion",							A=[1, 3],							):





            """simple docstring"""

            pass






class 			__A(    __UpperCAmelCase			):
     __A							=							"""config.json"""

     @register_to_config
     def __init__(   self,							A=2,							A=5,							A=(2, 5),							A="for diffusion",							A=[1, 3],							):





            """simple docstring"""

            pass






class 			__A(    __UpperCAmelCase			):
     __A							=							"""config.json"""

     @register_to_config
     def __init__(   self,							A=2,							A=5,							A=(2, 5),							A="for diffusion",							A=[1, 3],							A=[1, 3],							):





            """simple docstring"""

            pass






class 			__A(    __UpperCAmelCase			):
     __A							=							"""config.json"""

     @register_to_config
     def __init__(   self,							A=2,							A=5,							A=(2, 5),							A="for diffusion",							A=[1, 5],							A=[5, 4],							):





            """simple docstring"""

            pass






class 			__A(    unittest.TestCase			):

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            with self.assertRaises(A   ):
                   ConfigMixin.load_config('''dummy_path'''   )

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    SampleObject()
            _UpperCamelCase														=    obj.config
            assert config["a"] == 2
            assert config["b"] == 5
            assert config["c"] == (2, 5)
            assert config["d"] == "for diffusion"
            assert config["e"] == [1, 3]

            # init ignore private arguments
            _UpperCamelCase														=    SampleObject(_name_or_path='''lalala'''   )
            _UpperCamelCase														=    obj.config
            assert config["a"] == 2
            assert config["b"] == 5
            assert config["c"] == (2, 5)
            assert config["d"] == "for diffusion"
            assert config["e"] == [1, 3]

            # can override default
            _UpperCamelCase														=    SampleObject(c=6   )
            _UpperCamelCase														=    obj.config
            assert config["a"] == 2
            assert config["b"] == 5
            assert config["c"] == 6
            assert config["d"] == "for diffusion"
            assert config["e"] == [1, 3]

            # can use positional arguments.
            _UpperCamelCase														=    SampleObject(1,							c=6   )
            _UpperCamelCase														=    obj.config
            assert config["a"] == 1
            assert config["b"] == 5
            assert config["c"] == 6
            assert config["d"] == "for diffusion"
            assert config["e"] == [1, 3]

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    SampleObject()
            _UpperCamelCase														=    obj.config

            assert config["a"] == 2
            assert config["b"] == 5
            assert config["c"] == (2, 5)
            assert config["d"] == "for diffusion"
            assert config["e"] == [1, 3]

            with tempfile.TemporaryDirectory() as tmpdirname:
                   obj.save_config(A   )
                   _UpperCamelCase														=    SampleObject.from_config(SampleObject.load_config(A   )   )
                   _UpperCamelCase														=    new_obj.config

            # unfreeze configs
            _UpperCamelCase														=    dict(A   )
            _UpperCamelCase														=    dict(A   )

            assert config.pop('''c'''   ) == (2, 5)  # instantiated as tuple
            assert new_config.pop('''c'''   ) == [2, 5]  # saved & loaded as list because of json
            config.pop('''_use_default_values'''   )
            assert config == new_config

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    logging.get_logger('''diffusers.configuration_utils'''   )
            # 30 for warning
            logger.setLevel(30   )

            with CaptureLogger(A   ) as cap_logger:
                   _UpperCamelCase														=    DDIMScheduler.from_pretrained(
                       '''hf-internal-testing/tiny-stable-diffusion-torch''',							subfolder='''scheduler'''   )

            assert ddim.__class__ == DDIMScheduler
            # no warning should be thrown
            assert cap_logger.out == ""

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    logging.get_logger('''diffusers.configuration_utils'''   )
            # 30 for warning
            logger.setLevel(30   )

            with CaptureLogger(A   ) as cap_logger:
                   _UpperCamelCase														=    EulerDiscreteScheduler.from_pretrained(
                       '''hf-internal-testing/tiny-stable-diffusion-torch''',							subfolder='''scheduler'''   )

            assert euler.__class__ == EulerDiscreteScheduler
            # no warning should be thrown
            assert cap_logger.out == ""

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    logging.get_logger('''diffusers.configuration_utils'''   )
            # 30 for warning
            logger.setLevel(30   )

            with CaptureLogger(A   ) as cap_logger:
                   _UpperCamelCase														=    EulerAncestralDiscreteScheduler.from_pretrained(
                       '''hf-internal-testing/tiny-stable-diffusion-torch''',							subfolder='''scheduler'''   )

            assert euler.__class__ == EulerAncestralDiscreteScheduler
            # no warning should be thrown
            assert cap_logger.out == ""

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    logging.get_logger('''diffusers.configuration_utils'''   )
            # 30 for warning
            logger.setLevel(30   )

            with CaptureLogger(A   ) as cap_logger:
                   _UpperCamelCase														=    PNDMScheduler.from_pretrained(
                       '''hf-internal-testing/tiny-stable-diffusion-torch''',							subfolder='''scheduler'''   )

            assert pndm.__class__ == PNDMScheduler
            # no warning should be thrown
            assert cap_logger.out == ""

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    logging.get_logger('''diffusers.configuration_utils'''   )
            # 30 for warning
            logger.setLevel(30   )

            with CaptureLogger(A   ) as cap_logger:
                   _UpperCamelCase														=    DDPMScheduler.from_pretrained(
                       '''hf-internal-testing/tiny-stable-diffusion-torch''',							subfolder='''scheduler''',							prediction_type='''sample''',							beta_end=8,							)

            with CaptureLogger(A   ) as cap_logger_a:
                   _UpperCamelCase														=    DDPMScheduler.from_pretrained('''google/ddpm-celebahq-256''',							beta_start=88   )

            assert ddpm.__class__ == DDPMScheduler
            assert ddpm.config.prediction_type == "sample"
            assert ddpm.config.beta_end == 8
            assert ddpm_a.config.beta_start == 88

            # no warning should be thrown
            assert cap_logger.out == ""
            assert cap_logger_a.out == ""

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    logging.get_logger('''diffusers.configuration_utils'''   )
            # 30 for warning
            logger.setLevel(30   )

            with CaptureLogger(A   ) as cap_logger:
                   _UpperCamelCase														=    DPMSolverMultistepScheduler.from_pretrained(
                       '''hf-internal-testing/tiny-stable-diffusion-torch''',							subfolder='''scheduler'''   )

            assert dpm.__class__ == DPMSolverMultistepScheduler
            # no warning should be thrown
            assert cap_logger.out == ""



     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            _UpperCamelCase														=    SampleObject()

            _UpperCamelCase														=    {k: v for k, v in config.config.items() if not k.startswith('''_'''   )}

            # make sure that default config has all keys in `_use_default_values`
            assert set(config_dict.keys()   ) == set(config.config._use_default_values   )

            with tempfile.TemporaryDirectory() as tmpdirname:
                   config.save_config(A   )

                   # now loading it with SampleObject2 should put f into `_use_default_values`
                   _UpperCamelCase														=    SampleObjecta.from_config(A   )

                   assert "f" in config._use_default_values
                   assert config.f == [1, 3]

            # now loading the config, should **NOT** use [1, 3] for `f`, but the default [1, 4] value
            # **BECAUSE** it is part of `config._use_default_values`
            _UpperCamelCase														=    SampleObjecta.from_config(config.config   )
            assert new_config.f == [5, 4]

            config.config._use_default_values.pop()
            _UpperCamelCase														=    SampleObjecta.from_config(config.config   )
            assert new_config_a.f == [1, 3]

            # Nevertheless "e" should still be correctly loaded to [1, 3] from SampleObject2 instead of defaulting to [1, 5]
            assert new_config_a.e == [1, 3]








'''simple docstring'''







import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
						import torch

						from transformers import GPTaLMHeadModel


@require_torch
@require_sentencepiece
@require_tokenizers
class       lowerCAmelCase			(					unittest.TestCase      ):




		@slow
		@unittest.skip("Model is not available."   )
		def      lowercase							(					self   ):
								_SCREAMING_SNAKE_CASE					=			"nvidia/megatron-gpt2-345m/"
								if "MYDIR" in os.environ:
														_SCREAMING_SNAKE_CASE					=			os.path.join(os.environ["MYDIR"]       ,    UpperCamelCase   )
								_SCREAMING_SNAKE_CASE					=			GPTaLMHeadModel.from_pretrained(UpperCamelCase   )
								model.to(UpperCamelCase   )
								model.half()

								_SCREAMING_SNAKE_CASE					=			torch.tensor(
								    [[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]       ,    device=UpperCamelCase       ,    dtype=torch.long       ,    )

								with torch.no_grad():
														_SCREAMING_SNAKE_CASE					=			model(UpperCamelCase   ).logits

								_SCREAMING_SNAKE_CASE					=			torch.Size((1, 9, 50_257)   )
								self.assertEqual(output.shape       ,    UpperCamelCase   )

								_SCREAMING_SNAKE_CASE					=			torch.tensor(
								    [
								        4.94_14,
								        -0.29_20,
								        -1.21_48,
								        -4.02_73,
								        -0.51_61,
								        -5.21_09,
								        -1.24_12,
								        -1.83_01,
								        -1.77_34,
								        -4.71_48,
								        -0.23_17,
								        -1.08_11,
								        -2.17_77,
								        0.41_41,
								        -3.79_69,
								        -4.05_86,
								        -2.53_32,
								        -3.38_09,
								        4.38_67,
								    ]       ,    device=UpperCamelCase       ,    dtype=torch.half       ,    )

								for i in range(19   ):
														_SCREAMING_SNAKE_CASE,				_SCREAMING_SNAKE_CASE					=			8 * i // 17, 2_792 * i  # along the diagonal
														_SCREAMING_SNAKE_CASE,				_SCREAMING_SNAKE_CASE					=			output[0, r, c], expected_diag[i]
														_SCREAMING_SNAKE_CASE					=			F'row={r} col={c} computed={computed} expected={expected}'
														self.assertAlmostEqual(UpperCamelCase       ,    UpperCamelCase       ,    delta=1e-4       ,    msg=UpperCamelCase   )