Repository navigation
idol: the FLUX.2 VAE image encoder on Metal, in fp32 (stacked on #1766) - #1774
Conversation
|
Thanks @jayleaton. Computing in fp32 on the bf16 weights, well inside the float64 gate, is the right trade for an encode that runs once an image. It waits on #1766; please rebase after it lands. Alongside the contract we'll also run the |
|
I ran the requested offline Metal check on exact head zig build tf-idol-vae tf-kernel-contract -Dcpu=apple_m1 -j6 --summary all
mkdir -p ./vae-offline
./zig-out/bin/tf-kernel-contract --only 'idol vae' --offline ./vae-offlineThe build passed 67/67 steps. All 13 numerical entries passed, all 12 controls went red at their intended gates, and all eight registered VAE launches passed the offline M1 and M2 pipeline checks. The runner exited 0 with zero limit problems. The offline checks cover compilation and launch limits for those architectures. Live execution was on the M5 Max without a checkpoint. With #1766 merged, main Igor Lessio - AIFLOW LABS |
|
Thanks @AIFlowML for the M5 Max offline run and the rebase notes. Rebased onto main
Host checks (Linux, no Mac here): |
…f-idol-vae and its case Builds the denoiser's feature2 [4101, 128] from the preprocessed picture as idol-sdk's ImageEncoder does: the encoder, quant_conv, the posterior's sample with the SDK's noise as an input, the 2 x 2 patches, the batch norm's statistics and five zero rows. fp32 throughout on the SDK's bf16 weights (FLUX.2-klein-4B, or FLUX.2-dev rounded): Winograd 3x3s, implicit-GEMM downsamples and 1x1s, two-pass GroupNorm, single-head attention. idol_case.py vae records the SDK's MPS rows, its noise and a float64 ruler; tf-idol-vae gates worst row and 1 - cosine within twice the SDK's distance from float64 (ours 5.1e-5 and 5.0e-11 against 2.7e-2 and 5.1e-5), and repeat bytes. All eight kernels register with the contract: 13 forms pass gates 1-4, 12 controls red.
…ted probes, as 5a4583c asks
…in's decode stays under 600 lines idol_case.py dispatches its vae subcommand to the new module; the subcommand, its arguments and its pins are unchanged.
|
Thank you @jayleaton. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
Stacked on #1766 (Idol's denoiser on Metal). This branch holds #1766's commits too; its own change is the last two commits. Independent of #1767 and #1773.
What this changes
Idol's FLUX.2 VAE image encoder on Metal: the 1,024 x 1,024 picture to the denoiser's
feature2[4101, 128], as idol-sdk'sImageEncoder._flux2_tokensbuilds it, withtf-idol-vaeand anidol_case.py vaecase. For #1757. Details: the "VAE encoder" part ofdocs/recipes/idol.md.conv_in, fourDownEncoderBlock2Dstages (GroupNorm 32 and SiLU, stride-2 downsamples padded right and below), the mid block with its single-head attention over 16,384 positions,conv_norm_out,conv_out,quant_conv. Then the posterior's sample (the SDK samples, it does not take the mean), the 2 x 2 patches as 128 channels, the batch norm's saved statistics, and five zero rows ahead of the 4,096 patches.encodetakes the noise [128, 128, 32] as an input and the case records the SDK's.zig/kernels/metal/idol_vae.metal, eight kernels of Idol's own. No shared kernel changes. MOSS-Image: the image decoder on Metal #1687 (MOSS-Image's FLUX.2 VAE decoder) is the mirror image of this; the two share no code yet, and once both land the convolutions and GroupNorm could move to one place.Weights and precision
black-forest-labs/FLUX.2-klein-4Bvae/ate7b7dc27f91deacad38e78976d1f2b499d76a294(Apache 2.0, ungated, bf16). Klein's file is FLUX.2-dev's fp32 VAE rounded to bf16; the loader takes either and rounds an fp32 file to bf16 as torch does, and FLUX.2-dev's file gives the same rows bit for bit.The SDK runs the encoder in bf16 on the tensor units. This port computes in fp32 on the same bf16 weights: Winograd F(2x2, 3x3) with
G g G^Tmade in float64 and rounded once, implicit-GEMM stride-2 and 1x1 convolutions, two-pass GroupNorm, fp32 attention,precise::exp. None of it uses the tensor units, whose fp32 GEMM truncates its operands (#1772).iv_tokenskeeps a NaN logvar astorch.clampdoes.Kernel contract
zig/tests/contract/metal_idol_vae*.zig: all eight kernels in 13 forms against float64, with twelve controls (half-staged inputs, symmetric padding, a dropped Winograd term, half SiLU, half sums, attention without its rescale, swapped patch axes, a NaN logvar lost toclamp).Three controls go red by small margins: the half-staged 1x1 at 1.25 times its bound, Winograd's half patch at 1.05 times, attention without rescale at 1.15 times. These kernels use no tensor ops; their records reserve their thread counts in the source, and the
--offlineM1/M2 compile was not run.Receipt
Environment: Mac Studio M5 Ultra 256 GB, macOS 27; this branch on idol: Idol's denoiser as a native Metal family #1766's
94a039249; idol-sdk4702a7f0on torch 2.13.0 (MPS) with diffusers 0.42.1 (sha256-pinned files); the SDK's preprocessed avocado example, seed 42.Exactness, against float64 on the CPU with the SDK's own noise:
The gate (twice the SDK's distance, worst row and cosine, zero padding rows) passes by about 500 times on worst row. Against the SDK: cosine 0.999949, worst row 0.0275; the case's SDK rows equal the SDK's own recorded
feature2bit for bit. Each down block, the mid block and the moments sit within 4e-5 (worst pixel) of float64. Five more encodes repeat byte for byte.vae.zig's test plants a 2% row error and a non-zero padding value, and the gate fails each.Speed: 0.185 s an encode against the SDK's 0.095 s (medians of five): this port is half the SDK's speed, because it runs fp32 off the tensor units where the SDK runs bf16 on them. The attention (about 40 ms) reads K and V once every 8 queries; wider query tiles, or the hi/lo split onto the tensor units, are the obvious next steps. It runs once an image, against about 28 s of denoising.
Memory: 3.28 GB peak footprint (
/usr/bin/time -l, the whole run with its case and stage copies), 2 GB of it four 512 MB tensors at the 1,024 x 1,024 x 128 stage.Tests:
zig build test test-golden -Dcpu=apple_m1: 585 of 585 steps, 0 fail (before the rebase onto94a039249, a docs-only change; after it, plainzig build248 of 248).zig fmt --check build.zig zig: clean.lean_check.py: 424, the same as main.Not run: CUDA (none in this PR); Metal M1 to M4.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (n/a.)SUMMARYline is in the receipt.