From 9ea7deb5158b116bd22e2098b021d0ae11220889 Mon Sep 17 00:00:00 2001 From: nimlgen <138685161+nimlgen@users.noreply.github.com> Date: Mon, 30 Jun 2025 21:12:39 +0300 Subject: [PATCH] hcq: select_iface shared (#11033) * hcq: select_iface shared * errs * sorry * upprt --- tinygrad/runtime/ops_amd.py | 13 ++----------- tinygrad/runtime/ops_nv.py | 13 ++----------- tinygrad/runtime/support/hcq.py | 10 +++++++++- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/tinygrad/runtime/ops_amd.py b/tinygrad/runtime/ops_amd.py index dc92b86a89..72969fc00f 100644 --- a/tinygrad/runtime/ops_amd.py +++ b/tinygrad/runtime/ops_amd.py @@ -1,6 +1,6 @@ from __future__ import annotations from typing import cast, ClassVar -import os, ctypes, ctypes.util, struct, hashlib, functools, importlib, mmap, errno, array, contextlib, sys, weakref, traceback +import os, ctypes, ctypes.util, struct, hashlib, functools, importlib, mmap, errno, array, contextlib, sys, weakref assert sys.platform != 'win32' from dataclasses import dataclass from tinygrad.runtime.support.hcq import HCQCompiled, HCQAllocator, HCQBuffer, HWQueue, CLikeArgsState, HCQSignal, HCQProgram, FileIOInterface @@ -730,18 +730,9 @@ class AMDDevice(HCQCompiled): def is_am(self) -> bool: return isinstance(self.iface, (PCIIface, USBIface)) def is_usb(self) -> bool: return isinstance(self.iface, USBIface) - def _select_iface(self): - if len(nm:=getenv("AMD_IFACE", "")) > 0: return getattr(sys.modules[__name__], f"{nm.upper()}Iface")(self, self.device_id) - - errs:str = "" - for iface_t in (KFDIface, PCIIface, USBIface): - try: return iface_t(self, self.device_id) - except Exception: errs += f"\n{iface_t.__name__}: {traceback.format_exc()}" - raise RuntimeError(f"Cannot find a usable interface for AMD:{self.device_id}:\n{errs}") - def __init__(self, device:str=""): self.device_id = int(device.split(":")[1]) if ":" in device else 0 - self.iface = self._select_iface() + self.iface = self._select_iface(KFDIface, PCIIface, USBIface) self.target:tuple[int, ...] = ((trgt:=self.iface.props['gfx_target_version']) // 10000, (trgt // 100) % 100, trgt % 100) self.arch = "gfx%d%x%x" % self.target if self.target < (9,4,2) or self.target >= (13,0,0): raise RuntimeError(f"Unsupported arch: {self.arch}") diff --git a/tinygrad/runtime/ops_nv.py b/tinygrad/runtime/ops_nv.py index 26f018c875..76fc9a053b 100644 --- a/tinygrad/runtime/ops_nv.py +++ b/tinygrad/runtime/ops_nv.py @@ -1,5 +1,5 @@ from __future__ import annotations -import os, ctypes, contextlib, re, functools, mmap, struct, array, sys, weakref, traceback +import os, ctypes, contextlib, re, functools, mmap, struct, array, sys, weakref assert sys.platform != 'win32' from typing import cast, Union, ClassVar from dataclasses import dataclass @@ -484,18 +484,9 @@ class NVDevice(HCQCompiled[NVSignal]): def is_nvd(self) -> bool: return isinstance(self.iface, PCIIface) - def _select_iface(self): - if len(nm:=getenv("NV_IFACE", "")) > 0: return getattr(sys.modules[__name__], f"{nm.upper()}Iface")(self, self.device_id) - - errs:str = "" - for iface_t in (NVKIface, PCIIface): - try: return iface_t(self, self.device_id) - except Exception: errs += f"\n{iface_t.__name__}: {traceback.format_exc()}" - raise RuntimeError(f"Cannot find a usable interface for NV:{self.device_id}:\n{errs}") - def __init__(self, device:str=""): self.device_id = int(device.split(":")[1]) if ":" in device else 0 - self.iface = self._select_iface() + self.iface = self._select_iface(NVKIface, PCIIface) device_params = nv_gpu.NV0080_ALLOC_PARAMETERS(deviceId=self.iface.gpu_instance, hClientShare=self.iface.root, vaMode=nv_gpu.NV_DEVICE_ALLOCATION_VAMODE_MULTIPLE_VASPACES) diff --git a/tinygrad/runtime/support/hcq.py b/tinygrad/runtime/support/hcq.py index 8dcf1548b5..cd5f8961f0 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, ClassVar -import contextlib, decimal, statistics, time, ctypes, array, os, fcntl, struct +import contextlib, decimal, statistics, time, ctypes, array, os, fcntl, struct, traceback from tinygrad.helpers import PROFILE, getenv, to_mv, round_up from tinygrad.renderer import Renderer from tinygrad.device import BufferSpec, Compiler, Compiled, LRUAllocator, ProfileRangeEvent, ProfileDeviceEvent, ProfileProgramEvent @@ -426,6 +426,14 @@ 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 _select_iface(self, *ifaces:Type): + errs:str = "" + 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: errs += f"\n{iface_t.__name__}: {traceback.format_exc()}" + raise RuntimeError(f"Cannot find a usable interface for {type(self).__name__[:-6]}:{self.device_id}:\n{errs}") + class HCQBuffer: def __init__(self, va_addr:sint, size:int, texture_info:Any=None, meta:Any=None, _base:HCQBuffer|None=None, view:MMIOInterface|None=None): self.va_addr, self.size, self.texture_info, self.meta, self._base, self.view = va_addr, size, texture_info, meta, _base, view