






import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor



class 				__snake_case  (     unittest.TestCase						):


    '''simple docstring'''







    @property
    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= 1
        SCREAMING_SNAKE_CASE__       							= 3
        SCREAMING_SNAKE_CASE__       							= 8
        SCREAMING_SNAKE_CASE__       							= 8

        SCREAMING_SNAKE_CASE__       							= torch.rand((batch_size, num_channels, height, width)    )

        return sample



    def    lowercase_				(					self      ,							A_    ):





        '''simple docstring'''

        if isinstance(image[0]      ,							PIL.Image.Image    ):
            return np.stack([np.array(A_    ) for i in image]      ,							axis=0    )
        elif isinstance(A_      ,							torch.Tensor    ):
            return image.cpu().numpy().transpose(0      ,							2      ,							3      ,							1    )
        return image



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= VaeImageProcessor(do_resize=A_      ,							do_normalize=A_    )

        SCREAMING_SNAKE_CASE__       							= self.dummy_sample
        SCREAMING_SNAKE_CASE__       							= self.to_np(A_    )

        for output_type in ["pt", "np", "pil"]:
            SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
                image_processor.preprocess(A_    )      ,							output_type=A_      ,							)
            SCREAMING_SNAKE_CASE__       							= self.to_np(A_    )
            SCREAMING_SNAKE_CASE__       							= (input_np * 2_55).round() if output_type == '''pil''' else input_np
            assert (
                np.abs(in_np - out_np    ).max() < 1E-6
            ), f'''decoded output does not match input for output_type {output_type}'''



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= VaeImageProcessor(do_resize=A_      ,							do_normalize=A_    )
        SCREAMING_SNAKE_CASE__       							= self.dummy_sample.cpu().numpy().transpose(0      ,							2      ,							3      ,							1    )

        for output_type in ["pt", "np", "pil"]:
            SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(image_processor.preprocess(A_    )      ,							output_type=A_    )

            SCREAMING_SNAKE_CASE__       							= self.to_np(A_    )
            SCREAMING_SNAKE_CASE__       							= (input_np * 2_55).round() if output_type == '''pil''' else input_np
            assert (
                np.abs(in_np - out_np    ).max() < 1E-6
            ), f'''decoded output does not match input for output_type {output_type}'''



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= VaeImageProcessor(do_resize=A_      ,							do_normalize=A_    )

        SCREAMING_SNAKE_CASE__       							= self.dummy_sample.cpu().numpy().transpose(0      ,							2      ,							3      ,							1    )
        SCREAMING_SNAKE_CASE__       							= image_processor.numpy_to_pil(A_    )

        for output_type in ["pt", "np", "pil"]:
            SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(image_processor.preprocess(A_    )      ,							output_type=A_    )
            for i, o in zip(A_      ,							A_    ):
                SCREAMING_SNAKE_CASE__       							= np.array(A_    )
                SCREAMING_SNAKE_CASE__       							= self.to_np(A_    ) if output_type == '''pil''' else (self.to_np(A_    ) * 2_55).round()
                assert (
                    np.abs(in_np - out_np    ).max() < 1E-6
                ), f'''decoded output does not match input for output_type {output_type}'''



    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= VaeImageProcessor(do_resize=A_      ,							do_normalize=A_    )

        SCREAMING_SNAKE_CASE__       							= self.dummy_sample
        SCREAMING_SNAKE_CASE__       							= input_pt_ad.squeeze(0    )

        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)
        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)

        SCREAMING_SNAKE_CASE__       							= self.to_np(self.dummy_sample    )
        SCREAMING_SNAKE_CASE__       							= input_np_ad.squeeze(0    )

        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)
        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)

        assert np.abs(out_pt_ad - out_pt_ad    ).max() < 1E-6
        assert np.abs(out_np_ad - out_np_ad    ).max() < 1E-6




    def    lowercase_				(					self    ):





        '''simple docstring'''

        SCREAMING_SNAKE_CASE__       							= VaeImageProcessor(do_resize=A_      ,							do_normalize=A_    )

        SCREAMING_SNAKE_CASE__       							= self.dummy_sample
        SCREAMING_SNAKE_CASE__       							= list(A_    )

        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)

        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)

        SCREAMING_SNAKE_CASE__       							= self.to_np(self.dummy_sample    )
        list(A_    )

        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)

        SCREAMING_SNAKE_CASE__       							= image_processor.postprocess(
            image_processor.preprocess(A_    )      ,							output_type='''np'''      ,							)

        assert np.abs(out_pt_ad - out_pt_list    ).max() < 1E-6
        assert np.abs(out_np_ad - out_np_list    ).max() < 1E-6







import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
  import transformer_engine.pytorch as te


def        __snake_case       (						lowerCAmelCase_   ,       lowerCAmelCase_=True   ,       lowerCAmelCase_=True   ,       lowerCAmelCase_=True  )							->	Tuple:
    if not is_fpa_available():
        raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''  )
    for name, module in model.named_children():
        if isinstance(lowerCAmelCase_   ,       nn.Linear  ) and to_transformer_engine and _convert_linear:
            # Return early if the linear layer weights are not multiples of 16
            if any(p % 1_6 != 0 for p in module.weight.shape  ):
                return
            SCREAMING_SNAKE_CASE__       							= module.bias is not None
            SCREAMING_SNAKE_CASE__       							= te.Linear(
                module.in_features   ,       module.out_features   ,       bias=lowerCAmelCase_   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       nn.LayerNorm  ) and to_transformer_engine and _convert_ln:
            SCREAMING_SNAKE_CASE__       							= te.LayerNorm(module.normalized_shape[0]   ,       eps=module.eps   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       te.Linear  ) and not to_transformer_engine and _convert_linear:
            SCREAMING_SNAKE_CASE__       							= module.bias is not None
            SCREAMING_SNAKE_CASE__       							= nn.Linear(
                module.in_features   ,       module.out_features   ,       bias=lowerCAmelCase_   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            if has_bias:
                SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_   ,       te.LayerNorm  ) and not to_transformer_engine and _convert_ln:
            SCREAMING_SNAKE_CASE__       							= nn.LayerNorm(module.normalized_shape[0]   ,       eps=module.eps   ,       params_dtype=module.weight.dtype  )
            SCREAMING_SNAKE_CASE__       							= module.weight.data.clone()
            SCREAMING_SNAKE_CASE__       							= module.bias.data.clone()

            setattr(lowerCAmelCase_   ,       lowerCAmelCase_   ,       lowerCAmelCase_  )
        else:
            convert_model(
                lowerCAmelCase_   ,       to_transformer_engine=lowerCAmelCase_   ,       _convert_linear=_convert_linear   ,       _convert_ln=_convert_ln   ,       )



def        __snake_case       (						lowerCAmelCase_  )							->	Union[str, Any]:
    if not is_fpa_available():
        raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''  )
    for m in model.modules():
        if isinstance(lowerCAmelCase_   ,       (te.LayerNorm, te.Linear)  ):
            return True
    return False
