


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






  """simple docstring"""





  _SCREAMING_SNAKE_CASE		     =							CLIPTokenizer
  _SCREAMING_SNAKE_CASE		     =							CLIPTokenizerFast
  _SCREAMING_SNAKE_CASE		     =							True
  _SCREAMING_SNAKE_CASE		     =							{}
  _SCREAMING_SNAKE_CASE		     =							False



  def        lowerCAmelCase__	(  self							:							int	) ->			str:




     """simple docstring"""
     super().setUp()

     # fmt: off
     snake_case_            =       ["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
     snake_case_            =       dict(zip(_lowerCAmelCase     ,					range(len(_lowerCAmelCase	)	)	)	)
     snake_case_            =       ["#version: 0.2", "l o", "lo w</w>", "e r</w>"]
     snake_case_            =       {"unk_token": "<unk>"}

     snake_case_            =       os.path.join(self.tmpdirname     ,					VOCAB_FILES_NAMES["vocab_file"]	)
     snake_case_            =       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(_lowerCAmelCase	) + "\n"	)
     with open(self.merges_file     ,					"w"     ,					encoding="utf-8"	) as fp:
        fp.write("\n".join(_lowerCAmelCase	)	)



  def        lowerCAmelCase__	(  self							:							Optional[int]     ,					**_lowerCAmelCase							:							List[Any]	) ->			str:




     """simple docstring"""
     kwargs.update(self.special_tokens_map	)
     return CLIPTokenizer.from_pretrained(self.tmpdirname     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Tuple     ,					**_lowerCAmelCase							:							Dict	) ->			Optional[Any]:




     """simple docstring"""
     kwargs.update(self.special_tokens_map	)
     return CLIPTokenizerFast.from_pretrained(self.tmpdirname     ,					**_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Tuple	) ->			Dict:




     """simple docstring"""
     snake_case_            =       "lower newer"
     snake_case_            =       "lower newer"
     return input_text, output_text



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




     """simple docstring"""
     snake_case_            =       CLIPTokenizer(self.vocab_file     ,					self.merges_file     ,					**self.special_tokens_map	)
     snake_case_            =       "lower newer"
     snake_case_            =       ["lo", "w", "er</w>", "n", "e", "w", "er</w>"]
     snake_case_            =       tokenizer.tokenize(_lowerCAmelCase	)
     self.assertListEqual(_lowerCAmelCase     ,					_lowerCAmelCase	)

     snake_case_            =       tokens + [tokenizer.unk_token]
     snake_case_            =       [1_0, 2, 1_6, 9, 3, 2, 1_6, 2_0]
     self.assertListEqual(tokenizer.convert_tokens_to_ids(_lowerCAmelCase	)     ,					_lowerCAmelCase	)



  @require_ftfy
  def        lowerCAmelCase__	(  self							:							Union[str, Any]	) ->			Tuple:




     """simple docstring"""
     for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
        with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''	):
           snake_case_            =       self.tokenizer_class.from_pretrained(_lowerCAmelCase     ,					**_lowerCAmelCase	)
           snake_case_            =       self.rust_tokenizer_class.from_pretrained(_lowerCAmelCase     ,					**_lowerCAmelCase	)

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

           self.assertListEqual(_lowerCAmelCase     ,					_lowerCAmelCase	)

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

           self.assertListEqual(_lowerCAmelCase     ,					_lowerCAmelCase	)

           # Test that the tokenization is identical on unicode of space type
           snake_case_            =       [
               "\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:
              snake_case_            =       tokenizer_s.tokenize(_lowerCAmelCase	)
              snake_case_            =       tokenizer_r.tokenize(_lowerCAmelCase	)

              self.assertListEqual(_lowerCAmelCase     ,					_lowerCAmelCase	)

           # Test that the tokenization is identical on unicode of line break type
           snake_case_            =       [
               "\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:
              snake_case_            =       tokenizer_s.tokenize(_lowerCAmelCase	)
              snake_case_            =       tokenizer_r.tokenize(_lowerCAmelCase	)

              self.assertListEqual(_lowerCAmelCase     ,					_lowerCAmelCase	)



  def        lowerCAmelCase__	(  self							:							Tuple	) ->			List[Any]:




     """simple docstring"""
     # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
     for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
        with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''	):
           snake_case_            =       "hello"  # `hello` is a token in the vocabulary of `pretrained_name`
           snake_case_            =       F'''{text_of_1_token} {text_of_1_token}'''

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

           snake_case_            =       F''' {text}'''

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



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




     """simple docstring"""
     # Test related to the breaking change introduced in transformers v4.17.0
     # We need to check that an error in raised when the user try to load a previous version of the tokenizer.
     with self.assertRaises(_lowerCAmelCase	) 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							:							Tuple	) ->			int:




     """simple docstring"""
     super().test_tokenization_python_rust_equals()



  def        lowerCAmelCase__	(  self							:							Optional[Any]	) ->			Optional[Any]:




     """simple docstring"""
     # CLIP always lower cases letters
     pass








import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
   import torch_xla.debug.metrics as met


