

"""simple docstring"""




import gc
import unittest

import torch
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()


class 		_A       (			a__				,       a__				,       unittest.TestCase					):
      __a								=	AutoencoderKL
      __a								=	"sample"
      __a								=	1E-2





      @property
      def 					_lowerCamelCase  (  self		)   ->					Union[str, Any]:
         lowerCamelCase__						=			4
         lowerCamelCase__						=			3
         lowerCamelCase__						=			(32, 32)

         lowerCamelCase__						=			floats_tensor((batch_size, num_channels) + sizes		).to(lowerCamelCase_		)

         return {"sample": image}





      @property
      def 					_lowerCamelCase  (  self		)   ->					List[str]:
         return (3, 32, 32)





      @property
      def 					_lowerCamelCase  (  self		)   ->					List[Any]:
         return (3, 32, 32)





      def 					_lowerCamelCase  (  self		)   ->					Union[str, Any]:
         lowerCamelCase__						=			{
             "block_out_channels": [32, 64],
             "in_channels": 3,
             "out_channels": 3,
             "down_block_types": ["DownEncoderBlock2D", "DownEncoderBlock2D"],
             "up_block_types": ["UpDecoderBlock2D", "UpDecoderBlock2D"],
             "latent_channels": 4,
         }
         lowerCamelCase__						=			self.dummy_input
         return init_dict, inputs_dict





      def 					_lowerCamelCase  (  self		)   ->					Optional[Any]:
         pass





      def 					_lowerCamelCase  (  self		)   ->					List[Any]:
         pass





      @unittest.skipIf(torch_device == "mps"    ,						"Gradient checkpointing skipped on MPS"		)
      def 					_lowerCamelCase  (  self		)   ->					Any:
         # enable deterministic behavior for gradient checkpointing
         lowerCamelCase__					,       lowerCamelCase__						=			self.prepare_init_args_and_inputs_for_common()
         lowerCamelCase__						=			self.model_class(**lowerCamelCase_		)
         model.to(lowerCamelCase_		)

         assert not model.is_gradient_checkpointing and model.training

         lowerCamelCase__						=			model(**lowerCamelCase_		).sample
         # run the backwards pass on the model. For backwards pass, for simplicity purpose,
         # we won't calculate the loss and rather backprop on out.sum()
         model.zero_grad()

         lowerCamelCase__						=			torch.randn_like(lowerCamelCase_		)
         lowerCamelCase__						=			(out - labels).mean()
         loss.backward()

         # re-instantiate the model now enabling gradient checkpointing
         lowerCamelCase__						=			self.model_class(**lowerCamelCase_		)
         # clone model
         model_a.load_state_dict(model.state_dict()		)
         model_a.to(lowerCamelCase_		)
         model_a.enable_gradient_checkpointing()

         assert model_a.is_gradient_checkpointing and model_a.training

         lowerCamelCase__						=			model_a(**lowerCamelCase_		).sample
         # run the backwards pass on the model. For backwards pass, for simplicity purpose,
         # we won't calculate the loss and rather backprop on out.sum()
         model_a.zero_grad()
         lowerCamelCase__						=			(out_a - labels).mean()
         loss_a.backward()

         # compare the output and parameters gradients
         self.assertTrue((loss - loss_a).abs() < 1e-5		)
         lowerCamelCase__						=			dict(model.named_parameters()		)
         lowerCamelCase__						=			dict(model_a.named_parameters()		)
         for name, param in named_params.items():
            self.assertTrue(torch_all_close(param.grad.data    ,						named_params_a[name].grad.data    ,						atol=5e-5		)		)





      def 					_lowerCamelCase  (  self		)   ->					Optional[int]:
         lowerCamelCase__					,       lowerCamelCase__						=			AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy"    ,						output_loading_info=lowerCamelCase_		)
         self.assertIsNotNone(lowerCamelCase_		)
         self.assertEqual(len(loading_info["missing_keys"]		)    ,						0		)

         model.to(lowerCamelCase_		)
         lowerCamelCase__						=			model(**self.dummy_input		)

         assert image is not None, "Make sure output is not None"





      def 					_lowerCamelCase  (  self		)   ->					int:
         lowerCamelCase__						=			AutoencoderKL.from_pretrained("fusing/autoencoder-kl-dummy"		)
         lowerCamelCase__						=			model.to(lowerCamelCase_		)
         model.eval()

         if torch_device == "mps":
            lowerCamelCase__						=			torch.manual_seed(0		)
         else:
            lowerCamelCase__						=			torch.Generator(device=lowerCamelCase_		).manual_seed(0		)

         lowerCamelCase__						=			torch.randn(
             1    ,						model.config.in_channels    ,						model.config.sample_size    ,						model.config.sample_size    ,						generator=torch.manual_seed(0		)    ,						)
         lowerCamelCase__						=			image.to(lowerCamelCase_		)
         with torch.no_grad():
            lowerCamelCase__						=			model(lowerCamelCase_    ,						sample_posterior=lowerCamelCase_    ,						generator=lowerCamelCase_		).sample

         lowerCamelCase__						=			output[0, -1, -3:, -3:].flatten().cpu()

         # Since the VAE Gaussian prior's generator is seeded on the appropriate device,
         # the expected output slices are not the same for CPU and GPU.
         if torch_device == "mps":
            lowerCamelCase__						=			torch.tensor(
                [
                    -4.0078e-01,
                    -3.8323e-04,
                    -1.2681e-01,
                    -1.1462e-01,
                    2.0095e-01,
                    1.0893e-01,
                    -8.8247e-02,
                    -3.0361e-01,
                    -9.8644e-03,
                ]		)
         elif torch_device == "cpu":
            lowerCamelCase__						=			torch.tensor(
                [-0.13_52, 0.08_78, 0.04_19, -0.08_18, -0.10_69, 0.06_88, -0.14_58, -0.44_46, -0.00_26]		)
         else:
            lowerCamelCase__						=			torch.tensor(
                [-0.24_21, 0.46_42, 0.25_07, -0.04_38, 0.06_82, 0.31_60, -0.20_18, -0.07_27, 0.24_85]		)

         self.assertTrue(torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						rtol=1e-2		)		)






