






"""simple docstring"""



from manim import *

class 	a					(       a_     ):







    def       UpperCamelCase_				(      self	):
      lowercase							   =	Rectangle(height=0.5							,		width=0.5	)
      lowercase							   =	Rectangle(height=0.4_6							,		width=0.4_6	).set_stroke(width=0	)
      lowercase							   =	Rectangle(height=0.2_5							,		width=0.2_5	)

      lowercase							   =	[mem.copy() for i in range(6	)]
      lowercase							   =	[mem.copy() for i in range(6	)]
      lowercase							   =	VGroup(*_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	VGroup(*_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	VGroup(_lowerCamelCase							,		_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	Text('CPU'							,		font_size=2_4	)
      lowercase							   =	Group(_lowerCamelCase							,		_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0.5							,		aligned_edge=_lowerCamelCase	)
      cpu.move_to([-2.5, -0.5, 0]	)
      self.add(_lowerCamelCase	)

      lowercase							   =	[mem.copy() for i in range(4	)]
      lowercase							   =	VGroup(*_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	Text('GPU'							,		font_size=2_4	)
      lowercase							   =	Group(_lowerCamelCase							,		_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0.5							,		aligned_edge=_lowerCamelCase	)
      gpu.move_to([-1, -1, 0]	)
      self.add(_lowerCamelCase	)

      lowercase							   =	[mem.copy() for i in range(6	)]
      lowercase							   =	VGroup(*_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)

      lowercase							   =	Text('Model'							,		font_size=2_4	)
      lowercase							   =	Group(_lowerCamelCase							,		_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0.5							,		aligned_edge=_lowerCamelCase	)
      model.move_to([3, -1.0, 0]	)
      self.add(_lowerCamelCase	)

      lowercase							   =	[]
      lowercase							   =	[]

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

        lowercase							   =	Rectangle(height=0.4_6 / 4							,		width=0.4_6 / 3	).set_stroke(width=0.0	).set_fill(_lowerCamelCase							,		opacity=0.7	)

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

      self.add(*_lowerCamelCase							,		*_lowerCamelCase	)

      lowercase							   =	[meta_mem.copy() for i in range(6	)]
      lowercase							   =	[meta_mem.copy() for i in range(6	)]
      lowercase							   =	VGroup(*_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	VGroup(*_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	VGroup(_lowerCamelCase							,		_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0	)
      lowercase							   =	Text('Disk'							,		font_size=2_4	)
      lowercase							   =	Group(_lowerCamelCase							,		_lowerCamelCase	).arrange(_lowerCamelCase							,		buff=0.5							,		aligned_edge=_lowerCamelCase	)
      disk.move_to([-4.0, -1.2_5, 0]	)
      self.add(_lowerCamelCase							,		_lowerCamelCase	)

      lowercase							   =	[]

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

      self.add(*_lowerCamelCase	)

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

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

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

      self.add(_lowerCamelCase							,		_lowerCamelCase	)

      lowercase							   =	MarkupText(
          F'<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'							,		font_size=1_8							,		)

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

      lowercase							   =	MarkupText(F'The offloaded weights are all sent to the CPU.'							,		font_size=2_4	)
      step_a.move_to([2, 2, 0]	)

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

      for i in range(6	):
        lowercase							   =	cpu_disk_arr[i]
        lowercase							   =	rect.copy().set_fill(_lowerCamelCase							,		opacity=0.8	).scale(2.0	)
        cpa.generate_target()
        cpa.target.move_to(model_base[i]	)

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

          self.remove(
              *_lowerCamelCase							,		*_lowerCamelCase							,		)

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

      lowercase							   =	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=2_4							,		)
      step_a.move_to([2, 2, 0]	)

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

      lowercase							   =	[]
      lowercase							   =	[]
      for i in range(6	):
        lowercase							   =	Arrow(start=_lowerCamelCase							,		end=_lowerCamelCase							,		color=_lowerCamelCase							,		buff=0.5	)
        a.next_to(model_base[i].get_left()							,		_lowerCamelCase							,		buff=0.2	)
        arrows.append(_lowerCamelCase	)
        animations.append(Write(_lowerCamelCase	)	)
      self.play(*_lowerCamelCase	)
      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 ViTImageProcessor

class 	a					(       unittest.TestCase     ):







    def __init__(      self							,		_lowerCamelCase							,		_lowerCamelCase=7							,		_lowerCamelCase=3							,		_lowerCamelCase=1_8							,		_lowerCamelCase=3_0							,		_lowerCamelCase=4_0_0							,		_lowerCamelCase=True							,		_lowerCamelCase=None							,		_lowerCamelCase=True							,		_lowerCamelCase=[0.5, 0.5, 0.5]							,		_lowerCamelCase=[0.5, 0.5, 0.5]							,		):
      lowercase							   =	size if size is not None else {'height': 1_8, 'width': 1_8}
      lowercase							   =	parent
      lowercase							   =	batch_size
      lowercase							   =	num_channels
      lowercase							   =	image_size
      lowercase							   =	min_resolution
      lowercase							   =	max_resolution
      lowercase							   =	do_resize
      lowercase							   =	size
      lowercase							   =	do_normalize
      lowercase							   =	image_mean
      lowercase							   =	image_std







    def       UpperCamelCase_				(      self	):
      return {
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_normalize": self.do_normalize,
          "do_resize": self.do_resize,
          "size": self.size,
      }


@require_torch
@require_vision
class 	a					(       a_,    unittest.TestCase     ):
    UpperCAmelCase_			:					int     					=ViTImageProcessor if is_vision_available() else None







    def       UpperCamelCase_				(      self	):
      lowercase							   =	ViTImageProcessingTester(self	)







    @property
    def       UpperCamelCase_				(      self	):
      return self.image_processor_tester.prepare_image_processor_dict()







    def       UpperCamelCase_				(      self	):
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'image_mean'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'image_std'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'do_normalize'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'do_resize'	)	)
      self.assertTrue(hasattr(_lowerCamelCase							,		'size'	)	)







    def       UpperCamelCase_				(      self	):
      lowercase							   =	self.image_processing_class.from_dict(self.image_processor_dict	)
      self.assertEqual(image_processor.size							,		{'height': 1_8, 'width': 1_8}	)

      lowercase							   =	self.image_processing_class.from_dict(self.image_processor_dict							,		size=4_2	)
      self.assertEqual(image_processor.size							,		{'height': 4_2, 'width': 4_2}	)







    def       UpperCamelCase_				(      self	):
      pass







    def       UpperCamelCase_				(      self	):
      # Initialize image_processing
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      # create random PIL images
      lowercase							   =	prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase	)
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		Image.Image	)

      # Test not batched input
      lowercase							   =	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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							   =	image_processing(_lowerCamelCase							,		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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)







    def       UpperCamelCase_				(      self	):
      # Initialize image_processing
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      # create random numpy tensors
      lowercase							   =	prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase							,		numpify=_lowerCamelCase	)
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		np.ndarray	)

      # Test not batched input
      lowercase							   =	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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							   =	image_processing(_lowerCamelCase							,		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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)







    def       UpperCamelCase_				(      self	):
      # Initialize image_processing
      lowercase							   =	self.image_processing_class(**self.image_processor_dict	)
      # create random PyTorch tensors
      lowercase							   =	prepare_image_inputs(self.image_processor_tester							,		equal_resolution=_lowerCamelCase							,		torchify=_lowerCamelCase	)
      for image in image_inputs:
        self.assertIsInstance(_lowerCamelCase							,		torch.Tensor	)

      # Test not batched input
      lowercase							   =	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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)

      # Test batched
      lowercase							   =	image_processing(_lowerCamelCase							,		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.size['height'],
              self.image_processor_tester.size['width'],
          )							,		)


