glm-image: cache default HF processor to reduce input preprocessing latency (#3245)

Signed-off-by: lyj-jjj <liuyingjun5@huawei.com>
Co-authored-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
lyj-jjjandHongsheng Liu authored and GitHub committed 2026-05-02 07:51:32 +08:00
1 parent 68a484589d
commit 6509303a04
1 file changed
+26 -5
@@ -24,6 +24,7 @@
import math
import os
from collections.abc import Iterable, Mapping, Sequence
from functools import lru_cache
from typing import Annotated, Literal
import torch
@@ -103,6 +104,15 @@ logger = init_logger(__name__)
# === Multimodal Processing ===
@lru_cache(maxsize=1)
def _load_cached_glm_image_processor(processor_path: str, trust_remote_code: bool) -> GlmImageProcessor:
"""Cache GLM-Image processor loading to avoid repeated from_pretrained cost."""
return GlmImageProcessor.from_pretrained(
processor_path,
trust_remote_code=trust_remote_code,
)
class GlmImagePixelInputs(TensorSchema):
"""
Schema for GLM-Image pixel inputs.
@@ -177,11 +187,22 @@ class GlmImageProcessingInfo(BaseProcessingInfo):
if not os.path.exists(processor_path):
processor_path = model_path
# Load processor directly from the correct path
return GlmImageProcessor.from_pretrained(
processor_path,
trust_remote_code=self.ctx.model_config.trust_remote_code,
**kwargs,
trust_remote_code = self.ctx.model_config.trust_remote_code
# Keep dynamic override behavior when kwargs are provided, but use a
# cached instance for the default path to reduce per-request overhead.
# Default path (without kwargs): high frequency, stable, safely reuse cache;
# with kwargs: maintain precise semantics, construct instantly per call, avoid mismatch risk.
if kwargs:
return GlmImageProcessor.from_pretrained(
processor_path,
trust_remote_code=trust_remote_code,
**kwargs,
)
return _load_cached_glm_image_processor(
processor_path=processor_path,
trust_remote_code=trust_remote_code,
)
def get_data_parser(self) -> GlmImageDataParser: