






from manim import *


class   __SCREAMING_SNAKE_CASE		(	a_	):
							def lowerCAmelCase_ (     self:      List[str]						)		->		Any:
											snake_case__									=     Rectangle(height=0.5       ,	width=0.5						)
											snake_case__									=     Rectangle(height=0.25       ,	width=0.25						)
											snake_case__									=     Rectangle(height=0.46       ,	width=0.46						).set_stroke(width=0						)

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

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

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

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

											snake_case__									=     []
											snake_case__									=     []
											snake_case__									=     []

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

															snake_case__									=     Rectangle(height=0.46 / 4       ,	width=0.46 / 3						).set_stroke(width=0.0						).set_fill(UpperCamelCase       ,	opacity=0.7						)

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

											self.add(*UpperCamelCase       ,	*UpperCamelCase       ,	*UpperCamelCase						)

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

											snake_case__									=     Text('Loaded Checkpoint'       ,	font_size=24						)
											snake_case__									=     Group(UpperCamelCase       ,	UpperCamelCase						).arrange(UpperCamelCase       ,	buff=0.5       ,	aligned_edge=UpperCamelCase						)
											checkpoint.move_to([3, 0.5, 0]						)

											self.add(UpperCamelCase						)

											snake_case__									=     []
											snake_case__									=     []

											for i, rect in enumerate(UpperCamelCase						):
															snake_case__									=     fill.copy().set_fill(UpperCamelCase       ,	opacity=0.7						)
															target.move_to(UpperCamelCase						)
															ckpt_arr.append(UpperCamelCase						)

															snake_case__									=     target.copy()
															if i < 5:
																			cpu_target.move_to(cpu_left_col_base[i + 1]						)
															else:
																			cpu_target.move_to(cpu_right_col_base[i - 5]						)
															ckpt_cpu_arr.append(UpperCamelCase						)
											self.add(*UpperCamelCase       ,	*UpperCamelCase						)

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

											snake_case__									=     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(UpperCamelCase       ,	UpperCamelCase						)

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

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

											snake_case__									=     MarkupText(
											    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'''       ,	font_size=24       ,	)
											step_a.move_to([2, 2, 0]						)

											snake_case__									=     [meta_mem.copy() for i in range(6						)]
											snake_case__									=     [meta_mem.copy() for i in range(6						)]
											snake_case__									=     VGroup(*UpperCamelCase						).arrange(UpperCamelCase       ,	buff=0						)
											snake_case__									=     VGroup(*UpperCamelCase						).arrange(UpperCamelCase       ,	buff=0						)
											snake_case__									=     VGroup(UpperCamelCase       ,	UpperCamelCase						).arrange(UpperCamelCase       ,	buff=0						)
											snake_case__									=     Text('Disk'       ,	font_size=24						)
											snake_case__									=     Group(UpperCamelCase       ,	UpperCamelCase						).arrange(UpperCamelCase       ,	buff=0.5       ,	aligned_edge=UpperCamelCase						)
											disk.move_to([-4.0, -1.25, 0]						)
											self.play(Write(UpperCamelCase       ,	run_time=3						)       ,	Write(UpperCamelCase       ,	run_time=1						)       ,	Create(UpperCamelCase       ,	run_time=1						)						)

											snake_case__									=     []
											for i, rect in enumerate(UpperCamelCase						):
															snake_case__									=     rect.copy()
															target.generate_target()
															target.target.move_to(disk_left_col_base[i]						).scale(0.5						)
															animations.append(MoveToTarget(UpperCamelCase       ,	run_time=1.5						)						)
											self.play(*UpperCamelCase						)

											self.play(FadeOut(UpperCamelCase						)						)

											snake_case__									=     MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''       ,	font_size=24						)
											step_a.move_to([2, 2, 0]						)

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

											self.play(
											    FadeOut(UpperCamelCase       ,	UpperCamelCase       ,	*UpperCamelCase       ,	*UpperCamelCase						)       ,	)

											self.wait()












import unittest

from transformers import LiltConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch

							from transformers import (
							    LiltForQuestionAnswering,
							    LiltForSequenceClassification,
							    LiltForTokenClassification,
							    LiltModel,
							)
							from transformers.models.lilt.modeling_lilt import LILT_PRETRAINED_MODEL_ARCHIVE_LIST


class   __SCREAMING_SNAKE_CASE:
							def __init__(     self:      Any       ,	UpperCamelCase:      Tuple       ,	UpperCamelCase:      str=13       ,	UpperCamelCase:      int=7       ,	UpperCamelCase:      Optional[int]=True       ,	UpperCamelCase:      Tuple=True       ,	UpperCamelCase:      List[Any]=True       ,	UpperCamelCase:      Dict=True       ,	UpperCamelCase:      Any=99       ,	UpperCamelCase:      int=24       ,	UpperCamelCase:      List[Any]=2       ,	UpperCamelCase:      Any=6       ,	UpperCamelCase:      Union[str, Any]=37       ,	UpperCamelCase:      int="gelu"       ,	UpperCamelCase:      Optional[Any]=0.1       ,	UpperCamelCase:      Optional[Any]=0.1       ,	UpperCamelCase:      Union[str, Any]=5_12       ,	UpperCamelCase:      List[Any]=16       ,	UpperCamelCase:      str=2       ,	UpperCamelCase:      Any=0.02       ,	UpperCamelCase:      int=3       ,	UpperCamelCase:      str=None       ,	UpperCamelCase:      str=10_00       ,	)		->		Any:
											snake_case__									=     parent
											snake_case__									=     batch_size
											snake_case__									=     seq_length
											snake_case__									=     is_training
											snake_case__									=     use_input_mask
											snake_case__									=     use_token_type_ids
											snake_case__									=     use_labels
											snake_case__									=     vocab_size
											snake_case__									=     hidden_size
											snake_case__									=     num_hidden_layers
											snake_case__									=     num_attention_heads
											snake_case__									=     intermediate_size
											snake_case__									=     hidden_act
											snake_case__									=     hidden_dropout_prob
											snake_case__									=     attention_probs_dropout_prob
											snake_case__									=     max_position_embeddings
											snake_case__									=     type_vocab_size
											snake_case__									=     type_sequence_label_size
											snake_case__									=     initializer_range
											snake_case__									=     num_labels
											snake_case__									=     scope
											snake_case__									=     range_bbox
							def lowerCAmelCase_ (     self:      Optional[Any]						)		->		Dict:
											snake_case__									=     ids_tensor([self.batch_size, self.seq_length]       ,	self.vocab_size						)

											snake_case__									=     ids_tensor([self.batch_size, self.seq_length, 4]       ,	self.range_bbox						)
											# Ensure that bbox is legal
											for i in range(bbox.shape[0]						):
															for j in range(bbox.shape[1]						):
																			if bbox[i, j, 3] < bbox[i, j, 1]:
																							snake_case__									=     bbox[i, j, 3]
																							snake_case__									=     bbox[i, j, 1]
																							snake_case__									=     t
																			if bbox[i, j, 2] < bbox[i, j, 0]:
																							snake_case__									=     bbox[i, j, 2]
																							snake_case__									=     bbox[i, j, 0]
																							snake_case__									=     t

											snake_case__									=     None
											if self.use_input_mask:
															snake_case__									=     ids_tensor([self.batch_size, self.seq_length]       ,	vocab_size=2						)

											snake_case__									=     None
											if self.use_token_type_ids:
															snake_case__									=     ids_tensor([self.batch_size, self.seq_length]       ,	self.type_vocab_size						)

											snake_case__									=     None
											snake_case__									=     None
											if self.use_labels:
															snake_case__									=     ids_tensor([self.batch_size]       ,	self.type_sequence_label_size						)
															snake_case__									=     ids_tensor([self.batch_size, self.seq_length]       ,	self.num_labels						)

											snake_case__									=     self.get_config()

											return config, input_ids, bbox, token_type_ids, input_mask, sequence_labels, token_labels
							def lowerCAmelCase_ (     self:      int						)		->		Optional[Any]:
											return LiltConfig(
											    vocab_size=self.vocab_size       ,	hidden_size=self.hidden_size       ,	num_hidden_layers=self.num_hidden_layers       ,	num_attention_heads=self.num_attention_heads       ,	intermediate_size=self.intermediate_size       ,	hidden_act=self.hidden_act       ,	hidden_dropout_prob=self.hidden_dropout_prob       ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,	max_position_embeddings=self.max_position_embeddings       ,	type_vocab_size=self.type_vocab_size       ,	initializer_range=self.initializer_range       ,	)
							def lowerCAmelCase_ (     self:      Optional[int]       ,	UpperCamelCase:      Optional[Any]       ,	UpperCamelCase:      str       ,	UpperCamelCase:      Any       ,	UpperCamelCase:      int       ,	UpperCamelCase:      Union[str, Any]       ,	UpperCamelCase:      Optional[Any]       ,	UpperCamelCase:      Tuple       ,	)		->		Dict:
											snake_case__									=     LiltModel(config=UpperCamelCase						)
											model.to(UpperCamelCase						)
											model.eval()
											snake_case__									=     model(UpperCamelCase       ,	bbox=UpperCamelCase       ,	attention_mask=UpperCamelCase       ,	token_type_ids=UpperCamelCase						)
											snake_case__									=     model(UpperCamelCase       ,	bbox=UpperCamelCase       ,	token_type_ids=UpperCamelCase						)
											snake_case__									=     model(UpperCamelCase       ,	bbox=UpperCamelCase						)

											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 lowerCAmelCase_ (     self:      Optional[Any]       ,	UpperCamelCase:      Optional[Any]       ,	UpperCamelCase:      Optional[Any]       ,	UpperCamelCase:      List[str]       ,	UpperCamelCase:      str       ,	UpperCamelCase:      Union[str, Any]       ,	UpperCamelCase:      Any       ,	UpperCamelCase:      Optional[Any]       ,	)		->		Tuple:
											snake_case__									=     self.num_labels
											snake_case__									=     LiltForTokenClassification(config=UpperCamelCase						)
											model.to(UpperCamelCase						)
											model.eval()
											snake_case__									=     model(
											    UpperCamelCase       ,	bbox=UpperCamelCase       ,	attention_mask=UpperCamelCase       ,	token_type_ids=UpperCamelCase       ,	labels=UpperCamelCase						)
											self.parent.assertEqual(result.logits.shape       ,	(self.batch_size, self.seq_length, self.num_labels)						)
							def lowerCAmelCase_ (     self:      Optional[int]       ,	UpperCamelCase:      List[str]       ,	UpperCamelCase:      Dict       ,	UpperCamelCase:      Tuple       ,	UpperCamelCase:      Any       ,	UpperCamelCase:      List[Any]       ,	UpperCamelCase:      Union[str, Any]       ,	UpperCamelCase:      List[Any]       ,	)		->		Any:
											snake_case__									=     LiltForQuestionAnswering(config=UpperCamelCase						)
											model.to(UpperCamelCase						)
											model.eval()
											snake_case__									=     model(
											    UpperCamelCase       ,	bbox=UpperCamelCase       ,	attention_mask=UpperCamelCase       ,	token_type_ids=UpperCamelCase       ,	start_positions=UpperCamelCase       ,	end_positions=UpperCamelCase       ,	)
											self.parent.assertEqual(result.start_logits.shape       ,	(self.batch_size, self.seq_length)						)
											self.parent.assertEqual(result.end_logits.shape       ,	(self.batch_size, self.seq_length)						)


							def lowerCAmelCase_ (     self:      List[str]						)		->		Optional[int]:
											snake_case__									=     self.prepare_config_and_inputs()
											(
											    (
											    snake_case__
											)							,						(
											    snake_case__
											)							,						(
											    snake_case__
											)							,						(
											    snake_case__
											)							,						(
											    snake_case__
											)							,						(
											    snake_case__
											)							,						(
											    snake_case__
											)							,						
											)									=     config_and_inputs
											snake_case__									=     {
											    'input_ids': input_ids,
											    'bbox': bbox,
											    'token_type_ids': token_type_ids,
											    'attention_mask': input_mask,
											}
											return config, inputs_dict


@require_torch
class   __SCREAMING_SNAKE_CASE(	a_    ,  a_    ,  a_    ,  unittest.TestCase	):
							_UpperCAmelCase     =       (
							    (
							        LiltModel,
							        LiltForSequenceClassification,
							        LiltForTokenClassification,
							        LiltForQuestionAnswering,
							    )
							    if is_torch_available()
							    else ()
							)
							_UpperCAmelCase     =       (
							    {
							        "feature-extraction": LiltModel,
							        "question-answering": LiltForQuestionAnswering,
							        "text-classification": LiltForSequenceClassification,
							        "token-classification": LiltForTokenClassification,
							        "zero-shot": LiltForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							_UpperCAmelCase     =       False
							_UpperCAmelCase     =       False
							def lowerCAmelCase_ (     self:      Dict       ,	UpperCamelCase:      Dict       ,	UpperCamelCase:      List[str]       ,	UpperCamelCase:      Any       ,	UpperCamelCase:      Union[str, Any]       ,	UpperCamelCase:      int						)		->		int:
											return True
							def lowerCAmelCase_ (     self:      Dict						)		->		List[Any]:
											snake_case__									=     LiltModelTester(self						)
											snake_case__									=     ConfigTester(self       ,	config_class=UpperCamelCase       ,	hidden_size=37						)
							def lowerCAmelCase_ (     self:      Optional[Any]						)		->		Tuple:
											self.config_tester.run_common_tests()
							def lowerCAmelCase_ (     self:      Union[str, Any]						)		->		str:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*UpperCamelCase						)
							def lowerCAmelCase_ (     self:      Tuple						)		->		Dict:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											for type in ["absolute", "relative_key", "relative_key_query"]:
															snake_case__									=     type
															self.model_tester.create_and_check_model(*UpperCamelCase						)
							def lowerCAmelCase_ (     self:      int						)		->		Dict:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*UpperCamelCase						)
							def lowerCAmelCase_ (     self:      Dict						)		->		int:
											snake_case__									=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*UpperCamelCase						)


							@slow
							def lowerCAmelCase_ (     self:      int						)		->		Any:
											for model_name in LILT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case__									=     LiltModel.from_pretrained(UpperCamelCase						)
															self.assertIsNotNone(UpperCamelCase						)


@require_torch
@slow
class   __SCREAMING_SNAKE_CASE(	unittest.TestCase	):
							def lowerCAmelCase_ (     self:      List[Any]						)		->		Dict:
											snake_case__									=     LiltModel.from_pretrained('SCUT-DLVCLab/lilt-roberta-en-base'						).to(UpperCamelCase						)

											snake_case__									=     torch.tensor([[1, 2]]       ,	device=UpperCamelCase						)
											snake_case__									=     torch.tensor([[[1, 2, 3, 4], [5, 6, 7, 8]]]       ,	device=UpperCamelCase						)

											# forward pass
											with torch.no_grad():
															snake_case__									=     model(input_ids=UpperCamelCase       ,	bbox=UpperCamelCase						)

											snake_case__									=     torch.Size([1, 2, 7_68]						)
											snake_case__									=     torch.tensor(
											    [[-0.0_653, 0.0_950, -0.0_061], [-0.0_545, 0.0_926, -0.0_324]]       ,	device=UpperCamelCase       ,	)

											self.assertTrue(outputs.last_hidden_state.shape       ,	UpperCamelCase						)
											self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :, :3]       ,	UpperCamelCase       ,	atol=1e-3						)						)




