diff --git a/tools/idol/README.md b/tools/idol/README.md new file mode 100644 index 00000000..dfe9dc4e --- /dev/null +++ b/tools/idol/README.md @@ -0,0 +1,23 @@ +# IDOL (speridlabs/idol): image to 3D Gaussian splat, the ONNX work so far + +Parked 2026-11-10 before any export. What is here runs on Modal, because the weights and exports need about 20 GB. + +- `idol_modal.py`: one step script on a GPU, with an idol-sdk checkout (`IDOL_SDK`) and the volume `idol-work` + (`/vol/hf` the Hugging Face cache, `/vol/ref` the reference dumps, `/vol/onnx` for exports). +- `steps/common.py`: the ungated encoder sources and `CpuRandom`, which draws every random of a run from one CPU + generator so a run is the same on any device. +- `steps/s1_sources.py`: proves the two sources, runs the PyTorch reference (toy-robot-tin, seed 42, 262,144 + Gaussians, 50 steps) and saves each network's inputs and outputs as `.npy` with a `shapes.txt`. + +## Measured + +| Check | Result | +|---|---| +| FLUX.2 VAE: FLUX.2-klein-4B `vae/` (bf16) against FLUX.2-dev's (fp32, from diffusers/FLUX.2-dev-bnb-4bit) | 251 of 251 tensors equal after the bf16 cast, 84.0M parameters | +| DINOv3 ViT-H+/16 from timm against the gated transformers model (its public ONNX conversion, onnx-community/dinov3-vith16plus-pretrain-lvd1689m-ONNX), tokens at 1024x1024 | cosine 0.99999979, max abs difference 0.025 (tokens reach 10) | +| timm's forward against timm's weights loaded into transformers' `DINOv3ViTModel` | cosine 1.00000000, max abs difference 2.6e-5 | +| PyTorch reference, fp32 and bf16 DINOv3 | peak VRAM 8.72 GB; 20 s on the first GPU drawn, 59 s on the second | + +The transformers config for H+ is `hidden_act="silu"`, gated MLP, 32 layers, 20 heads, 4 registers, rope_theta 100. +With the default GELU the tokens fall to cosine 0.95. timm's file has no q/k/v biases, and the conversion has none +either. The key map is in `s1_sources.py`. diff --git a/tools/idol/idol_modal.py b/tools/idol/idol_modal.py new file mode 100644 index 00000000..bb9c917a --- /dev/null +++ b/tools/idol/idol_modal.py @@ -0,0 +1,55 @@ +"""IDOL -> ONNX on Modal: runs one step script on a GPU with the idol-sdk checkout and a volume (idol-work) for +weights, reference dumps and exports. + + IDOL_SDK=/path/to/idol-sdk modal run tools/idol/idol_modal.py --step s1_sources [--on gpu|big|cpu] +""" +import os, subprocess, sys +import modal + +HERE = os.path.dirname(os.path.abspath(__file__)) +# a checkout of github.com/speridlabs/idol-sdk (8db7003 when this was written), outside this repository +SDK = os.environ["IDOL_SDK"] +vol = modal.Volume.from_name("idol-work", create_if_missing=True) +image = ( + modal.Image.debian_slim(python_version="3.11") + .apt_install("git") + .uv_pip_install( + "torch==2.8.0", "torchvision==0.23.0", "transformers>=4.56", "diffusers>=0.40", "accelerate>=1.0", + "safetensors>=0.4", "tensordict>=0.8", "numpy>=1.26", "pillow>=10.0", + "plyfile>=1.0", "tqdm", "timm>=1.0.20", "onnx>=1.18", "onnxscript", "onnxconverter-common", + "onnxruntime-gpu==1.22.0", "hf_transfer", + ) + .env({"HF_HOME": "/vol/hf", "HF_HUB_ENABLE_HF_TRANSFER": "1", "PYTHONUNBUFFERED": "1"}) + .add_local_dir(SDK, "/sdk", ignore=[".git"]) + .add_local_dir(os.path.join(HERE, "steps"), "/steps") +) +app = modal.App("idol-onnx", image=image) + + +def _run(step, args): + vol.reload() + r = subprocess.run([sys.executable, f"/steps/{step}.py", *args.split()], cwd="/vol", + env={**os.environ, "PYTHONPATH": "/sdk:/steps"}) + vol.commit() + if r.returncode: + raise SystemExit(r.returncode) + + +@app.function(gpu=["L4", "A10G", "L40S"], volumes={"/vol": vol}, timeout=3600, memory=32768) +def gpu(step: str, args: str = ""): + _run(step, args) + + +@app.function(gpu="A100-40GB", volumes={"/vol": vol}, timeout=3600, memory=65536) +def big(step: str, args: str = ""): + _run(step, args) + + +@app.function(volumes={"/vol": vol}, timeout=3600, memory=65536, cpu=8) +def cpu(step: str, args: str = ""): + _run(step, args) + + +@app.local_entrypoint() +def main(step: str, args: str = "", on: str = "gpu"): + {"gpu": gpu, "big": big, "cpu": cpu}[on].remote(step, args) diff --git a/tools/idol/steps/common.py b/tools/idol/steps/common.py new file mode 100644 index 00000000..e67dbcc5 --- /dev/null +++ b/tools/idol/steps/common.py @@ -0,0 +1,69 @@ +"""Shared by the steps: the ungated encoder sources, and the reference pipeline with every random drawn on the CPU.""" +import os +import numpy as np +import torch +from huggingface_hub import hf_hub_download, snapshot_download +from safetensors.torch import load_file + +TIMM = "timm/vit_huge_plus_patch16_dinov3.lvd1689m" +KLEIN = "black-forest-labs/FLUX.2-klein-4B" +REF = "/vol/ref" +ONNX = "/vol/onnx" + + +def idol_dir(): + return snapshot_download("speridlabs/idol", allow_patterns=["*.json", "*.safetensors"]) + + +def timm_dinov3(): + import timm + m = timm.create_model("vit_huge_plus_patch16_dinov3.lvd1689m", pretrained=False, num_classes=0) + m.load_state_dict(load_file(hf_hub_download(TIMM, "model.safetensors")), strict=True) + return m.eval().requires_grad_(False) + + +class TimmTokens(torch.nn.Module): + """timm's DINOv3 with the output IDOL reads from transformers: last_hidden_state [B, 1 + 4 + patches, 1280].""" + def __init__(self, m): + super().__init__() + self.m = m + + def forward(self, pixel_values): + from types import SimpleNamespace + return SimpleNamespace(last_hidden_state=self.m.forward_features(pixel_values)) + + +def klein_vae(dtype=torch.bfloat16): + from diffusers.models import AutoencoderKLFlux2 + return AutoencoderKLFlux2.from_pretrained(KLEIN, subfolder="vae", torch_dtype=dtype).eval().requires_grad_(False) + + +class CpuRandom: + """torch.randn / rand / rand_like drawn from one CPU generator, so a run is the same on any device.""" + def __init__(self, seed): + self.g = torch.Generator().manual_seed(seed) + self.log = [] + + def __enter__(self): + self.old = (torch.randn, torch.rand, torch.rand_like) + def randn(*size, device=None, **kw): + x = self.old[0](*size, generator=self.g); self.log.append(("randn", x.numpy().copy())); return x.to(device) + def rand(*size, device=None, **kw): + x = self.old[1](*size, generator=self.g); self.log.append(("rand", x.numpy().copy())); return x.to(device) + def rand_like(t, dtype=None, **kw): + x = self.old[1](*t.shape, generator=self.g); self.log.append(("rand", x.numpy().copy())); return x.to(t.device) + torch.randn, torch.rand, torch.rand_like = randn, rand, rand_like + return self + + def __exit__(self, *a): + torch.randn, torch.rand, torch.rand_like = self.old + + +def save(name, x): + os.makedirs(REF, exist_ok=True) + np.save(f"{REF}/{name}.npy", x.detach().float().cpu().numpy() if torch.is_tensor(x) else np.asarray(x)) + + +def cos(a, b): + a, b = torch.as_tensor(a).double().flatten(), torch.as_tensor(b).double().flatten() + return float(torch.dot(a, b) / (a.norm() * b.norm())) diff --git a/tools/idol/steps/s1_sources.py b/tools/idol/steps/s1_sources.py new file mode 100644 index 00000000..93559fc8 --- /dev/null +++ b/tools/idol/steps/s1_sources.py @@ -0,0 +1,145 @@ +"""The two ungated encoder sources are the weights IDOL expects (FLUX.2 VAE from klein, DINOv3 from timm); then the +PyTorch reference on one image and seed, with every network's inputs and outputs saved to /vol/ref.""" +import os, sys, time +import numpy as np +import torch +from PIL import Image +from huggingface_hub import hf_hub_download +from safetensors.torch import load_file +from common import * + +dev = torch.device("cuda") +img_path = "/sdk/demo/examples/toy-robot-tin.webp" + +# --- FLUX.2 VAE: klein's file against FLUX.2-dev's (an ungated diffusers-format copy), tensor by tensor +a = load_file(hf_hub_download(KLEIN, "vae/diffusion_pytorch_model.safetensors")) +b = load_file(hf_hub_download("diffusers/FLUX.2-dev-bnb-4bit", "vae/diffusion_pytorch_model.safetensors")) +assert a.keys() == b.keys(), (set(a) ^ set(b)) +K = "encoder.conv_in.weight" +exact = sum(torch.equal(a[k], b[k].to(a[k].dtype)) for k in a) +worst = max(float((a[k].float() - b[k].float()).abs().max()) for k in a) +print(f"VAE: {len(a)} tensors, klein dtype {a[K].dtype}, dev dtype {b[K].dtype}, " + f"equal after cast {exact}/{len(a)}, max abs diff {worst:.3e}, params {sum(v.numel() for v in a.values())/1e6:.1f}M") +del a, b + +# --- DINOv3: timm's weights in transformers' DINOv3ViTModel, against timm's own forward +from transformers import DINOv3ViTConfig, DINOv3ViTModel +sd = load_file(hf_hub_download(TIMM, "model.safetensors")) +cfg = DINOv3ViTConfig(patch_size=16, hidden_size=1280, intermediate_size=5120, num_hidden_layers=32, + num_attention_heads=20, num_register_tokens=4, use_gated_mlp=True, hidden_act="silu", layerscale_value=1.0, + query_bias=False, key_bias=False, value_bias=False, proj_bias=True, mlp_bias=True, + rope_theta=100.0, pos_embed_rescale=2.0, layer_norm_eps=1e-5, image_size=224) +hf = DINOv3ViTModel(cfg).eval().requires_grad_(False) +want = hf.state_dict() +print("hf keys:", len(want), [k for k in want if ".layer." not in k and not k.startswith("layer.")]) +new = {"embeddings.cls_token": sd["cls_token"], "embeddings.register_tokens": sd["reg_token"], + "embeddings.mask_token": torch.zeros_like(want["embeddings.mask_token"]), + "embeddings.patch_embeddings.weight": sd["patch_embed.proj.weight"], + "embeddings.patch_embeddings.bias": sd["patch_embed.proj.bias"], + "norm.weight": sd["norm.weight"], "norm.bias": sd["norm.bias"]} +pre = "model." if any(k.startswith("model.layer.") for k in want) else "" +for i in range(32): + p, q = f"blocks.{i}.", f"{pre}layer.{i}." + w = sd[p + "attn.qkv.weight"] + for j, n in enumerate("qkv"): + new[q + f"attention.{n}_proj.weight"] = w[j * 1280:(j + 1) * 1280] + new[q + "attention.o_proj.weight"], new[q + "attention.o_proj.bias"] = sd[p + "attn.proj.weight"], sd[p + "attn.proj.bias"] + new[q + "layer_scale1.lambda1"], new[q + "layer_scale2.lambda1"] = sd[p + "gamma_1"], sd[p + "gamma_2"] + for n in ("norm1", "norm2"): + new[q + n + ".weight"], new[q + n + ".bias"] = sd[p + n + ".weight"], sd[p + n + ".bias"] + # timm's SwiGLU: fc1_g is the gate (activated), fc1_x the value; transformers: gate_proj (activated) * up_proj + for s, d in (("fc1_g", "gate_proj"), ("fc1_x", "up_proj"), ("fc2", "down_proj")): + new[q + f"mlp.{d}.weight"], new[q + f"mlp.{d}.bias"] = sd[p + f"mlp.{s}.weight"], sd[p + f"mlp.{s}.bias"] +missing = set(want) - set(new); extra = set(new) - set(want) +print("missing", sorted(missing)[:10], "extra", sorted(extra)[:10]) +hf.load_state_dict(new, strict=True) +print(f"DINOv3: transformers params {sum(p.numel() for p in hf.parameters())/1e6:.1f}M") +tm = timm_dinov3() +print(f"DINOv3: timm params {sum(p.numel() for p in tm.parameters())/1e6:.1f}M") + +from idol.pipeline import IdolPipeline +enc = None +pipe = IdolPipeline(idol_dir(), device="cuda", optimize=False, dinov3_dtype="fp32", + image_encoders={"dinov3": hf, "flux2_vae": klein_vae()}) +prepared = pipe.preprocess(img_path, mode="synthetic") +os.makedirs(REF, exist_ok=True) +prepared.save(f"{REF}/prepared.png") +Image.open(img_path).save(f"{REF}/input.png") +pixels = torch.from_numpy(np.array(prepared)).permute(2, 0, 1).float().div(255.0)[None].to(dev) +mean = torch.tensor((0.485, 0.456, 0.406), device=dev).view(1, 3, 1, 1); std = torch.tensor((0.229, 0.224, 0.225), device=dev).view(1, 3, 1, 1) +x = (pixels - mean) / std +with torch.no_grad(): + t_hf = hf.to(dev)(pixel_values=x).last_hidden_state + t_tm = tm.to(dev).forward_features(x) +print(f"DINOv3 tokens at 1024: shape {tuple(t_hf.shape)} vs {tuple(t_tm.shape)}, cosine {cos(t_hf, t_tm):.8f}, " + f"max abs diff {float((t_hf - t_tm).abs().max()):.3e} (token max {float(t_hf.abs().max()):.2f})") +for i, n in ((0, "cls"), (1, "reg0"), (5, "patch0")): + print(f" {n} cosine {cos(t_hf[0, i], t_tm[0, i]):.8f}") +del tm +# ground truth: the transformers model with the gated repo's own weights, as its public ONNX conversion +import onnxruntime as ort, onnx +ort.preload_dlls() +gt_dir = os.path.dirname(hf_hub_download("onnx-community/dinov3-vith16plus-pretrain-lvd1689m-ONNX", "onnx/model.onnx")) +hf_hub_download("onnx-community/dinov3-vith16plus-pretrain-lvd1689m-ONNX", "onnx/model.onnx_data") +mo = onnx.load(f"{gt_dir}/model.onnx", load_external_data=False) +print("gt onnx inputs", [(i.name, [d.dim_param or d.dim_value for d in i.type.tensor_type.shape.dim]) for i in mo.graph.input], + "outputs", [o.name for o in mo.graph.output], "initializers", len(mo.graph.initializer)) +print("gt initializers with bias in the name, layer 0:", [(i.name, list(i.dims)) for i in mo.graph.initializer if "layer.0." in i.name and "bias" in i.name]) +del mo +so = ort.SessionOptions() +sess = ort.InferenceSession(f"{gt_dir}/model.onnx", so, providers=["CUDAExecutionProvider", "CPUExecutionProvider"]) +print("gt providers", sess.get_providers()) +t_gt = torch.from_numpy(sess.run(["last_hidden_state"], {sess.get_inputs()[0].name: x.cpu().numpy()})[0]).to(dev) +del sess +for n, t in (("timm", t_tm), ("transformers arch + timm weights", t_hf)): + print(f"DINOv3 {n} vs the gated model's ONNX conversion: cosine {cos(t, t_gt):.8f}, max abs diff {float((t - t_gt).abs().max()):.3e}") +torch.cuda.empty_cache() + +# --- the reference, on the same randoms whatever the device; fp32 DINOv3 and bf16 DINOv3 (IDOL's default) both +def run(tag, dtype, seed=42, n=262144): + enc.dinov3 = hf.to(dev, {"fp32": torch.float32, "bf16": torch.bfloat16}[dtype]) + t0 = time.time() + with CpuRandom(seed) as r: + out = pipe(img_path, mode="synthetic", seed=seed, num_gaussians=n) + torch.cuda.synchronize() + print(f"reference {tag}: {time.time() - t0:.1f}s, peak VRAM {torch.cuda.max_memory_allocated()/2**30:.2f} GB, camera {out.camera}") + out.gaussian.save_ply(f"{REF}/ref_{tag}.ply") + return out, r.log + +# every network's inputs and outputs on the fp32 run, in the seam's shapes +calls = {"dit": 0, "oct": 0} +def h_mask(m, a, o): save("mask_in", a[0]); save("mask_out", o) +def h_dit(m, a, o): + i = calls["dit"]; calls["dit"] += 1 + step, neg = divmod(i, 2) + if step in (0, 12, 25, 37, 49): + tag = f"dit_s{step:02d}_{'neg' if neg else 'cond'}" + save(tag + "_latent", a[0]["latent"]); save(tag + "_camera", a[0]["camera"]); save(tag + "_t", a[1]) + save(tag + "_v_latent", o["latent"]); save(tag + "_v_camera", o["camera"]) +def h_oct(m, a, o): + calls["oct"] += 1; l = calls["oct"] + save(f"oct_l{l}_cells", a[0]); save(f"oct_l{l}_resolution", a[1]); save(f"oct_l{l}_logits", o) +def h_gs(m, a, o): save("gs_anchors", a[0]); save("gs_latent", a[1]); save("gs_features", o) +hooks = [pipe.background_remover.register_forward_hook(h_mask), pipe.denoiser.register_forward_hook(h_dit), + pipe.decoder.register_forward_hook(h_oct), pipe.decoder_gs.register_forward_hook(h_gs)] +enc = pipe.image_encoder +class Enc: + def __call__(self, px): + c = enc(px); save("pixels", px); save("feature1", c["feature1"]); save("feature2", c["feature2"]); return c +pipe.image_encoder = Enc() +out, log = run("fp32", "fp32") +for h in hooks: h.remove() +pipe.image_encoder = enc +save("vae_eps", log[0][1]); save("noise_latent", log[1][1]); save("noise_camera", log[2][1]) +save("octree_rand", np.concatenate([v.reshape(-1) for k, v in log[3:]])) +[os.remove(p) for p in __import__("glob").glob(f"{REF}/octree_rand_*.npy")] +save("gs_points_offset_perturbation", pipe.decoder_gs.points_offset_perturbation); save("gs_base_offset_scale", pipe.decoder_gs.base_offset_scale) +import glob +with open(f"{REF}/shapes.txt", "w") as f: + for p in sorted(glob.glob(f"{REF}/*.npy")): + a = np.load(p); f.write(f"{os.path.basename(p)}\t{list(a.shape)}\t{a.dtype}\n") +print(open(f"{REF}/shapes.txt").read()) +print("dit calls", calls) +print("randoms drawn:", [(k, v.shape) for k, v in log]) +out2, _ = run("bf16", "bf16") +print(f"splat fp32 vs bf16 DINOv3: xyz max diff {float((out.gaussian.xyz - out2.gaussian.xyz).abs().max()):.4f}")