Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
"""Tiny AutoEncoder for Hunyuan Video (TAEHV): the decoder half, used for denoising previews.
Adapted from madebyollin's TAEHV, pinned to:
https://github.com/madebyollin/taehv/blob/62f7591f59dfbb4c3c02b7a621d180a9eeaba26c/taehv.py
Kept: `conv`, `Clamp`, `MemBlock`, `TGrow`, the base decoder `nn.Sequential`, the parallel
memory-block application and `decode_video`'s trim. Left out: the encoder, the "super" decoder
(`SuperMemBlock`), the sequential and streaming paths, and `patch_tgrow_layers`, which no supported
checkpoint needs. The `decoder.N` indices are the state-dict contract with the released checkpoints:
do not reorder, insert or remove layers.
Original license:
MIT License
Copyright (c) 2025 Ollin Boer Bohan
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import math
from typing import Final
import torch
import torch.nn as nn
import torch.nn.functional as F
from invokeai.backend.tiny_autoencoder.decoder import PREVIEW_FIXED_ALLOWANCE_BYTES, TinyAutoencoderDecoder
def conv(n_in: int, n_out: int, **kwargs) -> nn.Conv2d:
return nn.Conv2d(n_in, n_out, 3, padding=1, **kwargs)
class Clamp(nn.Module):
def forward(self, x: torch.Tensor) -> torch.Tensor:
return torch.tanh(x / 3) * 3
class MemBlock(nn.Module):
"""Residual block whose conv also sees the previous frame's input (the temporal memory)."""
def __init__(self, n_in: int, n_out: int):
super().__init__()
self.conv = nn.Sequential(
conv(n_in * 2, n_out), nn.ReLU(inplace=True), conv(n_out, n_out), nn.ReLU(inplace=True), conv(n_out, n_out)
)
self.skip = nn.Conv2d(n_in, n_out, 1, bias=False) if n_in != n_out else nn.Identity()
self.act = nn.ReLU(inplace=True)
def forward(self, x: torch.Tensor, past: torch.Tensor) -> torch.Tensor:
return self.act(self.conv(torch.cat([x, past], 1)) + self.skip(x))
class TGrow(nn.Module):
"""Temporal upsampling: a 1x1 conv to `stride` channel groups, reshaped into frames."""
def __init__(self, n_f: int, stride: int):
super().__init__()
self.stride = stride
self.conv = nn.Conv2d(n_f, n_f * stride, 1, bias=False)
def forward(self, x: torch.Tensor) -> torch.Tensor:
_nt, c, h, w = x.shape
return self.conv(x).reshape(-1, c, h, w)
_LAST_STAGE_CHANNELS: Final = 64
"""Width of a TAEHV decoder's last two stages, which run at a half and at the full resolution."""
_LIVE_ACTIVATIONS_PER_FRAME: Final = 2.25
"""Full-resolution activations a TAEHV decode holds at once per output frame, read off the decoder below.
The peak is the convolution after the last `TGrow`: its input, every output frame at full resolution, and its output
are live together, two per output frame. The last memory block's `past` is still referenced: half the output frames
plus one frame of padding, at half resolution, at most a quarter more per output frame. Earlier stages hold less.
"""
_CONV_WORKSPACE_ACTIVATIONS_PER_FRAME: Final = 2
"""The convolutions' workspace on top, in activation-sized buffers per output frame. Measured with cuDNN on an RTX 4090
(fp16 and bf16, single latents and two-frame windows): one latent decoded at 1024x1024 grew `max_memory_allocated` by
2145 MiB for taew2_1, 536 MiB for taew2_2 and 266 MiB for taeltx2_3, 16.75, 16.76 and 33.3 activations of their
full-resolution stage. Their structure holds 8.75, 8.75 and 17.25 (four, four and eight output frames): two more per
output frame, and a two-frame window takes twice that. It depends on the algorithm the backend picks and is unmeasured
on MIOpen, MPS and XPU; there, as everywhere at the default settings, the 3 GB `device_working_mem_gb` floor still
covers the whole bound.
"""
class TaehvDecoder(TinyAutoencoderDecoder):
"""The base TAEHV decoder for one latent space.
Called as a module, it decodes `(B, C, h, w)` latents as single-frame videos and returns one RGB
frame each; `decode_window` returns the last frame of a window of latent frames. `decode_video`
decodes `(N, T, C, h, w)` latents as upstream does.
"""
def __init__(
self,
latent_channels: int,
patch_size: int = 1,
time_upscale: tuple[bool, bool, bool] = (False, True, True),
space_upscale: tuple[bool, bool, bool] = (True, True, True),
):
super().__init__()
n_f = (256, 128, 64, 64)
t = [2 if up else 1 for up in time_upscale]
s = [2 if up else 1 for up in space_upscale]
self.decoder = nn.Sequential(
Clamp(), conv(latent_channels, n_f[0]), nn.ReLU(inplace=True),
MemBlock(n_f[0], n_f[0]), MemBlock(n_f[0], n_f[0]), MemBlock(n_f[0], n_f[0]), nn.Upsample(scale_factor=s[0]), TGrow(n_f[0], t[0]), conv(n_f[0], n_f[1], bias=False),
MemBlock(n_f[1], n_f[1]), MemBlock(n_f[1], n_f[1]), MemBlock(n_f[1], n_f[1]), nn.Upsample(scale_factor=s[1]), TGrow(n_f[1], t[1]), conv(n_f[1], n_f[2], bias=False),
MemBlock(n_f[2], n_f[2]), MemBlock(n_f[2], n_f[2]), MemBlock(n_f[2], n_f[2]), nn.Upsample(scale_factor=s[2]), TGrow(n_f[2], t[2]), conv(n_f[2], n_f[3], bias=False),
nn.ReLU(inplace=True), conv(n_f[3], 3 * patch_size**2),
) # fmt: skip
self.latent_channels = latent_channels
self.patch_size = patch_size
self.spatial_compression = s[0] * s[1] * s[2] * patch_size
self.t_upscale = t[0] * t[1] * t[2]
self.frames_to_trim = self.t_upscale - 1
# Every latent frame decodes `t_upscale` frames through the full-resolution stages, which is what an
# 8x decoder pays for. Measured for one latent (RTX 4090, fp16, `decode_preview`): taew2_1 needs
# 2145 MiB and 63 ms at 1024 but 536 MiB and 22 ms at 512; the 16x and 32x decoders run those stages
# at a half or a quarter of the edge and stay at 536 MiB (taew2_2) and 266 MiB (taeltx2_3) at 1024,
# under TAESD's 640 MiB. A window of latent frames costs about that per frame: Wan video's
# two-frame window doubles the memory (see `PREVIEW_LATENT_WINDOW` in `wan_video_denoise.py`).
self.max_preview_edge = 512 if patch_size == 1 else 1024
def preview_working_memory_bytes(self, height: int, width: int, frames: int) -> int:
"""2181 MiB for taew2_1 at 1024x1024 in fp16, 549 MiB at its 512 cap, the activations and workspace again per
latent frame of a window."""
output_frames = frames * self.t_upscale
stage_pixels = height * width * (self.spatial_compression // self.patch_size) ** 2
activation_bytes = _LAST_STAGE_CHANNELS * stage_pixels * next(self.parameters()).element_size()
per_frame = _LIVE_ACTIVATIONS_PER_FRAME + _CONV_WORKSPACE_ACTIVATIONS_PER_FRAME
return math.ceil(per_frame * output_frames * activation_bytes) + PREVIEW_FIXED_ALLOWANCE_BYTES
def _decode_frames(self, x: torch.Tensor) -> torch.Tensor:
"""`(N, T, C, h, w)` latents to `(N, T * t_upscale, 3, H, W)` frames in [0, 1], untrimmed.
Parallel over time: each memory block sees the previous frame's input, frame 0 sees zeros.
"""
n = x.shape[0]
x = x.flatten(0, 1)
for block in self.decoder:
if isinstance(block, MemBlock):
nt, c, h, w = x.shape
t = nt // n
past = F.pad(x.view(n, t, c, h, w), (0, 0, 0, 0, 0, 0, 1, 0))[:, :t].reshape(x.shape)
x = block(x, past)
else:
x = block(x)
frames = F.pixel_shuffle(x, self.patch_size).clamp_(0, 1)
return frames.view(n, -1, *frames.shape[1:])
def decode_video(self, latents: torch.Tensor) -> torch.Tensor:
"""`(N, T, C, h, w)` latents to `(N, T * t_upscale - frames_to_trim, 3, H, W)` frames.
The first `frames_to_trim` outputs are causal warm-up that the reference VAEs do not produce.
"""
return self._decode_frames(latents)[:, self.frames_to_trim :]
def decode_window(self, window: torch.Tensor) -> torch.Tensor:
"""The last output frame of a `(B, C, T, h, w)` latent window, decoded after the frames before it."""
return self.decode_video(window.transpose(1, 2))[:, -1]
def forward(self, latents: torch.Tensor) -> torch.Tensor:
# One latent frame decodes to `t_upscale` frames, of which exactly one survives the trim.
return self.decode_window(latents.unsqueeze(2))