---
title: flux2vae
---

<div class="python-reference">
<a id="fastvideo.models.vaes.flux2vae-classes"></a>
<a id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2-attributes"></a>
<a id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2-functions"></a>
<a id="fastvideo.models.vaes.flux2vae"></a>

<h2 id="fastvideo.models.vaes.flux2vae.Flux2VAEConfig"><code>fastvideo.models.vaes.flux2vae.Flux2VAEConfig</code></h2>
<pre class="doc-signature"><code>Flux2VAEConfig(arch_config: Flux2VAEArchConfig = Flux2VAEArchConfig(), load_encoder: bool = True, load_decoder: bool = True, tile_sample_min_height: int = 256, tile_sample_min_width: int = 256, tile_sample_min_num_frames: int = 16, tile_sample_stride_height: int = 192, tile_sample_stride_width: int = 192, tile_sample_stride_num_frames: int = 12, blend_num_frames: int = 0, use_tiling: bool = False, use_temporal_tiling: bool = False, use_parallel_tiling: bool = False, use_temporal_scaling_frames: bool = True, *, _resolved_attention_backend: AttentionBackendEnum | None = None)</code></pre>
<p>Configuration for Flux2 VAE model.</p>

<h2 id="fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution"><code>fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution</code></h2>
<pre class="doc-signature"><code>DiagonalGaussianDistribution(parameters: torch.Tensor, deterministic: bool = False)</code></pre>


