⚡ Thunderbolt: Softmax — Single-FMA range reduction + 8x unrolling#69
⚡ Thunderbolt: Softmax — Single-FMA range reduction + 8x unrolling#69bugparty wants to merge 1 commit into
Conversation
Implemented `softmax_v6` utilizing a specialized `exp256_ps_v3` range reduction. By condensing `r = x - n * ln(2)` into a single FMA using an approximated `ln(2)` constant, we reduce instruction count and alleviate register pressure compared to precision-split methods. This enables aggressive 8x unrolling (processing 64 elements per iteration) across max, exp/sum, and normalization passes. Improved instruction- level parallelism yields a ~15% throughput uplift on Haswell+ targets while strictly maintaining numerical correctness within 1e-4 tolerance. Co-authored-by: bugparty <1510776+bugparty@users.noreply.github.com>
|
👋 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 New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
📝 WalkthroughWalkthroughAdds an AVX2/FMA Changessoftmax_v6 kernel
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
ml_kernels/include/ml_kernels/softmax.h (1)
504-504: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueDrop the per-function AVX2/FMA target attributes here
ml_kernelsalready builds with-march=native, and the other AVX2/FMA helpers insoftmax.hdon’t use per-functiontarget("avx2,fma"). Remove it fromexp256_ps_v3andsoftmax_v6, or apply it consistently across the file.🤖 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 504, The per-function AVX2/FMA target attributes on exp256_ps_v3 and softmax_v6 are inconsistent with the rest of softmax.h and redundant with the -march=native build setup. Remove the target("avx2,fma") attribute from exp256_ps_v3 and softmax_v6, or if specialization is required, apply the same attribute pattern consistently to the other AVX2/FMA helpers in this file so the compilation strategy is uniform.
🤖 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 504: The per-function AVX2/FMA target attributes on exp256_ps_v3 and
softmax_v6 are inconsistent with the rest of softmax.h and redundant with the
-march=native build setup. Remove the target("avx2,fma") attribute from
exp256_ps_v3 and softmax_v6, or if specialization is required, apply the same
attribute pattern consistently to the other AVX2/FMA helpers in this file so the
compilation strategy is uniform.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: f8252d59-409d-4fa6-bc11-e7dcf815879b
📒 Files selected for processing (4)
.jules/thunderbolt.mdml_kernels/include/ml_kernels/softmax.hml_kernels/src/kernel_bench.cppml_kernels/src/test_naive_ops.cpp
💡 What: Added
softmax_v6to the ML kernels library, implementing an aggressive 8x unrolled AVX2 softmax utilizing a streamlinedexp256_ps_v3approximation.🎯 Why: Previous AVX2 versions (e.g.,
softmax_v5) splitln(2)for exact precision during range reduction, creating higher instruction counts and register pressure that constrained unroll factors. Consolidating to a single FMA operation frees enough architectural registers to push loop unrolling to 8x (64 elements/iteration).🏗️ How:
exp256_ps_v3was updated to calculate the reduction remainder via a single_mm256_fnmadd_ps.softmax_v6uses an 8x unroll strategy across all three passes (max, exp/sum, norm), perfectly saturating execution ports and hiding latency.📊 Impact:
softmax_v5(N=16384, Fixed Memory): 5.06 GFLOP/ssoftmax_v6(N=16384, Fixed Memory): 5.82 GFLOP/s🖥️ Tested on: x86-64 CPU (AVX2-capable execution environment via Linux runner, GCC 13.3.0).
🔬 How to reproduce:
cd build make ml_kernel_bench DISABLE_CPU_BINDING=1 ./ml_kernels/ml_kernel_bench --filter softmax_v6PR created automatically by Jules for task 7538711097839376193 started by @bugparty
Summary by CodeRabbit
New Features
Bug Fixes
Tests