





"""simple docstring"""




import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 					_a		( _lowerCAmelCase							):
				UpperCamelCase								=       ['''image_processor''', '''tokenizer''']
				UpperCamelCase								=       '''LayoutLMv2ImageProcessor'''
				UpperCamelCase								=       ('''LayoutLMv2Tokenizer''', '''LayoutLMv2TokenizerFast''')






				def __init__(					self						:       Optional[int],					lowerCAmelCase__						:       List[Any]=None,					lowerCAmelCase__						:       List[Any]=None,					**lowerCAmelCase__						:       Union[str, Any]				)      -> Tuple:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       None
										if "feature_extractor" in kwargs:
																warnings.warn(
																    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
																    ''' instead.''',					lowerCAmelCase__,					)
																_UpperCamelCase     :   str      =       kwargs.pop('''feature_extractor'''				)

										_UpperCamelCase     :   str      =       image_processor if image_processor is not None else feature_extractor
										if image_processor is None:
																raise ValueError('''You need to specify an `image_processor`.'''				)
										if tokenizer is None:
																raise ValueError('''You need to specify a `tokenizer`.'''				)

										super().__init__(lowerCAmelCase__,					lowerCAmelCase__				)






				def __call__(					self						:       List[str],					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,					lowerCAmelCase__						:       Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None,					lowerCAmelCase__						:       Union[List[List[int]], List[List[List[int]]]] = None,					lowerCAmelCase__						:       Optional[Union[List[int], List[List[int]]]] = None,					lowerCAmelCase__						:       bool = True,					lowerCAmelCase__						:       Union[bool, str, PaddingStrategy] = False,					lowerCAmelCase__						:       Union[bool, str, TruncationStrategy] = False,					lowerCAmelCase__						:       Optional[int] = None,					lowerCAmelCase__						:       int = 0,					lowerCAmelCase__						:       Optional[int] = None,					lowerCAmelCase__						:       Optional[bool] = None,					lowerCAmelCase__						:       Optional[bool] = None,					lowerCAmelCase__						:       bool = False,					lowerCAmelCase__						:       bool = False,					lowerCAmelCase__						:       bool = False,					lowerCAmelCase__						:       bool = False,					lowerCAmelCase__						:       bool = True,					lowerCAmelCase__						:       Optional[Union[str, TensorType]] = None,					**lowerCAmelCase__						:       List[Any],					)      -> BatchEncoding:




										'''simple docstring'''



										if self.image_processor.apply_ocr and (boxes is not None):
																raise ValueError(
																    '''You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True.'''				)

										if self.image_processor.apply_ocr and (word_labels is not None):
																raise ValueError(
																    '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''				)

										if return_overflowing_tokens is True and return_offsets_mapping is False:
																raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''				)

										# first, apply the image processor
										_UpperCamelCase     :   List[str]      =       self.image_processor(images=lowerCAmelCase__,					return_tensors=lowerCAmelCase__				)

										# second, apply the tokenizer
										if text is not None and self.image_processor.apply_ocr and text_pair is None:
																if isinstance(lowerCAmelCase__,					lowerCAmelCase__				):
																						_UpperCamelCase     :   List[str]      =       [text]  # add batch dimension (as the image processor always adds a batch dimension)
																_UpperCamelCase     :   List[str]      =       features['''words''']

										_UpperCamelCase     :   Union[str, Any]      =       self.tokenizer(
										    text=text if text is not None else features['''words'''],					text_pair=text_pair if text_pair is not None else None,					boxes=boxes if boxes is not None else features['''boxes'''],					word_labels=lowerCAmelCase__,					add_special_tokens=lowerCAmelCase__,					padding=lowerCAmelCase__,					truncation=lowerCAmelCase__,					max_length=lowerCAmelCase__,					stride=lowerCAmelCase__,					pad_to_multiple_of=lowerCAmelCase__,					return_token_type_ids=lowerCAmelCase__,					return_attention_mask=lowerCAmelCase__,					return_overflowing_tokens=lowerCAmelCase__,					return_special_tokens_mask=lowerCAmelCase__,					return_offsets_mapping=lowerCAmelCase__,					return_length=lowerCAmelCase__,					verbose=lowerCAmelCase__,					return_tensors=lowerCAmelCase__,					**lowerCAmelCase__,					)

										# add pixel values
										_UpperCamelCase     :   List[str]      =       features.pop('''pixel_values'''				)
										if return_overflowing_tokens is True:
																_UpperCamelCase     :   Optional[Any]      =       self.get_overflowing_images(lowerCAmelCase__,					encoded_inputs['''overflow_to_sample_mapping''']				)
										_UpperCamelCase     :   List[Any]      =       images

										return encoded_inputs






				def 							snake_case      (					self						:       int,					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Dict				)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Optional[Any]      =       []
										for sample_idx in overflow_to_sample_mapping:
																images_with_overflow.append(images[sample_idx]				)

										if len(lowerCAmelCase__				) != len(lowerCAmelCase__				):
																raise ValueError(
																    '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
																    f""" {len(lowerCAmelCase__				)} and {len(lowerCAmelCase__				)}"""				)

										return images_with_overflow






				def 							snake_case      (					self						:       List[str],					*lowerCAmelCase__						:       Dict,					**lowerCAmelCase__						:       int				)      -> Optional[Any]:




										'''simple docstring'''



										return self.tokenizer.batch_decode(*lowerCAmelCase__,					**lowerCAmelCase__				)






				def 							snake_case      (					self						:       Tuple,					*lowerCAmelCase__						:       Optional[int],					**lowerCAmelCase__						:       Dict				)      -> List[Any]:




										'''simple docstring'''



										return self.tokenizer.decode(*lowerCAmelCase__,					**lowerCAmelCase__				)






				@property
				def 							snake_case      (					self						:       List[Any]				)      -> Optional[int]:




										'''simple docstring'''



										return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]






				@property
				def 							snake_case      (					self						:       List[Any]				)      -> List[str]:




										'''simple docstring'''



										warnings.warn(
										    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.''',					lowerCAmelCase__,					)
										return self.image_processor_class






				@property
				def 							snake_case      (					self						:       List[str]				)      -> int:




										'''simple docstring'''



										warnings.warn(
										    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.''',					lowerCAmelCase__,					)
										return self.image_processor










