#!/usr/bin/env python3 """ Convert the DINOv3 ViT-L/16 image-conditioning encoder (HF format, facebook/dinov3-vitl16-pretrain-lvd1689m or a mirror) to a GGUF file for trellis2.cpp. TRELLIS.2 uses this model (transformers DINOv3ViTModel) to turn the preprocessed 512x512 input image into the [1, 1029, 1024] conditioning tensor consumed by the flow models: 1 CLS + 4 register + 32x32 patch tokens, last transformer layer output passed through an affine-free layer norm (the model's own final `norm` layer is NOT applied). Architecture (config.json): hidden 1024, 24 layers, 16 heads, MLP 4096 (exact GELU, not gated), patch 16, axial 2D RoPE over patch-center coords with theta=100, LayerScale on both residual branches, q/v/o biases but no k bias. Self-contained like the other converters (safetensors + numpy). Tensors keep their HF state-dict names; hyperparameters travel as `trellis2.dino.*` KV. Usage: python convert_dino_to_gguf.py --model models/dinov3-vitl16/model.safetensors \ --output ggufs/dino_f32.gguf --ftype 0 ftype: 0 = f32 (validation), 1 = f16 (matrices f16; norms/biases/1-D f32). """ import argparse import json import os import struct import sys import numpy as np # ── GGUF / GGML constants (must match the bundled ggml) ────────────────────── GGUF_MAGIC = b"GGUF" GGUF_VERSION = 3 GGUF_ALIGNMENT = 32 GGML_TYPE_F32 = 0 GGML_TYPE_F16 = 1 GGUF_VT_UINT32 = 4 GGUF_VT_FLOAT32 = 6 GGUF_VT_BOOL = 7 GGUF_VT_STRING = 8 ARCH = "trellis2-dino" KV_PREFIX = "trellis2.dino." def _gguf_str(s: str) -> bytes: b = s.encode("utf-8") return struct.pack(" bytes: return _gguf_str(key) + struct.pack(" int: return (n + a - 1) // a * a def choose_type(name, shape, ftype: int) -> int: if ftype == 0: return GGML_TYPE_F32 # matrices f16; 1-D (biases, norms, layerscale) and token embeddings f32 if len(shape) < 2: return GGML_TYPE_F32 if "cls_token" in name or "register_tokens" in name or "mask_token" in name: return GGML_TYPE_F32 return GGML_TYPE_F16 def to_bytes(arr_f32: np.ndarray, ggml_type: int) -> bytes: if ggml_type == GGML_TYPE_F32: return arr_f32.astype("