From 35e461ef69503d01389bea4e6718236288255bfc Mon Sep 17 00:00:00 2001 From: nimlgen <138685161+nimlgen@users.noreply.github.com> Date: Fri, 7 Nov 2025 21:23:12 +0800 Subject: [PATCH] hcq: use exception group (#12616) * hcq: use exception group * fix --- ruff.toml | 2 +- tinygrad/runtime/support/hcq.py | 14 ++++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/ruff.toml b/ruff.toml index 0d5b7cb8f0..b6433bcef9 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,6 +1,6 @@ indent-width = 2 preview = true -target-version = "py310" +target-version = "py311" lint.select = [ "F", # Pyflakes diff --git a/tinygrad/runtime/support/hcq.py b/tinygrad/runtime/support/hcq.py index b8aa2f747e..ace5c77955 100644 --- a/tinygrad/runtime/support/hcq.py +++ b/tinygrad/runtime/support/hcq.py @@ -1,6 +1,6 @@ from __future__ import annotations from typing import cast, Callable, Type, TypeVar, Generic, Any, Sequence -import contextlib, decimal, statistics, time, ctypes, array, os, struct, traceback, collections +import contextlib, decimal, statistics, time, ctypes, array, os, struct, collections try: import fcntl # windows misses that except ImportError: fcntl = None #type:ignore[assignment] from tinygrad.helpers import PROFILE, getenv, to_mv, ProfileRangeEvent @@ -437,19 +437,13 @@ class HCQCompiled(Compiled, Generic[SignalType]): except MemoryError: buf, realloced = self.allocator.alloc(oldbuf.size if oldbuf is not None else new_size, options=options), False return buf, realloced - def _make_no_iface_error(self, errs:str, err_short:str) -> RuntimeError: - # Keep it in a separate function to avoid creating a traceback <-> locals ref cycle - e = RuntimeError(f"No interface for {type(self).__name__[:-6]}:{self.device_id} is available") - if hasattr(e, "add_note"): e.add_note(errs + err_short) - return e - def _select_iface(self, *ifaces:Type): - errs, err_short = "", "" + excs = [] if val:=getenv(f'{type(self).__name__[:-6].upper()}_IFACE', ""): ifaces = tuple(x for x in ifaces if x.__name__.startswith(val.upper())) for iface_t in ifaces: try: return iface_t(self, self.device_id) - except Exception as e: errs, err_short = errs + f"\n{iface_t.__name__}: {traceback.format_exc()}", err_short + f"\n{iface_t.__name__}: {e}." - raise self._make_no_iface_error(errs, err_short) + except Exception as e: excs.append(e) + raise ExceptionGroup(f"No interface for {type(self).__name__[:-6]}:{self.device_id} is available", excs) def _is_cpu(self) -> bool: return hasattr(self, 'device') and self.device.split(":")[0] == "CPU"