@slow
class 		_A       (			unittest.TestCase					):





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					int:
         return f'gaussian_noise_s={seed}_shape={"_".join([str(lowerCamelCase_		) for s in shape]		)}.npy'





      def 					_lowerCamelCase  (  self		)   ->					Dict:
         # clean up the VRAM after each test
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__=0    ,						SCREAMING_SNAKE_CASE__=(4, 3, 512, 512)    ,						SCREAMING_SNAKE_CASE__=False		)   ->					List[Any]:
         lowerCamelCase__						=			torch.floataa if fpaa else torch.floataa
         lowerCamelCase__						=			torch.from_numpy(load_hf_numpy(self.get_file_format(lowerCamelCase_    ,						lowerCamelCase_		)		)		).to(lowerCamelCase_		).to(lowerCamelCase_		)
         return image





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__="CompVis/stable-diffusion-v1-4"    ,						SCREAMING_SNAKE_CASE__=False		)   ->					str:
         lowerCamelCase__						=			"fp16" if fpaa else None
         lowerCamelCase__						=			torch.floataa if fpaa else torch.floataa

         lowerCamelCase__						=			AutoencoderKL.from_pretrained(
             lowerCamelCase_    ,						subfolder="vae"    ,						torch_dtype=lowerCamelCase_    ,						revision=lowerCamelCase_    ,						)
         model.to(lowerCamelCase_		).eval()

         return model





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__=0		)   ->					List[Any]:
         if torch_device == "mps":
            return torch.manual_seed(lowerCamelCase_		)
         return torch.Generator(device=lowerCamelCase_		).manual_seed(lowerCamelCase_		)





      @parameterized.expand(
          [
              # fmt: off
              [33, [-0.16_03, 0.98_78, -0.04_95, -0.07_90, -0.27_09, 0.83_75, -0.20_60, -0.08_24], [-0.23_95, 0.00_98, 0.01_02, -0.07_09, -0.28_40, -0.02_74, -0.07_18, -0.18_24]],
              [47, [-0.23_76, 0.11_68, 0.13_32, -0.48_40, -0.25_08, -0.07_91, -0.04_93, -0.40_89], [0.03_50, 0.08_47, 0.04_67, 0.03_44, -0.08_42, -0.05_47, -0.06_33, -0.11_31]],
              # fmt: on
          ]		)
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					Tuple:
         lowerCamelCase__						=			self.get_sd_vae_model()
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_		)
         lowerCamelCase__						=			self.get_generator(lowerCamelCase_		)

         with torch.no_grad():
            lowerCamelCase__						=			model(lowerCamelCase_    ,						generator=lowerCamelCase_    ,						sample_posterior=lowerCamelCase_		).sample

         assert sample.shape == image.shape

         lowerCamelCase__						=			sample[-1, -2:, -2:, :2].flatten().float().cpu()
         lowerCamelCase__						=			torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice		)

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=3e-3		)





      @parameterized.expand(
          [
              # fmt: off
              [33, [-0.05_13, 0.02_89, 1.37_99, 0.21_66, -0.25_73, -0.08_71, 0.51_03, -0.09_99]],
              [47, [-0.41_28, -0.13_20, -0.37_04, 0.19_65, -0.41_16, -0.23_32, -0.33_40, 0.22_47]],
              # fmt: on
          ]		)
      @require_torch_gpu
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					Union[str, Any]:
         lowerCamelCase__						=			self.get_sd_vae_model(fpaa=lowerCamelCase_		)
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_    ,						fpaa=lowerCamelCase_		)
         lowerCamelCase__						=			self.get_generator(lowerCamelCase_		)

         with torch.no_grad():
            lowerCamelCase__						=			model(lowerCamelCase_    ,						generator=lowerCamelCase_    ,						sample_posterior=lowerCamelCase_		).sample

         assert sample.shape == image.shape

         lowerCamelCase__						=			sample[-1, -2:, :2, -2:].flatten().float().cpu()
         lowerCamelCase__						=			torch.tensor(lowerCamelCase_		)

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=1e-2		)





      @parameterized.expand(
          [
              # fmt: off
              [33, [-0.16_09, 0.98_66, -0.04_87, -0.07_77, -0.27_16, 0.83_68, -0.20_55, -0.08_14], [-0.23_95, 0.00_98, 0.01_02, -0.07_09, -0.28_40, -0.02_74, -0.07_18, -0.18_24]],
              [47, [-0.23_77, 0.11_47, 0.13_33, -0.48_41, -0.25_06, -0.08_05, -0.04_91, -0.40_85], [0.03_50, 0.08_47, 0.04_67, 0.03_44, -0.08_42, -0.05_47, -0.06_33, -0.11_31]],
              # fmt: on
          ]		)
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					Dict:
         lowerCamelCase__						=			self.get_sd_vae_model()
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_		)

         with torch.no_grad():
            lowerCamelCase__						=			model(lowerCamelCase_		).sample

         assert sample.shape == image.shape

         lowerCamelCase__						=			sample[-1, -2:, -2:, :2].flatten().float().cpu()
         lowerCamelCase__						=			torch.tensor(expected_slice_mps if torch_device == "mps" else expected_slice		)

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=3e-3		)





      @parameterized.expand(
          [
              # fmt: off
              [13, [-0.20_51, -0.18_03, -0.23_11, -0.21_14, -0.32_92, -0.35_74, -0.29_53, -0.33_23]],
              [37, [-0.26_32, -0.26_25, -0.21_99, -0.27_41, -0.45_39, -0.49_90, -0.37_20, -0.49_25]],
              # fmt: on
          ]		)
      @require_torch_gpu
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					Optional[int]:
         lowerCamelCase__						=			self.get_sd_vae_model()
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_    ,						shape=(3, 4, 64, 64)		)

         with torch.no_grad():
            lowerCamelCase__						=			model.decode(lowerCamelCase_		).sample

         assert list(sample.shape		) == [3, 3, 512, 512]

         lowerCamelCase__						=			sample[-1, -2:, :2, -2:].flatten().cpu()
         lowerCamelCase__						=			torch.tensor(lowerCamelCase_		)

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=1e-3		)





      @parameterized.expand(
          [
              # fmt: off
              [27, [-0.03_69, 0.02_07, -0.07_76, -0.06_82, -0.17_47, -0.19_30, -0.14_65, -0.20_39]],
              [16, [-0.16_28, -0.21_34, -0.27_47, -0.26_42, -0.37_74, -0.44_04, -0.36_87, -0.42_77]],
              # fmt: on
          ]		)
      @require_torch_gpu
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					Dict:
         lowerCamelCase__						=			self.get_sd_vae_model(fpaa=lowerCamelCase_		)
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_    ,						shape=(3, 4, 64, 64)    ,						fpaa=lowerCamelCase_		)

         with torch.no_grad():
            lowerCamelCase__						=			model.decode(lowerCamelCase_		).sample

         assert list(sample.shape		) == [3, 3, 512, 512]

         lowerCamelCase__						=			sample[-1, -2:, :2, -2:].flatten().float().cpu()
         lowerCamelCase__						=			torch.tensor(lowerCamelCase_		)

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=5e-3		)





      @parameterized.expand([(13,), (16,), (27,)]		)
      @require_torch_gpu
      @unittest.skipIf(not is_xformers_available()    ,						reason="xformers is not required when using PyTorch 2.0."		)
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__		)   ->					int:
         lowerCamelCase__						=			self.get_sd_vae_model(fpaa=lowerCamelCase_		)
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_    ,						shape=(3, 4, 64, 64)    ,						fpaa=lowerCamelCase_		)

         with torch.no_grad():
            lowerCamelCase__						=			model.decode(lowerCamelCase_		).sample

         model.enable_xformers_memory_efficient_attention()
         with torch.no_grad():
            lowerCamelCase__						=			model.decode(lowerCamelCase_		).sample

         assert list(sample.shape		) == [3, 3, 512, 512]

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=1e-1		)





      @parameterized.expand([(13,), (16,), (37,)]		)
      @require_torch_gpu
      @unittest.skipIf(not is_xformers_available()    ,						reason="xformers is not required when using PyTorch 2.0."		)
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__		)   ->					Any:
         lowerCamelCase__						=			self.get_sd_vae_model()
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_    ,						shape=(3, 4, 64, 64)		)

         with torch.no_grad():
            lowerCamelCase__						=			model.decode(lowerCamelCase_		).sample

         model.enable_xformers_memory_efficient_attention()
         with torch.no_grad():
            lowerCamelCase__						=			model.decode(lowerCamelCase_		).sample

         assert list(sample.shape		) == [3, 3, 512, 512]

         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=1e-2		)





      @parameterized.expand(
          [
              # fmt: off
              [33, [-0.30_01, 0.09_18, -2.69_84, -3.97_20, -3.20_99, -5.03_53, 1.73_38, -0.20_65, 3.42_67]],
              [47, [-1.50_30, -4.38_71, -6.03_55, -9.11_57, -1.66_61, -2.78_53, 2.16_07, -5.08_23, 2.56_33]],
              # fmt: on
          ]		)
      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		)   ->					List[Any]:
         lowerCamelCase__						=			self.get_sd_vae_model()
         lowerCamelCase__						=			self.get_sd_image(lowerCamelCase_		)
         lowerCamelCase__						=			self.get_generator(lowerCamelCase_		)

         with torch.no_grad():
            lowerCamelCase__						=			model.encode(lowerCamelCase_		).latent_dist
            lowerCamelCase__						=			dist.sample(generator=lowerCamelCase_		)

         assert list(sample.shape		) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]]

         lowerCamelCase__						=			sample[0, -1, -3:, -3:].flatten().cpu()
         lowerCamelCase__						=			torch.tensor(lowerCamelCase_		)

         lowerCamelCase__						=			3e-3 if torch_device != "mps" else 1e-2
         assert torch_all_close(lowerCamelCase_    ,						lowerCamelCase_    ,						atol=lowerCamelCase_		)







