mstar.model.cosmos3.components.packing#
Joint-sequence packing for Cosmos3 generation (ported from the diffusers
Cosmos3OmniPipeline).
Pure, stateless primitives that turn a prompt + latent shape into the transformer’s per-step inputs: the 3D interleaved mRoPE position ids, the text/vision segment layouts, and the chat-template tokenization. Shared by the t2i pipeline and the engine submodule’s input preprocessing. Reproduces the diffusers pipeline’s packed t2i inputs byte-for-byte.
Functions
|
Clean (conditioning) action tokens for |
|
mRoPE start-frame offset for the action band: action chunks of length |
|
Assemble the static transformer inputs for joint video+action generation. |
|
Sound (AVAE-latent) band of the generation sequence: a |
|
Assemble the per-prompt static transformer inputs for image/video generation. |
|
Image-mode convenience wrapper around |
|
|
|
|
|
Action tokens: a frame-rate |
|
Text tokens: all three axes share the same increasing ids from |
|
Vision/sound (VAE) tokens: (t, h, w) grid ids, with optional fps modulation of the temporal axis (only when |
|
Normalize a request's video-conditioning latent-frame indexes. |
|
Return |
|
Clean (conditioning) latent video frames for |
- mstar.model.cosmos3.components.packing.action_condition_frame_indexes(mode, action_length)[source]#
Clean (conditioning) action tokens for
mode.
- mstar.model.cosmos3.components.packing.action_start_frame_offset(action_length, video_length)[source]#
mRoPE start-frame offset for the action band: action chunks of length
num_frames - 1start one frame in (aligned to predicted frames 1..N); a fullnum_frameschunk starts at 0.
- mstar.model.cosmos3.components.packing.build_action_static_inputs(input_ids, video_latent_shape, action_chunk_size, mode, config, vae_scale_factor_temporal, fps, action_fps, action_start_offset, device)[source]#
Assemble the static transformer inputs for joint video+action generation.
The generation sequence is
[video tokens | action tokens]after the text prefix. Both media bands share the post-text temporal offset (the 15000 margin), with the action band on its own frame-rate grid. Conditioning permodedecides which video frames and action tokens are clean context vs noisy/predicted (seevision_condition_frame_indexes()/action_condition_frame_indexes()).
- mstar.model.cosmos3.components.packing.build_sound_segment(sound_latent_frames, mrope_offset, curr, config, device)[source]#
Sound (AVAE-latent) band of the generation sequence: a
(T, 1, 1)grid at the sound tokenizer’s latent frame rate, sharing the post-text media temporal offset with the vision band. Every sound frame is noisy/predicted (sound generation has no clean conditioning tokens).
- mstar.model.cosmos3.components.packing.build_static_inputs(input_ids, latent_shape, config, vae_scale_factor_temporal, fps, device, has_image_condition=False, sound_latent_frames=None, noisy_frames=None)[source]#
Assemble the per-prompt static transformer inputs for image/video generation.
latent_shapeis[B, C, T, H, W](T == 1for images;T == 1 + (num_frames - 1) // temporal_factorfor video). Whenhas_image_conditionis set, latent frame 0 is a clean conditioning anchor (image-to-video): it stays in the sequence but is excluded from the noisy / predicted frames.noisy_framesinstead lists the predicted latent frames explicitly (video-to-video, where the conditioned frames are the complement).sound_latent_framesappends a jointly denoised sound band after the vision tokens (the generation block becomes[vision | sound]). Step-varying fields (vision_tokens,vision_timesteps) are spliced in per denoising step by the caller.
- mstar.model.cosmos3.components.packing.build_t2i_static_inputs(input_ids, latent_shape, config, vae_scale_factor_temporal, fps, device)[source]#
Image-mode convenience wrapper around
build_static_inputs().
- mstar.model.cosmos3.components.packing.build_vision_segment(latent_shape, has_image_condition, mrope_offset, vision_fps, curr, config, vae_scale_factor_temporal, device, noisy_frames=None)[source]#
latent_shapeis the vision latent tensor shape[B, C, T, H, W].noisy_frameslists the latent frames that are noisy (predicted); the rest are clean conditioning context. WhenNoneit defaults to frame 0 clean ifhas_image_conditionelse all frames noisy — i.e. the t2i/t2v/i2v layouts. Action modes pass an explicit list (e.g.[]for inverse-dynamics, where the whole video is conditioning).
- mstar.model.cosmos3.components.packing.get_3d_mrope_ids_action_tokens(grid_t, temporal_offset, action_fps, base_fps=24.0, base_temporal_compression_factor=4, start_frame_offset=1)[source]#
Action tokens: a frame-rate
(T, 1, 1)temporal grid sharing the media offset with the vision band. The action stream is uncompressed in time (temporal_compression_factor=1) but its rate is expressed in the same base-fps units as the vision latents (base_temporal_compression_factor), so an action chunk lines up temporally with the conditioning video.
- mstar.model.cosmos3.components.packing.get_3d_mrope_ids_text_tokens(num_tokens, temporal_offset, use_float_positions=False)[source]#
Text tokens: all three axes share the same increasing ids from
temporal_offset.
- mstar.model.cosmos3.components.packing.get_3d_mrope_ids_vae_tokens(grid_t, grid_h, grid_w, temporal_offset, reset_spatial_indices=True, fps=None, base_fps=24.0, temporal_compression_factor=4, base_temporal_compression_factor=None, start_frame_offset=0)[source]#
Vision/sound (VAE) tokens: (t, h, w) grid ids, with optional fps modulation of the temporal axis (only when
fpsis set andgrid_t > 1).
- mstar.model.cosmos3.components.packing.normalize_condition_frame_indexes(value, default)[source]#
Normalize a request’s video-conditioning latent-frame indexes.
Accepts an int, a comma-separated string, or an iterable of ints; returns a sorted, de-duplicated tuple.
Nonefalls back todefault.
- mstar.model.cosmos3.components.packing.tokenize_prompt(tokenizer, prompt, negative_prompt, num_frames, height, width, fps=24.0, use_system_prompt=True, add_resolution_template=True, add_duration_template=True, max_sequence_length=None)[source]#
Return
(cond_input_ids, uncond_input_ids)for image/video generation.Mirrors the diffusers pipeline: apply the Qwen2 chat template with the mode-specific system prompt and metadata sentences (duration for video, then resolution), using inverse templates for the negative prompt, then append the eos + start-of-generation (
<|vision_start|>) special tokens. Image mode isnum_frames == 1.max_sequence_lengthcaps the chat-template token count BEFORE the two trailing markers (the reference serving pipeline’s truncation contract);Noneleaves the prompt unbounded.