


import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING, Dict, Optional

import numpy as np
import pyarrow as pa

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


if TYPE_CHECKING:
				import jax
				import jaxlib

__UpperCamelCase    :						Tuple     =							get_logger()

__UpperCamelCase    :						Optional[dict]     =							None



class 							_UpperCamelCase (						TensorFormatter[Mapping, "jax.Array", Mapping]						):

			'''simple docstring'''







			def __init__(  self  : Tuple       ,  _lowerCamelCase  : Optional[Any]=None       ,  _lowerCamelCase  : Optional[Any]=None       ,  **_lowerCamelCase  : Optional[int]							):






									'''simple docstring'''



									super().__init__(features=__A							)
									import jax
									from jaxlib.xla_client import Device

									if isinstance(__A       ,  __A							):
															raise ValueError(
															    F"""Expected {device} to be a `str` not {type(__A							)}, as `jaxlib.xla_extension.Device` """
															    """is not serializable neither with `pickle` nor with `dill`. Instead you can surround """
															    """the device with `str()` to get its string identifier that will be internally mapped """
															    """to the actual `jaxlib.xla_extension.Device`."""							)
									__lowerCamelCase     :      List[str]       						=						device if isinstance(__A       ,  __A							) else str(jax.devices()[0]							)
									# using global variable since `jaxlib.xla_extension.Device` is not serializable neither
									# with `pickle` nor with `dill`, so we need to use a global variable instead
									global DEVICE_MAPPING
									if DEVICE_MAPPING is None:
															__lowerCamelCase     :      Tuple       						=						self._map_devices_to_str()
									if self.device not in list(DEVICE_MAPPING.keys()							):
															logger.warning(
															    F"""Device with string identifier {self.device} not listed among the available """
															    F"""devices: {list(DEVICE_MAPPING.keys()							)}, so falling back to the default """
															    F"""device: {str(jax.devices()[0]							)}."""							)
															__lowerCamelCase     :      Optional[int]       						=						str(jax.devices()[0]							)
									__lowerCamelCase     :      Tuple       						=						jnp_array_kwargs

			@staticmethod
			def   _snake_case    (  ):






									'''simple docstring'''



									import jax

									return {str(__A							): device for device in jax.devices()}

			def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : str							):






									'''simple docstring'''



									import jax
									import jax.numpy as jnp

									if isinstance(__A       ,  __A							) and column:
															if all(
															    isinstance(__A       ,  jax.Array							) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column							):
																					return jnp.stack(__A       ,  axis=0							)
									return column

			def   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : Dict							):






									'''simple docstring'''



									import jax
									import jax.numpy as jnp

									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()

									__lowerCamelCase     :      List[Any]       						=						{}

									if isinstance(__A       ,  (np.number, np.ndarray)							) and np.issubdtype(value.dtype       ,  np.integer							):
															# the default int precision depends on the jax config
															# see https://jax.readthedocs.io/en/latest/notebooks/Common_Gotchas_in_JAX.html#double-64bit-precision
															if jax.config.jax_enable_xaa:
																					__lowerCamelCase     :      Union[str, Any]       						=						{"dtype": jnp.intaa}
															else:
																					__lowerCamelCase     :      Optional[Any]       						=						{"dtype": jnp.intaa}
									elif isinstance(__A       ,  (np.number, np.ndarray)							) and np.issubdtype(value.dtype       ,  np.floating							):
															__lowerCamelCase     :      List[Any]       						=						{"dtype": jnp.floataa}
									elif config.PIL_AVAILABLE and "PIL" in sys.modules:
															import PIL.Image

															if isinstance(__A       ,  PIL.Image.Image							):
																					__lowerCamelCase     :      int       						=						np.asarray(__A							)

        # using global variable since `jaxlib.xla_extension.Device` is not serializable neither
        # with `pickle` nor with `dill`, so we need to use a global variable instead
									global DEVICE_MAPPING
									if DEVICE_MAPPING is None:
															__lowerCamelCase     :      str       						=						self._map_devices_to_str()

									with jax.default_device(DEVICE_MAPPING[self.device]							):
															# calling jnp.array on a np.ndarray does copy the data
															# see https://github.com/google/jax/issues/4486
															return jnp.array(__A       ,  **{**default_dtype, **self.jnp_array_kwargs}							)

			def   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : Optional[int]							):






									'''simple docstring'''



									import jax

									# support for torch, tf, jax etc.
									if config.TORCH_AVAILABLE and "torch" in sys.modules:
															import torch

															if isinstance(__A       ,  torch.Tensor							):
																					return self._tensorize(data_struct.detach().cpu().numpy()[()]							)
									if hasattr(__A       ,  """__array__"""							) and not isinstance(__A       ,  jax.Array							):
															__lowerCamelCase     :      Optional[int]       						=						data_struct.__array__()
									# support for nested types like struct of list of struct
									if isinstance(__A       ,  np.ndarray							):
															if data_struct.dtype == object:  # jax arrays 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   _snake_case    (  self  : Tuple       ,  _lowerCamelCase  : dict							):






									'''simple docstring'''



									return map_nested(self._recursive_tensorize       ,  __A       ,  map_list=__A							)

			def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : pa.Table							):






									'''simple docstring'''



									__lowerCamelCase     :      Dict       						=						self.numpy_arrow_extractor().extract_row(__A							)
									__lowerCamelCase     :      str       						=						self.python_features_decoder.decode_row(__A							)
									return self.recursive_tensorize(__A							)

			def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : pa.Table							):






									'''simple docstring'''



									__lowerCamelCase     :      Optional[int]       						=						self.numpy_arrow_extractor().extract_column(__A							)
									__lowerCamelCase     :      Dict       						=						self.python_features_decoder.decode_column(__A       ,  pa_table.column_names[0]							)
									__lowerCamelCase     :      Optional[Any]       						=						self.recursive_tensorize(__A							)
									__lowerCamelCase     :      Tuple       						=						self._consolidate(__A							)
									return column


			def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : pa.Table							):






									'''simple docstring'''



									__lowerCamelCase     :      Union[str, Any]       						=						self.numpy_arrow_extractor().extract_batch(__A							)
									__lowerCamelCase     :      Dict       						=						self.python_features_decoder.decode_batch(__A							)
									__lowerCamelCase     :      int       						=						self.recursive_tensorize(__A							)
									for column_name in batch:
															__lowerCamelCase     :      Optional[int]       						=						self._consolidate(batch[column_name]							)
									return batch







