



"""simple docstring"""







from manim import *

class 			_a     (   SCREAMING_SNAKE_CASE__):



			def 							__lowercase				( self       :							Any		)       ->				Union[str, Any]:
									snake_case      : Union[str, Any]							=   Rectangle(height=0.5      ,  width=0.5		)
									snake_case      : Any							=   Rectangle(height=0.46      ,  width=0.46		).set_stroke(width=0		)
									snake_case      : List[Any]							=   Rectangle(height=0.25      ,  width=0.25		)

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

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

									snake_case      : str							=   [mem.copy() for i in range(6		)]
									snake_case      : List[str]							=   VGroup(*_lowercase		).arrange(_lowercase      ,  buff=0		)

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

									snake_case      : List[str]							=   []
									snake_case      : int							=   []

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

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

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

									self.add(*_lowercase      ,  *_lowercase		)

									snake_case      : int							=   [meta_mem.copy() for i in range(6		)]
									snake_case      : List[str]							=   [meta_mem.copy() for i in range(6		)]
									snake_case      : List[Any]							=   VGroup(*_lowercase		).arrange(_lowercase      ,  buff=0		)
									snake_case      : List[Any]							=   VGroup(*_lowercase		).arrange(_lowercase      ,  buff=0		)
									snake_case      : Dict							=   VGroup(_lowercase      ,  _lowercase		).arrange(_lowercase      ,  buff=0		)
									snake_case      : Any							=   Text("Disk"      ,  font_size=24		)
									snake_case      : Optional[int]							=   Group(_lowercase      ,  _lowercase		).arrange(_lowercase      ,  buff=0.5      ,  aligned_edge=_lowercase		)
									disk.move_to([-4.0, -1.25, 0]		)
									self.add(_lowercase      ,  _lowercase		)

									snake_case      : List[Any]							=   []

									for i in range(6		):
															snake_case      : List[Any]							=   fill.copy().set_fill(_lowercase      ,  opacity=0.8		)
															target.move_to(disk_left_col_base[i]		).scale(0.5		)
															cpu_disk_arr.append(_lowercase		)

									self.add(*_lowercase		)

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

									snake_case      : 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(_lowercase      ,  _lowercase		)

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

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

									snake_case      : Union[str, Any]							=   MarkupText(F'''The offloaded weights are all sent to the CPU.'''      ,  font_size=24		)
									step_a.move_to([2, 2, 0]		)

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

									for i in range(6		):
															snake_case      : Dict							=   cpu_disk_arr[i]
															snake_case      : Optional[int]							=   rect.copy().set_fill(_lowercase      ,  opacity=0.8		).scale(2.0		)
															cpa.generate_target()
															cpa.target.move_to(model_base[i]		)

															if i == 0:
																					rect.set_fill(_lowercase      ,  opacity=0.8		)
																					rect.generate_target()
																					rect.target.move_to(cpu_left_col_base[0]		).scale(2.0		)

																					self.remove(
																					    *_lowercase      ,  *_lowercase      ,  )

															else:
																					rect.generate_target()
																					rect.target.move_to(cpu_left_col_base[i]		).scale(2.0		)
															self.play(MoveToTarget(_lowercase		)      ,  MoveToTarget(_lowercase		)      ,  model_base[i].animate.set_stroke(_lowercase		)		)
									self.play(FadeOut(_lowercase		)		)

									snake_case      : Optional[int]							=   MarkupText(
									    F'''Finally, hooks are added to each weight in the model\nto transfer the weights from CPU to GPU\n\t\tand back when needed.'''      ,  font_size=24      ,  )
									step_a.move_to([2, 2, 0]		)

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

									snake_case      : List[str]							=   []
									snake_case      : Union[str, Any]							=   []
									for i in range(6		):
															snake_case      : Any							=   Arrow(start=_lowercase      ,  end=_lowercase      ,  color=_lowercase      ,  buff=0.5		)
															a.next_to(model_base[i].get_left()      ,  _lowercase      ,  buff=0.2		)
															arrows.append(_lowercase		)
															animations.append(Write(_lowercase		)		)
									self.play(*_lowercase		)
									self.wait()








"""simple docstring"""







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
			import torch

if is_vision_available():
			from PIL import Image

			from transformers import OwlViTImageProcessor

