from __future__ import annotations import json import math from dataclasses import dataclass, fields from pathlib import Path SIGMAS = ( 1.0, 0.9675236940383911, 0.8875485062599182, 0.75, 0.5327739119529724, 0.23200887441635132, 0.0, ) @dataclass(frozen=True) class DiTConfig: hidden_size: int = 5376 num_layers: int = 50 token_refiner_num_layers: int = 2 num_attention_heads: int = 56 attention_head_dim: int = 128 ffn_hidden_size: int = 14336 latents_dim: int = 24 text_dim: int = 5120 timestep_input_dim: int = 256 time_embed_dim: int = 2688 norm_eps: float = 1e-5 qk_norm_eps: float = 1e-5 final_norm_eps: float = 1e-5 @classmethod def load(cls, path): data = json.loads(Path(path).read_text()) if data.get("audio_enabled", False): raise ValueError("Use the audio-free MiniMax-H3-Image checkpoint") if tuple(data.get("patch_size", (1, 2, 2))) != (1, 2, 2): raise ValueError("This image runtime requires patch_size [1,2,2]") return cls(**{f.name: data[f.name] for f in fields(cls) if f.name in data}) def canvas_size(width: int, height: int, min_area: int = 4194304): if isinstance(width, bool) or isinstance(height, bool) or width < 32 or height < 32: raise ValueError("width and height must be at least 32") if not 1024 <= min_area <= 16777216: raise ValueError("min_generation_area must be between 1024 and 16777216") scale = max(1.0, math.sqrt(min_area / (width * height))) return tuple(max(32, round(d * scale / 32) * 32) for d in (width, height)) def load_sigmas(model_path): data = json.loads((Path(model_path) / "pdd_config.json").read_text()) sigmas = tuple(float(x) for x in data["sigmas"]) if len(sigmas) != 7 or sigmas[0] != 1 or sigmas[-1] != 0: raise ValueError("Expected the native six-step PDD schedule") if any(a <= b for a, b in zip(sigmas, sigmas[1:], strict=False)): raise ValueError("PDD sigmas must be strictly decreasing") return sigmas