"""Rebuild the stable UCPE inference model from a native WDM checkpoint.""" from __future__ import annotations from dataclasses import asdict, dataclass from pathlib import Path from nets.ucpe_cd50_stable.decoder import D4RTDecoderConfig from nets.ucpe_cd50_stable.encoder import VideoMAEEncoderConfig from nets.ucpe_cd50_stable.model import ( UCPECD50StableModel, _flatten_weights, _read_weights, ) from nets.ucpe_cd50_stable.transition import UCPETransitionConfig @dataclass(frozen=True) class CheckpointInfo: step: int loaded_tensors: int encoder: dict decoder: dict camera_condition_max_diameter_m: float | None ucpe_transition: dict | None = None def recorded_model_config(recorded: dict) -> tuple[dict, dict]: trainer = recorded["trainer"] if trainer["type"] not in ( "mv_trainer_stable_ucpe_v00.MVTrainerConfig", "mv_trainer_stable_ucpe_forge_v00.MVTrainerConfig", ): raise ValueError("Expected the stable UCPE trainer configuration") if trainer["fusion"] != {"type": "none_v00.NoFusionConfig"}: raise ValueError("The standalone stable UCPE evaluator requires NoFusion") if not trainer["decoder_use_t_cam"] or trainer["decoder_t_cam_mode"] != "target": raise ValueError("Expected a decoder predicting in the target camera frame") losses = trainer["losses"] if losses["pos_3d_scale_mode"] != "none" or losses["metric_pos_3d_symlog"]: raise ValueError("Unsupported transformation of the predicted 3D head") batch = trainer["batch"] if not batch["use_all_views"] or batch["input_len"] is not None: raise ValueError("Expected all-view encoding of the full frame window") encoder = dict(trainer["encoder"]) decoder = dict(trainer["decoder"]) if encoder.pop("type") != "videomae_stable_ucpe_v00.VideoMAEEncoderConfig": raise ValueError("Expected the stable UCPE encoder") if decoder.pop("type") != "d4rt_fp32_v00.D4RTDecoderConfig": raise ValueError("Expected the fp32 D4RT decoder") if encoder["camera_encoding"] != "ucpe_relative" or not encoder["mv_interleave"]: raise ValueError("Expected relative UCPE with interleaved views") if decoder["img_window_size"] != batch["image_win_size"]: raise ValueError("Encoder-query patch size differs from the decoder") if not decoder["use_t_cam"] or not decoder["use_v_src"]: raise ValueError("Expected source-view and target-camera decoder conditioning") if encoder["load_pretrained"]: raise ValueError("Evaluation must load every model weight from this checkpoint") if ( batch["input_view_num"] is not None or batch["max_view_num"] != encoder["max_view_num"] ): raise ValueError("The encoded view limit differs from the recorded batch") return encoder, decoder def load_checkpoint( path: str | Path, *, use_flash_attn: bool = True ) -> tuple[UCPECD50StableModel, CheckpointInfo]: payload = _read_weights(path) if payload.get("format_version") != 1 or not isinstance( payload.get("run_config"), dict ): raise ValueError("Expected native WDM format 1 with a recorded run_config") recorded = payload["run_config"] encoder_options, decoder_options = recorded_model_config(recorded) encoder_options["use_flash_attn"] = use_flash_attn encoder_config = VideoMAEEncoderConfig(**encoder_options) decoder_config = D4RTDecoderConfig(**decoder_options) encoder = encoder_config.build() decoder = decoder_config.build(encoder.embed_dim) transition = recorded.get("ucpe_transition") model = UCPECD50StableModel( encoder, decoder, None if transition is None else UCPETransitionConfig(**transition), ) state = _flatten_weights(payload) model.load_state_dict(state, strict=True) step = int(payload["global_step"]) model.set_checkpoint_step(step) model.eval() return model, CheckpointInfo( step, len(state), asdict(encoder_config), asdict(decoder_config), recorded["trainer"].get("camera_condition_max_diameter_m"), transition, )