Skip to content

⚡ Thunderbolt: softmax_v6 — 8x unrolled AVX2 Softmax with single-FMA exp256#76

Open
bugparty wants to merge 2 commits into
mainfrom
thunderbolt-softmax-v6-16067841156487821278
Open

⚡ Thunderbolt: softmax_v6 — 8x unrolled AVX2 Softmax with single-FMA exp256#76
bugparty wants to merge 2 commits into
mainfrom
thunderbolt-softmax-v6-16067841156487821278

Conversation

@bugparty

@bugparty bugparty commented Jul 12, 2026

Copy link
Copy Markdown
Owner

💡 What: Added softmax_v6, an AVX2-optimized softmax kernel that unrolls the main computational loops 8x (handling 64 elements per iteration) and utilizes a fused constant in the exp256 approximation via a single _mm256_fnmadd_ps.

🎯 Why: The previous softmax_v5 kernel was unrolled 4x and utilized separate multiplication and subtraction instructions for range reduction. The exp256 approximations dominate execution time. By reducing the FMA instruction count and unrolling the loop 8x, the pipeline pressure is lowered, fully saturating the execution ports on AVX2, hiding the latencies of both the exp256 calculations and the max reductions.

🏗️ How:

  1. Defined an inline helper exp256_ps_v3 that computes r = x - n*ln2 using _mm256_fnmadd_ps with a full precision constant 0.6931471805599453f.
  2. Created softmax_v6 extending the 3-pass softmax structure, unrolling all loops 8x (64 elements).
  3. Registered softmax_v6 in ml_kernel_bench.
  4. Added correctness test test_softmax_v6 in test_naive_ops.cpp with a 72-element array to verify both the unrolled loop and the remainder loop.

📊 Impact:
Microbenchmarking showed noticeable throughput improvements. End-to-end benchmarks demonstrated increased throughput, reaching up to ~6.29 GFLOP/s on N=16384 (Fixed Memory) compared to softmax_v5's ~5.66 GFLOP/s.

🖥️ Tested on: Ubuntu Linux (Docker container), GNU compiler 13.3.0, x86-64 CPU with AVX2.

🔬 How to reproduce:

cd build
make -j$(nproc) ml_kernel_bench
DISABLE_CPU_BINDING=1 ./ml_kernels/ml_kernel_bench --filter "softmax_v6"

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

Summary by CodeRabbit

  • New Features

    • Added an optimized AVX2 softmax implementation for faster processing of larger inputs.
    • Added improved exponential calculations to support high-throughput softmax workloads.
  • Performance

    • Added benchmarking coverage for the new softmax implementation.
  • Tests

    • Added accuracy and probability-sum validation against the existing implementation.
    • Added coverage for mixed-value and larger input sets.

…exp256

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 12, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@bugparty, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 988b34bd-ad53-40fb-8de3-5e4d5ad0fe34

📥 Commits

Reviewing files that changed from the base of the PR and between a73cc23 and cf719a8.

📒 Files selected for processing (2)
  • .jules/thunderbolt.md
  • dgetrf/my.c
📝 Walkthrough

Walkthrough

Adds an AVX2 exp256_ps_v3 helper and softmax_v6 implementation with unrolled reductions, vectorized normalization, benchmark registration, optimization notes, and numerical tests.

Changes

Softmax v6

Layer / File(s) Summary
AVX2 exponential helper
ml_kernels/include/ml_kernels/softmax.h
Adds exp256_ps_v3 with range reduction, Horner polynomial evaluation, and exponent-bit construction.
Unrolled softmax implementation
ml_kernels/include/ml_kernels/softmax.h, .jules/thunderbolt.md
Adds softmax_v6 with unrolled maximum and sum reductions, vectorized exponential evaluation and normalization, scalar tail handling, early returns, and corresponding optimization guidance.
Benchmark and correctness coverage
ml_kernels/src/kernel_bench.cpp, ml_kernels/src/test_naive_ops.cpp
Registers the softmax_v6 benchmark and compares results with softmax_naive, including normalization and test-runner coverage.

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

Possibly related PRs

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant softmax_v6
  participant exp256_ps_v3
  participant Output
  Caller->>softmax_v6: provide input and output buffers
  softmax_v6->>softmax_v6: reduce maximum and compute sum
  softmax_v6->>exp256_ps_v3: evaluate shifted input vectors
  exp256_ps_v3-->>softmax_v6: return exponential vectors
  softmax_v6->>Output: store normalized probabilities
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: a new AVX2 softmax_v6 with 8x unrolling and exp256 optimization.
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 unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch thunderbolt-softmax-v6-16067841156487821278

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
ml_kernels/include/ml_kernels/softmax.h (1)

505-505: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Function-body opening braces should be on their own line.

The coding guideline requires braces on their own lines for function bodies, but both new definitions place { on the signature line (exp256_ps_v3 at Line 505 and softmax_v6 at Line 543). Note this matches the surrounding file style, so apply consistently only if the guideline is intended to be enforced here.

As per coding guidelines: "Keep braces on their own lines for function bodies".

Also applies to: 543-543

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@ml_kernels/include/ml_kernels/softmax.h` at line 505, Move the opening braces
for the function bodies of exp256_ps_v3 and softmax_v6 onto separate lines below
their signatures, preserving all existing implementation logic.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@ml_kernels/include/ml_kernels/softmax.h`:
- Line 505: Move the opening braces for the function bodies of exp256_ps_v3 and
softmax_v6 onto separate lines below their signatures, preserving all existing
implementation logic.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8b8974c2-6f9a-415f-b9c5-8b5010fafa5c

📥 Commits

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

📒 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

Co-authored-by: bugparty <1510776+bugparty@users.noreply.github.com>
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