



'''simple docstring'''



import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
 from PIL import Image

 from transformers import CLIPImageProcessor, CLIPProcessor
@require_vision
class     UpperCAmelCase__					(      unittest.TestCase):



 def 				__lowerCamelCase			(					self     )					->							Union[str, Any]:
  __UpperCamelCase			 =					tempfile.mkdtemp()

  # 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(lowercase						,		range(len(lowercase     )     )     )     )
  __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(lowercase     ) + """\n"""     )
  with open(self.merges_file						,		"""w"""						,		encoding="""utf-8"""     ) as fp:
   fp.write("""\n""".join(lowercase     )     )

  __UpperCamelCase			 =					{
      """do_resize""": True,
      """size""": 2_0,
      """do_center_crop""": True,
      """crop_size""": 1_8,
      """do_normalize""": True,
      """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
      """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
  }
  __UpperCamelCase			 =					os.path.join(self.tmpdirname						,		lowercase     )
  with open(self.image_processor_file						,		"""w"""						,		encoding="""utf-8"""     ) as fp:
   json.dump(lowercase						,		lowercase     )



 def 				__lowerCamelCase			(					self						,		**lowercase     )					->							Optional[int]:
  return CLIPTokenizer.from_pretrained(self.tmpdirname						,		**lowercase     )



 def 				__lowerCamelCase			(					self						,		**lowercase     )					->							List[Any]:
  return CLIPTokenizerFast.from_pretrained(self.tmpdirname						,		**lowercase     )



 def 				__lowerCamelCase			(					self						,		**lowercase     )					->							List[Any]:
  return CLIPImageProcessor.from_pretrained(self.tmpdirname						,		**lowercase     )



 def 				__lowerCamelCase			(					self     )					->							int:
  shutil.rmtree(self.tmpdirname     )



 def 				__lowerCamelCase			(					self     )					->							Optional[int]:
  __UpperCamelCase			 =					[np.random.randint(2_5_5						,		size=(3, 3_0, 4_0_0)						,		dtype=np.uinta     )]

  __UpperCamelCase			 =					[Image.fromarray(np.moveaxis(lowercase						,		0						,		-1     )     ) for x in image_inputs]

  return image_inputs



 def 				__lowerCamelCase			(					self     )					->							Union[str, Any]:
  __UpperCamelCase			 =					self.get_tokenizer()
  __UpperCamelCase			 =					self.get_rust_tokenizer()
  __UpperCamelCase			 =					self.get_image_processor()

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )
  processor_slow.save_pretrained(self.tmpdirname     )
  __UpperCamelCase			 =					CLIPProcessor.from_pretrained(self.tmpdirname						,		use_fast=lowercase     )

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )
  processor_fast.save_pretrained(self.tmpdirname     )
  __UpperCamelCase			 =					CLIPProcessor.from_pretrained(self.tmpdirname     )

  self.assertEqual(processor_slow.tokenizer.get_vocab()						,		tokenizer_slow.get_vocab()     )
  self.assertEqual(processor_fast.tokenizer.get_vocab()						,		tokenizer_fast.get_vocab()     )
  self.assertEqual(tokenizer_slow.get_vocab()						,		tokenizer_fast.get_vocab()     )
  self.assertIsInstance(processor_slow.tokenizer						,		lowercase     )
  self.assertIsInstance(processor_fast.tokenizer						,		lowercase     )

  self.assertEqual(processor_slow.image_processor.to_json_string()						,		image_processor.to_json_string()     )
  self.assertEqual(processor_fast.image_processor.to_json_string()						,		image_processor.to_json_string()     )
  self.assertIsInstance(processor_slow.image_processor						,		lowercase     )
  self.assertIsInstance(processor_fast.image_processor						,		lowercase     )



 def 				__lowerCamelCase			(					self     )					->							List[str]:
  __UpperCamelCase			 =					CLIPProcessor(tokenizer=self.get_tokenizer()						,		image_processor=self.get_image_processor()     )
  processor.save_pretrained(self.tmpdirname     )

  __UpperCamelCase			 =					self.get_tokenizer(bos_token="""(BOS)"""						,		eos_token="""(EOS)"""     )
  __UpperCamelCase			 =					self.get_image_processor(do_normalize=lowercase						,		padding_value=1.0     )

  __UpperCamelCase			 =					CLIPProcessor.from_pretrained(
      self.tmpdirname						,		bos_token="""(BOS)"""						,		eos_token="""(EOS)"""						,		do_normalize=lowercase						,		padding_value=1.0     )

  self.assertEqual(processor.tokenizer.get_vocab()						,		tokenizer_add_kwargs.get_vocab()     )
  self.assertIsInstance(processor.tokenizer						,		lowercase     )

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



 def 				__lowerCamelCase			(					self     )					->							int:
  __UpperCamelCase			 =					self.get_image_processor()
  __UpperCamelCase			 =					self.get_tokenizer()

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )

  __UpperCamelCase			 =					self.prepare_image_inputs()

  __UpperCamelCase			 =					image_processor(lowercase						,		return_tensors="""np"""     )
  __UpperCamelCase			 =					processor(images=lowercase						,		return_tensors="""np"""     )

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



 def 				__lowerCamelCase			(					self     )					->							Optional[Any]:
  __UpperCamelCase			 =					self.get_image_processor()
  __UpperCamelCase			 =					self.get_tokenizer()

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )

  __UpperCamelCase			 =					"""lower newer"""

  __UpperCamelCase			 =					processor(text=lowercase     )

  __UpperCamelCase			 =					tokenizer(lowercase     )

  for key in encoded_tok.keys():
   self.assertListEqual(encoded_tok[key]						,		encoded_processor[key]     )



 def 				__lowerCamelCase			(					self     )					->							Dict:
  __UpperCamelCase			 =					self.get_image_processor()
  __UpperCamelCase			 =					self.get_tokenizer()

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )

  __UpperCamelCase			 =					"""lower newer"""
  __UpperCamelCase			 =					self.prepare_image_inputs()

  __UpperCamelCase			 =					processor(text=lowercase						,		images=lowercase     )

  self.assertListEqual(list(inputs.keys()     )						,		["""input_ids""", """attention_mask""", """pixel_values"""]     )

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



 def 				__lowerCamelCase			(					self     )					->							Tuple:
  __UpperCamelCase			 =					self.get_image_processor()
  __UpperCamelCase			 =					self.get_tokenizer()

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )

  __UpperCamelCase			 =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

  __UpperCamelCase			 =					processor.batch_decode(lowercase     )
  __UpperCamelCase			 =					tokenizer.batch_decode(lowercase     )

  self.assertListEqual(lowercase						,		lowercase     )




 def 				__lowerCamelCase			(					self     )					->							int:
  __UpperCamelCase			 =					self.get_image_processor()
  __UpperCamelCase			 =					self.get_tokenizer()

  __UpperCamelCase			 =					CLIPProcessor(tokenizer=lowercase						,		image_processor=lowercase     )

  __UpperCamelCase			 =					"""lower newer"""
  __UpperCamelCase			 =					self.prepare_image_inputs()

  __UpperCamelCase			 =					processor(text=lowercase						,		images=lowercase     )

  self.assertListEqual(list(inputs.keys()     )						,		processor.model_input_names     )