'''simple docstring'''







from manim import *


class        __a   (lowerCamelCase  ):

							def      UpperCAmelCase__						(				self				:  str )       ->							Any:







													"""simple docstring"""
													UpperCAmelCase_      :   List[str]      =	Rectangle(height=0.5							,  width=0.5 )
													UpperCAmelCase_      :   int      =	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_      :   int      =	[mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   List[str]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   Optional[Any]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   List[Any]      =	VGroup(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   int      =	Text('''CPU'''							,  font_size=24 )
													UpperCAmelCase_      :   Optional[Any]      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													cpu.move_to([-2.5, -0.5, 0] )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   List[Any]      =	[mem.copy() for i in range(4 )]
													UpperCAmelCase_      :   List[str]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )
													UpperCAmelCase_      :   str      =	Text('''GPU'''							,  font_size=24 )
													UpperCAmelCase_      :   Union[str, Any]      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													gpu.move_to([-1, -1, 0] )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   Dict      =	[mem.copy() for i in range(6 )]
													UpperCAmelCase_      :   Optional[Any]      =	VGroup(*__magic_name__ ).arrange(__magic_name__							,  buff=0 )

													UpperCAmelCase_      :   Dict      =	Text('''Model'''							,  font_size=24 )
													UpperCAmelCase_      :   str      =	Group(__magic_name__							,  __magic_name__ ).arrange(__magic_name__							,  buff=0.5							,  aligned_edge=__magic_name__ )
													model.move_to([3, -1.0, 0] )
													self.add(__magic_name__ )

													UpperCAmelCase_      :   List[str]      =	[]
													UpperCAmelCase_      :   int      =	[]

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

																			UpperCAmelCase_      :   List[Any]      =	Rectangle(height=0.4_6 / 4							,  width=0.4_6 / 3 ).set_stroke(width=0.0 ).set_fill(__magic_name__							,  opacity=0.7 )

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

													self.add(*__magic_name__							,  *__magic_name__ )

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

													UpperCAmelCase_      :   Optional[Any]      =	[]

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

													self.add(*__magic_name__ )

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

													UpperCAmelCase_      :   Union[str, Any]      =	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(__magic_name__							,  __magic_name__ )

													UpperCAmelCase_      :   Union[str, Any]      =	MarkupText(
													    F"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""							,  font_size=18							,  )

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

													UpperCAmelCase_      :   Dict      =	MarkupText(F"""The offloaded weights are all sent to the CPU."""							,  font_size=24 )
													step_a.move_to([2, 2, 0] )

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

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

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

																									self.remove(
																									    *__magic_name__							,  *__magic_name__							,  )

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

													UpperCAmelCase_      :   int      =	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=24							,  )
													step_a.move_to([2, 2, 0] )

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

													UpperCAmelCase_      :   Union[str, Any]      =	[]
													UpperCAmelCase_      :   Tuple      =	[]
													for i in range(6 ):
																			UpperCAmelCase_      :   List[Any]      =	Arrow(start=__magic_name__							,  end=__magic_name__							,  color=__magic_name__							,  buff=0.5 )
																			a.next_to(model_base[i].get_left()							,  __magic_name__							,  buff=0.2 )
																			arrows.append(__magic_name__ )
																			animations.append(Write(__magic_name__ ) )
													self.play(*__magic_name__ )
													self.wait()





