Skip to content

⚡ Thunderbolt: softmax_v6 — 8x unroll with single-FMA exp#71

Open
bugparty wants to merge 1 commit into
mainfrom
thunderbolt-softmax-v6-12385161308874336821
Open

⚡ Thunderbolt: softmax_v6 — 8x unroll with single-FMA exp#71
bugparty wants to merge 1 commit into
mainfrom
thunderbolt-softmax-v6-12385161308874336821

Conversation

@bugparty

@bugparty bugparty commented Jul 5, 2026

Copy link
Copy Markdown
Owner

💡 What: Added softmax_v6 kernel that simplifies exp256_ps range reduction to a single FMA and aggressively unrolls all loop phases 8x.
🎯 Why: Breaking the traditional double-FMA exact ln(2) split for range reduction into a single FMA r = x - n * ln(2) saves register pressure and instruction ports. This reduction in pressure allows the outer loops (max, exp/sum, normalize) to be unrolled 8x, handling 64 elements per iteration, which perfectly saturates out-of-order execution resources and hides execution latencies better than 4x unrolling.
🏗️ How:

  • Wrote exp256_ps_single_fma using one FMA for range reduction and Horner's scheme.
  • Implemented 8x unrolling in softmax_v6 for max finding, exp and sum accumulation, and normalizaton.
  • Ensured careful remainder handling.
    📊 Impact:
    N=16384 (Fixed Memory): 5.63 -> 6.08 GFLOP/s (~8% gain)
    N=65536 (Fixed Memory): 5.61 -> 6.22 GFLOP/s (~10.8% gain)
    N=1048576 (Fixed Memory): 3.98 -> 4.26 GFLOP/s (~7% gain)
    🖥️ Tested on: Target CPU architecture (AVX2-capable, e.g. Haswell+)
    🔬 How to reproduce: DISABLE_CPU_BINDING=1 ./build/ml_kernels/ml_kernel_bench --filter "softmax_v5" and DISABLE_CPU_BINDING=1 ./build/ml_kernels/ml_kernel_bench --filter "softmax_v6"

PR created automatically by Jules for task 12385161308874336821 started by @bugparty

Summary by CodeRabbit

  • New Features

    • Added a faster softmax variant with improved SIMD performance and deeper loop unrolling.
    • Added a new benchmark entry to compare the latest softmax implementation against earlier versions.
  • Documentation

    • Added guidance and benchmark results for the new softmax approach, including accuracy notes.
  • Tests

    • Added correctness checks to verify the new softmax output stays numerically stable and sums to 1.

Co-authored-by: bugparty <1510776+bugparty@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@coderabbitai

coderabbitai Bot commented Jul 5, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 86632c09-eab9-48ce-a791-59007e77f389

📥 Commits

Reviewing files that changed from the base of the PR and between acca01e and 47dbd3a.

📒 Files selected for processing (4)
  • .jules/thunderbolt.md
  • ml_kernels/include/ml_kernels/softmax.h
  • ml_kernels/src/kernel_bench.cpp
  • ml_kernels/src/test_naive_ops.cpp

📝 Walkthrough

Walkthrough

Adds a new AVX2 softmax kernel variant (softmax_v6) using a single-FMA exponential range-reduction helper with 8x unrolling. Includes a corresponding benchmark class, a correctness/normalization test invoked from the test suite, and a documentation entry describing the optimization.

Changes

AVX2 softmax_v6 kernel and validation

Layer / File(s) Summary
exp256_ps_single_fma helper and softmax_v6 kernel
ml_kernels/include/ml_kernels/softmax.h
Adds exp256_ps_single_fma for single-FMA exponential range reduction and softmax_v6, an 8x-unrolled AVX2 softmax kernel performing max reduction, exp/sum accumulation, and normalization.
Benchmark, test, and documentation
ml_kernels/src/kernel_bench.cpp, ml_kernels/src/test_naive_ops.cpp, .jules/thunderbolt.md
Registers SoftmaxV6Benchmark, adds test_softmax_v6 correctness/normalization checks invoked from main(), and documents the single-FMA optimization approach.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Test as test_softmax_v6
  participant Kernel as softmax_v6
  participant Exp as exp256_ps_single_fma
  Test->>Kernel: call(input, output, n)
  Kernel->>Kernel: 8x unrolled max reduction
  Kernel->>Exp: compute exp per AVX vector
  Exp-->>Kernel: exponential values
  Kernel->>Kernel: accumulate sum across 8 accumulators
  Kernel->>Kernel: 8x unrolled normalize by inverse sum
  Kernel-->>Test: output array
  Test->>Test: compare against softmax_naive within tolerance
Loading

Related Issues: None specified.

Related PRs: None specified.

Suggested labels: performance, simd, testing, documentation

Suggested reviewers: None specified.

🐰 A quick hop through registers eight-wide,
Single FMA folds range reduction inside,
softmax_v6 leaps past naive's slow pace,
Tests confirm the sum still lands in place.
Carrot-fueled vectors, unrolled with pride! 🥕

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: the new softmax_v6 kernel with single-FMA exp and 8x unrolling.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch thunderbolt-softmax-v6-12385161308874336821

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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