


"""simple docstring"""


from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
	import tensorflow as tf

	from transformers import TFBlipTextModel
	from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST
class 						_UpperCamelCase						:
							'''simple docstring'''







							def __init__(  self					,     __a					,     __a=12					,     __a=7					,     __a=True					,     __a=True					,     __a=True					,     __a=99					,     __a=32					,     __a=32					,     __a=2					,     __a=4					,     __a=37					,     __a=0.1					,     __a=0.1					,     __a=5_12					,     __a=0.0_2					,     __a=0					,     __a=None					,     ):
										__lowerCAmelCase       					=    parent
										__lowerCAmelCase       					=    batch_size
										__lowerCAmelCase       					=    seq_length
										__lowerCAmelCase       					=    is_training
										__lowerCAmelCase       					=    use_input_mask
										__lowerCAmelCase       					=    use_labels
										__lowerCAmelCase       					=    vocab_size
										__lowerCAmelCase       					=    hidden_size
										__lowerCAmelCase       					=    projection_dim
										__lowerCAmelCase       					=    num_hidden_layers
										__lowerCAmelCase       					=    num_attention_heads
										__lowerCAmelCase       					=    intermediate_size
										__lowerCAmelCase       					=    dropout
										__lowerCAmelCase       					=    attention_dropout
										__lowerCAmelCase       					=    max_position_embeddings
										__lowerCAmelCase       					=    initializer_range
										__lowerCAmelCase       					=    scope
										__lowerCAmelCase       					=    bos_token_id

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size			)

										__lowerCAmelCase       					=    None
										if self.use_input_mask:
													__lowerCAmelCase       					=    random_attention_mask([self.batch_size, self.seq_length]			)

										if input_mask is not None:
													__lowerCAmelCase       					=    input_mask.numpy()
													__lowerCAmelCase					,			__lowerCAmelCase       					=    input_mask.shape
													__lowerCAmelCase       					=    np.random.randint(1					,     seq_length - 1					,     size=(batch_size,)			)
													for batch_idx, start_index in enumerate(_A			):
																__lowerCAmelCase       					=    1
																__lowerCAmelCase       					=    0

										__lowerCAmelCase       					=    self.get_config()

										return config, input_ids, tf.convert_to_tensor(_A			)

							def     snake_case       (  self			):
										return BlipTextConfig(
										    vocab_size=self.vocab_size					,     hidden_size=self.hidden_size					,     projection_dim=self.projection_dim					,     num_hidden_layers=self.num_hidden_layers					,     num_attention_heads=self.num_attention_heads					,     intermediate_size=self.intermediate_size					,     dropout=self.dropout					,     attention_dropout=self.attention_dropout					,     max_position_embeddings=self.max_position_embeddings					,     initializer_range=self.initializer_range					,     bos_token_id=self.bos_token_id					,     )

							def     snake_case       (  self					,     __a					,     __a					,     __a			):
										__lowerCAmelCase       					=    TFBlipTextModel(config=_A			)
										__lowerCAmelCase       					=    model(_A					,     attention_mask=_A					,     training=_A			)
										__lowerCAmelCase       					=    model(_A					,     training=_A			)
										self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)			)
										self.parent.assertEqual(result.pooler_output.shape					,     (self.batch_size, self.hidden_size)			)



							def     snake_case       (  self			):
										__lowerCAmelCase       					=    self.prepare_config_and_inputs()
										__lowerCAmelCase					,			__lowerCAmelCase					,			__lowerCAmelCase       					=    config_and_inputs
										__lowerCAmelCase       					=    {"input_ids": input_ids, "attention_mask": input_mask}
										return config, inputs_dict
@require_tf
class 						_UpperCamelCase						(     __lowercase ,unittest.TestCase	):
							'''simple docstring'''







							__UpperCAmelCase		:					Tuple          =(TFBlipTextModel,) if is_tf_available() else ()
							__UpperCAmelCase		:					List[str]          =False
							__UpperCAmelCase		:					Optional[Any]          =False
							__UpperCAmelCase		:					Tuple          =False

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    BlipTextModelTester(self			)
										__lowerCAmelCase       					=    ConfigTester(self					,     config_class=_A					,     hidden_size=37			)

							def     snake_case       (  self			):
										self.config_tester.run_common_tests()

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_A			)

							def     snake_case       (  self			):
										pass

							def     snake_case       (  self			):
										pass

							@unittest.skip(reason="Blip does not use inputs_embeds"			)
							def     snake_case       (  self			):
										pass

							@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"			)
							def     snake_case       (  self			):
										pass

							@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"			)
							def     snake_case       (  self			):
										pass

							@slow
							def     snake_case       (  self			):
										for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowerCAmelCase       					=    TFBlipTextModel.from_pretrained(_A			)
													self.assertIsNotNone(_A			)



							def     snake_case       (  self					,     __a=True			):
										super().test_pt_tf_model_equivalence(allow_missing_keys=_A			)








