






from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer


@dataclass
class       __lowercase      (	lowercase_  ):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE   						=				42


class       __lowercase      (	lowercase_			,      lowercase_  ):







     '''simple docstring'''




     @register_to_config
     def __init__(				self       :			List[Any] ,	UpperCamelCase_       :			int = 3 ,	UpperCamelCase_       :			int = 3 ,	UpperCamelCase_       :			Tuple[str] = ("DownEncoderBlock2D",) ,	UpperCamelCase_       :			Tuple[str] = ("UpDecoderBlock2D",) ,	UpperCamelCase_       :			Tuple[int] = (64,) ,	UpperCamelCase_       :			int = 1 ,	UpperCamelCase_       :			str = "silu" ,	UpperCamelCase_       :			int = 3 ,	UpperCamelCase_       :			int = 32 ,	UpperCamelCase_       :			int = 256 ,	UpperCamelCase_       :			int = 32 ,	UpperCamelCase_       :			Optional[int] = None ,	UpperCamelCase_       :			float = 0.18215 ,	UpperCamelCase_       :			str = "group" ,	):


         """simple docstring"""



         super().__init__()

         # pass init params to Encoder
         __A         =	Encoder(
             in_channels=UpperCamelCase_ ,	out_channels=UpperCamelCase_ ,	down_block_types=UpperCamelCase_ ,	block_out_channels=UpperCamelCase_ ,	layers_per_block=UpperCamelCase_ ,	act_fn=UpperCamelCase_ ,	norm_num_groups=UpperCamelCase_ ,	double_z=UpperCamelCase_ ,	)

         __A         =	vq_embed_dim if vq_embed_dim is not None else latent_channels

         __A         =	nn.Convad(UpperCamelCase_ ,	UpperCamelCase_ ,	1				)
         __A         =	VectorQuantizer(UpperCamelCase_ ,	UpperCamelCase_ ,	beta=0.25 ,	remap=UpperCamelCase_ ,	sane_index_shape=UpperCamelCase_				)
         __A         =	nn.Convad(UpperCamelCase_ ,	UpperCamelCase_ ,	1				)

         # pass init params to Decoder
         __A         =	Decoder(
             in_channels=UpperCamelCase_ ,	out_channels=UpperCamelCase_ ,	up_block_types=UpperCamelCase_ ,	block_out_channels=UpperCamelCase_ ,	layers_per_block=UpperCamelCase_ ,	act_fn=UpperCamelCase_ ,	norm_num_groups=UpperCamelCase_ ,	norm_type=UpperCamelCase_ ,	)




     @apply_forward_hook
     def 		lowerCAmelCase_     (				self       :			List[str] ,	UpperCamelCase_       :			torch.FloatTensor ,	UpperCamelCase_       :			bool = True				):


         """simple docstring"""



         __A         =	self.encoder(UpperCamelCase_				)
         __A         =	self.quant_conv(UpperCamelCase_				)

         if not return_dict:
             return (h,)

         return VQEncoderOutput(latents=UpperCamelCase_				)




     @apply_forward_hook
     def 		lowerCAmelCase_     (				self       :			List[str] ,	UpperCamelCase_       :			torch.FloatTensor ,	UpperCamelCase_       :			bool = False ,	UpperCamelCase_       :			bool = True				):


         """simple docstring"""



         if not force_not_quantize:
             __A    ,	__A    ,	__A         =	self.quantize(UpperCamelCase_				)
         else:
             __A         =	h
         __A         =	self.post_quant_conv(UpperCamelCase_				)
         __A         =	self.decoder(UpperCamelCase_ ,	quant if self.config.norm_type == """spatial""" else None				)

         if not return_dict:
             return (dec,)

         return DecoderOutput(sample=UpperCamelCase_				)





     def 		lowerCAmelCase_     (				self       :			Any ,	UpperCamelCase_       :			torch.FloatTensor ,	UpperCamelCase_       :			bool = True				):


         """simple docstring"""



         __A         =	sample
         __A         =	self.encode(UpperCamelCase_				).latents
         __A         =	self.decode(UpperCamelCase_				).sample

         if not return_dict:
             return (dec,)

         return DecoderOutput(sample=UpperCamelCase_				)












import unittest

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



def 			_SCREAMING_SNAKE_CASE						( __lowercase   :			Optional[int]    )						->      List[str]:





    """simple docstring"""

    return x + 2






class       __lowercase      (	unittest.TestCase  ):







     '''simple docstring'''




     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



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

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




     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



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

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




     def 		lowerCAmelCase_     (				self       :			Optional[Any]				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			str				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			List[str]				):


         """simple docstring"""



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

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




     def 		lowerCAmelCase_     (				self       :			Optional[int]				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			Dict				):


         """simple docstring"""



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




     def 		lowerCAmelCase_     (				self       :			List[Any]				):


         """simple docstring"""



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

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





     def 		lowerCAmelCase_     (				self       :			Any				):


         """simple docstring"""



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




