From 9a573a1d99d53a85be3052a6db82eaf0ce9d84e9 Mon Sep 17 00:00:00 2001 From: nimlgen <138685161+nimlgen@users.noreply.github.com> Date: Mon, 7 Jul 2025 16:31:59 +0300 Subject: [PATCH] nv: finalize nvdev (#11117) * nv: finalize nvdev * typo --- tinygrad/runtime/ops_amd.py | 5 ----- tinygrad/runtime/ops_nv.py | 2 ++ tinygrad/runtime/support/hcq.py | 7 +++++++ tinygrad/runtime/support/nv/nvdev.py | 2 ++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tinygrad/runtime/ops_amd.py b/tinygrad/runtime/ops_amd.py index 72969fc00f..65be9b081d 100644 --- a/tinygrad/runtime/ops_amd.py +++ b/tinygrad/runtime/ops_amd.py @@ -853,8 +853,3 @@ class AMDDevice(HCQCompiled): self.allocator._copyout(sqtt_buf:=memoryview(bytearray(wptr)), buf0) Compiled.profile_events += [ProfileSQTTEvent(self.device, i, bytes(sqtt_buf), bool((self.sqtt_itrace_se_mask >> i) & 0b1))] super()._at_profile_finalize() - - def finalize(self): - try: self.synchronize() # Try to finalize device in any case. - except RuntimeError as e: print(f"{self.device} synchronization failed before finalizing: {e}") - if hasattr(self.iface, 'device_fini'): self.iface.device_fini() diff --git a/tinygrad/runtime/ops_nv.py b/tinygrad/runtime/ops_nv.py index 09a7781a16..6147f34271 100644 --- a/tinygrad/runtime/ops_nv.py +++ b/tinygrad/runtime/ops_nv.py @@ -482,6 +482,8 @@ class PCIIface(PCIIfaceBase): res = self.dev_impl.gsp.rpc_rm_control(obj, cmd, params, self.root) return type(params).from_buffer_copy(res) if params is not None else None + def device_fini(self): self.dev_impl.fini() + class NVDevice(HCQCompiled[NVSignal]): devices: ClassVar[list[HCQCompiled]] = [] signal_pages: ClassVar[list[HCQBuffer]] = [] diff --git a/tinygrad/runtime/support/hcq.py b/tinygrad/runtime/support/hcq.py index cd5f8961f0..638a471755 100644 --- a/tinygrad/runtime/support/hcq.py +++ b/tinygrad/runtime/support/hcq.py @@ -434,6 +434,13 @@ class HCQCompiled(Compiled, Generic[SignalType]): 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}") + def finalize(self): + try: self.synchronize() # Try to finalize device in any case. + except RuntimeError as e: print(f"{self.device} synchronization failed before finalizing: {e}") + + # If the device has an interface, call its device_fini method to clean up resources. + if hasattr(self, 'iface') and hasattr(self.iface, 'device_fini'): self.iface.device_fini() + 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 diff --git a/tinygrad/runtime/support/nv/nvdev.py b/tinygrad/runtime/support/nv/nvdev.py index 51e009f90c..6f9d95ed7d 100644 --- a/tinygrad/runtime/support/nv/nvdev.py +++ b/tinygrad/runtime/support/nv/nvdev.py @@ -90,6 +90,8 @@ class NVDev(PCIDevImplBase): for ip in [self.flcn, self.gsp]: ip.init_sw() for ip in [self.flcn, self.gsp]: ip.init_hw() + def fini(self): System.pci_reset(self.devfmt) # Reset the device to clean up resources. TODO: Consider a warm start process. + def reg(self, reg:str) -> NVReg: return self.__dict__[reg] def wreg(self, addr, value): self.mmio[addr // 4] = value