from __future__ import annotations
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Any
from modelexpress import p2p_pb2
from modelexpress.client import MxClientBase
from modelexpress_rl.train import WeightPayloadFormat
class GeneratorEngineContext(ABC):
@dataclass(frozen=True)
class GeneratorSource:
source_slot_id: str
worker_id: str
manifest_endpoint: str
manifest_digest: str
transport: str
manifest: bytes
@dataclass(frozen=True)
class GeneratorTransferInputs:
version_id: str
layout_signature: str
payload_format: WeightPayloadFormat
sources: tuple[GeneratorSource, ...]
@property
def physical_fingerprint(self) -> tuple:
return (
self.layout_signature,
self.payload_format,
tuple(
(
source.source_slot_id,
source.worker_id,
source.manifest_endpoint,
source.manifest_digest,
source.transport,
)
for source in self.sources
),
)
class GeneratorEngineAdapter(ABC):
@property
@abstractmethod
def worker_rank(self) -> int:
@abstractmethod
def build_p2p_identity(self, version_id: str) -> p2p_pb2.SourceIdentity:
@abstractmethod
def stage_peer_weight(self, source: p2p_pb2.WorkerMetadata) -> object:
@abstractmethod
def publish_weight_version(
self,
*,
version_id: str,
staged: object,
p2p_client: MxClientBase,
worker_id: str,
) -> None:
@property
@abstractmethod
def supported_payload_formats(self) -> frozenset[WeightPayloadFormat]:
@abstractmethod
def create_transfer_plan(self, inputs: GeneratorTransferInputs) -> Any:
@abstractmethod
def validate_transfer_plan(
self,
plan: Any,
inputs: GeneratorTransferInputs,
) -> bool:
@abstractmethod
def stage_weight(self, plan: Any) -> Any:
@abstractmethod
def apply_weight(self, staged: Any) -> Any:
@abstractmethod
def release_staged_weight(self, staged: Any) -> None:
@abstractmethod
def close(self) -> None:
__all__ = [
"GeneratorEngineContext",
"GeneratorEngineAdapter",
"GeneratorSource",
"GeneratorTransferInputs",
]