# Copyright 2026 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. r""" MiniMax-H3 RefMod: a portable reference-conditioning file for `ref2va`. This is the diffusers-modular equivalent of the ComfyUI "RefMod" trick. A `ref2va` request conditions on an ordered list of references, and `MiniMaxH3Ref2VAReferenceEncoderStep` turns their pixels into the `condition_latents` the transformer prepends to its packed sequence. That VAE encode is deterministic (the posterior is sampled under a fixed `keyframe_encode_seed`) and independent of the prompt, so it is pure, reusable work. RefMod caches it: encode a reference once, write the latents to a small `.safetensors`, and inject them straight back on later requests instead of re-encoding. What RefMod does and does not carry: - It carries the **VAE condition latents** — `condition_latents` (one `(1, C, T, H, W)` tensor per image/video reference) and `audio_condition_latents` (one `(num_audio_latents * 2, audio_channels)` tensor per soundtrack). These are the rows the denoiser attends to, already normalized and fp16-rounded exactly as the live encoder leaves them, so a save/load round-trip is bitwise-lossless. - It does **not** carry the Qwen3-VL conditioning. In MiniMax-H3 a reference also appears to the text encoder as a `""`/`"