Skip to content

feat(cuda): TexTable — layered 1-D lookup-table textures with point/hardware filtering - #24

Open
NicolasRouquette wants to merge 1 commit into
lean-dojo:mainfrom
NicolasRouquette:cuda-texture-table
Open

feat(cuda): TexTable — layered 1-D lookup-table textures with point/hardware filtering#24
NicolasRouquette wants to merge 1 commit into
lean-dojo:mainfrom
NicolasRouquette:cuda-texture-table

Conversation

@NicolasRouquette

Copy link
Copy Markdown
Contributor

TexTable.ofFloatArray data width layers hwFilter builds an immutable layered 1-D float32 table; TexTable.fetch tbl coords layerIdx evaluates piecewise-linear interpolation at grid-space coordinates u ∈ [0, width−1] (clamp addressing; the +0.5 texel-center offset is handled internally) in one launch over the existing Buffer marshaling path. On the CUDA build the table is a layered cudaArray bound once to a cudaTextureObject_t; the default build ships a host-memory parity stub, so lake build stays green without a GPU.

Two filter modes, fixed at construction:

  • point (default): two point fetches + an explicit contraction-blocked (__fadd_rn/__fmul_rn/__fsub_rn) float32 lerp — bit-identical between the CUDA kernel and the CPU stub. (The native backend compiles without --fmad=false, so FMA is blocked by intrinsics, not flags.)
  • hardware: a single tex1DLayered filtered fetch — zero-ALU lerp using the texture unit's 9-bit fixed-point weight, tolerance-validated (CUDA does not specify coefficient rounding); the stub emulates the quantized weight.

Motivation

Small tabulated transfer functions — calibration curves, inverted sensor-response tables, activation LUTs — are read-only, reused across many launches, and small enough to stay texture-cache resident. Binding one as a texture object turns a gather that would otherwise be a multi-launch composed pipeline into a single fetch per element. The op is forward-only by design (no autograd node); the lerp-slope backward is well-defined future work.

Tests

NN/Tests/Runtime/Cuda/TexTable.lean (wired into the CUDA coverage suite): point mode bit-exact vs the executable texLerpSpec reference — compared by Float.toBits, not a tolerance; hardware mode within the 9-bit-weight bound of the unquantized reference; integer-node texel-center probes exact in both modes (catches ±0.5 coordinate errors); clamp-below/above, width = 1, multi-layer, and empty-coords edges. Like the rest of the suite it runs on the CPU stub (lake build) and on the GPU (-K cuda) alike.

Verification

  • nvcc compiles the kernel TU; cc compiles the stub; the @[extern] bindings elaborate; both TUs export the same symbol set so the default build links.
  • nn_tests_suite is green on the CPU stub and with -K cuda=true on an RTX A4500; the point-mode stub↔GPU bit-identity holds on real texture hardware, and the texel-center probes pass under both filter modes.

@NicolasRouquette
NicolasRouquette force-pushed the cuda-texture-table branch 2 times, most recently from 4b9cda5 to 394d9ec Compare August 3, 2026 18:01
…ardware filtering

Immutable layered 1-D float32 tables evaluated by piecewise-linear
interpolation at grid-space coordinates, backed by a layered cudaArray +
cudaTextureObject_t on the CUDA build and a host-memory parity stub by
default. Two filter modes fixed at construction: point (two point fetches
+ explicit contraction-blocked float32 lerp, bit-identical between the
CUDA kernel and the CPU stub) and hardware linear (zero-ALU texture-unit
lerp with CUDA's 9-bit fixed-point weight, tolerance-validated; the stub
emulates the quantized weight).

- csrc/cuda/common/torchlean_cuda_textable.h: boxed ABI + symbol contract
- csrc/cuda/textures/torchlean_cuda_textable{.cu,_stub.c}: external class,
  layered-array construction, fetch kernel/loop, metadata accessors
- lakefile: extern_lib torchlean_cuda_textable via buildNativeBackendLib
- Cuda.Trusted: opaque TexTable handle; Cuda.TexTable: extern bindings
- Cuda.KernelSpec: texLerpSpec (point-mode lerp over decomposed indices)
- Tests/Runtime/Cuda/TexTable: bit-exact point mode vs an executable
  Float32 reference, tolerance-gated hardware mode, integer-node
  texel-center probes, clamp/width=1/empty edges; wired into the suite
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant