'''simple docstring'''


import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
 from transformers import (
     FlaxBertModel,
     FlaxCLIPVisionModel,
     FlaxVisionTextDualEncoderModel,
     FlaxViTModel,
     VisionTextDualEncoderConfig,
     VisionTextDualEncoderProcessor,
 )
 from transformers.modeling_flax_pytorch_utils import (
     convert_pytorch_state_dict_to_flax,
     load_flax_weights_in_pytorch_model,
 )


if is_torch_available():
 import torch

 from transformers import VisionTextDualEncoderModel

if is_vision_available():
 from PIL import Image


def  _lowerCamelCase   (				lowerCamelCase_ :							Union[str, Any]       ):



     """simple docstring"""





     if isinstance(_A     ,					collections.abc.Iterable       ):
          return x
     return (x, x)






@require_flax
class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       pass


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       pass


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       pass


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				np.abs((a - b)						).max()
       self.assertLessEqual(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		F'''Difference between torch and flax is {diff} (>= {tol}).'''						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Dict       =				FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Optional[int]       =				model(input_ids=_SCREAMING_SNAKE_CASE				,		pixel_values=_SCREAMING_SNAKE_CASE				,		attention_mask=_SCREAMING_SNAKE_CASE						)

       self.assertEqual(output['text_embeds'].shape				,		(input_ids.shape[0], config.projection_dim)						)
       self.assertEqual(output['image_embeds'].shape				,		(pixel_values.shape[0], config.projection_dim)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       Union[str, Any]       =				self.get_vision_text_model(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       Optional[int]       =				{'vision_model': vision_model, 'text_model': text_model}
       UpperCAmelCase_							:       Optional[int]       =				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Union[str, Any]       =				model(input_ids=_SCREAMING_SNAKE_CASE				,		pixel_values=_SCREAMING_SNAKE_CASE				,		attention_mask=_SCREAMING_SNAKE_CASE						)

       self.assertEqual(output['text_embeds'].shape				,		(input_ids.shape[0], model.config.projection_dim)						)
       self.assertEqual(output['image_embeds'].shape				,		(pixel_values.shape[0], model.config.projection_dim)						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       Any       =				self.get_vision_text_model(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       Optional[Any]       =				{'vision_model': vision_model, 'text_model': text_model}
       UpperCAmelCase_							:       Optional[int]       =				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Tuple       =				model(input_ids=_SCREAMING_SNAKE_CASE				,		pixel_values=_SCREAMING_SNAKE_CASE				,		attention_mask=_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       List[Any]       =				output[0]

       with tempfile.TemporaryDirectory() as tmpdirname:
            model.save_pretrained(_SCREAMING_SNAKE_CASE						)
            UpperCAmelCase_							:       Any       =				FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE						)

            UpperCAmelCase_							:       Tuple       =				model(input_ids=_SCREAMING_SNAKE_CASE				,		pixel_values=_SCREAMING_SNAKE_CASE				,		attention_mask=_SCREAMING_SNAKE_CASE						)
            UpperCAmelCase_							:       Dict       =				after_output[0]
            UpperCAmelCase_							:       int       =				np.amax(np.abs(out_a - out_a						)						)
            self.assertLessEqual(_SCREAMING_SNAKE_CASE				,		1E-3						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       str       =				self.get_vision_text_model(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       List[Any]       =				{'vision_model': vision_model, 'text_model': text_model}
       UpperCAmelCase_							:       str       =				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       int       =				model(
           input_ids=_SCREAMING_SNAKE_CASE				,		pixel_values=_SCREAMING_SNAKE_CASE				,		attention_mask=_SCREAMING_SNAKE_CASE				,		output_attentions=_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Optional[int]       =				output.vision_model_output.attentions
       self.assertEqual(len(_SCREAMING_SNAKE_CASE						)				,		vision_config.num_hidden_layers						)

       # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
       UpperCAmelCase_							:       Tuple       =				to_atuple(vision_model.config.image_size						)
       UpperCAmelCase_							:       List[Any]       =				to_atuple(vision_model.config.patch_size						)
       UpperCAmelCase_							:       str       =				(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
       UpperCAmelCase_							:       List[Any]       =				num_patches + 1
       self.assertEqual(vision_attentions[0].shape[-3:]				,		(vision_config.num_attention_heads, seq_len, seq_len)						)

       UpperCAmelCase_							:       Union[str, Any]       =				output.text_model_output.attentions
       self.assertEqual(len(_SCREAMING_SNAKE_CASE						)				,		text_config.num_hidden_layers						)

       self.assertEqual(
           text_attentions[0].shape[-3:]				,		(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])				,		)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       pt_model.to(_SCREAMING_SNAKE_CASE						)
       pt_model.eval()

       # prepare inputs
       UpperCAmelCase_							:       List[Any]       =				inputs_dict
       UpperCAmelCase_							:       Optional[Any]       =				{k: torch.tensor(v.tolist()						) for k, v in flax_inputs.items()}

       with torch.no_grad():
            UpperCAmelCase_							:       Dict       =				pt_model(**_SCREAMING_SNAKE_CASE						).to_tuple()

       UpperCAmelCase_							:       int       =				fx_model(**_SCREAMING_SNAKE_CASE						).to_tuple()
       self.assertEqual(len(_SCREAMING_SNAKE_CASE						)				,		len(_SCREAMING_SNAKE_CASE						)				,		'Output lengths differ between Flax and PyTorch'						)
       for fx_output, pt_output in zip(fx_outputs[:4]				,		pt_outputs[:4]						):
            self.assert_almost_equals(_SCREAMING_SNAKE_CASE				,		pt_output.numpy()				,		4E-2						)

       # PT -> Flax
       with tempfile.TemporaryDirectory() as tmpdirname:
            pt_model.save_pretrained(_SCREAMING_SNAKE_CASE						)
            UpperCAmelCase_							:       List[str]       =				FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE				,		from_pt=_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Tuple       =				fx_model_loaded(**_SCREAMING_SNAKE_CASE						).to_tuple()
       self.assertEqual(len(_SCREAMING_SNAKE_CASE						)				,		len(_SCREAMING_SNAKE_CASE						)				,		'Output lengths differ between Flax and PyTorch'						)
       for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]				,		pt_outputs[:4]						):
            self.assert_almost_equals(_SCREAMING_SNAKE_CASE				,		pt_output.numpy()				,		4E-2						)

       # Flax -> PT
       with tempfile.TemporaryDirectory() as tmpdirname:
            fx_model.save_pretrained(_SCREAMING_SNAKE_CASE						)
            UpperCAmelCase_							:       Dict       =				VisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE				,		from_flax=_SCREAMING_SNAKE_CASE						)

       pt_model_loaded.to(_SCREAMING_SNAKE_CASE						)
       pt_model_loaded.eval()

       with torch.no_grad():
            UpperCAmelCase_							:       Optional[Any]       =				pt_model_loaded(**_SCREAMING_SNAKE_CASE						).to_tuple()

       self.assertEqual(len(_SCREAMING_SNAKE_CASE						)				,		len(_SCREAMING_SNAKE_CASE						)				,		'Output lengths differ between Flax and PyTorch'						)
       for fx_output, pt_output_loaded in zip(fx_outputs[:4]				,		pt_outputs_loaded[:4]						):
            self.assert_almost_equals(_SCREAMING_SNAKE_CASE				,		pt_output_loaded.numpy()				,		4E-2						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Union[str, Any]       =				VisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       Any       =				FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Tuple       =				convert_pytorch_state_dict_to_flax(pt_model.state_dict()				,		_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       Optional[int]       =				fx_state

       self.check_pt_flax_equivalence(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				VisionTextDualEncoderConfig.from_vision_text_configs(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       str       =				VisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       Tuple       =				FlaxVisionTextDualEncoderModel(_SCREAMING_SNAKE_CASE						)

       UpperCAmelCase_							:       Any       =				load_flax_weights_in_pytorch_model(_SCREAMING_SNAKE_CASE				,		fx_model.params						)

       self.check_pt_flax_equivalence(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				self.prepare_config_and_inputs()
       self.check_model_from_pretrained_configs(**_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				self.prepare_config_and_inputs()
       self.check_vision_text_dual_encoder_from_pretrained(**_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[Any]       =				self.prepare_config_and_inputs()
       self.check_save_load(**_SCREAMING_SNAKE_CASE						)


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				self.prepare_config_and_inputs()
       self.check_vision_text_output_attention(**_SCREAMING_SNAKE_CASE						)


  @is_pt_flax_cross_test
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				self.prepare_config_and_inputs()
       UpperCAmelCase_							:       Optional[int]       =				config_inputs_dict.pop('vision_config'						)
       UpperCAmelCase_							:       Dict       =				config_inputs_dict.pop('text_config'						)

       UpperCAmelCase_							:       int       =				config_inputs_dict

       self.check_equivalence_pt_to_flax(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)
       self.check_equivalence_flax_to_pt(_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE				,		_SCREAMING_SNAKE_CASE						)






  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_       ,      UpperCAmelCase_							:       str       =				self.get_pretrained_model_and_inputs()

       UpperCAmelCase_							:       Optional[Any]       =				model_a(**_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       List[str]       =				outputs[0]

       with tempfile.TemporaryDirectory() as tmp_dirname:
            model_a.save_pretrained(_SCREAMING_SNAKE_CASE						)
            UpperCAmelCase_							:       Any       =				FlaxVisionTextDualEncoderModel.from_pretrained(_SCREAMING_SNAKE_CASE						)

            UpperCAmelCase_							:       Tuple       =				model_a(**_SCREAMING_SNAKE_CASE						)
            UpperCAmelCase_							:       List[str]       =				after_outputs[0]
            UpperCAmelCase_							:       Union[str, Any]       =				np.amax(np.abs(out_a - out_a						)						)
            self.assertLessEqual(_SCREAMING_SNAKE_CASE				,		1E-5						)







@require_flax
class 					__SCREAMING_SNAKE_CASE						(	lowerCAmelCase__						,     unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
           'hf-internal-testing/tiny-random-vit'				,		'hf-internal-testing/tiny-bert'				,		vision_from_pt=_SCREAMING_SNAKE_CASE				,		text_from_pt=_SCREAMING_SNAKE_CASE				,		)
       UpperCAmelCase_							:       Optional[Any]       =				1_3
       UpperCAmelCase_							:       List[str]       =				floats_tensor(
           [
               batch_size,
               model.config.vision_config.num_channels,
               model.config.vision_config.image_size,
               model.config.vision_config.image_size,
           ]						)
       UpperCAmelCase_							:       Optional[Any]       =				ids_tensor([batch_size, 4]				,		model.config.text_config.vocab_size						)
       UpperCAmelCase_							:       int       =				random_attention_mask([batch_size, 4]						)
       UpperCAmelCase_							:       List[str]       =				{'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

       return model, inputs


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				FlaxViTModel(_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       List[Any]       =				FlaxBertModel(_SCREAMING_SNAKE_CASE						)
       return vision_model, text_model






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				FlaxViTModelTester(self						)
       UpperCAmelCase_							:       List[Any]       =				FlaxBertModelTester(self						)
       UpperCAmelCase_							:       Union[str, Any]       =				vit_model_tester.prepare_config_and_inputs()
       UpperCAmelCase_							:       List[str]       =				bert_model_tester.prepare_config_and_inputs()

       UpperCAmelCase_       ,      UpperCAmelCase_							:       int       =				vision_config_and_inputs

       UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_							:       Union[str, Any]       =				text_config_and_inputs

       # make sure that cross attention layers are added
       return {
           "text_config": text_config,
           "vision_config": vision_config,
           "pixel_values": pixel_values,
           "attention_mask": attention_mask,
           "input_ids": input_ids,
           "token_type_ids": token_type_ids,
       }







@require_torch
class 					__SCREAMING_SNAKE_CASE						(	lowerCAmelCase__						,     unittest.TestCase	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Any       =				FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
           'hf-internal-testing/tiny-random-clip'				,		'hf-internal-testing/tiny-bert'				,		vision_from_pt=_SCREAMING_SNAKE_CASE				,		text_from_pt=_SCREAMING_SNAKE_CASE				,		)
       UpperCAmelCase_							:       int       =				1_3
       UpperCAmelCase_							:       int       =				floats_tensor(
           [
               batch_size,
               model.config.vision_config.num_channels,
               model.config.vision_config.image_size,
               model.config.vision_config.image_size,
           ]						)
       UpperCAmelCase_							:       Optional[Any]       =				ids_tensor([batch_size, 4]				,		model.config.text_config.vocab_size						)
       UpperCAmelCase_							:       str       =				random_attention_mask([batch_size, 4]						)
       UpperCAmelCase_							:       Union[str, Any]       =				{'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

       return model, inputs


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[Any]       =				FlaxCLIPVisionModel(_SCREAMING_SNAKE_CASE						)
       UpperCAmelCase_							:       int       =				FlaxBertModel(_SCREAMING_SNAKE_CASE						)
       return vision_model, text_model






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				FlaxCLIPVisionModelTester(self						)
       UpperCAmelCase_							:       List[Any]       =				FlaxBertModelTester(self						)
       UpperCAmelCase_							:       List[str]       =				clip_model_tester.prepare_config_and_inputs()
       UpperCAmelCase_							:       List[Any]       =				bert_model_tester.prepare_config_and_inputs()

       UpperCAmelCase_       ,      UpperCAmelCase_							:       Any       =				vision_config_and_inputs

       UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_       ,      UpperCAmelCase_							:       Tuple       =				text_config_and_inputs

       # make sure that cross attention layers are added
       return {
           "text_config": text_config,
           "vision_config": vision_config,
           "pixel_values": pixel_values,
           "attention_mask": attention_mask,
           "input_ids": input_ids,
           "token_type_ids": token_type_ids,
       }







@require_flax
@require_vision
class 					__SCREAMING_SNAKE_CASE						(	unittest.TestCase	):







  '''simple docstring'''






  @slow
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       str       =				FlaxVisionTextDualEncoderModel.from_pretrained('clip-italian/clip-italian'				,		logit_scale_init_value=1.0						)
       UpperCAmelCase_							:       int       =				VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'						)

       UpperCAmelCase_							:       Optional[Any]       =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
       UpperCAmelCase_							:       Optional[Any]       =				processor(
           text=['una foto di un gatto', 'una foto di un cane']				,		images=_SCREAMING_SNAKE_CASE				,		padding=_SCREAMING_SNAKE_CASE				,		return_tensors='np'						)

       UpperCAmelCase_							:       List[Any]       =				model(**_SCREAMING_SNAKE_CASE						)

       # verify the logits
       self.assertEqual(outputs.logits_per_image.shape				,		(inputs.pixel_values.shape[0], inputs.input_ids.shape[0])						)
       self.assertEqual(
           outputs.logits_per_text.shape				,		(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])				,		)

       UpperCAmelCase_							:       Optional[Any]       =				np.array([[1.2_28_47_27, 0.3_10_41_22]]						)

       self.assertTrue(np.allclose(outputs.logits_per_image				,		_SCREAMING_SNAKE_CASE				,		atol=1E-3						)						)







'''simple docstring'''


from manim import *






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				Rectangle(height=0.5				,		width=0.5						)
       UpperCAmelCase_							:       Any       =				Rectangle(height=0.46				,		width=0.46						).set_stroke(width=0						)

       UpperCAmelCase_							:       List[str]       =				Rectangle(height=0.25				,		width=0.25						)

       UpperCAmelCase_							:       Any       =				[mem.copy() for i in range(6						)]
       UpperCAmelCase_							:       Union[str, Any]       =				[mem.copy() for i in range(6						)]
       UpperCAmelCase_							:       Optional[int]       =				VGroup(*snake_case_						).arrange(snake_case_				,		buff=0						)
       UpperCAmelCase_							:       Any       =				VGroup(*snake_case_						).arrange(snake_case_				,		buff=0						)
       UpperCAmelCase_							:       str       =				VGroup(snake_case_				,		snake_case_						).arrange(snake_case_				,		buff=0						)
       UpperCAmelCase_							:       Any       =				Text('CPU'				,		font_size=2_4						)
       UpperCAmelCase_							:       Tuple       =				Group(snake_case_				,		snake_case_						).arrange(snake_case_				,		buff=0.5				,		aligned_edge=snake_case_						)
       cpu.move_to([-2.5, -0.5, 0]						)
       self.add(snake_case_						)

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

       UpperCAmelCase_							:       str       =				[mem.copy() for i in range(6						)]
       UpperCAmelCase_							:       Tuple       =				VGroup(*snake_case_						).arrange(snake_case_				,		buff=0						)

       UpperCAmelCase_							:       str       =				Text('Model'				,		font_size=2_4						)
       UpperCAmelCase_							:       Optional[int]       =				Group(snake_case_				,		snake_case_						).arrange(snake_case_				,		buff=0.5				,		aligned_edge=snake_case_						)
       model.move_to([3, -1.0, 0]						)
       self.add(snake_case_						)

       UpperCAmelCase_							:       str       =				[]
       UpperCAmelCase_							:       Optional[Any]       =				[]

       for i, rect in enumerate(snake_case_						):
            UpperCAmelCase_							:       str       =				fill.copy().set_fill(snake_case_				,		opacity=0.8						)
            target.move_to(snake_case_						)
            model_arr.append(snake_case_						)

            UpperCAmelCase_							:       int       =				Rectangle(height=0.46				,		width=0.46						).set_stroke(width=0.0						).set_fill(snake_case_				,		opacity=0.8						)
            cpu_target.move_to(cpu_left_col_base[i]						)
            model_cpu_arr.append(snake_case_						)

       self.add(*snake_case_				,		*snake_case_						)

       UpperCAmelCase_							:       List[Any]       =				[meta_mem.copy() for i in range(6						)]
       UpperCAmelCase_							:       List[str]       =				[meta_mem.copy() for i in range(6						)]
       UpperCAmelCase_							:       Tuple       =				VGroup(*snake_case_						).arrange(snake_case_				,		buff=0						)
       UpperCAmelCase_							:       Dict       =				VGroup(*snake_case_						).arrange(snake_case_				,		buff=0						)
       UpperCAmelCase_							:       Optional[Any]       =				VGroup(snake_case_				,		snake_case_						).arrange(snake_case_				,		buff=0						)
       UpperCAmelCase_							:       Tuple       =				Text('Disk'				,		font_size=2_4						)
       UpperCAmelCase_							:       Union[str, Any]       =				Group(snake_case_				,		snake_case_						).arrange(snake_case_				,		buff=0.5				,		aligned_edge=snake_case_						)
       disk.move_to([-4, -1.25, 0]						)
       self.add(snake_case_				,		snake_case_						)

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

       UpperCAmelCase_							:       Any       =				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(snake_case_				,		snake_case_						)

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

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

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

       self.play(Write(snake_case_						)						)

       UpperCAmelCase_							:       Tuple       =				Square(0.3						)
       input.set_fill(snake_case_				,		opacity=1.0						)
       input.set_stroke(width=0.0						)
       input.next_to(model_base[0]				,		snake_case_				,		buff=0.5						)

       self.play(Write(snake_case_						)						)

       input.generate_target()
       input.target.next_to(model_arr[0]				,		direction=snake_case_				,		buff=0.02						)
       self.play(MoveToTarget(snake_case_						)						)

       self.play(FadeOut(snake_case_						)						)

       UpperCAmelCase_							:       Any       =				Arrow(start=snake_case_				,		end=snake_case_				,		color=snake_case_				,		buff=0.5						)
       a.next_to(model_arr[0].get_left()				,		snake_case_				,		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=2_4				,		)
       step_a.move_to([2, 2, 0]						)

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

       UpperCAmelCase_							:       List[Any]       =				{'run_time': 1, 'fade_in': True, 'fade_out': True, 'buff': 0.02}

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

       UpperCAmelCase_							:       Union[str, Any]       =				a.copy()
       for i in range(6						):
            a_c.next_to(model_arr[i].get_right() + 0.02				,		snake_case_				,		buff=0.2						)

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

            UpperCAmelCase_							:       Tuple       =				AnimationGroup(
                FadeOut(snake_case_				,		run_time=0.5						)				,		MoveToTarget(snake_case_				,		run_time=0.5						)				,		FadeIn(snake_case_				,		run_time=0.5						)				,		lag_ratio=0.2						)

            self.play(snake_case_						)

            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_							:       Any       =				0.7

                 self.play(
                     Circumscribe(model_arr[i]				,		**snake_case_						)				,		Circumscribe(cpu_left_col_base[i]				,		**snake_case_						)				,		Circumscribe(cpu_left_col_base[i + 1]				,		color=snake_case_				,		**snake_case_						)				,		Circumscribe(gpu_rect[0]				,		color=snake_case_				,		**snake_case_						)				,		Circumscribe(model_arr[i + 1]				,		color=snake_case_				,		**snake_case_						)				,		)
                 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.02				,		buff=0.2						)

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

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

            UpperCAmelCase_							:       Any       =				a_c
            UpperCAmelCase_							:       int       =				a_c.copy()

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

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

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

       self.wait()






