File size: 9,093 Bytes
e8b6587 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | from __future__ import annotations
import copy
import hashlib
import importlib.metadata as importlib_metadata
import json
import logging
import platform
import resource
import shutil
import subprocess
import sys
import time
import uuid
from dataclasses import dataclass
from pathlib import Path
import torch
def open_request_logger(log_path: Path, request_id: str, formatter: logging.Formatter):
"""Open one append-only FileHandler for one UUID request; caller must close it."""
logger = logging.getLogger(f"ltx25.request.{request_id}")
logger.setLevel(logging.DEBUG)
logger.propagate = True
handler = logging.FileHandler(log_path, mode="a", encoding="utf-8", delay=False)
handler.setLevel(logging.DEBUG)
handler.setFormatter(formatter)
logger.addHandler(handler)
return logger, handler
def close_request_logger(logger, handler) -> None:
if logger is None or handler is None:
return
try:
handler.flush()
finally:
logger.removeHandler(handler)
handler.close()
def is_uuid_hex(value: str) -> bool:
value = str(value or "").strip().lower()
return len(value) == 32 and all(ch in "0123456789abcdef" for ch in value)
def hf_repo_url(repo_id: str, repo_type: str = "model") -> str:
"""Return the canonical Hugging Face Hub page for one repository."""
repo_id = str(repo_id or "").strip().strip("/")
repo_type = str(repo_type or "model").strip().lower()
prefix = {"model": "", "dataset": "datasets/", "space": "spaces/"}.get(repo_type)
if not repo_id or prefix is None:
return ""
return f"https://huggingface.co/{prefix}{repo_id}"
def hf_repo_markdown_link(repo_id: str, repo_type: str = "model") -> str:
"""Render a repo ID as a beginner-friendly Markdown link when possible."""
repo_id = str(repo_id or "").strip()
url = hf_repo_url(repo_id, repo_type)
return f"[{repo_id}]({url})" if url else f"`{repo_id}`"
def create_session_id() -> str:
return uuid.uuid4().hex
def normalize_session_id(value) -> str:
value = str(value or "").strip().lower()
return value if is_uuid_hex(value) else create_session_id()
def cleanup_session_results(worker_root: Path, session_id) -> None:
value = str(session_id or "").strip().lower()
if not is_uuid_hex(value):
return
root = worker_root / value
try:
shutil.rmtree(root, ignore_errors=True)
except Exception:
pass
@dataclass(frozen=True)
class RequestPaths:
session_id: str
request_id: str
root: Path
video: Path
diagnostics: Path
run_info: Path
log: Path
probe: Path
def create_request_paths(worker_root: Path, session_id) -> RequestPaths:
session_id = normalize_session_id(session_id)
request_id = uuid.uuid4().hex
root = worker_root / session_id / request_id
root.mkdir(parents=True, mode=0o700, exist_ok=False)
return RequestPaths(
session_id=session_id,
request_id=request_id,
root=root,
video=root / f"ltx25_{request_id}.mp4",
diagnostics=root / f"ltx25_request_{request_id}.json",
run_info=root / f"ltx25_run_{request_id}.json",
log=root / f"ltx25_request_{request_id}.log",
probe=root / f"ltx25_probe_{request_id}.zip",
)
def monotonic_elapsed(started: float) -> float:
"""Return monotonic elapsed seconds for Probe/runtime phase timing."""
return time.monotonic() - float(started)
def process_rss_kib() -> int:
"""Return process max RSS using the platform value already used by runtime diagnostics."""
return int(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
def reset_gpu_peak_memory() -> None:
"""Reset CUDA peak counters when CUDA is available; safe on CPU-only runtimes."""
if torch.cuda.is_available():
torch.cuda.reset_peak_memory_stats()
def disk_state() -> dict:
d = shutil.disk_usage(Path.home())
return {"total": d.total, "used": d.used, "free": d.free}
def gpu_state() -> dict:
if not torch.cuda.is_available():
return {"cuda_available": False}
free, total = torch.cuda.mem_get_info()
return {
"cuda_available": True,
"device": torch.cuda.get_device_name(0),
"compute_capability": list(torch.cuda.get_device_capability(0)),
"torch_cuda": torch.version.cuda,
"free_bytes": int(free),
"total_bytes": int(total),
"allocated_bytes": int(torch.cuda.memory_allocated()),
"max_allocated_bytes": int(torch.cuda.max_memory_allocated()),
"reserved_bytes": int(torch.cuda.memory_reserved()),
"max_reserved_bytes": int(torch.cuda.max_memory_reserved()),
}
def _nvidia_driver_version() -> str | None:
if not torch.cuda.is_available():
return None
try:
proc = subprocess.run(
["nvidia-smi", "--query-gpu=driver_version", "--format=csv,noheader"],
capture_output=True,
text=True,
timeout=2.0,
check=False,
)
if proc.returncode == 0:
first = (proc.stdout or "").strip().splitlines()
if first:
return first[0].strip() or None
except Exception:
pass
return None
def runtime_environment_identity() -> dict:
"""Return process-level runtime identity.
On ZeroGPU this may run during startup CUDA emulation, so its GPU fields are
preload context rather than authoritative execution-GPU evidence.
"""
identity = {
"python": {
"version": platform.python_version(),
"implementation": platform.python_implementation(),
"compiler": platform.python_compiler(),
},
"platform": {
"system": platform.system(),
"release": platform.release(),
"version": platform.version(),
"machine": platform.machine(),
"platform": platform.platform(),
},
"process": {
"python_executable": sys.executable,
},
}
if torch.cuda.is_available():
free, total = torch.cuda.mem_get_info()
identity["cuda"] = {
"torch_cuda": torch.version.cuda,
"driver_version": _nvidia_driver_version(),
}
identity["gpu"] = {
"device": torch.cuda.get_device_name(0),
"compute_capability": list(torch.cuda.get_device_capability(0)),
"total_bytes": int(total),
"free_bytes_at_capture": int(free),
}
else:
identity["cuda"] = {"torch_cuda": torch.version.cuda, "driver_version": None}
identity["gpu"] = {"cuda_available": False}
return identity
def execution_environment_identity(preload_environment: dict | None = None) -> dict:
"""Return runtime identity with GPU fields captured inside the active GPU callback.
ZeroGPU exposes CUDA emulation during module startup and a real allocated GPU
only inside ``@spaces.GPU``. Preserve stable process/platform fields from the
preload capture, but replace CUDA/GPU identity with the callback-visible device.
"""
identity = copy.deepcopy(preload_environment or runtime_environment_identity())
identity["capture_scope"] = "gpu_callback_execution"
if torch.cuda.is_available():
free, total = torch.cuda.mem_get_info()
cuda_record = dict(identity.get("cuda") or {})
cuda_record["torch_cuda"] = torch.version.cuda
identity["cuda"] = cuda_record
identity["gpu"] = {
"device": torch.cuda.get_device_name(0),
"compute_capability": list(torch.cuda.get_device_capability(0)),
"total_bytes": int(total),
"free_bytes_at_capture": int(free),
}
else:
identity["gpu"] = {"cuda_available": False}
return identity
def resolved_revision_from_hub_path(path: str | Path) -> str | None:
"""Return the snapshot commit encoded in a Hugging Face Hub cache path."""
parts = list(Path(path).parts)
if "snapshots" in parts:
snap_idx = parts.index("snapshots")
if snap_idx + 1 < len(parts):
return parts[snap_idx + 1]
return None
def package_identity(name: str) -> dict:
out = {"name": name}
try:
dist = importlib_metadata.distribution(name)
out["version"] = dist.version
direct = dist.read_text("direct_url.json")
if direct:
payload = json.loads(direct)
out["direct_url"] = payload.get("url")
vcs = payload.get("vcs_info") or {}
if vcs.get("commit_id"):
out["vcs_commit"] = vcs["commit_id"]
if vcs.get("requested_revision"):
out["requested_revision"] = vcs["requested_revision"]
except Exception as exc:
out["identity_error"] = f"{type(exc).__name__}: {exc}"
return out
def sha256_file(path: str | Path) -> str:
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(1024 * 1024), b""):
h.update(chunk)
return h.hexdigest()
|