import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
    import torch

if is_tf_available():
    import tensorflow as tf



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      int       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Tuple       ,  **_lowerCamelCase  : Any							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      Optional[int]       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Optional[Any]       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Optional[Any]       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[str]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      List[Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop original_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()       ,  input_processor[key].sum()       ,  delta=1E-2							)


   @require_torch
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						self.get_image_processor()

         __lowerCamelCase     :      Union[str, Any]       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						[torch.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Any       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Any       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Tuple       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  torch.tensor(_lowerCamelCase							)       ,  torch.tensor(_lowerCamelCase							)							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Any       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      List[str]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(_lowerCamelCase							):
               __lowerCamelCase     :      Union[str, Any]       						=						processor.post_process_masks(_lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)							)



@require_vision
@require_tf
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[Any]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      int       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]       ,  **_lowerCamelCase  : List[Any]							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=self.get_image_processor()							)
         processor.save_pretrained(self.tmpdirname							)

         __lowerCamelCase     :      Dict       						=						self.get_image_processor(do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         __lowerCamelCase     :      Tuple       						=						SamProcessor.from_pretrained(self.tmpdirname       ,  do_normalize=_lowerCamelCase       ,  padding_value=1.0							)

         self.assertEqual(processor.image_processor.to_json_string()       ,  image_processor_add_kwargs.to_json_string()							)
         self.assertIsInstance(processor.image_processor       ,  _lowerCamelCase							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      str       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      List[Any]       						=						image_processor(_lowerCamelCase       ,  return_tensors="""np"""							)
         __lowerCamelCase     :      Union[str, Any]       						=						processor(images=_lowerCamelCase       ,  return_tensors="""np"""							)

         input_feat_extract.pop("""original_sizes"""							)  # pop original_sizes as it is popped in the processor
         input_feat_extract.pop("""reshaped_input_sizes"""							)  # pop reshaped_input_sizes as it is popped in the processor

         for key in input_feat_extract.keys():
               self.assertAlmostEqual(input_feat_extract[key].sum()       ,  input_processor[key].sum()       ,  delta=1E-2							)


   @require_tf
   def   _snake_case    (  self  : List[Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      List[Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      str       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						[tf.ones((1, 3, 5, 5)							)]

         __lowerCamelCase     :      Optional[Any]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      List[str]       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      Optional[int]       						=						processor.post_process_masks(_lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
             _lowerCamelCase       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  tf.convert_to_tensor(_lowerCamelCase							)       ,  return_tensors="""tf"""       ,  )
         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         # should also work with np
         __lowerCamelCase     :      Tuple       						=						[np.ones((1, 3, 5, 5)							)]
         __lowerCamelCase     :      str       						=						processor.post_process_masks(
             _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)

         self.assertEqual(masks[0].shape       ,  (1, 3, 1_7_6_4, 2_6_4_6)							)

         __lowerCamelCase     :      Union[str, Any]       						=						[[1, 0], [0, 1]]
         with self.assertRaises(tf.errors.InvalidArgumentError							):
               __lowerCamelCase     :      Optional[Any]       						=						processor.post_process_masks(
                   _lowerCamelCase       ,  np.array(_lowerCamelCase							)       ,  np.array(_lowerCamelCase							)       ,  return_tensors="""tf"""							)



@require_vision
@require_torchvision
class 							_UpperCamelCase (						unittest.TestCase						):

   '''simple docstring'''







   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						tempfile.mkdtemp()
         __lowerCamelCase     :      Union[str, Any]       						=						SamImageProcessor()
         __lowerCamelCase     :      Dict       						=						SamProcessor(_lowerCamelCase							)
         processor.save_pretrained(self.tmpdirname							)

   def   _snake_case    (  self  : Optional[int]       ,  **_lowerCamelCase  : Tuple							):






         '''simple docstring'''



         return AutoProcessor.from_pretrained(self.tmpdirname       ,  **_lowerCamelCase							).image_processor

   def   _snake_case    (  self  : List[str]							):






         '''simple docstring'''



         shutil.rmtree(self.tmpdirname							)

   def   _snake_case    (  self  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      List[str]       						=						[np.random.randint(2_5_5       ,  size=(3, 3_0, 4_0_0)       ,  dtype=np.uinta							)]

         __lowerCamelCase     :      str       						=						[Image.fromarray(np.moveaxis(_lowerCamelCase       ,  0       ,  -1							)							) for x in image_inputs]

         return image_inputs

   @is_pt_tf_cross_test
   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						self.get_image_processor()

         __lowerCamelCase     :      Tuple       						=						SamProcessor(image_processor=_lowerCamelCase							)
         __lowerCamelCase     :      str       						=						np.random.randint(0       ,  2       ,  size=(1, 3, 5, 5)							).astype(np.floataa							)
         __lowerCamelCase     :      Tuple       						=						[tf.convert_to_tensor(_lowerCamelCase							)]
         __lowerCamelCase     :      List[str]       						=						[torch.tensor(_lowerCamelCase							)]

         __lowerCamelCase     :      Optional[int]       						=						[[1_7_6_4, 2_6_4_6]]

         __lowerCamelCase     :      Tuple       						=						[[6_8_3, 1_0_2_4]]
         __lowerCamelCase     :      int       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""tf"""							)
         __lowerCamelCase     :      Any       						=						processor.post_process_masks(
             _lowerCamelCase       ,  _lowerCamelCase       ,  _lowerCamelCase       ,  return_tensors="""pt"""							)

         self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()							)							)


   @is_pt_tf_cross_test
   def   _snake_case    (  self  : Dict							):






         '''simple docstring'''



         __lowerCamelCase     :      Optional[int]       						=						self.get_image_processor()

         __lowerCamelCase     :      int       						=						SamProcessor(image_processor=_lowerCamelCase							)

         __lowerCamelCase     :      Any       						=						self.prepare_image_inputs()

         __lowerCamelCase     :      Any       						=						image_processor(_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      Any       						=						processor(images=_lowerCamelCase       ,  return_tensors="""pt"""							)["""pixel_values"""].numpy()

         __lowerCamelCase     :      Dict       						=						image_processor(_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()
         __lowerCamelCase     :      int       						=						processor(images=_lowerCamelCase       ,  return_tensors="""tf"""							)["""pixel_values"""].numpy()

         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)
         self.assertTrue(np.allclose(_lowerCamelCase       ,  _lowerCamelCase							)							)