"""simple docstring"""




import os
import sys
import warnings
from dataclasses import dataclass, field
from io import BytesIO
from typing import TYPE_CHECKING, Any, ClassVar, Dict, List, Optional, Union

import numpy as np
import pyarrow as pa

from .. import config
from ..download.streaming_download_manager import xopen
from ..table import array_cast
from ..utils.file_utils import is_local_path
from ..utils.py_utils import first_non_null_value, no_op_if_value_is_null, string_to_dict


if TYPE_CHECKING:
     import PIL.Image

     from .features import FeatureType


SCREAMING_SNAKE_CASE_     :			Optional[List[str]]	  =			None
SCREAMING_SNAKE_CASE_     :			Optional[int]	  =			'''<''' if sys.byteorder == '''little''' else '''>'''
# Origin: https://github.com/python-pillow/Pillow/blob/698951e19e19972aeed56df686868f1329981c12/src/PIL/Image.py#L3126 minus "|i1" which values are not preserved correctly when saving and loading an image
SCREAMING_SNAKE_CASE_     :			int	  =			[
    np.dtype('''|b1'''),
    np.dtype('''|u1'''),
    np.dtype('''<u2'''),
    np.dtype('''>u2'''),
    np.dtype('''<i2'''),
    np.dtype('''>i2'''),
    np.dtype('''<u4'''),
    np.dtype('''>u4'''),
    np.dtype('''<i4'''),
    np.dtype('''>i4'''),
    np.dtype('''<f4'''),
    np.dtype('''>f4'''),
    np.dtype('''<f8'''),
    np.dtype('''>f8'''),
]


