File size: 13,931 Bytes
85f2567 | 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | """Device selection and mixed precision for native PyTorch.
The order is XPU, then CUDA, then CPU. XPU means ``torch.xpu`` in an
official PyTorch build (2.5 or newer; 2.15 is the pinned Windows wheel).
This module does not import or call the archived IPEX package.
``CLOUDIE_ALLOW_GPU=1`` is required for XPU and CUDA alike. An explicit
accelerator without that gate is refused. Auto falls back to CPU and
logs the reason. CPU stays fp32. CUDA fp16 is the only path that uses
GradScaler. XPU uses bf16 autocast when native bf16 is supported, and
fp32 otherwise. ``cudnn.benchmark`` is not enabled.
"""
from __future__ import annotations
import inspect
import logging
import math
import os
from collections.abc import Iterator
from contextlib import contextmanager, nullcontext
from dataclasses import dataclass
LOGGER = logging.getLogger("src.device")
DEVICE_CHOICES = ("auto", "xpu", "cuda", "cpu")
GPU_DEVICES = ("xpu", "cuda")
GPU_GATE = "CLOUDIE_ALLOW_GPU"
def _torch():
import torch
return torch
def accelerator_available(torch_module, name: str) -> bool:
"""True when this torch build can see ``name``. Missing XPU attributes are false."""
if name == "cpu":
return True
if name == "xpu":
xpu = getattr(torch_module, "xpu", None)
checker = getattr(xpu, "is_available", None) if xpu is not None else None
if not callable(checker):
return False
try:
return bool(checker())
except Exception:
return False
if name == "cuda":
try:
return bool(torch_module.cuda.is_available())
except Exception:
return False
return False
def xpu_native_bf16(torch_module) -> bool:
"""True when native XPU bf16 is supported.
Uses ``torch.xpu.is_bf16_supported(including_emulation=False)`` when
that parameter exists, so emulated bf16 does not select the autocast
path. Older builds without the function, or whose call fails, stay
on fp32. A build that has the function but not the parameter is
called with no arguments.
"""
xpu = getattr(torch_module, "xpu", None)
checker = getattr(xpu, "is_bf16_supported", None) if xpu is not None else None
if not callable(checker):
return False
try:
parameters = inspect.signature(checker).parameters
has_kwarg = "including_emulation" in parameters
except (TypeError, ValueError):
has_kwarg = False
try:
if has_kwarg:
return bool(checker(including_emulation=False))
return bool(checker())
except TypeError:
try:
return bool(checker())
except Exception:
return False
except Exception:
return False
@dataclass(frozen=True)
class AmpPolicy:
"""How one device runs the forward and the backward."""
precision: str
autocast_device: str | None
dtype: object | None
use_grad_scaler: bool
def amp_policy_for(device, torch_module=None) -> AmpPolicy:
"""fp16+GradScaler on CUDA, native bf16 on XPU, fp32 everywhere else.
GradScaler is never constructed for XPU or CPU. A missing native bf16
on XPU falls back to fp32 with no autocast.
"""
torch_module = torch_module or _torch()
kind = device.type if hasattr(device, "type") else str(device)
if kind == "cuda":
return AmpPolicy("fp16", "cuda", torch_module.float16, True)
if kind == "xpu":
if xpu_native_bf16(torch_module):
return AmpPolicy("bf16", "xpu", torch_module.bfloat16, False)
return AmpPolicy("fp32", None, None, False)
return AmpPolicy("fp32", None, None, False)
@contextmanager
def autocast_context(policy: AmpPolicy) -> Iterator[None]:
"""Enter ``torch.autocast`` for the policy, or do nothing for fp32."""
if policy.autocast_device is None or policy.dtype is None:
with nullcontext():
yield
return
torch_module = _torch()
with torch_module.autocast(policy.autocast_device, dtype=policy.dtype):
yield
def make_grad_scaler(policy: AmpPolicy):
"""A CUDA fp16 GradScaler, or None. XPU and CPU never get one."""
if not policy.use_grad_scaler:
return None
if policy.precision != "fp16" or policy.autocast_device != "cuda":
raise RuntimeError("GradScaler is used only for cuda fp16")
torch_module = _torch()
scaler_cls = getattr(getattr(torch_module, "amp", None), "GradScaler", None)
if scaler_cls is not None:
try:
return scaler_cls("cuda")
except TypeError:
pass
return torch_module.cuda.amp.GradScaler()
def resolve_device(name: str):
"""Pick a ``torch.device``. Auto order is xpu, cuda, cpu.
Explicit xpu or cuda without ``CLOUDIE_ALLOW_GPU=1`` raises. Auto
that would have picked an accelerator falls back to cpu and logs why.
"""
choice = str(name).strip().lower()
if choice not in DEVICE_CHOICES:
raise ValueError("device must be auto, xpu, cuda, or cpu")
torch_module = _torch()
if choice == "auto":
if accelerator_available(torch_module, "xpu"):
picked = "xpu"
elif accelerator_available(torch_module, "cuda"):
picked = "cuda"
else:
picked = "cpu"
if picked != "cpu" and os.environ.get(GPU_GATE) != "1":
message = (
f"Refusing auto-picked device {picked} because {GPU_GATE} is not 1. "
"Falling back to cpu. An XPU or GPU run needs misty wind's approval. "
f"Set {GPU_GATE}=1 only after that approval."
)
LOGGER.warning(message)
print(message)
return torch_module.device("cpu")
choice = picked
if choice in GPU_DEVICES and os.environ.get(GPU_GATE) != "1":
raise RuntimeError(
f"Refusing device {choice}. An XPU or GPU run needs misty wind's approval. "
f"Set {GPU_GATE}=1 only after that approval."
)
if choice == "xpu" and not accelerator_available(torch_module, "xpu"):
raise RuntimeError("device xpu was approved but XPU is not available")
if choice == "cuda" and not accelerator_available(torch_module, "cuda"):
raise RuntimeError("device cuda was approved but CUDA is not available")
try:
return torch_module.device(choice)
except RuntimeError as exc:
raise RuntimeError(f"device {choice} is not supported by this torch build") from exc
def reset_peak_memory(device) -> None:
"""Clear the allocator peak for the device that tracks it. CPU is a no-op."""
torch_module = _torch()
if getattr(device, "type", None) == "cuda":
reset = getattr(torch_module.cuda, "reset_peak_memory_stats", None)
if callable(reset):
try:
reset()
except Exception:
return
if getattr(device, "type", None) == "xpu":
xpu = getattr(torch_module, "xpu", None)
reset = getattr(xpu, "reset_peak_memory_stats", None) if xpu is not None else None
if callable(reset):
try:
reset()
except Exception:
return
def peak_memory_bytes(device) -> int | None:
"""Peak bytes from XPU or CUDA, or null when the device does not report it.
XPU peak memory is trustworthy on the torch 2.15 line. Older builds
may return a number that is not the true peak; the value is still
whatever ``max_memory_allocated`` reports.
"""
torch_module = _torch()
kind = getattr(device, "type", None)
if kind == "xpu":
xpu = getattr(torch_module, "xpu", None)
reader = getattr(xpu, "max_memory_allocated", None) if xpu is not None else None
if not callable(reader):
return None
try:
return int(reader())
except Exception:
return None
if kind == "cuda":
reader = getattr(torch_module.cuda, "max_memory_allocated", None)
if not callable(reader):
return None
try:
return int(reader())
except Exception:
return None
return None
def _jsonable(value):
if value is None or isinstance(value, (str, int, float, bool)):
if isinstance(value, float) and not math.isfinite(value):
return None
return value
if isinstance(value, (list, tuple)):
return [_jsonable(item) for item in value]
return str(value)
def xpu_device_properties(device) -> dict | None:
"""``torch.xpu.get_device_properties`` as a JSON object, or null off XPU."""
if getattr(device, "type", None) != "xpu":
return None
torch_module = _torch()
xpu = getattr(torch_module, "xpu", None)
getter = getattr(xpu, "get_device_properties", None) if xpu is not None else None
if not callable(getter):
return None
index = 0
current = getattr(xpu, "current_device", None)
if callable(current):
try:
index = int(current())
except Exception:
index = 0
try:
props = getter(index)
except Exception:
return None
if hasattr(props, "_asdict"):
raw = dict(props._asdict())
else:
raw = {}
for key in dir(props):
if key.startswith("_"):
continue
try:
value = getattr(props, key)
except Exception:
continue
if callable(value):
continue
raw[key] = value
return {str(key): _jsonable(value) for key, value in raw.items()}
def parity_verdict(
output_max_abs: float,
loss_max_abs: float,
tolerance: float,
reference_scale: float,
) -> dict:
"""Decide whether a device-versus-CPU gap is inside the tolerance.
``tolerance`` is a fraction. The allowed absolute gap is
``tolerance * max(1, reference_scale)``. The floor of 1 keeps a small
bf16-versus-fp32 drift from failing when the CPU output is near zero.
A larger wind head may move by that fraction of its magnitude.
This does not convert physical units. It only compares two forwards.
"""
if isinstance(tolerance, bool) or not isinstance(tolerance, (int, float)):
raise ValueError("device_parity_tol must be a finite number >= 0")
if not math.isfinite(float(tolerance)) or float(tolerance) < 0:
raise ValueError("device_parity_tol must be a finite number >= 0")
output = float(output_max_abs)
loss = float(loss_max_abs)
if not math.isfinite(output) or not math.isfinite(loss):
worst = float("inf")
else:
worst = max(output, loss)
scale = float(reference_scale)
if not math.isfinite(scale) or scale < 1.0:
scale = 1.0
limit = float(tolerance) * scale
return {
"output_max_abs": output,
"loss_max_abs": loss,
"max_abs_diff": worst,
"reference_scale": scale,
"tolerance": float(tolerance),
"limit": limit,
"ok": bool(worst <= limit),
}
def warn_if_parity_failed(result: dict) -> None:
"""Loud warning when the seeded forward disagrees. Training still continues."""
if result.get("ok"):
return
message = (
"WARNING: device parity check failed. "
f"max abs diff {result['max_abs_diff']:.6g} exceeds limit {result['limit']:.6g} "
f"(tolerance {result['tolerance']:.6g}, scale {result['reference_scale']:.6g}). "
"device_parity_ok is false. Training continues. This is not a forecast."
)
LOGGER.warning(message)
print(message)
def seeded_parity_batch(batch_size: int, channels: int, side: int, seed: int = 0, n_frames: int = 1):
"""One fixed CPU batch. The same tensors are copied onto the device."""
torch_module = _torch()
generator = torch_module.Generator(device="cpu")
generator.manual_seed(int(seed))
if n_frames > 1:
images = torch_module.randn(
int(batch_size), int(n_frames), int(channels), int(side), int(side), generator=generator
)
else:
images = torch_module.randn(
int(batch_size), int(channels), int(side), int(side), generator=generator
)
winds = torch_module.rand(int(batch_size), 1, generator=generator)
return images, winds
def _forward_wind_loss(model, images, winds):
import torch.nn.functional as F
torch_module = _torch()
was_training = model.training
model.eval()
try:
with torch_module.no_grad():
output = model(images)
wind = output[0] if isinstance(output, tuple) else output
loss = F.smooth_l1_loss(wind.float().view(-1), winds.float().view(-1))
return wind.detach().float().cpu(), loss.detach().float().cpu()
finally:
model.train(was_training)
def compare_model_parity(model, images, winds, device, tolerance: float, *, policy=None) -> dict:
"""Run one forward on ``device`` and on CPU. Record the max absolute gap.
The training model is copied. Its weights are not updated. The device
copy uses the same autocast policy as training when one is passed.
"""
import copy
cpu_model = copy.deepcopy(model).to("cpu").eval()
device_model = copy.deepcopy(model).to(device).eval()
cpu_wind, cpu_loss = _forward_wind_loss(cpu_model, images, winds)
if policy is None:
policy = amp_policy_for(device)
with autocast_context(policy):
dev_wind, dev_loss = _forward_wind_loss(
device_model, images.to(device), winds.to(device)
)
output_diff = float((dev_wind - cpu_wind).abs().max())
loss_diff = abs(float(dev_loss) - float(cpu_loss))
scale = max(1.0, float(cpu_wind.abs().max()), abs(float(cpu_loss)))
result = parity_verdict(output_diff, loss_diff, float(tolerance), scale)
warn_if_parity_failed(result)
return result
|