





'''simple docstring'''







# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
					import torch
class 				__SCREAMING_SNAKE_CASE  (TensorFormatter[Mapping, 'torch.Tensor', Mapping]      ):






				"""simple docstring"""







				def __init__(  self     :					Any					,				__a     :					Tuple=None					,				**__a     :					Tuple					):
								super().__init__(features=__a					)
								_a								= torch_tensor_kwargs
								import torch  # noqa import torch at initialization




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					Any					):
								import torch

								if isinstance(__a					,				__a					) and column:
												if all(
												    isinstance(__a					,				torch.Tensor					) and x.shape == column[0].shape and x.dtype == column[0].dtype
												    for x in column					):
																return torch.stack(__a					)
								return column




				def   UpperCamelCase__   (  self     :					Optional[int]					,				__a     :					Tuple					):
								import torch

								if isinstance(__a					,				(str, bytes, type(__a					))					):
												return value
								elif isinstance(__a					,				(np.character, np.ndarray)					) and np.issubdtype(value.dtype					,				np.character					):
												return value.tolist()

								_a								= {}

								if isinstance(__a					,				(np.number, np.ndarray)					) and np.issubdtype(value.dtype					,				np.integer					):
												_a								= {"dtype": torch.intaa}
								elif isinstance(__a					,				(np.number, np.ndarray)					) and np.issubdtype(value.dtype					,				np.floating					):
												_a								= {"dtype": torch.floataa}
								elif config.PIL_AVAILABLE and "PIL" in sys.modules:
												import PIL.Image

												if isinstance(__a					,				PIL.Image.Image					):
																_a								= np.asarray(__a					)
								return torch.tensor(__a					,				**{**default_dtype, **self.torch_tensor_kwargs}					)




				def   UpperCamelCase__   (  self     :					Optional[int]					,				__a     :					List[Any]					):
								import torch

								# support for torch, tf, jax etc.
								if hasattr(__a					,				"__array__"					) and not isinstance(__a					,				torch.Tensor					):
												_a								= data_struct.__array__()
								# support for nested types like struct of list of struct
								if isinstance(__a					,				np.ndarray					):
												if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
																return self._consolidate([self.recursive_tensorize(__a					) for substruct in data_struct]					)
								elif isinstance(__a					,				(list, tuple)					):
												return self._consolidate([self.recursive_tensorize(__a					) for substruct in data_struct]					)
								return self._tensorize(__a					)




				def   UpperCamelCase__   (  self     :					int					,				__a     :					dict					):
								return map_nested(self._recursive_tensorize					,				__a					,				map_list=__a					)




				def   UpperCamelCase__   (  self     :					Any					,				__a     :					pa.Table					):
								_a								= self.numpy_arrow_extractor().extract_row(__a					)
								_a								= self.python_features_decoder.decode_row(__a					)
								return self.recursive_tensorize(__a					)




				def   UpperCamelCase__   (  self     :					List[Any]					,				__a     :					pa.Table					):
								_a								= self.numpy_arrow_extractor().extract_column(__a					)
								_a								= self.python_features_decoder.decode_column(__a					,				pa_table.column_names[0]					)
								_a								= self.recursive_tensorize(__a					)
								_a								= self._consolidate(__a					)
								return column




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				__a     :					pa.Table					):
								_a								= self.numpy_arrow_extractor().extract_batch(__a					)
								_a								= self.python_features_decoder.decode_batch(__a					)
								_a								= self.recursive_tensorize(__a					)
								for column_name in batch:
												_a								= self._consolidate(batch[column_name]					)
								return batch







'''simple docstring'''







import unittest