'''simple docstring'''



import functools
import gc
import inspect

import torch

from .imports import is_npu_available, is_xpu_available







def _lowercase				(					*__A    ):



 '''simple docstring'''



 if not isinstance(__A							,__A    ):
  __UpperCamelCase			 =					list(__A    )
 for i in range(len(__A    )    ):
  __UpperCamelCase			 =					None
 gc.collect()
 if is_xpu_available():
  torch.xpu.empty_cache()
 elif is_npu_available():
  torch.npu.empty_cache()
 else:
  torch.cuda.empty_cache()
 return objects







def _lowercase				(					__A    ):



 '''simple docstring'''



 __UpperCamelCase			 =					[
     """CUDA out of memory.""",  # CUDA OOM
     """cuDNN error: CUDNN_STATUS_NOT_SUPPORTED.""",  # CUDNN SNAFU
     """DefaultCPUAllocator: can't allocate memory""",  # CPU OOM
 ]
 if isinstance(__A							,__A    ) and len(exception.args    ) == 1:
  return any(err in exception.args[0] for err in _statements    )
 return False







def _lowercase				(					__A = None							,__A = 128    ):



 '''simple docstring'''



 if function is None:
  return functools.partial(__A							,starting_batch_size=__A    )

 __UpperCamelCase			 =					starting_batch_size

 def decorator(*__A							,**__A    ):
  nonlocal batch_size
  gc.collect()
  if is_xpu_available():
   torch.xpu.empty_cache()
  elif is_npu_available():
   torch.npu.empty_cache()
  else:
   torch.cuda.empty_cache()
  __UpperCamelCase			 =					list(inspect.signature(__A    ).parameters.keys()    )
  # Guard against user error
  if len(__A    ) < (len(__A    ) + 1):
   __UpperCamelCase			 =					""", """.join([f"{arg}={value}" for arg, value in zip(params[1:]							,args[1:]    )]    )
   raise TypeError(
       f"Batch size was passed into `{function.__name__}` as the first argument when called."
       f"Remove this as the decorator already does so: `{function.__name__}({arg_str})`"    )
  while True:
   if batch_size == 0:
    raise RuntimeError("""No executable batch size found, reached zero."""    )
   try:
    return function(__A							,*__A							,**__A    )
   except Exception as e:
    if should_reduce_batch_size(__A    ):
     gc.collect()
     if is_xpu_available():
      torch.xpu.empty_cache()
     elif is_npu_available():
      torch.npu.empty_cache()
     else:
      torch.cuda.empty_cache()
     batch_size //= 2
    else:
     raise

 return decorator





