mirror of
https://github.com/elder-plinius/OBLITERATUS.git
synced 2026-12-06 02:53:54 +01:00
Review of 9eabacc: lstat/O_NOFOLLOW only protected the final path
component, so a symlinked ancestor (e.g. /var/run) was accepted and
mkdir(parents=True) followed ancestors before validation. The opt-in log
directory is now walked from "/" one component at a time with
O_DIRECTORY|O_NOFOLLOW relative to the parent fd, missing components are
created 0700 relative to that fd, relative paths and ".." are rejected,
and ownership/permissions are checked on the final opened fd.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
233 lines
8.2 KiB
Python
233 lines
8.2 KiB
Python
"""CPU-importable launch helpers for the conditional Llama TP qualification."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ipaddress
|
|
import os
|
|
import signal
|
|
import socket
|
|
import stat
|
|
import subprocess
|
|
import sys
|
|
import uuid
|
|
from contextlib import ExitStack
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
_VIRTUAL_INTERFACE_PREFIXES = (
|
|
"docker", "br-", "veth", "virbr", "cni", "flannel", "tailscale", "lo",
|
|
)
|
|
_PRIVATE_NETWORKS = tuple(
|
|
ipaddress.ip_network(cidr) for cidr in ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16")
|
|
)
|
|
|
|
|
|
def _torchrun_command(
|
|
*,
|
|
shape: str,
|
|
master_addr: str,
|
|
master_port: int,
|
|
rendezvous_id: str,
|
|
worker_path: Path,
|
|
job_path: Path,
|
|
) -> list[str]:
|
|
if shape == "local_tp2":
|
|
topology = ["--nnodes=1", "--nproc-per-node=2"]
|
|
# Static rendezvous preserves the profiled worker MASTER_ADDR and MASTER_PORT.
|
|
rendezvous = [
|
|
"--rdzv-backend=static",
|
|
f"--master-addr={master_addr}",
|
|
f"--master-port={master_port}",
|
|
"--node-rank=0",
|
|
]
|
|
elif shape == "two_host_tp2":
|
|
topology = ["--nnodes=2", "--nproc-per-node=1"]
|
|
rendezvous = [
|
|
"--rdzv-backend=c10d",
|
|
f"--rdzv-endpoint={master_addr}:{master_port}",
|
|
]
|
|
else:
|
|
raise ValueError(f"unknown TP qualification shape: {shape}")
|
|
return [
|
|
sys.executable,
|
|
"-m",
|
|
"torch.distributed.run",
|
|
*topology,
|
|
*rendezvous,
|
|
f"--rdzv-id={rendezvous_id}",
|
|
"--max-restarts=0",
|
|
str(worker_path),
|
|
"--obliteratus-tp-worker",
|
|
str(job_path),
|
|
]
|
|
|
|
|
|
def _private_network_profile() -> tuple[str, str, str, int]:
|
|
import psutil # type: ignore[import-untyped]
|
|
|
|
interfaces = psutil.net_if_addrs()
|
|
override = os.environ.get("OBLITERATUS_TP_INTERFACE")
|
|
candidates = (
|
|
[override]
|
|
if override is not None
|
|
else sorted(
|
|
interfaces,
|
|
key=lambda name: (name.startswith(_VIRTUAL_INTERFACE_PREFIXES), name),
|
|
)
|
|
)
|
|
for interface in candidates:
|
|
for record in interfaces.get(interface, ()):
|
|
if record.family != socket.AF_INET:
|
|
continue
|
|
if not isinstance(record.address, str):
|
|
continue
|
|
try:
|
|
address = ipaddress.ip_address(record.address)
|
|
except ValueError:
|
|
continue
|
|
if not any(address in network for network in _PRIVATE_NETWORKS):
|
|
continue
|
|
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as listener:
|
|
listener.bind((record.address, 0))
|
|
port = int(listener.getsockname()[1])
|
|
return interface, record.address, f"{record.address}/32", port
|
|
if override is not None:
|
|
pytest.skip(
|
|
f"LMS-E2 non-coverage: OBLITERATUS_TP_INTERFACE={override!r} must name "
|
|
"an interface with an RFC1918 IPv4 address"
|
|
)
|
|
pytest.skip("LMS-E2 non-coverage: no private numeric runner interface is available")
|
|
raise AssertionError("pytest.skip unexpectedly returned")
|
|
|
|
|
|
def _worker_environment(
|
|
*,
|
|
repository_root: Path,
|
|
run_id: str,
|
|
interface: str,
|
|
) -> tuple[dict[str, str], list[str]]:
|
|
optional = {
|
|
"CUDA_HOME",
|
|
"CUDA_VISIBLE_DEVICES",
|
|
"GROUP_WORLD_SIZE",
|
|
"HOME",
|
|
"LANG",
|
|
"LC_ALL",
|
|
"LC_CTYPE", # Python may add this during PEP 538 locale coercion.
|
|
"LD_LIBRARY_PATH",
|
|
"NVIDIA_VISIBLE_DEVICES",
|
|
"OMP_NUM_THREADS",
|
|
"PATH",
|
|
"PYTHONIOENCODING",
|
|
"PYTHONNOUSERSITE",
|
|
"PYTHONPATH",
|
|
"PYTHONUNBUFFERED",
|
|
"ROLE_NAME", # torchrun 2.13 sets each worker's role name.
|
|
"TMPDIR",
|
|
"TORCHELASTIC_ERROR_FILE",
|
|
"TORCHELASTIC_SIGNALS_TO_HANDLE", # torchrun 2.13 sets each worker's handled signals.
|
|
"TORCHELASTIC_USE_AGENT_STORE",
|
|
"TORCH_NCCL_ASYNC_ERROR_HANDLING",
|
|
"VIRTUAL_ENV",
|
|
}
|
|
environment = {name: value for name in optional if (value := os.environ.get(name)) is not None}
|
|
environment.update(
|
|
{
|
|
"GLOO_SOCKET_IFNAME": interface,
|
|
"NCCL_SOCKET_IFNAME": interface,
|
|
"OBLITERATUS_RUN_ID": run_id,
|
|
"OMP_NUM_THREADS": "1",
|
|
"PATH": os.environ.get("PATH", "/usr/bin:/bin"),
|
|
"PYTHONNOUSERSITE": "1",
|
|
"PYTHONPATH": str(repository_root),
|
|
"PYTHONUNBUFFERED": "1",
|
|
}
|
|
)
|
|
return environment, sorted(optional)
|
|
|
|
|
|
def _open_worker_log_directory(directory: Path) -> int:
|
|
setting = f"OBLITERATUS_TP_WORKER_LOG_DIR={str(directory)!r}"
|
|
if not directory.is_absolute() or ".." in directory.parts:
|
|
raise ValueError(f"{setting} must be an absolute path without '..' components")
|
|
flags = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
|
try:
|
|
descriptor = os.open("/", flags)
|
|
try:
|
|
before = None
|
|
for index, component in enumerate(directory.parts[1:], start=1):
|
|
final = index == len(directory.parts) - 1
|
|
if final:
|
|
try:
|
|
before = os.stat(component, dir_fd=descriptor, follow_symlinks=False)
|
|
except FileNotFoundError:
|
|
pass
|
|
try:
|
|
child = os.open(component, flags, dir_fd=descriptor)
|
|
except FileNotFoundError:
|
|
os.mkdir(component, 0o700, dir_fd=descriptor)
|
|
if final:
|
|
before = os.stat(component, dir_fd=descriptor, follow_symlinks=False)
|
|
child = os.open(component, flags, dir_fd=descriptor)
|
|
parent, descriptor = descriptor, child
|
|
os.close(parent)
|
|
opened = os.fstat(descriptor)
|
|
if not stat.S_ISDIR(opened.st_mode):
|
|
raise ValueError(f"{setting} must be a directory, not a symlink or other file")
|
|
if before is not None and (
|
|
opened.st_dev, opened.st_ino
|
|
) != (before.st_dev, before.st_ino):
|
|
raise ValueError(f"{setting} changed while opening the directory")
|
|
if opened.st_uid != os.getuid():
|
|
raise ValueError(f"{setting} must be owned by the current uid")
|
|
if opened.st_mode & (stat.S_IWGRP | stat.S_IWOTH):
|
|
raise ValueError(f"{setting} must have no group/other write bits")
|
|
except BaseException:
|
|
os.close(descriptor)
|
|
raise
|
|
except OSError as error:
|
|
raise ValueError(
|
|
f"{setting} could not be securely opened as a directory, "
|
|
f"not a symlink or other file: {error}"
|
|
) from error
|
|
return descriptor
|
|
|
|
|
|
def _run_process_group(command: list[str], *, cwd: Path, environment: dict[str, str]) -> int:
|
|
with ExitStack() as logs:
|
|
stdout = stderr = subprocess.DEVNULL
|
|
log_directory = os.environ.get("OBLITERATUS_TP_WORKER_LOG_DIR")
|
|
if log_directory is not None:
|
|
# Logs can contain env-derived paths; pin a validated private directory.
|
|
directory_descriptor = _open_worker_log_directory(Path(log_directory))
|
|
logs.callback(os.close, directory_descriptor)
|
|
log_id = uuid.uuid4().hex
|
|
streams = []
|
|
for name in ("stdout", "stderr"):
|
|
descriptor = os.open(
|
|
f"llama-tp-{log_id}-{name}.log",
|
|
os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW,
|
|
0o600,
|
|
dir_fd=directory_descriptor,
|
|
)
|
|
logs.callback(os.close, descriptor)
|
|
streams.append(descriptor)
|
|
stdout, stderr = streams
|
|
process = subprocess.Popen(
|
|
command,
|
|
cwd=cwd,
|
|
env=environment,
|
|
stdout=stdout,
|
|
stderr=stderr,
|
|
start_new_session=True,
|
|
)
|
|
try:
|
|
process.wait(timeout=600)
|
|
except subprocess.TimeoutExpired:
|
|
os.killpg(process.pid, signal.SIGKILL)
|
|
process.wait(timeout=30)
|
|
pytest.fail("local TP worker qualification exceeded its process-hard deadline")
|
|
return int(process.returncode)
|