feat(compute): fused PatchTST encoder CUDA kernels#86
Merged
Conversation
Implement fused encoder forward and backward pass orchestrators that replace ~78 discrete Engine operations per layer with a single C function call. The orchestrator launches cuBLAS GEMMs for matrix multiplications and custom CUDA sub-kernels for LayerNorm, head transpose, GELU, softmax, and residual operations. Forward kernel (fused_encoder_fwd.cu): - 7 sub-kernels: layernorm_fwd, bias_add, head_split, head_merge, softmax_fwd, bias_gelu_fwd, bias_residual_add - 8 cuBLAS Sgemm/SgemmStridedBatched calls - Caches 16 intermediate buffers (FEB_*) for backward use Backward kernel (fused_encoder_bwd.cu): - 6 sub-kernels: layernorm_bwd, gelu_bwd, softmax_bwd, bias_grad_reduce, add, head_split/merge - 14 cuBLAS calls for weight/input gradient computation - Reads forward cache; accumulates into gradient buffers Go bindings: - Purego and CGo wrappers for both kernels - KernelLib symbol registration (optional, non-fatal if absent) - KernelRunner interface methods + all backend stubs - FusedEncoderProvider optional interface on GPUEngine - cublas.Handle.Ptr() for passing raw handle to C Build: Makefile adds -lcublas to libkernels.so link step. Closes zerfoo/zerfoo E55 tasks T55.1.1, T55.1.2, T55.2.1, T55.2.2.
The cublas package uses the same uintptr-to-unsafe.Pointer pattern as other GPU runtime binding packages for storing C library handles via purego/dlopen. Add it to the vet exclusion list alongside internal/cuda, internal/hip, etc. Triggered by the new Handle.Ptr() method added for the fused encoder kernel orchestrator.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Files
CUDA kernels (new):
internal/cuda/kernels/fused_encoder_fwd.{cu,h}— forward orchestrator + sub-kernelsinternal/cuda/kernels/fused_encoder_bwd.{cu,h}— backward orchestrator + sub-kernelsGo bindings (new):
internal/cuda/kernels/fused_encoder_{fwd,bwd}_purego.go— purego wrappers + buffer index constantsinternal/cuda/kernels/fused_encoder_{fwd,bwd}.go— CGo wrapperscompute/fused_encoder.go— FusedEncoderProvider optional interfacecompute/gpu_fused_encoder.go— GPUEngine implementationModified:
internal/cuda/kernels/purego.go— KernelLib symbol registration (optional)internal/cuda/kernels/Makefile— add .cu files +-lcublaslinkinternal/cublas/cublas_purego.go—Handle.Ptr()for raw pointer accessinternal/gpuapi/kernels.go— KernelRunner interface additionsinternal/gpuapi/{cuda,opencl,rocm,sycl,fpga,metal}_kernels.go— adapter implementations/stubsTest plan
go build ./...passesgo test ./compute/passes (CPU tests)cd internal/cuda/kernels && make CUDA_ARCH=sm_121 shared