LatentEUPE
LatentEUPE computes the features of convnext_tiny.eupe_lvd1689m
(the teacher) from the Qwen-Image VAE latent of an image, without decoding the latent. It reads the normalised latent
of a 512×512 image (16×64×64) and outputs the teacher's 768×8×8 feature map of
the 256×256 downscale of that image, and the teacher's pooled 768-dimensional embedding. It serves as a
feature network on diffusion latents, for example for a perceptual or Fréchet-distance loss or as a critic backbone,
at a fraction of the cost of decoding.
A normalised latent is the mode of the VAE encoder's output minus the VAE config's per-channel latents_mean,
divided by its latents_std: the space the diffusion model works in. The VAE is the Qwen-Image VAE (Wan2.1
architecture), Qwen/Qwen-Image subfolder vae.
Usage
model.py in this repository defines the model; it needs torch, timm and huggingface_hub. Encoding images
needs diffusers.
import sys
import torch
from diffusers import AutoencoderKLQwenImage
from huggingface_hub import snapshot_download
sys.path.insert(0, snapshot_download("Gaeros/wan2.1-convnext_tiny.eupe_lvd1689m", allow_patterns=["model.py"]))
from model import LatentEUPE
student = LatentEUPE.from_pretrained("Gaeros/wan2.1-convnext_tiny.eupe_lvd1689m").cuda().eval()
vae = AutoencoderKLQwenImage.from_pretrained("Qwen/Qwen-Image", subfolder="vae", torch_dtype=torch.bfloat16).cuda()
mean = torch.tensor(vae.config.latents_mean, device="cuda").view(1, -1, 1, 1)
std = torch.tensor(vae.config.latents_std, device="cuda").view(1, -1, 1, 1)
images = ... # [B, 3, 512, 512] in [-1, 1]
with torch.no_grad():
# the VAE works on videos, [B, C, T, H, W]
z = vae.encode(images.to(torch.bfloat16)[:, :, None]).latent_dist.mode()[:, :, 0]
z = (z.float() - mean) / std # normalised latent [B, 16, 64, 64]
with torch.autocast("cuda", torch.bfloat16):
features = student(z) # [B, 768, 8, 8]
embedding = student.embed(z) # [B, 768]
Latents of the diffusion model in the normalised space go to student directly. model.Teacher is the stock teacher on
256 px images in [0, 1], for reference features.
student.feature_std [768] is the teacher's standard deviation of each output channel over the training images. A
loss on features divided by it per channel reads in the units of the validation below; for example, the training loss
of two latents' features is ((student(z1) - student(z2)) / student.feature_std.view(1, -1, 1, 1)).square().mean(),
and features at each channel's training mean score 1 on average over the training images.
Architecture
LatentEUPE is the teacher with its pixel stem (4×4 stride-4 convolution, then LayerNorm) replaced by a 3×3 convolution from the 16 latent channels, then LayerNorm. The 64×64 latent grid equals the grid the teacher's stem outputs, and each latent cell covers the image region of one stem patch, so the four ConvNeXt stages and the head stay. Stages 2 and 3 keep the teacher's weights; the stem, stage 0 and stage 1 (1.25 M of the 27.8 M parameters) differ from it.
Validation
validate.py compares four pipelines on the validation images. Each maps an image to a 768×8×8
feature map; the reference is the teacher on the image's 256 px downscale, LatentEUPE's training target.
- pixels: the 256 px image, then the teacher; the reference itself.
- VAE: the latent, then the Qwen-Image VAE decoder, an 8-bit image, the 256 px downscale and the teacher.
- taew2.1: the same with madebyollin's tiny Wan2.1 decoder in place of the VAE decoder.
- LatentEUPE: the latent, then LatentEUPE.
Per source, loss is the training loss (per-channel normalised MSE), pos the mean per-position cosine and pooled the cosine of the pooled embeddings. Times are per image on an RTX 4090, eager, in bf16 autocast, at the largest batch of up to 64 (a power of two) whose input gradient fits in 24 GB: the forward pass, and the forward pass plus the gradient with respect to the pipeline's input (the image for pixels, the latent otherwise).
| pipeline | aesthetic_5k loss / pos / pooled | e6 loss / pos / pooled | midjourney loss / pos / pooled | mean loss | batch | forward | input gradient |
|---|---|---|---|---|---|---|---|
| pixels | 0 / 1 / 1 | 0 / 1 / 1 | 0 / 1 / 1 | 0 | 64 | 0.37 ms | 0.80 ms |
| VAE | 0.0060 / 0.9978 / 0.9985 | 0.0051 / 0.9981 / 0.9987 | 0.0048 / 0.9980 / 0.9988 | 0.0053 | 4 | 51.2 ms | 153.4 ms |
| taew2.1 | 0.0172 / 0.9939 / 0.9961 | 0.0192 / 0.9931 / 0.9953 | 0.0131 / 0.9947 / 0.9966 | 0.0165 | 16 | 8.71 ms | 20.3 ms |
| LatentEUPE | 0.0108 / 0.9962 / 0.9983 | 0.0113 / 0.9960 / 0.9980 | 0.0061 / 0.9977 / 0.9987 | 0.0094 | 64 | 0.37 ms | 0.81 ms |
LatentEUPE's loss is 1.8× the VAE round trip's and 0.57× taew2.1's; its pooled cosine is 1 to 7·10⁻⁴ below the round trip's. Its input gradient costs what the teacher's costs on pixels: 25× less than through taew2.1 and 190× less than through the VAE decoder.
Training
The training set holds 29429 images from three sources: aesthetic_5k (4719), midjourney (20171) and e6 (4539); the validation sets hold 249, 500 and 249 images. Each image contributes the largest centred square, resized to 512 px; images under 512 px are skipped. The student's input is the VAE latent of the 512 px square, its target the teacher's features of the 256 px downscale.
The loss is the per-channel normalised MSE: the mean over channels c and positions p of ((s[c,p] − t[c,p]) / σ[c])², where s and t are the student's and the teacher's outputs and σ[c] is the teacher's standard deviation of channel c over the training images. Training runs four phases, each from the previous phase's best checkpoint with fresh AdamW states (batch 64, weight decay 0.1 on conv and linear weights, linear warmup then cosine decay):
| phase | trains | target | steps | lr stem / stages | warmup stem / stages |
|---|---|---|---|---|---|
| a | stem conv | final features | 3000 | 10⁻³ / – | 0 / – |
| s0 | stem, stage 0 | teacher's stage-0 output | 24000 | 3·10⁻³ / 10⁻⁴ | 0 / 1000 |
| s1 | stem, stages 0–1 | teacher's stage-1 output | 24000 | 10⁻³ / 10⁻⁴ | 1000 / 1000 |
| final | stem, stages 0–1 | final features | 5000 | 10⁻⁴ / 2·10⁻⁵ | 200 / 200 |
In the experiments behind this recipe, matching the teacher's intermediate outputs (s0, s1) before the final features lowered the validation loss by 39% against training on the final features alone; stages 2–3 stay the teacher's because training them lowers the training loss only. The four phases take 55 min on an RTX 4090.
Limitations
- The training and validation latents are encoder outputs of real images. Latents the encoder does not produce are out of distribution: on horizontally flipped latents, which differ from the latent of the mirrored image by 45% to 55% of their norm, an earlier student's loss is 0.30 to 0.49 against 0.009 to 0.014 on encoder latents. Latents generated by a diffusion model are untested.
- Only 512×512 images (64×64 latents) are trained and validated.
License
The weights derive from convnext_tiny.eupe_lvd1689m and carry its license, the FAIR Noncommercial Research License.
The taew2.1 decoder used in validation is MIT-licensed (madebyollin/taehv).