class 			_a     (   unittest.TestCase):



			def __init__( self       :							Optional[int]      ,  _lowercase       :							int      ,  _lowercase       :							Union[str, Any]=7      ,  _lowercase       :							Union[str, Any]=3      ,  _lowercase       :							int=18      ,  _lowercase       :							Any=30      ,  _lowercase       :							str=400      ,  _lowercase       :							str=True      ,  _lowercase       :							Dict=None      ,  _lowercase       :							int=True      ,  _lowercase       :							Union[str, Any]=None      ,  _lowercase       :							str=True      ,  _lowercase       :							Union[str, Any]=[0.48145466, 0.4578275, 0.40821073]      ,  _lowercase       :							str=[0.26862954, 0.26130258, 0.27577711]      ,  _lowercase       :							Optional[int]=True      ,  )       ->				Optional[int]:
									snake_case      : str							=   parent
									snake_case      : Any							=   batch_size
									snake_case      : Optional[int]							=   num_channels
									snake_case      : Any							=   image_size
									snake_case      : Optional[Any]							=   min_resolution
									snake_case      : int							=   max_resolution
									snake_case      : List[Any]							=   do_resize
									snake_case      : Dict							=   size if size is not None else {"height": 18, "width": 18}
									snake_case      : str							=   do_center_crop
									snake_case      : List[str]							=   crop_size if crop_size is not None else {"height": 18, "width": 18}
									snake_case      : str							=   do_normalize
									snake_case      : str							=   image_mean
									snake_case      : Optional[Any]							=   image_std
									snake_case      : str							=   do_convert_rgb



			def 							__lowercase				( self       :							Tuple		)       ->				Union[str, Any]:
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_center_crop": self.do_center_crop,
									    "crop_size": self.crop_size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_convert_rgb": self.do_convert_rgb,
									}



@require_torch
@require_vision
class 			_a     (   SCREAMING_SNAKE_CASE__    ,  unittest.TestCase):
			__magic_name__    				=    OwlViTImageProcessor if is_vision_available() else None



			def 							__lowercase				( self       :							Any		)       ->				List[Any]:
									snake_case      : Dict							=   OwlViTImageProcessingTester(self		)



			@property
			def 							__lowercase				( self       :							Optional[int]		)       ->				List[Any]:
									return self.image_processor_tester.prepare_image_processor_dict()



			def 							__lowercase				( self       :							List[Any]		)       ->				Union[str, Any]:
									snake_case      : List[Any]							=   self.image_processing_class(**self.image_processor_dict		)
									self.assertTrue(hasattr(_lowercase      ,  "do_resize"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "size"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "do_center_crop"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "center_crop"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "do_normalize"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "image_mean"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "image_std"		)		)
									self.assertTrue(hasattr(_lowercase      ,  "do_convert_rgb"		)		)



			def 							__lowercase				( self       :							Any		)       ->				Any:
									snake_case      : List[Any]							=   self.image_processing_class.from_dict(self.image_processor_dict		)
									self.assertEqual(image_processor.size      ,  {"height": 18, "width": 18}		)
									self.assertEqual(image_processor.crop_size      ,  {"height": 18, "width": 18}		)

									snake_case      : int							=   self.image_processing_class.from_dict(self.image_processor_dict      ,  size=42      ,  crop_size=84		)
									self.assertEqual(image_processor.size      ,  {"height": 42, "width": 42}		)
									self.assertEqual(image_processor.crop_size      ,  {"height": 84, "width": 84}		)



			def 							__lowercase				( self       :							Any		)       ->				Optional[int]:
									# Initialize image_processing
									snake_case      : List[Any]							=   self.image_processing_class(**self.image_processor_dict		)
									# create random PIL images
									snake_case      : Tuple							=   prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=_lowercase		)

									for image in image_inputs:
															self.assertIsInstance(_lowercase      ,  Image.Image		)

									# Test not batched input
									snake_case      : Dict							=   image_processing(image_inputs[0]      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )

									# Test batched
									snake_case      : Optional[Any]							=   image_processing(_lowercase      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )



			def 							__lowercase				( self       :							Optional[Any]		)       ->				Dict:
									# Initialize image_processing
									snake_case      : Optional[int]							=   self.image_processing_class(**self.image_processor_dict		)
									# create random numpy tensors
									snake_case      : int							=   prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=_lowercase      ,  numpify=_lowercase		)
									for image in image_inputs:
															self.assertIsInstance(_lowercase      ,  np.ndarray		)

									# Test not batched input
									snake_case      : str							=   image_processing(image_inputs[0]      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )

									# Test batched
									snake_case      : str							=   image_processing(_lowercase      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )



			def 							__lowercase				( self       :							List[Any]		)       ->				List[str]:
									# Initialize image_processing
									snake_case      : Any							=   self.image_processing_class(**self.image_processor_dict		)
									# create random PyTorch tensors
									snake_case      : Dict							=   prepare_image_inputs(self.image_processor_tester      ,  equal_resolution=_lowercase      ,  torchify=_lowercase		)
									for image in image_inputs:
															self.assertIsInstance(_lowercase      ,  torch.Tensor		)

									# Test not batched input
									snake_case      : Union[str, Any]							=   image_processing(image_inputs[0]      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )

									# Test batched
									snake_case      : Any							=   image_processing(_lowercase      ,  return_tensors="pt"		).pixel_values
									self.assertEqual(
									    encoded_images.shape      ,  (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )      ,  )



