<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Python API on Inference Cookbook</title>
    <link>https://inference.lei6393.com/flashattention/04-python-api/</link>
    <description>Recent content in Python API 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/04-python-api/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Python API 参考</title>
      <link>https://inference.lei6393.com/flashattention/04-python-api/01-api-reference/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://inference.lei6393.com/flashattention/04-python-api/01-api-reference/</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%a0%b8%e5%bf%83%e5%87%bd%e6%95%b0%e6%a6%82%e8%a7%88&#34;&gt;1. 核心函数概览&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#2-flash_attn_func--%e6%a0%87%e5%87%86-attention&#34;&gt;2. flash_attn_func — 标准 Attention&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#3-%e6%89%93%e5%8c%85%e5%8f%98%e4%bd%93--qkvpacked-%e4%b8%8e-kvpacked&#34;&gt;3. 打包变体 — QKVPacked 与 KVPacked&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#4-varlen-%e5%8f%98%e4%bd%93--%e5%8f%98%e9%95%bf%e5%ba%8f%e5%88%97&#34;&gt;4. Varlen 变体 — 变长序列&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#5-flash_attn_with_kvcache--%e6%8e%a8%e7%90%86%e4%b8%93%e7%94%a8&#34;&gt;5. flash_attn_with_kvcache — 推理专用&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#6-%e5%90%8e%e7%ab%af%e9%80%89%e6%8b%a9%e9%80%bb%e8%be%91&#34;&gt;6. 后端选择逻辑&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#7-%e5%bc%a0%e9%87%8f%e5%bd%a2%e7%8a%b6%e9%80%9f%e6%9f%a5&#34;&gt;7. 张量形状速查&lt;/a&gt;&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&lt;hr&gt;&#xA;&lt;p&gt;本页表列的函数属于 FA2 &lt;code&gt;v2.8.3.post2&lt;/code&gt;。FA3 的同名函数额外支持 Hopper 路径的参数；FA4 beta39 的 &lt;code&gt;flash_attn_func&lt;/code&gt; 是 &lt;code&gt;flash_attn.cute&lt;/code&gt; 下的独立接口，返回 &lt;code&gt;(out, lse)&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 源码&lt;/a&gt; · &lt;a href=&#34;https://inference.lei6393.com/flashattention/00-overview/02-architecture-overview/&#34;&gt;三代入口&lt;/a&gt;&lt;/p&gt;</description>
    </item>
    <item>
      <title>Autograd 集成与编译支持</title>
      <link>https://inference.lei6393.com/flashattention/04-python-api/02-autograd-integration/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://inference.lei6393.com/flashattention/04-python-api/02-autograd-integration/</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-autograd-function-%e6%9e%b6%e6%9e%84&#34;&gt;2. Autograd Function 架构&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#3-flashattnfunc-%e8%af%a6%e8%a7%a3&#34;&gt;3. FlashAttnFunc 详解&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#4-%e5%8f%98%e4%bd%93-function-%e5%af%b9%e6%af%94&#34;&gt;4. 变体 Function 对比&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#5-torchcompile-%e9%9b%86%e6%88%90&#34;&gt;5. torch.compile 集成&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#6-dropout-%e4%b8%8e-rng-%e7%8a%b6%e6%80%81%e7%ae%a1%e7%90%86&#34;&gt;6. Dropout 与 RNG 状态管理&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#7-head-dimension-padding&#34;&gt;7. Head Dimension Padding&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#8-%e6%a2%af%e5%ba%a6%e6%b5%81%e5%88%86%e6%9e%90&#34;&gt;8. 梯度流分析&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 API 通过 &lt;code&gt;torch.autograd.Function&lt;/code&gt; 将 CUDA 内核封装为可自动微分的 PyTorch 算子。这一层的核心职责包括：&lt;/p&gt;&#xA;&lt;ul&gt;&#xA;&lt;li&gt;&lt;strong&gt;前向/反向调度&lt;/strong&gt;：将用户调用路由到对应的 CUDA 前向和反向内核&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;上下文管理&lt;/strong&gt;：在前向时保存反向所需的中间结果（&lt;code&gt;q&lt;/code&gt;, &lt;code&gt;k&lt;/code&gt;, &lt;code&gt;v&lt;/code&gt;, &lt;code&gt;out&lt;/code&gt;, &lt;code&gt;softmax_lse&lt;/code&gt;, &lt;code&gt;rng_state&lt;/code&gt;）&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;Head Dimension 对齐&lt;/strong&gt;：自动 padding 到 8 的倍数以满足 CUDA 内核对齐要求&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;torch.compile 兼容&lt;/strong&gt;：通过 &lt;code&gt;torch.library.custom_op&lt;/code&gt; 注册为可被编译器追踪的自定义算子&lt;/li&gt;&#xA;&lt;li&gt;&lt;strong&gt;RNG 状态捕获&lt;/strong&gt;：保存 Dropout 随机数生成器状态，确保反向传播时能精确重放 Dropout mask&lt;/li&gt;&#xA;&lt;/ul&gt;&#xA;&#xD;&#xA;&#xD;&#xA;&#xD;&#xA;&lt;div class=&#34;expressive-code&#34;&gt;&#xD;&#xA;  &lt;figure class=&#34;frame not-content&#34;&gt;&#xD;&#xA;  &lt;figcaption class=&#34;header&#34;&gt;&#xD;&#xA;    &lt;span class=&#34;title&#34;&gt;&lt;/span&gt;&#xD;&#xA;  &lt;/figcaption&gt;&#xD;&#xA;  &lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# flash_attn/flash_attn_interface.py 的分层结构&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;#&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# 用户 API（flash_attn_func 等）&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;#   ↓ 调用&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Autograd Function（FlashAttnFunc.apply）&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;#   ↓ 调用&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# Custom Op Wrapper（_wrapped_flash_attn_forward）&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;#   ↓ 调用&lt;/span&gt;&#xA;&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;c1&#34;&gt;# CUDA 绑定（flash_attn_gpu.fwd / flash_attn_gpu.bwd）&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&#xD;&#xA;  &lt;/figure&gt;&#xD;&#xA;&lt;/div&gt;&#xD;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;2-autograd-function-架构&#34;&gt;&#xA;    &lt;span id=&#34;toc-h-2-autograd-function-架构&#34;&gt;2. Autograd Function 架构&lt;/span&gt;&lt;/h2&gt;&#xA;&lt;h3 id=&#34;21-六个-function-类&#34;&gt;&#xA;    &lt;span id=&#34;toc-h-21-六个-function-类&#34;&gt;2.1 六个 Function 类&lt;/span&gt;&lt;/h3&gt;&#xA;&lt;p&gt;Flash Attention 定义了 6 个 &lt;code&gt;torch.autograd.Function&lt;/code&gt; 子类，与 6 个训练 API 一一对应：&lt;/p&gt;</description>
    </item>
    <item>
      <title>模块与层</title>
      <link>https://inference.lei6393.com/flashattention/04-python-api/03-modules-and-layers/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      <guid>https://inference.lei6393.com/flashattention/04-python-api/03-modules-and-layers/</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%86%85%e9%83%a8-attention-%e6%a8%a1%e5%9d%97&#34;&gt;2. 内部 Attention 模块&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#3-mha-%e6%a8%a1%e5%9d%97&#34;&gt;3. MHA 模块&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#4-rotaryembedding&#34;&gt;4. RotaryEmbedding&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#5-transformer-block&#34;&gt;5. Transformer Block&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#6-%e5%b9%b6%e8%a1%8c%e5%8c%96%e5%8f%98%e4%bd%93&#34;&gt;6. 并行化变体&lt;/a&gt;&lt;/li&gt;&#xA;&lt;li&gt;&lt;a href=&#34;#7-%e7%bb%84%e8%a3%85%e5%ae%8c%e6%95%b4%e6%a8%a1%e5%9e%8b&#34;&gt;7. 组装完整模型&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 不仅提供底层的 Attention 计算函数，还封装了一系列生产可用的 &lt;code&gt;nn.Module&lt;/code&gt; 组件。这些模块在 &lt;code&gt;flash_attn/modules/&lt;/code&gt; 和 &lt;code&gt;flash_attn/layers/&lt;/code&gt; 目录下，形成从 Attention 计算到完整 Transformer 层的组件栈：&lt;/p&gt;&#xA;&lt;pre class=&#34;mermaid text-center&#34;&gt;graph TB&#xA;    subgraph layers[&amp;#34;组件栈（自底向上）&amp;#34;]&#xA;        L1[&amp;#34;Flash Attention CUDA 内核&amp;#34;]&#xA;        L2[&amp;#34;flash_attn_func / flash_attn_kvpacked_func&amp;#34;]&#xA;        L3[&amp;#34;FlashSelfAttention / FlashCrossAttention&amp;#34;]&#xA;        L4[&amp;#34;MHA（Multi-Head Attention）&amp;#34;]&#xA;        L5[&amp;#34;Block / ParallelBlock&amp;#34;]&#xA;    end&#xA;&#xA;    L1 --&amp;gt; L2&#xA;    L2 --&amp;gt; L3&#xA;    L3 --&amp;gt; L4&#xA;    L4 --&amp;gt; L5&#xA;&#xA;    subgraph support[&amp;#34;辅助组件&amp;#34;]&#xA;        R[&amp;#34;RotaryEmbedding&amp;#34;]&#xA;        M[&amp;#34;Mlp / GatedMlp&amp;#34;]&#xA;        N[&amp;#34;LayerNorm / RMSNorm&amp;#34;]&#xA;    end&#xA;&#xA;    R --&amp;gt; L4&#xA;    M --&amp;gt; L5&#xA;    N --&amp;gt; L5&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;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;&lt;code&gt;FlashSelfAttention&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/mha.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Self-Attention 包装&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;FlashCrossAttention&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/mha.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;Cross-Attention 包装&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;MHA&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/mha.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;完整多头注意力（含投影）&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;ParallelMHA&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/mha.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;张量并行版 MHA&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;RotaryEmbedding&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/layers/rotary.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;RoPE 位置编码&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;Block&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/block.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;标准 Transformer 块&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;ParallelBlock&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/block.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;并行 Attention+MLP 块&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;      &lt;tr&gt;&#xA;          &lt;td&gt;&lt;code&gt;Mlp&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;&lt;code&gt;flash_attn/modules/mlp.py&lt;/code&gt;&lt;/td&gt;&#xA;          &lt;td&gt;标准前馈网络&lt;/td&gt;&#xA;      &lt;/tr&gt;&#xA;  &lt;/tbody&gt;&#xA;&lt;/table&gt;&#xA;&lt;hr&gt;&#xA;&lt;h2 id=&#34;2-内部-attention-模块&#34;&gt;&#xA;    &lt;span id=&#34;toc-h-2-内部-attention-模块&#34;&gt;2. 内部 Attention 模块&lt;/span&gt;&lt;/h2&gt;&#xA;&lt;h3 id=&#34;21-flashselfattention&#34;&gt;&#xA;    &lt;span id=&#34;toc-h-21-flashselfattention&#34;&gt;2.1 FlashSelfAttention&lt;/span&gt;&lt;/h3&gt;&#xA;&lt;p&gt;&lt;code&gt;FlashSelfAttention&lt;/code&gt;（&lt;code&gt;flash_attn/modules/mha.py:53-130&lt;/code&gt;）是对 &lt;code&gt;flash_attn_qkvpacked_func&lt;/code&gt; 和 &lt;code&gt;flash_attn_varlen_qkvpacked_func&lt;/code&gt; 的 &lt;code&gt;nn.Module&lt;/code&gt; 包装：&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
