



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   )		->							Union[str, Any]:




   """simple docstring"""







   _A          =      1
   _A          =      3
   _A          =      8
   _A          =      8

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

   return sample
 def lowercase_						(  self		,      a   )		->							List[Any]:




   """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   )		->							List[Any]:




   """simple docstring"""







   _A          =      VaeImageProcessor(do_resize=a		,      do_normalize=a   )

   _A          =      self.dummy_sample
   _A          =      self.to_np(a   )

   for output_type in ["pt", "np", "pil"]:
     _A          =      image_processor.postprocess(
         image_processor.preprocess(a   )		,      output_type=a		,      )
     _A          =      self.to_np(a   )
     _A          =      (input_np * 2_5_5).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   )		->							Union[str, Any]:




   """simple docstring"""







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

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

     _A          =      self.to_np(a   )
     _A          =      (input_np * 2_5_5).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   )		->							str:




   """simple docstring"""







   _A          =      VaeImageProcessor(do_resize=a		,      do_normalize=a   )

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

   for output_type in ["pt", "np", "pil"]:
     _A          =      image_processor.postprocess(image_processor.preprocess(a   )		,      output_type=a   )
     for i, o in zip(a		,      a   ):
       _A          =      np.array(a   )
       _A          =      self.to_np(a   ) if output_type == '''pil''' else (self.to_np(a   ) * 2_5_5).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   )		->							Dict:




   """simple docstring"""







   _A          =      VaeImageProcessor(do_resize=a		,      do_normalize=a   )

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

   _A          =      image_processor.postprocess(
       image_processor.preprocess(a   )		,      output_type='''np'''		,      )
   _A          =      image_processor.postprocess(
       image_processor.preprocess(a   )		,      output_type='''np'''		,      )

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

   _A          =      image_processor.postprocess(
       image_processor.preprocess(a   )		,      output_type='''np'''		,      )
   _A          =      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   )		->							int:




   """simple docstring"""







   _A          =      VaeImageProcessor(do_resize=a		,      do_normalize=a   )

   _A          =      self.dummy_sample
   _A          =      list(a   )

   _A          =      image_processor.postprocess(
       image_processor.preprocess(a   )		,      output_type='''np'''		,      )

   _A          =      image_processor.postprocess(
       image_processor.preprocess(a   )		,      output_type='''np'''		,      )

   _A          =      self.to_np(self.dummy_sample   )
   list(a   )

   _A          =      image_processor.postprocess(
       image_processor.preprocess(a   )		,      output_type='''np'''		,      )

   _A          =      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 importlib.metadata
import subprocess
import sys




def       UpperCAmelCase__	(							__snake_case = False							)				->	List[Any]:
  _A          =      False
  if "IPython" in sys.modules:
    _A          =      '''google.colab''' in str(sys.modules['''IPython'''].get_ipython()							)

  if in_colab:
    if upgrade:
      _A          =      ['''pip''', '''install''', '''-U''', '''torch''']
      subprocess.run(__snake_case  ,			check=__snake_case							)
    # get the current version of torch
    _A          =      importlib.metadata.version('''torch'''							)
    _A          =      torch_version[: torch_version.rindex('''.'''							)]
    _A          =      F'''https://storage.googleapis.com/tpu-pytorch/wheels/colab/torch_xla-{torch_version_trunc}-cp37-cp37m-linux_x86_64.whl'''
    _A          =      ['''pip''', '''install''', xla_wheel]
    subprocess.run(__snake_case  ,			check=__snake_case							)
  else:
    raise RuntimeError('''`install_xla` utility works only on google colab.'''							)