[Core] Add Host Weight Runtime foundation (#6419)
Signed-off-by: Hongsheng Liu <liuhongsheng4@huawei.com>
This commit is contained in:
1 parent
5730af0a95
commit
73b623f2f7
21 files changed
+6108
-6
No files matched your search
@@ -0,0 +1,139 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Neutral contracts for immutable runtime-ready host weight artifacts."""
|
||||
|
||||
from .config import (
|
||||
CapacityPolicy,
|
||||
HostWeightRuntimeConfig,
|
||||
IntegrityPolicy,
|
||||
ProductionPolicy,
|
||||
RemoteImportPolicy,
|
||||
RemoteOnMiss,
|
||||
RuntimeMode,
|
||||
StorageClass,
|
||||
StorageDomainPolicy,
|
||||
StorageScope,
|
||||
ValidationLevel,
|
||||
WaitPolicy,
|
||||
)
|
||||
from .errors import FailureCode, HostWeightError, HostWeightFailure, ResolutionAction, ResolutionStage
|
||||
from .identity import (
|
||||
AdaptationIdentity,
|
||||
CanonicalJson,
|
||||
ComponentIdentity,
|
||||
IdentityValidationError,
|
||||
ProducerIdentity,
|
||||
RuntimeWeightLayout,
|
||||
WeightArtifactIdentity,
|
||||
WeightRepresentation,
|
||||
WeightSourceIdentity,
|
||||
)
|
||||
from .inspection import (
|
||||
ArtifactInspection,
|
||||
ArtifactInventoryEntry,
|
||||
ArtifactInventoryState,
|
||||
DomainInspection,
|
||||
HostWeightInspector,
|
||||
)
|
||||
from .lease import HostWeightLease, LeaseProvenance, MappedHostRegion
|
||||
from .manifest import (
|
||||
FileManifestEntry,
|
||||
ManifestValidationError,
|
||||
PublicationMarker,
|
||||
TensorKind,
|
||||
TensorManifestEntry,
|
||||
WeightManifest,
|
||||
)
|
||||
from .outcomes import (
|
||||
AttemptResult,
|
||||
HostWeightResolution,
|
||||
ResolutionAttempt,
|
||||
ResolutionOutcome,
|
||||
ResolutionReport,
|
||||
StoreResult,
|
||||
StoreStatus,
|
||||
)
|
||||
from .protocols import (
|
||||
ArtifactWriter,
|
||||
BuildRequest,
|
||||
CoordinationScope,
|
||||
HostWeightStore,
|
||||
LookupPhase,
|
||||
ProductionMetadata,
|
||||
ProductionSourceMode,
|
||||
RemoteArtifactDescriptor,
|
||||
RemoteArtifactProvider,
|
||||
TensorFileWriter,
|
||||
TensorWriteSpec,
|
||||
WeightProducer,
|
||||
WeightProductionSpec,
|
||||
WeightRestorePlan,
|
||||
WeightRestorer,
|
||||
)
|
||||
from .runtime import HostWeightRuntime
|
||||
|
||||
__all__ = [
|
||||
"AdaptationIdentity",
|
||||
"ArtifactInspection",
|
||||
"ArtifactInventoryEntry",
|
||||
"ArtifactInventoryState",
|
||||
"ArtifactWriter",
|
||||
"AttemptResult",
|
||||
"BuildRequest",
|
||||
"CanonicalJson",
|
||||
"CapacityPolicy",
|
||||
"ComponentIdentity",
|
||||
"CoordinationScope",
|
||||
"DomainInspection",
|
||||
"FailureCode",
|
||||
"FileManifestEntry",
|
||||
"HostWeightError",
|
||||
"HostWeightFailure",
|
||||
"HostWeightLease",
|
||||
"HostWeightResolution",
|
||||
"HostWeightInspector",
|
||||
"HostWeightRuntime",
|
||||
"HostWeightRuntimeConfig",
|
||||
"HostWeightStore",
|
||||
"IdentityValidationError",
|
||||
"IntegrityPolicy",
|
||||
"LeaseProvenance",
|
||||
"LookupPhase",
|
||||
"ManifestValidationError",
|
||||
"MappedHostRegion",
|
||||
"ProducerIdentity",
|
||||
"ProductionMetadata",
|
||||
"ProductionPolicy",
|
||||
"ProductionSourceMode",
|
||||
"PublicationMarker",
|
||||
"RemoteArtifactDescriptor",
|
||||
"RemoteArtifactProvider",
|
||||
"RemoteImportPolicy",
|
||||
"RemoteOnMiss",
|
||||
"ResolutionAction",
|
||||
"ResolutionAttempt",
|
||||
"ResolutionOutcome",
|
||||
"ResolutionReport",
|
||||
"ResolutionStage",
|
||||
"RuntimeMode",
|
||||
"RuntimeWeightLayout",
|
||||
"StorageClass",
|
||||
"StorageDomainPolicy",
|
||||
"StorageScope",
|
||||
"StoreResult",
|
||||
"StoreStatus",
|
||||
"TensorFileWriter",
|
||||
"TensorKind",
|
||||
"TensorManifestEntry",
|
||||
"TensorWriteSpec",
|
||||
"ValidationLevel",
|
||||
"WaitPolicy",
|
||||
"WeightArtifactIdentity",
|
||||
"WeightManifest",
|
||||
"WeightProducer",
|
||||
"WeightProductionSpec",
|
||||
"WeightRepresentation",
|
||||
"WeightRestorePlan",
|
||||
"WeightRestorer",
|
||||
"WeightSourceIdentity",
|
||||
]
|
||||
@@ -0,0 +1,183 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Typed policy for the loader-adjacent Host Weight Runtime."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class RuntimeMode(str, Enum):
|
||||
DISABLED = "disabled"
|
||||
PREFERRED = "preferred"
|
||||
REQUIRED = "required"
|
||||
|
||||
|
||||
class StorageScope(str, Enum):
|
||||
NODE = "node"
|
||||
NUMA = "numa"
|
||||
|
||||
|
||||
class StorageClass(str, Enum):
|
||||
DISK = "disk"
|
||||
TMPFS = "tmpfs"
|
||||
|
||||
|
||||
class ValidationLevel(str, Enum):
|
||||
MANIFEST_AND_METADATA = "manifest_and_metadata"
|
||||
FULL_CHECKSUM = "full_checksum"
|
||||
FS_VERITY = "fs_verity"
|
||||
|
||||
|
||||
class RemoteOnMiss(str, Enum):
|
||||
DISABLED = "disabled"
|
||||
TRY = "try"
|
||||
REQUIRE = "require"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class StorageDomainPolicy:
|
||||
root: Path
|
||||
scope: StorageScope = StorageScope.NODE
|
||||
domain_id: str = "node"
|
||||
storage_class: StorageClass = StorageClass.DISK
|
||||
numa_prefault: bool = False
|
||||
verify_numa_residency: bool = False
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.root, Path) or not str(self.root):
|
||||
raise ValueError("host weight store root must not be empty")
|
||||
if not isinstance(self.scope, StorageScope) or not isinstance(self.storage_class, StorageClass):
|
||||
raise ValueError("storage scope and class must use their typed enum values")
|
||||
if not isinstance(self.domain_id, str) or not self.domain_id:
|
||||
raise ValueError("host weight store domain_id must not be empty")
|
||||
if not isinstance(self.numa_prefault, bool) or not isinstance(self.verify_numa_residency, bool):
|
||||
raise ValueError("NUMA placement options must be booleans")
|
||||
if self.scope is StorageScope.NODE and (self.numa_prefault or self.verify_numa_residency):
|
||||
raise ValueError("NUMA placement options require scope='numa'")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CapacityPolicy:
|
||||
max_artifact_bytes: int | None = None
|
||||
max_store_bytes: int | None = None
|
||||
min_free_bytes: int = 0
|
||||
eviction: str = "none"
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
for name, value in (
|
||||
("max_artifact_bytes", self.max_artifact_bytes),
|
||||
("max_store_bytes", self.max_store_bytes),
|
||||
):
|
||||
if value is not None and (isinstance(value, bool) or not isinstance(value, int) or value <= 0):
|
||||
raise ValueError(f"{name} must be positive when configured")
|
||||
if isinstance(self.min_free_bytes, bool) or not isinstance(self.min_free_bytes, int) or self.min_free_bytes < 0:
|
||||
raise ValueError("min_free_bytes must not be negative")
|
||||
if not isinstance(self.eviction, str) or self.eviction != "none":
|
||||
raise ValueError("automatic host weight eviction is not implemented")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class IntegrityPolicy:
|
||||
local_lookup: ValidationLevel = ValidationLevel.MANIFEST_AND_METADATA
|
||||
require_trusted_permissions: bool = True
|
||||
background_scrub: bool = False
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.local_lookup, ValidationLevel):
|
||||
raise ValueError("local lookup validation must use ValidationLevel")
|
||||
if not isinstance(self.require_trusted_permissions, bool) or not isinstance(self.background_scrub, bool):
|
||||
raise ValueError("integrity switches must be booleans")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RemoteImportPolicy:
|
||||
on_local_miss: RemoteOnMiss = RemoteOnMiss.DISABLED
|
||||
providers: tuple[str, ...] = ()
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.on_local_miss, RemoteOnMiss):
|
||||
raise ValueError("remote on-miss policy must use RemoteOnMiss")
|
||||
if not isinstance(self.providers, tuple):
|
||||
raise ValueError("remote providers must be an immutable tuple")
|
||||
if self.on_local_miss is not RemoteOnMiss.DISABLED and not self.providers:
|
||||
raise ValueError("remote providers are required when remote import is enabled")
|
||||
if any(not isinstance(provider, str) or not provider for provider in self.providers):
|
||||
raise ValueError("remote provider identifiers must not be empty")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProductionPolicy:
|
||||
allow_local_build: bool = True
|
||||
allow_post_load_publish: bool = False
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.allow_local_build, bool) or not isinstance(self.allow_post_load_publish, bool):
|
||||
raise ValueError("production switches must be booleans")
|
||||
if self.allow_post_load_publish:
|
||||
raise ValueError("post-load publication is not implemented")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WaitPolicy:
|
||||
coordination_timeout_seconds: float = 600.0
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
timeout = self.coordination_timeout_seconds
|
||||
if (
|
||||
isinstance(timeout, bool)
|
||||
or not isinstance(timeout, int | float)
|
||||
or not math.isfinite(timeout)
|
||||
or timeout <= 0
|
||||
):
|
||||
raise ValueError("coordination_timeout_seconds must be positive")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HostWeightRuntimeConfig:
|
||||
mode: RuntimeMode = RuntimeMode.DISABLED
|
||||
domain: StorageDomainPolicy | None = None
|
||||
remote: RemoteImportPolicy = field(default_factory=RemoteImportPolicy)
|
||||
production: ProductionPolicy = field(default_factory=ProductionPolicy)
|
||||
capacity: CapacityPolicy = field(default_factory=CapacityPolicy)
|
||||
wait: WaitPolicy = field(default_factory=WaitPolicy)
|
||||
integrity: IntegrityPolicy = field(default_factory=IntegrityPolicy)
|
||||
schema_version: int = 1
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if isinstance(self.schema_version, bool) or self.schema_version != 1:
|
||||
raise ValueError("only Host Weight Runtime config schema_version=1 is supported")
|
||||
if not isinstance(self.mode, RuntimeMode):
|
||||
raise ValueError("runtime mode must use RuntimeMode")
|
||||
if self.domain is not None and not isinstance(self.domain, StorageDomainPolicy):
|
||||
raise ValueError("runtime domain must use StorageDomainPolicy")
|
||||
for name, value, expected in (
|
||||
("remote", self.remote, RemoteImportPolicy),
|
||||
("production", self.production, ProductionPolicy),
|
||||
("capacity", self.capacity, CapacityPolicy),
|
||||
("wait", self.wait, WaitPolicy),
|
||||
("integrity", self.integrity, IntegrityPolicy),
|
||||
):
|
||||
if not isinstance(value, expected):
|
||||
raise ValueError(f"runtime {name} policy has the wrong type")
|
||||
if self.mode is not RuntimeMode.DISABLED and self.domain is None:
|
||||
raise ValueError("enabled Host Weight Runtime requires a storage domain")
|
||||
|
||||
|
||||
__all__ = [
|
||||
"CapacityPolicy",
|
||||
"HostWeightRuntimeConfig",
|
||||
"IntegrityPolicy",
|
||||
"ProductionPolicy",
|
||||
"RemoteImportPolicy",
|
||||
"RemoteOnMiss",
|
||||
"RuntimeMode",
|
||||
"StorageClass",
|
||||
"StorageDomainPolicy",
|
||||
"StorageScope",
|
||||
"ValidationLevel",
|
||||
"WaitPolicy",
|
||||
]
|
||||
@@ -0,0 +1,106 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Stable Host Weight Runtime failures and actions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
|
||||
from .identity import CanonicalJson
|
||||
|
||||
|
||||
class ResolutionStage(str, Enum):
|
||||
CONFIGURATION = "configuration"
|
||||
DOMAIN = "domain"
|
||||
LOOKUP = "lookup"
|
||||
VALIDATION = "validation"
|
||||
REMOTE = "remote"
|
||||
PRODUCTION = "production"
|
||||
CAPACITY = "capacity"
|
||||
RESTORATION = "restoration"
|
||||
LIFECYCLE = "lifecycle"
|
||||
CANONICAL_LOADING = "canonical_loading"
|
||||
|
||||
|
||||
class FailureCode(str, Enum):
|
||||
INVALID_CONFIG = "invalid_config"
|
||||
COHORT_POLICY_MISMATCH = "cohort_policy_mismatch"
|
||||
DOMAIN_POLICY_MISMATCH = "domain_policy_mismatch"
|
||||
DOMAIN_UNAVAILABLE = "domain_unavailable"
|
||||
STORAGE_CLASS_MISMATCH = "storage_class_mismatch"
|
||||
NUMA_PLACEMENT_INVALID = "numa_placement_invalid"
|
||||
LOCAL_MISS = "local_miss"
|
||||
ARTIFACT_DENIED = "artifact_denied"
|
||||
ACTIVE_BUILD_TIMEOUT = "active_build_timeout"
|
||||
MANIFEST_INCOMPATIBLE = "manifest_incompatible"
|
||||
MANIFEST_CORRUPT = "manifest_corrupt"
|
||||
CHECKSUM_MISMATCH = "checksum_mismatch"
|
||||
IDENTITY_COLLISION = "identity_collision"
|
||||
REMOTE_MISS = "remote_miss"
|
||||
REMOTE_UNAVAILABLE = "remote_unavailable"
|
||||
REMOTE_AUTH_FAILED = "remote_auth_failed"
|
||||
REMOTE_TIMEOUT = "remote_timeout"
|
||||
REMOTE_ARTIFACT_INVALID = "remote_artifact_invalid"
|
||||
PRODUCER_UNAVAILABLE = "producer_unavailable"
|
||||
PRODUCER_UNSUPPORTED = "producer_unsupported"
|
||||
PRODUCER_FAILED = "producer_failed"
|
||||
PRODUCTION_TIMEOUT = "production_timeout"
|
||||
ARTIFACT_TOO_LARGE = "artifact_too_large"
|
||||
STORE_LIMIT_EXCEEDED = "store_limit_exceeded"
|
||||
INSUFFICIENT_CAPACITY = "insufficient_capacity"
|
||||
ENOSPC = "enospc"
|
||||
RESTORER_UNAVAILABLE = "restorer_unavailable"
|
||||
RESTORE_PREFLIGHT_FAILED = "restore_preflight_failed"
|
||||
RESTORE_COMMIT_FAILED = "restore_commit_failed"
|
||||
ACTIVE_LEASE = "active_lease"
|
||||
PUBLICATION_FAILED = "publication_failed"
|
||||
QUARANTINE_FAILED = "quarantine_failed"
|
||||
CANONICAL_SOURCE_FAILED = "canonical_source_failed"
|
||||
UNSUPPORTED = "unsupported"
|
||||
|
||||
|
||||
class ResolutionAction(str, Enum):
|
||||
RETURN_LEASE = "return_lease"
|
||||
TRY_REMOTE = "try_remote"
|
||||
TRY_PRODUCER = "try_producer"
|
||||
QUARANTINE_THEN_PRODUCE = "quarantine_then_produce"
|
||||
CANONICAL_FALLBACK = "canonical_fallback"
|
||||
FAIL_STARTUP = "fail_startup"
|
||||
SKIP = "skip"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HostWeightFailure:
|
||||
stage: ResolutionStage
|
||||
code: FailureCode
|
||||
retryable: bool
|
||||
message: str
|
||||
details: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.stage, ResolutionStage) or not isinstance(self.code, FailureCode):
|
||||
raise ValueError("host weight failure stage and code must use their typed enums")
|
||||
if not isinstance(self.retryable, bool):
|
||||
raise ValueError("host weight failure retryable must be a boolean")
|
||||
if not isinstance(self.message, str) or not self.message:
|
||||
raise ValueError("host weight failure requires a message")
|
||||
if not isinstance(self.details, CanonicalJson):
|
||||
raise ValueError("host weight failure details must use CanonicalJson")
|
||||
|
||||
|
||||
class HostWeightError(RuntimeError):
|
||||
"""Internal exception carrying a stable, policy-neutral failure."""
|
||||
|
||||
def __init__(self, failure: HostWeightFailure):
|
||||
super().__init__(failure.message)
|
||||
self.failure = failure
|
||||
|
||||
|
||||
__all__ = [
|
||||
"FailureCode",
|
||||
"HostWeightError",
|
||||
"HostWeightFailure",
|
||||
"ResolutionAction",
|
||||
"ResolutionStage",
|
||||
]
|
||||
@@ -0,0 +1,11 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Local filesystem Host Weight Store implementation."""
|
||||
|
||||
from .store import FilesystemHostWeightStore, detect_filesystem_type, detect_storage_class
|
||||
|
||||
__all__ = [
|
||||
"FilesystemHostWeightStore",
|
||||
"detect_filesystem_type",
|
||||
"detect_storage_class",
|
||||
]
|
||||
@@ -0,0 +1,115 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Stable POSIX advisory locks used by the filesystem store."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import errno
|
||||
import fcntl
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
class FileLockTimeoutError(TimeoutError):
|
||||
pass
|
||||
|
||||
|
||||
class FileLock:
|
||||
"""One held flock whose inode is stable outside artifact directories."""
|
||||
|
||||
def __init__(self, path: Path, *, exclusive: bool, deadline: float | None, nonblocking: bool = False) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
self.path = path
|
||||
self._fd = os.open(path, os.O_CREAT | os.O_RDWR | os.O_CLOEXEC | os.O_NOFOLLOW, 0o600)
|
||||
self._creator_pid = os.getpid()
|
||||
self._closed = False
|
||||
operation = fcntl.LOCK_EX if exclusive else fcntl.LOCK_SH
|
||||
if nonblocking:
|
||||
operation |= fcntl.LOCK_NB
|
||||
try:
|
||||
if nonblocking:
|
||||
self._acquire_once(operation)
|
||||
elif deadline is None:
|
||||
self._acquire_blocking(operation)
|
||||
else:
|
||||
self._acquire_until(operation | fcntl.LOCK_NB, deadline)
|
||||
except BaseException:
|
||||
os.close(self._fd)
|
||||
self._closed = True
|
||||
raise
|
||||
|
||||
def _acquire_once(self, operation: int) -> None:
|
||||
try:
|
||||
fcntl.flock(self._fd, operation)
|
||||
except OSError as exc:
|
||||
if exc.errno in (errno.EACCES, errno.EAGAIN):
|
||||
raise FileLockTimeoutError(f"lock is active: {self.path}") from exc
|
||||
raise
|
||||
|
||||
def _acquire_blocking(self, operation: int) -> None:
|
||||
while True:
|
||||
try:
|
||||
fcntl.flock(self._fd, operation)
|
||||
return
|
||||
except OSError as exc:
|
||||
if exc.errno != errno.EINTR:
|
||||
raise
|
||||
|
||||
def _acquire_until(self, operation: int, deadline: float) -> None:
|
||||
while True:
|
||||
try:
|
||||
fcntl.flock(self._fd, operation)
|
||||
return
|
||||
except OSError as exc:
|
||||
if exc.errno == errno.EINTR:
|
||||
continue
|
||||
if exc.errno not in (errno.EACCES, errno.EAGAIN):
|
||||
raise
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise FileLockTimeoutError(f"timed out waiting for {self.path}") from exc
|
||||
time.sleep(min(0.05, remaining))
|
||||
|
||||
def close(self) -> None:
|
||||
if self._closed:
|
||||
return
|
||||
self._closed = True
|
||||
if os.getpid() != self._creator_pid:
|
||||
# flock state belongs to the inherited open-file description. A
|
||||
# child must only drop its descriptor reference; LOCK_UN would
|
||||
# release the creator process's active lock as well.
|
||||
os.close(self._fd)
|
||||
return
|
||||
try:
|
||||
fcntl.flock(self._fd, fcntl.LOCK_UN)
|
||||
finally:
|
||||
os.close(self._fd)
|
||||
|
||||
def __enter__(self) -> FileLock:
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type: object, exc: object, _traceback: object) -> None:
|
||||
self.close()
|
||||
|
||||
|
||||
def lock_is_active(path: Path) -> bool:
|
||||
"""Return a point-in-time contention snapshot without creating a lock file."""
|
||||
try:
|
||||
fd = os.open(path, os.O_RDWR | os.O_CLOEXEC | os.O_NOFOLLOW)
|
||||
except FileNotFoundError:
|
||||
return False
|
||||
try:
|
||||
try:
|
||||
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except OSError as exc:
|
||||
if exc.errno in (errno.EACCES, errno.EAGAIN):
|
||||
return True
|
||||
raise
|
||||
fcntl.flock(fd, fcntl.LOCK_UN)
|
||||
return False
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
__all__ = ["FileLock", "FileLockTimeoutError", "lock_is_active"]
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,425 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Store-scoped streaming safetensors publication."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import errno
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import struct
|
||||
import uuid
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path, PurePosixPath
|
||||
from types import TracebackType
|
||||
|
||||
import torch
|
||||
from safetensors import safe_open
|
||||
|
||||
from ..identity import WeightArtifactIdentity, canonical_json
|
||||
from ..manifest import FileManifestEntry, PublicationMarker, TensorManifestEntry, WeightManifest
|
||||
from ..protocols import ProductionMetadata, TensorWriteSpec
|
||||
|
||||
_HASH_CHUNK_BYTES = 8 * 1024**2
|
||||
_MANIFEST_FILE = "manifest.json"
|
||||
_READY_FILE = "READY.json"
|
||||
|
||||
_SAFETENSORS_DTYPES: dict[torch.dtype, str] = {
|
||||
torch.bool: "BOOL",
|
||||
torch.uint8: "U8",
|
||||
torch.int8: "I8",
|
||||
torch.int16: "I16",
|
||||
torch.int32: "I32",
|
||||
torch.int64: "I64",
|
||||
torch.float16: "F16",
|
||||
torch.bfloat16: "BF16",
|
||||
torch.float32: "F32",
|
||||
torch.float64: "F64",
|
||||
}
|
||||
for _torch_name, _safe_name in (
|
||||
("float8_e4m3fn", "F8_E4M3"),
|
||||
("float8_e5m2", "F8_E5M2"),
|
||||
("float8_e4m3fnuz", "F8_E4M3FNUZ"),
|
||||
("float8_e5m2fnuz", "F8_E5M2FNUZ"),
|
||||
):
|
||||
if (_dtype := getattr(torch, _torch_name, None)) is not None:
|
||||
_SAFETENSORS_DTYPES[_dtype] = _safe_name
|
||||
|
||||
|
||||
class ArtifactWriterError(RuntimeError):
|
||||
pass
|
||||
|
||||
|
||||
def _safe_relative_name(value: str) -> str:
|
||||
path = PurePosixPath(value)
|
||||
if not value or path.is_absolute() or len(path.parts) != 1 or path.name != value or value in {".", ".."}:
|
||||
raise ArtifactWriterError(f"artifact payload name must be one safe relative basename: {value!r}")
|
||||
if not value.endswith(".safetensors"):
|
||||
raise ArtifactWriterError(f"artifact payload must use the .safetensors format: {value!r}")
|
||||
return value
|
||||
|
||||
|
||||
def _pwrite_all(fd: int, data: bytes | bytearray | memoryview, offset: int) -> None:
|
||||
view = memoryview(data).cast("B")
|
||||
written = 0
|
||||
while written < len(view):
|
||||
count = os.pwrite(fd, view[written:], offset + written)
|
||||
if count <= 0:
|
||||
raise OSError(errno.EIO, "short pwrite while publishing host weights")
|
||||
written += count
|
||||
|
||||
|
||||
def _fsync_directory(path: Path) -> None:
|
||||
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY | os.O_CLOEXEC)
|
||||
try:
|
||||
os.fsync(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
def _file_sha256(path: Path) -> str:
|
||||
digest = hashlib.sha256()
|
||||
with path.open("rb") as handle:
|
||||
while chunk := handle.read(_HASH_CHUNK_BYTES):
|
||||
digest.update(chunk)
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
def _tensor_sha256(tensor: torch.Tensor) -> str:
|
||||
digest = hashlib.sha256()
|
||||
byte_view = tensor.detach().reshape(-1).view(torch.uint8)
|
||||
for offset in range(0, byte_view.numel(), _HASH_CHUNK_BYTES):
|
||||
chunk = byte_view[offset : offset + _HASH_CHUNK_BYTES]
|
||||
digest.update(memoryview(chunk.numpy()))
|
||||
return digest.hexdigest()
|
||||
|
||||
|
||||
class _Coverage:
|
||||
def __init__(self, size: int) -> None:
|
||||
self._size = size
|
||||
self._ranges: list[tuple[int, int]] = []
|
||||
|
||||
def add(self, start: int, end: int) -> None:
|
||||
if start < 0 or end < start or end > self._size:
|
||||
raise ArtifactWriterError(f"write range [{start}, {end}) exceeds tensor storage {self._size}")
|
||||
if start == end:
|
||||
return
|
||||
for existing_start, existing_end in self._ranges:
|
||||
if start < existing_end and existing_start < end:
|
||||
raise ArtifactWriterError(f"overlapping tensor write ranges [{start}, {end}) and {self._ranges}")
|
||||
self._ranges.append((start, end))
|
||||
|
||||
def complete(self) -> bool:
|
||||
if self._size == 0:
|
||||
return True
|
||||
cursor = 0
|
||||
for start, end in sorted(self._ranges):
|
||||
if start != cursor:
|
||||
return False
|
||||
cursor = end
|
||||
return cursor == self._size
|
||||
|
||||
|
||||
class StreamingSafeTensorFile:
|
||||
"""One preallocated safetensors file filled through bounded CPU writes."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
owner: FilesystemArtifactWriter,
|
||||
relative_name: str,
|
||||
specs: tuple[TensorWriteSpec, ...],
|
||||
) -> None:
|
||||
self._owner = owner
|
||||
self.relative_name = _safe_relative_name(relative_name)
|
||||
if not specs:
|
||||
raise ArtifactWriterError("a tensor payload file must contain at least one tensor")
|
||||
by_name = {spec.name: spec for spec in specs}
|
||||
if len(by_name) != len(specs):
|
||||
raise ArtifactWriterError("tensor payload contains duplicate storage keys")
|
||||
unsupported = [spec.dtype for spec in specs if spec.dtype not in _SAFETENSORS_DTYPES]
|
||||
if unsupported:
|
||||
raise ArtifactWriterError(f"unsupported safetensors dtypes: {unsupported}")
|
||||
self.specs = tuple(sorted(specs, key=lambda spec: spec.name))
|
||||
self._specs = {spec.name: spec for spec in self.specs}
|
||||
self._coverage = {spec.name: _Coverage(spec.nbytes) for spec in self.specs}
|
||||
self._closed = False
|
||||
|
||||
offsets: dict[str, int] = {}
|
||||
cursor = 0
|
||||
for spec in self.specs:
|
||||
offsets[spec.name] = cursor
|
||||
cursor += spec.nbytes
|
||||
header: dict[str, object] = {"__metadata__": {"format": "pt", "host_weight_runtime": "1"}}
|
||||
for spec in self.specs:
|
||||
start = offsets[spec.name]
|
||||
header[spec.name] = {
|
||||
"dtype": _SAFETENSORS_DTYPES[spec.dtype],
|
||||
"shape": list(spec.shape),
|
||||
"data_offsets": [start, start + spec.nbytes],
|
||||
}
|
||||
encoded_header = json.dumps(header, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
||||
encoded_header += b" " * (-len(encoded_header) % 8)
|
||||
self._payload_offset = 8 + len(encoded_header)
|
||||
self._offsets = offsets
|
||||
self._path = owner.staging_dir / self.relative_name
|
||||
total_size = self._payload_offset + cursor
|
||||
owner._reserve_file(total_size)
|
||||
self._fd = os.open(self._path, os.O_CREAT | os.O_EXCL | os.O_RDWR | os.O_CLOEXEC, 0o600)
|
||||
try:
|
||||
if hasattr(os, "posix_fallocate"):
|
||||
try:
|
||||
os.posix_fallocate(self._fd, 0, total_size)
|
||||
except OSError as exc:
|
||||
if exc.errno not in (errno.EINVAL, errno.ENOSYS, errno.EOPNOTSUPP):
|
||||
raise
|
||||
os.ftruncate(self._fd, total_size)
|
||||
else: # pragma: no cover - Linux production path has posix_fallocate
|
||||
os.ftruncate(self._fd, total_size)
|
||||
_pwrite_all(self._fd, struct.pack("<Q", len(encoded_header)), 0)
|
||||
_pwrite_all(self._fd, encoded_header, 8)
|
||||
except BaseException:
|
||||
os.close(self._fd)
|
||||
self._path.unlink(missing_ok=True)
|
||||
owner._forget_reserved_file(total_size)
|
||||
raise
|
||||
owner._opened(self)
|
||||
|
||||
@staticmethod
|
||||
def _bytes(tensor: torch.Tensor) -> memoryview:
|
||||
if tensor.device.type != "cpu" or tensor.is_meta:
|
||||
raise ArtifactWriterError("artifact producers must provide materialized CPU tensor chunks")
|
||||
contiguous = tensor.detach().contiguous()
|
||||
return memoryview(contiguous.view(torch.uint8).numpy()).cast("B")
|
||||
|
||||
def write_tensor(self, name: str, tensor: torch.Tensor) -> None:
|
||||
spec = self._get_spec(name)
|
||||
if tuple(tensor.shape) != spec.shape or tensor.dtype != spec.dtype:
|
||||
raise ArtifactWriterError(
|
||||
f"tensor {name!r} differs from its declared shape/dtype: "
|
||||
f"got {tuple(tensor.shape)}/{tensor.dtype}, expected {spec.shape}/{spec.dtype}"
|
||||
)
|
||||
self._write(name, 0, self._bytes(tensor))
|
||||
|
||||
def write_rows(self, name: str, start_row: int, tensor: torch.Tensor) -> None:
|
||||
spec = self._get_spec(name)
|
||||
if not spec.shape:
|
||||
if start_row != 0 or tuple(tensor.shape) != ():
|
||||
raise ArtifactWriterError(f"scalar tensor {name!r} only accepts one complete scalar write")
|
||||
self.write_tensor(name, tensor)
|
||||
return
|
||||
if tensor.dtype != spec.dtype or tuple(tensor.shape[1:]) != spec.shape[1:]:
|
||||
raise ArtifactWriterError(f"row chunk for {name!r} differs from its declared dtype or trailing shape")
|
||||
row_count = tensor.shape[0]
|
||||
if start_row < 0 or start_row + row_count > spec.shape[0]:
|
||||
raise ArtifactWriterError(f"row chunk for {name!r} exceeds its declared first dimension")
|
||||
row_bytes = spec.nbytes // spec.shape[0] if spec.shape[0] else 0
|
||||
self._write(name, start_row * row_bytes, self._bytes(tensor))
|
||||
|
||||
def _get_spec(self, name: str) -> TensorWriteSpec:
|
||||
if self._closed:
|
||||
raise ArtifactWriterError("cannot write through a closed tensor file")
|
||||
try:
|
||||
return self._specs[name]
|
||||
except KeyError as exc:
|
||||
raise ArtifactWriterError(f"tensor {name!r} was not declared in this payload file") from exc
|
||||
|
||||
def _write(self, name: str, byte_offset: int, data: memoryview) -> None:
|
||||
coverage = self._coverage[name]
|
||||
coverage.add(byte_offset, byte_offset + len(data))
|
||||
_pwrite_all(self._fd, data, self._payload_offset + self._offsets[name] + byte_offset)
|
||||
|
||||
def close(self) -> None:
|
||||
if self._closed:
|
||||
return
|
||||
missing = [name for name, coverage in self._coverage.items() if not coverage.complete()]
|
||||
if missing:
|
||||
self.abort()
|
||||
raise ArtifactWriterError(f"tensor payload closed with incomplete writes: {missing}")
|
||||
self._closed = True
|
||||
try:
|
||||
os.fsync(self._fd)
|
||||
finally:
|
||||
os.close(self._fd)
|
||||
self._owner._closed(self)
|
||||
|
||||
def abort(self) -> None:
|
||||
if self._closed:
|
||||
return
|
||||
self._closed = True
|
||||
os.close(self._fd)
|
||||
size = self._path.stat().st_size if self._path.exists() else 0
|
||||
self._path.unlink(missing_ok=True)
|
||||
self._owner._aborted(self, size)
|
||||
|
||||
def __enter__(self) -> StreamingSafeTensorFile:
|
||||
return self
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc: BaseException | None,
|
||||
_traceback: TracebackType | None,
|
||||
) -> None:
|
||||
if exc_type is None:
|
||||
self.close()
|
||||
else:
|
||||
self.abort()
|
||||
|
||||
|
||||
class FilesystemArtifactWriter:
|
||||
"""Scoped writer that cannot publish outside one invisible staging dir."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
staging_dir: Path,
|
||||
*,
|
||||
capacity_check: Callable[[int, int], None],
|
||||
) -> None:
|
||||
self.staging_dir = staging_dir
|
||||
self._capacity_check = capacity_check
|
||||
self._active: set[StreamingSafeTensorFile] = set()
|
||||
self._files: dict[str, tuple[TensorWriteSpec, ...]] = {}
|
||||
self._reserved_bytes = 0
|
||||
self._finalized = False
|
||||
|
||||
def open_tensor_file(
|
||||
self,
|
||||
relative_name: str,
|
||||
specs: tuple[TensorWriteSpec, ...],
|
||||
) -> StreamingSafeTensorFile:
|
||||
if self._finalized:
|
||||
raise ArtifactWriterError("cannot add payloads after artifact finalization")
|
||||
relative_name = _safe_relative_name(relative_name)
|
||||
if relative_name in self._files or any(item.relative_name == relative_name for item in self._active):
|
||||
raise ArtifactWriterError(f"artifact payload {relative_name!r} was declared more than once")
|
||||
return StreamingSafeTensorFile(self, relative_name, specs)
|
||||
|
||||
def _reserve_file(self, size: int) -> None:
|
||||
self._capacity_check(self._reserved_bytes + size, size)
|
||||
self._reserved_bytes += size
|
||||
|
||||
def _forget_reserved_file(self, size: int) -> None:
|
||||
self._reserved_bytes = max(0, self._reserved_bytes - size)
|
||||
|
||||
def _opened(self, writer: StreamingSafeTensorFile) -> None:
|
||||
self._active.add(writer)
|
||||
|
||||
def _closed(self, writer: StreamingSafeTensorFile) -> None:
|
||||
self._active.remove(writer)
|
||||
self._files[writer.relative_name] = writer.specs
|
||||
|
||||
def _aborted(self, writer: StreamingSafeTensorFile, size: int) -> None:
|
||||
self._active.discard(writer)
|
||||
self._forget_reserved_file(size)
|
||||
|
||||
def abort(self) -> None:
|
||||
for writer in tuple(self._active):
|
||||
with contextlib.suppress(Exception):
|
||||
writer.abort()
|
||||
|
||||
def finalize(
|
||||
self,
|
||||
identity: WeightArtifactIdentity,
|
||||
metadata: ProductionMetadata,
|
||||
) -> tuple[WeightManifest, PublicationMarker]:
|
||||
if self._finalized:
|
||||
raise ArtifactWriterError("artifact writer was finalized more than once")
|
||||
if self._active:
|
||||
raise ArtifactWriterError("artifact producer left tensor payload files open")
|
||||
if not self._files:
|
||||
raise ArtifactWriterError("artifact producer emitted no tensor payload files")
|
||||
if metadata.producer_schema != identity.producer.manifest_schema:
|
||||
raise ArtifactWriterError("production metadata does not match the identity's manifest schema")
|
||||
if metadata.restorer_schema != identity.producer.restorer_schema:
|
||||
raise ArtifactWriterError("production metadata does not match the identity's restorer schema")
|
||||
|
||||
tensor_entries: list[TensorManifestEntry] = []
|
||||
file_entries: list[FileManifestEntry] = []
|
||||
seen_tensors: set[str] = set()
|
||||
for file_name in sorted(self._files):
|
||||
path = self.staging_dir / file_name
|
||||
os.chmod(path, 0o444)
|
||||
with path.open("rb") as handle:
|
||||
os.fsync(handle.fileno())
|
||||
file_entries.append(
|
||||
FileManifestEntry(
|
||||
file_name=file_name,
|
||||
size=path.stat().st_size,
|
||||
sha256=_file_sha256(path),
|
||||
)
|
||||
)
|
||||
expected = {spec.name: spec for spec in self._files[file_name]}
|
||||
with safe_open(str(path), framework="pt", device="cpu") as handle:
|
||||
if set(handle.keys()) != set(expected):
|
||||
raise ArtifactWriterError(f"published tensor keys differ in {file_name!r}")
|
||||
for name in sorted(expected):
|
||||
if name in seen_tensors:
|
||||
raise ArtifactWriterError(f"artifact tensor {name!r} occurs in multiple payload files")
|
||||
seen_tensors.add(name)
|
||||
spec = expected[name]
|
||||
tensor = handle.get_tensor(name)
|
||||
if tuple(tensor.shape) != spec.shape or tensor.dtype != spec.dtype:
|
||||
raise ArtifactWriterError(f"published tensor {name!r} differs from its declared metadata")
|
||||
entry = TensorManifestEntry(
|
||||
name=name,
|
||||
kind=spec.kind,
|
||||
role=spec.role,
|
||||
file_name=file_name,
|
||||
storage_key=name,
|
||||
shape=tuple(tensor.shape),
|
||||
dtype=str(tensor.dtype),
|
||||
stride=tuple(tensor.stride()),
|
||||
nbytes=tensor.numel() * tensor.element_size(),
|
||||
sha256=_tensor_sha256(tensor),
|
||||
)
|
||||
tensor_entries.append(entry)
|
||||
|
||||
ordered_tensor_entries = tuple(sorted(tensor_entries, key=lambda entry: entry.name))
|
||||
content_digest = hashlib.sha256()
|
||||
for entry in ordered_tensor_entries:
|
||||
content_digest.update(canonical_json(entry.semantic_dict()))
|
||||
|
||||
manifest = WeightManifest(
|
||||
schema_version=1,
|
||||
artifact_key=identity.key,
|
||||
identity=identity,
|
||||
producer_schema=metadata.producer_schema,
|
||||
restorer_schema=metadata.restorer_schema,
|
||||
tensors=ordered_tensor_entries,
|
||||
files=tuple(file_entries),
|
||||
artifact_content_sha256=content_digest.hexdigest(),
|
||||
format_metadata=metadata.format_metadata,
|
||||
)
|
||||
manifest_bytes = manifest.canonical_bytes
|
||||
self._write_metadata_file(_MANIFEST_FILE, manifest_bytes)
|
||||
marker = PublicationMarker(
|
||||
schema_version=1,
|
||||
publication_id=uuid.uuid4().hex,
|
||||
manifest_sha256=hashlib.sha256(manifest_bytes).hexdigest(),
|
||||
artifact_content_sha256=manifest.artifact_content_sha256,
|
||||
integrity_mode="full_checksum",
|
||||
)
|
||||
self._write_metadata_file(_READY_FILE, marker.canonical_bytes)
|
||||
_fsync_directory(self.staging_dir)
|
||||
self._finalized = True
|
||||
return manifest, marker
|
||||
|
||||
def _write_metadata_file(self, name: str, data: bytes) -> None:
|
||||
path = self.staging_dir / name
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_CLOEXEC, 0o600)
|
||||
try:
|
||||
_pwrite_all(fd, data, 0)
|
||||
os.fsync(fd)
|
||||
os.fchmod(fd, 0o444)
|
||||
os.fsync(fd)
|
||||
finally:
|
||||
os.close(fd)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ArtifactWriterError",
|
||||
"FilesystemArtifactWriter",
|
||||
"StreamingSafeTensorFile",
|
||||
]
|
||||
@@ -0,0 +1,407 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Deterministic semantic identity for runtime-ready host weights."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import math
|
||||
from dataclasses import dataclass, field
|
||||
from typing import TypeAlias, cast
|
||||
|
||||
JsonScalar: TypeAlias = None | bool | int | float | str
|
||||
JsonValue: TypeAlias = JsonScalar | list["JsonValue"] | dict[str, "JsonValue"]
|
||||
|
||||
|
||||
class IdentityValidationError(ValueError):
|
||||
"""Raised when an artifact identity is not deterministic JSON."""
|
||||
|
||||
|
||||
def _validate_json(value: object, path: str = "$") -> JsonValue:
|
||||
if value is None or isinstance(value, (bool, int, str)):
|
||||
return value
|
||||
if isinstance(value, float):
|
||||
if not math.isfinite(value):
|
||||
raise IdentityValidationError(f"{path} contains a non-finite float")
|
||||
return value
|
||||
if isinstance(value, list | tuple):
|
||||
return [_validate_json(item, f"{path}[{index}]") for index, item in enumerate(value)]
|
||||
if isinstance(value, dict):
|
||||
normalized: dict[str, JsonValue] = {}
|
||||
for key, item in value.items():
|
||||
if not isinstance(key, str):
|
||||
raise IdentityValidationError(f"{path} contains a non-string mapping key")
|
||||
normalized[key] = _validate_json(item, f"{path}.{key}")
|
||||
return {key: normalized[key] for key in sorted(normalized)}
|
||||
raise IdentityValidationError(f"{path} contains unsupported value type {type(value).__qualname__}")
|
||||
|
||||
|
||||
def canonical_json(value: object) -> bytes:
|
||||
"""Return one stable JSON encoding, rejecting process-specific objects."""
|
||||
return json.dumps(
|
||||
_validate_json(value),
|
||||
sort_keys=True,
|
||||
separators=(",", ":"),
|
||||
ensure_ascii=True,
|
||||
allow_nan=False,
|
||||
).encode("utf-8")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CanonicalJson:
|
||||
"""Immutable snapshot of deterministic JSON metadata."""
|
||||
|
||||
encoded: bytes
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.encoded, bytes):
|
||||
raise IdentityValidationError("canonical metadata must be immutable bytes")
|
||||
try:
|
||||
value = json.loads(self.encoded)
|
||||
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
||||
raise IdentityValidationError("canonical metadata is not valid JSON") from exc
|
||||
if canonical_json(value) != self.encoded:
|
||||
raise IdentityValidationError("canonical metadata does not use the required stable encoding")
|
||||
|
||||
@classmethod
|
||||
def from_value(cls, value: object) -> CanonicalJson:
|
||||
return cls(canonical_json(value))
|
||||
|
||||
@classmethod
|
||||
def empty(cls) -> CanonicalJson:
|
||||
return cls(b"{}")
|
||||
|
||||
def to_value(self) -> JsonValue:
|
||||
return json.loads(self.encoded)
|
||||
|
||||
|
||||
def _require_text(name: str, value: object) -> str:
|
||||
if not isinstance(value, str) or not value:
|
||||
raise IdentityValidationError(f"{name} must not be empty")
|
||||
return value
|
||||
|
||||
|
||||
def _require_int(name: str, value: object) -> int:
|
||||
if isinstance(value, bool) or not isinstance(value, int):
|
||||
raise IdentityValidationError(f"{name} must be an integer")
|
||||
return value
|
||||
|
||||
|
||||
def _require_metadata(name: str, value: object) -> None:
|
||||
if not isinstance(value, CanonicalJson):
|
||||
raise IdentityValidationError(f"{name} must use CanonicalJson")
|
||||
|
||||
|
||||
def _require_exact_keys(value: dict[str, object], expected: set[str], kind: str) -> None:
|
||||
keys = set(value)
|
||||
if keys != expected:
|
||||
raise IdentityValidationError(
|
||||
f"{kind} fields differ: missing={sorted(expected - keys)}, unexpected={sorted(keys - expected)}"
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeightSourceIdentity:
|
||||
model_id: str
|
||||
revision: str
|
||||
fingerprint: str
|
||||
metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_text("source.model_id", self.model_id)
|
||||
_require_text("source.revision", self.revision)
|
||||
_require_text("source.fingerprint", self.fingerprint)
|
||||
_require_metadata("source.metadata", self.metadata)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"model_id": self.model_id,
|
||||
"revision": self.revision,
|
||||
"fingerprint": self.fingerprint,
|
||||
"metadata": self.metadata.to_value(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> WeightSourceIdentity:
|
||||
_require_exact_keys(value, {"model_id", "revision", "fingerprint", "metadata"}, "source identity")
|
||||
return cls(
|
||||
model_id=_require_text("source.model_id", value["model_id"]),
|
||||
revision=_require_text("source.revision", value["revision"]),
|
||||
fingerprint=_require_text("source.fingerprint", value["fingerprint"]),
|
||||
metadata=CanonicalJson.from_value(value["metadata"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ComponentIdentity:
|
||||
name: str
|
||||
ownership: str
|
||||
metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_text("component.name", self.name)
|
||||
_require_text("component.ownership", self.ownership)
|
||||
_require_metadata("component.metadata", self.metadata)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {"name": self.name, "ownership": self.ownership, "metadata": self.metadata.to_value()}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> ComponentIdentity:
|
||||
_require_exact_keys(value, {"name", "ownership", "metadata"}, "component identity")
|
||||
return cls(
|
||||
name=_require_text("component.name", value["name"]),
|
||||
ownership=_require_text("component.ownership", value["ownership"]),
|
||||
metadata=CanonicalJson.from_value(value["metadata"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeightRepresentation:
|
||||
name: str
|
||||
dtype: str
|
||||
metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_text("representation.name", self.name)
|
||||
_require_text("representation.dtype", self.dtype)
|
||||
_require_metadata("representation.metadata", self.metadata)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {"name": self.name, "dtype": self.dtype, "metadata": self.metadata.to_value()}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> WeightRepresentation:
|
||||
_require_exact_keys(value, {"name", "dtype", "metadata"}, "weight representation")
|
||||
return cls(
|
||||
name=_require_text("representation.name", value["name"]),
|
||||
dtype=_require_text("representation.dtype", value["dtype"]),
|
||||
metadata=CanonicalJson.from_value(value["metadata"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RuntimeWeightLayout:
|
||||
name: str
|
||||
tensor_parallel_size: int = 1
|
||||
tensor_parallel_rank: int = 0
|
||||
sequence_parallel_size: int = 1
|
||||
sequence_parallel_backend: str = "none"
|
||||
metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_text("layout.name", self.name)
|
||||
tensor_parallel_size = _require_int("layout.tensor_parallel_size", self.tensor_parallel_size)
|
||||
tensor_parallel_rank = _require_int("layout.tensor_parallel_rank", self.tensor_parallel_rank)
|
||||
sequence_parallel_size = _require_int("layout.sequence_parallel_size", self.sequence_parallel_size)
|
||||
if tensor_parallel_size < 1:
|
||||
raise IdentityValidationError("tensor_parallel_size must be positive")
|
||||
if not 0 <= tensor_parallel_rank < tensor_parallel_size:
|
||||
raise IdentityValidationError("tensor_parallel_rank must be within tensor_parallel_size")
|
||||
if sequence_parallel_size < 1:
|
||||
raise IdentityValidationError("sequence_parallel_size must be positive")
|
||||
_require_text("layout.sequence_parallel_backend", self.sequence_parallel_backend)
|
||||
_require_metadata("layout.metadata", self.metadata)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"tensor_parallel_size": self.tensor_parallel_size,
|
||||
"tensor_parallel_rank": self.tensor_parallel_rank,
|
||||
"sequence_parallel_size": self.sequence_parallel_size,
|
||||
"sequence_parallel_backend": self.sequence_parallel_backend,
|
||||
"metadata": self.metadata.to_value(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> RuntimeWeightLayout:
|
||||
expected = {
|
||||
"name",
|
||||
"tensor_parallel_size",
|
||||
"tensor_parallel_rank",
|
||||
"sequence_parallel_size",
|
||||
"sequence_parallel_backend",
|
||||
"metadata",
|
||||
}
|
||||
_require_exact_keys(value, expected, "runtime weight layout")
|
||||
return cls(
|
||||
name=_require_text("layout.name", value["name"]),
|
||||
tensor_parallel_size=_require_int("layout.tensor_parallel_size", value["tensor_parallel_size"]),
|
||||
tensor_parallel_rank=_require_int("layout.tensor_parallel_rank", value["tensor_parallel_rank"]),
|
||||
sequence_parallel_size=_require_int("layout.sequence_parallel_size", value["sequence_parallel_size"]),
|
||||
sequence_parallel_backend=_require_text(
|
||||
"layout.sequence_parallel_backend", value["sequence_parallel_backend"]
|
||||
),
|
||||
metadata=CanonicalJson.from_value(value["metadata"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdaptationIdentity:
|
||||
kind: str = "base"
|
||||
fingerprint: str | None = None
|
||||
metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_text("adaptation.kind", self.kind)
|
||||
if self.kind != "base" and not self.fingerprint:
|
||||
raise IdentityValidationError("non-base adaptation requires a fingerprint")
|
||||
if self.fingerprint is not None and (not isinstance(self.fingerprint, str) or not self.fingerprint):
|
||||
raise IdentityValidationError("adaptation fingerprint must be a non-empty string or null")
|
||||
_require_metadata("adaptation.metadata", self.metadata)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {"kind": self.kind, "fingerprint": self.fingerprint, "metadata": self.metadata.to_value()}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> AdaptationIdentity:
|
||||
_require_exact_keys(value, {"kind", "fingerprint", "metadata"}, "adaptation identity")
|
||||
fingerprint = value["fingerprint"]
|
||||
if fingerprint is not None and not isinstance(fingerprint, str):
|
||||
raise IdentityValidationError("adaptation fingerprint must be a string or null")
|
||||
return cls(
|
||||
kind=_require_text("adaptation.kind", value["kind"]),
|
||||
fingerprint=fingerprint,
|
||||
metadata=CanonicalJson.from_value(value["metadata"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProducerIdentity:
|
||||
producer_id: str
|
||||
version: str
|
||||
implementation_fingerprint: str
|
||||
manifest_schema: str
|
||||
restorer_schema: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
for name, value in (
|
||||
("producer.producer_id", self.producer_id),
|
||||
("producer.version", self.version),
|
||||
("producer.implementation_fingerprint", self.implementation_fingerprint),
|
||||
("producer.manifest_schema", self.manifest_schema),
|
||||
("producer.restorer_schema", self.restorer_schema),
|
||||
):
|
||||
_require_text(name, value)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"producer_id": self.producer_id,
|
||||
"version": self.version,
|
||||
"implementation_fingerprint": self.implementation_fingerprint,
|
||||
"manifest_schema": self.manifest_schema,
|
||||
"restorer_schema": self.restorer_schema,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> ProducerIdentity:
|
||||
expected = {
|
||||
"producer_id",
|
||||
"version",
|
||||
"implementation_fingerprint",
|
||||
"manifest_schema",
|
||||
"restorer_schema",
|
||||
}
|
||||
_require_exact_keys(value, expected, "producer identity")
|
||||
return cls(
|
||||
producer_id=_require_text("producer.producer_id", value["producer_id"]),
|
||||
version=_require_text("producer.version", value["version"]),
|
||||
implementation_fingerprint=_require_text(
|
||||
"producer.implementation_fingerprint", value["implementation_fingerprint"]
|
||||
),
|
||||
manifest_schema=_require_text("producer.manifest_schema", value["manifest_schema"]),
|
||||
restorer_schema=_require_text("producer.restorer_schema", value["restorer_schema"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeightArtifactIdentity:
|
||||
schema_version: int
|
||||
source: WeightSourceIdentity
|
||||
component: ComponentIdentity
|
||||
representation: WeightRepresentation
|
||||
layout: RuntimeWeightLayout
|
||||
adaptation: AdaptationIdentity
|
||||
producer: ProducerIdentity
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if _require_int("identity.schema_version", self.schema_version) < 1:
|
||||
raise IdentityValidationError("identity schema_version must be positive")
|
||||
for name, value, expected in (
|
||||
("source", self.source, WeightSourceIdentity),
|
||||
("component", self.component, ComponentIdentity),
|
||||
("representation", self.representation, WeightRepresentation),
|
||||
("layout", self.layout, RuntimeWeightLayout),
|
||||
("adaptation", self.adaptation, AdaptationIdentity),
|
||||
("producer", self.producer, ProducerIdentity),
|
||||
):
|
||||
if not isinstance(value, expected):
|
||||
raise IdentityValidationError(f"identity {name} has the wrong contract type")
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"schema_version": self.schema_version,
|
||||
"source": self.source.to_dict(),
|
||||
"component": self.component.to_dict(),
|
||||
"representation": self.representation.to_dict(),
|
||||
"layout": self.layout.to_dict(),
|
||||
"adaptation": self.adaptation.to_dict(),
|
||||
"producer": self.producer.to_dict(),
|
||||
}
|
||||
|
||||
@property
|
||||
def canonical_bytes(self) -> bytes:
|
||||
return canonical_json(self.to_dict())
|
||||
|
||||
@property
|
||||
def key(self) -> str:
|
||||
return hashlib.sha256(self.canonical_bytes).hexdigest()
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> WeightArtifactIdentity:
|
||||
expected = {
|
||||
"schema_version",
|
||||
"source",
|
||||
"component",
|
||||
"representation",
|
||||
"layout",
|
||||
"adaptation",
|
||||
"producer",
|
||||
}
|
||||
_require_exact_keys(value, expected, "weight artifact identity")
|
||||
source = value["source"]
|
||||
component = value["component"]
|
||||
representation = value["representation"]
|
||||
layout = value["layout"]
|
||||
adaptation = value["adaptation"]
|
||||
producer = value["producer"]
|
||||
if any(
|
||||
not isinstance(section, dict)
|
||||
for section in (source, component, representation, layout, adaptation, producer)
|
||||
):
|
||||
raise IdentityValidationError("identity sections must be JSON objects")
|
||||
return cls(
|
||||
schema_version=_require_int("identity.schema_version", value["schema_version"]),
|
||||
source=WeightSourceIdentity.from_dict(cast(dict[str, object], source)),
|
||||
component=ComponentIdentity.from_dict(cast(dict[str, object], component)),
|
||||
representation=WeightRepresentation.from_dict(cast(dict[str, object], representation)),
|
||||
layout=RuntimeWeightLayout.from_dict(cast(dict[str, object], layout)),
|
||||
adaptation=AdaptationIdentity.from_dict(cast(dict[str, object], adaptation)),
|
||||
producer=ProducerIdentity.from_dict(cast(dict[str, object], producer)),
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AdaptationIdentity",
|
||||
"CanonicalJson",
|
||||
"ComponentIdentity",
|
||||
"IdentityValidationError",
|
||||
"JsonValue",
|
||||
"ProducerIdentity",
|
||||
"RuntimeWeightLayout",
|
||||
"WeightArtifactIdentity",
|
||||
"WeightRepresentation",
|
||||
"WeightSourceIdentity",
|
||||
"canonical_json",
|
||||
]
|
||||
@@ -0,0 +1,80 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Typed, read-only snapshots for local Host Weight Store inspection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import Protocol
|
||||
|
||||
from .config import ValidationLevel
|
||||
from .errors import HostWeightFailure
|
||||
from .identity import WeightArtifactIdentity
|
||||
from .manifest import WeightManifest
|
||||
from .outcomes import StoreStatus
|
||||
|
||||
|
||||
class ArtifactInventoryState(str, Enum):
|
||||
READY = "ready"
|
||||
DENIED = "denied"
|
||||
QUARANTINED = "quarantined"
|
||||
TEMPORARY = "temporary"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ArtifactInventoryEntry:
|
||||
artifact_key: str
|
||||
state: ArtifactInventoryState
|
||||
storage_name: str
|
||||
size_bytes: int
|
||||
reason: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class DomainInspection:
|
||||
domain_uuid: str
|
||||
scope: str
|
||||
domain_id: str
|
||||
storage_class: str
|
||||
filesystem_type: str
|
||||
policy_version: int
|
||||
store_bytes: int
|
||||
filesystem_free_bytes: int
|
||||
max_artifact_bytes: int | None
|
||||
max_store_bytes: int | None
|
||||
min_free_bytes: int
|
||||
inventory: tuple[ArtifactInventoryEntry, ...]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ArtifactInspection:
|
||||
artifact_key: str
|
||||
status: StoreStatus
|
||||
size_bytes: int
|
||||
manifest: WeightManifest | None
|
||||
publication_id: str | None
|
||||
backing_fingerprint: str | None
|
||||
failure: HostWeightFailure | None
|
||||
build_lock_active: bool
|
||||
artifact_lock_active: bool
|
||||
|
||||
|
||||
class HostWeightInspector(Protocol):
|
||||
def inspect_domain(self) -> DomainInspection: ...
|
||||
|
||||
def inspect_artifact(
|
||||
self,
|
||||
identity: WeightArtifactIdentity,
|
||||
*,
|
||||
validation: ValidationLevel,
|
||||
) -> ArtifactInspection: ...
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ArtifactInspection",
|
||||
"ArtifactInventoryEntry",
|
||||
"ArtifactInventoryState",
|
||||
"DomainInspection",
|
||||
"HostWeightInspector",
|
||||
]
|
||||
@@ -0,0 +1,143 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Process-local ownership for immutable mmap-backed host tensors."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import threading
|
||||
from collections.abc import Callable, Mapping
|
||||
from dataclasses import dataclass
|
||||
from types import MappingProxyType
|
||||
from typing import NoReturn, SupportsIndex
|
||||
|
||||
import torch
|
||||
|
||||
from .identity import WeightArtifactIdentity
|
||||
from .manifest import WeightManifest
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class MappedHostRegion:
|
||||
"""One process-local virtual range belonging to an artifact payload file."""
|
||||
|
||||
file_name: str
|
||||
address: int
|
||||
length: int
|
||||
read_only: bool = True
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.file_name or self.address < 0 or self.length < 0:
|
||||
raise ValueError("mapped host region contains invalid fields")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class LeaseProvenance:
|
||||
resolution_id: str
|
||||
identity_digest: str
|
||||
artifact_content_sha256: str
|
||||
domain_uuid: str
|
||||
publication_id: str
|
||||
resolution_source: str
|
||||
artifact_origin: str
|
||||
producer_id: str | None
|
||||
provider_id: str | None
|
||||
validation_level: str
|
||||
backing_fingerprint: str
|
||||
|
||||
|
||||
class HostWeightLease:
|
||||
"""Stable tensor views and resources retained until transport teardown."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
identity: WeightArtifactIdentity,
|
||||
manifest: WeightManifest,
|
||||
tensors: Mapping[str, torch.Tensor],
|
||||
mapped_regions: tuple[MappedHostRegion, ...],
|
||||
provenance: LeaseProvenance,
|
||||
resource_closers: tuple[Callable[[], None], ...],
|
||||
release_artifact_lock: Callable[[], None],
|
||||
) -> None:
|
||||
if manifest.identity.canonical_bytes != identity.canonical_bytes:
|
||||
raise ValueError("lease manifest identity differs from the requested identity")
|
||||
if set(tensors) != {entry.name for entry in manifest.tensors}:
|
||||
raise ValueError("lease tensor names differ from the validated manifest")
|
||||
entries = {entry.name: entry for entry in manifest.tensors}
|
||||
for name, tensor in tensors.items():
|
||||
entry = entries[name]
|
||||
if (
|
||||
not isinstance(tensor, torch.Tensor)
|
||||
or tensor.device.type != "cpu"
|
||||
or tuple(tensor.shape) != entry.shape
|
||||
or str(tensor.dtype) != entry.dtype
|
||||
or tuple(tensor.stride()) != entry.stride
|
||||
or tensor.numel() * tensor.element_size() != entry.nbytes
|
||||
):
|
||||
raise ValueError(f"lease tensor {name!r} differs from the validated host metadata")
|
||||
if provenance.identity_digest != identity.key:
|
||||
raise ValueError("lease provenance identity digest differs from the requested identity")
|
||||
if provenance.artifact_content_sha256 != manifest.artifact_content_sha256:
|
||||
raise ValueError("lease provenance content digest differs from the validated manifest")
|
||||
if not isinstance(mapped_regions, tuple) or any(
|
||||
not isinstance(region, MappedHostRegion) for region in mapped_regions
|
||||
):
|
||||
raise ValueError("lease mapped regions must be an immutable tuple")
|
||||
if not isinstance(resource_closers, tuple) or any(not callable(closer) for closer in resource_closers):
|
||||
raise ValueError("lease resource closers must be an immutable tuple of callables")
|
||||
if not callable(release_artifact_lock):
|
||||
raise ValueError("lease artifact lock release must be callable")
|
||||
self.identity = identity
|
||||
self.manifest = manifest
|
||||
self._tensor_storage = dict(tensors)
|
||||
self.tensors = MappingProxyType(self._tensor_storage)
|
||||
self.mapped_regions = mapped_regions
|
||||
self.provenance = provenance
|
||||
self._resource_closers = resource_closers
|
||||
self._release_artifact_lock = release_artifact_lock
|
||||
self._state_lock = threading.RLock()
|
||||
self._closed = False
|
||||
|
||||
@property
|
||||
def closed(self) -> bool:
|
||||
with self._state_lock:
|
||||
return self._closed
|
||||
|
||||
def close(self) -> None:
|
||||
with self._state_lock:
|
||||
if self._closed:
|
||||
return
|
||||
self._closed = True
|
||||
|
||||
errors: list[BaseException] = []
|
||||
self._tensor_storage.clear()
|
||||
for closer in reversed(self._resource_closers):
|
||||
try:
|
||||
closer()
|
||||
except BaseException as exc: # pragma: no cover - best-effort teardown aggregation
|
||||
errors.append(exc)
|
||||
try:
|
||||
self._release_artifact_lock()
|
||||
except BaseException as exc: # pragma: no cover - best-effort teardown aggregation
|
||||
errors.append(exc)
|
||||
if errors:
|
||||
raise RuntimeError(f"failed to close {len(errors)} host weight lease resources") from errors[0]
|
||||
|
||||
def __enter__(self) -> HostWeightLease:
|
||||
if self.closed:
|
||||
raise RuntimeError("cannot re-enter a closed HostWeightLease")
|
||||
return self
|
||||
|
||||
def __exit__(self, exc_type: object, exc: object, _traceback: object) -> None:
|
||||
self.close()
|
||||
|
||||
def __reduce_ex__(self, _protocol: SupportsIndex) -> NoReturn:
|
||||
raise TypeError("HostWeightLease is process-local and cannot be serialized")
|
||||
|
||||
def __del__(self) -> None:
|
||||
with contextlib.suppress(Exception):
|
||||
self.close()
|
||||
|
||||
|
||||
__all__ = ["HostWeightLease", "LeaseProvenance", "MappedHostRegion"]
|
||||
@@ -0,0 +1,353 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Versioned immutable manifests for runtime-ready host weights."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from pathlib import PurePosixPath
|
||||
from typing import cast
|
||||
|
||||
import regex as re
|
||||
|
||||
from .identity import CanonicalJson, JsonValue, WeightArtifactIdentity, canonical_json
|
||||
|
||||
_SHA256_RE = re.compile(r"[0-9a-f]{64}")
|
||||
|
||||
|
||||
class ManifestValidationError(ValueError):
|
||||
"""Raised when a published manifest violates its schema."""
|
||||
|
||||
|
||||
class TensorKind(str, Enum):
|
||||
PARAMETER = "parameter"
|
||||
BUFFER = "buffer"
|
||||
|
||||
|
||||
def _require_text(name: str, value: object) -> str:
|
||||
if not isinstance(value, str) or not value:
|
||||
raise ManifestValidationError(f"{name} must be a non-empty string")
|
||||
return value
|
||||
|
||||
|
||||
def _require_int(name: str, value: object) -> int:
|
||||
if isinstance(value, bool) or not isinstance(value, int):
|
||||
raise ManifestValidationError(f"{name} must be an integer")
|
||||
return value
|
||||
|
||||
|
||||
def _require_sha256(name: str, value: object) -> str:
|
||||
if not isinstance(value, str) or _SHA256_RE.fullmatch(value) is None:
|
||||
raise ManifestValidationError(f"{name} must be a lowercase SHA-256 digest")
|
||||
return value
|
||||
|
||||
|
||||
def _require_safe_file_name(value: object) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise ManifestValidationError("artifact file name must be a string")
|
||||
path = PurePosixPath(value)
|
||||
if not value or path.is_absolute() or len(path.parts) != 1 or path.name != value or value in {".", ".."}:
|
||||
raise ManifestValidationError(f"artifact file name must be one safe relative basename: {value!r}")
|
||||
return value
|
||||
|
||||
|
||||
def _require_int_tuple(name: str, value: object) -> tuple[int, ...]:
|
||||
if not isinstance(value, list):
|
||||
raise ManifestValidationError(f"{name} must be an array")
|
||||
return tuple(_require_int(f"{name}[{index}]", item) for index, item in enumerate(value))
|
||||
|
||||
|
||||
def _exact_keys(value: dict[str, object], expected: set[str], kind: str) -> None:
|
||||
keys = set(value)
|
||||
if keys != expected:
|
||||
raise ManifestValidationError(
|
||||
f"{kind} fields differ: missing={sorted(expected - keys)}, unexpected={sorted(keys - expected)}"
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TensorManifestEntry:
|
||||
name: str
|
||||
kind: TensorKind
|
||||
role: str
|
||||
file_name: str
|
||||
storage_key: str
|
||||
shape: tuple[int, ...]
|
||||
dtype: str
|
||||
stride: tuple[int, ...]
|
||||
nbytes: int
|
||||
sha256: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_text("tensor.name", self.name)
|
||||
_require_text("tensor.storage_key", self.storage_key)
|
||||
_require_text("tensor.role", self.role)
|
||||
_require_text("tensor.dtype", self.dtype)
|
||||
if not isinstance(self.kind, TensorKind):
|
||||
raise ManifestValidationError("tensor kind must use TensorKind")
|
||||
_require_safe_file_name(self.file_name)
|
||||
if not isinstance(self.shape, tuple) or any(
|
||||
isinstance(dimension, bool) or not isinstance(dimension, int) or dimension < 0 for dimension in self.shape
|
||||
):
|
||||
raise ManifestValidationError(f"tensor {self.name!r} has a negative shape dimension")
|
||||
if (
|
||||
not isinstance(self.stride, tuple)
|
||||
or len(self.stride) != len(self.shape)
|
||||
or any(isinstance(value, bool) or not isinstance(value, int) or value < 0 for value in self.stride)
|
||||
):
|
||||
raise ManifestValidationError(f"tensor {self.name!r} has an invalid stride")
|
||||
if _require_int(f"tensors[{self.name!r}].nbytes", self.nbytes) < 0:
|
||||
raise ManifestValidationError(f"tensor {self.name!r} has negative nbytes")
|
||||
_require_sha256(f"tensors[{self.name!r}].sha256", self.sha256)
|
||||
|
||||
def semantic_dict(self) -> dict[str, JsonValue]:
|
||||
"""Return content identity independent of physical file sharding."""
|
||||
return {
|
||||
"name": self.name,
|
||||
"kind": self.kind.value,
|
||||
"role": self.role,
|
||||
"shape": list(self.shape),
|
||||
"dtype": self.dtype,
|
||||
"stride": list(self.stride),
|
||||
"nbytes": self.nbytes,
|
||||
"sha256": self.sha256,
|
||||
}
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"name": self.name,
|
||||
"kind": self.kind.value,
|
||||
"role": self.role,
|
||||
"file_name": self.file_name,
|
||||
"storage_key": self.storage_key,
|
||||
"shape": list(self.shape),
|
||||
"dtype": self.dtype,
|
||||
"stride": list(self.stride),
|
||||
"nbytes": self.nbytes,
|
||||
"sha256": self.sha256,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> TensorManifestEntry:
|
||||
expected = {
|
||||
"name",
|
||||
"kind",
|
||||
"role",
|
||||
"file_name",
|
||||
"storage_key",
|
||||
"shape",
|
||||
"dtype",
|
||||
"stride",
|
||||
"nbytes",
|
||||
"sha256",
|
||||
}
|
||||
_exact_keys(value, expected, "tensor manifest entry")
|
||||
kind_value = _require_text("tensor.kind", value["kind"])
|
||||
try:
|
||||
kind = TensorKind(kind_value)
|
||||
except ValueError as exc:
|
||||
raise ManifestValidationError(f"unknown tensor kind {value['kind']!r}") from exc
|
||||
return cls(
|
||||
name=_require_text("tensor.name", value["name"]),
|
||||
kind=kind,
|
||||
role=_require_text("tensor.role", value["role"]),
|
||||
file_name=_require_safe_file_name(value["file_name"]),
|
||||
storage_key=_require_text("tensor.storage_key", value["storage_key"]),
|
||||
shape=_require_int_tuple("tensor.shape", value["shape"]),
|
||||
dtype=_require_text("tensor.dtype", value["dtype"]),
|
||||
stride=_require_int_tuple("tensor.stride", value["stride"]),
|
||||
nbytes=_require_int("tensor.nbytes", value["nbytes"]),
|
||||
sha256=_require_sha256("tensor.sha256", value["sha256"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class FileManifestEntry:
|
||||
file_name: str
|
||||
size: int
|
||||
sha256: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
_require_safe_file_name(self.file_name)
|
||||
if _require_int(f"files[{self.file_name!r}].size", self.size) < 0:
|
||||
raise ManifestValidationError(f"artifact file {self.file_name!r} has negative size")
|
||||
_require_sha256(f"files[{self.file_name!r}].sha256", self.sha256)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {"file_name": self.file_name, "size": self.size, "sha256": self.sha256}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> FileManifestEntry:
|
||||
_exact_keys(value, {"file_name", "size", "sha256"}, "file manifest entry")
|
||||
return cls(
|
||||
file_name=_require_safe_file_name(value["file_name"]),
|
||||
size=_require_int("file.size", value["size"]),
|
||||
sha256=_require_sha256("file.sha256", value["sha256"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeightManifest:
|
||||
schema_version: int
|
||||
artifact_key: str
|
||||
identity: WeightArtifactIdentity
|
||||
producer_schema: str
|
||||
restorer_schema: str
|
||||
tensors: tuple[TensorManifestEntry, ...]
|
||||
files: tuple[FileManifestEntry, ...]
|
||||
artifact_content_sha256: str
|
||||
format_metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if _require_int("manifest.schema_version", self.schema_version) < 1:
|
||||
raise ManifestValidationError("manifest schema_version must be positive")
|
||||
_require_sha256("artifact_key", self.artifact_key)
|
||||
_require_sha256("artifact_content_sha256", self.artifact_content_sha256)
|
||||
if not isinstance(self.identity, WeightArtifactIdentity):
|
||||
raise ManifestValidationError("manifest identity must use WeightArtifactIdentity")
|
||||
if self.artifact_key != self.identity.key:
|
||||
raise ManifestValidationError("manifest artifact_key does not match its semantic identity")
|
||||
_require_text("manifest.producer_schema", self.producer_schema)
|
||||
_require_text("manifest.restorer_schema", self.restorer_schema)
|
||||
if (
|
||||
not isinstance(self.tensors, tuple)
|
||||
or not isinstance(self.files, tuple)
|
||||
or not self.tensors
|
||||
or not self.files
|
||||
or any(not isinstance(entry, TensorManifestEntry) for entry in self.tensors)
|
||||
or any(not isinstance(entry, FileManifestEntry) for entry in self.files)
|
||||
):
|
||||
raise ManifestValidationError("an artifact must contain at least one tensor and one file")
|
||||
if not isinstance(self.format_metadata, CanonicalJson):
|
||||
raise ManifestValidationError("manifest format_metadata must use CanonicalJson")
|
||||
tensor_names = [entry.name for entry in self.tensors]
|
||||
if tensor_names != sorted(tensor_names) or len(set(tensor_names)) != len(tensor_names):
|
||||
raise ManifestValidationError("manifest tensors must have unique names in canonical order")
|
||||
file_names = [entry.file_name for entry in self.files]
|
||||
if file_names != sorted(file_names) or len(set(file_names)) != len(file_names):
|
||||
raise ManifestValidationError("manifest files must have unique names in canonical order")
|
||||
available_files = set(file_names)
|
||||
if any(entry.file_name not in available_files for entry in self.tensors):
|
||||
raise ManifestValidationError("tensor manifest references an undeclared artifact file")
|
||||
storage_bindings = [(entry.file_name, entry.storage_key) for entry in self.tensors]
|
||||
if len(set(storage_bindings)) != len(storage_bindings):
|
||||
raise ManifestValidationError("manifest tensor storage bindings must be unique")
|
||||
|
||||
@property
|
||||
def canonical_bytes(self) -> bytes:
|
||||
return canonical_json(self.to_dict())
|
||||
|
||||
@property
|
||||
def total_tensor_bytes(self) -> int:
|
||||
return sum(entry.nbytes for entry in self.tensors)
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"schema_version": self.schema_version,
|
||||
"artifact_key": self.artifact_key,
|
||||
"identity": self.identity.to_dict(),
|
||||
"producer_schema": self.producer_schema,
|
||||
"restorer_schema": self.restorer_schema,
|
||||
"tensors": [entry.to_dict() for entry in self.tensors],
|
||||
"files": [entry.to_dict() for entry in self.files],
|
||||
"artifact_content_sha256": self.artifact_content_sha256,
|
||||
"format_metadata": self.format_metadata.to_value(),
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> WeightManifest:
|
||||
expected = {
|
||||
"schema_version",
|
||||
"artifact_key",
|
||||
"identity",
|
||||
"producer_schema",
|
||||
"restorer_schema",
|
||||
"tensors",
|
||||
"files",
|
||||
"artifact_content_sha256",
|
||||
"format_metadata",
|
||||
}
|
||||
_exact_keys(value, expected, "weight manifest")
|
||||
identity = value["identity"]
|
||||
if not isinstance(identity, dict):
|
||||
raise ManifestValidationError("manifest identity must be an object")
|
||||
tensors = value["tensors"]
|
||||
files = value["files"]
|
||||
if not isinstance(tensors, list) or not all(isinstance(item, dict) for item in tensors):
|
||||
raise ManifestValidationError("manifest tensors must be an array of objects")
|
||||
if not isinstance(files, list) or not all(isinstance(item, dict) for item in files):
|
||||
raise ManifestValidationError("manifest files must be an array of objects")
|
||||
return cls(
|
||||
schema_version=_require_int("manifest.schema_version", value["schema_version"]),
|
||||
artifact_key=_require_sha256("manifest.artifact_key", value["artifact_key"]),
|
||||
identity=WeightArtifactIdentity.from_dict(cast(dict[str, object], identity)),
|
||||
producer_schema=_require_text("manifest.producer_schema", value["producer_schema"]),
|
||||
restorer_schema=_require_text("manifest.restorer_schema", value["restorer_schema"]),
|
||||
tensors=tuple(TensorManifestEntry.from_dict(cast(dict[str, object], item)) for item in tensors),
|
||||
files=tuple(FileManifestEntry.from_dict(cast(dict[str, object], item)) for item in files),
|
||||
artifact_content_sha256=_require_sha256(
|
||||
"manifest.artifact_content_sha256", value["artifact_content_sha256"]
|
||||
),
|
||||
format_metadata=CanonicalJson.from_value(value["format_metadata"]),
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PublicationMarker:
|
||||
schema_version: int
|
||||
publication_id: str
|
||||
manifest_sha256: str
|
||||
artifact_content_sha256: str
|
||||
integrity_mode: str
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if _require_int("publication.schema_version", self.schema_version) < 1:
|
||||
raise ManifestValidationError("publication schema_version must be positive")
|
||||
_require_text("publication.publication_id", self.publication_id)
|
||||
_require_text("publication.integrity_mode", self.integrity_mode)
|
||||
_require_sha256("publication.manifest_sha256", self.manifest_sha256)
|
||||
_require_sha256("publication.artifact_content_sha256", self.artifact_content_sha256)
|
||||
|
||||
@property
|
||||
def canonical_bytes(self) -> bytes:
|
||||
return canonical_json(self.to_dict())
|
||||
|
||||
def to_dict(self) -> dict[str, JsonValue]:
|
||||
return {
|
||||
"schema_version": self.schema_version,
|
||||
"publication_id": self.publication_id,
|
||||
"manifest_sha256": self.manifest_sha256,
|
||||
"artifact_content_sha256": self.artifact_content_sha256,
|
||||
"integrity_mode": self.integrity_mode,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, value: dict[str, object]) -> PublicationMarker:
|
||||
expected = {
|
||||
"schema_version",
|
||||
"publication_id",
|
||||
"manifest_sha256",
|
||||
"artifact_content_sha256",
|
||||
"integrity_mode",
|
||||
}
|
||||
_exact_keys(value, expected, "publication marker")
|
||||
return cls(
|
||||
schema_version=_require_int("publication.schema_version", value["schema_version"]),
|
||||
publication_id=_require_text("publication.publication_id", value["publication_id"]),
|
||||
manifest_sha256=_require_sha256("publication.manifest_sha256", value["manifest_sha256"]),
|
||||
artifact_content_sha256=_require_sha256(
|
||||
"publication.artifact_content_sha256", value["artifact_content_sha256"]
|
||||
),
|
||||
integrity_mode=_require_text("publication.integrity_mode", value["integrity_mode"]),
|
||||
)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"FileManifestEntry",
|
||||
"ManifestValidationError",
|
||||
"PublicationMarker",
|
||||
"TensorKind",
|
||||
"TensorManifestEntry",
|
||||
"WeightManifest",
|
||||
]
|
||||
@@ -0,0 +1,119 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Typed store and end-to-end resolution outcomes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from .errors import HostWeightFailure, ResolutionAction, ResolutionStage
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .lease import HostWeightLease
|
||||
|
||||
|
||||
class StoreStatus(str, Enum):
|
||||
HIT = "hit"
|
||||
MISS = "miss"
|
||||
BUILT = "built"
|
||||
JOINED = "joined"
|
||||
INVALID = "invalid"
|
||||
TIMEOUT = "timeout"
|
||||
FAILED = "failed"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class StoreResult:
|
||||
status: StoreStatus
|
||||
lease: HostWeightLease | None = None
|
||||
failure: HostWeightFailure | None = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
has_lease = self.status in {StoreStatus.HIT, StoreStatus.BUILT, StoreStatus.JOINED}
|
||||
if has_lease != (self.lease is not None):
|
||||
raise ValueError(f"store result {self.status.value} has inconsistent lease ownership")
|
||||
if self.status in {StoreStatus.HIT, StoreStatus.MISS, StoreStatus.BUILT, StoreStatus.JOINED}:
|
||||
if self.failure is not None:
|
||||
raise ValueError(f"successful store result {self.status.value} must not carry a failure")
|
||||
if self.status in {StoreStatus.INVALID, StoreStatus.TIMEOUT, StoreStatus.FAILED} and self.failure is None:
|
||||
raise ValueError(f"store result {self.status.value} requires a typed failure")
|
||||
|
||||
|
||||
class AttemptResult(str, Enum):
|
||||
HIT = "hit"
|
||||
MISS = "miss"
|
||||
SUCCESS = "success"
|
||||
TIMEOUT = "timeout"
|
||||
ERROR = "error"
|
||||
SKIPPED = "skipped"
|
||||
|
||||
|
||||
class ResolutionOutcome(str, Enum):
|
||||
LOCAL_HIT = "local_hit"
|
||||
REMOTE_IMPORT = "remote_import"
|
||||
LOCAL_PRODUCTION = "local_production"
|
||||
CANONICAL_DIRECT = "canonical_direct"
|
||||
CANONICAL_FALLBACK = "canonical_fallback"
|
||||
FAILED = "failed"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResolutionAttempt:
|
||||
stage: ResolutionStage
|
||||
result: AttemptResult
|
||||
action: ResolutionAction
|
||||
elapsed_seconds: float
|
||||
failure: HostWeightFailure | None = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not math.isfinite(self.elapsed_seconds) or self.elapsed_seconds < 0:
|
||||
raise ValueError("resolution attempt elapsed_seconds must be finite and non-negative")
|
||||
if self.result in {AttemptResult.HIT, AttemptResult.MISS, AttemptResult.SUCCESS} and self.failure is not None:
|
||||
raise ValueError(f"resolution attempt {self.result.value} must not carry a failure")
|
||||
if self.result in {AttemptResult.TIMEOUT, AttemptResult.ERROR} and self.failure is None:
|
||||
raise ValueError(f"resolution attempt {self.result.value} requires a typed failure")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResolutionReport:
|
||||
resolution_id: str
|
||||
outcome: ResolutionOutcome
|
||||
attempts: tuple[ResolutionAttempt, ...]
|
||||
elapsed_seconds: float
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not self.resolution_id:
|
||||
raise ValueError("resolution report requires a resolution_id")
|
||||
if not isinstance(self.attempts, tuple):
|
||||
raise ValueError("resolution report attempts must be an immutable tuple")
|
||||
if not math.isfinite(self.elapsed_seconds) or self.elapsed_seconds < 0:
|
||||
raise ValueError("resolution report elapsed_seconds must be finite and non-negative")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HostWeightResolution:
|
||||
report: ResolutionReport
|
||||
lease: HostWeightLease | None = None
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
lease_outcomes = {
|
||||
ResolutionOutcome.LOCAL_HIT,
|
||||
ResolutionOutcome.REMOTE_IMPORT,
|
||||
ResolutionOutcome.LOCAL_PRODUCTION,
|
||||
}
|
||||
if (self.report.outcome in lease_outcomes) != (self.lease is not None):
|
||||
raise ValueError("resolution outcome has inconsistent lease ownership")
|
||||
|
||||
|
||||
__all__ = [
|
||||
"AttemptResult",
|
||||
"HostWeightResolution",
|
||||
"ResolutionAttempt",
|
||||
"ResolutionOutcome",
|
||||
"ResolutionReport",
|
||||
"StoreResult",
|
||||
"StoreStatus",
|
||||
]
|
||||
@@ -0,0 +1,228 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Provider and consumer protocols for Host Weight Runtime extensions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from types import TracebackType
|
||||
from typing import TYPE_CHECKING, Protocol
|
||||
|
||||
import torch
|
||||
|
||||
from .identity import CanonicalJson, WeightArtifactIdentity
|
||||
from .manifest import TensorKind
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from .config import ValidationLevel
|
||||
from .lease import HostWeightLease
|
||||
from .outcomes import StoreResult
|
||||
|
||||
|
||||
class ProductionSourceMode(str, Enum):
|
||||
FINALIZED_MODEL = "finalized_model"
|
||||
CHECKPOINT_STREAM = "checkpoint_stream"
|
||||
|
||||
|
||||
class CoordinationScope(str, Enum):
|
||||
SINGLE_PROCESS = "single_process"
|
||||
GROUP_COHORT = "group_cohort"
|
||||
ARTIFACT_BUNDLE = "artifact_bundle"
|
||||
|
||||
|
||||
class LookupPhase(str, Enum):
|
||||
PRE_LOAD_SAFE = "pre_load_safe"
|
||||
POST_LOAD_ONLY = "post_load_only"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TensorWriteSpec:
|
||||
name: str
|
||||
shape: tuple[int, ...]
|
||||
dtype: torch.dtype
|
||||
kind: TensorKind = TensorKind.PARAMETER
|
||||
role: str = "weight"
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.name, str) or not self.name or not isinstance(self.role, str) or not self.role:
|
||||
raise ValueError("tensor write name and role must not be empty")
|
||||
if not isinstance(self.shape, tuple) or any(
|
||||
isinstance(dimension, bool) or not isinstance(dimension, int) or dimension < 0 for dimension in self.shape
|
||||
):
|
||||
raise ValueError(f"tensor {self.name!r} has a negative shape dimension")
|
||||
if not isinstance(self.dtype, torch.dtype):
|
||||
raise ValueError(f"tensor {self.name!r} must declare one torch dtype")
|
||||
if not isinstance(self.kind, TensorKind):
|
||||
raise ValueError(f"tensor {self.name!r} must use TensorKind")
|
||||
|
||||
@property
|
||||
def numel(self) -> int:
|
||||
return math.prod(self.shape) if self.shape else 1
|
||||
|
||||
@property
|
||||
def nbytes(self) -> int:
|
||||
return self.numel * torch.empty((), dtype=self.dtype).element_size()
|
||||
|
||||
|
||||
class TensorFileWriter(Protocol):
|
||||
def write_tensor(self, name: str, tensor: torch.Tensor) -> None: ...
|
||||
|
||||
def write_rows(self, name: str, start_row: int, tensor: torch.Tensor) -> None: ...
|
||||
|
||||
def close(self) -> None: ...
|
||||
|
||||
def __enter__(self) -> TensorFileWriter: ...
|
||||
|
||||
def __exit__(
|
||||
self,
|
||||
exc_type: type[BaseException] | None,
|
||||
exc: BaseException | None,
|
||||
_traceback: TracebackType | None,
|
||||
) -> bool | None: ...
|
||||
|
||||
|
||||
class ArtifactWriter(Protocol):
|
||||
def open_tensor_file(
|
||||
self,
|
||||
relative_name: str,
|
||||
specs: tuple[TensorWriteSpec, ...],
|
||||
) -> TensorFileWriter: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WeightProductionSpec:
|
||||
producer_id: str
|
||||
outputs: tuple[WeightArtifactIdentity, ...]
|
||||
source_mode: ProductionSourceMode
|
||||
coordination_scope: CoordinationScope = CoordinationScope.SINGLE_PROCESS
|
||||
lookup_phase: LookupPhase = LookupPhase.PRE_LOAD_SAFE
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if not isinstance(self.producer_id, str) or not self.producer_id or not self.outputs:
|
||||
raise ValueError("producer_id and outputs must not be empty")
|
||||
if not isinstance(self.outputs, tuple) or any(
|
||||
not isinstance(output, WeightArtifactIdentity) for output in self.outputs
|
||||
):
|
||||
raise ValueError("producer outputs must be an immutable tuple of artifact identities")
|
||||
if not isinstance(self.source_mode, ProductionSourceMode):
|
||||
raise ValueError("producer source mode must use ProductionSourceMode")
|
||||
if not isinstance(self.coordination_scope, CoordinationScope):
|
||||
raise ValueError("producer coordination scope must use CoordinationScope")
|
||||
if not isinstance(self.lookup_phase, LookupPhase):
|
||||
raise ValueError("producer lookup phase must use LookupPhase")
|
||||
if len({output.key for output in self.outputs}) != len(self.outputs):
|
||||
raise ValueError("producer outputs must have unique semantic identities")
|
||||
if any(output.producer.producer_id != self.producer_id for output in self.outputs):
|
||||
raise ValueError("producer outputs must name the declaring producer_id")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ProductionMetadata:
|
||||
producer_schema: str
|
||||
restorer_schema: str
|
||||
format_metadata: CanonicalJson = field(default_factory=CanonicalJson.empty)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if (
|
||||
not isinstance(self.producer_schema, str)
|
||||
or not self.producer_schema
|
||||
or not isinstance(self.restorer_schema, str)
|
||||
or not self.restorer_schema
|
||||
):
|
||||
raise ValueError("producer and restorer schema identifiers must not be empty")
|
||||
if not isinstance(self.format_metadata, CanonicalJson):
|
||||
raise ValueError("production format metadata must use CanonicalJson")
|
||||
|
||||
|
||||
class WeightProducer(Protocol):
|
||||
"""Synchronous V1 producer; callers do not preempt an active build."""
|
||||
|
||||
@property
|
||||
def spec(self) -> WeightProductionSpec: ...
|
||||
|
||||
def produce(self, writer: ArtifactWriter) -> ProductionMetadata:
|
||||
"""Produce one exact artifact synchronously or raise."""
|
||||
...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class BuildRequest:
|
||||
identity: WeightArtifactIdentity
|
||||
|
||||
|
||||
class WeightRestorePlan(Protocol):
|
||||
"""A validated, one-shot model mutation plan."""
|
||||
|
||||
def commit(self) -> None:
|
||||
"""Mutate the target model exactly once or raise."""
|
||||
...
|
||||
|
||||
|
||||
class WeightRestorer(Protocol):
|
||||
"""Validate a lease-to-model restore without mutating either input."""
|
||||
|
||||
@property
|
||||
def schema(self) -> str: ...
|
||||
|
||||
def plan_restore(self, model: object, lease: HostWeightLease) -> WeightRestorePlan:
|
||||
"""Return a validated plan; all model mutation is deferred to commit()."""
|
||||
...
|
||||
|
||||
|
||||
class HostWeightStore(Protocol):
|
||||
"""Exact artifact access whose deadlines bound lock acquisition only."""
|
||||
|
||||
def lookup(
|
||||
self,
|
||||
identity: WeightArtifactIdentity,
|
||||
*,
|
||||
validation: ValidationLevel,
|
||||
deadline: float | None = None,
|
||||
) -> StoreResult: ...
|
||||
|
||||
def get_or_build(
|
||||
self,
|
||||
request: BuildRequest,
|
||||
producer: WeightProducer,
|
||||
*,
|
||||
validation: ValidationLevel,
|
||||
deadline: float,
|
||||
) -> StoreResult: ...
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RemoteArtifactDescriptor:
|
||||
identity: WeightArtifactIdentity
|
||||
provider_id: str
|
||||
manifest_sha256: str
|
||||
content_sha256: str
|
||||
|
||||
|
||||
class RemoteArtifactProvider(Protocol):
|
||||
@property
|
||||
def provider_id(self) -> str: ...
|
||||
|
||||
def probe(self, identity: WeightArtifactIdentity) -> RemoteArtifactDescriptor | None: ...
|
||||
|
||||
def fetch(self, descriptor: RemoteArtifactDescriptor, writer: ArtifactWriter) -> ProductionMetadata: ...
|
||||
|
||||
|
||||
__all__ = [
|
||||
"ArtifactWriter",
|
||||
"BuildRequest",
|
||||
"CoordinationScope",
|
||||
"HostWeightStore",
|
||||
"LookupPhase",
|
||||
"ProductionMetadata",
|
||||
"ProductionSourceMode",
|
||||
"RemoteArtifactDescriptor",
|
||||
"RemoteArtifactProvider",
|
||||
"TensorFileWriter",
|
||||
"TensorWriteSpec",
|
||||
"WeightProducer",
|
||||
"WeightProductionSpec",
|
||||
"WeightRestorePlan",
|
||||
"WeightRestorer",
|
||||
]
|
||||
@@ -0,0 +1,332 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM-Omni project
|
||||
"""Policy-driven exact resolution over HostWeightStore contracts."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from collections.abc import Callable
|
||||
|
||||
from .config import HostWeightRuntimeConfig, RemoteOnMiss, RuntimeMode
|
||||
from .errors import FailureCode, HostWeightError, HostWeightFailure, ResolutionAction, ResolutionStage
|
||||
from .identity import CanonicalJson, WeightArtifactIdentity
|
||||
from .lease import HostWeightLease
|
||||
from .outcomes import (
|
||||
AttemptResult,
|
||||
HostWeightResolution,
|
||||
ResolutionAttempt,
|
||||
ResolutionOutcome,
|
||||
ResolutionReport,
|
||||
StoreResult,
|
||||
StoreStatus,
|
||||
)
|
||||
from .protocols import BuildRequest, HostWeightStore, LookupPhase, WeightProducer
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class HostWeightRuntime:
|
||||
"""Resolve one exact representation without owning model restoration."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config: HostWeightRuntimeConfig,
|
||||
store: HostWeightStore | None = None,
|
||||
*,
|
||||
observer: Callable[[ResolutionReport], None] | None = None,
|
||||
_initialization_failure: HostWeightFailure | None = None,
|
||||
) -> None:
|
||||
if observer is not None and not callable(observer):
|
||||
raise ValueError("Host Weight Runtime observer must be callable")
|
||||
if config.mode is RuntimeMode.DISABLED and (store is not None or _initialization_failure is not None):
|
||||
raise ValueError("disabled Host Weight Runtime must not initialize a store")
|
||||
if config.mode is not RuntimeMode.DISABLED and (store is None) == (_initialization_failure is None):
|
||||
raise ValueError("enabled Host Weight Runtime requires exactly one store or initialization failure")
|
||||
self.config = config
|
||||
self.store = store
|
||||
self._initialization_failure = _initialization_failure
|
||||
self._observer = observer
|
||||
|
||||
@classmethod
|
||||
def from_config(
|
||||
cls,
|
||||
config: HostWeightRuntimeConfig,
|
||||
*,
|
||||
observer: Callable[[ResolutionReport], None] | None = None,
|
||||
) -> HostWeightRuntime:
|
||||
"""Construct lazily so disabled mode performs no filesystem probe."""
|
||||
if config.mode is RuntimeMode.DISABLED:
|
||||
return cls(config, observer=observer)
|
||||
assert config.domain is not None
|
||||
from .filesystem import FilesystemHostWeightStore # noqa: PLC0415
|
||||
|
||||
try:
|
||||
store = FilesystemHostWeightStore(config.domain, config.capacity, config.integrity)
|
||||
except HostWeightError as exc:
|
||||
return cls(config, observer=observer, _initialization_failure=exc.failure)
|
||||
return cls(config, store, observer=observer)
|
||||
|
||||
@property
|
||||
def enabled(self) -> bool:
|
||||
return self.config.mode is not RuntimeMode.DISABLED
|
||||
|
||||
def resolve(
|
||||
self,
|
||||
identity: WeightArtifactIdentity | None = None,
|
||||
*,
|
||||
producer: WeightProducer | None = None,
|
||||
) -> HostWeightResolution:
|
||||
started = time.monotonic()
|
||||
if not self.enabled:
|
||||
return self._finish(
|
||||
ResolutionReport(
|
||||
resolution_id=uuid.uuid4().hex,
|
||||
outcome=ResolutionOutcome.CANONICAL_DIRECT,
|
||||
attempts=(),
|
||||
elapsed_seconds=time.monotonic() - started,
|
||||
)
|
||||
)
|
||||
if identity is None:
|
||||
raise ValueError("enabled Host Weight Runtime requires an exact artifact identity")
|
||||
if self._initialization_failure is not None:
|
||||
failure = self._initialization_failure
|
||||
action = self._action_for_failure(failure)
|
||||
return self._finish_terminal(
|
||||
(
|
||||
ResolutionAttempt(
|
||||
stage=failure.stage,
|
||||
result=AttemptResult.ERROR,
|
||||
action=action,
|
||||
elapsed_seconds=time.monotonic() - started,
|
||||
failure=failure,
|
||||
),
|
||||
),
|
||||
started,
|
||||
)
|
||||
assert self.store is not None
|
||||
deadline = started + self.config.wait.coordination_timeout_seconds
|
||||
attempts: list[ResolutionAttempt] = []
|
||||
|
||||
lookup_started = time.monotonic()
|
||||
local = self.store.lookup(
|
||||
identity,
|
||||
validation=self.config.integrity.local_lookup,
|
||||
deadline=deadline,
|
||||
)
|
||||
if local.status is StoreStatus.HIT:
|
||||
assert local.lease is not None
|
||||
attempts.append(
|
||||
ResolutionAttempt(
|
||||
stage=ResolutionStage.LOOKUP,
|
||||
result=AttemptResult.HIT,
|
||||
action=ResolutionAction.RETURN_LEASE,
|
||||
elapsed_seconds=time.monotonic() - lookup_started,
|
||||
)
|
||||
)
|
||||
return self._finish(
|
||||
ResolutionReport(
|
||||
resolution_id=local.lease.provenance.resolution_id,
|
||||
outcome=ResolutionOutcome.LOCAL_HIT,
|
||||
attempts=tuple(attempts),
|
||||
elapsed_seconds=time.monotonic() - started,
|
||||
),
|
||||
lease=local.lease,
|
||||
)
|
||||
|
||||
local_action = self._next_after_local(local, producer)
|
||||
attempts.append(
|
||||
self._store_attempt(
|
||||
ResolutionStage.LOOKUP,
|
||||
local,
|
||||
time.monotonic() - lookup_started,
|
||||
action=local_action,
|
||||
)
|
||||
)
|
||||
if local.status in {StoreStatus.TIMEOUT, StoreStatus.FAILED}:
|
||||
return self._finish_terminal(tuple(attempts), started)
|
||||
|
||||
producer_allowed = self._producer_allowed(producer)
|
||||
if self.config.remote.on_local_miss is not RemoteOnMiss.DISABLED:
|
||||
remote_failure = HostWeightFailure(
|
||||
stage=ResolutionStage.REMOTE,
|
||||
code=FailureCode.UNSUPPORTED,
|
||||
retryable=False,
|
||||
message="remote artifact providers are modeled but not implemented",
|
||||
details=CanonicalJson.empty(),
|
||||
)
|
||||
attempts.append(
|
||||
ResolutionAttempt(
|
||||
stage=ResolutionStage.REMOTE,
|
||||
result=AttemptResult.SKIPPED,
|
||||
action=self._action_for_failure(remote_failure),
|
||||
elapsed_seconds=0.0,
|
||||
failure=remote_failure,
|
||||
)
|
||||
)
|
||||
return self._finish_terminal(tuple(attempts), started)
|
||||
|
||||
if producer_allowed:
|
||||
assert producer is not None
|
||||
production_started = time.monotonic()
|
||||
produced = self.store.get_or_build(
|
||||
BuildRequest(identity),
|
||||
producer,
|
||||
validation=self.config.integrity.local_lookup,
|
||||
deadline=deadline,
|
||||
)
|
||||
if produced.status in {StoreStatus.HIT, StoreStatus.BUILT, StoreStatus.JOINED}:
|
||||
assert produced.lease is not None
|
||||
attempts.append(
|
||||
ResolutionAttempt(
|
||||
stage=ResolutionStage.PRODUCTION,
|
||||
result=AttemptResult.SUCCESS,
|
||||
action=ResolutionAction.RETURN_LEASE,
|
||||
elapsed_seconds=time.monotonic() - production_started,
|
||||
)
|
||||
)
|
||||
return self._finish(
|
||||
ResolutionReport(
|
||||
resolution_id=produced.lease.provenance.resolution_id,
|
||||
outcome=ResolutionOutcome.LOCAL_PRODUCTION,
|
||||
attempts=tuple(attempts),
|
||||
elapsed_seconds=time.monotonic() - started,
|
||||
),
|
||||
lease=produced.lease,
|
||||
)
|
||||
attempts.append(
|
||||
self._store_attempt(
|
||||
ResolutionStage.PRODUCTION,
|
||||
produced,
|
||||
time.monotonic() - production_started,
|
||||
action=self._terminal_action_for_store_result(produced),
|
||||
)
|
||||
)
|
||||
else:
|
||||
unavailable = HostWeightFailure(
|
||||
stage=ResolutionStage.PRODUCTION,
|
||||
code=FailureCode.PRODUCER_UNAVAILABLE,
|
||||
retryable=True,
|
||||
message="no allowed producer can build the exact requested representation",
|
||||
details=CanonicalJson.empty(),
|
||||
)
|
||||
attempts.append(
|
||||
ResolutionAttempt(
|
||||
stage=ResolutionStage.PRODUCTION,
|
||||
result=AttemptResult.SKIPPED,
|
||||
action=self._action_for_failure(unavailable),
|
||||
elapsed_seconds=0.0,
|
||||
failure=unavailable,
|
||||
)
|
||||
)
|
||||
|
||||
return self._finish_terminal(tuple(attempts), started)
|
||||
|
||||
def _mode_terminal_action(self) -> ResolutionAction:
|
||||
if self.config.mode is RuntimeMode.PREFERRED:
|
||||
return ResolutionAction.CANONICAL_FALLBACK
|
||||
return ResolutionAction.FAIL_STARTUP
|
||||
|
||||
def _action_for_failure(self, failure: HostWeightFailure | None) -> ResolutionAction:
|
||||
if failure is None:
|
||||
raise ValueError("failed store results require a typed failure")
|
||||
if self.config.mode is RuntimeMode.PREFERRED and failure.retryable:
|
||||
return ResolutionAction.CANONICAL_FALLBACK
|
||||
return ResolutionAction.FAIL_STARTUP
|
||||
|
||||
def _terminal_action_for_store_result(self, result: StoreResult) -> ResolutionAction:
|
||||
if result.status in {StoreStatus.MISS, StoreStatus.INVALID}:
|
||||
return self._mode_terminal_action()
|
||||
return self._action_for_failure(result.failure)
|
||||
|
||||
def _next_after_local(
|
||||
self,
|
||||
result: StoreResult,
|
||||
producer: WeightProducer | None,
|
||||
) -> ResolutionAction:
|
||||
if result.status in {StoreStatus.TIMEOUT, StoreStatus.FAILED}:
|
||||
return self._terminal_action_for_store_result(result)
|
||||
if self.config.remote.on_local_miss is not RemoteOnMiss.DISABLED:
|
||||
return ResolutionAction.TRY_REMOTE
|
||||
if self._producer_allowed(producer):
|
||||
if result.status is StoreStatus.INVALID:
|
||||
return ResolutionAction.QUARANTINE_THEN_PRODUCE
|
||||
return ResolutionAction.TRY_PRODUCER
|
||||
return self._mode_terminal_action()
|
||||
|
||||
def _producer_allowed(self, producer: WeightProducer | None) -> bool:
|
||||
return (
|
||||
producer is not None
|
||||
and self.config.production.allow_local_build
|
||||
and producer.spec.lookup_phase is LookupPhase.PRE_LOAD_SAFE
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _store_attempt(
|
||||
stage: ResolutionStage,
|
||||
result: StoreResult,
|
||||
elapsed: float,
|
||||
*,
|
||||
action: ResolutionAction,
|
||||
) -> ResolutionAttempt:
|
||||
if result.status is StoreStatus.MISS:
|
||||
return ResolutionAttempt(
|
||||
stage=stage,
|
||||
result=AttemptResult.MISS,
|
||||
action=action,
|
||||
elapsed_seconds=elapsed,
|
||||
)
|
||||
if result.status is StoreStatus.TIMEOUT:
|
||||
attempt_result = AttemptResult.TIMEOUT
|
||||
elif result.status is StoreStatus.INVALID:
|
||||
attempt_result = AttemptResult.ERROR
|
||||
else:
|
||||
attempt_result = AttemptResult.ERROR
|
||||
return ResolutionAttempt(
|
||||
stage=stage,
|
||||
result=attempt_result,
|
||||
action=action,
|
||||
elapsed_seconds=elapsed,
|
||||
failure=result.failure,
|
||||
)
|
||||
|
||||
def _finish_terminal(
|
||||
self,
|
||||
attempts: tuple[ResolutionAttempt, ...],
|
||||
started: float,
|
||||
) -> HostWeightResolution:
|
||||
action = attempts[-1].action
|
||||
if action is ResolutionAction.CANONICAL_FALLBACK:
|
||||
outcome = ResolutionOutcome.CANONICAL_FALLBACK
|
||||
elif action is ResolutionAction.FAIL_STARTUP:
|
||||
outcome = ResolutionOutcome.FAILED
|
||||
else:
|
||||
raise ValueError(f"resolution cannot terminate with action {action.value}")
|
||||
return self._finish(
|
||||
ResolutionReport(
|
||||
resolution_id=uuid.uuid4().hex,
|
||||
outcome=outcome,
|
||||
attempts=attempts,
|
||||
elapsed_seconds=time.monotonic() - started,
|
||||
)
|
||||
)
|
||||
|
||||
def _finish(
|
||||
self,
|
||||
report: ResolutionReport,
|
||||
*,
|
||||
lease: HostWeightLease | None = None,
|
||||
) -> HostWeightResolution:
|
||||
if self._observer is not None:
|
||||
try:
|
||||
self._observer(report)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Host Weight Runtime observer failed",
|
||||
extra={"resolution_id": report.resolution_id},
|
||||
)
|
||||
return HostWeightResolution(report=report, lease=lease)
|
||||
|
||||
|
||||
__all__ = ["HostWeightRuntime"]
|
||||
Reference in new issue
Block a user