---
title: attn_qat_train
---

<div class="python-reference">
<a id="fastvideo.attention.backends.attn_qat_train-classes"></a>
<a id="fastvideo.attention.backends.attn_qat_train-functions"></a>
<a id="fastvideo.attention.backends.attn_qat_train"></a>

<h2 id="fastvideo.attention.backends.attn_qat_train.AttentionBackend"><code>fastvideo.attention.backends.attn_qat_train.AttentionBackend</code></h2>
<pre class="doc-signature"><code>AttentionBackend()</code></pre>
<p>Abstract class for attention backends.</p>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_name"><code>fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_name</code></h3>
<pre class="doc-signature"><code>get_name() -&gt; str</code></pre>

<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_impl_cls"><code>fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_impl_cls</code></h3>
<pre class="doc-signature"><code>get_impl_cls() -&gt; type&#91;AttentionImpl&#93;</code></pre>

<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_metadata_cls"><code>fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_metadata_cls</code></h3>
<pre class="doc-signature"><code>get_metadata_cls() -&gt; type&#91;AttentionMetadata&#93;</code></pre>

<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_builder_cls"><code>fastvideo.attention.backends.attn_qat_train.AttentionBackend.get_builder_cls</code></h3>
<pre class="doc-signature"><code>get_builder_cls() -&gt; type&#91;AttentionMetadataBuilder&#93;</code></pre>

