Repository navigation
Conversation
A custom op is a CPU function plus one kernel for each backend that can run it (ggml_custom_op, ggml_custom_op_4d). The node is still a GGML_OP_CUSTOM: the CPU backend calls the function, a backend with a kernel under its name runs the node itself, on the given number and size of workgroups. Every implementation gets a copy of the same params. Without a kernel, or if it cannot be loaded, the CPU computes the node. - CUDA: the kernel is PTX (nvcc -ptx), loaded with the driver API. The parameters of the kernel are checked against the node. The CUDA graph node properties hold a hash of the params, so that a graph rebuilt with other params is captured again - Metal: the kernel is Metal source, compiled once per device - examples/custom-op: saxpy on the best backend and on the CPU - test-backend-ops: test_custom_op compares the kernel of the backend with the CPU function Assisted-by: Claude Fable 5.2
| int n_tasks; // CPU: number of threads, GGML_N_TASKS_MAX to use them all | ||
| int n_groups[3]; // kernels: number of workgroups | ||
| int group_size[3]; // kernels: threads of a workgroup | ||
| }; |
There was a problem hiding this comment.
The scheduling logic should be part of the ggml_custom_kernel. The idea is each kernel implementation to expose it's scheduling sizes as a function of the inputs. So probably a callback in ggml_custom_kernel that is implemented for each backend. Then the backend calls is callback using the op srcs to determine the threads, workgroups, etc.
There was a problem hiding this comment.
I made the change so that the scheduling logic is part of the kernel. With regards to the overlap we can pass an extra flag here which gives the hint to the backend that this op can be run async. For metal/vulkan it would not matter as it already re-order nodes for concurrency, but for CUDA it can be run on a separate stream
The scheduling moves from the call site into the kernels: each
ggml_custom_kernel has a callback that fills ggml_custom_sched from the
node, its arguments and its params, and the backend calls it. The CPU
function is a kernel too ("CPU"), its callback gives the number of
threads. ggml_custom_op_4d() takes the op, the arguments and the params
only.
Assisted-by: Claude Fable 5.2
| struct ggml_custom_sched { | ||
| int n_tasks; // CPU: number of threads, GGML_N_TASKS_MAX to use them all | ||
| int n_groups[3]; // other backends: number of workgroups | ||
| int group_size[3]; // other backends: threads of a workgroup | ||
| }; | ||
|
|
||
| // each kernel tells how it runs a node, from the node, its arguments (node->src) and its params | ||
| // the backend calls it, the result must depend on the shapes and the params only | ||
| typedef void (*ggml_custom_sched_t)(const struct ggml_tensor * node, const void * params, struct ggml_custom_sched * sched); | ||
|
|
||
| struct ggml_custom_kernel { | ||
| const char * backend; | ||
| ggml_custom_sched_t sched; // NULL: 1 thread, 1 workgroup of 1 thread | ||
| ggml_custom_op_t fun; // CPU | ||
| const void * code; // other backends | ||
| size_t code_size; // 0: code is a string | ||
| const char * name; // name of the kernel in the code | ||
| }; | ||
|
|
||
| struct ggml_custom_op { | ||
| const char * name; | ||
| const struct ggml_custom_kernel * kernels; | ||
| int n_kernels; | ||
| }; |
There was a problem hiding this comment.
The experience is that this pattern of using structs in the interface is difficult change - there is no way to deprecated things. Instead, hide the definition of the structs in the source and implement a thin interface of functions that operates on them. Similar to how we designed llama_batch_ext.
ggml_custom_op, ggml_custom_kernel and ggml_custom_sched are only declared in ggml.h, their definitions move to ggml-impl.h. An op is made with ggml_custom_op_init(), a kernel is added with ggml_custom_op_add_kernel() and set with ggml_custom_kernel_set_fun(), _set_code() and _set_sched(). The scheduling callback gives its result with ggml_custom_sched_set_n_tasks(), _set_n_groups() and _set_group_size(). The op owns its kernels and copies the names and the code, so the backends no longer find what they loaded by the address of the code: - CUDA: the functions by the uid of the code, the modules by its content. The uid is also in the CUDA graph node properties, so that an op freed and made again at the same address is captured again - Metal: the libraries by the source Assisted-by: Claude Fable 5.2
|
Likely with a little bit of polish this approach can work. Should we prototype a Vulkan kernel before we continue? |
The code of a "Vulkan" kernel is SPIR-V and its name is the entry point of the compute shader: - set 0, binding 0 is the data of the node, binding 1 + i the data of argument i - the push constants are the params (at most 128 bytes, what every device has) - the specialization constants 0, 1 and 2 are the size of a workgroup that the kernel tells, for local_size_x_id, local_size_y_id and local_size_z_id. A shader with a fixed size ignores them The pipelines are cached on the device by the uid of the code and the size of a workgroup, kernels with the same code share them. The node goes to the CPU when the code has no compute shader of that name, the pipeline cannot be made, the workgroups exceed the limits of the device, the params do not fit, or a tensor is a view at an offset that a binding cannot have. The example and test-backend-ops get a shader, compiled with glslc. Assisted-by: Claude Fable 5.2
|
I added a vulkan prototype, but no idea if it's feasible. @0cc4m, do you mind taking a look? This is to run the MoE cache without syncs, I measured 50% improvement in decode because of this for vulkan and 10% for CUDA |
|
This is for ggml-org#32076? Did you measure that improvement with this kind of custom op spir-v approach? I have a prototype branch for the PR that is not this simple, it needs more invocations than CUDA since Vulkan lacks the host-memory-coherency barrier primitive. |
|
Yes, I didn't measure it with this approach. The approach that works is simply removing the syncs in vulkan, so this should translate quite well there. I'm currently not sure how the CPU experts will run with this, but this is a start. |
A custom op that backends other than the CPU can run: a list of kernels, one per backend, described once. Prototype for discussion, with CPU, CUDA, Metal and Vulkan implemented.
API (
ggml.h)ggml.honly declares them, the definitions are inggml-impl.h. User code goes through the functions, so a field can be added or retired later without breaking it.GGML_OP_CUSTOM. A backend runs the kernel that has its name (ggml_backend_reg_name():"CPU","CUDA","MTL","Vulkan").paramsis a small struct, copied into the context. Every kernel gets the same one.ggml.handggml-backend.h.fun(dst, ith, nth, params), as forggml_custom_4d()nvcc -ptx),extern "C" __global__ void name(T * dst, const T0 * a0, ..., const P params)kernel void name(constant P & params, device T * dst, device const T0 * a0, ...)glslc), a compute shader with the entry pointname:dst,a0, ... are the storage buffers at the bindings 0, 1, ...,paramsare the push constantsBackends
CPU: asks the CPU kernel for its number of threads, then calls the function as before.
CUDA (
ggml-cuda/custom-op.cu): loads the PTX withcuModuleLoadData(the driver API is already linked for the VMM pool) and launches withcuLaunchKernelon the stream of the graph, with the grid and blocks that the kernel tells. Kernels with the same code share one module. With CUDA >= 12.4 the parameters of the kernel are checked against the node (cuFuncGetParamInfo). The CUDA graph node properties hold a hash of the params and of the id of the kernel code, so a graph rebuilt in the same memory with other params or another kernel is captured again. Not available with HIP, MUSA orGGML_CUDA_NO_VMM.Metal: compiles each source once per device (
ggml_metal_library_init_from_source), binds the params at index 0, the node at 1 and the arguments from 2, then dispatches the threadgroups that the kernel tells.Vulkan (
ggml-vulkan-custom-op.cpp): makes a compute pipeline from the SPIR-V with the existingggml_vk_create_pipeline_funcand dispatches it like the other ops, so the barriers between nodes are the ones of the backend. Three points are specific to Vulkan:layout(local_size_x_id = 0, local_size_y_id = 1, local_size_z_id = 2) in;. A shader with a fixedlocal_size_xignores them. There is one pipeline for each size.minStorageBufferOffsetAlignmentgoes to the CPU.The node also goes to the CPU if the code has no compute shader of that name, the pipeline cannot be made, or the workgroups exceed the limits of the device. Kernels with the same code share their pipelines.
Example and test
examples/custom-op: saxpy (dst = a*x + y) computed with the best backend, then with the CPU only, and compared. Modelled onexamples/simple/simple-backend.cppof ggml. The CUDA kernel asks for blocks of 256 threads, the Metal kernel for threadgroups of 32, the Vulkan kernel for workgroups of 64, the CPU kernel for all the threads.test-backend-ops:test_custom_opcompares the kernel of the backend with the CPU kernel.Testing
saxpy computed by CUDA0, same values as the CPU runsaxpy computed by MTL0, same values as the CPU runsaxpy computed by Vulkan0, same values as the CPU runtest-backend-ops -o CUSTOMon CUDA0 and on MTL0test-backend-ops -o CUSTOMon Vulkan (NVIDIA and RADV)ggml_custom_kernel_set_code()gl_WorkGroupSize, with kernels that tell 64, 128 and 4 x 2 x 3local_size_xtest-backend-opsfor ADD, SCALE on CUDA0 and MTL0, MUL_MAT on CUDA0test-backend-ops -o CUSTOMand the Vulkan rows above withVK_LAYER_KHRONOS_validation1.4.341 (NVIDIA and RADV)VUID-VkComputePipelineCreateInfo-layout-10069test-backend-opsfor ADD, SCALE, TIMESTEP_EMBEDDING on Vulkan (NVIDIA and RADV)Not in this prototype
🤖 Generated with Claude Code