Skip to content

feat(cuda): fused scaledProdExp buffer op — exp((c·x)·y) in one kernel - #23

Open
NicolasRouquette wants to merge 2 commits into
lean-dojo:mainfrom
NicolasRouquette:cuda-scaled-prod-exp
Open

feat(cuda): fused scaledProdExp buffer op — exp((c·x)·y) in one kernel#23
NicolasRouquette wants to merge 2 commits into
lean-dojo:mainfrom
NicolasRouquette:cuda-scaled-prod-exp

Conversation

@NicolasRouquette

Copy link
Copy Markdown
Contributor

Buffer.scaledProdExp x y c computes exp((c·x)·y) as a single fused elementwise device kernel (torchlean_scaled_prod_exp_f32, exported via the existing binary+scalar macro), with a portable host-loop stub twin. It computes in fp32 with the same left-association as the composed exp(mul(mul(full(c), x), y)), so it is bit-identical to that four-op form while issuing one launch and allocating one result buffer instead of four.

Motivation

A scaled product exponential is the hot inner form of two-way extinction / propagation factors exp(-2·κ·ℓ) in computational electromagnetism and radar/optical remote sensing, and of Boltzmann-type weights exp(-β·E·s). It names no problem domain — a portable primitive a batched pipeline can fuse.

Tests

NN/Tests/Runtime/Cuda/ScaledProdExp.lean (wired into the CUDA coverage suite) asserts the fused kernel is bit-identical to the composed exp(((full c)·x)·y) — compared by Float.toBits, not a tolerance — across signed/zero fixtures and a range of scalars. A fast-math __expf, a reassociated product, or a dropped float32 cast would fail it. 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; cc compiles the stub; the @[extern] binding elaborates.
  • nn_tests_suite -K cuda=true is green on an RTX A4500; the parity test above passes.

@NicolasRouquette
NicolasRouquette force-pushed the cuda-scaled-prod-exp branch 2 times, most recently from 74b09d3 to bcebc07 Compare August 3, 2026 18:01
Add `Buffer.scaledProdExp x y c = exp((c*x)*y)` as a single fused elementwise device kernel
(`torchlean_scaled_prod_exp_f32`, exported via the existing binary+scalar macro) with the
portable host-loop stub twin. It computes in fp32 with the same left-association as the
composed `exp(mul(mul(full(c), x), y))`, so it is bit-identical to that four-op form while
issuing one launch and allocating one result buffer instead of four.

A *scaled product exponential* is the hot inner form of two-way extinction / propagation
factors `exp(-2*κ*ℓ)` in computational electromagnetism and radar/optical remote sensing, and
of Boltzmann-type weights `exp(-β*E*s)`; naming no problem domain, it is a portable primitive.

Verified: nvcc compiles the kernel, cc compiles the stub, the `@[extern]` binding elaborates.
`Buffer.scaledProdExp x y c` is a single fused device kernel for
`exp((c·x)·y)`. Add a CUDA kernel-coverage test asserting it is
bit-identical — compared by `Float.toBits`, not a tolerance — to the
composed form built from the `full` / `mul` / `exp` elementwise kernels,
over signed/zero fixtures and a range of scalars. A fast-math `__expf`,
a reassociated product, or a dropped float32 cast would fail it. Runs on
the CPU stub (`lake build`) and on the GPU (`-K cuda`) alike; wired into
the CUDA 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