



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__       =	get_logger()

UpperCAmelCase__       =	None







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



			def __init__(						self       :  Optional[Any]			,		A       :  Dict=None			,		A       :  Any=None			,		**A       :  Any)       ->   List[Any]:



						"""simple docstring"""






						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`.')
						_UpperCAmelCase			=       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:
									_UpperCAmelCase			=       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])}.")
									_UpperCAmelCase			=       str(jax.devices()[0])
						_UpperCAmelCase			=       jnp_array_kwargs



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



						"""simple docstring"""






						import jax

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



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  str)       ->   Optional[int]:



						"""simple docstring"""






						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    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  Optional[Any])       ->   Union[str, Any]:



						"""simple docstring"""






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

						_UpperCAmelCase			=       {}

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

									if isinstance(__snake_case			,		PIL.Image.Image):
												_UpperCAmelCase			=       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:
									_UpperCAmelCase			=       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    _lowerCamelCase	(						self       :  List[Any]			,		A       :  Tuple)       ->   List[Any]:



						"""simple docstring"""






						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):
									_UpperCAmelCase			=       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    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  dict)       ->   str:



						"""simple docstring"""






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



			def    _lowerCamelCase	(						self       :  List[str]			,		A       :  pa.Table)       ->   Mapping:



						"""simple docstring"""






						_UpperCAmelCase			=       self.numpy_arrow_extractor().extract_row(__snake_case)
						_UpperCAmelCase			=       self.python_features_decoder.decode_row(__snake_case)
						return self.recursive_tensorize(__snake_case)



			def    _lowerCamelCase	(						self       :  Union[str, Any]			,		A       :  pa.Table)       ->   "jax.Array":



						"""simple docstring"""






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



			def    _lowerCamelCase	(						self       :  Any			,		A       :  pa.Table)       ->   Mapping:



						"""simple docstring"""






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





import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class       __lowerCAmelCase   (      A    ,						unittest.TestCase						):
			UpperCamelCase        =					CLIPTokenizer
			UpperCamelCase        =					CLIPTokenizerFast
			UpperCamelCase        =					True
			UpperCamelCase        =					{}
			UpperCamelCase        =					False



			def    _lowerCamelCase	(						self       :  List[str])       ->   List[str]:



						"""simple docstring"""






						super().setUp()

						# fmt: off
						_UpperCAmelCase			=       ['l', 'o', 'w', 'e', 'r', 's', 't', 'i', 'd', 'n', 'lo', 'l</w>', 'w</w>', 'r</w>', 't</w>', 'low</w>', 'er</w>', 'lowest</w>', 'newer</w>', 'wider', '<unk>', '<|startoftext|>', '<|endoftext|>']
						# fmt: on
						_UpperCAmelCase			=       dict(zip(A			,		range(len(A))))
						_UpperCAmelCase			=       ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>']
						_UpperCAmelCase			=       {'unk_token': '<unk>'}

						_UpperCAmelCase			=       os.path.join(self.tmpdirname			,		VOCAB_FILES_NAMES['vocab_file'])
						_UpperCAmelCase			=       os.path.join(self.tmpdirname			,		VOCAB_FILES_NAMES['merges_file'])
						with open(self.vocab_file			,		'w'			,		encoding='utf-8') as fp:
									fp.write(json.dumps(A) + '\n')
						with open(self.merges_file			,		'w'			,		encoding='utf-8') as fp:
									fp.write('\n'.join(A))



			def    _lowerCamelCase	(						self       :  Optional[Any]			,		**A       :  str)       ->   Optional[int]:



						"""simple docstring"""






						kwargs.update(self.special_tokens_map)
						return CLIPTokenizer.from_pretrained(self.tmpdirname			,		**A)



			def    _lowerCamelCase	(						self       :  Any			,		**A       :  Dict)       ->   Union[str, Any]:



						"""simple docstring"""






						kwargs.update(self.special_tokens_map)
						return CLIPTokenizerFast.from_pretrained(self.tmpdirname			,		**A)



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  Union[str, Any])       ->   int:



						"""simple docstring"""






						_UpperCAmelCase			=       'lower newer'
						_UpperCAmelCase			=       'lower newer'
						return input_text, output_text



			def    _lowerCamelCase	(						self       :  Dict)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       CLIPTokenizer(self.vocab_file			,		self.merges_file			,		**self.special_tokens_map)
						_UpperCAmelCase			=       'lower newer'
						_UpperCAmelCase			=       ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>']
						_UpperCAmelCase			=       tokenizer.tokenize(A)
						self.assertListEqual(A			,		A)

						_UpperCAmelCase			=       tokens + [tokenizer.unk_token]
						_UpperCAmelCase			=       [10, 2, 16, 9, 3, 2, 16, 20]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(A)			,		A)



			@require_ftfy
			def    _lowerCamelCase	(						self       :  List[Any])       ->   Dict:



						"""simple docstring"""






						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
									with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"):
												_UpperCAmelCase			=       self.tokenizer_class.from_pretrained(A			,		**A)
												_UpperCAmelCase			=       self.rust_tokenizer_class.from_pretrained(A			,		**A)

												_UpperCAmelCase			=       'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'
												_UpperCAmelCase			=       tokenizer_s.tokenize(A)
												_UpperCAmelCase			=       tokenizer_r.tokenize(A)

												self.assertListEqual(A			,		A)

												# Test that the tokenization is identical on an example containing a character (Latin Small Letter A
												# with Tilde) encoded in 2 different ways
												_UpperCAmelCase			=       'xa\u0303y' + ' ' + 'x\xe3y'
												_UpperCAmelCase			=       tokenizer_s.tokenize(A)
												_UpperCAmelCase			=       tokenizer_r.tokenize(A)

												self.assertListEqual(A			,		A)

												# Test that the tokenization is identical on unicode of space type
												_UpperCAmelCase			=       [
												    '\u0009',  # (horizontal tab, '\t')
												    '\u000B',  # (vertical tab)
												    '\u000C',  # (form feed)
												    '\u0020',  # (space, ' ')
												    '\u200E',  # (left-to-right mark):w
												    '\u200F',  # (right-to-left mark)
												]
												for unicode_seq in spaces_unicodes:
															_UpperCAmelCase			=       tokenizer_s.tokenize(A)
															_UpperCAmelCase			=       tokenizer_r.tokenize(A)

															self.assertListEqual(A			,		A)

												# Test that the tokenization is identical on unicode of line break type
												_UpperCAmelCase			=       [
												    '\u000A',  # (line feed, '\n')
												    '\r\n',  # (carriage return and line feed, '\r\n')
												    '\u000D',  # (carriage return, '\r')
												    '\r',  # (carriage return, '\r')
												    '\u000D',  # (carriage return, '\r')
												    '\u2028',  # (line separator)
												    '\u2029',  # (paragraph separator)
												    # "\u0085", # (next line)
												]

												# The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
												# it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
												# space (and thus into an empty list).

												for unicode_seq in line_break_unicodes:
															_UpperCAmelCase			=       tokenizer_s.tokenize(A)
															_UpperCAmelCase			=       tokenizer_r.tokenize(A)

															self.assertListEqual(A			,		A)



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



						"""simple docstring"""






						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
									with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"):
												_UpperCAmelCase			=       'hello'  # `hello` is a token in the vocabulary of `pretrained_name`
												_UpperCAmelCase			=       F"{text_of_1_token} {text_of_1_token}"

												_UpperCAmelCase			=       self.rust_tokenizer_class.from_pretrained(
												    A			,		use_fast=A			,		)
												_UpperCAmelCase			=       tokenizer_r(A			,		return_offsets_mapping=A			,		add_special_tokens=A)
												self.assertEqual(encoding.offset_mapping[0]			,		(0, len(A)))
												self.assertEqual(
												    encoding.offset_mapping[1]			,		(len(A) + 1, len(A) + 1 + len(A))			,		)

												_UpperCAmelCase			=       F" {text}"

												_UpperCAmelCase			=       self.rust_tokenizer_class.from_pretrained(
												    A			,		use_fast=A			,		)
												_UpperCAmelCase			=       tokenizer_r(A			,		return_offsets_mapping=A			,		add_special_tokens=A)
												self.assertEqual(encoding.offset_mapping[0]			,		(1, 1 + len(A)))
												self.assertEqual(
												    encoding.offset_mapping[1]			,		(1 + len(A) + 1, 1 + len(A) + 1 + len(A))			,		)



			def    _lowerCamelCase	(						self       :  Tuple)       ->   str:



						"""simple docstring"""






						with self.assertRaises(A) as context:
									self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer')

						self.assertTrue(
						    context.exception.args[0].startswith(
						        'The `backend_tokenizer` provided does not match the expected format.'))



			@require_ftfy
			def    _lowerCamelCase	(						self       :  int)       ->   int:



						"""simple docstring"""






						super().test_tokenization_python_rust_equals()



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



						"""simple docstring"""






						pass
