feat: harden node placement and partial model loading

This commit is contained in:
Dobromir Popov
2026-07-13 21:58:08 +02:00
parent a6bcc69288
commit 5d87e81bc9
21 changed files with 497 additions and 55 deletions

View File

@@ -36,6 +36,12 @@ def _load_env_file(path: Path) -> None:
os.environ[key] = value
def _apply_relay_concurrency_flag(value: int | None) -> None:
"""Expose relay bridge worker cap via CLI (env MESHNET_RELAY_CONCURRENCY)."""
if value is not None:
os.environ["MESHNET_RELAY_CONCURRENCY"] = str(max(1, value))
def _load_env_defaults() -> None:
"""Load machine-specific, local, and user-level node env defaults."""
machine = socket.gethostname().strip()
@@ -189,6 +195,8 @@ def _cmd_default(args) -> int:
if getattr(args, "cpu", False):
overrides["force_cpu"] = True
_apply_relay_concurrency_flag(getattr(args, "relay_concurrency", None))
if overrides:
cfg = merge_cli_overrides(cfg, **overrides)
@@ -349,6 +357,8 @@ def _cmd_start(args) -> int:
if getattr(args, "node_name", None):
cfg["node_name"] = args.node_name
_apply_relay_concurrency_flag(getattr(args, "relay_concurrency", None))
# Legacy start: just run without the dashboard (keep original blocking loop)
from .startup import run_startup
@@ -433,6 +443,8 @@ def main() -> None:
help="Set PyTorch inter-op CPU worker threads")
parser.add_argument("--cpu", action="store_true",
help="Force CPU inference even when a GPU is available")
parser.add_argument("--relay-concurrency", type=int, metavar="N",
help="Max concurrent relay-http-request workers (env MESHNET_RELAY_CONCURRENCY)")
parser.add_argument("--debug", action="store_true", help="Enable verbose node debug logging")
parser.add_argument("--no-tui", action="store_true", help="Plain-text output (no rich dashboard)")
parser.add_argument("--compact", action="store_true", help="Single-line status output")
@@ -510,6 +522,8 @@ def main() -> None:
help="Set PyTorch inter-op CPU worker threads")
start_cmd.add_argument("--cpu", action="store_true",
help="Force CPU inference even when a GPU is available")
start_cmd.add_argument("--relay-concurrency", type=int, metavar="N",
help="Max concurrent relay-http-request workers (env MESHNET_RELAY_CONCURRENCY)")
start_cmd.add_argument("--debug", action="store_true", help="Enable verbose node debug logging")
start_cmd.add_argument("--tracker-source-disabled", action="store_true",
help="Skip tracker/peer model-file sources and download from HuggingFace directly")

View File

@@ -899,12 +899,41 @@ def _load_partial_model_from_snapshot(
dtype=dtype,
)
for module in _active_modules_for_shard(model, shard_start, shard_end):
if hasattr(module, "to"):
module.to(device)
_finalize_active_shard_modules_on_device(model, shard_start, shard_end, device)
return model
def _finalize_active_shard_modules_on_device(
model: Any, shard_start: int, shard_end: int, device: Any
) -> None:
"""Place active shard modules on device without copying unmaterialized meta weights."""
for module in _active_modules_for_shard(model, shard_start, shard_end):
parameters = getattr(module, "parameters", None)
if not callable(parameters):
if hasattr(module, "to"):
module.to(device)
continue
params = list(parameters(recurse=True))
buffers_fn = getattr(module, "buffers", None)
buffers = list(buffers_fn(recurse=True)) if callable(buffers_fn) else []
tensors = params + buffers
if not tensors:
if hasattr(module, "to"):
module.to(device)
continue
if all(tensor.device.type == "meta" for tensor in tensors):
to_empty = getattr(module, "to_empty", None)
if callable(to_empty):
to_empty(device)
continue
if all(tensor.device.type != "meta" for tensor in tensors):
if hasattr(module, "to"):
module.to(device)
continue
# Partially materialized: set_module_tensor_to_device already placed loaded
# weights on the target device; leave remaining meta parameters untouched.
def _model_load_plan(
auto_config: Any,
model_id: str,