


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





					def  SCREAMING_SNAKE_CASE__       (    self					:				Any							):
								__lowerCamelCase:				Optional[Any]     =	tempfile.mkdtemp()

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

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

								__lowerCamelCase:				Any     =	{
								    '''do_resize''': True,
								    '''size''': 20,
								    '''do_center_crop''': True,
								    '''crop_size''': 18,
								    '''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],
								}
								__lowerCamelCase:				Union[str, Any]     =	os.path.join(self.tmpdirname    ,	_UpperCAmelCase							)
								with open(self.image_processor_file    ,	"""w"""    ,	encoding="""utf-8"""							) as fp:
											json.dump(_UpperCAmelCase    ,	_UpperCAmelCase							)





					def  SCREAMING_SNAKE_CASE__       (    self					:				List[str]    ,	**SCREAMING_SNAKE_CASE_					:				Any							):
								return CLIPTokenizer.from_pretrained(self.tmpdirname    ,	**_UpperCAmelCase							)





					def  SCREAMING_SNAKE_CASE__       (    self					:				Union[str, Any]    ,	**SCREAMING_SNAKE_CASE_					:				List[Any]							):
								return CLIPTokenizerFast.from_pretrained(self.tmpdirname    ,	**_UpperCAmelCase							)





					def  SCREAMING_SNAKE_CASE__       (    self					:				List[str]    ,	**SCREAMING_SNAKE_CASE_					:				Tuple							):
								return CLIPImageProcessor.from_pretrained(self.tmpdirname    ,	**_UpperCAmelCase							)





					def  SCREAMING_SNAKE_CASE__       (    self					:				str							):
								shutil.rmtree(self.tmpdirname							)





					def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]							):
								__lowerCamelCase:				Tuple     =	[np.random.randint(255    ,	size=(3, 30, 400)    ,	dtype=np.uinta							)]

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

								return image_inputs





					def  SCREAMING_SNAKE_CASE__       (    self					:				Optional[int]							):
								__lowerCamelCase:				str     =	self.get_tokenizer()
								__lowerCamelCase:				Any     =	self.get_rust_tokenizer()
								__lowerCamelCase:				Union[str, Any]     =	self.get_image_processor()

								__lowerCamelCase:				Any     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)
								processor_slow.save_pretrained(self.tmpdirname							)
								__lowerCamelCase:				List[str]     =	CLIPProcessor.from_pretrained(self.tmpdirname    ,	use_fast=_UpperCAmelCase							)

								__lowerCamelCase:				int     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)
								processor_fast.save_pretrained(self.tmpdirname							)
								__lowerCamelCase:				Dict     =	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    ,	_UpperCAmelCase							)
								self.assertIsInstance(processor_fast.tokenizer    ,	_UpperCAmelCase							)

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





					def  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]							):
								__lowerCamelCase:				Optional[int]     =	CLIPProcessor(tokenizer=self.get_tokenizer()    ,	image_processor=self.get_image_processor()							)
								processor.save_pretrained(self.tmpdirname							)

								__lowerCamelCase:				int     =	self.get_tokenizer(bos_token="""(BOS)"""    ,	eos_token="""(EOS)"""							)
								__lowerCamelCase:				Optional[Any]     =	self.get_image_processor(do_normalize=_UpperCAmelCase    ,	padding_value=1.0							)

								__lowerCamelCase:				Dict     =	CLIPProcessor.from_pretrained(
								    self.tmpdirname    ,	bos_token="""(BOS)"""    ,	eos_token="""(EOS)"""    ,	do_normalize=_UpperCAmelCase    ,	padding_value=1.0							)

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

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





					def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple							):
								__lowerCamelCase:				Any     =	self.get_image_processor()
								__lowerCamelCase:				Tuple     =	self.get_tokenizer()

								__lowerCamelCase:				Optional[Any]     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)

								__lowerCamelCase:				Optional[int]     =	self.prepare_image_inputs()

								__lowerCamelCase:				int     =	image_processor(_UpperCAmelCase    ,	return_tensors="""np"""							)
								__lowerCamelCase:				Dict     =	processor(images=_UpperCAmelCase    ,	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  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]							):
								__lowerCamelCase:				Union[str, Any]     =	self.get_image_processor()
								__lowerCamelCase:				List[str]     =	self.get_tokenizer()

								__lowerCamelCase:				Optional[int]     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)

								__lowerCamelCase:				Optional[Any]     =	'''lower newer'''

								__lowerCamelCase:				List[Any]     =	processor(text=_UpperCAmelCase							)

								__lowerCamelCase:				Optional[Any]     =	tokenizer(_UpperCAmelCase							)

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





					def  SCREAMING_SNAKE_CASE__       (    self					:				Tuple							):
								__lowerCamelCase:				Optional[int]     =	self.get_image_processor()
								__lowerCamelCase:				Tuple     =	self.get_tokenizer()

								__lowerCamelCase:				Dict     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)

								__lowerCamelCase:				int     =	'''lower newer'''
								__lowerCamelCase:				List[Any]     =	self.prepare_image_inputs()

								__lowerCamelCase:				Union[str, Any]     =	processor(text=_UpperCAmelCase    ,	images=_UpperCAmelCase							)

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

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





					def  SCREAMING_SNAKE_CASE__       (    self					:				Any							):
								__lowerCamelCase:				Union[str, Any]     =	self.get_image_processor()
								__lowerCamelCase:				str     =	self.get_tokenizer()

								__lowerCamelCase:				Optional[int]     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)

								__lowerCamelCase:				Optional[Any]     =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

								__lowerCamelCase:				int     =	processor.batch_decode(_UpperCAmelCase							)
								__lowerCamelCase:				Dict     =	tokenizer.batch_decode(_UpperCAmelCase							)

								self.assertListEqual(_UpperCAmelCase    ,	_UpperCAmelCase							)







					def  SCREAMING_SNAKE_CASE__       (    self					:				List[Any]							):
								__lowerCamelCase:				Tuple     =	self.get_image_processor()
								__lowerCamelCase:				Optional[int]     =	self.get_tokenizer()

								__lowerCamelCase:				Any     =	CLIPProcessor(tokenizer=_UpperCAmelCase    ,	image_processor=_UpperCAmelCase							)

								__lowerCamelCase:				Union[str, Any]     =	'''lower newer'''
								__lowerCamelCase:				Tuple     =	self.prepare_image_inputs()

								__lowerCamelCase:				Dict     =	processor(text=_UpperCAmelCase    ,	images=_UpperCAmelCase							)

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






from manim import *



class        a					(			_UpperCAmelCase   ):





     def  SCREAMING_SNAKE_CASE__       (    self					:				int							):
        __lowerCamelCase:				int     =	Rectangle(height=0.5    ,	width=0.5							)
        __lowerCamelCase:				List[str]     =	Rectangle(height=0.25    ,	width=0.25							)
        __lowerCamelCase:				Optional[int]     =	Rectangle(height=0.46    ,	width=0.46							).set_stroke(width=0							)

        __lowerCamelCase:				str     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Union[str, Any]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Tuple     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""CPU"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        cpu.move_to([-2.5, -0.5, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	[mem.copy() for i in range(4							)]
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""GPU"""    ,	font_size=24							)
        __lowerCamelCase:				Dict     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        gpu.move_to([-1, -1, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Any     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Dict     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				List[Any]     =	Text("""Model"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        model.move_to([3, -1.0, 0]							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	[]
        __lowerCamelCase:				Any     =	[]
        __lowerCamelCase:				int     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           rect.set_stroke(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				int     =	Rectangle(height=0.46 / 4    ,	width=0.46 / 3							).set_stroke(width=0.0							).set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)

           if i == 0:
              cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)    ,	buff=0.02    ,	direction=SCREAMING_SNAKE_CASE_							)
              cpu_target.set_x(cpu_target.get_x() + 0.1							)
           elif i == 3:
              cpu_target.next_to(model_cpu_arr[0]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           else:
              cpu_target.next_to(model_cpu_arr[i - 1]    ,	direction=SCREAMING_SNAKE_CASE_    ,	buff=0.0							)
           self.add(SCREAMING_SNAKE_CASE_							)
           model_cpu_arr.append(SCREAMING_SNAKE_CASE_							)

        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[mem.copy() for i in range(6							)]
        __lowerCamelCase:				Any     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)

        __lowerCamelCase:				Tuple     =	Text("""Loaded Checkpoint"""    ,	font_size=24							)
        __lowerCamelCase:				Tuple     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        checkpoint.move_to([3, 0.5, 0]							)

        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				List[Any]     =	[]
        __lowerCamelCase:				Optional[int]     =	[]

        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	fill.copy().set_fill(SCREAMING_SNAKE_CASE_    ,	opacity=0.7							)
           target.move_to(SCREAMING_SNAKE_CASE_							)
           ckpt_arr.append(SCREAMING_SNAKE_CASE_							)

           __lowerCamelCase:				List[Any]     =	target.copy()
           if i < 5:
              cpu_target.move_to(cpu_left_col_base[i + 1]							)
           else:
              cpu_target.move_to(cpu_right_col_base[i - 5]							)
           ckpt_cpu_arr.append(SCREAMING_SNAKE_CASE_							)
        self.add(*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[Any]     =	Square(side_length=2.2							)
        key.move_to([-5, 2, 0]							)

        __lowerCamelCase:				Dict     =	MarkupText(
            F'<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'    ,	font_size=18    ,	)

        key_text.move_to([-5, 2.4, 0]							)

        self.add(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Optional[int]     =	MarkupText(
            F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'    ,	font_size=18    ,	)

        blue_text.next_to(SCREAMING_SNAKE_CASE_    ,	DOWN * 2.4    ,	aligned_edge=key_text.get_left()							)
        self.add(SCREAMING_SNAKE_CASE_							)

        __lowerCamelCase:				Tuple     =	MarkupText(
            F'Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'    ,	font_size=24    ,	)
        step_a.move_to([2, 2, 0]							)

        __lowerCamelCase:				List[Any]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				Optional[int]     =	[meta_mem.copy() for i in range(6							)]
        __lowerCamelCase:				str     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Optional[int]     =	VGroup(*SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				List[str]     =	VGroup(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0							)
        __lowerCamelCase:				Dict     =	Text("""Disk"""    ,	font_size=24							)
        __lowerCamelCase:				Any     =	Group(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_							).arrange(SCREAMING_SNAKE_CASE_    ,	buff=0.5    ,	aligned_edge=SCREAMING_SNAKE_CASE_							)
        disk.move_to([-4.0, -1.25, 0]							)
        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)    ,	Write(SCREAMING_SNAKE_CASE_    ,	run_time=1							)    ,	Create(SCREAMING_SNAKE_CASE_    ,	run_time=1							)							)

        __lowerCamelCase:				int     =	[]
        for i, rect in enumerate(SCREAMING_SNAKE_CASE_							):
           __lowerCamelCase:				Optional[int]     =	rect.copy()
           target.generate_target()
           target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
           animations.append(MoveToTarget(SCREAMING_SNAKE_CASE_    ,	run_time=1.5							)							)
        self.play(*SCREAMING_SNAKE_CASE_							)

        self.play(FadeOut(SCREAMING_SNAKE_CASE_							)							)

        __lowerCamelCase:				List[Any]     =	MarkupText(F'Then, the checkpoint is removed from memory\nthrough garbage collection.'    ,	font_size=24							)
        step_a.move_to([2, 2, 0]							)

        self.play(Write(SCREAMING_SNAKE_CASE_    ,	run_time=3							)							)

        self.play(
            FadeOut(SCREAMING_SNAKE_CASE_    ,	SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_    ,	*SCREAMING_SNAKE_CASE_							)    ,	)

        self.wait()