@dataclass
class 		_A       :
      __a								=	True
      __a								=	None
      # Automatically constructed
      __a								=	"PIL.Image.Image"
      __a								=	pa.struct({'bytes': pa.binary(), 'path': pa.string()}					)
      __a								=	field(default='Image'				,       init=__a				,       repr=__a					)





      def __call__(  self		)   ->					Dict:
         return self.pa_type





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__		)   ->					dict:
         if config.PIL_AVAILABLE:
            import PIL.Image
         else:
            raise ImportError("To support encoding images, please install 'Pillow'."		)

         if isinstance(SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		):
            lowerCamelCase__						=			np.array(SCREAMING_SNAKE_CASE__		)

         if isinstance(SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		):
            return {"path": value, "bytes": None}
         elif isinstance(SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__		):
            return {"path": None, "bytes": value}
         elif isinstance(SCREAMING_SNAKE_CASE__    ,						np.ndarray		):
            # convert the image array to PNG/TIFF bytes
            return encode_np_array(SCREAMING_SNAKE_CASE__		)
         elif isinstance(SCREAMING_SNAKE_CASE__    ,						PIL.Image.Image		):
            # convert the PIL image to bytes (default format is PNG/TIFF)
            return encode_pil_image(SCREAMING_SNAKE_CASE__		)
         elif value.get("path"		) is not None and os.path.isfile(value["path"]		):
            # we set "bytes": None to not duplicate the data if they're already available locally
            return {"bytes": None, "path": value.get("path"		)}
         elif value.get("bytes"		) is not None or value.get("path"		) is not None:
            # store the image bytes, and path is used to infer the image format using the file extension
            return {"bytes": value.get("bytes"		), "path": value.get("path"		)}
         else:
            raise ValueError(
                f'An image sample should have one of \'path\' or \'bytes\' but they are missing or None in {value}.'		)





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__    ,						SCREAMING_SNAKE_CASE__=None		)   ->					"PIL.Image.Image":
         if not self.decode:
            raise RuntimeError("Decoding is disabled for this feature. Please use Image(decode=True) instead."		)

         if config.PIL_AVAILABLE:
            import PIL.Image
         else:
            raise ImportError("To support decoding images, please install 'Pillow'."		)

         if token_per_repo_id is None:
            lowerCamelCase__						=			{}

         lowerCamelCase__					,       lowerCamelCase__						=			value["path"], value["bytes"]
         if bytes_ is None:
            if path is None:
               raise ValueError(f'An image should have one of \'path\' or \'bytes\' but both are None in {value}.'		)
            else:
               if is_local_path(SCREAMING_SNAKE_CASE__		):
                  lowerCamelCase__						=			PIL.Image.open(SCREAMING_SNAKE_CASE__		)
               else:
                  lowerCamelCase__						=			path.split("::"		)[-1]
                  try:
                     lowerCamelCase__						=			string_to_dict(SCREAMING_SNAKE_CASE__    ,						config.HUB_DATASETS_URL		)["repo_id"]
                     lowerCamelCase__						=			token_per_repo_id.get(SCREAMING_SNAKE_CASE__		)
                  except ValueError:
                     lowerCamelCase__						=			None
                  with xopen(SCREAMING_SNAKE_CASE__    ,						"rb"    ,						use_auth_token=SCREAMING_SNAKE_CASE__		) as f:
                     lowerCamelCase__						=			BytesIO(f.read()		)
                  lowerCamelCase__						=			PIL.Image.open(bytes_		)
         else:
            lowerCamelCase__						=			PIL.Image.open(BytesIO(bytes_		)		)
         image.load()  # to avoid "Too many open files" errors
         return image





      def 					_lowerCamelCase  (  self		)   ->					Union["FeatureType", Dict[str, "FeatureType"]]:
         from .features import Value

         return (
             self
             if self.decode
             else {
                 "bytes": Value("binary"		),
                 "path": Value("string"		),
             }
         )





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__		)   ->					pa.StructArray:
         if pa.types.is_string(storage.type		):
            lowerCamelCase__						=			pa.array([None] * len(SCREAMING_SNAKE_CASE__		)    ,						type=pa.binary()		)
            lowerCamelCase__						=			pa.StructArray.from_arrays([bytes_array, storage]    ,						["bytes", "path"]    ,						mask=storage.is_null()		)
         elif pa.types.is_binary(storage.type		):
            lowerCamelCase__						=			pa.array([None] * len(SCREAMING_SNAKE_CASE__		)    ,						type=pa.string()		)
            lowerCamelCase__						=			pa.StructArray.from_arrays([storage, path_array]    ,						["bytes", "path"]    ,						mask=storage.is_null()		)
         elif pa.types.is_struct(storage.type		):
            if storage.type.get_field_index("bytes"		) >= 0:
               lowerCamelCase__						=			storage.field("bytes"		)
            else:
               lowerCamelCase__						=			pa.array([None] * len(SCREAMING_SNAKE_CASE__		)    ,						type=pa.binary()		)
            if storage.type.get_field_index("path"		) >= 0:
               lowerCamelCase__						=			storage.field("path"		)
            else:
               lowerCamelCase__						=			pa.array([None] * len(SCREAMING_SNAKE_CASE__		)    ,						type=pa.string()		)
            lowerCamelCase__						=			pa.StructArray.from_arrays([bytes_array, path_array]    ,						["bytes", "path"]    ,						mask=storage.is_null()		)
         elif pa.types.is_list(storage.type		):
            lowerCamelCase__						=			pa.array(
                [encode_np_array(np.array(SCREAMING_SNAKE_CASE__		)		)["bytes"] if arr is not None else None for arr in storage.to_pylist()]    ,						type=pa.binary()    ,						)
            lowerCamelCase__						=			pa.array([None] * len(SCREAMING_SNAKE_CASE__		)    ,						type=pa.string()		)
            lowerCamelCase__						=			pa.StructArray.from_arrays(
                [bytes_array, path_array]    ,						["bytes", "path"]    ,						mask=bytes_array.is_null()		)
         return array_cast(SCREAMING_SNAKE_CASE__    ,						self.pa_type		)





      def 					_lowerCamelCase  (  self    ,						SCREAMING_SNAKE_CASE__		)   ->					pa.StructArray:

         @no_op_if_value_is_null
         def path_to_bytes(SCREAMING_SNAKE_CASE__		):
            with xopen(SCREAMING_SNAKE_CASE__    ,						"rb"		) as f:
               lowerCamelCase__						=			f.read()
            return bytes_

         lowerCamelCase__						=			pa.array(
             [
                 (path_to_bytes(x["path"]		) if x["bytes"] is None else x["bytes"]) if x is not None else None
                 for x in storage.to_pylist()
             ]    ,						type=pa.binary()    ,						)
         lowerCamelCase__						=			pa.array(
             [os.path.basename(SCREAMING_SNAKE_CASE__		) if path is not None else None for path in storage.field("path"		).to_pylist()]    ,						type=pa.string()    ,						)
         lowerCamelCase__						=			pa.StructArray.from_arrays([bytes_array, path_array]    ,						["bytes", "path"]    ,						mask=bytes_array.is_null()		)
         return array_cast(SCREAMING_SNAKE_CASE__    ,						self.pa_type		)






