Engine: a compiled anti-aliasing filter no longer crashes the session while another compiles

torch.compile's tracing state is global: running the compiled CAS while FXAA was still being
compiled on its thread ended the engine on the GPU VM. One lock now covers each compile, a
compiled filter runs only when it can take it at once (else op by op for that frame), and a
compiled filter that fails falls back to op by op for good.
This commit is contained in:
alex committed 2026-11-26 04:53:51 +00:00
1 parent 013f3c7df2
commit 23e0849942
1 file changed
+22 -3
+22 -3
View File
@@ -301,6 +301,10 @@ def _whole(mode: str):
COMPILE = os.environ.get("SUMMIT_AA_COMPILE", "1") != "0"
_compiled: dict = {} # (mode, shape, device) -> the compiled function, "pending" or "failed"
_lock = threading.Lock()
# held for the whole of each compile: torch.compile's tracing state is global, and a compiled filter run while
# another is being compiled fails ("FX to symbolically trace a dynamo-optimized function"). So one compile at a time,
# and a compiled filter runs only when no compile is going on (else it runs op by op for that frame).
_compiling = threading.Lock()
def _can_compile(frame) -> bool:
@@ -317,7 +321,13 @@ def _compile(key: tuple, mode: str, sample) -> None:
import time
import torch
t0 = time.perf_counter()
with _compiling:
t0 = time.perf_counter()
_compile_locked(key, mode, sample, t0, torch)
def _compile_locked(key: tuple, mode: str, sample, t0: float, torch) -> None:
import time
try:
fn = torch.compile(_whole(mode), dynamic=False)
with torch.no_grad(): # (grad mode is per thread)
@@ -385,7 +395,16 @@ def apply(frame, mode: str, gpu: bool = True):
return apply(torch.from_numpy(np.ascontiguousarray(frame)).cuda(), mode).cpu().numpy()
if is_tensor(frame):
import torch
fn = compiled(mode, frame) or _whole(mode)
fn = compiled(mode, frame)
with torch.no_grad():
return fn(frame)
if fn is not None and _compiling.acquire(blocking=False):
try:
return fn(frame)
except Exception as e: # noqa: BLE001 - never ends the session: op by op from now on
_compiled[(mode, tuple(frame.shape), str(frame.device))] = "failed"
print(f"[aa] the compiled {NAMES[mode]} failed ({type(e).__name__}: {str(e)[:160]}): it runs op "
"by op from now on", flush=True)
finally:
_compiling.release()
return _whole(mode)(frame)
return _whole(mode)(frame)