


# 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

_SCREAMING_SNAKE_CASE     :					Dict      =       get_logger()

_SCREAMING_SNAKE_CASE     :					Optional[dict]      =       None

class     A__   (       TensorFormatter[Mapping, 'jax.Array', Mapping]      ):







    """simple docstring"""

    def __init__(			self     ,   __snake_case=None     ,   __snake_case=None     ,   **__snake_case       ):
         super().__init__(features=__snake_case       )
         import jax
         from jaxlib.xla_client import Device

         if isinstance(__snake_case     ,   __snake_case       ):
              raise ValueError(
                  F'''Expected {device} to be a `str` not {type(__snake_case       )}, 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`.'''       )
         snake_case											=							device if isinstance(__snake_case     ,   __snake_case       ) 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:
              snake_case											=							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]       )}.'''       )
              snake_case											=							str(jax.devices()[0]       )
         snake_case											=							jnp_array_kwargs
    @staticmethod
    def 						a_     (			):
         import jax

         return {str(__snake_case       ): device for device in jax.devices()}
    def 						a_     (			self     ,   __snake_case       ):
         import jax
         import jax.numpy as jnp

         if isinstance(__snake_case     ,   __snake_case       ) and column:
              if all(
                  isinstance(__snake_case     ,   jax.Array       ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column       ):
                   return jnp.stack(__snake_case     ,   axis=0       )
         return column
    def 						a_     (			self     ,   __snake_case       ):
         import jax
         import jax.numpy as jnp

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

         snake_case											=							{}

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

              if isinstance(__snake_case     ,   PIL.Image.Image       ):
                   snake_case											=							np.asarray(__snake_case       )

        # 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:
              snake_case											=							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(__snake_case     ,   **{**default_dtype, **self.jnp_array_kwargs}       )
    def 						a_     (			self     ,   __snake_case       ):
         import jax

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

              if isinstance(__snake_case     ,   torch.Tensor       ):
                   return self._tensorize(data_struct.detach().cpu().numpy()[()]       )
         if hasattr(__snake_case     ,   '''__array__'''       ) and not isinstance(__snake_case     ,   jax.Array       ):
              snake_case											=							data_struct.__array__()
         # support for nested types like struct of list of struct
         if isinstance(__snake_case     ,   np.ndarray       ):
              if data_struct.dtype == object:  # jax arrays cannot be instantied from an array of objects
                   return self._consolidate([self.recursive_tensorize(__snake_case       ) for substruct in data_struct]       )
         elif isinstance(__snake_case     ,   (list, tuple)       ):
              return self._consolidate([self.recursive_tensorize(__snake_case       ) for substruct in data_struct]       )
         return self._tensorize(__snake_case       )
    def 						a_     (			self     ,   __snake_case       ):
         return map_nested(self._recursive_tensorize     ,   __snake_case     ,   map_list=__snake_case       )
    def 						a_     (			self     ,   __snake_case       ):
         snake_case											=							self.numpy_arrow_extractor().extract_row(__snake_case       )
         snake_case											=							self.python_features_decoder.decode_row(__snake_case       )
         return self.recursive_tensorize(__snake_case       )
    def 						a_     (			self     ,   __snake_case       ):
         snake_case											=							self.numpy_arrow_extractor().extract_column(__snake_case       )
         snake_case											=							self.python_features_decoder.decode_column(__snake_case     ,   pa_table.column_names[0]       )
         snake_case											=							self.recursive_tensorize(__snake_case       )
         snake_case											=							self._consolidate(__snake_case       )
         return column




    def 						a_     (			self     ,   __snake_case       ):
         snake_case											=							self.numpy_arrow_extractor().extract_batch(__snake_case       )
         snake_case											=							self.python_features_decoder.decode_batch(__snake_case       )
         snake_case											=							self.recursive_tensorize(__snake_case       )
         for column_name in batch:
              snake_case											=							self._consolidate(batch[column_name]       )
         return batch








import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
 from transformers import TvltImageProcessor

if is_speech_available():
 from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class     A__   (       unittest.TestCase      ):







    """simple docstring"""

    def 						a_     (			self       ):
         snake_case											=							'''ZinengTang/tvlt-base'''
         snake_case											=							tempfile.mkdtemp()
    def 						a_     (			self     ,   **__snake_case       ):
         return TvltImageProcessor.from_pretrained(self.checkpoint     ,   **__snake_case       )
    def 						a_     (			self     ,   **__snake_case       ):
         return TvltFeatureExtractor.from_pretrained(self.checkpoint     ,   **__snake_case       )
    def 						a_     (			self       ):
         shutil.rmtree(self.tmpdirname       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )
         processor.save_pretrained(self.tmpdirname       )
         snake_case											=							TvltProcessor.from_pretrained(self.tmpdirname       )

         self.assertIsInstance(processor.feature_extractor     ,   __snake_case       )
         self.assertIsInstance(processor.image_processor     ,   __snake_case       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							np.ones([1_2_0_0_0]       )

         snake_case											=							feature_extractor(__snake_case     ,   return_tensors='''np'''       )
         snake_case											=							processor(audio=__snake_case     ,   return_tensors='''np'''       )

         for key in audio_dict.keys():
              self.assertAlmostEqual(audio_dict[key].sum()     ,   input_processor[key].sum()     ,   delta=1E-2       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							np.ones([3, 2_2_4, 2_2_4]       )

         snake_case											=							image_processor(__snake_case     ,   return_tensors='''np'''       )
         snake_case											=							processor(images=__snake_case     ,   return_tensors='''np'''       )

         for key in image_dict.keys():
              self.assertAlmostEqual(image_dict[key].sum()     ,   input_processor[key].sum()     ,   delta=1E-2       )
    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         snake_case											=							np.ones([1_2_0_0_0]       )
         snake_case											=							np.ones([3, 2_2_4, 2_2_4]       )

         snake_case											=							processor(audio=__snake_case     ,   images=__snake_case       )

         self.assertListEqual(list(inputs.keys()       )     ,   ['''audio_values''', '''audio_mask''', '''pixel_values''', '''pixel_mask''']       )

         # test if it raises when no input is passed
         with pytest.raises(__snake_case       ):
              processor()




    def 						a_     (			self       ):
         snake_case											=							self.get_image_processor()
         snake_case											=							self.get_feature_extractor()

         snake_case											=							TvltProcessor(image_processor=__snake_case     ,   feature_extractor=__snake_case       )

         self.assertListEqual(
             processor.model_input_names     ,   image_processor.model_input_names + feature_extractor.model_input_names     ,   msg='''`processor` and `image_processor`+`feature_extractor` model input names do not match'''     ,   )




