touchup cl_errors (#6058)

* touchup cl_errors

* update test
This commit is contained in:
chenyu
2024-08-13 13:06:59 -04:00
committed by GitHub
parent 9145ad52ff
commit e3af273fa1
2 changed files with 12 additions and 5 deletions

View File

@@ -1,13 +1,20 @@
import unittest
from tinygrad import Device
from tinygrad.helpers import CI
from tinygrad.runtime.ops_gpu import CLDevice, CLAllocator
from tinygrad.runtime.ops_gpu import CLDevice, CLAllocator, CLCompiler, CLProgram
@unittest.skipUnless(Device.DEFAULT in ["GPU"] and not CI, "Runs only on OpenCL (GPU)")
class TestOCLOOM(unittest.TestCase):
def test_opencl_oom(self):
@unittest.skipUnless(Device.DEFAULT == "GPU", "Runs only on OpenCL (GPU)")
class TestCLError(unittest.TestCase):
@unittest.skipIf(CI, "dangerous for CI, it allocates tons of memory")
def test_oom(self):
with self.assertRaises(RuntimeError) as err:
allocator = CLAllocator(CLDevice())
for i in range(1_000_000):
allocator.alloc(1_000_000_000)
assert str(err.exception) == "OpenCL Error -6: CL_OUT_OF_HOST_MEMORY"
def test_invalid_kernel_name(self):
device = Device[Device.DEFAULT]
with self.assertRaises(RuntimeError) as err:
CLProgram(device, name="", lib=CLCompiler(device, "test").compile("__kernel void test(__global int* a) { a[0] = 1; }"))
assert str(err.exception) == "OpenCL Error -46: CL_INVALID_KERNEL_NAME"

View File

@@ -9,7 +9,7 @@ from tinygrad.device import BufferOptions, LRUAllocator, Compiled, Compiler, Com
# see test/external/external_osx_profiling.py to determine this ratio. it's in like GPU clocks or something
OSX_TIMING_RATIO = (125/3) if OSX else 1.0
cl_errors = {getattr(cl, k): k for k in filter(lambda e: e.startswith("CL_"), dir(cl)) if getattr(cl, k) <= 0}
cl_errors = {attr: k for k in dir(cl) if k.startswith("CL_") and (attr:=getattr(cl, k)) <= 0}
def check(status):
if status != 0: raise RuntimeError(f"OpenCL Error {status}: {cl_errors.get(status, 'Unknown error')}")
def checked(ret, status): return (check(status.value), ret)[1]