from diffusers.pipelines.pipeline_utils import is_safetensors_compatible
class 				__SCREAMING_SNAKE_CASE  (unittest.TestCase      ):






				"""simple docstring"""







				def   UpperCamelCase__   (  self     :					str					):
								_a								= [
								    "safety_checker/pytorch_model.bin",
								    "safety_checker/model.safetensors",
								    "vae/diffusion_pytorch_model.bin",
								    "vae/diffusion_pytorch_model.safetensors",
								    "text_encoder/pytorch_model.bin",
								    "text_encoder/model.safetensors",
								    "unet/diffusion_pytorch_model.bin",
								    "unet/diffusion_pytorch_model.safetensors",
								]
								self.assertTrue(is_safetensors_compatible(__a					)					)




				def   UpperCamelCase__   (  self     :					List[str]					):
								_a								= [
								    "unet/diffusion_pytorch_model.bin",
								    "unet/diffusion_pytorch_model.safetensors",
								]
								self.assertTrue(is_safetensors_compatible(__a					)					)




				def   UpperCamelCase__   (  self     :					List[str]					):
								_a								= [
								    "safety_checker/pytorch_model.bin",
								    "safety_checker/model.safetensors",
								    "vae/diffusion_pytorch_model.bin",
								    "vae/diffusion_pytorch_model.safetensors",
								    "text_encoder/pytorch_model.bin",
								    "text_encoder/model.safetensors",
								    "unet/diffusion_pytorch_model.bin",
								    # Removed: 'unet/diffusion_pytorch_model.safetensors',
								]
								self.assertFalse(is_safetensors_compatible(__a					)					)




				def   UpperCamelCase__   (  self     :					List[str]					):
								_a								= [
								    "text_encoder/pytorch_model.bin",
								    "text_encoder/model.safetensors",
								]
								self.assertTrue(is_safetensors_compatible(__a					)					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					):
								_a								= [
								    "safety_checker/pytorch_model.bin",
								    "safety_checker/model.safetensors",
								    "vae/diffusion_pytorch_model.bin",
								    "vae/diffusion_pytorch_model.safetensors",
								    "text_encoder/pytorch_model.bin",
								    # Removed: 'text_encoder/model.safetensors',
								    "unet/diffusion_pytorch_model.bin",
								    "unet/diffusion_pytorch_model.safetensors",
								]
								self.assertFalse(is_safetensors_compatible(__a					)					)




				def   UpperCamelCase__   (  self     :					str					):
								_a								= [
								    "safety_checker/pytorch_model.fp16.bin",
								    "safety_checker/model.fp16.safetensors",
								    "vae/diffusion_pytorch_model.fp16.bin",
								    "vae/diffusion_pytorch_model.fp16.safetensors",
								    "text_encoder/pytorch_model.fp16.bin",
								    "text_encoder/model.fp16.safetensors",
								    "unet/diffusion_pytorch_model.fp16.bin",
								    "unet/diffusion_pytorch_model.fp16.safetensors",
								]
								_a								= "fp16"
								self.assertTrue(is_safetensors_compatible(__a					,				variant=__a					)					)




				def   UpperCamelCase__   (  self     :					Any					):
								_a								= [
								    "unet/diffusion_pytorch_model.fp16.bin",
								    "unet/diffusion_pytorch_model.fp16.safetensors",
								]
								_a								= "fp16"
								self.assertTrue(is_safetensors_compatible(__a					,				variant=__a					)					)




				def   UpperCamelCase__   (  self     :					Any					):
								# pass variant but use the non-variant filenames
								_a								= [
								    "unet/diffusion_pytorch_model.bin",
								    "unet/diffusion_pytorch_model.safetensors",
								]
								_a								= "fp16"
								self.assertTrue(is_safetensors_compatible(__a					,				variant=__a					)					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					):
								_a								= [
								    "safety_checker/pytorch_model.fp16.bin",
								    "safety_checker/model.fp16.safetensors",
								    "vae/diffusion_pytorch_model.fp16.bin",
								    "vae/diffusion_pytorch_model.fp16.safetensors",
								    "text_encoder/pytorch_model.fp16.bin",
								    "text_encoder/model.fp16.safetensors",
								    "unet/diffusion_pytorch_model.fp16.bin",
								    # Removed: 'unet/diffusion_pytorch_model.fp16.safetensors',
								]
								_a								= "fp16"
								self.assertFalse(is_safetensors_compatible(__a					,				variant=__a					)					)




				def   UpperCamelCase__   (  self     :					Dict					):
								_a								= [
								    "text_encoder/pytorch_model.fp16.bin",
								    "text_encoder/model.fp16.safetensors",
								]
								_a								= "fp16"
								self.assertTrue(is_safetensors_compatible(__a					,				variant=__a					)					)




				def   UpperCamelCase__   (  self     :					List[str]					):
								# pass variant but use the non-variant filenames
								_a								= [
								    "text_encoder/pytorch_model.bin",
								    "text_encoder/model.safetensors",
								]
								_a								= "fp16"
								self.assertTrue(is_safetensors_compatible(__a					,				variant=__a					)					)




				def   UpperCamelCase__   (  self     :					Optional[int]					):
								_a								= [
								    "safety_checker/pytorch_model.fp16.bin",
								    "safety_checker/model.fp16.safetensors",
								    "vae/diffusion_pytorch_model.fp16.bin",
								    "vae/diffusion_pytorch_model.fp16.safetensors",
								    "text_encoder/pytorch_model.fp16.bin",
								    # 'text_encoder/model.fp16.safetensors',
								    "unet/diffusion_pytorch_model.fp16.bin",
								    "unet/diffusion_pytorch_model.fp16.safetensors",
								]
								_a								= "fp16"
								self.assertFalse(is_safetensors_compatible(__a					,				variant=__a					)					)
