"""HTML rendering for accelerator-memory status."""
from __future__ import annotations
from typing import Any
VRAM_REFRESH_CHOICES = (("10 sec", 10.0), ("30 sec", 30.0), ("1 min", 60.0))
DEFAULT_VRAM_REFRESH_INTERVAL = 30.0
def resolve_vram_refresh_interval(value: float) -> float:
"""Return a supported VRAM polling interval, falling back to the default."""
allowed = {interval for _label, interval in VRAM_REFRESH_CHOICES}
return value if value in allowed else DEFAULT_VRAM_REFRESH_INTERVAL
def render_vram_html(device: Any) -> str:
"""Return per-device GPU/accelerator memory usage as styled bars."""
try:
if not device.is_gpu_available():
return (
'
CPU ONLY — NO GPU DETECTED
'
)
cuda_count = device.device_count() if device.is_cuda() else 0
device_indices = range(cuda_count) if cuda_count else (None,)
rows = []
for device_index in device_indices:
query_index = 0 if device_index is None else device_index
device_key = "single" if device_index is None else device_index
mem = device.get_memory_info(query_index)
used = mem.used_gb
total = mem.total_gb
pct = (used / total * 100) if total > 0 else 0
if pct < 50:
bar_color = "#00ff41"
elif pct < 80:
bar_color = "#ffcc00"
else:
bar_color = "#ff003c"
device_name = (
f"GPU {device_index} · {mem.device_name}"
if device_index is not None
else mem.device_name
)
reserved_html = (
f'reserved: {mem.reserved_gb:.1f} GB'
if mem.reserved_gb > 0
else 'unified memory'
)
rows.append(
f''
f'
'
f'{device_name}'
f'{used:.1f} / {total:.1f} GB ({pct:.0f}%)
'
f'
'
f'
'
f'{reserved_html}
'
f'
'
)
if cuda_count > 1:
rows.append(
'Automatic sharding uses additional GPUs as model size '
'requires; smaller models may remain on GPU 0.
'
)
return "".join(rows)
except Exception:
return 'Memory: unavailable
'