# Copyright 2026 The HuggingFace Team. All rights reserved. # # Licensed under the Apache License, Version 2.0. See the LICENSE file in this repository. import functools import torch from diffusers import AutoModel from diffusers.modular_pipelines import ModularPipelineBlocks, PipelineState from diffusers.modular_pipelines.modular_pipeline_utils import ComponentSpec, InputParam, OutputParam from .motion_utils import FRAMES_PER_TOKEN, root_to_local @functools.cache def allowed_lengths(min_tokens: int, max_tokens: int, num_tokens: tuple, device: torch.device) -> torch.Tensor: """`(1, lengths)` mask of the allowed lengths among `min_tokens` to `max_tokens`.""" lengths = torch.arange(min_tokens, max_tokens + 1) return torch.isin(lengths, torch.tensor(num_tokens))[None].to(device) class MotionBricksRootStep(ModularPipelineBlocks): model_name = "motionbricks" @property def description(self) -> str: return ( "Root step that chooses the length of the transition among the allowed ones and predicts the root " "trajectory of every frame." ) @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("root_model", AutoModel)] @property def inputs(self) -> list[InputParam]: return [ InputParam( "num_tokens", default=list(range(8, 15)), type_hint=int | list[int], description="The transition lengths the model may choose, in tokens of 4 frames, from 6 to 16. A " "length of `n` tokens gives `4 * n - 6` frames.", ), InputParam( "guide_global_root", required=True, type_hint=torch.Tensor, description="`(batch_size, 8, 5)` normalized global root of the guide poses. Can be generated in the " "guides step.", ), InputParam( "guide_local_root", required=True, type_hint=torch.Tensor, description="`(batch_size, 8, 4)` normalized local root of the guide poses. Can be generated in the " "guides step.", ), InputParam( "guide_pose", required=True, type_hint=torch.Tensor, description="`(batch_size, 8, 268)` normalized pose features of the guide poses. Can be generated in " "the guides step.", ), InputParam( "has_global_root", required=True, type_hint=torch.Tensor, description="`(batch_size, 8)` guide poses with a global root. Can be generated in the guides step.", ), InputParam( "has_local_root", required=True, type_hint=torch.Tensor, description="`(batch_size, 8)` guide poses with a local root. Can be generated in the guides step.", ), InputParam( "has_pose", required=True, type_hint=torch.Tensor, description="`(batch_size, 8)` guide poses with pose features. Can be generated in the guides step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("motion_tokens", type_hint=torch.Tensor, description="`(batch_size,)` chosen lengths."), OutputParam("root", type_hint=torch.Tensor, description="`(batch_size, 64, 5)` normalized global root."), ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) root_model = components.root_model config = root_model.config num_tokens = block_state.num_tokens num_tokens = [num_tokens] if isinstance(num_tokens, int) else list(num_tokens) if not num_tokens or not all(config.min_tokens <= n <= config.max_tokens for n in num_tokens): raise ValueError(f"`num_tokens` must be in {config.min_tokens} to {config.max_tokens}, got {num_tokens}") allowed = allowed_lengths( config.min_tokens, config.max_tokens, tuple(num_tokens), block_state.guide_pose.device ).expand(block_state.guide_pose.shape[0], -1) output = root_model( block_state.guide_global_root, block_state.guide_local_root, block_state.guide_pose, block_state.has_global_root, block_state.has_local_root, block_state.has_pose, allowed, ) block_state.motion_tokens = output.num_tokens block_state.root = output.root self.set_block_state(state, block_state) return components, state class MotionBricksPoseTokensStep(ModularPipelineBlocks): model_name = "motionbricks" @property def description(self) -> str: return ( "Pose step that places the guide poses on the frame timeline and predicts the pose codes of every token " "in one pass of the masked token transformer." ) @property def expected_components(self) -> list[ComponentSpec]: return [ComponentSpec("root_model", AutoModel), ComponentSpec("pose_model", AutoModel)] @property def inputs(self) -> list[InputParam]: return [ InputParam( "guide_local_root", required=True, type_hint=torch.Tensor, description="`(batch_size, 8, 4)` normalized local root of the guide poses. Can be generated in the " "guides step.", ), InputParam( "guide_pose", required=True, type_hint=torch.Tensor, description="`(batch_size, 8, 268)` normalized pose features of the guide poses. Can be generated in " "the guides step.", ), InputParam( "has_local_root", required=True, type_hint=torch.Tensor, description="`(batch_size, 8)` guide poses with a local root. Can be generated in the guides step.", ), InputParam( "has_pose", required=True, type_hint=torch.Tensor, description="`(batch_size, 8)` guide poses with pose features. Can be generated in the guides step.", ), InputParam( "motion_tokens", required=True, type_hint=torch.Tensor, description="`(batch_size,)` motion lengths in tokens. Can be generated in the root step.", ), InputParam( "root", required=True, type_hint=torch.Tensor, description="`(batch_size, 64, 5)` normalized global root. Can be generated in the root step.", ), ] @property def intermediate_outputs(self) -> list[OutputParam]: return [ OutputParam("pose_codes", type_hint=torch.Tensor, description="`(batch_size, 16, 5)` pose codes."), OutputParam("pose_timeline", type_hint=torch.Tensor, description="`(batch_size, 64, 268)` guide poses."), OutputParam("has_pose_timeline", type_hint=torch.Tensor, description="`(batch_size, 64)` given poses."), OutputParam( "local_root", type_hint=torch.Tensor, description="`(batch_size, 64, 4)` normalized local root." ), ] @torch.no_grad() def __call__(self, components, state: PipelineState) -> PipelineState: block_state = self.get_block_state(state) root_model, pose_model = components.root_model, components.pose_model num_tokens, root = block_state.motion_tokens, block_state.root local_root = root_to_local(root_model.denormalize(root.float(), "global_root"), num_tokens) local_root = root_model.normalize(local_root.to(root.dtype), "local_root") # A given local root at the last target pose replaces the derived one. batch = torch.arange(len(num_tokens), device=num_tokens.device) last = num_tokens * FRAMES_PER_TOKEN - 1 given = block_state.has_local_root[:, -1:] local_root[batch, last] = torch.where(given, block_state.guide_local_root[:, -1], local_root[batch, last]) pose_timeline = root_model.place_guides(block_state.guide_pose, num_tokens, fill=0) has_pose_timeline = root_model.place_guides(block_state.has_pose, num_tokens, fill=False) ground_root = torch.cat([root[..., :1], root[..., 2:]], dim=-1) logits = pose_model(pose_timeline, has_pose_timeline, ground_root, num_tokens).logits block_state.pose_codes = logits.argmax(-1) block_state.pose_timeline = pose_timeline block_state.has_pose_timeline = has_pose_timeline block_state.local_root = local_root self.set_block_state(state, block_state) return components, state