



'''simple docstring'''
from manim import *






class 				lowercase__      (				lowercase  ):





	def       UpperCamelCase_    (					self   :							str					):







							'''simple docstring'''
							_UpperCamelCase   :						Any												=  Rectangle(height=0.5					,width=0.5					)
							_UpperCamelCase   :						List[Any]												=  Rectangle(height=0.4_6					,width=0.4_6					).set_stroke(width=0					)

							_UpperCamelCase   :						str												=  Rectangle(height=0.2_5					,width=0.2_5					)

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

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

							_UpperCamelCase   :						str												=  [mem.copy() for i in range(6					)]
							_UpperCamelCase   :						Optional[int]												=  VGroup(*lowerCamelCase__					).arrange(lowerCamelCase__					,buff=0					)

							_UpperCamelCase   :						Tuple												=  Text('Model'					,font_size=24					)
							_UpperCamelCase   :						List[Any]												=  Group(lowerCamelCase__					,lowerCamelCase__					).arrange(lowerCamelCase__					,buff=0.5					,aligned_edge=lowerCamelCase__					)
							model.move_to([3, -1.0, 0]					)
							self.add(lowerCamelCase__					)

							_UpperCamelCase   :						Optional[Any]												=  []
							_UpperCamelCase   :						Any												=  []

							for i, rect in enumerate(lowerCamelCase__					):
													_UpperCamelCase   :						List[str]												=  fill.copy().set_fill(lowerCamelCase__					,opacity=0.8					)
													target.move_to(lowerCamelCase__					)
													model_arr.append(lowerCamelCase__					)

													_UpperCamelCase   :						Union[str, Any]												=  Rectangle(height=0.4_6					,width=0.4_6					).set_stroke(width=0.0					).set_fill(lowerCamelCase__					,opacity=0.8					)
													cpu_target.move_to(cpu_left_col_base[i]					)
													model_cpu_arr.append(lowerCamelCase__					)

							self.add(*lowerCamelCase__					,*lowerCamelCase__					)

							_UpperCamelCase   :						Optional[int]												=  [meta_mem.copy() for i in range(6					)]
							_UpperCamelCase   :						List[str]												=  [meta_mem.copy() for i in range(6					)]
							_UpperCamelCase   :						Optional[int]												=  VGroup(*lowerCamelCase__					).arrange(lowerCamelCase__					,buff=0					)
							_UpperCamelCase   :						int												=  VGroup(*lowerCamelCase__					).arrange(lowerCamelCase__					,buff=0					)
							_UpperCamelCase   :						Any												=  VGroup(lowerCamelCase__					,lowerCamelCase__					).arrange(lowerCamelCase__					,buff=0					)
							_UpperCamelCase   :						int												=  Text('Disk'					,font_size=24					)
							_UpperCamelCase   :						List[str]												=  Group(lowerCamelCase__					,lowerCamelCase__					).arrange(lowerCamelCase__					,buff=0.5					,aligned_edge=lowerCamelCase__					)
							disk.move_to([-4, -1.2_5, 0]					)
							self.add(lowerCamelCase__					,lowerCamelCase__					)

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

							_UpperCamelCase   :						List[str]												=  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(lowerCamelCase__					,lowerCamelCase__					)

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

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

							_UpperCamelCase   :						Dict												=  MarkupText(
							    F'Now watch as an input is passed through the model\nand how the memory is utilized and handled.'					,font_size=24					,)
							step_a.move_to([2, 2, 0]					)

							self.play(Write(lowerCamelCase__					)					)

							_UpperCamelCase   :						List[Any]												=  Square(0.3					)
							input.set_fill(lowerCamelCase__					,opacity=1.0					)
							input.set_stroke(width=0.0					)
							input.next_to(model_base[0]					,lowerCamelCase__					,buff=0.5					)

							self.play(Write(lowerCamelCase__					)					)

							input.generate_target()
							input.target.next_to(model_arr[0]					,direction=lowerCamelCase__					,buff=0.0_2					)
							self.play(MoveToTarget(lowerCamelCase__					)					)

							self.play(FadeOut(lowerCamelCase__					)					)

							_UpperCamelCase   :						Dict												=  Arrow(start=lowerCamelCase__					,end=lowerCamelCase__					,color=lowerCamelCase__					,buff=0.5					)
							a.next_to(model_arr[0].get_left()					,lowerCamelCase__					,buff=0.2					)

							model_cpu_arr[0].generate_target()
							model_cpu_arr[0].target.move_to(gpu_rect[0]					)

							_UpperCamelCase   :						List[str]												=  MarkupText(
							    F'As the input reaches a layer, the hook triggers\nand weights are moved from the CPU\nto the GPU and back.'					,font_size=24					,)
							step_a.move_to([2, 2, 0]					)

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

							_UpperCamelCase   :						int												=  {'run_time': 1, 'fade_in': True, 'fade_out': True, 'buff': 0.0_2}

							self.play(
							    Write(lowerCamelCase__					)					,Circumscribe(model_arr[0]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,Circumscribe(model_cpu_arr[0]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,Circumscribe(gpu_rect[0]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,)
							self.play(MoveToTarget(model_cpu_arr[0]					)					)

							_UpperCamelCase   :						Any												=  a.copy()
							for i in range(6					):
													a_c.next_to(model_arr[i].get_right() + 0.0_2					,lowerCamelCase__					,buff=0.2					)

													input.generate_target()
													input.target.move_to(model_arr[i].get_right() + 0.0_2					)

													_UpperCamelCase   :						Optional[Any]												=  AnimationGroup(
													    FadeOut(lowerCamelCase__					,run_time=0.5					)					,MoveToTarget(lowerCamelCase__					,run_time=0.5					)					,FadeIn(lowerCamelCase__					,run_time=0.5					)					,lag_ratio=0.2					)

													self.play(lowerCamelCase__					)

													model_cpu_arr[i].generate_target()
													model_cpu_arr[i].target.move_to(cpu_left_col_base[i]					)

													if i < 5:
																			model_cpu_arr[i + 1].generate_target()
																			model_cpu_arr[i + 1].target.move_to(gpu_rect[0]					)
																			if i >= 1:
																									_UpperCamelCase   :						List[str]												=  0.7

																			self.play(
																			    Circumscribe(model_arr[i]					,**lowerCamelCase__					)					,Circumscribe(cpu_left_col_base[i]					,**lowerCamelCase__					)					,Circumscribe(cpu_left_col_base[i + 1]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,Circumscribe(gpu_rect[0]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,Circumscribe(model_arr[i + 1]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,)
																			if i < 1:
																									self.play(
																									    MoveToTarget(model_cpu_arr[i]					)					,MoveToTarget(model_cpu_arr[i + 1]					)					,)
																			else:
																									self.play(
																									    MoveToTarget(model_cpu_arr[i]					,run_time=0.7					)					,MoveToTarget(model_cpu_arr[i + 1]					,run_time=0.7					)					,)
													else:
																			model_cpu_arr[i].generate_target()
																			model_cpu_arr[i].target.move_to(cpu_left_col_base[-1]					)
																			input.generate_target()
																			input.target.next_to(model_arr[-1].get_right()					,RIGHT + 0.0_2					,buff=0.2					)

																			self.play(
																			    Circumscribe(model_arr[-1]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,Circumscribe(cpu_left_col_base[-1]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,Circumscribe(gpu_rect[0]					,color=lowerCamelCase__					,**lowerCamelCase__					)					,)

																			self.play(MoveToTarget(model_cpu_arr[i]					)					)

													_UpperCamelCase   :						Any												=  a_c
													_UpperCamelCase   :						Any												=  a_c.copy()

							input.generate_target()
							input.target.next_to(model_base[-1]					,RIGHT + 0.0_2					,buff=0.5					)
							self.play(
							    FadeOut(lowerCamelCase__					)					,FadeOut(lowerCamelCase__					,run_time=0.5					)					,)

							_UpperCamelCase   :						Optional[Any]												=  MarkupText(F'Inference on a model too large for GPU memory\nis successfully completed.'					,font_size=24					)
							step_a.move_to([2, 2, 0]					)

							self.play(Write(lowerCamelCase__					,run_time=3					)					,MoveToTarget(lowerCamelCase__					)					)

							self.wait()







'''simple docstring'''
import os
import tempfile
import unittest
import uuid
from pathlib import Path

from transformers.testing_utils import get_tests_dir, require_soundfile, require_torch, require_vision
from transformers.tools.agent_types import AgentAudio, AgentImage, AgentText
from transformers.utils import is_soundfile_availble, is_torch_available, is_vision_available


if is_torch_available():
					import torch

if is_soundfile_availble():
					import soundfile as sf

if is_vision_available():
					from PIL import Image






def A__       (	UpperCAmelCase_=""				):
						_UpperCamelCase   :						Any												=  tempfile.mkdtemp()
						return os.path.join(UpperCAmelCase_    ,    str(uuid.uuida()				) + suffix				)






@require_soundfile
@require_torch
class 				lowercase__      (				unittest.TestCase  ):





	def       UpperCamelCase_    (					self   :							int					):







							'''simple docstring'''
							_UpperCamelCase   :						List[str]												=  torch.rand(12					,dtype=torch.floataa					) - 0.5
							_UpperCamelCase   :						Optional[int]												=  AgentAudio(lowerCamelCase__					)
							_UpperCamelCase   :						List[str]												=  str(agent_type.to_string()					)

							# Ensure that the tensor and the agent_type's tensor are the same
							self.assertTrue(torch.allclose(lowerCamelCase__					,agent_type.to_raw()					,atol=1E-4					)					)

							del agent_type

							# Ensure the path remains even after the object deletion
							self.assertTrue(os.path.exists(lowerCamelCase__					)					)

							# Ensure that the file contains the same value as the original tensor
							_UpperCamelCase							,     _UpperCamelCase   :						Union[str, Any]												=  sf.read(lowerCamelCase__					)
							self.assertTrue(torch.allclose(lowerCamelCase__					,torch.tensor(lowerCamelCase__					)					,atol=1E-4					)					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						Optional[Any]												=  torch.rand(12					,dtype=torch.floataa					) - 0.5
							_UpperCamelCase   :						Any												=  get_new_path(suffix='.wav'					)
							sf.write(lowerCamelCase__					,lowerCamelCase__					,16000					)

							_UpperCamelCase   :						List[Any]												=  AgentAudio(lowerCamelCase__					)

							self.assertTrue(torch.allclose(lowerCamelCase__					,agent_type.to_raw()					,atol=1E-4					)					)
							self.assertEqual(agent_type.to_string()					,lowerCamelCase__					)






@require_vision
@require_torch
class 				lowercase__      (				unittest.TestCase  ):





	def       UpperCamelCase_    (					self   :							List[Any]					):







							'''simple docstring'''
							_UpperCamelCase   :						int												=  torch.randint(0					,256					,(64, 64, 3)					)
							_UpperCamelCase   :						Optional[Any]												=  AgentImage(lowerCamelCase__					)
							_UpperCamelCase   :						List[Any]												=  str(agent_type.to_string()					)

							# Ensure that the tensor and the agent_type's tensor are the same
							self.assertTrue(torch.allclose(lowerCamelCase__					,agent_type._tensor					,atol=1E-4					)					)

							self.assertIsInstance(agent_type.to_raw()					,Image.Image					)

							# Ensure the path remains even after the object deletion
							del agent_type
							self.assertTrue(os.path.exists(lowerCamelCase__					)					)





	def       UpperCamelCase_    (					self   :							Dict					):







							'''simple docstring'''
							_UpperCamelCase   :						str												=  Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'
							_UpperCamelCase   :						Tuple												=  Image.open(lowerCamelCase__					)
							_UpperCamelCase   :						Optional[Any]												=  AgentImage(lowerCamelCase__					)

							self.assertTrue(path.samefile(agent_type.to_string()					)					)
							self.assertTrue(image == agent_type.to_raw()					)

							# Ensure the path remains even after the object deletion
							del agent_type
							self.assertTrue(os.path.exists(lowerCamelCase__					)					)





	def       UpperCamelCase_    (					self   :							Optional[int]					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  Path(get_tests_dir('fixtures/tests_samples/COCO'					)					) / '000000039769.png'
							_UpperCamelCase   :						Union[str, Any]												=  Image.open(lowerCamelCase__					)
							_UpperCamelCase   :						List[Any]												=  AgentImage(lowerCamelCase__					)

							self.assertFalse(path.samefile(agent_type.to_string()					)					)
							self.assertTrue(image == agent_type.to_raw()					)

							# Ensure the path remains even after the object deletion
							del agent_type
							self.assertTrue(os.path.exists(lowerCamelCase__					)					)






class 				lowercase__      (				unittest.TestCase  ):





	def       UpperCamelCase_    (					self   :							Any					):







							'''simple docstring'''
							_UpperCamelCase   :						List[Any]												=  'Hey!'
							_UpperCamelCase   :						Optional[int]												=  AgentText(lowerCamelCase__					)

							self.assertEqual(lowerCamelCase__					,agent_type.to_string()					)
							self.assertEqual(lowerCamelCase__					,agent_type.to_raw()					)
							self.assertEqual(lowerCamelCase__					,lowerCamelCase__					)


