From a62372c700f329e2c44a1f42c72d4a9075ab0e55 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 2 Dec 2026 19:57:29 +0000 Subject: [PATCH] GPU on Intel: a launch on GPU-resident arrays does not wait, as on CUDA and HIP (4.6x for small launches) Level Zero's immediate command list was synchronous, so every Intel launch waited for its kernel, where CUDA and HIP queue a launch whose arrays all stay on the GPU (GpuArrays, gpuBatch) and wait where its results are needed. The list is now asynchronous: - each launch is followed by a barrier, as the list runs its commands in any order unless told: what comes after a kernel (another kernel on its arrays, a copy back) waits for it, as on a stream; - a free waits for the list first: zeMemFree does not wait for a kernel still on the memory (cuMemFree and hipFree do), and a GpuArray collected after a launch not waited for would be freed under it; - copies still wait for the list, as before. 2000 small launches on GpuArrays (Core Ultra 9 285K's iGPU): 13 ms, against 63 ms waiting after each. All 26 GPU programs (JIT and exe) and the 11 fault cases pass on Intel. docs/gpu.md says so. Co-Authored-By: Claude Opus 5.5 --- docs/gpu.md | 6 +++--- tslang/lib/TypeScriptAsyncRuntime/GpuRuntime.c | 18 +++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/docs/gpu.md b/docs/gpu.md index ee26f8b3c..3a32c37e0 100644 --- a/docs/gpu.md +++ b/docs/gpu.md @@ -538,7 +538,8 @@ their results are needed: - the end of the program. On the RTX 5080 a launch that does not wait costs about 7.5 µs, against 14 µs for one that does, whatever the -kernel's size: for small kernels, launches are what the time goes on. +kernel's size: for small kernels, launches are what the time goes on. On Intel's integrated GPU of a Core Ultra 9 +285K, 2000 small launches take 13 ms that way, against 63 ms waiting after each. A kernel that fails (an array index past the end, say) is reported where the program next waits, not at its own launch, and at the end of the program if nothing waits before it: @@ -548,8 +549,7 @@ tslang gpu (cuda): running a kernel failed (719 unspecified launch failure) ``` `TSLANG_GPU_SYNC=1` waits after every launch, so the error comes from the launch that caused it, and so does -`TSLANG_GPU_PROFILE=1`, which times each kernel. On Intel's GPUs every launch waits for now (Level Zero's -synchronous command list). +`TSLANG_GPU_PROFILE=1`, which times each kernel. ## Numbers on the GPU diff --git a/tslang/lib/TypeScriptAsyncRuntime/GpuRuntime.c b/tslang/lib/TypeScriptAsyncRuntime/GpuRuntime.c index 314d9125d..b7efbb5cf 100644 --- a/tslang/lib/TypeScriptAsyncRuntime/GpuRuntime.c +++ b/tslang/lib/TypeScriptAsyncRuntime/GpuRuntime.c @@ -348,7 +348,7 @@ static GpuDriver *hipInitDriver(void) #define ZE_STRUCTURE_TYPE_KERNEL_DESC 0x1d #define ZE_INIT_FLAG_GPU_ONLY 1 #define ZE_DEVICE_TYPE_GPU 1 -#define ZE_COMMAND_QUEUE_MODE_SYNCHRONOUS 1 +#define ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS 2 #define ZE_MODULE_FORMAT_IL_SPIRV 0 #define ZE_KERNEL_INDIRECT_ACCESS_FLAG_DEVICE 2 @@ -433,6 +433,7 @@ static struct GpuResult (*zeKernelSetArgumentValue)(void *, uint32_t, size_t, const void *); GpuResult (*zeKernelSetIndirectAccess)(void *, uint32_t); GpuResult (*zeCommandListAppendLaunchKernel)(void *, void *, const ze_group_count_t *, void *, uint32_t, void **); + GpuResult (*zeCommandListAppendBarrier)(void *, void *, uint32_t, void **); GpuResult (*zeDriverGetLastErrorDescription)(void *, const char **); // since 1.6; may be missing GpuResult (*zeModuleGetGlobalPointer)(void *, const char *, size_t *, void **); void *driver; @@ -494,7 +495,13 @@ static GpuResult levelZeroAlloc(GpuPtr *ptr, size_t bytes) *ptr = (GpuPtr)(uintptr_t)p; return r; } -static GpuResult levelZeroFree(GpuPtr ptr) { return ze.zeMemFree(ze.context, (void *)(uintptr_t)ptr); } +// zeMemFree does not wait for a kernel still on the memory (cuMemFree and hipFree do): a GpuArray collected +// after a launch that was not waited for is freed only after it +static GpuResult levelZeroFree(GpuPtr ptr) +{ + GpuResult r = ze.zeCommandListHostSynchronize(ze.list, UINT64_MAX); + return r != 0 ? r : ze.zeMemFree(ze.context, (void *)(uintptr_t)ptr); +} static GpuResult levelZeroCopy(void *dst, const void *src, size_t bytes) { GpuResult r = ze.zeCommandListAppendMemoryCopy(ze.list, dst, src, bytes, NULL, 0, NULL); @@ -521,6 +528,10 @@ static GpuResult levelZeroLaunch(void *fn, const unsigned *grid, const unsigned ze_group_count_t groups = {grid[0], grid[1], grid[2]}; if (r == 0) r = ze.zeCommandListAppendLaunchKernel(ze.list, fn, &groups, NULL, 0, NULL); + // the list runs its commands in any order unless told: what comes after the kernel (another kernel on its + // arrays, a copy back) waits for it, as on a CUDA or HIP stream + if (r == 0) + r = ze.zeCommandListAppendBarrier(ze.list, NULL, 0, NULL); return r; } static GpuResult levelZeroSynchronize(void) { return ze.zeCommandListHostSynchronize(ze.list, UINT64_MAX); } @@ -569,6 +580,7 @@ static GpuDriver *levelZeroInit(void) GPU_SYM(driver, lib, ze.zeKernelSetArgumentValue, "zeKernelSetArgumentValue"); GPU_SYM(driver, lib, ze.zeKernelSetIndirectAccess, "zeKernelSetIndirectAccess"); GPU_SYM(driver, lib, ze.zeCommandListAppendLaunchKernel, "zeCommandListAppendLaunchKernel"); + GPU_SYM(driver, lib, ze.zeCommandListAppendBarrier, "zeCommandListAppendBarrier"); *(void **)&ze.zeDriverGetLastErrorDescription = gpuSym(lib, "zeDriverGetLastErrorDescription"); if ((*(void **)&ze.zeModuleGetGlobalPointer = gpuSym(lib, "zeModuleGetGlobalPointer"))) driver->getGlobal = levelZeroGetGlobal; @@ -616,7 +628,7 @@ static GpuDriver *levelZeroInit(void) if (r != 0) return gpuUnusable(driver, "zeContextCreate failed", r); ze_command_queue_desc_t queueDesc = {ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC, NULL, 0, 0, 0, - ZE_COMMAND_QUEUE_MODE_SYNCHRONOUS, 0}; + ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS, 0}; if ((r = ze.zeCommandListCreateImmediate(ze.context, ze.device, &queueDesc, &ze.list)) != 0) return gpuUnusable(driver, "zeCommandListCreateImmediate failed", r); driver->ready = 1;