From 635c559eaba1b4c1060ee542ebc476726e7a4945 Mon Sep 17 00:00:00 2001 From: h-avsha Date: Mon, 23 Jun 2025 16:06:29 +0300 Subject: [PATCH 1/2] moving to a faster base64 implementation Signed-off-by: h-avsha --- requirements/common.txt | 1 + vllm/multimodal/image.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/requirements/common.txt b/requirements/common.txt index 639abe511017..9a9ae1d93896 100644 --- a/requirements/common.txt +++ b/requirements/common.txt @@ -44,3 +44,4 @@ watchfiles # required for http server to monitor the updates of TLS files python-json-logger # Used by logging as per examples/others/logging_configuration.md scipy # Required for phi-4-multimodal-instruct ninja # Required for xgrammar, rocm, tpu, xpu +pybase64 # fast base64 implementation diff --git a/vllm/multimodal/image.py b/vllm/multimodal/image.py index e673632d4366..cc0aa63c8559 100644 --- a/vllm/multimodal/image.py +++ b/vllm/multimodal/image.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project -import base64 +import pybase64 from io import BytesIO from pathlib import Path @@ -55,7 +55,7 @@ def load_bytes(self, data: bytes) -> Image.Image: return convert_image_mode(image, self.image_mode) def load_base64(self, media_type: str, data: str) -> Image.Image: - return self.load_bytes(base64.b64decode(data)) + return self.load_bytes(pybase64.b64decode(data, validate=True)) def load_file(self, filepath: Path) -> Image.Image: image = Image.open(filepath) @@ -75,7 +75,7 @@ def encode_base64( image.save(buffer, image_format) data = buffer.getvalue() - return base64.b64encode(data).decode('utf-8') + return pybase64.b64encode(data).decode('utf-8') class ImageEmbeddingMediaIO(MediaIO[torch.Tensor]): @@ -88,10 +88,10 @@ def load_bytes(self, data: bytes) -> torch.Tensor: return torch.load(buffer, weights_only=True) def load_base64(self, media_type: str, data: str) -> torch.Tensor: - return self.load_bytes(base64.b64decode(data)) + return self.load_bytes(pybase64.b64decode(data, validate=True)) def load_file(self, filepath: Path) -> torch.Tensor: return torch.load(filepath, weights_only=True) def encode_base64(self, media: torch.Tensor) -> str: - return base64.b64encode(media.numpy()).decode('utf-8') + return pybase64.b64encode(media.numpy()).decode('utf-8') From ce59e333a3a05fd1ab090ed573a3b05d0f3d39d9 Mon Sep 17 00:00:00 2001 From: h-avsha Date: Mon, 23 Jun 2025 16:17:55 +0300 Subject: [PATCH 2/2] import fix Signed-off-by: h-avsha --- vllm/multimodal/image.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vllm/multimodal/image.py b/vllm/multimodal/image.py index cc0aa63c8559..dce4c4c1cadb 100644 --- a/vllm/multimodal/image.py +++ b/vllm/multimodal/image.py @@ -1,10 +1,10 @@ # SPDX-License-Identifier: Apache-2.0 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project -import pybase64 from io import BytesIO from pathlib import Path +import pybase64 import torch from PIL import Image