



"""simple docstring"""
import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation
class 				lowerCamelCase  (			unittest.TestCase				):






					'''simple docstring'''





					def    lowerCAmelCase_				(       self:							Optional[Any]						)   ->	Union[str, Any]:
											snake_case_ :List[str]						=			get_activation("""swish"""						)

											self.assertIsInstance(snake_case						,     nn.SiLU						)

											self.assertEqual(act(torch.tensor(-100						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertNotEqual(act(torch.tensor(-1						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(0						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(20						,     dtype=torch.floataa						)						).item()						,     20						)






					def    lowerCAmelCase_				(       self:							Optional[Any]						)   ->	Optional[Any]:
											snake_case_ :Optional[int]						=			get_activation("""silu"""						)

											self.assertIsInstance(snake_case						,     nn.SiLU						)

											self.assertEqual(act(torch.tensor(-100						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertNotEqual(act(torch.tensor(-1						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(0						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(20						,     dtype=torch.floataa						)						).item()						,     20						)






					def    lowerCAmelCase_				(       self:							Optional[Any]						)   ->	Any:
											snake_case_ :Optional[Any]						=			get_activation("""mish"""						)

											self.assertIsInstance(snake_case						,     nn.Mish						)

											self.assertEqual(act(torch.tensor(-200						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertNotEqual(act(torch.tensor(-1						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(0						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(20						,     dtype=torch.floataa						)						).item()						,     20						)






					def    lowerCAmelCase_				(       self:							Union[str, Any]						)   ->	Optional[Any]:
											snake_case_ :List[Any]						=			get_activation("""gelu"""						)

											self.assertIsInstance(snake_case						,     nn.GELU						)

											self.assertEqual(act(torch.tensor(-100						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertNotEqual(act(torch.tensor(-1						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(0						,     dtype=torch.floataa						)						).item()						,     0						)
											self.assertEqual(act(torch.tensor(20						,     dtype=torch.floataa						)						).item()						,     20						)







import PIL.Image
import PIL.ImageOps
from packaging import version
from PIL import Image


if version.parse(version.parse(PIL.__version__).base_version) >= version.parse('''9.1.0'''):
	lowerCAmelCase_    =				{
	    '''linear''': PIL.Image.Resampling.BILINEAR,
	    '''bilinear''': PIL.Image.Resampling.BILINEAR,
	    '''bicubic''': PIL.Image.Resampling.BICUBIC,
	    '''lanczos''': PIL.Image.Resampling.LANCZOS,
	    '''nearest''': PIL.Image.Resampling.NEAREST,
	}
else:
	lowerCAmelCase_    =				{
	    '''linear''': PIL.Image.LINEAR,
	    '''bilinear''': PIL.Image.BILINEAR,
	    '''bicubic''': PIL.Image.BICUBIC,
	    '''lanczos''': PIL.Image.LANCZOS,
	    '''nearest''': PIL.Image.NEAREST,
	}






def      lowerCamelCase_						(				_UpperCamelCase )		->						Optional[int]:






				"""simple docstring"""


				snake_case_						:    Dict				=  (images / 2 + 0.5).clamp(0 ,				1 )
				snake_case_						:    Dict				=  images.cpu().permute(0 ,				2 ,				3 ,				1 ).float().numpy()
				snake_case_						:    int				=  numpy_to_pil(_UpperCamelCase )
				return images






def      lowerCamelCase_						(				_UpperCamelCase )		->						List[Any]:






				"""simple docstring"""


				if images.ndim == 3:
								snake_case_						:    Optional[Any]				=  images[None, ...]
				snake_case_						:    Any				=  (images * 255).round().astype('''uint8''' )
				if images.shape[-1] == 1:
								# special case for grayscale (single channel) images
								snake_case_						:    str				=  [Image.fromarray(image.squeeze() ,				mode='''L''' ) for image in images]
				else:
								snake_case_						:    List[Any]				=  [Image.fromarray(_UpperCamelCase ) for image in images]

				return pil_images