"""simple docstring"""


from manim import *
class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							def     snake_case       (  self			):
										__lowerCAmelCase       					=    Rectangle(height=0.5					,     width=0.5			)
										__lowerCAmelCase       					=    Rectangle(height=0.4_6					,     width=0.4_6			).set_stroke(width=0			)

										__lowerCAmelCase       					=    Rectangle(height=0.2_5					,     width=0.2_5			)

										__lowerCAmelCase       					=    [mem.copy() for i in range(6			)]
										__lowerCAmelCase       					=    [mem.copy() for i in range(6			)]
										__lowerCAmelCase       					=    VGroup(*__a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    VGroup(*__a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    VGroup(__a					,     __a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    Text("CPU"					,     font_size=24			)
										__lowerCAmelCase       					=    Group(__a					,     __a			).arrange(__a					,     buff=0.5					,     aligned_edge=__a			)
										cpu.move_to([-2.5, -0.5, 0]			)
										self.add(__a			)

										__lowerCAmelCase       					=    [mem.copy() for i in range(4			)]
										__lowerCAmelCase       					=    VGroup(*__a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    Text("GPU"					,     font_size=24			)
										__lowerCAmelCase       					=    Group(__a					,     __a			).arrange(__a					,     buff=0.5					,     aligned_edge=__a			)
										gpu.move_to([-1, -1, 0]			)
										self.add(__a			)

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

										__lowerCAmelCase       					=    Text("Model"					,     font_size=24			)
										__lowerCAmelCase       					=    Group(__a					,     __a			).arrange(__a					,     buff=0.5					,     aligned_edge=__a			)
										model.move_to([3, -1.0, 0]			)
										self.add(__a			)

										__lowerCAmelCase       					=    []
										__lowerCAmelCase       					=    []

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

													__lowerCAmelCase       					=    Rectangle(height=0.4_6					,     width=0.4_6			).set_stroke(width=0.0			).set_fill(__a					,     opacity=0.8			)
													cpu_target.move_to(cpu_left_col_base[i]			)
													model_cpu_arr.append(__a			)

										self.add(*__a					,     *__a			)

										__lowerCAmelCase       					=    [meta_mem.copy() for i in range(6			)]
										__lowerCAmelCase       					=    [meta_mem.copy() for i in range(6			)]
										__lowerCAmelCase       					=    VGroup(*__a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    VGroup(*__a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    VGroup(__a					,     __a			).arrange(__a					,     buff=0			)
										__lowerCAmelCase       					=    Text("Disk"					,     font_size=24			)
										__lowerCAmelCase       					=    Group(__a					,     __a			).arrange(__a					,     buff=0.5					,     aligned_edge=__a			)
										disk.move_to([-4, -1.2_5, 0]			)
										self.add(__a					,     __a			)

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

										__lowerCAmelCase       					=    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(__a					,     __a			)

										__lowerCAmelCase       					=    MarkupText(
										    f"<span fgcolor='{BLUE}'>●</span> Checkpoint"					,     font_size=18					,     )

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

										__lowerCAmelCase       					=    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(__a			)			)

										__lowerCAmelCase       					=    Square(0.3			)
										input.set_fill(__a					,     opacity=1.0			)
										input.set_stroke(width=0.0			)
										input.next_to(model_base[0]					,     __a					,     buff=0.5			)

										self.play(Write(__a			)			)

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

										self.play(FadeOut(__a			)			)

										__lowerCAmelCase       					=    Arrow(start=__a					,     end=__a					,     color=__a					,     buff=0.5			)
										a.next_to(model_arr[0].get_left()					,     __a					,     buff=0.2			)

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

										__lowerCAmelCase       					=    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(__a					,     run_time=3			)			)

										__lowerCAmelCase       					=    {"run_time": 1, "fade_in": True, "fade_out": True, "buff": 0.0_2}

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

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

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

													__lowerCAmelCase       					=    AnimationGroup(
													    FadeOut(__a					,     run_time=0.5			)					,     MoveToTarget(__a					,     run_time=0.5			)					,     FadeIn(__a					,     run_time=0.5			)					,     lag_ratio=0.2			)

													self.play(__a			)

													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:
																			__lowerCAmelCase       					=    0.7

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

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

													__lowerCAmelCase       					=    a_c
													__lowerCAmelCase       					=    a_c.copy()

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

										__lowerCAmelCase       					=    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(__a					,     run_time=3			)					,     MoveToTarget(__a			)			)

										self.wait()