<h2 id="fastvideo.attention.backends.attn_qat_train.AttentionImpl"><code>fastvideo.attention.backends.attn_qat_train.AttentionImpl</code></h2>
<pre class="doc-signature"><code>AttentionImpl(num_heads: int, head_size: int, softmax_scale: float, causal: bool = False, num_kv_heads: int | None = None, prefix: str = &#x27;&#x27;, **extra_impl_args)</code></pre>


<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionImpl.preprocess_qkv"><code>fastvideo.attention.backends.attn_qat_train.AttentionImpl.preprocess_qkv</code></h3>
<pre class="doc-signature"><code>preprocess_qkv(qkv: torch.Tensor, attn_metadata: T) -&gt; torch.Tensor</code></pre>
<p>Preprocess QKV tensor before performing attention operation.</p>
<p>Default implementation returns the tensor unchanged.
Subclasses can override this to implement custom preprocessing
like reshaping, tiling, scaling, or other transformations.</p>
<p>Called AFTER all_to_all for distributed attention</p>
<h4>Parameters</h4>
<dl>
<dt><code>qkv: torch.Tensor</code></dt><dd><p>The query-key-value tensor</p></dd>
<dt><code>attn_metadata: T</code></dt><dd><p>Metadata for the attention operation</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>torch.Tensor</code></dt><dd><p>Processed QKV tensor</p></dd>
</dl>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionImpl.postprocess_output"><code>fastvideo.attention.backends.attn_qat_train.AttentionImpl.postprocess_output</code></h3>
<pre class="doc-signature"><code>postprocess_output(output: torch.Tensor, attn_metadata: T) -&gt; torch.Tensor</code></pre>
<p>Postprocess the output tensor after the attention operation.</p>
<p>Default implementation returns the tensor unchanged.
Subclasses can override this to implement custom postprocessing
like untiling, scaling, or other transformations.</p>
<p>Called BEFORE all_to_all for distributed attention</p>
<h4>Parameters</h4>
<dl>
<dt><code>output: torch.Tensor</code></dt><dd><p>The output tensor from the attention operation</p></dd>
<dt><code>attn_metadata: T</code></dt><dd><p>Metadata for the attention operation</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>torch.Tensor</code></dt><dd><p>Postprocessed output tensor</p></dd>
</dl>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionImpl.forward"><code>fastvideo.attention.backends.attn_qat_train.AttentionImpl.forward</code></h3>
<pre class="doc-signature"><code>forward(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_metadata: T) -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.attention.backends.attn_qat_train.AttentionMetadata"><code>fastvideo.attention.backends.attn_qat_train.AttentionMetadata</code></h2>
<pre class="doc-signature"><code>AttentionMetadata(current_timestep: int, *, VSA_sparsity: float = 0.0)</code></pre>
<p>Attention metadata for prefill and decode batched together.</p>

<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionMetadata.asdict_zerocopy"><code>fastvideo.attention.backends.attn_qat_train.AttentionMetadata.asdict_zerocopy</code></h3>
<pre class="doc-signature"><code>asdict_zerocopy(skip_fields: set&#91;str&#93; | None = None) -&gt; dict&#91;str, Any&#93;</code></pre>
<p>Similar to dataclasses.asdict, but avoids deepcopying.</p>
<h2 id="fastvideo.attention.backends.attn_qat_train.AttentionMetadataBuilder"><code>fastvideo.attention.backends.attn_qat_train.AttentionMetadataBuilder</code></h2>
<pre class="doc-signature"><code>AttentionMetadataBuilder()</code></pre>
<p>Abstract class for attention metadata builders.</p>
<p>Create the builder, remember some configuration and parameters.</p>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionMetadataBuilder.prepare"><code>fastvideo.attention.backends.attn_qat_train.AttentionMetadataBuilder.prepare</code></h3>
<pre class="doc-signature"><code>prepare() -&gt; None</code></pre>
<p>Prepare for one batch.</p>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttentionMetadataBuilder.build"><code>fastvideo.attention.backends.attn_qat_train.AttentionMetadataBuilder.build</code></h3>
<pre class="doc-signature"><code>build(**kwargs: Any) -&gt; AttentionMetadata</code></pre>
<p>Build attention metadata with on-device tensors.</p>
<h2 id="fastvideo.attention.backends.attn_qat_train.init_logger"><code>fastvideo.attention.backends.attn_qat_train.init_logger</code></h2>
<pre class="doc-signature"><code>init_logger(name: str) -&gt; _FastvideoLogger</code></pre>
<p>The main purpose of this function is to ensure that loggers are
retrieved in such a way that we can be sure the root fastvideo logger has
already been configured.</p>
<h2 id="fastvideo.attention.backends.attn_qat_train.is_attn_qat_train_available"><code>fastvideo.attention.backends.attn_qat_train.is_attn_qat_train_available</code></h2>
<pre class="doc-signature"><code>is_attn_qat_train_available() -&gt; bool</code></pre>

<details><summary>Source</summary><pre><code>def is_attn_qat_train_available() -&gt; bool:
    return _get_attn_qat_train_attention() is not None</code></pre></details>
<h2 id="fastvideo.attention.backends.attn_qat_train.attn_qat_train"><code>fastvideo.attention.backends.attn_qat_train.attn_qat_train</code></h2>
<pre class="doc-signature"><code>attn_qat_train(q_BLHD: torch.Tensor, k_BLHD: torch.Tensor, v_BLHD: torch.Tensor, is_causal: bool = False, sm_scale: float | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def attn_qat_train(q_BLHD: torch.Tensor,
                   k_BLHD: torch.Tensor,
                   v_BLHD: torch.Tensor,
                   is_causal: bool = False,
                   sm_scale: float | None = None) -&gt; torch.Tensor:
    attention = _get_attn_qat_train_attention()
    if attention is None:
        detail = f&quot; Original import error: &#123;_attn_qat_train_import_error&#125;&quot; if _attn_qat_train_import_error else &quot;&quot;
        raise ImportError(&quot;ATTN_QAT_TRAIN requires FastVideo&#x27;s fastvideo-kernel package. Install it or make &quot;
                          f&quot;fastvideo-kernel/python importable.&#123;detail&#125;&quot;)

    q_BHLD = q_BLHD.permute(0, 2, 1, 3).contiguous()
    k_BHLD = k_BLHD.permute(0, 2, 1, 3).contiguous()
    v_BHLD = v_BLHD.permute(0, 2, 1, 3).contiguous()

    use_qat_qkv_backward = True
    smooth_k = False
    # Triton 3.7&#x27;s NVWS pass aborts while compiling this kernel on Blackwell.
    # The kernel has a supported non-warp-specialized path, so use it on both
    # datacenter (sm_100) and consumer (sm_120) Blackwell GPUs.
    capability_major = torch.cuda.get_device_capability()&#91;0&#93;
    warp_specialize = capability_major not in (10, 12)
    is_qat = True
    two_level_quant_p_sage3 = False
    fake_quant_p_bwd = True
    use_high_prec_o = True
    smooth_q = False
    if sm_scale is None:
        sm_scale = 1.0 / (q_BHLD.shape&#91;-1&#93;**0.5)
    use_global_sf_qkv = False
    use_global_sf_p = False

    o_BHLD = attention(
        q_BHLD,
        k_BHLD,
        v_BHLD,
        is_causal,
        sm_scale,
        use_qat_qkv_backward,
        smooth_k,
        warp_specialize,
        is_qat,
        two_level_quant_p_sage3,
        fake_quant_p_bwd,
        use_high_prec_o,
        smooth_q,
        use_global_sf_p,
        use_global_sf_qkv,
    )
    return o_BHLD.permute(0, 2, 1, 3).contiguous()</code></pre></details>
<h2 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend</code></h2>
<pre class="doc-signature"><code>AttnQatTrainBackend()</code></pre>

<details><summary>Source</summary><pre><code>class AttnQatTrainBackend(AttentionBackend):

    accept_output_buffer: bool = True

    @staticmethod
    def get_supported_head_sizes() -&gt; list&#91;int&#93;:
        return &#91;128&#93;

    @staticmethod
    def get_name() -&gt; str:
        return &quot;ATTN_QAT_TRAIN&quot;

    @staticmethod
    def get_impl_cls() -&gt; type&#91;&quot;AttnQatTrainImpl&quot;&#93;:
        return AttnQatTrainImpl

    @staticmethod
    def get_metadata_cls() -&gt; type&#91;&quot;AttentionMetadata&quot;&#93;:
        raise NotImplementedError

    @staticmethod
    def get_builder_cls() -&gt; type&#91;&quot;AttentionMetadataBuilder&#91;AttentionMetadata&#93;&quot;&#93;:
        raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_supported_head_sizes"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_supported_head_sizes</code></h3>
<pre class="doc-signature"><code>get_supported_head_sizes() -&gt; list&#91;int&#93;</code></pre>

<details><summary>Source</summary><pre><code>@staticmethod
def get_supported_head_sizes() -&gt; list&#91;int&#93;:
    return &#91;128&#93;</code></pre></details>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_name"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_name</code></h3>
<pre class="doc-signature"><code>get_name() -&gt; str</code></pre>

<details><summary>Source</summary><pre><code>@staticmethod
def get_name() -&gt; str:
    return &quot;ATTN_QAT_TRAIN&quot;</code></pre></details>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_impl_cls"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_impl_cls</code></h3>
<pre class="doc-signature"><code>get_impl_cls() -&gt; type&#91;AttnQatTrainImpl&#93;</code></pre>

<details><summary>Source</summary><pre><code>@staticmethod
def get_impl_cls() -&gt; type&#91;&quot;AttnQatTrainImpl&quot;&#93;:
    return AttnQatTrainImpl</code></pre></details>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_metadata_cls"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_metadata_cls</code></h3>
<pre class="doc-signature"><code>get_metadata_cls() -&gt; type&#91;AttentionMetadata&#93;</code></pre>

<details><summary>Source</summary><pre><code>@staticmethod
def get_metadata_cls() -&gt; type&#91;&quot;AttentionMetadata&quot;&#93;:
    raise NotImplementedError</code></pre></details>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_builder_cls"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainBackend.get_builder_cls</code></h3>
<pre class="doc-signature"><code>get_builder_cls() -&gt; type&#91;AttentionMetadataBuilder&#91;AttentionMetadata&#93;&#93;</code></pre>

<details><summary>Source</summary><pre><code>@staticmethod
def get_builder_cls() -&gt; type&#91;&quot;AttentionMetadataBuilder&#91;AttentionMetadata&#93;&quot;&#93;:
    raise NotImplementedError</code></pre></details>
<h2 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainImpl"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainImpl</code></h2>
<pre class="doc-signature"><code>AttnQatTrainImpl(num_heads: int, head_size: int, causal: bool, softmax_scale: float, num_kv_heads: int | None = None, prefix: str = &#x27;&#x27;, **extra_impl_args)</code></pre>


<details><summary>Source</summary><pre><code>class AttnQatTrainImpl(AttentionImpl&#91;AttentionMetadata&#93;):

    def __init__(
        self,
        num_heads: int,
        head_size: int,
        causal: bool,
        softmax_scale: float,
        num_kv_heads: int | None = None,
        prefix: str = &quot;&quot;,
        **extra_impl_args,
    ) -&gt; None:
        self.causal = causal
        self.softmax_scale = softmax_scale
        dropout_p = extra_impl_args.get(&quot;dropout_p&quot;, 0.0)
        if dropout_p &gt; 0:
            raise NotImplementedError(f&quot;attn_qat_train does not support dropout (got dropout_p=&#123;dropout_p&#125;). &quot;
                                      &quot;The QAT training kernel applies no stochastic dropout.&quot;)

    def forward(
        self,
        query: torch.Tensor,
        key: torch.Tensor,
        value: torch.Tensor,
        attn_metadata: AttentionMetadata,
    ) -&gt; torch.Tensor:
        return attn_qat_train(query, key, value, is_causal=self.causal, sm_scale=self.softmax_scale)</code></pre></details>
<h3 id="fastvideo.attention.backends.attn_qat_train.AttnQatTrainImpl.forward"><code>fastvideo.attention.backends.attn_qat_train.AttnQatTrainImpl.forward</code></h3>
<pre class="doc-signature"><code>forward(query: torch.Tensor, key: torch.Tensor, value: torch.Tensor, attn_metadata: AttentionMetadata) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def forward(
    self,
    query: torch.Tensor,
    key: torch.Tensor,
    value: torch.Tensor,
    attn_metadata: AttentionMetadata,
) -&gt; torch.Tensor:
    return attn_qat_train(query, key, value, is_causal=self.causal, sm_scale=self.softmax_scale)</code></pre></details>
</div>
