[Diffusion] Rename runtime cache to host weight cache

Signed-off-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
Hongsheng Liu committed 2026-08-19 11:23:36 +08:00
1 parent ee665fcfc3
commit 3bb7547a0f
20 files changed
+317 -302

No files matched your search

+15 -15
View File
@@ -712,14 +712,14 @@ class OmniDiffusionConfig:
# used when direct checkpoint mmap is unavailable, or when direct CUDA
# registration requires a transform-complete final layout. The first
# version is no-AllGather only.
dlo_enable_runtime_cache: bool = False
# Shared local-disk root. None selects ~/.cache/vllm-omni/dlo-runtime-weights.
dlo_runtime_cache_dir: str | None = None
dlo_enable_host_weight_cache: bool = False
# Shared local-disk root. None selects ~/.cache/vllm-omni/dlo-host-weights.
dlo_host_weight_cache_dir: str | None = None
# Maximum wait for the per-layout POSIX writer lock.
dlo_runtime_cache_lock_timeout: float = 600.0
dlo_host_weight_cache_lock_timeout: float = 600.0
# Maximum file-backed memory a worker may register with CUDA. Zero keeps
# the bounded mmap-to-pinned staging path.
dlo_runtime_cache_pin_limit_gib: float = 0.0
dlo_host_weight_cache_pin_limit_gib: float = 0.0
# Leading main-DiT blocks kept resident by distributed layerwise offload.
dlo_resident_layers: int = 0
@@ -948,16 +948,16 @@ class OmniDiffusionConfig:
)
def __post_init__(self):
if not math.isfinite(self.dlo_runtime_cache_lock_timeout) or self.dlo_runtime_cache_lock_timeout <= 0:
raise ValueError("dlo_runtime_cache_lock_timeout must be positive")
if not math.isfinite(self.dlo_runtime_cache_pin_limit_gib) or self.dlo_runtime_cache_pin_limit_gib < 0:
raise ValueError("dlo_runtime_cache_pin_limit_gib must be non-negative")
if self.dlo_enable_runtime_cache and not self.enable_distributed_layerwise_offload:
raise ValueError("dlo_enable_runtime_cache requires distributed layerwise offload")
if self.dlo_enable_runtime_cache and self.dlo_use_allgather:
raise ValueError("dlo_enable_runtime_cache currently requires dlo_use_allgather=False")
if self.dlo_runtime_cache_pin_limit_gib and not self.dlo_enable_runtime_cache:
raise ValueError("dlo_runtime_cache_pin_limit_gib requires dlo_enable_runtime_cache=True")
if not math.isfinite(self.dlo_host_weight_cache_lock_timeout) or self.dlo_host_weight_cache_lock_timeout <= 0:
raise ValueError("dlo_host_weight_cache_lock_timeout must be positive")
if not math.isfinite(self.dlo_host_weight_cache_pin_limit_gib) or self.dlo_host_weight_cache_pin_limit_gib < 0:
raise ValueError("dlo_host_weight_cache_pin_limit_gib must be non-negative")
if self.dlo_enable_host_weight_cache and not self.enable_distributed_layerwise_offload:
raise ValueError("dlo_enable_host_weight_cache requires distributed layerwise offload")
if self.dlo_enable_host_weight_cache and self.dlo_use_allgather:
raise ValueError("dlo_enable_host_weight_cache currently requires dlo_use_allgather=False")
if self.dlo_host_weight_cache_pin_limit_gib and not self.dlo_enable_host_weight_cache:
raise ValueError("dlo_host_weight_cache_pin_limit_gib requires dlo_enable_host_weight_cache=True")
if self.diffusion_compile_granularity not in {"regional", "full"}:
raise ValueError(
"diffusion_compile_granularity must be 'regional' or 'full', "
@@ -441,16 +441,16 @@ class DiffusersPipelineLoader:
_dp_size = int(getattr(self.parallel_config, "data_parallel_size", 1))
_sp_size = int(getattr(self.parallel_config, "sequence_parallel_size", 1))
_dlo_group_size = _dp_size if _dp_size > 1 else _sp_size
_prefer_registered_runtime_cache = (
_prefer_registered_host_weight_cache = (
_dist_offload
and not _use_ag
and bool(getattr(self.od_config, "dlo_enable_runtime_cache", False))
and float(getattr(self.od_config, "dlo_runtime_cache_pin_limit_gib", 0.0)) > 0
and bool(getattr(self.od_config, "dlo_enable_host_weight_cache", False))
and float(getattr(self.od_config, "dlo_host_weight_cache_pin_limit_gib", 0.0)) > 0
)
plan_result = None
weight_sources = self._get_weight_sources(model)
if _dist_offload and not _prefer_registered_runtime_cache:
if _dist_offload and not _prefer_registered_host_weight_cache:
modules = ModuleDiscovery.discover(model)
plan_result = build_checkpoint_mmap_plan(
model,
@@ -462,14 +462,14 @@ class DiffusersPipelineLoader:
online_quantization=_has_online_quant,
)
self.host_weight_plan = plan_result.plan
elif _prefer_registered_runtime_cache:
elif _prefer_registered_host_weight_cache:
# Direct checkpoint bindings can carry deferred transforms
# (for example grouped-QKV reordering), so registering the
# raw checkpoint mapping would not make every recurrent H2D
# source directly copyable. Materialize once, apply every
# post-load mutation, then publish/register the final layout.
logger.info(
"DLO runtime-cache registration requested; using the ordinary loader "
"DLO host weight cache registration requested; using the ordinary loader "
"once to publish a transform-complete final mmap layout"
)
@@ -540,19 +540,19 @@ class DiffusersPipelineLoader:
self._apply_skip_softmax_calibration(model)
model.eval()
self._maybe_build_runtime_weight_cache(model, load_format)
self._maybe_build_host_weight_cache(model, load_format)
return model
def _maybe_build_runtime_weight_cache(self, model: nn.Module, load_format: str) -> None:
def _maybe_build_host_weight_cache(self, model: nn.Module, load_format: str) -> None:
"""Publish final ordinary-loader DiT tensors for no-AllGather DLO."""
if self.host_weight_plan is not None:
return
if not getattr(self.od_config, "enable_distributed_layerwise_offload", False):
return
if not getattr(self.od_config, "dlo_enable_runtime_cache", False):
if not getattr(self.od_config, "dlo_enable_host_weight_cache", False):
return
if getattr(self.od_config, "dlo_use_allgather", True):
logger.info("DLO runtime cache is no-AllGather only; leaving AllGather storage unchanged")
logger.info("DLO host weight cache is no-AllGather only; leaving AllGather storage unchanged")
return
parallel_config = self.parallel_config
@@ -565,17 +565,18 @@ class DiffusersPipelineLoader:
tp_rank = int(get_tensor_model_parallel_rank())
except Exception as exc:
logger.warning(
"DLO runtime cache unavailable [parallel_rank_unavailable]: cannot resolve TP rank for TP=%d: %s",
"DLO host weight cache unavailable [parallel_rank_unavailable]: "
"cannot resolve TP rank for TP=%d: %s",
tp_size,
exc,
)
return
modules = ModuleDiscovery.discover(model)
from vllm_omni.diffusion.model_loader.runtime_weight_cache import (
from vllm_omni.diffusion.model_loader.host_weight_cache import (
DEFAULT_LOCK_TIMEOUT_SECONDS,
DEFAULT_SHARD_SIZE_BYTES,
build_runtime_weight_cache_plan,
build_host_weight_cache_plan,
)
loader_inputs = {
@@ -602,13 +603,13 @@ class DiffusersPipelineLoader:
for module in model.modules()
):
runtime_quantization = "module_quant_method"
result = build_runtime_weight_cache_plan(
result = build_host_weight_cache_plan(
model,
dit_modules=tuple(zip(modules.dit_names, modules.dits)),
loader_type=type(self),
cache_root=getattr(self.od_config, "dlo_runtime_cache_dir", None),
cache_root=getattr(self.od_config, "dlo_host_weight_cache_dir", None),
lock_timeout_seconds=float(
getattr(self.od_config, "dlo_runtime_cache_lock_timeout", DEFAULT_LOCK_TIMEOUT_SECONDS)
getattr(self.od_config, "dlo_host_weight_cache_lock_timeout", DEFAULT_LOCK_TIMEOUT_SECONDS)
),
max_shard_bytes=DEFAULT_SHARD_SIZE_BYTES,
model_identity=str(model_identity) if model_identity is not None else None,
@@ -628,7 +629,7 @@ class DiffusersPipelineLoader:
if result.plan is not None:
self.host_weight_plan = result.plan
logger.info(
"DLO runtime cache selected (layout=%s, TP=%d/%d, SP=%d)",
"DLO host weight cache selected (layout=%s, TP=%d/%d, SP=%d)",
result.plan.runtime_layout_key[:12] if result.plan.runtime_layout_key else "unknown",
tp_rank,
tp_size,
@@ -636,7 +637,7 @@ class DiffusersPipelineLoader:
)
else:
logger.warning(
"DLO runtime cache unavailable [%s]; retaining ordinary host tensors: %s",
"DLO host weight cache unavailable [%s]; retaining ordinary host tensors: %s",
result.fallback_code or "unknown",
result.fallback_reason or "unspecified reason",
)
@@ -1,12 +1,12 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Build node-local runtime-weight cache plans for diffusion DLO.
"""Build node-local host weight cache plans for diffusion DLO.
``build_runtime_weight_cache_plan`` is the module's single entry point. It
``build_host_weight_cache_plan`` is the module's single entry point. It
validates final ordinary-loader DiT tensors, joins or atomically publishes an
immutable content-addressed entry, validates the mmap result, and returns a
fail-closed host-weight plan. Reusable identity, hashing, locking, and file
primitives live in :mod:`runtime_cache_utils`.
primitives live in :mod:`host_weight_cache_utils`.
"""
from __future__ import annotations
@@ -30,14 +30,9 @@ from safetensors.torch import save_file
from torch import nn
from vllm.logger import init_logger
from vllm_omni.diffusion.model_loader.host_weight_plan import (
HostWeightPlan,
HostWeightPlanResult,
TensorBinding,
)
from vllm_omni.diffusion.model_loader.runtime_cache_utils import (
from vllm_omni.diffusion.model_loader.host_weight_cache_utils import (
HostWeightCacheError,
IdentityNormalizationError,
RuntimeCacheError,
RuntimeTensor,
canonical_json,
canonicalize_existing_local_path,
@@ -52,15 +47,20 @@ from vllm_omni.diffusion.model_loader.runtime_cache_utils import (
split_shards,
tensor_metadata,
)
from vllm_omni.diffusion.model_loader.host_weight_plan import (
HostWeightPlan,
HostWeightPlanResult,
TensorBinding,
)
logger = init_logger(__name__)
# Internal cache-format and I/O defaults, not environment variables. Public
# configuration may override the root and lock timeout; chunk sizes stay private.
# Bump the schema to isolate incompatible manifest layouts.
RUNTIME_CACHE_SCHEMA_VERSION = 1
HOST_WEIGHT_CACHE_SCHEMA_VERSION = 1
# Keep the default shared across local stage replicas.
DEFAULT_RUNTIME_CACHE_ROOT = "~/.cache/vllm-omni/dlo-runtime-weights"
DEFAULT_HOST_WEIGHT_CACHE_ROOT = "~/.cache/vllm-omni/dlo-host-weights"
# Bound waits for another publisher instead of hanging initialization forever.
DEFAULT_LOCK_TIMEOUT_SECONDS = 600.0
# Avoid one model-sized output file and bound hashing/read working sets.
@@ -83,9 +83,9 @@ for _dtype_name in ("float8_e4m3fn", "float8_e5m2"):
_SUPPORTED_DTYPES.add(_dtype)
def default_runtime_cache_root() -> str:
def default_host_weight_cache_root() -> str:
"""Return the default root, intentionally independent of VLLM_CACHE_ROOT."""
return os.path.expanduser(DEFAULT_RUNTIME_CACHE_ROOT)
return os.path.expanduser(DEFAULT_HOST_WEIGHT_CACHE_ROOT)
def _persistent_buffer(module: nn.Module, local_name: str) -> bool:
@@ -135,7 +135,7 @@ def _collect_runtime_tensors(
candidate = RuntimeTensor(runtime_name, tensor, "parameter")
existing = records.get(runtime_name)
if existing is not None and existing.tensor is not tensor:
raise RuntimeCacheError(
raise HostWeightCacheError(
"ambiguous_ownership",
f"multiple DiT tensors resolve to {runtime_name!r}",
)
@@ -147,14 +147,14 @@ def _collect_runtime_tensors(
candidate = RuntimeTensor(runtime_name, tensor, "buffer")
existing = records.get(runtime_name)
if existing is not None and existing.tensor is not tensor:
raise RuntimeCacheError(
raise HostWeightCacheError(
"ambiguous_ownership",
f"multiple DiT tensors resolve to {runtime_name!r}",
)
records[runtime_name] = candidate
if not records:
raise RuntimeCacheError(
raise HostWeightCacheError(
"ambiguous_ownership",
"no DiT parameters or persistent buffers were discovered",
)
@@ -165,37 +165,37 @@ def _collect_runtime_tensors(
try:
pipeline_tensor = _resolve_pipeline_tensor(pipeline, record.name)
except AttributeError as exc:
raise RuntimeCacheError(
raise HostWeightCacheError(
"ambiguous_ownership",
f"DiT tensor {record.name!r} is not owned by the pipeline",
) from exc
if pipeline_tensor is not tensor:
raise RuntimeCacheError(
raise HostWeightCacheError(
"ambiguous_ownership",
f"DiT tensor {record.name!r} does not resolve to the discovered object",
)
if tensor.device.type != "cpu" or tensor.is_meta:
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_tensor",
f"{record.name!r} must be a materialized CPU tensor, got {tensor.device}",
)
if tensor.layout != torch.strided:
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_tensor",
f"{record.name!r} uses unsupported layout {tensor.layout}",
)
if not tensor.is_contiguous():
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_tensor",
f"{record.name!r} is non-contiguous with stride {tensor.stride()}",
)
if tensor.dtype not in _SUPPORTED_DTYPES:
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_tensor",
f"{record.name!r} uses unsupported dtype {tensor.dtype}",
)
if hasattr(tensor, "to_local"):
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_tensor",
f"{record.name!r} is a distributed tensor",
)
@@ -203,7 +203,7 @@ def _collect_runtime_tensors(
tensor_nbytes = tensor.numel() * tensor.element_size()
storage = tensor.untyped_storage()
if tensor.storage_offset() != 0 or storage.nbytes() != tensor_nbytes:
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_tensor",
f"{record.name!r} is a view into a larger storage",
)
@@ -211,7 +211,7 @@ def _collect_runtime_tensors(
storage_id = (storage.data_ptr(), storage.nbytes())
owner = storage_owners.setdefault(storage_id, record.name)
if owner != record.name:
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_alias",
f"{record.name!r} shares storage with {owner!r}",
)
@@ -228,7 +228,7 @@ def _collect_runtime_tensors(
storage = tensor.untyped_storage()
owner = storage_owners.get((storage.data_ptr(), storage.nbytes()))
if owner is not None and owner != pipeline_name:
raise RuntimeCacheError(
raise HostWeightCacheError(
"unsupported_alias",
f"cached tensor {owner!r} shares storage with pipeline tensor {pipeline_name!r}",
)
@@ -250,9 +250,9 @@ def _publish_entry(
free_bytes = shutil.disk_usage(cache_root).free
required_bytes = total_nbytes + max(64 * 1024**2, total_nbytes // 100)
if free_bytes < required_bytes:
raise RuntimeCacheError(
raise HostWeightCacheError(
"insufficient_disk",
f"runtime cache needs {required_bytes} bytes but only {free_bytes} are free under {cache_root}",
f"host weight cache needs {required_bytes} bytes but only {free_bytes} are free under {cache_root}",
)
tmp_root = cache_root / ".tmp"
@@ -270,7 +270,7 @@ def _publish_entry(
save_file(
{record.name: record.tensor.detach() for record in shard},
str(path),
metadata={"format": "pt", "dlo_runtime_cache_schema": str(RUNTIME_CACHE_SCHEMA_VERSION)},
metadata={"format": "pt", "dlo_host_weight_cache_schema": str(HOST_WEIGHT_CACHE_SCHEMA_VERSION)},
)
fsync_file(path)
file_manifest[filename] = {
@@ -285,7 +285,7 @@ def _publish_entry(
}
manifest = {
"schema_version": RUNTIME_CACHE_SCHEMA_VERSION,
"schema_version": HOST_WEIGHT_CACHE_SCHEMA_VERSION,
"cache_key": cache_key,
"layout_identity": layout_identity,
"runtime_content_sha256": content_digest,
@@ -328,35 +328,35 @@ def _validate_entry(
with manifest_path.open(encoding="utf-8") as handle:
manifest = json.load(handle)
except (OSError, ValueError) as exc:
raise RuntimeCacheError("entry_rejected", f"cannot read {manifest_path}: {exc}") from exc
raise HostWeightCacheError("entry_rejected", f"cannot read {manifest_path}: {exc}") from exc
if manifest.get("schema_version") != RUNTIME_CACHE_SCHEMA_VERSION:
raise RuntimeCacheError("entry_rejected", "runtime-cache schema mismatch")
if manifest.get("schema_version") != HOST_WEIGHT_CACHE_SCHEMA_VERSION:
raise HostWeightCacheError("entry_rejected", "host weight cache schema mismatch")
if manifest.get("cache_key") != cache_key:
raise RuntimeCacheError("entry_rejected", "runtime-cache key mismatch")
raise HostWeightCacheError("entry_rejected", "host weight cache key mismatch")
if manifest.get("layout_identity") != layout_identity:
raise RuntimeCacheError("entry_rejected", "runtime-cache layout identity mismatch")
raise HostWeightCacheError("entry_rejected", "host weight cache layout identity mismatch")
if manifest.get("runtime_content_sha256") != content_digest:
raise RuntimeCacheError("entry_rejected", "runtime-cache content identity mismatch")
raise HostWeightCacheError("entry_rejected", "host weight cache content identity mismatch")
expected_metadata = {record.name: tensor_metadata(record) for record in expected_records}
tensor_manifest = manifest.get("tensors")
file_manifest = manifest.get("files")
if not isinstance(tensor_manifest, dict) or not isinstance(file_manifest, dict):
raise RuntimeCacheError("entry_rejected", "runtime-cache manifest has invalid sections")
raise HostWeightCacheError("entry_rejected", "host weight cache manifest has invalid sections")
if set(tensor_manifest) != set(expected_metadata):
raise RuntimeCacheError("entry_rejected", "runtime-cache tensor names differ from the loaded DiT")
raise HostWeightCacheError("entry_rejected", "host weight cache tensor names differ from the loaded DiT")
for filename, metadata in file_manifest.items():
if Path(filename).name != filename or not isinstance(metadata, dict):
raise RuntimeCacheError("entry_rejected", f"invalid cache filename {filename!r}")
raise HostWeightCacheError("entry_rejected", f"invalid cache filename {filename!r}")
path = entry_dir / filename
try:
stat = path.stat()
except OSError as exc:
raise RuntimeCacheError("entry_rejected", f"missing cache shard {path}: {exc}") from exc
raise HostWeightCacheError("entry_rejected", f"missing cache shard {path}: {exc}") from exc
if stat.st_size != metadata.get("size") or file_digest(path) != metadata.get("sha256"):
raise RuntimeCacheError("entry_rejected", f"content digest mismatch for cache shard {path}")
raise HostWeightCacheError("entry_rejected", f"content digest mismatch for cache shard {path}")
bindings: dict[str, TensorBinding] = {}
mapped_records: list[RuntimeTensor] = []
@@ -369,43 +369,43 @@ def _validate_entry(
for record in expected_records:
stored = tensor_manifest.get(record.name)
if not isinstance(stored, dict):
raise RuntimeCacheError("entry_rejected", f"invalid tensor metadata for {record.name!r}")
raise HostWeightCacheError("entry_rejected", f"invalid tensor metadata for {record.name!r}")
filename = stored.get("file")
storage_key = stored.get("storage_key")
if filename not in handles or not isinstance(storage_key, str):
raise RuntimeCacheError("entry_rejected", f"invalid storage binding for {record.name!r}")
raise HostWeightCacheError("entry_rejected", f"invalid storage binding for {record.name!r}")
actual_metadata = {key: stored.get(key) for key in expected_metadata[record.name]}
if actual_metadata != expected_metadata[record.name]:
raise RuntimeCacheError("entry_rejected", f"metadata mismatch for {record.name!r}")
raise HostWeightCacheError("entry_rejected", f"metadata mismatch for {record.name!r}")
expected_keys_by_file[filename].add(storage_key)
try:
tensor = handles[filename].get_tensor(storage_key)
except Exception as exc:
raise RuntimeCacheError(
raise HostWeightCacheError(
"entry_rejected",
f"cannot map {record.name!r} from {filename}: {exc}",
) from exc
if tuple(tensor.shape) != tuple(record.tensor.shape) or tensor.dtype != record.tensor.dtype:
raise RuntimeCacheError("entry_rejected", f"mapped metadata mismatch for {record.name!r}")
raise HostWeightCacheError("entry_rejected", f"mapped metadata mismatch for {record.name!r}")
mapped_records.append(RuntimeTensor(record.name, tensor, record.kind))
bindings[record.name] = TensorBinding(storage_key=storage_key, file_path=str(entry_dir / filename))
for filename, expected_keys in expected_keys_by_file.items():
if set(handles[filename].keys()) != expected_keys:
raise RuntimeCacheError("entry_rejected", f"unexpected tensor keys in cache shard {filename}")
raise HostWeightCacheError("entry_rejected", f"unexpected tensor keys in cache shard {filename}")
if runtime_content_digest(mapped_records) != content_digest:
raise RuntimeCacheError("entry_rejected", "mapped runtime tensor content does not match the publisher")
raise HostWeightCacheError("entry_rejected", "mapped runtime tensor content does not match the publisher")
return HostWeightPlan(
backing_kind="runtime_cache",
backing_kind="host_weight_cache",
bindings=bindings,
runtime_layout_key=cache_key,
post_load_complete=True,
)
def build_runtime_weight_cache_plan(
def build_host_weight_cache_plan(
pipeline: nn.Module,
*,
dit_modules: Sequence[tuple[str, nn.Module]],
@@ -427,7 +427,7 @@ def build_runtime_weight_cache_plan(
cfg_parallel_size: int,
pipeline_parallel_size: int,
) -> HostWeightPlanResult:
"""Build the runtime-cache host-weight plan after ordinary model loading.
"""Build a host-weight plan from the host weight cache after ordinary loading.
This is the module's main entry point. It first rejects unsupported runtime
layouts, then discovers and validates the final CPU DiT tensors and derives
@@ -448,33 +448,37 @@ def build_runtime_weight_cache_plan(
"unsupported_load_format",
)
if use_hsdp:
return HostWeightPlanResult(None, "HSDP/DTensor layouts are not runtime-cache compatible", "unsupported_hsdp")
return HostWeightPlanResult(
None, "HSDP/DTensor layouts are not host weight cache compatible", "unsupported_hsdp"
)
if enable_expert_parallel:
return HostWeightPlanResult(
None,
"expert-parallel weight ownership is outside runtime-cache v1",
"expert-parallel weight ownership is outside host weight cache v1",
"unsupported_expert_parallel",
)
if quantization_config is not None:
return HostWeightPlanResult(
None,
"quantized runtime layouts are not supported by the first runtime-cache version",
"quantized runtime layouts are not supported by the first host weight cache version",
"unsupported_quantization",
)
if cfg_parallel_size != 1:
return HostWeightPlanResult(None, "CFG-parallel component ownership is not proven", "unsupported_cfg_parallel")
if pipeline_parallel_size != 1:
return HostWeightPlanResult(None, "pipeline-parallel ownership is outside runtime-cache v1", "unsupported_pp")
return HostWeightPlanResult(
None, "pipeline-parallel ownership is outside host weight cache v1", "unsupported_pp"
)
if not math.isfinite(lock_timeout_seconds) or lock_timeout_seconds <= 0:
return HostWeightPlanResult(None, "runtime-cache lock timeout must be positive", "invalid_configuration")
return HostWeightPlanResult(None, "host weight cache lock timeout must be positive", "invalid_configuration")
if max_shard_bytes <= 0:
return HostWeightPlanResult(None, "runtime-cache shard size must be positive", "invalid_configuration")
return HostWeightPlanResult(None, "host weight cache shard size must be positive", "invalid_configuration")
try:
records = _collect_runtime_tensors(pipeline, dit_modules)
content_digest = runtime_content_digest(records)
layout_identity = {
"schema_version": RUNTIME_CACHE_SCHEMA_VERSION,
"schema_version": HOST_WEIGHT_CACHE_SCHEMA_VERSION,
"model": (canonicalize_existing_local_path(model_identity) if model_identity is not None else None),
"revision": revision,
"runtime_dtype": str(runtime_dtype),
@@ -488,8 +492,8 @@ def build_runtime_weight_cache_plan(
"runtime_content_sha256": content_digest,
}
cache_key = hashlib.sha256(canonical_json(layout_identity)).hexdigest()
root = Path(cache_root or default_runtime_cache_root()).expanduser().resolve()
entries_root = root / f"v{RUNTIME_CACHE_SCHEMA_VERSION}"
root = Path(cache_root or default_host_weight_cache_root()).expanduser().resolve()
entries_root = root / f"v{HOST_WEIGHT_CACHE_SCHEMA_VERSION}"
entry_dir = entries_root / cache_key
entries_root.mkdir(parents=True, exist_ok=True)
@@ -502,10 +506,10 @@ def build_runtime_weight_cache_plan(
content_digest=content_digest,
expected_records=records,
)
logger.info("Reusing DLO runtime cache entry %s from %s", cache_key[:12], entry_dir)
logger.info("Reusing DLO host weight cache entry %s from %s", cache_key[:12], entry_dir)
return HostWeightPlanResult(plan)
except RuntimeCacheError as exc:
logger.warning("DLO runtime cache entry %s will be rebuilt: %s", cache_key[:12], exc)
except HostWeightCacheError as exc:
logger.warning("DLO host weight cache entry %s will be rebuilt: %s", cache_key[:12], exc)
lock_path = root / ".locks" / f"{cache_key}.lock"
with exclusive_lock(lock_path, lock_timeout_seconds):
@@ -519,9 +523,9 @@ def build_runtime_weight_cache_plan(
content_digest=content_digest,
expected_records=records,
)
logger.info("Joined DLO runtime cache entry %s from %s", cache_key[:12], entry_dir)
logger.info("Joined DLO host weight cache entry %s from %s", cache_key[:12], entry_dir)
return HostWeightPlanResult(plan)
except RuntimeCacheError:
except HostWeightCacheError:
pass
_publish_entry(
@@ -541,25 +545,25 @@ def build_runtime_weight_cache_plan(
expected_records=records,
)
logger.info(
"Published DLO runtime cache entry %s with %d tensors at %s",
"Published DLO host weight cache entry %s with %d tensors at %s",
cache_key[:12],
len(records),
entry_dir,
)
return HostWeightPlanResult(plan)
except RuntimeCacheError as exc:
except HostWeightCacheError as exc:
return HostWeightPlanResult(None, str(exc), exc.code)
except IdentityNormalizationError as exc:
return HostWeightPlanResult(None, str(exc), "unstable_identity")
except Exception as exc:
return HostWeightPlanResult(None, f"runtime-cache operation failed: {exc}", "cache_operation_failed")
return HostWeightPlanResult(None, f"host weight cache operation failed: {exc}", "cache_operation_failed")
__all__ = [
"DEFAULT_LOCK_TIMEOUT_SECONDS",
"DEFAULT_RUNTIME_CACHE_ROOT",
"DEFAULT_HOST_WEIGHT_CACHE_ROOT",
"DEFAULT_SHARD_SIZE_BYTES",
"RUNTIME_CACHE_SCHEMA_VERSION",
"build_runtime_weight_cache_plan",
"default_runtime_cache_root",
"HOST_WEIGHT_CACHE_SCHEMA_VERSION",
"build_host_weight_cache_plan",
"default_host_weight_cache_root",
]
@@ -1,6 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
"""Reusable identity, hashing, and file helpers for the DLO runtime cache."""
"""Reusable identity, hashing, and file helpers for the DLO host weight cache."""
from __future__ import annotations
@@ -43,7 +43,7 @@ class RuntimeTensor:
kind: str
class RuntimeCacheError(RuntimeError):
class HostWeightCacheError(RuntimeError):
"""Expected cache failure that should fall back with a stable code."""
def __init__(self, code: str, message: str):
@@ -102,7 +102,7 @@ def normalize_identity(value: object) -> JsonValue:
normalized: dict[str, JsonValue] = {}
for key, item in value.items():
if not isinstance(key, str):
raise IdentityNormalizationError("runtime-cache identity mappings require string keys")
raise IdentityNormalizationError("host weight cache identity mappings require string keys")
normalized[key] = normalize_identity(item)
return {key: normalized[key] for key in sorted(normalized)}
if isinstance(value, (set, frozenset)):
@@ -110,7 +110,9 @@ def normalize_identity(value: object) -> JsonValue:
if isinstance(value, (list, tuple)):
return [normalize_identity(item) for item in value]
raise IdentityNormalizationError(f"runtime-cache identity does not support values of type {_type_identity(value)}")
raise IdentityNormalizationError(
f"host weight cache identity does not support values of type {_type_identity(value)}"
)
def canonical_json(value: object) -> bytes:
@@ -203,12 +205,12 @@ def exclusive_lock(path: Path, timeout_seconds: float) -> Iterator[None]:
break
except OSError as exc:
if exc.errno not in (errno.EACCES, errno.EAGAIN):
raise RuntimeCacheError("lock_failed", f"failed to lock {path}: {exc}") from exc
raise HostWeightCacheError("lock_failed", f"failed to lock {path}: {exc}") from exc
remaining = deadline - time.monotonic()
if remaining <= 0:
raise RuntimeCacheError(
raise HostWeightCacheError(
"lock_timeout",
f"timed out after {timeout_seconds:g}s waiting for runtime-cache writer {path.name}",
f"timed out after {timeout_seconds:g}s waiting for host weight cache writer {path.name}",
) from exc
time.sleep(min(0.2, remaining))
try:
@@ -268,7 +270,7 @@ def remove_stale_temps(cache_root: Path, cache_key: str) -> None:
__all__ = [
"IdentityNormalizationError",
"JsonValue",
"RuntimeCacheError",
"HostWeightCacheError",
"RuntimeTensor",
"canonical_json",
"canonicalize_existing_local_path",
+2 -2
View File
@@ -72,7 +72,7 @@ def get_offload_backend(
od_config: OmniDiffusionConfig with offload settings
device: Target device (auto-detected if None)
host_weight_plan: Exact loader-produced backing plan. Checkpoint plans
may have skipped ordinary materialization; runtime-cache plans
may have skipped ordinary materialization; host weight cache plans
describe already-valid ordinary tensors that can be remapped.
Returns:
@@ -92,7 +92,7 @@ def get_offload_backend(
raise RuntimeError(
"A loader-owned DLO host-weight plan was produced, but distributed layerwise offload is not selected"
)
logger.warning("Ignoring optional runtime-cache plan because distributed layerwise offload is not selected")
logger.warning("Ignoring optional host weight cache plan because distributed layerwise offload is not selected")
host_weight_plan = None
# Return None if no offloading requested
+4 -4
View File
@@ -31,9 +31,9 @@ class OffloadConfig:
# blocks from the loader-selected host backing with H2D only.
dlo_use_allgather: bool = True
dlo_resident_layers: int = 0 # leading DiT layers kept on device
# Per-worker budget for registering shared runtime-cache mappings with CUDA.
# Per-worker budget for registering shared host weight cache mappings with CUDA.
# Zero retains bounded host staging.
dlo_runtime_cache_pin_limit_gib: float = 0.0
dlo_host_weight_cache_pin_limit_gib: float = 0.0
@classmethod
def from_od_config(cls, od_config: OmniDiffusionConfig) -> "OffloadConfig":
@@ -104,7 +104,7 @@ class OffloadConfig:
# requirements (concurrent requests, dummy run skip).
dlo_use_allgather = getattr(od_config, "dlo_use_allgather", True)
dlo_resident_layers = int(getattr(od_config, "dlo_resident_layers", 0))
dlo_runtime_cache_pin_limit_gib = float(getattr(od_config, "dlo_runtime_cache_pin_limit_gib", 0.0))
dlo_host_weight_cache_pin_limit_gib = float(getattr(od_config, "dlo_host_weight_cache_pin_limit_gib", 0.0))
if dlo_resident_layers < 0:
raise ValueError(f"dlo_resident_layers must be >= 0, got {dlo_resident_layers}")
if dlo_resident_layers and dlo_use_allgather:
@@ -140,7 +140,7 @@ class OffloadConfig:
dp_size=dp_size,
dlo_use_allgather=dlo_use_allgather,
dlo_resident_layers=dlo_resident_layers,
dlo_runtime_cache_pin_limit_gib=dlo_runtime_cache_pin_limit_gib,
dlo_host_weight_cache_pin_limit_gib=dlo_host_weight_cache_pin_limit_gib,
)
@@ -966,7 +966,7 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
self._using_registered_mmap = False
self.host_weight_plan = host_weight_plan
self._mmap_transforms_by_tensor_id: dict[int, Any] = {}
self._runtime_cache_mapped_sources: dict[str, list[torch.Tensor]] = {}
self._host_weight_cache_mapped_sources: dict[str, list[torch.Tensor]] = {}
self._host_registration: HostRegistration | None = None
def load_resident_layers(self) -> None:
@@ -1168,7 +1168,7 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
f"(first 5: {remaining_meta[:5]})."
)
def _load_weights_from_runtime_cache(
def _load_weights_from_host_weight_cache(
self,
pipeline: nn.Module,
modules,
@@ -1178,9 +1178,9 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
from safetensors import safe_open
if not plan.post_load_complete:
raise RuntimeError("A runtime-cache plan must describe fully post-processed weights")
raise RuntimeError("A host weight cache plan must describe fully post-processed weights")
if any(binding.transform is not None for binding in plan.bindings.values()):
raise RuntimeError("Runtime-cache bindings cannot carry deferred checkpoint transforms")
raise RuntimeError("Host weight cache bindings cannot carry deferred checkpoint transforms")
required_names: set[str] = set()
for dit_name, dit_module in zip(modules.dit_names, modules.dits):
@@ -1194,7 +1194,7 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
missing = sorted(required_names - set(plan.bindings))
unexpected = sorted(set(plan.bindings) - required_names)
raise RuntimeError(
"Runtime-cache plan coverage changed after loader validation: "
"Host weight cache plan coverage changed after loader validation: "
f"missing={missing[:5]}, unexpected={unexpected[:5]}"
)
@@ -1209,16 +1209,16 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
try:
parent = pipeline.get_submodule(parent_path)
except AttributeError as exc:
raise RuntimeError(f"Runtime-cache target module {parent_path!r} no longer exists") from exc
raise RuntimeError(f"Host weight cache target module {parent_path!r} no longer exists") from exc
target = parent._parameters.get(leaf_name)
if target is None:
target = parent._buffers.get(leaf_name)
if target is None:
raise RuntimeError(f"Runtime-cache target tensor {runtime_name!r} no longer exists")
raise RuntimeError(f"Host weight cache target tensor {runtime_name!r} no longer exists")
if target.device.type != "cpu" or target.is_meta:
raise RuntimeError(
f"Runtime-cache target {runtime_name!r} is not an ordinary CPU tensor: {target.device}"
f"Host weight cache target {runtime_name!r} is not an ordinary CPU tensor: {target.device}"
)
if binding.file_path not in file_cache:
@@ -1230,7 +1230,7 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
tensor = file_cache[binding.file_path].get_tensor(binding.storage_key)
if tensor.shape != target.shape or tensor.dtype != target.dtype:
raise RuntimeError(
f"Runtime-cache metadata changed for {runtime_name!r}: "
f"Host weight cache metadata changed for {runtime_name!r}: "
f"cache={tuple(tensor.shape)}/{tensor.dtype}, "
f"runtime={tuple(target.shape)}/{target.dtype}"
)
@@ -1248,48 +1248,50 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
self._mmap_transforms_by_tensor_id.clear()
self._mmap_file_cache = file_cache
self._runtime_cache_mapped_sources = mapped_sources
self._host_weight_cache_mapped_sources = mapped_sources
logger.info(
"Remapped %d final DiT tensors from runtime cache %s",
"Remapped %d final DiT tensors from host weight cache %s",
len(replacements),
plan.runtime_layout_key[:12] if plan.runtime_layout_key else "unknown",
)
def _try_register_runtime_cache_mmap(self) -> bool:
def _try_register_host_weight_cache_mmap(self) -> bool:
"""Register complete cache mappings when the operator budget permits."""
pin_limit_gib = self.config.dlo_runtime_cache_pin_limit_gib
pin_limit_gib = self.config.dlo_host_weight_cache_pin_limit_gib
if pin_limit_gib <= 0:
return False
if not self.config.pin_cpu_memory:
logger.warning("DLO runtime-cache registration requires pin_cpu_memory=True; using bounded host staging")
logger.warning(
"DLO host weight cache registration requires pin_cpu_memory=True; using bounded host staging"
)
return False
if not self._runtime_cache_mapped_sources:
logger.warning("DLO runtime-cache registration found no mapped sources; using bounded host staging")
if not self._host_weight_cache_mapped_sources:
logger.warning("DLO host weight cache registration found no mapped sources; using bounded host staging")
return False
max_bytes = int(pin_limit_gib * 1024**3)
started = time.perf_counter()
try:
registration = register_host_mappings(
self._runtime_cache_mapped_sources,
self._host_weight_cache_mapped_sources,
device=self.device,
max_bytes=max_bytes,
)
except HostRegistrationCleanupError:
# Falling back could unmap a range the platform still owns. Abort
# the worker so process/context teardown provides final cleanup.
logger.exception("DLO runtime-cache registration rollback failed")
logger.exception("DLO host weight cache registration rollback failed")
raise
except HostRegistrationError as exc:
logger.warning(
"DLO runtime-cache direct H2D unavailable (%s); using bounded host staging",
"DLO host weight cache direct H2D unavailable (%s); using bounded host staging",
exc,
)
return False
self._host_registration = registration
logger.info(
"Registered %.2f GiB of shared runtime-cache mappings in %d range(s) for direct H2D in %.3f s",
"Registered %.2f GiB of shared host weight cache mappings in %d range(s) for direct H2D in %.3f s",
registration.total_bytes / 1024**3,
registration.region_count,
time.perf_counter() - started,
@@ -1303,12 +1305,12 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
errors = registration.close()
if errors:
logger.error(
"DLO runtime-cache host unregistration failed; retaining mmap handles for retry/process exit: %s",
"DLO host weight cache unregistration failed; retaining mmap handles for retry/process exit: %s",
errors[:3],
)
return False
self._host_registration = None
logger.info("Unregistered DLO runtime-cache host mappings")
logger.info("Unregistered DLO host weight cache mappings")
return True
def _init_dp_group(self) -> None:
@@ -1657,11 +1659,11 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
modules,
self.host_weight_plan,
)
elif self.host_weight_plan.backing_kind == "runtime_cache":
elif self.host_weight_plan.backing_kind == "host_weight_cache":
if self.config.dlo_use_allgather:
raise ValueError("Runtime-cache backing is no-AllGather only in the first implementation")
raise ValueError("Host weight cache backing is no-AllGather only in the first implementation")
self._using_rank_local_mmap = True
self._load_weights_from_runtime_cache(
self._load_weights_from_host_weight_cache(
pipeline,
modules,
self.host_weight_plan,
@@ -1847,9 +1849,13 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
if not self._all_hook_groups:
self.enabled = bool(self._resident_blocks)
if self.enabled and self._using_rank_local_mmap and self.host_weight_plan.backing_kind == "runtime_cache":
self._using_registered_mmap = self._try_register_runtime_cache_mmap()
self._runtime_cache_mapped_sources.clear()
if (
self.enabled
and self._using_rank_local_mmap
and self.host_weight_plan.backing_kind == "host_weight_cache"
):
self._using_registered_mmap = self._try_register_host_weight_cache_mmap()
self._host_weight_cache_mapped_sources.clear()
assert self._resident_layer_group is not None
self._resident_layer_group.registered_mmap = self._using_registered_mmap
if self._using_registered_mmap:
@@ -1870,9 +1876,9 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
if self.dp_size > 1:
unified_shard_buffers = self._allocate_shared_shard_buffers(all_hooks)
if self._using_rank_local_mmap and self.host_weight_plan.backing_kind == "runtime_cache":
self._using_registered_mmap = self._try_register_runtime_cache_mmap()
self._runtime_cache_mapped_sources.clear()
if self._using_rank_local_mmap and self.host_weight_plan.backing_kind == "host_weight_cache":
self._using_registered_mmap = self._try_register_host_weight_cache_mmap()
self._host_weight_cache_mapped_sources.clear()
for hook in all_hooks:
hook.registered_mmap = self._using_registered_mmap
if self._resident_layer_group is not None:
@@ -1962,7 +1968,7 @@ class DistributedLayerwiseOffloadBackend(OffloadBackend):
def _release_mmap_handles(self) -> None:
"""Release safetensors mmap file handles."""
self._mmap_transforms_by_tensor_id.clear()
self._runtime_cache_mapped_sources.clear()
self._host_weight_cache_mapped_sources.clear()
if hasattr(self, "_mmap_file_cache"):
self._mmap_file_cache.clear()
del self._mmap_file_cache