This commit is contained in:
nimlgen
2025-07-31 09:25:31 +00:00
parent 91b1e09216
commit a068aa5793
6 changed files with 11 additions and 11 deletions
+1 -1
View File
@@ -82,7 +82,7 @@ def word_wrap(x, wrap=80):
return x[:i] + "\n" + word_wrap(x[i:], wrap)
@contextlib.contextmanager
def suppress_finalizing(*exceptions:type[Exception]) -> Generator[None, None, None]:
def suppress_fini(*exceptions:type[Exception]) -> Generator[None, None, None]:
try: yield
except exceptions:
if not getattr(sys, 'is_finalizing', lambda: True)(): raise # re-raise if not finalizing
+2 -2
View File
@@ -7,7 +7,7 @@ from tinygrad.runtime.support.hcq import HCQCompiled, HCQAllocator, HCQBuffer, H
from tinygrad.runtime.support.hcq import MMIOInterface
from tinygrad.uop.ops import sint
from tinygrad.device import Compiled, DMAFdRef, BufferSpec
from tinygrad.helpers import getenv, to_mv, round_up, data64_le, all_same, flatten, DEBUG, AMD_LLVM, PROFILE, ProfileEvent, suppress_finalizing
from tinygrad.helpers import getenv, to_mv, round_up, data64_le, all_same, flatten, DEBUG, AMD_LLVM, PROFILE, ProfileEvent,suppress_fini as _supp_fini
from tinygrad.renderer.cstyle import AMDRenderer
from tinygrad.renderer.llvmir import AMDLLVMRenderer
from tinygrad.runtime.autogen import kfd, hsa, pci, sqtt
@@ -474,7 +474,7 @@ class AMDAllocator(HCQAllocator['AMDDevice']):
return self.dev.iface.alloc(size, host=options.host, uncached=options.uncached, cpu_access=options.cpu_access)
def _free(self, opaque, options:BufferSpec):
with suppress_finalizing(AttributeError, TypeError):
with _supp_fini(AttributeError, TypeError):
self.dev.synchronize()
self.dev.iface.free(opaque)
+2 -2
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
import ctypes, ctypes.util, functools
from tinygrad.helpers import DEBUG, getenv, mv_address, init_c_var, init_c_struct_t, suppress_finalizing
from tinygrad.helpers import DEBUG, getenv, mv_address, init_c_var, init_c_struct_t, suppress_fini as _supp_fini
from tinygrad.device import Compiled, BufferSpec, LRUAllocator
from tinygrad.renderer.cstyle import CUDARenderer
from tinygrad.renderer.ptx import PTXRenderer
@@ -46,7 +46,7 @@ class CUDAProgram:
if self.smem > 0: check(cuda.cuFuncSetAttribute(self.prg, cuda.CU_FUNC_ATTRIBUTE_MAX_DYNAMIC_SHARED_SIZE_BYTES, self.smem))
def __del__(self):
with suppress_finalizing(AttributeError, TypeError): check(cuda.cuModuleUnload(self.module))
with _supp_fini(AttributeError, TypeError): check(cuda.cuModuleUnload(self.module))
def __call__(self, *args, global_size:tuple[int,int,int]=(1,1,1), local_size:tuple[int,int,int]=(1,1,1), vals:tuple[int, ...]=(), wait=False):
check(cuda.cuCtxSetCurrent(self.dev.context))
+2 -2
View File
@@ -2,7 +2,7 @@ from __future__ import annotations
from typing import cast
import ctypes, functools, hashlib
from tinygrad.runtime.autogen import opencl as cl
from tinygrad.helpers import init_c_var, to_char_p_p, from_mv, OSX, DEBUG, getenv, mv_address, suppress_finalizing
from tinygrad.helpers import init_c_var, to_char_p_p, from_mv, OSX, DEBUG, getenv, mv_address, suppress_fini as _supp_fini
from tinygrad.renderer.cstyle import OpenCLRenderer, IntelRenderer
from tinygrad.device import BufferSpec, LRUAllocator, Compiled, Compiler, CompileError
@@ -70,7 +70,7 @@ class CLAllocator(LRUAllocator['CLDevice']):
options.image.shape[1], options.image.shape[0], 0, None, status := ctypes.c_int32()), status), options)
return (checked(cl.clCreateBuffer(self.dev.context, cl.CL_MEM_READ_WRITE, size, None, status := ctypes.c_int32()), status), options)
def _free(self, opaque:tuple[ctypes._CData, BufferSpec], options:BufferSpec):
with suppress_finalizing(AttributeError): check(cl.clReleaseMemObject(opaque[0]))
with _supp_fini(AttributeError): check(cl.clReleaseMemObject(opaque[0]))
def _copyin(self, dest:tuple[ctypes._CData, BufferSpec], src:memoryview):
if dest[1].image is not None:
check(cl.clEnqueueWriteImage(self.dev.queue, dest[0], False, (ctypes.c_size_t * 3)(0,0,0),
+2 -2
View File
@@ -7,7 +7,7 @@ from tinygrad.runtime.support.hcq import HCQCompiled, HCQAllocator, HCQBuffer, H
from tinygrad.runtime.support.hcq import MMIOInterface, FileIOInterface, MOCKGPU
from tinygrad.uop.ops import sint
from tinygrad.device import BufferSpec
from tinygrad.helpers import getenv, mv_address, round_up, data64, data64_le, prod, OSX, to_mv, hi32, lo32, suppress_finalizing
from tinygrad.helpers import getenv, mv_address, round_up, data64, data64_le, prod, OSX, to_mv, hi32, lo32, suppress_fini as _supp_fini
from tinygrad.renderer.ptx import PTXRenderer
from tinygrad.renderer.cstyle import NVRenderer
from tinygrad.runtime.support.compiler_cuda import CUDACompiler, PTXCompiler, PTX, NVPTXCompiler, NVCompiler
@@ -277,7 +277,7 @@ class NVAllocator(HCQAllocator['NVDevice']):
return self.dev.iface.alloc(size, cpu_access=options.cpu_access, host=options.host)
def _free(self, opaque:HCQBuffer, options:BufferSpec):
with suppress_finalizing(AttributeError, TypeError):
with _supp_fini(AttributeError, TypeError):
self.dev.synchronize()
self.dev.iface.free(opaque)
+2 -2
View File
@@ -1,7 +1,7 @@
import functools, struct
from tinygrad.device import Compiled, Allocator, Compiler, BufferSpec
from tinygrad.renderer.wgsl import WGSLRenderer
from tinygrad.helpers import round_up, suppress_finalizing
from tinygrad.helpers import round_up, suppress_fini as _supp_fini
from tinygrad.runtime.autogen import webgpu
from typing import List, Any, TypeAlias
import ctypes
@@ -189,7 +189,7 @@ class WebGpuAllocator(Allocator['WGPUDevPtr']):
buffer_data = read_buffer(self.dev, src)
dest[:] = buffer_data[:dest.nbytes] if webgpu.wgpuBufferGetSize(src) > dest.nbytes else buffer_data
def _free(self, opaque:WGPUBufPtr, options:BufferSpec):
with suppress_finalizing(AttributeError, TypeError): webgpu.wgpuBufferDestroy(opaque)
with _supp_fini(AttributeError, TypeError): webgpu.wgpuBufferDestroy(opaque)
class WebGpuDevice(Compiled):
def __init__(self, device:str):