


'''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 		lowerCAmelCase__	(	unittest.TestCase		):




			def 							lowerCAmelCase__			(						self     :   str       )      ->str:



					'''simple docstring'''







					_UpperCAmelCase						:				str	    =					tempfile.mkdtemp()

					# fmt: off
					_UpperCAmelCase						:				Any	    =					['''''', '''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						:				List[str]	    =					dict(zip(A_		,     range(len(A_       )       )       )       )
					_UpperCAmelCase						:				Tuple	    =					['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''', '''''']
					_UpperCAmelCase						:				Dict	    =					{'''unk_token''': '''<unk>'''}

					_UpperCAmelCase						:				Any	    =					os.path.join(self.tmpdirname		,     VOCAB_FILES_NAMES["vocab_file"]       )
					_UpperCAmelCase						:				Optional[int]	    =					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_       )       )

					_UpperCAmelCase						:				str	    =					{
					    '''do_resize''': True,
					    '''size''': 20,
					    '''do_center_crop''': True,
					    '''crop_size''': 18,
					    '''do_normalize''': True,
					    '''image_mean''': [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],
					    '''image_std''': [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],
					}
					_UpperCAmelCase						:				Optional[int]	    =					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     :   Optional[int]		,     **lowerCamelCase__     :   Optional[Any]       )      ->Union[str, Any]:



					'''simple docstring'''







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




			def 							lowerCAmelCase__			(						self     :   List[Any]		,     **lowerCamelCase__     :   List[Any]       )      ->Union[str, Any]:



					'''simple docstring'''







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




			def 							lowerCAmelCase__			(						self     :   Optional[Any]		,     **lowerCamelCase__     :   Any       )      ->str:



					'''simple docstring'''







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




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



					'''simple docstring'''







					shutil.rmtree(self.tmpdirname       )




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



					'''simple docstring'''







					_UpperCAmelCase						:				str	    =					[np.random.randint(2_55		,     size=(3, 30, 4_00)		,     dtype=np.uinta       )]

					_UpperCAmelCase						:				Optional[Any]	    =					[Image.fromarray(np.moveaxis(A_		,     0		,     -1       )       ) for x in image_inputs]

					return image_inputs




			def 							lowerCAmelCase__			(						self     :   Tuple       )      ->Dict:



					'''simple docstring'''







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

					_UpperCAmelCase						:				Tuple	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )
					processor_slow.save_pretrained(self.tmpdirname       )
					_UpperCAmelCase						:				Optional[int]	    =					OwlViTProcessor.from_pretrained(self.tmpdirname		,     use_fast=A_       )

					_UpperCAmelCase						:				Union[str, Any]	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )
					processor_fast.save_pretrained(self.tmpdirname       )
					_UpperCAmelCase						:				Union[str, Any]	    =					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     :   Union[str, Any]       )      ->Optional[Any]:



					'''simple docstring'''







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

					_UpperCAmelCase						:				Any	    =					self.get_tokenizer(bos_token="(BOS)"		,     eos_token="(EOS)"       )
					_UpperCAmelCase						:				Union[str, Any]	    =					self.get_image_processor(do_normalize=A_       )

					_UpperCAmelCase						:				Optional[Any]	    =					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       )      ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase						:				Dict	    =					self.get_image_processor()
					_UpperCAmelCase						:				List[Any]	    =					self.get_tokenizer()

					_UpperCAmelCase						:				Optional[Any]	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )

					_UpperCAmelCase						:				Tuple	    =					self.prepare_image_inputs()

					_UpperCAmelCase						:				Optional[Any]	    =					image_processor(A_		,     return_tensors="np"       )
					_UpperCAmelCase						:				Union[str, 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     :   Union[str, Any]       )      ->Any:



					'''simple docstring'''







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

					_UpperCAmelCase						:				Optional[int]	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )

					_UpperCAmelCase						:				Tuple	    =					'''lower newer'''

					_UpperCAmelCase						:				List[str]	    =					processor(text=A_		,     return_tensors="np"       )

					_UpperCAmelCase						:				List[str]	    =					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       )      ->Tuple:



					'''simple docstring'''







					_UpperCAmelCase						:				str	    =					self.get_image_processor()
					_UpperCAmelCase						:				Any	    =					self.get_tokenizer()

					_UpperCAmelCase						:				List[str]	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )

					_UpperCAmelCase						:				Tuple	    =					'''lower newer'''
					_UpperCAmelCase						:				str	    =					self.prepare_image_inputs()

					_UpperCAmelCase						:				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       )      ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				Union[str, Any]	    =					'''google/owlvit-base-patch32'''
					_UpperCAmelCase						:				Any	    =					OwlViTProcessor.from_pretrained(A_       )

					_UpperCAmelCase						:				List[str]	    =					['''cat''', '''nasa badge''']
					_UpperCAmelCase						:				str	    =					processor(text=A_       )

					_UpperCAmelCase						:				Optional[Any]	    =					16
					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     :   List[str]       )      ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				Union[str, Any]	    =					'''google/owlvit-base-patch32'''
					_UpperCAmelCase						:				Optional[Any]	    =					OwlViTProcessor.from_pretrained(A_       )

					_UpperCAmelCase						:				Dict	    =					[['''cat''', '''nasa badge'''], ['''person''']]
					_UpperCAmelCase						:				List[Any]	    =					processor(text=A_       )

					_UpperCAmelCase						:				List[Any]	    =					16
					_UpperCAmelCase						:				List[str]	    =					len(A_       )
					_UpperCAmelCase						:				Dict	    =					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     :   List[str]       )      ->List[str]:



					'''simple docstring'''







					_UpperCAmelCase						:				List[str]	    =					'''google/owlvit-base-patch32'''
					_UpperCAmelCase						:				List[Any]	    =					OwlViTProcessor.from_pretrained(A_       )

					_UpperCAmelCase						:				Tuple	    =					['''cat''', '''nasa badge''']
					_UpperCAmelCase						:				List[str]	    =					processor(text=A_       )

					_UpperCAmelCase						:				Dict	    =					16
					_UpperCAmelCase						:				Optional[int]	    =					inputs['''input_ids''']
					_UpperCAmelCase						:				List[Any]	    =					[
					    [4_94_06, 23_68, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
					    [4_94_06, 68_41, 1_13_01, 4_94_07, 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     :   Any       )      ->List[Any]:



					'''simple docstring'''







					_UpperCAmelCase						:				Optional[Any]	    =					self.get_image_processor()
					_UpperCAmelCase						:				str	    =					self.get_tokenizer()

					_UpperCAmelCase						:				Optional[Any]	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )

					_UpperCAmelCase						:				List[str]	    =					self.prepare_image_inputs()
					_UpperCAmelCase						:				Union[str, Any]	    =					self.prepare_image_inputs()

					_UpperCAmelCase						:				Optional[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     :   Union[str, Any]       )      ->Optional[int]:



					'''simple docstring'''







					_UpperCAmelCase						:				int	    =					self.get_image_processor()
					_UpperCAmelCase						:				Tuple	    =					self.get_tokenizer()

					_UpperCAmelCase						:				str	    =					OwlViTProcessor(tokenizer=A_		,     image_processor=A_       )

					_UpperCAmelCase						:				Union[str, Any]	    =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

					_UpperCAmelCase						:				Any	    =					processor.batch_decode(A_       )
					_UpperCAmelCase						:				Dict	    =					tokenizer.batch_decode(A_       )

					self.assertListEqual(A_		,     A_       )






import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)