def 	UpperCAmelCase__   (				)      ->      List[str]:




   """simple docstring"""
   if config.PIL_AVAILABLE:
      import PIL.Image
   else:
      raise ImportError("To support encoding images, please install 'Pillow'."   )

   global _IMAGE_COMPRESSION_FORMATS
   if _IMAGE_COMPRESSION_FORMATS is None:
      PIL.Image.init()
      lowerCamelCase__						=			list(set(PIL.Image.OPEN.keys()   ) & set(PIL.Image.SAVE.keys()   )   )
   return _IMAGE_COMPRESSION_FORMATS



def 	UpperCAmelCase__   (				A__   )      ->      bytes:




   """simple docstring"""
   lowerCamelCase__						=			BytesIO()
   if image.format in list_image_compression_formats():
      lowerCamelCase__						=			image.format
   else:
      lowerCamelCase__						=			"PNG" if image.mode in ["1", "L", "LA", "RGB", "RGBA"] else "TIFF"
   image.save(A__	,    format=A__   )
   return buffer.getvalue()



def 	UpperCAmelCase__   (				A__   )      ->      dict:




   """simple docstring"""
   if hasattr(A__	,    "filename"   ) and image.filename != "":
      return {"path": image.filename, "bytes": None}
   else:
      return {"path": None, "bytes": image_to_bytes(A__   )}



