"""The halves of a **split** MiniMax-H3 deployment, for both of its checkpoint partitions. MiniMax-H3 is 195.9 GiB in bfloat16 and a ZeroGPU Space is evicted at 150 GB of storage, so `MiniMaxH3Blocks` is cut at its `text_encoder` step: the 62.14 GiB Qwen3-VL runs in the conditioner Space, everything else in a generator Space, and `prompt_embeds` + `text_token_tags` is the whole wire format between them. `resize` / `setup` run on **both** sides: they own no pretrained component, and each half needs the canvas and the prepared keyframes or normalized references. Both conditioner halves also return the resolved `height` / `width` / `num_frames`, which the generating half pins rather than re-deriving. Two things the blocks leave to the caller: a keyframe reaches them EXIF-transposed and in RGB, and the `t2va` / `fl2va` frame count is aligned to `17 * n + 5` before the call, since that arithmetic lives on the denoising side of the cut. """ from diffusers.modular_pipelines.minimax_h3.before_encoder import MiniMaxH3Ref2VASetupStep, MiniMaxH3ResizeStep from diffusers.modular_pipelines.minimax_h3.decoders import ( MiniMaxH3AfterDenoiseStep, MiniMaxH3AudioDecodeStep, MiniMaxH3VideoDecodeStep, ) from diffusers.modular_pipelines.minimax_h3.encoders import ( MiniMaxH3KeyframeVaeEncoderStep, MiniMaxH3Ref2VAReferenceEncoderStep, MiniMaxH3Ref2VATextEncoderStep, MiniMaxH3TextEncoderStep, ) from diffusers.modular_pipelines.minimax_h3.modular_blocks_minimax_h3 import ( MiniMaxH3FL2VACoreDenoiseStep, MiniMaxH3Ref2VACoreDenoiseStep, ) from diffusers.modular_pipelines.modular_pipeline import SequentialPipelineBlocks from diffusers.modular_pipelines.modular_pipeline_utils import OutputParam def _wire_outputs(num_frames: bool = True) -> list[OutputParam]: """The wire format of the split. `num_frames` is declared by the `ref2va` half alone, whose setup resolves one.""" return [ OutputParam.template("prompt_embeds"), OutputParam("text_token_tags", description="The per-row modality tag of every row of `prompt_embeds`."), OutputParam("height", type_hint=int, description="Resolved height of the generated video in pixels."), OutputParam("width", type_hint=int, description="Resolved width of the generated video in pixels."), *( [OutputParam("num_frames", type_hint=int, description="Resolved number of frames, of the form 17 * n + 5.")] if num_frames else [] ), ] class MiniMaxH3ConditionerBlocks(SequentialPipelineBlocks): """The conditioner half of a split MiniMax-H3: the keyframes on the canvas plus the Qwen3-VL read at layer 50.""" model_name = "minimax-h3" block_classes = [MiniMaxH3ResizeStep, MiniMaxH3TextEncoderStep] block_names = ["resize", "text_encoder"] @property def description(self): return ( "The conditioner half of a split MiniMax-H3 deployment: puts the keyframes onto the target canvas and " "encodes MiniMax-H3's presentation of the request into the `prompt_embeds` / `text_token_tags` pair the " "denoising half consumes. The frame count is the caller's to align." ) @property def outputs(self): return _wire_outputs(num_frames=False) class MiniMaxH3FL2VAAfterDenoiseStep(MiniMaxH3AfterDenoiseStep): """Normalize FL2VA audio latents before the upstream unpack step. The denoiser can leave audio latents as [2 * latent_channels, T] (and in some builds with an extra leading singleton dimension). The upstream AfterDenoise implementation assumes [2 * T, latent_channels] and otherwise attempts an invalid reshape using T as the feature width. """ def __call__(self, components, state): block_state = self.get_block_state(state) audio_rows = block_state.audio_latents audio_channels = int(getattr(components, "audio_channels", 2)) latent_channels = int(getattr(components, "audio_latent_channels", 32)) num_audio_latents = int(block_state.num_audio_latents) expected_elements = audio_channels * latent_channels * num_audio_latents expected_rows_shape = (audio_channels * num_audio_latents, latent_channels) # The error seen in the Space is 13,248 elements = 2 * 32 * 207, # while upstream tries to reshape them as [2, 207, 207]. Normalize # any equivalent channel-major representation, including [1, 64, T], # before delegating to the official video/audio decode preparation. if ( audio_rows.numel() == expected_elements and tuple(audio_rows.shape) != expected_rows_shape ): if audio_rows.ndim >= 1 and audio_rows.shape[-1] == num_audio_latents: channel_major = audio_rows.reshape( audio_channels, latent_channels, num_audio_latents ) block_state.audio_latents = ( channel_major.permute(0, 2, 1) .reshape(audio_channels * num_audio_latents, latent_channels) .contiguous() ) self.set_block_state(state, block_state) else: raise RuntimeError( "Unexpected FL2VA audio latent layout before AfterDenoise: " f"shape={tuple(audio_rows.shape)}, numel={audio_rows.numel()}, " f"expected_elements={expected_elements}, " f"num_audio_latents={num_audio_latents}, " f"latent_channels={latent_channels}." ) return super().__call__(components, state) class MiniMaxH3FL2VACoreDenoiseStepWithAudioFix(MiniMaxH3FL2VACoreDenoiseStep): """Use the audio-layout fix inside FL2VA's own denoising sequence. FL2VACoreDenoiseStep already contains its AfterDenoise step. Adding a second AfterDenoise at the outer generator level unpacks the same latents twice; this can leave an empty video batch even though audio decoding succeeds. Replace the nested step instead of running a second unpack pass. """ block_classes = [ *MiniMaxH3FL2VACoreDenoiseStep.block_classes[:-1], MiniMaxH3FL2VAAfterDenoiseStep, ] class MiniMaxH3GeneratorBlocks(SequentialPipelineBlocks): """The denoising half of a split MiniMax-H3: `MiniMaxH3Blocks` with its `text_encoder` step removed.""" model_name = "minimax-h3" block_classes = [ MiniMaxH3ResizeStep, MiniMaxH3KeyframeVaeEncoderStep, MiniMaxH3FL2VACoreDenoiseStepWithAudioFix, MiniMaxH3VideoDecodeStep, MiniMaxH3AudioDecodeStep, ] block_names = ["resize", "vae_encoder", "denoise", "video_decode", "audio_decode"] @property def description(self): return ( "The denoising half of a split MiniMax-H3 deployment: the `t2va` / `fl2va` branch of `MiniMaxH3Blocks` " "without its text-encoder step, so `prompt_embeds` and `text_token_tags` come in as inputs and the " "62.14 GiB Qwen3-VL conditioner is never loaded here." ) @property def outputs(self): return [ OutputParam.template("videos", description="The generated video."), OutputParam.template("audio", description="The generated soundtrack."), OutputParam("sampling_rate", type_hint=int, description="Audio sample rate in Hz."), ] class MiniMaxH3Ref2VAConditionerBlocks(SequentialPipelineBlocks): """The conditioner half of a split `ref2va`: the resolved plan plus the Qwen3-VL read at its 50th layer. Component for component this is `MiniMaxH3ConditionerBlocks`, so one conditioner Space serves both partitions. What differs is the presentation: `ref2va` prepends a label per reference and a vision block per image and per merged video frame pair, so the references themselves have to reach this half. """ model_name = "minimax-h3" block_classes = [MiniMaxH3Ref2VASetupStep, MiniMaxH3Ref2VATextEncoderStep] block_names = ["setup", "text_encoder"] @property def description(self): return ( "The conditioner half of a split MiniMax-H3 `ref2va` deployment: resolves the request plan (canvas, frame " "count, references normalized onto MiniMax-H3's own rates and resolutions) and encodes MiniMax-H3's " "presentation of it into the `prompt_embeds` / `text_token_tags` pair the denoising half consumes." ) @property def outputs(self): return _wire_outputs() class MiniMaxH3Ref2VAGeneratorBlocks(SequentialPipelineBlocks): """The denoising half of a split `ref2va`: the `ref2va` branch with its `text_encoder` step removed. `reference_encoder` stays here, next to the two autoencoders it runs: its output shapes are where every reference block's geometry in the packed layout comes from. """ model_name = "minimax-h3" block_classes = [ MiniMaxH3Ref2VASetupStep, MiniMaxH3Ref2VAReferenceEncoderStep, MiniMaxH3Ref2VACoreDenoiseStep, MiniMaxH3VideoDecodeStep, MiniMaxH3AudioDecodeStep, ] block_names = ["setup", "reference_encoder", "denoise", "video_decode", "audio_decode"] @property def description(self): return ( "The denoising half of a split MiniMax-H3 `ref2va` deployment: the `ref2va` branch of `MiniMaxH3Blocks` " "without its text-encoder step, so `prompt_embeds` and `text_token_tags` come in as inputs and the " "62.14 GiB Qwen3-VL conditioner is never loaded here. The transformer is the `transformer_ref` partition." ) @property def outputs(self): return [ OutputParam.template("videos", description="The generated video."), OutputParam.template("audio", description="The generated soundtrack."), OutputParam("sampling_rate", type_hint=int, description="Audio sample rate in Hz."), ]