




"""simple docstring"""





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
					from PIL import Image

					from transformers import AutoProcessor, BertTokenizer, BlipImageProcessor, BlipProcessor, PreTrainedTokenizerFast



@require_vision
class      a__				(	unittest.TestCase   ):


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Any		=					tempfile.mkdtemp()

						__UpperCAmelCase	: Optional[Any]		=					BlipImageProcessor()
						__UpperCAmelCase	: int		=					BertTokenizer.from_pretrained("hf-internal-testing/tiny-random-BertModel")

						__UpperCAmelCase	: int		=					BlipProcessor(UpperCamelCase_      ,  UpperCamelCase_)

						processor.save_pretrained(self.tmpdirname)


			def       a_     (      self      :    List[str]      ,  **UpperCamelCase_      :    int):


						"""simple docstring"""




						return AutoProcessor.from_pretrained(self.tmpdirname      ,  **UpperCamelCase_).tokenizer


			def       a_     (      self      :    Optional[int]      ,  **UpperCamelCase_      :    List[Any]):


						"""simple docstring"""




						return AutoProcessor.from_pretrained(self.tmpdirname      ,  **UpperCamelCase_).image_processor


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						shutil.rmtree(self.tmpdirname)


			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					[np.random.randint(255      ,  size=(3, 30, 400)      ,  dtype=np.uinta)]

						__UpperCAmelCase	: Dict		=					[Image.fromarray(np.moveaxis(UpperCamelCase_      ,  0      ,  -1)) for x in image_inputs]

						return image_inputs


			def       a_     (      self      :    int):


						"""simple docstring"""




						__UpperCAmelCase	: str		=					BlipProcessor(tokenizer=self.get_tokenizer()      ,  image_processor=self.get_image_processor())
						processor.save_pretrained(self.tmpdirname)

						__UpperCAmelCase	: str		=					self.get_tokenizer(bos_token="(BOS)"      ,  eos_token="(EOS)")
						__UpperCAmelCase	: Any		=					self.get_image_processor(do_normalize=UpperCamelCase_      ,  padding_value=1.0)

						__UpperCAmelCase	: List[Any]		=					BlipProcessor.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       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.get_image_processor()
						__UpperCAmelCase	: Union[str, Any]		=					self.get_tokenizer()

						__UpperCAmelCase	: Optional[Any]		=					BlipProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					self.prepare_image_inputs()

						__UpperCAmelCase	: Optional[Any]		=					image_processor(UpperCamelCase_      ,  return_tensors="np")
						__UpperCAmelCase	: Union[str, Any]		=					processor(images=UpperCamelCase_      ,  return_tensors="np")

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


			def       a_     (      self      :    List[str]):


						"""simple docstring"""




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

						__UpperCAmelCase	: Dict		=					BlipProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					"lower newer"

						__UpperCAmelCase	: str		=					processor(text=UpperCamelCase_)

						__UpperCAmelCase	: Tuple		=					tokenizer(UpperCamelCase_      ,  return_token_type_ids=UpperCamelCase_)

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


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					self.get_image_processor()
						__UpperCAmelCase	: Tuple		=					self.get_tokenizer()

						__UpperCAmelCase	: Optional[int]		=					BlipProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					"lower newer"
						__UpperCAmelCase	: Optional[Any]		=					self.prepare_image_inputs()

						__UpperCAmelCase	: int		=					processor(text=UpperCamelCase_      ,  images=UpperCamelCase_)

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

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


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					self.get_image_processor()
						__UpperCAmelCase	: Union[str, Any]		=					self.get_tokenizer()

						__UpperCAmelCase	: Dict		=					BlipProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: Dict		=					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

						__UpperCAmelCase	: Union[str, Any]		=					processor.batch_decode(UpperCamelCase_)
						__UpperCAmelCase	: Tuple		=					tokenizer.batch_decode(UpperCamelCase_)

						self.assertListEqual(UpperCamelCase_      ,  UpperCamelCase_)


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




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

						__UpperCAmelCase	: Optional[int]		=					BlipProcessor(tokenizer=UpperCamelCase_      ,  image_processor=UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					"lower newer"
						__UpperCAmelCase	: Optional[Any]		=					self.prepare_image_inputs()

						__UpperCAmelCase	: Dict		=					processor(text=UpperCamelCase_      ,  images=UpperCamelCase_)

						# For now the processor supports only ['pixel_values', 'input_ids', 'attention_mask']
						self.assertListEqual(list(inputs.keys())      ,  ["pixel_values", "input_ids", "attention_mask"])








"""simple docstring"""





from manim import *



class      a__				(	__magic_name__   ):


			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					Rectangle(height=0.5      ,  width=0.5)
						__UpperCAmelCase	: List[str]		=					Rectangle(height=0.25      ,  width=0.25)
						__UpperCAmelCase	: Dict		=					Rectangle(height=0.46      ,  width=0.46).set_stroke(width=0)

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

						__UpperCAmelCase	: List[str]		=					[mem.copy() for i in range(4)]
						__UpperCAmelCase	: List[Any]		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)
						__UpperCAmelCase	: Tuple		=					Text("GPU"      ,  font_size=24)
						__UpperCAmelCase	: Tuple		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						gpu.move_to([-1, -1, 0])
						self.add(UpperCamelCase_)

						__UpperCAmelCase	: Any		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: int		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)

						__UpperCAmelCase	: Union[str, Any]		=					Text("Model"      ,  font_size=24)
						__UpperCAmelCase	: Optional[Any]		=					Group(UpperCamelCase_      ,  UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0.5      ,  aligned_edge=UpperCamelCase_)
						model.move_to([3, -1.0, 0])
						self.add(UpperCamelCase_)

						__UpperCAmelCase	: str		=					[]
						__UpperCAmelCase	: Tuple		=					[]
						__UpperCAmelCase	: Any		=					[]

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

									__UpperCAmelCase	: Optional[int]		=					Rectangle(height=0.46 / 4      ,  width=0.46 / 3).set_stroke(width=0.0).set_fill(UpperCamelCase_      ,  opacity=0.7)

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

						self.add(*UpperCamelCase_      ,  *UpperCamelCase_      ,  *UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					[mem.copy() for i in range(6)]
						__UpperCAmelCase	: Dict		=					VGroup(*UpperCamelCase_).arrange(UpperCamelCase_      ,  buff=0)

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

						self.add(UpperCamelCase_)

						__UpperCAmelCase	: Optional[int]		=					[]
						__UpperCAmelCase	: int		=					[]

						for i, rect in enumerate(UpperCamelCase_):
									__UpperCAmelCase	: Optional[Any]		=					fill.copy().set_fill(UpperCamelCase_      ,  opacity=0.7)
									target.move_to(UpperCamelCase_)
									ckpt_arr.append(UpperCamelCase_)

									__UpperCAmelCase	: Optional[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(UpperCamelCase_)
						self.add(*UpperCamelCase_      ,  *UpperCamelCase_)

						__UpperCAmelCase	: str		=					Square(side_length=2.2)
						key.move_to([-5, 2, 0])

						__UpperCAmelCase	: int		=					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(UpperCamelCase_      ,  UpperCamelCase_)

						__UpperCAmelCase	: Optional[Any]		=					MarkupText(
						    F"<span fgcolor='{BLUE}'>●</span> Checkpoint"      ,  font_size=18      ,  )

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

						__UpperCAmelCase	: Dict		=					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])

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

						__UpperCAmelCase	: str		=					[]
						for i, rect in enumerate(UpperCamelCase_):
									__UpperCAmelCase	: Union[str, Any]		=					rect.copy()
									target.generate_target()
									target.target.move_to(disk_left_col_base[i]).scale(0.5)
									animations.append(MoveToTarget(UpperCamelCase_      ,  run_time=1.5))
						self.play(*UpperCamelCase_)

						self.play(FadeOut(UpperCamelCase_))

						__UpperCAmelCase	: Union[str, 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(UpperCamelCase_      ,  run_time=3))

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

						self.wait()


