"""simple docstring"""




import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()
class 						__A							( unittest.TestCase		):






     @property
     def     __A  (		self  ):
       torch.manual_seed(0  )
       _lowerCAmelCase						:       Tuple  			=     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     __A  (		self  ):
       _lowerCAmelCase						:       Dict  			=     self.dummy_uncond_unet
       _lowerCAmelCase						:       List[str]  			=     KarrasVeScheduler()

       _lowerCAmelCase						:       List[Any]  			=     KarrasVePipeline(unet=a__				,     scheduler=a__  )
       pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:       str  			=     torch.manual_seed(0  )
       _lowerCAmelCase						:       Tuple  			=     pipe(num_inference_steps=2				,     generator=a__				,     output_type="""numpy"""  ).images

       _lowerCAmelCase						:       Tuple  			=     torch.manual_seed(0  )
       _lowerCAmelCase						:       Optional[int]  			=     pipe(num_inference_steps=2				,     generator=a__				,     output_type="""numpy"""				,     return_dict=a__  )[0]

       _lowerCAmelCase						:       Dict  			=     image[0, -3:, -3:, -1]
       _lowerCAmelCase						:       Tuple  			=     image_from_tuple[0, -3:, -3:, -1]

       assert image.shape == (1, 32, 32, 3)
       _lowerCAmelCase						:       Dict  			=     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 						__A							( unittest.TestCase		):






     def     __A  (		self  ):
       _lowerCAmelCase						:       int  			=     """google/ncsnpp-celebahq-256"""
       _lowerCAmelCase						:       str  			=     UNetaDModel.from_pretrained(a__  )
       _lowerCAmelCase						:       Optional[Any]  			=     KarrasVeScheduler()

       _lowerCAmelCase						:       Dict  			=     KarrasVePipeline(unet=a__				,     scheduler=a__  )
       pipe.to(a__  )
       pipe.set_progress_bar_config(disable=a__  )

       _lowerCAmelCase						:       Tuple  			=     torch.manual_seed(0  )
       _lowerCAmelCase						:       Tuple  			=     pipe(num_inference_steps=20				,     generator=a__				,     output_type="""numpy"""  ).images

       _lowerCAmelCase						:       List[str]  			=     image[0, -3:, -3:, -1]
       assert image.shape == (1, 256, 256, 3)
       _lowerCAmelCase						:       List[Any]  			=     np.array([0.5_7_8, 0.5_8_1_1, 0.5_9_2_4, 0.5_8_0_9, 0.5_8_7, 0.5_8_8_6, 0.5_8_6_1, 0.5_8_0_2, 0.5_8_6]  )

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











'''simple docstring'''


import unittest

from transformers.testing_utils import CaptureStdout
from transformers.tools.python_interpreter import evaluate
def 							UpperCAmelCase_  (						__lowerCamelCase					:					List[Any] ):
   return x + 2


class       a_   (				unittest.TestCase							):





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= "x = 3"
        lowercase_			:Any    			= {}
        lowercase_			:Any    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result == 3
        self.assertDictEqual(lowercase	,      {"x": 3}						)

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





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:Dict    			= "y = add_two(x)"
        lowercase_			:Optional[Any]    			= {"x": 3}
        lowercase_			:int    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 5}						)

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





     def 							lowercase__      ( self		:					int						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= "x = 3"
        lowercase_			:Any    			= {}
        lowercase_			:Any    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result == 3
        self.assertDictEqual(lowercase	,      {"x": 3}						)





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:Optional[Any]    			= "test_dict = {'x': x, 'y': add_two(x)}"
        lowercase_			:List[str]    			= {"x": 3}
        lowercase_			:Any    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 5}						)
        self.assertDictEqual(lowercase	,      {"x": 3, "test_dict": {"x": 3, "y": 5}}						)





     def 							lowercase__      ( self		:					Optional[Any]						):





        """simple docstring"""




        lowercase_			:Optional[int]    			= "x = 3\ny = 5"
        lowercase_			:Union[str, Any]    			= {}
        lowercase_			:Optional[int]    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 5}						)





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:List[str]    			= "text = f'This is x: {x}.'"
        lowercase_			:int    			= {"x": 3}
        lowercase_			:int    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == "This is x: 3."
        self.assertDictEqual(lowercase	,      {"x": 3, "text": "This is x: 3."}						)





     def 							lowercase__      ( self		:					Optional[int]						):





        """simple docstring"""




        lowercase_			:List[Any]    			= "if x <= 3:\n    y = 2\nelse:\n    y = 5"
        lowercase_			:List[str]    			= {"x": 3}
        lowercase_			:Union[str, Any]    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 2
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 2}						)

        lowercase_			:List[str]    			= {"x": 8}
        lowercase_			:Tuple    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        # evaluate returns the value of the last assignment.
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 8, "y": 5}						)





     def 							lowercase__      ( self		:					Dict						):





        """simple docstring"""




        lowercase_			:Any    			= "test_list = [x, add_two(x)]"
        lowercase_			:Optional[int]    			= {"x": 3}
        lowercase_			:List[str]    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        self.assertListEqual(lowercase	,      [3, 5]						)
        self.assertDictEqual(lowercase	,      {"x": 3, "test_list": [3, 5]}						)





     def 							lowercase__      ( self		:					List[Any]						):





        """simple docstring"""




        lowercase_			:str    			= "y = x"
        lowercase_			:Any    			= {"x": 3}
        lowercase_			:Tuple    			= evaluate(lowercase	,      {}	,      state=lowercase						)
        assert result == 3
        self.assertDictEqual(lowercase	,      {"x": 3, "y": 3}						)





     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:List[Any]    			= "test_list = [x, add_two(x)]\ntest_list[1]"
        lowercase_			:Dict    			= {"x": 3}
        lowercase_			:Any    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "test_list": [3, 5]}						)

        lowercase_			:Union[str, Any]    			= "test_dict = {'x': x, 'y': add_two(x)}\ntest_dict['y']"
        lowercase_			:List[Any]    			= {"x": 3}
        lowercase_			:List[Any]    			= evaluate(lowercase	,      {"add_two": add_two}	,      state=lowercase						)
        assert result == 5
        self.assertDictEqual(lowercase	,      {"x": 3, "test_dict": {"x": 3, "y": 5}}						)







     def 							lowercase__      ( self		:					Any						):





        """simple docstring"""




        lowercase_			:Dict    			= "x = 0\nfor i in range(3):\n    x = i"
        lowercase_			:Any    			= {}
        lowercase_			:Tuple    			= evaluate(lowercase	,      {"range": range}	,      state=lowercase						)
        assert result == 2
        self.assertDictEqual(lowercase	,      {"x": 2, "i": 2}						)


