



# Lint as: python3
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

a__      :				int   	=			get_logger()

a__      :				Optional[dict]   	=			None



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

   '''simple docstring'''




   def __init__(						self  :			Any			,				lowerCAmelCase  :			Tuple=None			,				lowerCAmelCase  :			Optional[int]=None			,				**lowerCAmelCase  :			Optional[Any])	->				List[str]:




         """simple docstring"""

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

         if isinstance(lowerCAmelCase			,				lowerCAmelCase):
               raise ValueError(
                   f'''Expected {device} to be a `str` not {type(lowerCAmelCase)}, 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`.')
         lowercase__     =  device if isinstance(lowerCAmelCase			,				lowerCAmelCase) 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:
               lowercase__     =  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])}.''')
               lowercase__     =  str(jax.devices()[0])
         lowercase__     =  jnp_array_kwargs


   @staticmethod
   def 			UpperCAmelCase		(						)	->				Dict[str, "jaxlib.xla_extension.Device"]:




         """simple docstring"""

         import jax

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


   def 			UpperCAmelCase		(						self  :			Dict			,				lowerCAmelCase  :			List[str])	->				Dict:




         """simple docstring"""

         import jax
         import jax.numpy as jnp

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


   def 			UpperCAmelCase		(						self  :			Tuple			,				lowerCAmelCase  :			Optional[Any])	->				int:




         """simple docstring"""

         import jax
         import jax.numpy as jnp

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

         lowercase__     =  {}

         if isinstance(lowerCAmelCase			,				(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:
                     lowercase__     =  {'dtype': jnp.intaa}
               else:
                     lowercase__     =  {'dtype': jnp.intaa}
         elif isinstance(lowerCAmelCase			,				(np.number, np.ndarray)) and np.issubdtype(value.dtype			,				np.floating):
               lowercase__     =  {'dtype': jnp.floataa}
         elif config.PIL_AVAILABLE and "PIL" in sys.modules:
               import PIL.Image

               if isinstance(lowerCAmelCase			,				PIL.Image.Image):
                     lowercase__     =  np.asarray(lowerCAmelCase)

        # 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:
               lowercase__     =  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(lowerCAmelCase			,				**{**default_dtype, **self.jnp_array_kwargs})


   def 			UpperCAmelCase		(						self  :			Optional[Any]			,				lowerCAmelCase  :			Union[str, Any])	->				List[str]:




         """simple docstring"""

         import jax

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

               if isinstance(lowerCAmelCase			,				torch.Tensor):
                     return self._tensorize(data_struct.detach().cpu().numpy()[()])
         if hasattr(lowerCAmelCase			,				'__array__') and not isinstance(lowerCAmelCase			,				jax.Array):
               lowercase__     =  data_struct.__array__()
         # support for nested types like struct of list of struct
         if isinstance(lowerCAmelCase			,				np.ndarray):
               if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
                     return self._consolidate([self.recursive_tensorize(lowerCAmelCase) for substruct in data_struct])
         elif isinstance(lowerCAmelCase			,				(list, tuple)):
               return self._consolidate([self.recursive_tensorize(lowerCAmelCase) for substruct in data_struct])
         return self._tensorize(lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Optional[Any]			,				lowerCAmelCase  :			dict)	->				int:




         """simple docstring"""

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


   def 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			pa.Table)	->				Mapping:




         """simple docstring"""

         lowercase__     =  self.numpy_arrow_extractor().extract_row(lowerCAmelCase)
         lowercase__     =  self.python_features_decoder.decode_row(lowerCAmelCase)
         return self.recursive_tensorize(lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Any			,				lowerCAmelCase  :			pa.Table)	->				"jax.Array":




         """simple docstring"""

         lowercase__     =  self.numpy_arrow_extractor().extract_column(lowerCAmelCase)
         lowercase__     =  self.python_features_decoder.decode_column(lowerCAmelCase			,				pa_table.column_names[0])
         lowercase__     =  self.recursive_tensorize(lowerCAmelCase)
         lowercase__     =  self._consolidate(lowerCAmelCase)
         return column



   def 			UpperCAmelCase		(						self  :			List[Any]			,				lowerCAmelCase  :			pa.Table)	->				Mapping:




         """simple docstring"""

         lowercase__     =  self.numpy_arrow_extractor().extract_batch(lowerCAmelCase)
         lowercase__     =  self.python_features_decoder.decode_batch(lowerCAmelCase)
         lowercase__     =  self.recursive_tensorize(lowerCAmelCase)
         for column_name in batch:
               lowercase__     =  self._consolidate(batch[column_name])
         return batch





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin



class       UpperCAmelCase__(							unittest.TestCase     ,							lowerCamelCase	):

   '''simple docstring'''




   def 			UpperCAmelCase		(						self  :			List[str])	->				Any:




         """simple docstring"""

         lowercase__     =  load_tool('text-classification')
         self.tool.setup()
         lowercase__     =  load_tool('text-classification'			,				remote=lowerCAmelCase)


   def 			UpperCAmelCase		(						self  :			Any)	->				Tuple:




         """simple docstring"""

         lowercase__     =  self.tool('That\'s quite cool'			,				['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')


   def 			UpperCAmelCase		(						self  :			int)	->				Optional[int]:




         """simple docstring"""

         lowercase__     =  self.remote_tool('That\'s quite cool'			,				['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')


   def 			UpperCAmelCase		(						self  :			Optional[Any])	->				Union[str, Any]:




         """simple docstring"""

         lowercase__     =  self.tool(text='That\'s quite cool'			,				labels=['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')



   def 			UpperCAmelCase		(						self  :			Any)	->				Any:




         """simple docstring"""

         lowercase__     =  self.remote_tool(text='That\'s quite cool'			,				labels=['positive', 'negative'])
         self.assertEqual(lowerCAmelCase			,				'positive')