<h3 id="fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.sample"><code>fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.sample</code></h3>
<pre class="doc-signature"><code>sample(generator: torch.Generator | None = None) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.kl"><code>fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.kl</code></h3>
<pre class="doc-signature"><code>kl(other: Optional&#91;DiagonalGaussianDistribution&#93; = None) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.nll"><code>fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.nll</code></h3>
<pre class="doc-signature"><code>nll(sample: torch.Tensor, dims: tuple&#91;int, ...&#93; = (1, 2, 3)) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.mode"><code>fastvideo.models.vaes.flux2vae.DiagonalGaussianDistribution.mode</code></h3>
<pre class="doc-signature"><code>mode() -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE</code></h2>
<pre class="doc-signature"><code>ParallelTiledVAE(config: VAEConfig, **kwargs)</code></pre>


<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.to"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.to</code></h3>
<pre class="doc-signature"><code>to(device) -&gt; ParallelTiledVAE</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.encode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.encode</code></h3>
<pre class="doc-signature"><code>encode(x: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.decode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.decode</code></h3>
<pre class="doc-signature"><code>decode(z: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.blend_v"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.blend_v</code></h3>
<pre class="doc-signature"><code>blend_v(a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.blend_h"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.blend_h</code></h3>
<pre class="doc-signature"><code>blend_h(a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.blend_t"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.blend_t</code></h3>
<pre class="doc-signature"><code>blend_t(a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.spatial_tiled_encode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.spatial_tiled_encode</code></h3>
<pre class="doc-signature"><code>spatial_tiled_encode(x: torch.Tensor) -&gt; torch.Tensor</code></pre>
<p>Encode a batch of images using a tiled encoder.</p>
<h4>Parameters</h4>
<dl>
<dt><code>x: `torch.Tensor`</code></dt><dd><p>Input batch of videos.</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>torch.Tensor</code></dt><dd><p><code>torch.Tensor</code>:
The latent representation of the encoded videos.</p></dd>
</dl>
<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.parallel_tiled_decode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.parallel_tiled_decode</code></h3>
<pre class="doc-signature"><code>parallel_tiled_decode(z: torch.FloatTensor) -&gt; torch.FloatTensor</code></pre>
<p>Parallel version of tiled_decode that distributes both temporal and spatial computation across GPUs</p>
<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.spatial_tiled_decode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.spatial_tiled_decode</code></h3>
<pre class="doc-signature"><code>spatial_tiled_decode(z: torch.Tensor) -&gt; torch.Tensor</code></pre>
<p>Decode a batch of images using a tiled decoder.</p>
<h4>Parameters</h4>
<dl>
<dt><code>z: `torch.Tensor`</code></dt><dd><p>Input batch of latent vectors.</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>torch.Tensor</code></dt><dd><p><code>torch.Tensor</code>:
The decoded images.</p></dd>
</dl>
<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.tiled_encode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.tiled_encode</code></h3>
<pre class="doc-signature"><code>tiled_encode(x: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.tiled_decode"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.tiled_decode</code></h3>
<pre class="doc-signature"><code>tiled_decode(z: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.enable_tiling"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.enable_tiling</code></h3>
<pre class="doc-signature"><code>enable_tiling(tile_sample_min_height: int | None = None, tile_sample_min_width: int | None = None, tile_sample_min_num_frames: int | None = None, tile_sample_stride_height: int | None = None, tile_sample_stride_width: int | None = None, tile_sample_stride_num_frames: int | None = None, blend_num_frames: int | None = None, use_tiling: bool | None = None, use_temporal_tiling: bool | None = None, use_parallel_tiling: bool | None = None) -&gt; None</code></pre>
<p>Enable tiled VAE decoding. When this option is enabled, the VAE will split the input tensor into tiles to
compute decoding and encoding in several steps. This is useful for saving a large amount of memory and to allow
processing larger images.</p>
<h4>Parameters</h4>
<dl>
<dt><code>tile_sample_min_height: `int`, *optional*</code></dt><dd><p>The minimum height required for a sample to be separated into tiles across the height dimension.</p></dd>
<dt><code>tile_sample_min_width: `int`, *optional*</code></dt><dd><p>The minimum width required for a sample to be separated into tiles across the width dimension.</p></dd>
<dt><code>tile_sample_min_num_frames: `int`, *optional*</code></dt><dd><p>The minimum number of frames required for a sample to be separated into tiles across the frame
dimension.</p></dd>
<dt><code>tile_sample_stride_height: `int`, *optional*</code></dt><dd><p>The minimum amount of overlap between two consecutive vertical tiles. This is to ensure that there are
no tiling artifacts produced across the height dimension.</p></dd>
<dt><code>tile_sample_stride_width: `int`, *optional*</code></dt><dd><p>The stride between two consecutive horizontal tiles. This is to ensure that there are no tiling
artifacts produced across the width dimension.</p></dd>
<dt><code>tile_sample_stride_num_frames: `int`, *optional*</code></dt><dd><p>The stride between two consecutive frame tiles. This is to ensure that there are no tiling artifacts
produced across the frame dimension.</p></dd>
</dl>
<h3 id="fastvideo.models.vaes.flux2vae.ParallelTiledVAE.disable_tiling"><code>fastvideo.models.vaes.flux2vae.ParallelTiledVAE.disable_tiling</code></h3>
<pre class="doc-signature"><code>disable_tiling() -&gt; None</code></pre>
<p>Disable tiled VAE decoding. If <code>enable_tiling</code> was previously enabled, this method will go back to computing
decoding in one step.</p>
<h2 id="fastvideo.models.vaes.flux2vae.Attention"><code>fastvideo.models.vaes.flux2vae.Attention</code></h2>
<pre class="doc-signature"><code>Attention(query_dim: int, heads: int = 8, dim_head: int = 64, dropout: float = 0.0, bias: bool = False, upcast_softmax: bool = False, norm_num_groups: Optional&#91;int&#93; = None, spatial_norm_dim: Optional&#91;int&#93; = None, out_bias: bool = True, eps: float = 1e-05, rescale_output_factor: float = 1.0, residual_connection: bool = False, _from_deprecated_attn_block: bool = False, **_: object)</code></pre>


<h3 id="fastvideo.models.vaes.flux2vae.Attention.set_processor"><code>fastvideo.models.vaes.flux2vae.Attention.set_processor</code></h3>
<pre class="doc-signature"><code>set_processor(processor: AttnProcessor) -&gt; None</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.Attention.get_processor"><code>fastvideo.models.vaes.flux2vae.Attention.get_processor</code></h3>
<pre class="doc-signature"><code>get_processor() -&gt; AttnProcessor</code></pre>

<h3 id="fastvideo.models.vaes.flux2vae.Attention.forward"><code>fastvideo.models.vaes.flux2vae.Attention.forward</code></h3>
<pre class="doc-signature"><code>forward(hidden_states: torch.Tensor, temb: Optional&#91;torch.Tensor&#93; = None) -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.models.vaes.flux2vae.AttnAddedKVProcessor"><code>fastvideo.models.vaes.flux2vae.AttnAddedKVProcessor</code></h2>
<pre class="doc-signature"><code>AttnAddedKVProcessor()</code></pre>

<h2 id="fastvideo.models.vaes.flux2vae.AttnProcessor"><code>fastvideo.models.vaes.flux2vae.AttnProcessor</code></h2>
<pre class="doc-signature"><code>AttnProcessor()</code></pre>

<h2 id="fastvideo.models.vaes.flux2vae.AutoencoderKLOutput"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLOutput</code></h2>
<pre class="doc-signature"><code>AutoencoderKLOutput(latent_dist: DiagonalGaussianDistribution)</code></pre>


<h2 id="fastvideo.models.vaes.flux2vae.Decoder"><code>fastvideo.models.vaes.flux2vae.Decoder</code></h2>
<pre class="doc-signature"><code>Decoder(in_channels: int = 3, out_channels: int = 3, up_block_types: Tuple&#91;str, ...&#93; = (&#x27;UpDecoderBlock2D&#x27;,), block_out_channels: Tuple&#91;int, ...&#93; = (64,), layers_per_block: int = 2, norm_num_groups: int = 32, act_fn: str = &#x27;silu&#x27;, norm_type: str = &#x27;group&#x27;, mid_block_add_attention: bool = True)</code></pre>


<h3 id="fastvideo.models.vaes.flux2vae.Decoder.forward"><code>fastvideo.models.vaes.flux2vae.Decoder.forward</code></h3>
<pre class="doc-signature"><code>forward(sample: torch.Tensor, latent_embeds: Optional&#91;torch.Tensor&#93; = None) -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.models.vaes.flux2vae.DecoderOutput"><code>fastvideo.models.vaes.flux2vae.DecoderOutput</code></h2>
<pre class="doc-signature"><code>DecoderOutput(sample: torch.Tensor, commit_loss: Optional&#91;torch.Tensor&#93; = None)</code></pre>


<h2 id="fastvideo.models.vaes.flux2vae.Encoder"><code>fastvideo.models.vaes.flux2vae.Encoder</code></h2>
<pre class="doc-signature"><code>Encoder(in_channels: int = 3, out_channels: int = 3, down_block_types: Tuple&#91;str, ...&#93; = (&#x27;DownEncoderBlock2D&#x27;,), block_out_channels: Tuple&#91;int, ...&#93; = (64,), layers_per_block: int = 2, norm_num_groups: int = 32, act_fn: str = &#x27;silu&#x27;, double_z: bool = True, mid_block_add_attention: bool = True)</code></pre>


<h3 id="fastvideo.models.vaes.flux2vae.Encoder.forward"><code>fastvideo.models.vaes.flux2vae.Encoder.forward</code></h3>
<pre class="doc-signature"><code>forward(sample: torch.Tensor) -&gt; torch.Tensor</code></pre>

<h2 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2</code></h2>
<pre class="doc-signature"><code>AutoencoderKLFlux2(config: Flux2VAEConfig)</code></pre>
<p>A VAE model with KL loss for encoding images into latents and decoding latent representations into images.</p>
<p>This model inherits from [<code>ParallelTiledVAE</code>] for tiling support and uses standard diffusers
Encoder/Decoder components for Flux2 image generation.</p>

<details><summary>Source</summary><pre><code>class AutoencoderKLFlux2(nn.Module, ParallelTiledVAE):
    r&quot;&quot;&quot;
    A VAE model with KL loss for encoding images into latents and decoding latent representations into images.

    This model inherits from &#91;`ParallelTiledVAE`&#93; for tiling support and uses standard diffusers
    Encoder/Decoder components for Flux2 image generation.
    &quot;&quot;&quot;

    _supports_gradient_checkpointing = True
    _no_split_modules = &#91;&quot;Attention&quot;, &quot;ResnetBlock2D&quot;&#93;

    def __init__(
        self,
        config: Flux2VAEConfig,
    ):
        nn.Module.__init__(self)
        ParallelTiledVAE.__init__(self, config=config)

        self.config = config
        arch_config = config.arch_config

        in_channels: int = arch_config.in_channels
        out_channels: int = arch_config.out_channels
        down_block_types: Tuple&#91;str, ...&#93; = arch_config.down_block_types
        up_block_types: Tuple&#91;str, ...&#93; = arch_config.up_block_types
        block_out_channels: Tuple&#91;int, ...&#93; = arch_config.block_out_channels
        layers_per_block: int = arch_config.layers_per_block
        act_fn: str = arch_config.act_fn
        latent_channels: int = arch_config.latent_channels
        norm_num_groups: int = arch_config.norm_num_groups
        sample_size: int = arch_config.sample_size
        force_upcast: bool = arch_config.force_upcast
        use_quant_conv: bool = arch_config.use_quant_conv
        use_post_quant_conv: bool = arch_config.use_post_quant_conv
        mid_block_add_attention: bool = arch_config.mid_block_add_attention
        batch_norm_eps: float = arch_config.batch_norm_eps
        batch_norm_momentum: float = arch_config.batch_norm_momentum
        patch_size: Tuple&#91;int, int&#93; = arch_config.patch_size

        # pass init params to Encoder
        self.encoder = Encoder(
            in_channels=in_channels,
            out_channels=latent_channels,
            down_block_types=down_block_types,
            block_out_channels=block_out_channels,
            layers_per_block=layers_per_block,
            act_fn=act_fn,
            norm_num_groups=norm_num_groups,
            double_z=True,
            mid_block_add_attention=mid_block_add_attention,
        )

        # pass init params to Decoder
        self.decoder = Decoder(
            in_channels=latent_channels,
            out_channels=out_channels,
            up_block_types=up_block_types,
            block_out_channels=block_out_channels,
            layers_per_block=layers_per_block,
            norm_num_groups=norm_num_groups,
            act_fn=act_fn,
            mid_block_add_attention=mid_block_add_attention,
        )

        self.quant_conv = (nn.Conv2d(2 * latent_channels, 2 * latent_channels, 1) if use_quant_conv else None)
        self.post_quant_conv = (nn.Conv2d(latent_channels, latent_channels, 1) if use_post_quant_conv else None)

        self.bn = nn.BatchNorm2d(
            math.prod(patch_size) * latent_channels,
            eps=batch_norm_eps,
            momentum=batch_norm_momentum,
            affine=False,
            track_running_stats=True,
        )

        self.use_slicing = False
        self.use_tiling = False

        # only relevant if vae tiling is enabled
        self.tile_sample_min_size = sample_size
        sample_size_val = (sample_size&#91;0&#93; if isinstance(sample_size, (list, tuple)) else sample_size)
        self.tile_latent_min_size = int(sample_size_val / (2**(len(block_out_channels) - 1)))
        self.tile_overlap_factor = 0.25

    @property
    # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.attn_processors
    def attn_processors(self) -&gt; Dict&#91;str, AttentionProcessor&#93;:
        r&quot;&quot;&quot;
        Returns:
            `dict` of attention processors: A dictionary containing all attention processors used in the model with
            indexed by its weight name.
        &quot;&quot;&quot;
        # set recursively
        processors = &#123;&#125;

        def fn_recursive_add_processors(
            name: str,
            module: torch.nn.Module,
            processors: Dict&#91;str, AttentionProcessor&#93;,
        ):
            if hasattr(module, &quot;get_processor&quot;):
                processors&#91;f&quot;&#123;name&#125;.processor&quot;&#93; = module.get_processor()

            for sub_name, child in module.named_children():
                fn_recursive_add_processors(f&quot;&#123;name&#125;.&#123;sub_name&#125;&quot;, child, processors)

            return processors

        for name, module in self.named_children():
            fn_recursive_add_processors(name, module, processors)

        return processors

    # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_attn_processor
    def set_attn_processor(self, processor: Union&#91;AttentionProcessor, Dict&#91;str, AttentionProcessor&#93;&#93;):
        r&quot;&quot;&quot;
        Sets the attention processor to use to compute attention.

        Parameters:
            processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
                The instantiated processor class or a dictionary of processor classes that will be set as the processor
                for **all** `Attention` layers.

                If `processor` is a dict, the key needs to define the path to the corresponding cross attention
                processor. This is strongly recommended when setting trainable attention processors.

        &quot;&quot;&quot;
        count = len(self.attn_processors.keys())

        if isinstance(processor, dict) and len(processor) != count:
            raise ValueError(
                f&quot;A dict of processors was passed, but the number of processors &#123;len(processor)&#125; does not match the&quot;
                f&quot; number of attention layers: &#123;count&#125;. Please make sure to pass &#123;count&#125; processor classes.&quot;)

        def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
            if hasattr(module, &quot;set_processor&quot;):
                if not isinstance(processor, dict):
                    module.set_processor(processor)
                else:
                    module.set_processor(processor.pop(f&quot;&#123;name&#125;.processor&quot;))

            for sub_name, child in module.named_children():
                fn_recursive_attn_processor(f&quot;&#123;name&#125;.&#123;sub_name&#125;&quot;, child, processor)

        for name, module in self.named_children():
            fn_recursive_attn_processor(name, module, processor)

    # Copied from diffusers.models.unets.unet_2d_condition.UNet2DConditionModel.set_default_attn_processor
    def set_default_attn_processor(self):
        &quot;&quot;&quot;
        Disables custom attention processors and sets the default attention implementation.
        &quot;&quot;&quot;
        if all(proc.__class__ in ADDED_KV_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
            processor = AttnAddedKVProcessor()
        elif all(proc.__class__ in CROSS_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
            processor = AttnProcessor()
        else:
            raise ValueError(
                f&quot;Cannot call `set_default_attn_processor` when attention processors are of type &#123;next(iter(self.attn_processors.values()))&#125;&quot;
            )

        self.set_attn_processor(processor)

    def _encode(self, x: torch.Tensor) -&gt; torch.Tensor:
        batch_size, num_channels, height, width = x.shape

        if self.use_tiling and (width &gt; self.tile_sample_min_size or height &gt; self.tile_sample_min_size):
            return self._tiled_encode(x)

        enc = self.encoder(x)
        if self.quant_conv is not None:
            enc = self.quant_conv(enc)

        return enc

    def encode(self,
               x: torch.Tensor,
               return_dict: bool = True) -&gt; Union&#91;AutoencoderKLOutput, Tuple&#91;DiagonalGaussianDistribution&#93;&#93;:
        &quot;&quot;&quot;
        Encode a batch of images into latents.

        Args:
            x (`torch.Tensor`): Input batch of images.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether to return a &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; instead of a plain tuple.

        Returns:
                The latent representations of the encoded images. If `return_dict` is True, a
                &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; is returned, otherwise a plain `tuple` is returned.
        &quot;&quot;&quot;

        if x.ndim == 5:
            assert x.shape&#91;2&#93; == 1
            x = x.squeeze(2)

        if self.use_slicing and x.shape&#91;0&#93; &gt; 1:
            encoded_slices = &#91;self._encode(x_slice) for x_slice in x.split(1)&#93;
            h = torch.cat(encoded_slices)
        else:
            h = self._encode(x)

        posterior = DiagonalGaussianDistribution(h)
        if not return_dict:
            return (posterior, )

        return AutoencoderKLOutput(latent_dist=posterior)

    def _decode(self, z: torch.Tensor, return_dict: bool = True) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;:
        if self.use_tiling and (z.shape&#91;-1&#93; &gt; self.tile_latent_min_size or z.shape&#91;-2&#93; &gt; self.tile_latent_min_size):
            return self.tiled_decode(z, return_dict=return_dict)

        if self.post_quant_conv is not None:
            z = self.post_quant_conv(z)

        dec = self.decoder(z)

        if not return_dict:
            return (dec, )

        return DecoderOutput(sample=dec)

    def decode(self,
               z: torch.FloatTensor,
               return_dict: bool = True,
               generator=None) -&gt; Union&#91;DecoderOutput, torch.FloatTensor&#93;:
        &quot;&quot;&quot;
        Decode a batch of images.

        Args:
            z (`torch.Tensor`): Input batch of latent vectors.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether to return a &#91;`~models.vae.DecoderOutput`&#93; instead of a plain tuple.

        Returns:
            &#91;`~models.vae.DecoderOutput`&#93; or `tuple`:
                If return_dict is True, a &#91;`~models.vae.DecoderOutput`&#93; is returned, otherwise a plain `tuple` is
                returned.

        &quot;&quot;&quot;
        if self.use_slicing and z.shape&#91;0&#93; &gt; 1:
            decoded_slices = &#91;self._decode(z_slice).sample for z_slice in z.split(1)&#93;
            decoded = torch.cat(decoded_slices)
        else:
            decoded = self._decode(z).sample

        if not return_dict:
            return (decoded, )

        return DecoderOutput(sample=decoded)

    def blend_v(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor:
        blend_extent = min(a.shape&#91;2&#93;, b.shape&#91;2&#93;, blend_extent)
        for y in range(blend_extent):
            b&#91;:, :, y, :&#93; = a&#91;:, :, -blend_extent + y, :&#93; * (1 - y / blend_extent) + b&#91;:, :, y, :&#93; * (y / blend_extent)
        return b

    def blend_h(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor:
        blend_extent = min(a.shape&#91;3&#93;, b.shape&#91;3&#93;, blend_extent)
        for x in range(blend_extent):
            b&#91;:, :, :, x&#93; = a&#91;:, :, :, -blend_extent + x&#93; * (1 - x / blend_extent) + b&#91;:, :, :, x&#93; * (x / blend_extent)
        return b

    def _tiled_encode(self, x: torch.Tensor) -&gt; torch.Tensor:
        r&quot;&quot;&quot;Encode a batch of images using a tiled encoder.

        When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several
        steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encoding is
        different from non-tiled encoding because each tile uses a different encoder. To avoid tiling artifacts, the
        tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the
        output, but they should be much less noticeable.

        Args:
            x (`torch.Tensor`): Input batch of images.

        Returns:
            `torch.Tensor`:
                The latent representation of the encoded videos.
        &quot;&quot;&quot;

        overlap_size = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor))
        blend_extent = int(self.tile_latent_min_size * self.tile_overlap_factor)
        row_limit = self.tile_latent_min_size - blend_extent

        # Split the image into 512x512 tiles and encode them separately.
        rows = &#91;&#93;
        for i in range(0, x.shape&#91;2&#93;, overlap_size):
            row = &#91;&#93;
            for j in range(0, x.shape&#91;3&#93;, overlap_size):
                tile = x&#91;
                    :,
                    :,
                    i:i + self.tile_sample_min_size,
                    j:j + self.tile_sample_min_size,
                &#93;
                tile = self.encoder(tile)
                if self.quant_conv is not None:
                    tile = self.quant_conv(tile)
                row.append(tile)
            rows.append(row)
        result_rows = &#91;&#93;
        for i, row in enumerate(rows):
            result_row = &#91;&#93;
            for j, tile in enumerate(row):
                # blend the above tile and the left tile
                # to the current tile and add the current tile to the result row
                if i &gt; 0:
                    tile = self.blend_v(rows&#91;i - 1&#93;&#91;j&#93;, tile, blend_extent)
                if j &gt; 0:
                    tile = self.blend_h(row&#91;j - 1&#93;, tile, blend_extent)
                result_row.append(tile&#91;:, :, :row_limit, :row_limit&#93;)
            result_rows.append(torch.cat(result_row, dim=3))

        enc = torch.cat(result_rows, dim=2)
        return enc

    def tiled_encode(self, x: torch.Tensor, return_dict: bool = True) -&gt; AutoencoderKLOutput:
        r&quot;&quot;&quot;Encode a batch of images using a tiled encoder.

        When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several
        steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encoding is
        different from non-tiled encoding because each tile uses a different encoder. To avoid tiling artifacts, the
        tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the
        output, but they should be much less noticeable.

        Args:
            x (`torch.Tensor`): Input batch of images.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; instead of a plain tuple.

        Returns:
            &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; or `tuple`:
                If return_dict is True, a &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; is returned, otherwise a plain
                `tuple` is returned.
        &quot;&quot;&quot;
        deprecation_message = (
            &quot;The tiled_encode implementation supporting the `return_dict` parameter is deprecated. In the future, the &quot;
            &quot;implementation of this method will be replaced with that of `_tiled_encode` and you will no longer be able &quot;
            &quot;to pass `return_dict`. You will also have to create a `DiagonalGaussianDistribution()` from the returned value.&quot;
        )

        overlap_size = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor))
        blend_extent = int(self.tile_latent_min_size * self.tile_overlap_factor)
        row_limit = self.tile_latent_min_size - blend_extent

        # Split the image into 512x512 tiles and encode them separately.
        rows = &#91;&#93;
        for i in range(0, x.shape&#91;2&#93;, overlap_size):
            row = &#91;&#93;
            for j in range(0, x.shape&#91;3&#93;, overlap_size):
                tile = x&#91;
                    :,
                    :,
                    i:i + self.tile_sample_min_size,
                    j:j + self.tile_sample_min_size,
                &#93;
                tile = self.encoder(tile)
                if self.quant_conv is not None:
                    tile = self.quant_conv(tile)
                row.append(tile)
            rows.append(row)
        result_rows = &#91;&#93;
        for i, row in enumerate(rows):
            result_row = &#91;&#93;
            for j, tile in enumerate(row):
                # blend the above tile and the left tile
                # to the current tile and add the current tile to the result row
                if i &gt; 0:
                    tile = self.blend_v(rows&#91;i - 1&#93;&#91;j&#93;, tile, blend_extent)
                if j &gt; 0:
                    tile = self.blend_h(row&#91;j - 1&#93;, tile, blend_extent)
                result_row.append(tile&#91;:, :, :row_limit, :row_limit&#93;)
            result_rows.append(torch.cat(result_row, dim=3))

        moments = torch.cat(result_rows, dim=2)
        posterior = DiagonalGaussianDistribution(moments)

        if not return_dict:
            return (posterior, )

        return AutoencoderKLOutput(latent_dist=posterior)

    def tiled_decode(self, z: torch.Tensor, return_dict: bool = True) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;:
        r&quot;&quot;&quot;
        Decode a batch of images using a tiled decoder.

        Args:
            z (`torch.Tensor`): Input batch of latent vectors.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a &#91;`~models.vae.DecoderOutput`&#93; instead of a plain tuple.

        Returns:
            &#91;`~models.vae.DecoderOutput`&#93; or `tuple`:
                If return_dict is True, a &#91;`~models.vae.DecoderOutput`&#93; is returned, otherwise a plain `tuple` is
                returned.
        &quot;&quot;&quot;
        overlap_size = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor))
        blend_extent = int(self.tile_sample_min_size * self.tile_overlap_factor)
        row_limit = self.tile_sample_min_size - blend_extent

        # Split z into overlapping 64x64 tiles and decode them separately.
        # The tiles have an overlap to avoid seams between tiles.
        rows = &#91;&#93;
        for i in range(0, z.shape&#91;2&#93;, overlap_size):
            row = &#91;&#93;
            for j in range(0, z.shape&#91;3&#93;, overlap_size):
                tile = z&#91;
                    :,
                    :,
                    i:i + self.tile_latent_min_size,
                    j:j + self.tile_latent_min_size,
                &#93;
                if self.post_quant_conv is not None:
                    tile = self.post_quant_conv(tile)
                decoded = self.decoder(tile)
                row.append(decoded)
            rows.append(row)
        result_rows = &#91;&#93;
        for i, row in enumerate(rows):
            result_row = &#91;&#93;
            for j, tile in enumerate(row):
                # blend the above tile and the left tile
                # to the current tile and add the current tile to the result row
                if i &gt; 0:
                    tile = self.blend_v(rows&#91;i - 1&#93;&#91;j&#93;, tile, blend_extent)
                if j &gt; 0:
                    tile = self.blend_h(row&#91;j - 1&#93;, tile, blend_extent)
                result_row.append(tile&#91;:, :, :row_limit, :row_limit&#93;)
            result_rows.append(torch.cat(result_row, dim=3))

        dec = torch.cat(result_rows, dim=2)
        if not return_dict:
            return (dec, )

        return DecoderOutput(sample=dec)

    def forward(
        self,
        sample: torch.Tensor,
        sample_posterior: bool = False,
        return_dict: bool = True,
        generator: Optional&#91;torch.Generator&#93; = None,
    ) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;:
        r&quot;&quot;&quot;
        Args:
            sample (`torch.Tensor`): Input sample.
            sample_posterior (`bool`, *optional*, defaults to `False`):
                Whether to sample from the posterior.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a &#91;`DecoderOutput`&#93; instead of a plain tuple.
        &quot;&quot;&quot;
        x = sample
        posterior = self.encode(x).latent_dist
        if sample_posterior:
            z = posterior.sample(generator=generator)
        else:
            z = posterior.mode()
        dec = self.decode(z).sample

        if not return_dict:
            return (dec, )

        return DecoderOutput(sample=dec)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.attn_processors"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.attn_processors</code></h3>
<pre class="doc-signature"><code>attn_processors: Dict&#91;str, AttentionProcessor&#93;</code></pre>
<h4>Returns</h4>
<dl>
<dt><code>Dict[str, AttentionProcessor]</code></dt><dd><p><code>dict</code> of attention processors: A dictionary containing all attention processors used in the model with</p></dd>
<dt><code>Dict[str, AttentionProcessor]</code></dt><dd><p>indexed by its weight name.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def attn_processors(self) -&gt; Dict&#91;str, AttentionProcessor&#93;:
    r&quot;&quot;&quot;
    Returns:
        `dict` of attention processors: A dictionary containing all attention processors used in the model with
        indexed by its weight name.
    &quot;&quot;&quot;
    # set recursively
    processors = &#123;&#125;

    def fn_recursive_add_processors(
        name: str,
        module: torch.nn.Module,
        processors: Dict&#91;str, AttentionProcessor&#93;,
    ):
        if hasattr(module, &quot;get_processor&quot;):
            processors&#91;f&quot;&#123;name&#125;.processor&quot;&#93; = module.get_processor()

        for sub_name, child in module.named_children():
            fn_recursive_add_processors(f&quot;&#123;name&#125;.&#123;sub_name&#125;&quot;, child, processors)

        return processors

    for name, module in self.named_children():
        fn_recursive_add_processors(name, module, processors)

    return processors</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.set_attn_processor"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.set_attn_processor</code></h3>
<pre class="doc-signature"><code>set_attn_processor(processor: Union&#91;AttentionProcessor, Dict&#91;str, AttentionProcessor&#93;&#93;)</code></pre>
<p>Sets the attention processor to use to compute attention.</p>
<h4>Parameters</h4>
<dl>
<dt><code>processor: `dict` of `AttentionProcessor` or only `AttentionProcessor`</code></dt><dd><p>The instantiated processor class or a dictionary of processor classes that will be set as the processor
for <strong>all</strong> <code>Attention</code> layers.</p>
<p>If <code>processor</code> is a dict, the key needs to define the path to the corresponding cross attention
processor. This is strongly recommended when setting trainable attention processors.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def set_attn_processor(self, processor: Union&#91;AttentionProcessor, Dict&#91;str, AttentionProcessor&#93;&#93;):
    r&quot;&quot;&quot;
    Sets the attention processor to use to compute attention.

    Parameters:
        processor (`dict` of `AttentionProcessor` or only `AttentionProcessor`):
            The instantiated processor class or a dictionary of processor classes that will be set as the processor
            for **all** `Attention` layers.

            If `processor` is a dict, the key needs to define the path to the corresponding cross attention
            processor. This is strongly recommended when setting trainable attention processors.

    &quot;&quot;&quot;
    count = len(self.attn_processors.keys())

    if isinstance(processor, dict) and len(processor) != count:
        raise ValueError(
            f&quot;A dict of processors was passed, but the number of processors &#123;len(processor)&#125; does not match the&quot;
            f&quot; number of attention layers: &#123;count&#125;. Please make sure to pass &#123;count&#125; processor classes.&quot;)

    def fn_recursive_attn_processor(name: str, module: torch.nn.Module, processor):
        if hasattr(module, &quot;set_processor&quot;):
            if not isinstance(processor, dict):
                module.set_processor(processor)
            else:
                module.set_processor(processor.pop(f&quot;&#123;name&#125;.processor&quot;))

        for sub_name, child in module.named_children():
            fn_recursive_attn_processor(f&quot;&#123;name&#125;.&#123;sub_name&#125;&quot;, child, processor)

    for name, module in self.named_children():
        fn_recursive_attn_processor(name, module, processor)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.set_default_attn_processor"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.set_default_attn_processor</code></h3>
<pre class="doc-signature"><code>set_default_attn_processor()</code></pre>
<p>Disables custom attention processors and sets the default attention implementation.</p>
<details><summary>Source</summary><pre><code>def set_default_attn_processor(self):
    &quot;&quot;&quot;
    Disables custom attention processors and sets the default attention implementation.
    &quot;&quot;&quot;
    if all(proc.__class__ in ADDED_KV_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
        processor = AttnAddedKVProcessor()
    elif all(proc.__class__ in CROSS_ATTENTION_PROCESSORS for proc in self.attn_processors.values()):
        processor = AttnProcessor()
    else:
        raise ValueError(
            f&quot;Cannot call `set_default_attn_processor` when attention processors are of type &#123;next(iter(self.attn_processors.values()))&#125;&quot;
        )

    self.set_attn_processor(processor)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.encode"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.encode</code></h3>
<pre class="doc-signature"><code>encode(x: torch.Tensor, return_dict: bool = True) -&gt; Union&#91;AutoencoderKLOutput, Tuple&#91;DiagonalGaussianDistribution&#93;&#93;</code></pre>
<p>Encode a batch of images into latents.</p>
<h4>Parameters</h4>
<dl>
<dt><code>x: `torch.Tensor`</code></dt><dd><p>Input batch of images.</p></dd>
<dt><code>return_dict: `bool`, *optional*, defaults to `True`</code></dt><dd><p>Whether to return a [<code>~models.autoencoder_kl.AutoencoderKLOutput</code>] instead of a plain tuple.</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>Union[AutoencoderKLOutput, Tuple[DiagonalGaussianDistribution]]</code></dt><dd><p>The latent representations of the encoded images. If <code>return_dict</code> is True, a</p></dd>
<dt><code>Union[AutoencoderKLOutput, Tuple[DiagonalGaussianDistribution]]</code></dt><dd><p>[<code>~models.autoencoder_kl.AutoencoderKLOutput</code>] is returned, otherwise a plain <code>tuple</code> is returned.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def encode(self,
           x: torch.Tensor,
           return_dict: bool = True) -&gt; Union&#91;AutoencoderKLOutput, Tuple&#91;DiagonalGaussianDistribution&#93;&#93;:
    &quot;&quot;&quot;
    Encode a batch of images into latents.

    Args:
        x (`torch.Tensor`): Input batch of images.
        return_dict (`bool`, *optional*, defaults to `True`):
            Whether to return a &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; instead of a plain tuple.

    Returns:
            The latent representations of the encoded images. If `return_dict` is True, a
            &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; is returned, otherwise a plain `tuple` is returned.
    &quot;&quot;&quot;

    if x.ndim == 5:
        assert x.shape&#91;2&#93; == 1
        x = x.squeeze(2)

    if self.use_slicing and x.shape&#91;0&#93; &gt; 1:
        encoded_slices = &#91;self._encode(x_slice) for x_slice in x.split(1)&#93;
        h = torch.cat(encoded_slices)
    else:
        h = self._encode(x)

    posterior = DiagonalGaussianDistribution(h)
    if not return_dict:
        return (posterior, )

    return AutoencoderKLOutput(latent_dist=posterior)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.decode"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.decode</code></h3>
<pre class="doc-signature"><code>decode(z: torch.FloatTensor, return_dict: bool = True, generator = None) -&gt; Union&#91;DecoderOutput, torch.FloatTensor&#93;</code></pre>
<p>Decode a batch of images.</p>
<h4>Parameters</h4>
<dl>
<dt><code>z: `torch.Tensor`</code></dt><dd><p>Input batch of latent vectors.</p></dd>
<dt><code>return_dict: `bool`, *optional*, defaults to `True`</code></dt><dd><p>Whether to return a [<code>~models.vae.DecoderOutput</code>] instead of a plain tuple.</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>Union[DecoderOutput, torch.FloatTensor]</code></dt><dd><p>[<code>~models.vae.DecoderOutput</code>] or <code>tuple</code>:
If return_dict is True, a [<code>~models.vae.DecoderOutput</code>] is returned, otherwise a plain <code>tuple</code> is
returned.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def decode(self,
           z: torch.FloatTensor,
           return_dict: bool = True,
           generator=None) -&gt; Union&#91;DecoderOutput, torch.FloatTensor&#93;:
    &quot;&quot;&quot;
    Decode a batch of images.

    Args:
        z (`torch.Tensor`): Input batch of latent vectors.
        return_dict (`bool`, *optional*, defaults to `True`):
            Whether to return a &#91;`~models.vae.DecoderOutput`&#93; instead of a plain tuple.

    Returns:
        &#91;`~models.vae.DecoderOutput`&#93; or `tuple`:
            If return_dict is True, a &#91;`~models.vae.DecoderOutput`&#93; is returned, otherwise a plain `tuple` is
            returned.

    &quot;&quot;&quot;
    if self.use_slicing and z.shape&#91;0&#93; &gt; 1:
        decoded_slices = &#91;self._decode(z_slice).sample for z_slice in z.split(1)&#93;
        decoded = torch.cat(decoded_slices)
    else:
        decoded = self._decode(z).sample

    if not return_dict:
        return (decoded, )

    return DecoderOutput(sample=decoded)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.blend_v"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.blend_v</code></h3>
<pre class="doc-signature"><code>blend_v(a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def blend_v(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor:
    blend_extent = min(a.shape&#91;2&#93;, b.shape&#91;2&#93;, blend_extent)
    for y in range(blend_extent):
        b&#91;:, :, y, :&#93; = a&#91;:, :, -blend_extent + y, :&#93; * (1 - y / blend_extent) + b&#91;:, :, y, :&#93; * (y / blend_extent)
    return b</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.blend_h"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.blend_h</code></h3>
<pre class="doc-signature"><code>blend_h(a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor</code></pre>

<details><summary>Source</summary><pre><code>def blend_h(self, a: torch.Tensor, b: torch.Tensor, blend_extent: int) -&gt; torch.Tensor:
    blend_extent = min(a.shape&#91;3&#93;, b.shape&#91;3&#93;, blend_extent)
    for x in range(blend_extent):
        b&#91;:, :, :, x&#93; = a&#91;:, :, :, -blend_extent + x&#93; * (1 - x / blend_extent) + b&#91;:, :, :, x&#93; * (x / blend_extent)
    return b</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.tiled_encode"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.tiled_encode</code></h3>
<pre class="doc-signature"><code>tiled_encode(x: torch.Tensor, return_dict: bool = True) -&gt; AutoencoderKLOutput</code></pre>
<p>Encode a batch of images using a tiled encoder.</p>
<p>When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several
steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encoding is
different from non-tiled encoding because each tile uses a different encoder. To avoid tiling artifacts, the
tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the
output, but they should be much less noticeable.</p>
<h4>Parameters</h4>
<dl>
<dt><code>x: `torch.Tensor`</code></dt><dd><p>Input batch of images.</p></dd>
<dt><code>return_dict: `bool`, *optional*, defaults to `True`</code></dt><dd><p>Whether or not to return a [<code>~models.autoencoder_kl.AutoencoderKLOutput</code>] instead of a plain tuple.</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>AutoencoderKLOutput</code></dt><dd><p>[<code>~models.autoencoder_kl.AutoencoderKLOutput</code>] or <code>tuple</code>:
If return_dict is True, a [<code>~models.autoencoder_kl.AutoencoderKLOutput</code>] is returned, otherwise a plain
<code>tuple</code> is returned.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def tiled_encode(self, x: torch.Tensor, return_dict: bool = True) -&gt; AutoencoderKLOutput:
    r&quot;&quot;&quot;Encode a batch of images using a tiled encoder.

    When this option is enabled, the VAE will split the input tensor into tiles to compute encoding in several
    steps. This is useful to keep memory use constant regardless of image size. The end result of tiled encoding is
    different from non-tiled encoding because each tile uses a different encoder. To avoid tiling artifacts, the
    tiles overlap and are blended together to form a smooth output. You may still see tile-sized changes in the
    output, but they should be much less noticeable.

    Args:
        x (`torch.Tensor`): Input batch of images.
        return_dict (`bool`, *optional*, defaults to `True`):
            Whether or not to return a &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; instead of a plain tuple.

    Returns:
        &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; or `tuple`:
            If return_dict is True, a &#91;`~models.autoencoder_kl.AutoencoderKLOutput`&#93; is returned, otherwise a plain
            `tuple` is returned.
    &quot;&quot;&quot;
    deprecation_message = (
        &quot;The tiled_encode implementation supporting the `return_dict` parameter is deprecated. In the future, the &quot;
        &quot;implementation of this method will be replaced with that of `_tiled_encode` and you will no longer be able &quot;
        &quot;to pass `return_dict`. You will also have to create a `DiagonalGaussianDistribution()` from the returned value.&quot;
    )

    overlap_size = int(self.tile_sample_min_size * (1 - self.tile_overlap_factor))
    blend_extent = int(self.tile_latent_min_size * self.tile_overlap_factor)
    row_limit = self.tile_latent_min_size - blend_extent

    # Split the image into 512x512 tiles and encode them separately.
    rows = &#91;&#93;
    for i in range(0, x.shape&#91;2&#93;, overlap_size):
        row = &#91;&#93;
        for j in range(0, x.shape&#91;3&#93;, overlap_size):
            tile = x&#91;
                :,
                :,
                i:i + self.tile_sample_min_size,
                j:j + self.tile_sample_min_size,
            &#93;
            tile = self.encoder(tile)
            if self.quant_conv is not None:
                tile = self.quant_conv(tile)
            row.append(tile)
        rows.append(row)
    result_rows = &#91;&#93;
    for i, row in enumerate(rows):
        result_row = &#91;&#93;
        for j, tile in enumerate(row):
            # blend the above tile and the left tile
            # to the current tile and add the current tile to the result row
            if i &gt; 0:
                tile = self.blend_v(rows&#91;i - 1&#93;&#91;j&#93;, tile, blend_extent)
            if j &gt; 0:
                tile = self.blend_h(row&#91;j - 1&#93;, tile, blend_extent)
            result_row.append(tile&#91;:, :, :row_limit, :row_limit&#93;)
        result_rows.append(torch.cat(result_row, dim=3))

    moments = torch.cat(result_rows, dim=2)
    posterior = DiagonalGaussianDistribution(moments)

    if not return_dict:
        return (posterior, )

    return AutoencoderKLOutput(latent_dist=posterior)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.tiled_decode"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.tiled_decode</code></h3>
<pre class="doc-signature"><code>tiled_decode(z: torch.Tensor, return_dict: bool = True) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;</code></pre>
<p>Decode a batch of images using a tiled decoder.</p>
<h4>Parameters</h4>
<dl>
<dt><code>z: `torch.Tensor`</code></dt><dd><p>Input batch of latent vectors.</p></dd>
<dt><code>return_dict: `bool`, *optional*, defaults to `True`</code></dt><dd><p>Whether or not to return a [<code>~models.vae.DecoderOutput</code>] instead of a plain tuple.</p></dd>
</dl>
<h4>Returns</h4>
<dl>
<dt><code>Union[DecoderOutput, torch.Tensor]</code></dt><dd><p>[<code>~models.vae.DecoderOutput</code>] or <code>tuple</code>:
If return_dict is True, a [<code>~models.vae.DecoderOutput</code>] is returned, otherwise a plain <code>tuple</code> is
returned.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def tiled_decode(self, z: torch.Tensor, return_dict: bool = True) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;:
    r&quot;&quot;&quot;
    Decode a batch of images using a tiled decoder.

    Args:
        z (`torch.Tensor`): Input batch of latent vectors.
        return_dict (`bool`, *optional*, defaults to `True`):
            Whether or not to return a &#91;`~models.vae.DecoderOutput`&#93; instead of a plain tuple.

    Returns:
        &#91;`~models.vae.DecoderOutput`&#93; or `tuple`:
            If return_dict is True, a &#91;`~models.vae.DecoderOutput`&#93; is returned, otherwise a plain `tuple` is
            returned.
    &quot;&quot;&quot;
    overlap_size = int(self.tile_latent_min_size * (1 - self.tile_overlap_factor))
    blend_extent = int(self.tile_sample_min_size * self.tile_overlap_factor)
    row_limit = self.tile_sample_min_size - blend_extent

    # Split z into overlapping 64x64 tiles and decode them separately.
    # The tiles have an overlap to avoid seams between tiles.
    rows = &#91;&#93;
    for i in range(0, z.shape&#91;2&#93;, overlap_size):
        row = &#91;&#93;
        for j in range(0, z.shape&#91;3&#93;, overlap_size):
            tile = z&#91;
                :,
                :,
                i:i + self.tile_latent_min_size,
                j:j + self.tile_latent_min_size,
            &#93;
            if self.post_quant_conv is not None:
                tile = self.post_quant_conv(tile)
            decoded = self.decoder(tile)
            row.append(decoded)
        rows.append(row)
    result_rows = &#91;&#93;
    for i, row in enumerate(rows):
        result_row = &#91;&#93;
        for j, tile in enumerate(row):
            # blend the above tile and the left tile
            # to the current tile and add the current tile to the result row
            if i &gt; 0:
                tile = self.blend_v(rows&#91;i - 1&#93;&#91;j&#93;, tile, blend_extent)
            if j &gt; 0:
                tile = self.blend_h(row&#91;j - 1&#93;, tile, blend_extent)
            result_row.append(tile&#91;:, :, :row_limit, :row_limit&#93;)
        result_rows.append(torch.cat(result_row, dim=3))

    dec = torch.cat(result_rows, dim=2)
    if not return_dict:
        return (dec, )

    return DecoderOutput(sample=dec)</code></pre></details>
<h3 id="fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.forward"><code>fastvideo.models.vaes.flux2vae.AutoencoderKLFlux2.forward</code></h3>
<pre class="doc-signature"><code>forward(sample: torch.Tensor, sample_posterior: bool = False, return_dict: bool = True, generator: Optional&#91;torch.Generator&#93; = None) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;</code></pre>
<h4>Parameters</h4>
<dl>
<dt><code>sample: `torch.Tensor`</code></dt><dd><p>Input sample.</p></dd>
<dt><code>sample_posterior: `bool`, *optional*, defaults to `False`</code></dt><dd><p>Whether to sample from the posterior.</p></dd>
<dt><code>return_dict: `bool`, *optional*, defaults to `True`</code></dt><dd><p>Whether or not to return a [<code>DecoderOutput</code>] instead of a plain tuple.</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def forward(
    self,
    sample: torch.Tensor,
    sample_posterior: bool = False,
    return_dict: bool = True,
    generator: Optional&#91;torch.Generator&#93; = None,
) -&gt; Union&#91;DecoderOutput, torch.Tensor&#93;:
    r&quot;&quot;&quot;
    Args:
        sample (`torch.Tensor`): Input sample.
        sample_posterior (`bool`, *optional*, defaults to `False`):
            Whether to sample from the posterior.
        return_dict (`bool`, *optional*, defaults to `True`):
            Whether or not to return a &#91;`DecoderOutput`&#93; instead of a plain tuple.
    &quot;&quot;&quot;
    x = sample
    posterior = self.encode(x).latent_dist
    if sample_posterior:
        z = posterior.sample(generator=generator)
    else:
        z = posterior.mode()
    dec = self.decode(z).sample

    if not return_dict:
        return (dec, )

    return DecoderOutput(sample=dec)</code></pre></details>
</div>
