Skip to content

【训练营】FlashAttention接入 @simon_chou#128

Open
Simon-CHOU wants to merge 33 commits intoInfiniTensor:masterfrom
Simon-CHOU:simon_chou-20260316
Open

【训练营】FlashAttention接入 @simon_chou#128
Simon-CHOU wants to merge 33 commits intoInfiniTensor:masterfrom
Simon-CHOU:simon_chou-20260316

Conversation

@Simon-CHOU
Copy link

[Feat] FlashAttention Integration (GPT-2/LLaMA-3)

Summary

Integrates FlashAttention kernels into InfiniTrain to optimize memory usage and support long-sequence training. Aligns with project requirements for the 2025 Winter Training Camp.

Key Changes

  • Kernels: Added FP32-based FlashAttentionForwardKernel and FlashAttentionBackwardKernel with causal masking and scaling support.
  • Ops: Implemented ScaledDotProductAttention autograd function, mirroring PyTorch's interface.
  • Models: Enabled --flash flag for GPT-2 and LLaMA-3 to toggle between baseline and FlashAttention.
  • Build: Updated CMake to support CUDA kernels and enforce Release optimization.

Verification

  • Precision (GPT-2): Loss alignment within 0.2% of baseline (FP32), verifying numerical correctness.
  • Stability: Fixed NaN issues by correcting shared memory initialization and gradient accumulation.
  • Functionality: LLaMA-3 (1B) training runs successfully without OOM.

Performance

  • Memory: ~15.7% reduction in peak memory usage on GPT-2 (SeqLen=1024).
  • Throughput: Comparable to baseline (FP32 SIMT kernel used for precision).

Notes

Simon Chou added 30 commits March 10, 2026 17:47
- Initialize shared memory padding to 0 to prevent NaN propagation in WMMA
- Fix s_Qi buffer reuse bug by correctly casting to float*
- Vectorize output store to float4
- Reduce input range to [-1, 1] to prevent expf overflow
- Enable Causal Mask in test
- Add multi-tile test cases (T=16, 32, 1024)
- Log NaN fix and Performance Regression (Story 7)
- Update Tasking Plan status
- Update performance report with latest benchmark results
- Implement Tiled Backward Kernel (Block-level accumulation for dK/dV)
- Use Shared Memory for dK/dV accumulation (reduce atomics by 32x)
- Add Backward Gradient Check to test_flash_layout
- Implement Tiled Backward Kernel to remove global atomicAdd bottleneck
- Fix stride issues in GEMM helpers (Bc_pad vs Bc_bw)
- Enable Dynamic Shared Memory (>48KB) for Backward Kernel
- Verify correctness with Gradient Check (test_flash_layout)
- Benchmark: 7814 TPS (0.64x Baseline), 9x improvement over Story 7
Switch to FP32 kernel and double accumulators to fix precision issues. Correct mask value and epsilon logic.
Record root cause for LLaMA-3 performance bottleneck (atomicAdd) and precision alignment fixes.
Add evaluation conclusion for precision alignment and LLaMA-3 performance analysis.
Simon Chou added 3 commits March 16, 2026 19:59
@kilinchange
Copy link
Collaborator

  1. 请移除 pr 中不必要的提交,pr 中只需包含代码部分修改,项目报告相关内容请作为邮件附件发送;
  2. 请解决目前 pr 与 master 分支的冲突。

@kilinchange kilinchange self-requested a review March 17, 2026 06:17
@kilinchange kilinchange self-assigned this Mar 17, 2026
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.

2 participants