<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>代码链路追踪 on Inference Cookbook</title>
    <link>https://inference.lei6393.com/flashattention/05-code-walkthrough/</link>
    <description>Recent content in 代码链路追踪 on Inference Cookbook</description>
    <generator>Hugo</generator>
    <language>zh</language>
    <copyright>Copyright (c) 2024-2025 Inference Cookbook</copyright>
    <atom:link href="https://inference.lei6393.com/flashattention/05-code-walkthrough/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>前向调用链追踪</title>
      <link>https://inference.lei6393.com/flashattention/05-code-walkthrough/01-forward-call-trace/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://inference.lei6393.com/flashattention/05-code-walkthrough/01-forward-call-trace/</guid>
      <description>&lt;p&gt;以 FA2 &lt;code&gt;v2.8.3.post2&lt;/code&gt; 的 NVIDIA CUDA 稠密前向为例。输入 &lt;code&gt;[2, 1024, 32, 128]&lt;/code&gt; 表示两条等长序列，每个位置 32 个头，每头 128 维。调用者给出 &lt;code&gt;causal=True&lt;/code&gt;，函数按这些条件分配输出并派发内核。&lt;a href=&#34;https://github.com/Dao-AILab/flash-attention/blob/v2.8.3.post2/flash_attn/flash_attn_interface.py&#34;&gt;FA2 Python 入口&lt;/a&gt;&lt;/p&gt;&#xA;&lt;pre class=&#34;mermaid text-center&#34;&gt;flowchart LR&#xA;    A[&amp;#34;flash_attn_func&amp;#34;] --&amp;gt; B[&amp;#34;FlashAttnFunc.apply&amp;#34;]&#xA;    B --&amp;gt; C[&amp;#34;_wrapped_flash_attn_forward&amp;#34;]&#xA;    C --&amp;gt; D[&amp;#34;flash_attn_gpu.fwd&amp;#34;]&#xA;    D --&amp;gt; E[&amp;#34;mha_fwd&amp;#34;]&#xA;    E --&amp;gt; F[&amp;#34;run_mha_fwd&amp;#34;]&#xA;    F --&amp;gt; G[&amp;#34;FA2 CUDA 前向内核&amp;#34;]&lt;/pre&gt;&lt;table&gt;&#xA;  &lt;thead&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;th&gt;边界&lt;/th&gt;&#xA;          &lt;th&gt;本例要追的状态&lt;/th&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/thead&gt;&#xA;  &lt;tbody&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Python → Autograd&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn_func&lt;/code&gt; 调用 &lt;code&gt;FlashAttnFunc.apply&lt;/code&gt;。前向处理最后一维的布局和 padding。存在梯度需求时，保存反向所需张量。&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;Custom op → 扩展&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;_flash_attn_forward&lt;/code&gt; 调用 &lt;code&gt;flash_attn_gpu.fwd&lt;/code&gt;。NVIDIA 构建中的模块为 &lt;code&gt;flash_attn_2_cuda&lt;/code&gt;。&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;C++ → CUDA&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;mha_fwd&lt;/code&gt; 校验设备、形状、dtype，构造 &lt;code&gt;Flash_fwd_params&lt;/code&gt;；&lt;code&gt;run_mha_fwd&lt;/code&gt; 按 dtype、head dimension、causal 等条件选择已实例化的内核。&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;内核 → 调用者&lt;/td&gt;&#xA;          &lt;td&gt;每个 Q 行块遍历可见的 K/V 块，更新行统计量，写回 O 和反向所需的 log-sum-exp。Python 返回去掉额外 padding 的输出。&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;p&gt;&lt;a href=&#34;https://github.com/Dao-AILab/flash-attention/blob/v2.8.3.post2/csrc/flash_attn/flash_api.cpp&#34;&gt;扩展绑定&lt;/a&gt; · &lt;a href=&#34;https://github.com/Dao-AILab/flash-attention/blob/v2.8.3.post2/csrc/flash_attn/src/flash_fwd_kernel.h&#34;&gt;FA2 前向内核&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>反向调用链追踪</title>
      <link>https://inference.lei6393.com/flashattention/05-code-walkthrough/02-backward-call-trace/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://inference.lei6393.com/flashattention/05-code-walkthrough/02-backward-call-trace/</guid>
      <description>&lt;p&gt;FA2 的 CUDA 路径中，Autograd 在上游产生 &lt;code&gt;dO&lt;/code&gt; 时调用 &lt;code&gt;FlashAttnFunc.backward&lt;/code&gt;。前向保存 Q、K、V、O、行 log-sum-exp 和必要的 RNG 状态；反向重算当前分块的注意力权重，不必从显存读回整张概率矩阵。&lt;a href=&#34;https://github.com/Dao-AILab/flash-attention/blob/v2.8.3.post2/flash_attn/flash_attn_interface.py&#34;&gt;FA2 Autograd 入口&lt;/a&gt;&lt;/p&gt;&#xA;&lt;h2 id=&#34;从一个头的一行看梯度&#34;&gt;从一个头的一行看梯度&lt;/h2&gt;&#xA;&lt;p&gt;设 &lt;span class=&#34;katex&#34;&gt;&lt;math xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi mathvariant=&#34;normal&#34;&gt;softmax&lt;/mi&gt;&lt;mo&gt;⁡&lt;/mo&gt;&lt;mo stretchy=&#34;false&#34;&gt;(&lt;/mo&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mo stretchy=&#34;false&#34;&gt;)&lt;/mo&gt;&lt;/mrow&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;P=\operatorname{softmax}(S)&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;、&lt;span class=&#34;katex&#34;&gt;&lt;math xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;O&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;P&lt;/mi&gt;&lt;mi&gt;V&lt;/mi&gt;&lt;/mrow&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;O=PV&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;，其中 &lt;span class=&#34;katex&#34;&gt;&lt;math xmlns=&#34;http://www.w3.org/1998/Math/MathML&#34;&gt;&lt;semantics&gt;&lt;mrow&gt;&lt;mi&gt;S&lt;/mi&gt;&lt;mo&gt;=&lt;/mo&gt;&lt;mi&gt;Q&lt;/mi&gt;&lt;msup&gt;&lt;mi&gt;K&lt;/mi&gt;&lt;mi&gt;T&lt;/mi&gt;&lt;/msup&gt;&lt;mi mathvariant=&#34;normal&#34;&gt;/&lt;/mi&gt;&lt;msqrt&gt;&lt;mi&gt;d&lt;/mi&gt;&lt;/msqrt&gt;&lt;/mrow&gt;&lt;annotation encoding=&#34;application/x-tex&#34;&gt;S=QK^T/\sqrt{d}&lt;/annotation&gt;&lt;/semantics&gt;&lt;/math&gt;&lt;/span&gt;。没有 dropout 时，&lt;code&gt;dV = Pᵀ @ dO&lt;/code&gt;，&lt;code&gt;dP = dO @ Vᵀ&lt;/code&gt;。对一行 softmax，&lt;code&gt;dS = P * (dP - sum(dP * P))&lt;/code&gt;。等价的行标量 &lt;code&gt;sum(dO * O)&lt;/code&gt; 已能从输出得到，免去一次完整的概率矩阵访问。再乘入原有缩放，得到 Q、K 的梯度。&lt;/p&gt;</description>
    </item>
    <item>
      <title>调试指南</title>
      <link>https://inference.lei6393.com/flashattention/05-code-walkthrough/03-debug-guide/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://inference.lei6393.com/flashattention/05-code-walkthrough/03-debug-guide/</guid>
      <description>&lt;h2 id=&#34;目录&#34;&gt;目录&lt;/h2&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#1-%e6%a6%82%e8%bf%b0&#34;&gt;1. 概述&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#2-%e5%b8%b8%e8%a7%81%e9%94%99%e8%af%af%e4%b8%8e%e6%8e%92%e6%9f%a5&#34;&gt;2. 常见错误与排查&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#3-%e6%95%b0%e5%80%bc%e6%ad%a3%e7%a1%ae%e6%80%a7%e9%aa%8c%e8%af%81&#34;&gt;3. 数值正确性验证&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#4-%e6%80%a7%e8%83%bd%e8%b0%83%e8%af%95&#34;&gt;4. 性能调试&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#5-cuda-%e5%86%85%e6%a0%b8%e8%b0%83%e8%af%95&#34;&gt;5. CUDA 内核调试&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#6-%e8%b0%83%e8%af%95%e5%b7%a5%e5%85%b7%e4%b8%8e%e6%8a%80%e5%b7%a7&#34;&gt;6. 调试工具与技巧&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#7-%e5%b8%b8%e8%a7%81-faq&#34;&gt;7. 常见 FAQ&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;1-概述&#34;&gt;&#xA;    &lt;span id=&#34;toc-h-1-概述&#34;&gt;1. 概述&lt;/span&gt;&lt;/h2&gt;&#xA;&lt;p&gt;Flash Attention 的调试涉及 Python、C++ 和 CUDA 三个层次。由于其高性能实现大量使用了 TMA、GMMA 等硬件特性，传统的 &lt;code&gt;print&lt;/code&gt; 调试难以直接应用。先用同一组小张量复现输出，再沿 Python 接口和扩展入口缩小出错范围。&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
