Skip to content

vllm.models.inkling2.processing

Inkling2 image preprocessing and vLLM multimodal processing.

The renderer emits one <|content_image|> token per image. The processor replaces it with <|content_image|> followed by rows * cols copies of IMAGE_PAD_ID; only the pad positions receive vision embeddings.

Classes:

Functions:

  • decode_image –

    Decode an image to HWC uint8 RGB, compositing alpha on a checkerboard.

Inkling2ImageProcessor

Pads each image to a 40-pixel grid and emits one patch per cell.

Returns vision_pixels (bf16 [sum(rows*cols), 40, 40, 3] raw RGB values, padding pixels at -1; the encoder normalizes them) and vision_grid_thw.

Source code in vllm/models/inkling2/processing.py
class Inkling2ImageProcessor:
    """Pads each image to a 40-pixel grid and emits one patch per cell.

    Returns `vision_pixels` (bf16 `[sum(rows*cols), 40, 40, 3]` raw RGB
    values, padding pixels at -1; the encoder normalizes them) and
    `vision_grid_thw`.
    """

    @staticmethod
    def get_grid(width: int, height: int) -> tuple[int, int]:
        return math.ceil(height / PATCH_SIZE), math.ceil(width / PATCH_SIZE)

    def preprocess(self, images: Sequence[ImageInput]) -> dict[str, torch.Tensor]:
        decoded = [torch.from_numpy(decode_image(image)) for image in images]
        grids = [self.get_grid(pixels.shape[1], pixels.shape[0]) for pixels in decoded]
        out = torch.empty(
            (sum(rows * cols for rows, cols in grids), PATCH_SIZE, PATCH_SIZE, 3),
            dtype=torch.bfloat16,
        )
        start = 0
        for pixels, (rows, cols) in zip(decoded, grids):
            height, width, _ = pixels.shape
            if (height, width) != (rows * PATCH_SIZE, cols * PATCH_SIZE):
                padded = pixels.new_full(
                    (rows * PATCH_SIZE, cols * PATCH_SIZE, 3), -1, dtype=torch.bfloat16
                )
                padded[:height, :width] = pixels
                pixels = padded
            cells = pixels.view(rows, PATCH_SIZE, cols, PATCH_SIZE, 3)
            out[start : start + rows * cols].view(
                rows, cols, PATCH_SIZE, PATCH_SIZE, 3
            ).copy_(cells.permute(0, 2, 1, 3, 4))
            start += rows * cols
        return {
            "vision_pixels": out,
            "vision_grid_thw": torch.tensor(
                [[1, rows, cols] for rows, cols in grids], dtype=torch.int64
            ),
        }

Inkling2MultiModalDataParser

Bases: MultiModalDataParser

Keeps PIL image modes so transparency reaches the checkerboard step.

Source code in vllm/models/inkling2/processing.py
class Inkling2MultiModalDataParser(MultiModalDataParser):
    """Keeps PIL image modes so transparency reaches the checkerboard step."""

    def _parse_image_data(self, data: Any) -> Any:
        if isinstance(data, Image.Image):
            return ImageProcessorItems([data])
        if (
            isinstance(data, Sequence)
            and data
            and all(isinstance(item, Image.Image) for item in data)
        ):
            return ImageProcessorItems(list(data))
        return super()._parse_image_data(data)

decode_image(image)

Decode an image to HWC uint8 RGB, compositing alpha on a checkerboard.

Encoded bytes (including MediaWithBytes.original_bytes) are preferred over decoded media, so no vLLM-side mode conversion or EXIF handling leaks into the pixels.

Source code in vllm/models/inkling2/processing.py
def decode_image(image: ImageInput) -> np.ndarray:
    """Decode an image to HWC uint8 RGB, compositing alpha on a checkerboard.

    Encoded bytes (including `MediaWithBytes.original_bytes`) are preferred
    over decoded media, so no vLLM-side mode conversion or EXIF handling
    leaks into the pixels.
    """
    if isinstance(image, MediaWithBytes):
        image = image.original_bytes
    if isinstance(image, (bytes, bytearray, memoryview)):
        data = bytes(image)
        if data.startswith(b"\xff\xd8") and not data.endswith(b"\xff\xd9"):
            data += b"\xff\xd9"
        with Image.open(io.BytesIO(data)) as pil:
            pil.load()
            return _pil_to_rgb(pil)
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    return _pil_to_rgb(image)