def 	UpperCAmelCase__   (				A__   )      ->      dict:




   """simple docstring"""
   if config.PIL_AVAILABLE:
      import PIL.Image
   else:
      raise ImportError("To support encoding images, please install 'Pillow'."   )

   lowerCamelCase__						=			array.dtype
   lowerCamelCase__						=			dtype.byteorder if dtype.byteorder != "=" else _NATIVE_BYTEORDER
   lowerCamelCase__						=			dtype.kind
   lowerCamelCase__						=			dtype.itemsize

   lowerCamelCase__						=			None

   # Multi-channel array case (only np.dtype("|u1") is allowed)
   if array.shape[2:]:
      lowerCamelCase__						=			np.dtype("|u1"   )
      if dtype_kind not in ["u", "i"]:
         raise TypeError(
             f'Unsupported array dtype {dtype} for image encoding. Only {dest_dtype} is supported for multi-channel arrays.'   )
      if dtype is not dest_dtype:
         warnings.warn(f'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''   )
    # Exact match
   elif dtype in _VALID_IMAGE_ARRAY_DTPYES:
      lowerCamelCase__						=			dtype
   else:  # Downcast the type within the kind (np.can_cast(from_type, to_type, casting="same_kind") doesn't behave as expected, so do it manually)
      while dtype_itemsize >= 1:
         lowerCamelCase__						=			dtype_byteorder + dtype_kind + str(A__   )
         lowerCamelCase__						=			np.dtype(A__   )
         if dest_dtype in _VALID_IMAGE_ARRAY_DTPYES:
            warnings.warn(f'Downcasting array dtype {dtype} to {dest_dtype} to be compatible with \'Pillow\''   )
            break
         else:
            dtype_itemsize //= 2

   if dest_dtype is None:
      raise TypeError(
          f'Cannot convert dtype {dtype} to a valid image dtype. Valid image dtypes: {_VALID_IMAGE_ARRAY_DTPYES}'   )
   lowerCamelCase__						=			PIL.Image.fromarray(array.astype(A__   )   )
   return {"path": None, "bytes": image_to_bytes(A__   )}



def 	UpperCAmelCase__   (				A__   )      ->      List[dict]:




   """simple docstring"""
   if config.PIL_AVAILABLE:
      import PIL.Image
   else:
      raise ImportError("To support encoding images, please install 'Pillow'."   )

   if objs:
      lowerCamelCase__					,       lowerCamelCase__						=			first_non_null_value(A__   )
      if isinstance(A__	,    A__   ):
         return [{"path": obj, "bytes": None} if obj is not None else None for obj in objs]
      if isinstance(A__	,    np.ndarray   ):
         lowerCamelCase__						=			no_op_if_value_is_null(A__   )
         return [obj_to_image_dict_func(A__   ) for obj in objs]
      elif isinstance(A__	,    PIL.Image.Image   ):
         lowerCamelCase__						=			no_op_if_value_is_null(A__   )
         return [obj_to_image_dict_func(A__   ) for obj in objs]
      else:
         return objs
   else:
      return objs




