---
title: fp8_config
---

<div class="python-reference">
<a id="fastvideo.layers.quantization.fp8_config-classes"></a>
<a id="fastvideo.layers.quantization.fp8_config-functions"></a>
<a id="fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod-functions"></a>
<a id="fastvideo.layers.quantization.fp8_config"></a>
<p>Generic FP8 quantization backed by <code>torch._scaled_mm</code>.</p>
<p>Matches linear layers by suffix (<code>to_q/k/v/to_out</code>, <code>ffn.fc_in/fc_out</code>).
Supports per-tensor (default, fast) and per-channel (higher accuracy) granularity.
On sm89 the per-channel path runs the per-tensor GEMM plus a Triton scale epilogue
(<code>fp8_kernels</code>), since torch's rowwise-scaled kernel there is slower than bf16.
Falls back to bf16 dequant on GPUs older than sm89.</p>
<h2 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig</code></h2>
<pre class="doc-signature"><code>QuantizationConfig()</code></pre>
<p>Base class for quantization configs.</p>

<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_name"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_name</code></h3>
<pre class="doc-signature"><code>get_name() -&gt; QuantizationMethods</code></pre>
<p>Name of the quantization method.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_supported_act_dtypes"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_supported_act_dtypes</code></h3>
<pre class="doc-signature"><code>get_supported_act_dtypes() -&gt; list&#91;torch.dtype&#93;</code></pre>
<p>List of supported activation dtypes.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_min_capability"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_min_capability</code></h3>
<pre class="doc-signature"><code>get_min_capability() -&gt; int</code></pre>
<p>Minimum GPU capability to support the quantization method.</p>
<p>E.g., 70 for Volta, 75 for Turing, 80 for Ampere.
This requirement is due to the custom CUDA kernels used by the
quantization method.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_config_filenames"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_config_filenames</code></h3>
<pre class="doc-signature"><code>get_config_filenames() -&gt; list&#91;str&#93;</code></pre>
<p>List of filenames to search for in the model directory.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.from_config"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.from_config</code></h3>
<pre class="doc-signature"><code>from_config(config: dict&#91;str, Any&#93;) -&gt; QuantizationConfig</code></pre>
<p>Create a config class from the model's quantization config.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.override_quantization_method"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.override_quantization_method</code></h3>
<pre class="doc-signature"><code>override_quantization_method(hf_quant_cfg, user_quant) -&gt; QuantizationMethods | None</code></pre>
<p>Detects if this quantization method can support a given checkpoint
format by overriding the user specified quantization method -- 
this method should only be overwritten by subclasses in exceptional 
circumstances</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_from_keys"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_from_keys</code></h3>
<pre class="doc-signature"><code>get_from_keys(config: dict&#91;str, Any&#93;, keys: list&#91;str&#93;) -&gt; Any</code></pre>
<p>Get a value from the model's quantization config.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_from_keys_or"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_from_keys_or</code></h3>
<pre class="doc-signature"><code>get_from_keys_or(config: dict&#91;str, Any&#93;, keys: list&#91;str&#93;, default: Any) -&gt; Any</code></pre>
<p>Get a optional value from the model's quantization config.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_quant_method"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_quant_method</code></h3>
<pre class="doc-signature"><code>get_quant_method(layer: torch.nn.Module, prefix: str) -&gt; QuantizeMethodBase | None</code></pre>
<p>Get the quantize method to use for the quantized layer.</p>
<h4>Parameters</h4>
<dl>
<dt><code>layer: torch.nn.Module</code></dt><dd><p>The layer for the quant method.</p></dd>
<dt><code>prefix: str</code></dt><dd><p>The full name of the layer in the state dict</p></dd>
</dl>
<p>Returns:
    The quantize method. None if the given layer doesn't support quant
    method.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.validate_runtime"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.validate_runtime</code></h3>
<pre class="doc-signature"><code>validate_runtime(device: torch.device) -&gt; None</code></pre>
<p>Fail fast on a device the scheme cannot execute on.</p>
<p>Called once by loaders that select a serialized checkpoint scheme,
before any weight is read. The default accepts every device; schemes
with kernel or capability requirements override it.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_cache_scale"><code>fastvideo.layers.quantization.fp8_config.QuantizationConfig.get_cache_scale</code></h3>
<pre class="doc-signature"><code>get_cache_scale(name: str) -&gt; str | None</code></pre>

<h2 id="fastvideo.layers.quantization.fp8_config.QuantizeMethodBase"><code>fastvideo.layers.quantization.fp8_config.QuantizeMethodBase</code></h2>
<pre class="doc-signature"><code>QuantizeMethodBase()</code></pre>
<p>Base class for different quantized methods.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.create_weights"><code>fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.create_weights</code></h3>
<pre class="doc-signature"><code>create_weights(layer: torch.nn.Module, *weight_args, **extra_weight_attrs)</code></pre>
<p>Create weights for a layer.</p>
<p>The weights will be set as attributes of the layer.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.apply"><code>fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.apply</code></h3>
<pre class="doc-signature"><code>apply(layer: torch.nn.Module, *args, **kwargs) -&gt; torch.Tensor</code></pre>
<p>Apply the weights in layer to the input tensor.</p>
<p>Expects create_weights to have been called before on the layer.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.embedding"><code>fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.embedding</code></h3>
<pre class="doc-signature"><code>embedding(layer: torch.nn.Module, *args, **kwargs) -&gt; torch.Tensor</code></pre>
<p>Gather embeddings in the layer based on indices in the input tensor.</p>
<p>Expects create_weights to have been called before on the layer.</p>
<h3 id="fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.process_weights_after_loading"><code>fastvideo.layers.quantization.fp8_config.QuantizeMethodBase.process_weights_after_loading</code></h3>
<pre class="doc-signature"><code>process_weights_after_loading(layer: nn.Module) -&gt; None</code></pre>
<p>Process the weight after loading.</p>
<p>This can be used for example, to transpose weights for computation.</p>
<h2 id="fastvideo.layers.quantization.fp8_config.quantize_rowwise_fused"><code>fastvideo.layers.quantization.fp8_config.quantize_rowwise_fused</code></h2>
<pre class="doc-signature"><code>quantize_rowwise_fused(x_2d: torch.Tensor) -&gt; tuple&#91;torch.Tensor, torch.Tensor&#93;</code></pre>
<p>Returns <code>(x_fp8 [M, K], x_scale [M, 1] float32)</code>; one kernel launch.</p>
<h2 id="fastvideo.layers.quantization.fp8_config.quantize_tensorwise_fused"><code>fastvideo.layers.quantization.fp8_config.quantize_tensorwise_fused</code></h2>
<pre class="doc-signature"><code>quantize_tensorwise_fused(x_2d: torch.Tensor) -&gt; tuple&#91;torch.Tensor, torch.Tensor&#93;</code></pre>
<p>Returns <code>(x_fp8 [M, K], x_scale [1] float32)</code>; an <code>aminmax</code> reduction + one pass.</p>
<h2 id="fastvideo.layers.quantization.fp8_config.triton_quant_available"><code>fastvideo.layers.quantization.fp8_config.triton_quant_available</code></h2>
<pre class="doc-signature"><code>triton_quant_available(x: torch.Tensor) -&gt; bool</code></pre>

<h2 id="fastvideo.layers.quantization.fp8_config.set_weight_attrs"><code>fastvideo.layers.quantization.fp8_config.set_weight_attrs</code></h2>
<pre class="doc-signature"><code>set_weight_attrs(weight: torch.Tensor, weight_attrs: dict&#91;str, Any&#93; | None)</code></pre>
<p>Set attributes on a weight tensor.</p>
<p>This method is used to set attributes on a weight tensor. This method
will not overwrite existing attributes.</p>
<h3>Parameters</h3>
<dl>
<dt><code>weight: torch.Tensor</code></dt><dd><p>The weight tensor.</p></dd>
<dt><code>weight_attrs: dict[str, Any] | None</code></dt><dd><p>A dictionary of attributes to set on the weight tensor.</p></dd>
</dl>
<h2 id="fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod"><code>fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod</code></h2>
<pre class="doc-signature"><code>FP8QuantizeMethod(granularity: str = &#x27;tensor&#x27;)</code></pre>
<p>FP8 linear method.</p>
<p><code>granularity='tensor'</code> (default): per-tensor weight + per-tensor
dynamic activation scales — the fast tensorwise <code>_scaled_mm</code> path.
<code>granularity='channel'</code>: per-output-channel weight + per-token
activation scales (rowwise) — higher accuracy but slower <code>_scaled_mm</code>.</p>

<details><summary>Source</summary><pre><code>class FP8QuantizeMethod(QuantizeMethodBase):
    &quot;&quot;&quot;FP8 linear method.

    ``granularity=&#x27;tensor&#x27;`` (default): per-tensor weight + per-tensor
    dynamic activation scales — the fast tensorwise ``_scaled_mm`` path.
    ``granularity=&#x27;channel&#x27;``: per-output-channel weight + per-token
    activation scales (rowwise) — higher accuracy but slower ``_scaled_mm``.
    &quot;&quot;&quot;

    def __init__(self, granularity: str = &quot;tensor&quot;):
        super().__init__()
        self.granularity = granularity

    def create_weights(
        self,
        layer: torch.nn.Module,
        input_size_per_partition: int,
        output_partition_sizes: list&#91;int&#93;,
        input_size: int,
        output_size: int,
        params_dtype: torch.dtype,
        **extra_weight_attrs,
    ):
        weight = Parameter(
            torch.empty(
                sum(output_partition_sizes),
                input_size_per_partition,
                dtype=params_dtype,
            ),
            requires_grad=False,
        )
        set_weight_attrs(weight, &#123;&quot;input_dim&quot;: 1, &quot;output_dim&quot;: 0&#125;)
        layer.register_parameter(&quot;weight&quot;, weight)
        set_weight_attrs(weight, extra_weight_attrs)

    def quantize_input(self, x: torch.Tensor) -&gt; tuple&#91;torch.Tensor, torch.Tensor, None&#93;:
        &quot;&quot;&quot;Pre-quantize an activation for reuse across q/k/v projections.&quot;&quot;&quot;
        assert x.dtype in (torch.bfloat16, torch.float16), (f&quot;only allow bf16/fp16 inputs to fp8 linear, got &#123;x.dtype&#125;&quot;)
        x_2d = x.view(-1, x.shape&#91;-1&#93;)
        if self.granularity == &quot;channel&quot;:
            x_fp8, x_scale = _quantize_rowwise(x_2d)
        else:
            x_fp8, x_scale = _quantize_tensorwise(x_2d)
        return x_fp8, x_scale, None

    def wants_prequantized_input(self) -&gt; bool:
        return True

    def apply(
        self,
        layer: torch.nn.Module,
        x: torch.Tensor,
        bias: torch.Tensor | None = None,
        pre_quantized: tuple&#91;torch.Tensor, torch.Tensor, Any&#93; | None = None,
    ) -&gt; torch.Tensor:
        out_dim = layer._fp8_weight.shape&#91;0&#93;
        original_shape = x.shape

        if not _supports_fp8_compute():
            return self._apply_dequant(layer, x, bias)

        if pre_quantized is not None:
            x_fp8, x_scale, _ = pre_quantized
            if x_fp8.dim() &gt; 2:
                x_fp8 = x_fp8.reshape(-1, x_fp8.shape&#91;-1&#93;)
            if x_scale.dim() &gt; 2:
                x_scale = x_scale.reshape(-1, x_scale.shape&#91;-1&#93;)
        elif self.granularity == &quot;channel&quot;:
            x_fp8, x_scale = _quantize_rowwise(x.reshape(-1, x.shape&#91;-1&#93;))
        else:
            x_fp8, x_scale = _quantize_tensorwise(x.reshape(-1, x.shape&#91;-1&#93;))

        w_fp8 = layer._fp8_weight
        w_scale = layer._fp8_weight_scale
        if self.granularity == &quot;channel&quot; and _rowwise_scaled_mm_is_slow():
            from fastvideo.layers.quantization.fp8_kernels import scaled_mm_token_channel
            out = scaled_mm_token_channel(x_fp8, x_scale, w_fp8.t(), w_scale)
            if bias is not None:
                out = out + bias
            return out.view(*original_shape&#91;:-1&#93;, out_dim)
        scale_b = w_scale.view(1, -1) if self.granularity == &quot;channel&quot; else w_scale

        out = torch._scaled_mm(
            x_fp8,
            w_fp8.t(),
            scale_a=x_scale,
            scale_b=scale_b,
            out_dtype=torch.bfloat16,
        )
        if isinstance(out, tuple):
            out = out&#91;0&#93;
        if bias is not None:
            out = out + bias
        return out.view(*original_shape&#91;:-1&#93;, out_dim)

    def _apply_dequant(
        self,
        layer: torch.nn.Module,
        x: torch.Tensor,
        bias: torch.Tensor | None = None,
    ) -&gt; torch.Tensor:
        &quot;&quot;&quot;bf16 fallback for pre-sm89 GPUs.&quot;&quot;&quot;
        out_dim = layer._fp8_weight.shape&#91;0&#93;
        original_shape = x.shape
        w_fp8 = layer._fp8_weight
        w_scale = layer._fp8_weight_scale.to(x.dtype)
        weight = w_fp8.to(x.dtype) * w_scale.unsqueeze(1)
        out = F.linear(x, weight, bias)
        return out.view(*original_shape&#91;:-1&#93;, out_dim)</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.create_weights"><code>fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.create_weights</code></h3>
<pre class="doc-signature"><code>create_weights(layer: torch.nn.Module, input_size_per_partition: int, output_partition_sizes: list&#91;int&#93;, input_size: int, output_size: int, params_dtype: torch.dtype, **extra_weight_attrs)</code></pre>

<details><summary>Source</summary><pre><code>def create_weights(
    self,
    layer: torch.nn.Module,
    input_size_per_partition: int,
    output_partition_sizes: list&#91;int&#93;,
    input_size: int,
    output_size: int,
    params_dtype: torch.dtype,
    **extra_weight_attrs,
):
    weight = Parameter(
        torch.empty(
            sum(output_partition_sizes),
            input_size_per_partition,
            dtype=params_dtype,
        ),
        requires_grad=False,
    )
    set_weight_attrs(weight, &#123;&quot;input_dim&quot;: 1, &quot;output_dim&quot;: 0&#125;)
    layer.register_parameter(&quot;weight&quot;, weight)
    set_weight_attrs(weight, extra_weight_attrs)</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.quantize_input"><code>fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.quantize_input</code></h3>
<pre class="doc-signature"><code>quantize_input(x: torch.Tensor) -&gt; tuple&#91;torch.Tensor, torch.Tensor, None&#93;</code></pre>
<p>Pre-quantize an activation for reuse across q/k/v projections.</p>
<details><summary>Source</summary><pre><code>def quantize_input(self, x: torch.Tensor) -&gt; tuple&#91;torch.Tensor, torch.Tensor, None&#93;:
    &quot;&quot;&quot;Pre-quantize an activation for reuse across q/k/v projections.&quot;&quot;&quot;
    assert x.dtype in (torch.bfloat16, torch.float16), (f&quot;only allow bf16/fp16 inputs to fp8 linear, got &#123;x.dtype&#125;&quot;)
    x_2d = x.view(-1, x.shape&#91;-1&#93;)
    if self.granularity == &quot;channel&quot;:
        x_fp8, x_scale = _quantize_rowwise(x_2d)
    else:
        x_fp8, x_scale = _quantize_tensorwise(x_2d)
    return x_fp8, x_scale, None</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.wants_prequantized_input"><code>fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.wants_prequantized_input</code></h3>
<pre class="doc-signature"><code>wants_prequantized_input() -&gt; bool</code></pre>

<details><summary>Source</summary><pre><code>def wants_prequantized_input(self) -&gt; bool:
    return True</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.apply"><code>fastvideo.layers.quantization.fp8_config.FP8QuantizeMethod.apply</code></h3>
<pre class="doc-signature"><code>apply(layer: torch.nn.Module, x: torch.Tensor, bias: torch.Tensor | None = None, pre_quantized: tuple&#91;torch.Tensor, torch.Tensor, Any&#93; | None = None) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def apply(
    self,
    layer: torch.nn.Module,
    x: torch.Tensor,
    bias: torch.Tensor | None = None,
    pre_quantized: tuple&#91;torch.Tensor, torch.Tensor, Any&#93; | None = None,
) -&gt; torch.Tensor:
    out_dim = layer._fp8_weight.shape&#91;0&#93;
    original_shape = x.shape

    if not _supports_fp8_compute():
        return self._apply_dequant(layer, x, bias)

    if pre_quantized is not None:
        x_fp8, x_scale, _ = pre_quantized
        if x_fp8.dim() &gt; 2:
            x_fp8 = x_fp8.reshape(-1, x_fp8.shape&#91;-1&#93;)
        if x_scale.dim() &gt; 2:
            x_scale = x_scale.reshape(-1, x_scale.shape&#91;-1&#93;)
    elif self.granularity == &quot;channel&quot;:
        x_fp8, x_scale = _quantize_rowwise(x.reshape(-1, x.shape&#91;-1&#93;))
    else:
        x_fp8, x_scale = _quantize_tensorwise(x.reshape(-1, x.shape&#91;-1&#93;))

    w_fp8 = layer._fp8_weight
    w_scale = layer._fp8_weight_scale
    if self.granularity == &quot;channel&quot; and _rowwise_scaled_mm_is_slow():
        from fastvideo.layers.quantization.fp8_kernels import scaled_mm_token_channel
        out = scaled_mm_token_channel(x_fp8, x_scale, w_fp8.t(), w_scale)
        if bias is not None:
            out = out + bias
        return out.view(*original_shape&#91;:-1&#93;, out_dim)
    scale_b = w_scale.view(1, -1) if self.granularity == &quot;channel&quot; else w_scale

    out = torch._scaled_mm(
        x_fp8,
        w_fp8.t(),
        scale_a=x_scale,
        scale_b=scale_b,
        out_dtype=torch.bfloat16,
    )
    if isinstance(out, tuple):
        out = out&#91;0&#93;
    if bias is not None:
        out = out + bias
    return out.view(*original_shape&#91;:-1&#93;, out_dim)</code></pre></details>
<h2 id="fastvideo.layers.quantization.fp8_config.FP8Config"><code>fastvideo.layers.quantization.fp8_config.FP8Config</code></h2>
<pre class="doc-signature"><code>FP8Config(granularity: str = &#x27;tensor&#x27;)</code></pre>
<p>FP8 (e4m3) quantization via suffix matching on standard linear layer names.</p>

<details><summary>Source</summary><pre><code>class FP8Config(QuantizationConfig):
    &quot;&quot;&quot;FP8 (e4m3) quantization via suffix matching on standard linear layer names.&quot;&quot;&quot;

    def __init__(self, granularity: str = &quot;tensor&quot;):
        super().__init__()
        if granularity not in (&quot;tensor&quot;, &quot;channel&quot;):
            raise ValueError(f&quot;granularity must be &#x27;tensor&#x27; or &#x27;channel&#x27;, got &#123;granularity!r&#125;&quot;)
        self.granularity = granularity

    def get_name(self) -&gt; str:
        return &quot;FP8&quot;

    def get_supported_act_dtypes(self) -&gt; list&#91;torch.dtype&#93;:
        return &#91;torch.bfloat16, torch.float16&#93;

    @classmethod
    def get_min_capability(cls) -&gt; int:
        return 89

    @staticmethod
    def get_config_filenames() -&gt; list&#91;str&#93;:
        return &#91;&#93;

    @classmethod
    def from_config(cls, config: dict&#91;str, Any&#93;) -&gt; FP8Config:
        return cls(granularity=config.get(&quot;granularity&quot;, &quot;tensor&quot;))

    def get_quant_method(self, layer: torch.nn.Module, prefix: str):
        from fastvideo.layers.linear import LinearBase

        if isinstance(layer, LinearBase) and any(s in prefix for s in _FP8_SUFFIXES):
            return FP8QuantizeMethod(granularity=self.granularity)
        return None</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8Config.get_name"><code>fastvideo.layers.quantization.fp8_config.FP8Config.get_name</code></h3>
<pre class="doc-signature"><code>get_name() -&gt; str</code></pre>

<details><summary>Source</summary><pre><code>def get_name(self) -&gt; str:
    return &quot;FP8&quot;</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8Config.get_supported_act_dtypes"><code>fastvideo.layers.quantization.fp8_config.FP8Config.get_supported_act_dtypes</code></h3>
<pre class="doc-signature"><code>get_supported_act_dtypes() -&gt; list&#91;torch.dtype&#93;</code></pre>

<details><summary>Source</summary><pre><code>def get_supported_act_dtypes(self) -&gt; list&#91;torch.dtype&#93;:
    return &#91;torch.bfloat16, torch.float16&#93;</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8Config.get_min_capability"><code>fastvideo.layers.quantization.fp8_config.FP8Config.get_min_capability</code></h3>
<pre class="doc-signature"><code>get_min_capability() -&gt; int</code></pre>

<details><summary>Source</summary><pre><code>@classmethod
def get_min_capability(cls) -&gt; int:
    return 89</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8Config.get_config_filenames"><code>fastvideo.layers.quantization.fp8_config.FP8Config.get_config_filenames</code></h3>
<pre class="doc-signature"><code>get_config_filenames() -&gt; list&#91;str&#93;</code></pre>

<details><summary>Source</summary><pre><code>@staticmethod
def get_config_filenames() -&gt; list&#91;str&#93;:
    return &#91;&#93;</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8Config.from_config"><code>fastvideo.layers.quantization.fp8_config.FP8Config.from_config</code></h3>
<pre class="doc-signature"><code>from_config(config: dict&#91;str, Any&#93;) -&gt; FP8Config</code></pre>

<details><summary>Source</summary><pre><code>@classmethod
def from_config(cls, config: dict&#91;str, Any&#93;) -&gt; FP8Config:
    return cls(granularity=config.get(&quot;granularity&quot;, &quot;tensor&quot;))</code></pre></details>
<h3 id="fastvideo.layers.quantization.fp8_config.FP8Config.get_quant_method"><code>fastvideo.layers.quantization.fp8_config.FP8Config.get_quant_method</code></h3>
<pre class="doc-signature"><code>get_quant_method(layer: torch.nn.Module, prefix: str)</code></pre>

<details><summary>Source</summary><pre><code>def get_quant_method(self, layer: torch.nn.Module, prefix: str):
    from fastvideo.layers.linear import LinearBase

    if isinstance(layer, LinearBase) and any(s in prefix for s in _FP8_SUFFIXES):
        return FP8QuantizeMethod(granularity=self.granularity)
    return None</code></pre></details>
<h2 id="fastvideo.layers.quantization.fp8_config.convert_model_to_fp8"><code>fastvideo.layers.quantization.fp8_config.convert_model_to_fp8</code></h2>
<pre class="doc-signature"><code>convert_model_to_fp8(model: torch.nn.Module) -&gt; None</code></pre>
<p>Quantize all FP8-tagged linear layers in-place after weights are loaded.</p>
<details><summary>Source</summary><pre><code>def convert_model_to_fp8(model: torch.nn.Module) -&gt; None:
    &quot;&quot;&quot;Quantize all FP8-tagged linear layers in-place after weights are loaded.&quot;&quot;&quot;
    import gc
    from torch.distributed.tensor import DTensor  # type: ignore

    with torch.no_grad():
        for mod in model.modules():
            qm = getattr(mod, &quot;quant_method&quot;, None)
            if not isinstance(qm, FP8QuantizeMethod):
                continue
            weight = getattr(mod, &quot;weight&quot;, None)
            if weight is None:
                continue
            weight_local = weight.to_local() if isinstance(weight, DTensor) else weight  # type: ignore&#91;arg-type&#93;
            if getattr(qm, &quot;granularity&quot;, &quot;tensor&quot;) == &quot;channel&quot;:
                w_absmax = weight_local.detach().abs().amax(dim=1).nan_to_num().float()
                w_scale = (w_absmax / FP8_MAX).clamp(min=FP8_MIN_SCALE)
                w_fp8 = (weight_local / w_scale.to(weight_local.dtype).unsqueeze(1)).clamp(-FP8_MAX,
                                                                                           FP8_MAX).to(FP8_DTYPE)
            else:
                w_absmax = weight_local.detach().abs().amax().nan_to_num().to(torch.float32)
                w_scale = (w_absmax / FP8_MAX).clamp(min=FP8_MIN_SCALE).view(1)
                w_fp8 = (weight_local / w_scale.to(weight_local.dtype)).clamp(-FP8_MAX, FP8_MAX).to(FP8_DTYPE)
            mod.register_buffer(&quot;_fp8_weight&quot;, w_fp8.contiguous(), persistent=False)
            mod.register_buffer(&quot;_fp8_weight_scale&quot;, w_scale.to(torch.float32), persistent=False)
            removed_weight = mod._parameters.pop(&quot;weight&quot;, None)
            if removed_weight is not None:
                removed_weight.grad = None
            del removed_weight, weight, weight_local, w_absmax, w_scale, w_fp8

    gc.collect()
    torch.cuda.empty_cache()</code></pre></details>
</div>
