





'''simple docstring'''




from manim import *
class 			_lowerCAmelCase		(			A__	):






     """simple docstring"""






     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	Union[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(*__snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   VGroup(*__snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   VGroup(__snake_case   ,       __snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   Text("""CPU"""   ,       font_size=24						)
        snake_case 						=   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						)

        snake_case 						=   [mem.copy() for i in range(4						)]
        snake_case 						=   VGroup(*__snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   Text("""GPU"""   ,       font_size=24						)
        snake_case 						=   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						)

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

        snake_case 						=   Text("""Model"""   ,       font_size=24						)
        snake_case 						=   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						)

        snake_case 						=   []
        snake_case 						=   []
        snake_case 						=   []

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

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

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

        self.add(*__snake_case   ,       *__snake_case   ,       *__snake_case						)

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

        snake_case 						=   Text("""Loaded Checkpoint"""   ,       font_size=24						)
        snake_case 						=   Group(__snake_case   ,       __snake_case						).arrange(__snake_case   ,       buff=0.5   ,       aligned_edge=__snake_case						)
        checkpoint.move_to([3, 0.5, 0]						)

        self.add(__snake_case						)

        snake_case 						=   []
        snake_case 						=   []

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

           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(__snake_case						)
        self.add(*__snake_case   ,       *__snake_case						)

        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(__snake_case   ,       __snake_case						)

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

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

        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(*__snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   VGroup(*__snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   VGroup(__snake_case   ,       __snake_case						).arrange(__snake_case   ,       buff=0						)
        snake_case 						=   Text("""Disk"""   ,       font_size=24						)
        snake_case 						=   Group(__snake_case   ,       __snake_case						).arrange(__snake_case   ,       buff=0.5   ,       aligned_edge=__snake_case						)
        disk.move_to([-4.0, -1.25, 0]						)
        self.play(Write(__snake_case   ,       run_time=3						)   ,       Write(__snake_case   ,       run_time=1						)   ,       Create(__snake_case   ,       run_time=1						)						)

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

        self.play(FadeOut(__snake_case						)						)

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

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

        self.wait()








'''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 			_lowerCAmelCase		(			A__	):






     """simple docstring"""






     snake_case_						      =   ["image_processor", "tokenizer"]
     snake_case_						      =   "LayoutLMv2ImageProcessor"
     snake_case_						      =   ("LayoutXLMTokenizer", "LayoutXLMTokenizerFast")



     def __init__(   self							:			List[Any]   ,       __snake_case							:			Union[str, Any]=None   ,       __snake_case							:			Optional[int]=None   ,       **__snake_case							:			Union[str, Any]						)->	str:
        if "feature_extractor" in kwargs:
           warnings.warn(
               """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
               """ instead."""   ,       __snake_case   ,       )
           snake_case 						=   kwargs.pop("""feature_extractor"""						)

        snake_case 						=   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__(__snake_case   ,       __snake_case						)



     def __call__(   self							:			Optional[Any]   ,       __snake_case							:			Dict   ,       __snake_case							:			Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None   ,       __snake_case							:			Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None   ,       __snake_case							:			Union[List[List[int]], List[List[List[int]]]] = None   ,       __snake_case							:			Optional[Union[List[int], List[List[int]]]] = None   ,       __snake_case							:			bool = True   ,       __snake_case							:			Union[bool, str, PaddingStrategy] = False   ,       __snake_case							:			Union[bool, str, TruncationStrategy] = None   ,       __snake_case							:			Optional[int] = None   ,       __snake_case							:			int = 0   ,       __snake_case							:			Optional[int] = None   ,       __snake_case							:			Optional[bool] = None   ,       __snake_case							:			Optional[bool] = None   ,       __snake_case							:			bool = False   ,       __snake_case							:			bool = False   ,       __snake_case							:			bool = False   ,       __snake_case							:			bool = False   ,       __snake_case							:			bool = True   ,       __snake_case							:			Optional[Union[str, TensorType]] = None   ,       **__snake_case							:			List[str]   ,       )->	BatchEncoding:
        # verify input
        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
        snake_case 						=   self.image_processor(images=__snake_case   ,       return_tensors=__snake_case						)

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

        snake_case 						=   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=__snake_case   ,       add_special_tokens=__snake_case   ,       padding=__snake_case   ,       truncation=__snake_case   ,       max_length=__snake_case   ,       stride=__snake_case   ,       pad_to_multiple_of=__snake_case   ,       return_token_type_ids=__snake_case   ,       return_attention_mask=__snake_case   ,       return_overflowing_tokens=__snake_case   ,       return_special_tokens_mask=__snake_case   ,       return_offsets_mapping=__snake_case   ,       return_length=__snake_case   ,       verbose=__snake_case   ,       return_tensors=__snake_case   ,       **__snake_case   ,       )

        # add pixel values
        snake_case 						=   features.pop("""pixel_values"""						)
        if return_overflowing_tokens is True:
           snake_case 						=   self.get_overflowing_images(__snake_case   ,       encoded_inputs["""overflow_to_sample_mapping"""]						)
        snake_case 						=   images

        return encoded_inputs



     def      lowerCAmelCase			(   self							:			Any   ,       __snake_case							:			List[Any]   ,       __snake_case							:			str						)->	List[str]:
        # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
        snake_case 						=   []
        for sample_idx in overflow_to_sample_mapping:
           images_with_overflow.append(images[sample_idx]						)

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

        return images_with_overflow



     def      lowerCAmelCase			(   self							:			int   ,       *__snake_case							:			Optional[int]   ,       **__snake_case							:			Tuple						)->	str:
        return self.tokenizer.batch_decode(*__snake_case   ,       **__snake_case						)



     def      lowerCAmelCase			(   self							:			Dict   ,       *__snake_case							:			Tuple   ,       **__snake_case							:			Optional[int]						)->	List[Any]:
        return self.tokenizer.decode(*__snake_case   ,       **__snake_case						)



     @property
     def      lowerCAmelCase			(   self							:			str						)->	int:
        return ["input_ids", "bbox", "attention_mask", "image"]



     @property
     def      lowerCAmelCase			(   self							:			Union[str, Any]						)->	List[str]:
        warnings.warn(
            """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""   ,       __snake_case   ,       )
        return self.image_processor_class



     @property
     def      lowerCAmelCase			(   self							:			Optional[Any]						)->	Tuple:
        warnings.warn(
            """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""   ,       __snake_case   ,       )
        return self.image_processor

