





import torch

from diffusers import EulerDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest



class 							_UpperCamelCase					(	lowerCAmelCase_     ):
       _UpperCamelCase				:					Union[str, Any]         =   (EulerDiscreteScheduler,)
       _UpperCamelCase				:					List[str]         =   1_0

       def lowercase (		self: Any					,   **_SCREAMING_SNAKE_CASE: List[str]		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  {
                  "num_train_timesteps": 1100,
                  "beta_start": 0.00_01,
                  "beta_end": 0.02,
                  "beta_schedule": "linear",
              }

              config.update(**_SCREAMING_SNAKE_CASE		)
              return config

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




              """simple docstring"""


              for timesteps in [10, 50, 100, 1000]:
                     self.check_over_configs(num_train_timesteps=_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: Dict		)       -> Optional[int]:




              """simple docstring"""


              for beta_start, beta_end in zip([0.0_00_01, 0.00_01, 0.0_01]					,   [0.00_02, 0.0_02, 0.02]		):
                     self.check_over_configs(beta_start=_SCREAMING_SNAKE_CASE					,   beta_end=_SCREAMING_SNAKE_CASE		)

       def lowercase (		self: Dict		)       -> Optional[int]:




              """simple docstring"""


              for schedule in ["linear", "scaled_linear"]:
                     self.check_over_configs(beta_schedule=_SCREAMING_SNAKE_CASE		)

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




              """simple docstring"""


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

       def lowercase (		self: Dict		)       -> List[str]:




              """simple docstring"""


              UpperCamelCase_          =  self.scheduler_classes[0]
              UpperCamelCase_          =  self.get_scheduler_config()
              UpperCamelCase_          =  scheduler_class(**_SCREAMING_SNAKE_CASE		)

              scheduler.set_timesteps(self.num_inference_steps		)

              UpperCamelCase_          =  torch.manual_seed(0		)

              UpperCamelCase_          =  self.dummy_model()
              UpperCamelCase_          =  self.dummy_sample_deter * scheduler.init_noise_sigma
              UpperCamelCase_          =  sample.to(_SCREAMING_SNAKE_CASE		)

              for i, t in enumerate(scheduler.timesteps		):
                     UpperCamelCase_          =  scheduler.scale_model_input(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  scheduler.step(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   generator=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  output.prev_sample

              UpperCamelCase_          =  torch.sum(torch.abs(_SCREAMING_SNAKE_CASE		)		)
              UpperCamelCase_          =  torch.mean(torch.abs(_SCREAMING_SNAKE_CASE		)		)

              assert abs(result_sum.item() - 10.08_07		) < 1e-2
              assert abs(result_mean.item() - 0.01_31		) < 1e-3

       def lowercase (		self: List[Any]		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  self.scheduler_classes[0]
              UpperCamelCase_          =  self.get_scheduler_config(prediction_type="v_prediction"		)
              UpperCamelCase_          =  scheduler_class(**_SCREAMING_SNAKE_CASE		)

              scheduler.set_timesteps(self.num_inference_steps		)

              UpperCamelCase_          =  torch.manual_seed(0		)

              UpperCamelCase_          =  self.dummy_model()
              UpperCamelCase_          =  self.dummy_sample_deter * scheduler.init_noise_sigma
              UpperCamelCase_          =  sample.to(_SCREAMING_SNAKE_CASE		)

              for i, t in enumerate(scheduler.timesteps		):
                     UpperCamelCase_          =  scheduler.scale_model_input(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  scheduler.step(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   generator=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  output.prev_sample

              UpperCamelCase_          =  torch.sum(torch.abs(_SCREAMING_SNAKE_CASE		)		)
              UpperCamelCase_          =  torch.mean(torch.abs(_SCREAMING_SNAKE_CASE		)		)

              assert abs(result_sum.item() - 0.00_02		) < 1e-2
              assert abs(result_mean.item() - 2.2676e-06		) < 1e-3

       def lowercase (		self: List[Any]		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  self.scheduler_classes[0]
              UpperCamelCase_          =  self.get_scheduler_config()
              UpperCamelCase_          =  scheduler_class(**_SCREAMING_SNAKE_CASE		)

              scheduler.set_timesteps(self.num_inference_steps					,   device=_SCREAMING_SNAKE_CASE		)

              UpperCamelCase_          =  torch.manual_seed(0		)

              UpperCamelCase_          =  self.dummy_model()
              UpperCamelCase_          =  self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
              UpperCamelCase_          =  sample.to(_SCREAMING_SNAKE_CASE		)

              for t in scheduler.timesteps:
                     UpperCamelCase_          =  scheduler.scale_model_input(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  scheduler.step(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   generator=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  output.prev_sample

              UpperCamelCase_          =  torch.sum(torch.abs(_SCREAMING_SNAKE_CASE		)		)
              UpperCamelCase_          =  torch.mean(torch.abs(_SCREAMING_SNAKE_CASE		)		)

              assert abs(result_sum.item() - 10.08_07		) < 1e-2
              assert abs(result_mean.item() - 0.01_31		) < 1e-3



       def lowercase (		self: Optional[int]		)       -> List[Any]:




              """simple docstring"""


              UpperCamelCase_          =  self.scheduler_classes[0]
              UpperCamelCase_          =  self.get_scheduler_config()
              UpperCamelCase_          =  scheduler_class(**_SCREAMING_SNAKE_CASE					,   use_karras_sigmas=_SCREAMING_SNAKE_CASE		)

              scheduler.set_timesteps(self.num_inference_steps					,   device=_SCREAMING_SNAKE_CASE		)

              UpperCamelCase_          =  torch.manual_seed(0		)

              UpperCamelCase_          =  self.dummy_model()
              UpperCamelCase_          =  self.dummy_sample_deter * scheduler.init_noise_sigma.cpu()
              UpperCamelCase_          =  sample.to(_SCREAMING_SNAKE_CASE		)

              for t in scheduler.timesteps:
                     UpperCamelCase_          =  scheduler.scale_model_input(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  model(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)

                     UpperCamelCase_          =  scheduler.step(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   generator=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  output.prev_sample

              UpperCamelCase_          =  torch.sum(torch.abs(_SCREAMING_SNAKE_CASE		)		)
              UpperCamelCase_          =  torch.mean(torch.abs(_SCREAMING_SNAKE_CASE		)		)

              assert abs(result_sum.item() - 1_24.52_29_94_99_51_17_19		) < 1e-2
              assert abs(result_mean.item() - 0.1_62_13_93_26_33_39_99_63		) < 1e-3








import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate




def  lowerCAmelCase_ (							UpperCamelCase_  )     ->		Optional[int]:
       return x + 2







class 							_UpperCamelCase					(	unittest.TestCase     ):

       def lowercase (		self: Optional[int]		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  "x = 3"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 3
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3}		)

              UpperCamelCase_          =  "x = y"
              UpperCamelCase_          =  {"y": 5}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 5, "y": 5}		)

       def lowercase (		self: List[str]		)       -> Dict:




              """simple docstring"""


              UpperCamelCase_          =  "y = add_two(x)"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 5}		)

              # Won't work without the tool
              with CaptureStdout() as out:
                     UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result is None
              assert "tried to execute add_two" in out.out

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




              """simple docstring"""


              UpperCamelCase_          =  "x = 3"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 3
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3}		)

       def lowercase (		self: Dict		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  "test_dict = {'x': x, 'y': add_two(x)}"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 5}		)
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_dict": {"x": 3, "y": 5}}		)

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




              """simple docstring"""


              UpperCamelCase_          =  "x = 3\ny = 5"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 5}		)

       def lowercase (		self: List[Any]		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  "text = f'This is x: {x}.'"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == "This is x: 3."
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "text": "This is x: 3."}		)

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




              """simple docstring"""


              UpperCamelCase_          =  "if x <= 3:\n    y = 2\nelse:\n    y = 5"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 2
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 2}		)

              UpperCamelCase_          =  {"x": 8}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              # evaluate returns the value of the last assignment.
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 8, "y": 5}		)

       def lowercase (		self: str		)       -> Optional[Any]:




              """simple docstring"""


              UpperCamelCase_          =  "test_list = [x, add_two(x)]"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              self.assertListEqual(_SCREAMING_SNAKE_CASE					,   [3, 5]		)
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_list": [3, 5]}		)

       def lowercase (		self: List[str]		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  "y = x"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 3
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "y": 3}		)

       def lowercase (		self: str		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  "test_list = [x, add_two(x)]\ntest_list[1]"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_list": [3, 5]}		)

              UpperCamelCase_          =  "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
              UpperCamelCase_          =  {"x": 3}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"add_two": add_two}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 5
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 3, "test_dict": {"x": 3, "y": 5}}		)



       def lowercase (		self: Dict		)       -> Optional[int]:




              """simple docstring"""


              UpperCamelCase_          =  "x = 0\nfor i in range(3):\n    x = i"
              UpperCamelCase_          =  {}
              UpperCamelCase_          =  evaluate(_SCREAMING_SNAKE_CASE					,   {"range": range}					,   state=_SCREAMING_SNAKE_CASE		)
              assert result == 2
              self.assertDictEqual(_SCREAMING_SNAKE_CASE					,   {"x": 2, "i": 2}		)

