# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # This file was automatically generated from src/transformers/models/lighton_ocr/modular_lighton_ocr.py. # Do NOT edit this file manually as any edits will be overwritten by the generation of # the file from the modular. If any change should be done, please apply the change to the # modular_lighton_ocr.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # Copyright 2026 The LightOn Team and The HuggingFace Inc. team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import math import numpy as np from ...image_utils import ChannelDimension, ImageInput, get_image_size from ...processing_utils import MultiModalData, ProcessingKwargs, ProcessorMixin class LightOnOcrProcessorKwargs(ProcessingKwargs, total=False): _defaults = { "text_kwargs": { "padding": False, "return_mm_token_type_ids": False, }, "common_kwargs": { "return_tensors": "pt", }, } def _num_image_tokens(image_size: tuple[int, int], patch_size: tuple[int, int]) -> int: """ Calculate the number of image tokens given the image size and patch size. Args: image_size (`tuple[int, int]`): The size of the image as `(height, width)`. patch_size (`tuple[int, int]`): The patch size as `(height, width)`. Returns: `int`: The number of image tokens. """ height, width = image_size patch_height, patch_width = patch_size if isinstance(patch_size, (tuple, list)) else (patch_size, patch_size) num_width_tokens = (width - 1) // patch_width + 1 num_height_tokens = (height - 1) // patch_height + 1 return num_height_tokens, num_width_tokens def get_resize_output_image_size( input_image: ImageInput, size: int | tuple[int, int] | list[int] | tuple[int], patch_size: int | tuple[int, int] | list[int] | tuple[int], input_data_format: str | ChannelDimension | None = None, ) -> tuple: """ Find the target (height, width) dimension of the output image after resizing given the input image and the desired size. Args: input_image (`ImageInput`): The image to resize. size (`int` or `tuple[int, int]`): Max image size an input image can be. Must be a dictionary with the key "longest_edge". patch_size (`int` or `tuple[int, int]`): The patch_size as `(height, width)` to use for resizing the image. If patch_size is an integer, `(patch_size, patch_size)` will be used input_data_format (`ChannelDimension`, *optional*): The channel dimension format of the input image. If unset, will use the inferred format from the input. Returns: `tuple`: The target (height, width) dimension of the output image after resizing. """ max_height, max_width = size if isinstance(size, (tuple, list)) else (size, size) patch_height, patch_width = patch_size if isinstance(patch_size, (tuple, list)) else (patch_size, patch_size) height, width = get_image_size(input_image, input_data_format) ratio = max(height / max_height, width / max_width) if ratio > 1: # Original implementation uses `round` which utilises bankers rounding, which can lead to surprising results # Here we use floor to ensure the image is always smaller than the given "longest_edge" height = int(math.floor(height / ratio)) width = int(math.floor(width / ratio)) num_height_tokens, num_width_tokens = _num_image_tokens((height, width), (patch_height, patch_width)) return num_height_tokens * patch_height, num_width_tokens * patch_width class LightOnOcrProcessor(ProcessorMixin): valid_processor_kwargs = LightOnOcrProcessorKwargs def __init__( self, image_processor=None, tokenizer=None, patch_size: int = 14, spatial_merge_size: int = 2, chat_template=None, **kwargs, ): super().__init__(image_processor, tokenizer, chat_template=chat_template) self.patch_size = patch_size self.spatial_merge_size = spatial_merge_size # Calculate effective patch size for image processing self.effective_patch_size = patch_size * spatial_merge_size # Get special tokens and IDs directly from tokenizer attributes self.image_token = tokenizer.image_token self.image_break_token = tokenizer.image_break_token self.image_end_token = tokenizer.image_end_token self.image_token_id = tokenizer.image_token_id self.image_break_token_id = tokenizer.image_break_token_id self.image_end_token_id = tokenizer.image_end_token_id @property def image_token_ids(self) -> list[int]: return [self.image_token_id, self.image_break_token_id, self.image_end_token_id] def replace_image_token(self, image_inputs: dict, image_idx: int, **kwargs) -> str: image_height, image_width = image_inputs["image_sizes"][image_idx] num_height_tokens = image_height // self.effective_patch_size num_width_tokens = image_width // self.effective_patch_size return self.image_token * (num_height_tokens * num_width_tokens) def _get_num_multimodal_tokens(self, image_sizes=None, **kwargs): """ Computes the number of placeholder tokens needed for multimodal inputs with the given sizes. Args: image_sizes (`list[list[int]]`, *optional*): The input sizes formatted as (height, width) per each image. Returns: `MultiModalData`: A `MultiModalData` object holding number of tokens per each of the provided input modalities, along with other useful data. """ vision_data = {} if image_sizes is not None: images_kwargs = LightOnOcrProcessorKwargs._defaults.get("images_kwargs", {}) images_kwargs.update(kwargs) size = images_kwargs.get("size", None) or self.image_processor.size patch_size = images_kwargs.get("patch_size", None) or self.image_processor.patch_size if isinstance(patch_size, dict) and "height" in patch_size and "width" in patch_size: patch_size = (patch_size["height"], patch_size["width"]) num_image_tokens = [] for height, width in image_sizes: resized_height, resized_width = get_resize_output_image_size( np.zeros((height, width, 3)), size=(size["longest_edge"], size["longest_edge"]), patch_size=patch_size, ) num_height_tokens = resized_height // self.effective_patch_size num_width_tokens = resized_width // self.effective_patch_size num_image_tokens.append(num_width_tokens * num_height_tokens) num_image_patches = [1] * len(image_sizes) vision_data.update({"num_image_tokens": num_image_tokens, "num_image_patches": num_image_patches}) return MultiModalData(**vision_data) __all__ = ["LightOnOcrProcessor"]