class      __lowerCAmelCase  (			a       ):






  """simple docstring"""





  def __init__(  self							:							Union[str, Any]     ,					*_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							List[Any]=None     ,					_lowerCAmelCase							:							str=None     ,					**_lowerCAmelCase							:							str	) ->			Dict:




     """simple docstring"""
     super().__init__(*_lowerCAmelCase     ,					**_lowerCAmelCase	)
     snake_case_            =       eval_examples
     snake_case_            =       post_process_function



  def        lowerCAmelCase__	(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							Optional[Dataset] = None     ,					_lowerCAmelCase							:							Any=None     ,					_lowerCAmelCase							:							Optional[List[str]] = None     ,					_lowerCAmelCase							:							str = "eval"     ,					**_lowerCAmelCase							:							Tuple     ,					) ->			Dict[str, float]:




     """simple docstring"""
     snake_case_            =       gen_kwargs.copy()
     snake_case_            =       (
         gen_kwargs["max_length"] if gen_kwargs.get("max_length"	) is not None else self.args.generation_max_length
     )
     snake_case_            =       (
         gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"	) is not None else self.args.generation_num_beams
     )
     snake_case_            =       gen_kwargs

     snake_case_            =       self.eval_dataset if eval_dataset is None else eval_dataset
     snake_case_            =       self.get_eval_dataloader(_lowerCAmelCase	)
     snake_case_            =       self.eval_examples if eval_examples is None else eval_examples

     # Temporarily disable metric computation, we will do it in the loop here.
     snake_case_            =       self.compute_metrics
     snake_case_            =       None
     snake_case_            =       time.time()
     snake_case_            =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
        snake_case_            =       eval_loop(
            _lowerCAmelCase     ,					description="Evaluation"     ,					prediction_loss_only=True if compute_metrics is None else None     ,					ignore_keys=_lowerCAmelCase     ,					metric_key_prefix=_lowerCAmelCase     ,					)
     finally:
        snake_case_            =       compute_metrics
     snake_case_            =       self.args.eval_batch_size * self.args.world_size
     if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
        start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _lowerCAmelCase     ,					_lowerCAmelCase     ,					num_samples=output.num_samples     ,					num_steps=math.ceil(output.num_samples / total_batch_size	)     ,					)	)

     if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
        # Only the main node write the results by default
        snake_case_            =       self.post_process_function(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase	)
        snake_case_            =       self.compute_metrics(_lowerCAmelCase	)

        # Prefix all keys with metric_key_prefix + '_'
        for key in list(metrics.keys()	):
           if not key.startswith(F'''{metric_key_prefix}_'''	):
              snake_case_            =       metrics.pop(_lowerCAmelCase	)

        metrics.update(output.metrics	)
     else:
        snake_case_            =       output.metrics

     if self.args.should_log:
        # Only the main node log the results by default
        self.log(_lowerCAmelCase	)

     if self.args.tpu_metrics_debug or self.args.debug:
        # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
        xm.master_print(met.metrics_report()	)

     snake_case_            =       self.callback_handler.on_evaluate(self.args     ,					self.state     ,					self.control     ,					_lowerCAmelCase	)
     return metrics



  def        lowerCAmelCase__	(  self							:							Optional[Any]     ,					_lowerCAmelCase							:							Optional[Any]     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							List[str]=None     ,					_lowerCAmelCase							:							str = "test"     ,					**_lowerCAmelCase							:							Optional[Any]	) ->			Union[str, Any]:




     """simple docstring"""
     snake_case_            =       gen_kwargs.copy()

     snake_case_            =       self.get_test_dataloader(_lowerCAmelCase	)

     # Temporarily disable metric computation, we will do it in the loop here.
     snake_case_            =       self.compute_metrics
     snake_case_            =       None
     snake_case_            =       time.time()
     snake_case_            =       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
        snake_case_            =       eval_loop(
            _lowerCAmelCase     ,					description="Prediction"     ,					prediction_loss_only=True if compute_metrics is None else None     ,					ignore_keys=_lowerCAmelCase     ,					metric_key_prefix=_lowerCAmelCase     ,					)
     finally:
        snake_case_            =       compute_metrics

     snake_case_            =       self.args.eval_batch_size * self.args.world_size
     if F'''{metric_key_prefix}_jit_compilation_time''' in output.metrics:
        start_time += output.metrics[F'''{metric_key_prefix}_jit_compilation_time''']
     output.metrics.update(
         speed_metrics(
             _lowerCAmelCase     ,					_lowerCAmelCase     ,					num_samples=output.num_samples     ,					num_steps=math.ceil(output.num_samples / total_batch_size	)     ,					)	)
     if self.post_process_function is None or self.compute_metrics is None:
        return output

     snake_case_            =       self.post_process_function(_lowerCAmelCase     ,					_lowerCAmelCase     ,					_lowerCAmelCase     ,					"predict"	)
     snake_case_            =       self.compute_metrics(_lowerCAmelCase	)

     # Prefix all keys with metric_key_prefix + '_'
     for key in list(metrics.keys()	):
        if not key.startswith(F'''{metric_key_prefix}_'''	):
           snake_case_            =       metrics.pop(_lowerCAmelCase	)
     metrics.update(output.metrics	)
     return PredictionOutput(predictions=predictions.predictions     ,					label_ids=predictions.label_ids     ,					metrics=_lowerCAmelCase	)




