


import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY
@is_pipeline_test
class 	lowerCamelCase_       (				unittest.TestCase				):



     '''simple docstring'''







     __UpperCAmelCase            =				MODEL_FOR_MASKED_LM_MAPPING
     __UpperCAmelCase            =				TF_MODEL_FOR_MASKED_LM_MAPPING






     def A      (				self						)   ->			Optional[Any]:







        '''simple docstring'''

        super().tearDown()
        # clean-up as much as possible GPU memory occupied by PyTorch
        gc.collect()
        if is_torch_available():
           import torch

           torch.cuda.empty_cache()






     @require_tf
     def A      (				self						)   ->			str:







        '''simple docstring'''

        __lowercase 					=   pipeline(task='''fill-mask''' ,       model='''sshleifer/tiny-distilroberta-base''' ,       top_k=2 ,       framework='''tf'''						)
        __lowercase 					=   unmasker('''My name is <mask>'''						)
        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                {'''sequence''': '''My name is grouped''', '''score''': 2.1e-0_5, '''token''': 3_8_0_1_5, '''token_str''': ''' grouped'''},
                {'''sequence''': '''My name is accuser''', '''score''': 2.1e-0_5, '''token''': 2_5_5_0_6, '''token_str''': ''' accuser'''},
            ] ,       )

        __lowercase 					=   unmasker('''The largest city in France is <mask>'''						)
        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                {
                    '''sequence''': '''The largest city in France is grouped''',
                    '''score''': 2.1e-0_5,
                    '''token''': 3_8_0_1_5,
                    '''token_str''': ''' grouped''',
                },
                {
                    '''sequence''': '''The largest city in France is accuser''',
                    '''score''': 2.1e-0_5,
                    '''token''': 2_5_5_0_6,
                    '''token_str''': ''' accuser''',
                },
            ] ,       )

        __lowercase 					=   unmasker('''My name is <mask>''' ,       targets=[''' Patrick''', ''' Clara''', ''' Teven'''] ,       top_k=3						)
        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                {'''sequence''': '''My name is Clara''', '''score''': 2e-0_5, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
                {'''sequence''': '''My name is Patrick''', '''score''': 2e-0_5, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
                {'''sequence''': '''My name is Te''', '''score''': 1.9e-0_5, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
            ] ,       )






     @require_torch
     def A      (				self						)   ->			Any:







        '''simple docstring'''

        __lowercase 					=   pipeline(task='''fill-mask''' ,       model='''sshleifer/tiny-distilroberta-base''' ,       top_k=2 ,       framework='''pt'''						)

        __lowercase 					=   unmasker('''My name is <mask>'''						)
        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                {'''sequence''': '''My name is Maul''', '''score''': 2.2e-0_5, '''token''': 3_5_6_7_6, '''token_str''': ''' Maul'''},
                {'''sequence''': '''My name isELS''', '''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS'''},
            ] ,       )

        __lowercase 					=   unmasker('''The largest city in France is <mask>'''						)
        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                {
                    '''sequence''': '''The largest city in France is Maul''',
                    '''score''': 2.2e-0_5,
                    '''token''': 3_5_6_7_6,
                    '''token_str''': ''' Maul''',
                },
                {'''sequence''': '''The largest city in France isELS''', '''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS'''},
            ] ,       )

        __lowercase 					=   unmasker('''My name is <mask>''' ,       targets=[''' Patrick''', ''' Clara''', ''' Teven'''] ,       top_k=3						)
        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                {'''sequence''': '''My name is Patrick''', '''score''': 2.1e-0_5, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
                {'''sequence''': '''My name is Te''', '''score''': 2e-0_5, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
                {'''sequence''': '''My name is Clara''', '''score''': 2e-0_5, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
            ] ,       )

        __lowercase 					=   unmasker('''My name is <mask> <mask>''' ,       top_k=2						)

        self.assertEqual(
            nested_simplify(snake_case_ ,       decimals=6						) ,       [
                [
                    {
                        '''score''': 2.2e-0_5,
                        '''token''': 3_5_6_7_6,
                        '''token_str''': ''' Maul''',
                        '''sequence''': '''<s>My name is Maul<mask></s>''',
                    },
                    {'''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS''', '''sequence''': '''<s>My name isELS<mask></s>'''},
                ],
                [
                    {
                        '''score''': 2.2e-0_5,
                        '''token''': 3_5_6_7_6,
                        '''token_str''': ''' Maul''',
                        '''sequence''': '''<s>My name is<mask> Maul</s>''',
                    },
                    {'''score''': 2.2e-0_5, '''token''': 1_6_4_1_6, '''token_str''': '''ELS''', '''sequence''': '''<s>My name is<mask>ELS</s>'''},
                ],
            ] ,       )






     @require_torch_gpu
     def A      (				self						)   ->			str:







        '''simple docstring'''

        __lowercase 					=   pipeline('''fill-mask''' ,       model='''hf-internal-testing/tiny-random-distilbert''' ,       device=0 ,       framework='''pt'''						)

        # convert model to fp16
        pipe.model.half()

        __lowercase 					=   pipe('''Paris is the [MASK] of France.'''						)
        # We actually don't care about the result, we just want to make sure
        # it works, meaning the float16 tensor got casted back to float32
        # for postprocessing.
        self.assertIsInstance(snake_case_ ,       snake_case_						)






     @slow
     @require_torch
     def A      (				self						)   ->			Tuple:







        '''simple docstring'''

        __lowercase 					=   pipeline(task='''fill-mask''' ,       model='''distilroberta-base''' ,       top_k=2 ,       framework='''pt'''						)
        self.run_large_test(snake_case_						)






     @slow
     @require_tf
     def A      (				self						)   ->			Dict:







        '''simple docstring'''

        __lowercase 					=   pipeline(task='''fill-mask''' ,       model='''distilroberta-base''' ,       top_k=2 ,       framework='''tf'''						)
        self.run_large_test(snake_case_						)






     def A      (				self ,       snake_case_						)   ->			Any:







        '''simple docstring'''

        __lowercase 					=   unmasker('''My name is <mask>'''						)
        self.assertEqual(
            nested_simplify(snake_case_						) ,       [
                {'''sequence''': '''My name is John''', '''score''': 0.0_0_8, '''token''': 6_1_0, '''token_str''': ''' John'''},
                {'''sequence''': '''My name is Chris''', '''score''': 0.0_0_7, '''token''': 1_5_7_3, '''token_str''': ''' Chris'''},
            ] ,       )
        __lowercase 					=   unmasker('''The largest city in France is <mask>'''						)
        self.assertEqual(
            nested_simplify(snake_case_						) ,       [
                {
                    '''sequence''': '''The largest city in France is Paris''',
                    '''score''': 0.2_5_1,
                    '''token''': 2_2_0_1,
                    '''token_str''': ''' Paris''',
                },
                {
                    '''sequence''': '''The largest city in France is Lyon''',
                    '''score''': 0.2_1_4,
                    '''token''': 1_2_7_9_0,
                    '''token_str''': ''' Lyon''',
                },
            ] ,       )

        __lowercase 					=   unmasker('''My name is <mask>''' ,       targets=[''' Patrick''', ''' Clara''', ''' Teven'''] ,       top_k=3						)
        self.assertEqual(
            nested_simplify(snake_case_						) ,       [
                {'''sequence''': '''My name is Patrick''', '''score''': 0.0_0_5, '''token''': 3_4_9_9, '''token_str''': ''' Patrick'''},
                {'''sequence''': '''My name is Clara''', '''score''': 0.0_0_0, '''token''': 1_3_6_0_6, '''token_str''': ''' Clara'''},
                {'''sequence''': '''My name is Te''', '''score''': 0.0_0_0, '''token''': 2_9_4_1, '''token_str''': ''' Te'''},
            ] ,       )






     @require_torch
     def A      (				self						)   ->			Optional[Any]:







        '''simple docstring'''

        __lowercase 					=   pipeline(task='''fill-mask''' ,       model='''sshleifer/tiny-distilroberta-base''' ,       framework='''pt'''						)
        __lowercase 					=   None
        __lowercase 					=   None
        self.run_pipeline_test(snake_case_ ,       []						)






     @require_tf
     def A      (				self						)   ->			int:







        '''simple docstring'''

        __lowercase 					=   pipeline(task='''fill-mask''' ,       model='''sshleifer/tiny-distilroberta-base''' ,       framework='''tf'''						)
        __lowercase 					=   None
        __lowercase 					=   None
        self.run_pipeline_test(snake_case_ ,       []						)






     def A      (				self ,       snake_case_ ,       snake_case_ ,       snake_case_						)   ->			List[Any]:







        '''simple docstring'''

        if tokenizer is None or tokenizer.mask_token_id is None:
           self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)'''						)

        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_						)
        __lowercase 					=   [
            F'This is another {tokenizer.mask_token} test',
        ]
        return fill_masker, examples






     def A      (				self ,       snake_case_ ,       snake_case_						)   ->			Optional[Any]:







        '''simple docstring'''

        __lowercase 					=   fill_masker.tokenizer
        __lowercase 					=   fill_masker.model

        __lowercase 					=   fill_masker(
            F'This is a {tokenizer.mask_token}' ,       )
        self.assertEqual(
            snake_case_ ,       [
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
            ] ,       )

        __lowercase 					=   fill_masker([F'This is a {tokenizer.mask_token}']						)
        self.assertEqual(
            snake_case_ ,       [
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
            ] ,       )

        __lowercase 					=   fill_masker([F'This is a {tokenizer.mask_token}', F'Another {tokenizer.mask_token} great test.']						)
        self.assertEqual(
            snake_case_ ,       [
                [
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                ],
                [
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                ],
            ] ,       )

        with self.assertRaises(snake_case_						):
           fill_masker([None]						)
        # No mask_token is not supported
        with self.assertRaises(snake_case_						):
           fill_masker('''This is'''						)

        self.run_test_top_k(snake_case_ ,       snake_case_						)
        self.run_test_targets(snake_case_ ,       snake_case_						)
        self.run_test_top_k_targets(snake_case_ ,       snake_case_						)
        self.fill_mask_with_duplicate_targets_and_top_k(snake_case_ ,       snake_case_						)
        self.fill_mask_with_multiple_masks(snake_case_ ,       snake_case_						)






     def A      (				self ,       snake_case_ ,       snake_case_						)   ->			Optional[Any]:







        '''simple docstring'''

        __lowercase 					=   tokenizer.get_vocab()
        __lowercase 					=   sorted(vocab.keys()						)[:2]
        # Pipeline argument
        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_ ,       targets=snake_case_						)
        __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}'						)
        self.assertEqual(
            snake_case_ ,       [
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
            ] ,       )
        __lowercase 					=   {vocab[el] for el in targets}
        self.assertEqual({el['''token'''] for el in outputs} ,       snake_case_						)
        __lowercase 					=   [tokenizer.decode([x]						) for x in target_ids]
        self.assertEqual({el['''token_str'''] for el in outputs} ,       set(snake_case_						)						)

        # Call argument
        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_						)
        __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       targets=snake_case_						)
        self.assertEqual(
            snake_case_ ,       [
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
            ] ,       )
        __lowercase 					=   {vocab[el] for el in targets}
        self.assertEqual({el['''token'''] for el in outputs} ,       snake_case_						)
        __lowercase 					=   [tokenizer.decode([x]						) for x in target_ids]
        self.assertEqual({el['''token_str'''] for el in outputs} ,       set(snake_case_						)						)

        # Score equivalence
        __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       targets=snake_case_						)
        __lowercase 					=   [top_mask['''token_str'''] for top_mask in outputs]
        __lowercase 					=   [top_mask['''score'''] for top_mask in outputs]

        # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
        if set(snake_case_						) == set(snake_case_						):
           __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       targets=snake_case_						)
           __lowercase 					=   [top_mask['''score'''] for top_mask in unmasked_targets]
           self.assertEqual(nested_simplify(snake_case_						) ,       nested_simplify(snake_case_						)						)

        # Raises with invalid
        with self.assertRaises(snake_case_						):
           __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       targets=[]						)
        # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
        if "" not in tokenizer.get_vocab():
           with self.assertRaises(snake_case_						):
              __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       targets=['''''']						)
           with self.assertRaises(snake_case_						):
              __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       targets=''''''						)






     def A      (				self ,       snake_case_ ,       snake_case_						)   ->			Dict:







        '''simple docstring'''

        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_ ,       top_k=2						)
        __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}'						)
        self.assertEqual(
            snake_case_ ,       [
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
            ] ,       )

        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_						)
        __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       top_k=2						)
        self.assertEqual(
            snake_case_ ,       [
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
            ] ,       )
        self.assertEqual(nested_simplify(snake_case_						) ,       nested_simplify(snake_case_						)						)






     def A      (				self ,       snake_case_ ,       snake_case_						)   ->			List[str]:







        '''simple docstring'''

        __lowercase 					=   tokenizer.get_vocab()
        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_						)

        # top_k=2, ntargets=3
        __lowercase 					=   sorted(vocab.keys()						)[:3]
        __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       top_k=2 ,       targets=snake_case_						)

        # If we use the most probably targets, and filter differently, we should still
        # have the same results
        __lowercase 					=   [el['''token_str'''] for el in sorted(snake_case_ ,       key=lambda snake_case_						: x["score"] ,       reverse=snake_case_						)]
        # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
        if set(snake_case_						).issubset(snake_case_						):
           __lowercase 					=   fill_masker(F'This is a {tokenizer.mask_token}' ,       top_k=3 ,       targets=snake_case_						)
           # They should yield exactly the same result
           self.assertEqual(nested_simplify(snake_case_						) ,       nested_simplify(snake_case_						)						)






     def A      (				self ,       snake_case_ ,       snake_case_						)   ->			int:







        '''simple docstring'''

        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_						)
        __lowercase 					=   tokenizer.get_vocab()
        # String duplicates + id duplicates
        __lowercase 					=   sorted(vocab.keys()						)[:3]
        __lowercase 					=   [targets[0], targets[1], targets[0], targets[2], targets[1]]
        __lowercase 					=   fill_masker(F'My name is {tokenizer.mask_token}' ,       targets=snake_case_ ,       top_k=1_0						)

        # The target list contains duplicates, so we can't output more
        # than them
        self.assertEqual(len(snake_case_						) ,       3						)






     def A      (				self ,       snake_case_ ,       snake_case_						)   ->			List[str]:







        '''simple docstring'''

        __lowercase 					=   FillMaskPipeline(model=snake_case_ ,       tokenizer=snake_case_						)

        __lowercase 					=   fill_masker(
            F'This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}' ,       top_k=2						)
        self.assertEqual(
            snake_case_ ,       [
                [
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                ],
                [
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                ],
                [
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                    {'''sequence''': ANY(snake_case_						), '''score''': ANY(snake_case_						), '''token''': ANY(snake_case_						), '''token_str''': ANY(snake_case_						)},
                ],
            ] ,       )






import tempfile

import torch

from diffusers import (
    DEISMultistepScheduler,
    DPMSolverMultistepScheduler,
    DPMSolverSinglestepScheduler,
    UniPCMultistepScheduler,
)

from .test_schedulers import SchedulerCommonTest
class 	lowerCamelCase_       (				lowerCAmelCase__				):



					'''simple docstring'''







					__UpperCAmelCase            =				(DEISMultistepScheduler,)
					__UpperCAmelCase            =				(("num_inference_steps", 25),)






					def A      (				self ,       **snake_case_						)   ->			int:







								'''simple docstring'''

								__lowercase 					=   {
								    '''num_train_timesteps''': 1_0_0_0,
								    '''beta_start''': 0.0_0_0_1,
								    '''beta_end''': 0.0_2,
								    '''beta_schedule''': '''linear''',
								    '''solver_order''': 2,
								}

								config.update(**snake_case_						)
								return config






					def A      (				self ,       snake_case_=0 ,       **snake_case_						)   ->			List[Any]:







								'''simple docstring'''

								__lowercase 					=   dict(self.forward_default_kwargs						)
								__lowercase 					=   kwargs.pop('''num_inference_steps''' ,       snake_case_						)
								__lowercase 					=   self.dummy_sample
								__lowercase 					=   0.1 * sample
								__lowercase 					=   [residual + 0.2, residual + 0.1_5, residual + 0.1_0]

								for scheduler_class in self.scheduler_classes:
											__lowercase 					=   self.get_scheduler_config(**snake_case_						)
											__lowercase 					=   scheduler_class(**snake_case_						)
											scheduler.set_timesteps(snake_case_						)
											# copy over dummy past residuals
											__lowercase 					=   dummy_past_residuals[: scheduler.config.solver_order]

											with tempfile.TemporaryDirectory() as tmpdirname:
														scheduler.save_config(snake_case_						)
														__lowercase 					=   scheduler_class.from_pretrained(snake_case_						)
														new_scheduler.set_timesteps(snake_case_						)
														# copy over dummy past residuals
														__lowercase 					=   dummy_past_residuals[: new_scheduler.config.solver_order]

											__lowercase					, __lowercase 					=   sample, sample
											for t in range(snake_case_ ,       time_step + scheduler.config.solver_order + 1						):
														__lowercase 					=   scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_ ,       **snake_case_						).prev_sample
														__lowercase 					=   new_scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_ ,       **snake_case_						).prev_sample

														assert torch.sum(torch.abs(output - new_output						)						) < 1e-5, "Scheduler outputs are not identical"






					def A      (				self						)   ->			Dict:







								'''simple docstring'''

								pass






					def A      (				self ,       snake_case_=0 ,       **snake_case_						)   ->			List[str]:







								'''simple docstring'''

								__lowercase 					=   dict(self.forward_default_kwargs						)
								__lowercase 					=   kwargs.pop('''num_inference_steps''' ,       snake_case_						)
								__lowercase 					=   self.dummy_sample
								__lowercase 					=   0.1 * sample
								__lowercase 					=   [residual + 0.2, residual + 0.1_5, residual + 0.1_0]

								for scheduler_class in self.scheduler_classes:
											__lowercase 					=   self.get_scheduler_config()
											__lowercase 					=   scheduler_class(**snake_case_						)
											scheduler.set_timesteps(snake_case_						)

											# copy over dummy past residuals (must be after setting timesteps)
											__lowercase 					=   dummy_past_residuals[: scheduler.config.solver_order]

											with tempfile.TemporaryDirectory() as tmpdirname:
														scheduler.save_config(snake_case_						)
														__lowercase 					=   scheduler_class.from_pretrained(snake_case_						)
														# copy over dummy past residuals
														new_scheduler.set_timesteps(snake_case_						)

														# copy over dummy past residual (must be after setting timesteps)
														__lowercase 					=   dummy_past_residuals[: new_scheduler.config.solver_order]

											__lowercase 					=   scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_ ,       **snake_case_						).prev_sample
											__lowercase 					=   new_scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_ ,       **snake_case_						).prev_sample

											assert torch.sum(torch.abs(output - new_output						)						) < 1e-5, "Scheduler outputs are not identical"






					def A      (				self ,       snake_case_=None ,       **snake_case_						)   ->			Optional[int]:







								'''simple docstring'''

								if scheduler is None:
											__lowercase 					=   self.scheduler_classes[0]
											__lowercase 					=   self.get_scheduler_config(**snake_case_						)
											__lowercase 					=   scheduler_class(**snake_case_						)

								__lowercase 					=   self.scheduler_classes[0]
								__lowercase 					=   self.get_scheduler_config(**snake_case_						)
								__lowercase 					=   scheduler_class(**snake_case_						)

								__lowercase 					=   1_0
								__lowercase 					=   self.dummy_model()
								__lowercase 					=   self.dummy_sample_deter
								scheduler.set_timesteps(snake_case_						)

								for i, t in enumerate(scheduler.timesteps						):
											__lowercase 					=   model(snake_case_ ,       snake_case_						)
											__lowercase 					=   scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_						).prev_sample

								return sample






					def A      (				self						)   ->			str:







								'''simple docstring'''

								__lowercase 					=   dict(self.forward_default_kwargs						)

								__lowercase 					=   kwargs.pop('''num_inference_steps''' ,       snake_case_						)

								for scheduler_class in self.scheduler_classes:
											__lowercase 					=   self.get_scheduler_config()
											__lowercase 					=   scheduler_class(**snake_case_						)

											__lowercase 					=   self.dummy_sample
											__lowercase 					=   0.1 * sample

											if num_inference_steps is not None and hasattr(snake_case_ ,       '''set_timesteps'''						):
														scheduler.set_timesteps(snake_case_						)
											elif num_inference_steps is not None and not hasattr(snake_case_ ,       '''set_timesteps'''						):
														__lowercase 					=   num_inference_steps

											# copy over dummy past residuals (must be done after set_timesteps)
											__lowercase 					=   [residual + 0.2, residual + 0.1_5, residual + 0.1_0]
											__lowercase 					=   dummy_past_residuals[: scheduler.config.solver_order]

											__lowercase 					=   scheduler.timesteps[5]
											__lowercase 					=   scheduler.timesteps[6]

											__lowercase 					=   scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_ ,       **snake_case_						).prev_sample
											__lowercase 					=   scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_ ,       **snake_case_						).prev_sample

											self.assertEqual(output_a.shape ,       sample.shape						)
											self.assertEqual(output_a.shape ,       output_a.shape						)






					def A      (				self						)   ->			str:







								'''simple docstring'''

								__lowercase 					=   DEISMultistepScheduler(**self.get_scheduler_config()						)
								__lowercase 					=   self.full_loop(scheduler=snake_case_						)
								__lowercase 					=   torch.mean(torch.abs(snake_case_						)						)

								assert abs(result_mean.item() - 0.2_3_9_1_6						) < 1e-3

								__lowercase 					=   DPMSolverSinglestepScheduler.from_config(scheduler.config						)
								__lowercase 					=   DPMSolverMultistepScheduler.from_config(scheduler.config						)
								__lowercase 					=   UniPCMultistepScheduler.from_config(scheduler.config						)
								__lowercase 					=   DEISMultistepScheduler.from_config(scheduler.config						)

								__lowercase 					=   self.full_loop(scheduler=snake_case_						)
								__lowercase 					=   torch.mean(torch.abs(snake_case_						)						)

								assert abs(result_mean.item() - 0.2_3_9_1_6						) < 1e-3






					def A      (				self						)   ->			Optional[Any]:







								'''simple docstring'''

								for timesteps in [2_5, 5_0, 1_0_0, 9_9_9, 1_0_0_0]:
											self.check_over_configs(num_train_timesteps=snake_case_						)






					def A      (				self						)   ->			Optional[int]:







								'''simple docstring'''

								self.check_over_configs(thresholding=snake_case_						)
								for order in [1, 2, 3]:
											for solver_type in ["logrho"]:
														for threshold in [0.5, 1.0, 2.0]:
																	for prediction_type in ["epsilon", "sample"]:
																				self.check_over_configs(
																				    thresholding=snake_case_ ,       prediction_type=snake_case_ ,       sample_max_value=snake_case_ ,       algorithm_type='''deis''' ,       solver_order=snake_case_ ,       solver_type=snake_case_ ,       )






					def A      (				self						)   ->			Union[str, Any]:







								'''simple docstring'''

								for prediction_type in ["epsilon", "v_prediction"]:
											self.check_over_configs(prediction_type=snake_case_						)






					def A      (				self						)   ->			Optional[Any]:







								'''simple docstring'''

								for algorithm_type in ["deis"]:
											for solver_type in ["logrho"]:
														for order in [1, 2, 3]:
																	for prediction_type in ["epsilon", "sample"]:
																				self.check_over_configs(
																				    solver_order=snake_case_ ,       solver_type=snake_case_ ,       prediction_type=snake_case_ ,       algorithm_type=snake_case_ ,       )
																				__lowercase 					=   self.full_loop(
																				    solver_order=snake_case_ ,       solver_type=snake_case_ ,       prediction_type=snake_case_ ,       algorithm_type=snake_case_ ,       )
																				assert not torch.isnan(snake_case_						).any(), "Samples have nan numbers"






					def A      (				self						)   ->			List[str]:







								'''simple docstring'''

								self.check_over_configs(lower_order_final=snake_case_						)
								self.check_over_configs(lower_order_final=snake_case_						)






					def A      (				self						)   ->			List[str]:







								'''simple docstring'''

								for num_inference_steps in [1, 2, 3, 5, 1_0, 5_0, 1_0_0, 9_9_9, 1_0_0_0]:
											self.check_over_forward(num_inference_steps=snake_case_ ,       time_step=0						)






					def A      (				self						)   ->			List[str]:







								'''simple docstring'''

								__lowercase 					=   self.full_loop()
								__lowercase 					=   torch.mean(torch.abs(snake_case_						)						)

								assert abs(result_mean.item() - 0.2_3_9_1_6						) < 1e-3






					def A      (				self						)   ->			Dict:







								'''simple docstring'''

								__lowercase 					=   self.full_loop(prediction_type='''v_prediction'''						)
								__lowercase 					=   torch.mean(torch.abs(snake_case_						)						)

								assert abs(result_mean.item() - 0.0_9_1						) < 1e-3






					def A      (				self						)   ->			Union[str, Any]:







								'''simple docstring'''

								__lowercase 					=   self.scheduler_classes[0]
								__lowercase 					=   self.get_scheduler_config(thresholding=snake_case_ ,       dynamic_thresholding_ratio=0						)
								__lowercase 					=   scheduler_class(**snake_case_						)

								__lowercase 					=   1_0
								__lowercase 					=   self.dummy_model()
								__lowercase 					=   self.dummy_sample_deter.half()
								scheduler.set_timesteps(snake_case_						)

								for i, t in enumerate(scheduler.timesteps						):
											__lowercase 					=   model(snake_case_ ,       snake_case_						)
											__lowercase 					=   scheduler.step(snake_case_ ,       snake_case_ ,       snake_case_						).prev_sample

								assert sample.dtype == torch.floataa


