






'''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 OwlViTImageProcessor, OwlViTProcessor


@require_vision
class        _UpperCAmelCase						(		unittest.TestCase			):




    def 				lowerCAmelCase__				(			self  :       Any			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     tempfile.mkdtemp()

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

       lowercase_	:    List[Any]           =     os.path.join(self.tmpdirname  ,     VOCAB_FILES_NAMES["vocab_file"]			)
       lowercase_	:    List[str]           =     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			)			)

       lowercase_	:    Any           =     {
           "do_resize": True,
           "size": 2_0,
           "do_center_crop": True,
           "crop_size": 1_8,
           "do_normalize": True,
           "image_mean": [0.4814_5466, 0.457_8275, 0.4082_1073],
           "image_std": [0.2686_2954, 0.2613_0258, 0.2757_7711],
       }
       lowercase_	:    List[Any]           =     os.path.join(self.tmpdirname  ,     a			)
       with open(self.image_processor_file  ,     "w"  ,     encoding="utf-8"			) as fp:
          json.dump(a  ,     a			)




    def 				lowerCAmelCase__				(			self  :       int  ,     **a  :       Any			):






       '''simple docstring'''






       return CLIPTokenizer.from_pretrained(self.tmpdirname  ,     pad_token="!"  ,     **a			)




    def 				lowerCAmelCase__				(			self  :       List[Any]  ,     **a  :       str			):






       '''simple docstring'''






       return CLIPTokenizerFast.from_pretrained(self.tmpdirname  ,     pad_token="!"  ,     **a			)




    def 				lowerCAmelCase__				(			self  :       Any  ,     **a  :       Dict			):






       '''simple docstring'''






       return OwlViTImageProcessor.from_pretrained(self.tmpdirname  ,     **a			)




    def 				lowerCAmelCase__				(			self  :       Optional[Any]			):






       '''simple docstring'''






       shutil.rmtree(self.tmpdirname			)




    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       lowercase_	:    int           =     [np.random.randint(2_5_5  ,     size=(3, 3_0, 4_0_0)  ,     dtype=np.uinta			)]

       lowercase_	:    Tuple           =     [Image.fromarray(np.moveaxis(a  ,     0  ,     -1			)			) for x in image_inputs]

       return image_inputs




    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     self.get_tokenizer()
       lowercase_	:    Dict           =     self.get_rust_tokenizer()
       lowercase_	:    str           =     self.get_image_processor()

       lowercase_	:    List[str]           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)
       processor_slow.save_pretrained(self.tmpdirname			)
       lowercase_	:    Optional[Any]           =     OwlViTProcessor.from_pretrained(self.tmpdirname  ,     use_fast=a			)

       lowercase_	:    Optional[int]           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)
       processor_fast.save_pretrained(self.tmpdirname			)
       lowercase_	:    Optional[int]           =     OwlViTProcessor.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  ,     a			)
       self.assertIsInstance(processor_fast.tokenizer  ,     a			)

       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  ,     a			)
       self.assertIsInstance(processor_fast.image_processor  ,     a			)




    def 				lowerCAmelCase__				(			self  :       List[str]			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     OwlViTProcessor(tokenizer=self.get_tokenizer()  ,     image_processor=self.get_image_processor()			)
       processor.save_pretrained(self.tmpdirname			)

       lowercase_	:    str           =     self.get_tokenizer(bos_token="(BOS)"  ,     eos_token="(EOS)"			)
       lowercase_	:    Dict           =     self.get_image_processor(do_normalize=a			)

       lowercase_	:    str           =     OwlViTProcessor.from_pretrained(
           self.tmpdirname  ,     bos_token="(BOS)"  ,     eos_token="(EOS)"  ,     do_normalize=a			)

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

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




    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       lowercase_	:    List[Any]           =     self.get_image_processor()
       lowercase_	:    Optional[int]           =     self.get_tokenizer()

       lowercase_	:    List[str]           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)

       lowercase_	:    Any           =     self.prepare_image_inputs()

       lowercase_	:    Any           =     image_processor(a  ,     return_tensors="np"			)
       lowercase_	:    Optional[Any]           =     processor(images=a  ,     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  :       List[Any]			):






       '''simple docstring'''






       lowercase_	:    Dict           =     self.get_image_processor()
       lowercase_	:    str           =     self.get_tokenizer()

       lowercase_	:    Optional[Any]           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)

       lowercase_	:    List[Any]           =     "lower newer"

       lowercase_	:    Dict           =     processor(text=a  ,     return_tensors="np"			)

       lowercase_	:    Optional[int]           =     tokenizer(a  ,     return_tensors="np"			)

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




    def 				lowerCAmelCase__				(			self  :       Tuple			):






       '''simple docstring'''






       lowercase_	:    str           =     self.get_image_processor()
       lowercase_	:    str           =     self.get_tokenizer()

       lowercase_	:    Tuple           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)

       lowercase_	:    Optional[int]           =     "lower newer"
       lowercase_	:    Optional[int]           =     self.prepare_image_inputs()

       lowercase_	:    int           =     processor(text=a  ,     images=a			)

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

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




    def 				lowerCAmelCase__				(			self  :       Any			):






       '''simple docstring'''






       lowercase_	:    str           =     "google/owlvit-base-patch32"
       lowercase_	:    Optional[int]           =     OwlViTProcessor.from_pretrained(a			)

       lowercase_	:    Any           =     ["cat", "nasa badge"]
       lowercase_	:    List[Any]           =     processor(text=a			)

       lowercase_	:    Any           =     1_6
       self.assertListEqual(list(inputs.keys()			)  ,     ["input_ids", "attention_mask"]			)
       self.assertEqual(inputs["input_ids"].shape  ,     (2, seq_length)			)

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




    def 				lowerCAmelCase__				(			self  :       Dict			):






       '''simple docstring'''






       lowercase_	:    int           =     "google/owlvit-base-patch32"
       lowercase_	:    Union[str, Any]           =     OwlViTProcessor.from_pretrained(a			)

       lowercase_	:    Optional[int]           =     [["cat", "nasa badge"], ["person"]]
       lowercase_	:    Dict           =     processor(text=a			)

       lowercase_	:    Tuple           =     1_6
       lowercase_	:    List[Any]           =     len(a			)
       lowercase_	:    Optional[Any]           =     max([len(a			) for texts in input_texts]			)

       self.assertListEqual(list(inputs.keys()			)  ,     ["input_ids", "attention_mask"]			)
       self.assertEqual(inputs["input_ids"].shape  ,     (batch_size * num_max_text_queries, seq_length)			)

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




    def 				lowerCAmelCase__				(			self  :       int			):






       '''simple docstring'''






       lowercase_	:    Optional[int]           =     "google/owlvit-base-patch32"
       lowercase_	:    int           =     OwlViTProcessor.from_pretrained(a			)

       lowercase_	:    Tuple           =     ["cat", "nasa badge"]
       lowercase_	:    Optional[int]           =     processor(text=a			)

       lowercase_	:    Dict           =     1_6
       lowercase_	:    int           =     inputs["input_ids"]
       lowercase_	:    Union[str, Any]           =     [
           [4_9_4_0_6, 2_3_6_8, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
           [4_9_4_0_6, 6_8_4_1, 1_1_3_0_1, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       ]

       self.assertListEqual(list(inputs.keys()			)  ,     ["input_ids", "attention_mask"]			)
       self.assertEqual(inputs["input_ids"].shape  ,     (2, seq_length)			)
       self.assertListEqual(list(input_ids[0]			)  ,     predicted_ids[0]			)
       self.assertListEqual(list(input_ids[1]			)  ,     predicted_ids[1]			)




    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






       lowercase_	:    List[str]           =     self.get_image_processor()
       lowercase_	:    List[Any]           =     self.get_tokenizer()

       lowercase_	:    Optional[Any]           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)

       lowercase_	:    str           =     self.prepare_image_inputs()
       lowercase_	:    List[str]           =     self.prepare_image_inputs()

       lowercase_	:    Union[str, Any]           =     processor(images=a  ,     query_images=a			)

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

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





    def 				lowerCAmelCase__				(			self  :       str			):






       '''simple docstring'''






       lowercase_	:    str           =     self.get_image_processor()
       lowercase_	:    List[Any]           =     self.get_tokenizer()

       lowercase_	:    Any           =     OwlViTProcessor(tokenizer=a  ,     image_processor=a			)

       lowercase_	:    List[Any]           =     [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

       lowercase_	:    Union[str, Any]           =     processor.batch_decode(a			)
       lowercase_	:    List[str]           =     tokenizer.batch_decode(a			)

       self.assertListEqual(a  ,     a			)












'''simple docstring'''
import math
from enum import Enum
from typing import Optional, Union

from torch.optim import Optimizer
from torch.optim.lr_scheduler import LambdaLR

from .utils import logging


UpperCamelCase__ =			logging.get_logger(__name__)


class        _UpperCAmelCase						(		snake_case			):
    __lowerCamelCase:   Tuple		       =    'linear'
    __lowerCamelCase:   Any		       =    'cosine'
    __lowerCamelCase:   Optional[Any]		       =    'cosine_with_restarts'
    __lowerCamelCase:   Tuple		       =    'polynomial'
    __lowerCamelCase:   int		       =    'constant'
    __lowerCamelCase:   Optional[Any]		       =    'constant_with_warmup'
    __lowerCamelCase:   List[str]		       =    'piecewise_constant'




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase = -1							):


   """simple docstring"""




   return LambdaLR(_UpperCamelCase						,		lambda _UpperCamelCase							: 1						,		last_epoch=_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase = -1							):


   """simple docstring"""




   def lr_lambda(_UpperCamelCase							):
      if current_step < num_warmup_steps:
         return float(_UpperCamelCase							) / float(max(1.0						,		_UpperCamelCase							)							)
      return 1.0

   return LambdaLR(_UpperCamelCase						,		_UpperCamelCase						,		last_epoch=_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase = -1							):


   """simple docstring"""




   lowercase_	:    List[Any]           =     {}
   lowercase_	:    Dict           =     step_rules.split(","							)
   for rule_str in rule_list[:-1]:
      lowercase_    ,		lowercase_	:    Any           =     rule_str.split(":"							)
      lowercase_	:    List[Any]           =     int(_UpperCamelCase							)
      lowercase_	:    int           =     float(_UpperCamelCase							)
      lowercase_	:    Optional[int]           =     value
   lowercase_	:    Union[str, Any]           =     float(rule_list[-1]							)

   def create_rules_function(_UpperCamelCase						,		_UpperCamelCase							):
      def rule_func(_UpperCamelCase							) -> float:
         lowercase_	:    Optional[Any]           =     sorted(rules_dict.keys()							)
         for i, sorted_step in enumerate(_UpperCamelCase							):
            if steps < sorted_step:
               return rules_dict[sorted_steps[i]]
         return last_lr_multiple

      return rule_func

   lowercase_	:    Optional[int]           =     create_rules_function(_UpperCamelCase						,		_UpperCamelCase							)

   return LambdaLR(_UpperCamelCase						,		_UpperCamelCase						,		last_epoch=_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase=-1							):


   """simple docstring"""




   def lr_lambda(_UpperCamelCase							):
      if current_step < num_warmup_steps:
         return float(_UpperCamelCase							) / float(max(1						,		_UpperCamelCase							)							)
      return max(
          0.0						,		float(num_training_steps - current_step							) / float(max(1						,		num_training_steps - num_warmup_steps							)							)							)

   return LambdaLR(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase = 0.5						,		_UpperCamelCase = -1							):


   """simple docstring"""




   def lr_lambda(_UpperCamelCase							):
      if current_step < num_warmup_steps:
         return float(_UpperCamelCase							) / float(max(1						,		_UpperCamelCase							)							)
      lowercase_	:    List[str]           =     float(current_step - num_warmup_steps							) / float(max(1						,		num_training_steps - num_warmup_steps							)							)
      return max(0.0						,		0.5 * (1.0 + math.cos(math.pi * float(_UpperCamelCase							) * 2.0 * progress							))							)

   return LambdaLR(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase = 1						,		_UpperCamelCase = -1							):


   """simple docstring"""




   def lr_lambda(_UpperCamelCase							):
      if current_step < num_warmup_steps:
         return float(_UpperCamelCase							) / float(max(1						,		_UpperCamelCase							)							)
      lowercase_	:    Dict           =     float(current_step - num_warmup_steps							) / float(max(1						,		num_training_steps - num_warmup_steps							)							)
      if progress >= 1.0:
         return 0.0
      return max(0.0						,		0.5 * (1.0 + math.cos(math.pi * ((float(_UpperCamelCase							) * progress) % 1.0)							))							)

   return LambdaLR(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)




def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase=1e-7						,		_UpperCamelCase=1.0						,		_UpperCamelCase=-1							):


   """simple docstring"""




   lowercase_	:    Dict           =     optimizer.defaults["lr"]
   if not (lr_init > lr_end):
      raise ValueError(F"""lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})"""							)

   def lr_lambda(_UpperCamelCase							):
      if current_step < num_warmup_steps:
         return float(_UpperCamelCase							) / float(max(1						,		_UpperCamelCase							)							)
      elif current_step > num_training_steps:
         return lr_end / lr_init  # as LambdaLR multiplies by lr_init
      else:
         lowercase_	:    int           =     lr_init - lr_end
         lowercase_	:    Optional[int]           =     num_training_steps - num_warmup_steps
         lowercase_	:    Optional[Any]           =     1 - (current_step - num_warmup_steps) / decay_steps
         lowercase_	:    List[Any]           =     lr_range * pct_remaining**power + lr_end
         return decay / lr_init  # as LambdaLR multiplies by lr_init

   return LambdaLR(_UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase							)


UpperCamelCase__ =			{
    SchedulerType.LINEAR: get_linear_schedule_with_warmup,
    SchedulerType.COSINE: get_cosine_schedule_with_warmup,
    SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup,
    SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup,
    SchedulerType.CONSTANT: get_constant_schedule,
    SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup,
    SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule,
}





def 		__SCREAMING_SNAKE_CASE					(  _UpperCamelCase						,		_UpperCamelCase						,		_UpperCamelCase = None						,		_UpperCamelCase = None						,		_UpperCamelCase = None						,		_UpperCamelCase = 1						,		_UpperCamelCase = 1.0						,		_UpperCamelCase = -1						,		):


   """simple docstring"""




   lowercase_	:    Any           =     SchedulerType(_UpperCamelCase							)
   lowercase_	:    List[Any]           =     TYPE_TO_SCHEDULER_FUNCTION[name]
   if name == SchedulerType.CONSTANT:
      return schedule_func(_UpperCamelCase						,		last_epoch=_UpperCamelCase							)

   if name == SchedulerType.PIECEWISE_CONSTANT:
      return schedule_func(_UpperCamelCase						,		step_rules=_UpperCamelCase						,		last_epoch=_UpperCamelCase							)

   # All other schedulers require `num_warmup_steps`
   if num_warmup_steps is None:
      raise ValueError(F"""{name} requires `num_warmup_steps`, please provide that argument."""							)

   if name == SchedulerType.CONSTANT_WITH_WARMUP:
      return schedule_func(_UpperCamelCase						,		num_warmup_steps=_UpperCamelCase						,		last_epoch=_UpperCamelCase							)

   # All other schedulers require `num_training_steps`
   if num_training_steps is None:
      raise ValueError(F"""{name} requires `num_training_steps`, please provide that argument."""							)

   if name == SchedulerType.COSINE_WITH_RESTARTS:
      return schedule_func(
          _UpperCamelCase						,		num_warmup_steps=_UpperCamelCase						,		num_training_steps=_UpperCamelCase						,		num_cycles=_UpperCamelCase						,		last_epoch=_UpperCamelCase						,		)

   if name == SchedulerType.POLYNOMIAL:
      return schedule_func(
          _UpperCamelCase						,		num_warmup_steps=_UpperCamelCase						,		num_training_steps=_UpperCamelCase						,		power=_UpperCamelCase						,		last_epoch=_UpperCamelCase						,		)

   return schedule_func(
       _UpperCamelCase						,		num_warmup_steps=_UpperCamelCase						,		num_training_steps=_UpperCamelCase						,		last_epoch=_UpperCamelCase							)