class   __lowercase      (unittest.TestCase ):
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					Vector([1, 2, 3]					)
       self.assertEqual(x.component(0					)      ,							1					)
       self.assertEqual(x.component(2					)      ,							3					)
       __lowerCAmelCase					:						Dict					=					Vector()
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Vector([0, 0, 0, 0, 0, 1]					)
       self.assertEqual(str(A_					)      ,							'''(0,0,0,0,0,1)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					Vector([1, 2, 3, 4]					)
       self.assertEqual(len(A_					)      ,							4					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					Vector([1, 2]					)
       __lowerCAmelCase					:						Optional[int]					=					Vector([1, 2, 3, 4, 5]					)
       __lowerCAmelCase					:						Optional[Any]					=					Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]					)
       __lowerCAmelCase					:						str					=					Vector([1, -1, 1, -1, 2, -3, 4, -5]					)
       self.assertAlmostEqual(x.euclidean_length()      ,							2.236      ,							3					)
       self.assertAlmostEqual(y.euclidean_length()      ,							7.416      ,							3					)
       self.assertEqual(z.euclidean_length()      ,							0					)
       self.assertAlmostEqual(w.euclidean_length()      ,							7.616      ,							3					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						List[str]					=					Vector([1, 1, 1]					)
       self.assertEqual((x + y).component(0					)      ,							2					)
       self.assertEqual((x + y).component(1					)      ,							3					)
       self.assertEqual((x + y).component(2					)      ,							4					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						List[str]					=					Vector([1, 1, 1]					)
       self.assertEqual((x - y).component(0					)      ,							0					)
       self.assertEqual((x - y).component(1					)      ,							1					)
       self.assertEqual((x - y).component(2					)      ,							2					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						List[Any]					=					Vector([2, -1, 4]					)  # for test of dot product
       __lowerCAmelCase					:						Optional[int]					=					Vector([1, -2, -1]					)
       self.assertEqual(str(x * 3.0					)      ,							'''(3.0,6.0,9.0)'''					)
       self.assertEqual((a * b)      ,							0					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       self.assertEqual(str(zero_vector(10					)					).count('''0'''					)      ,							10					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       self.assertEqual(str(unit_basis_vector(3      ,							1					)					)      ,							'''(0,1,0)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						str					=					Vector([1, 2, 3]					)
       __lowerCAmelCase					:						Any					=					Vector([1, 0, 1]					)
       self.assertEqual(str(axpy(2      ,							A_      ,							A_					)					)      ,							'''(3,4,7)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Vector([1, 0, 0, 0, 0, 0]					)
       __lowerCAmelCase					:						Optional[Any]					=					x.copy()
       self.assertEqual(str(A_					)      ,							str(A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						List[str]					=					Vector([1, 0, 0]					)
       x.change_component(0      ,							0					)
       x.change_component(1      ,							1					)
       self.assertEqual(str(A_					)      ,							'''(0,1,0)'''					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Union[str, Any]					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       self.assertEqual('''|1,2,3|\n|2,4,5|\n|6,7,8|\n'''      ,							str(A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						List[Any]					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						str					=					[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
       for x in range(a.height()					):
             for y in range(a.width()					):
                   self.assertEqual(minors[x][y]      ,							a.minor(A_      ,							A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Optional[int]					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						Tuple					=					[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
       for x in range(a.height()					):
             for y in range(a.width()					):
                   self.assertEqual(cofactors[x][y]      ,							a.cofactor(A_      ,							A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       self.assertEqual(-5      ,							a.determinant()					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]      ,							3      ,							3					)
       __lowerCAmelCase					:						Union[str, Any]					=					Vector([1, 2, 3]					)
       self.assertEqual('''(14,32,50)'''      ,							str(a * x					)					)
       self.assertEqual('''|2,4,6|\n|8,10,12|\n|14,16,18|\n'''      ,							str(a * 2					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       a.change_component(0      ,							2      ,							5					)
       self.assertEqual('''|1,2,5|\n|2,4,5|\n|6,7,8|\n'''      ,							str(A_					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Tuple					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       self.assertEqual(7      ,							a.component(2      ,							1					)      ,							0.01					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Any					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						Dict					=					Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]      ,							3      ,							3					)
       self.assertEqual('''|2,4,10|\n|4,8,10|\n|12,14,18|\n'''      ,							str(a + b					)					)
 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       __lowerCAmelCase					:						Dict					=					Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]      ,							3      ,							3					)
       __lowerCAmelCase					:						str					=					Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]      ,							3      ,							3					)
       self.assertEqual('''|0,0,-4|\n|0,0,0|\n|0,0,-2|\n'''      ,							str(a - b					)					)





 def 	UpperCamelCase__  (							self					)					->None:



       '''simple docstring'''





       self.assertEqual(
           '''|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n'''      ,							str(square_zero_matrix(5					)					)      ,							)


if __name__ == "__main__":
 unittest.main()


