Skip to content

reductions: frobenius/cosine/triplet (CUDA) + Triton parity for the reduction family - #38

Open
plotfi wants to merge 1 commit into
mainfrom
reduction-kernels
Open

reductions: frobenius/cosine/triplet (CUDA) + Triton parity for the reduction family#38
plotfi wants to merge 1 commit into
mainfrom
reduction-kernels

Conversation

@plotfi

@plotfi plotfi commented Aug 11, 2026

Copy link
Copy Markdown
Owner

What

Implements the remaining reduction-shaped kernels on the shared SmemTreeReduce block-reduction primitive — one per reduction shape — plus Triton parity so run-bench -C compares both backends.

CUDA kernels (each a different reduction shape, all reusing SmemTreeReduce)

  • frobenius-norm — global sum-of-squares → broadcast normalize (Y = X/‖X‖_F), two-pass.
  • cosine-similarity — per-row, three accumulators (dot, ‖p‖², ‖t‖²), one block per row.
  • triplet-margintwo-level: per-triplet distance reduce → atomicAdd batch mean.

Triton parity

solutions-triton/{mse-loss,kl-loss,frobenius-norm,cosine-similarity,triplet-margin}.py, each checked against a torch reference.

mse-loss sizing fix

The CUDA mse-loss harness hardcoded 64*64 and ignored TENSOR_*, so -C -B compared mismatched sizes. Now it sizes via bench_size(M)*bench_size(N), the Triton mse mirrors that, and run-bench's bench_profile gets an mse-loss entry (sets both dims).

Reference value

Together with huber/hinge/mse/kl this is a complete reference for reduction shapes, all sharing one block-reduction implementation:

Shape Kernels
global-scalar mean (2-input map) huber, hinge, mse, kl
global sum + broadcast (two-pass) frobenius-norm
per-row, multi-accumulator cosine-similarity
two-level (per-row reduce + atomic merge) triplet-margin

Verified

  • CUDA 35/35, Triton 35/35 tests pass.
  • run-bench -C -B = ~1.0× CUDA-vs-Triton parity across all reductions (all bandwidth-bound; e.g. mse-loss 1.14 vs 1.12 ms at matched 67M).

🤖 Generated with Claude Code

… for the reduction family

Implement the remaining reduction-shaped kernels on the shared SmemTreeReduce
block-reduction primitive, one per reduction shape:

  * frobenius-norm    — global sum-of-squares + broadcast normalize (two-pass)
  * cosine-similarity — per-row, three accumulators (dot, ||p||^2, ||t||^2)
  * triplet-margin    — two-level: per-triplet distance reduce -> atomic batch mean

Add Triton parity for the reduction family so run-bench -C compares both backends:
solutions-triton/{mse-loss,kl-loss,frobenius-norm,cosine-similarity,triplet-margin}.py.

Also fix the mse-loss harness to size via bench_size (M*N) instead of a hardcoded
64*64 so it honors TENSOR_*/-B, mirror that in the Triton mse (M*N), and add an
mse-loss entry to run-bench's benchmark profile (sets both dims).

Verified: CUDA 35/35 and Triton 35/35 tests pass; run-bench -C -B shows ~1.0x
CUDA-vs-Triton parity across all reductions (all bandwidth-bound).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012o3X6Kr1cr8yS6CaosQTa8
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