-
Notifications
You must be signed in to change notification settings - Fork 1
Add MoMa (Mixture of Modality-Aware Experts) VLM architecture #107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4e2b56
Add MoMa (Mixture of Modality-Aware Experts) VLM architecture
amazloumi bf13b36
Merge remote-tracking branch 'origin/main' into moma-arch
amazloumi d6f72ee
MoMa post-review: validate modality_ids, expose expert counts, note A…
amazloumi a75c6f8
fixing pyright ckeck
amazloumi 8dce33f
resovling the above comments
amazloumi 9a6fa81
Merge branch 'main' into moma-arch
amazloumi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| # 7B VLM Mixture of Modality-Aware Experts (MoMa) on 4x H200 (141 GB/GPU). | ||
| # | ||
| # Mode: smoke / starter. | ||
| # | ||
| # Shared Q/K/V/O attention (one set across modalities) + per-modality MoE | ||
| # FFN groups with expert-choice + Sigmoid routing (Lin et al. 2024, | ||
| # arXiv:2407.21770). Default 4 text + 4 image experts per layer | ||
| # (paper's optimal moe_4t4i). Tokens route deterministically by modality | ||
| # (level 1) then through a learned EC + Sigmoid router within their | ||
| # modality group (level 2). Image tokens prepend the text sequence | ||
| # (same residual layout as Joint-Decoder / MoT); output_slice trims | ||
| # them off the LM head input. | ||
| # | ||
| # Inference note: MoMa v1 supports training only. Expert-choice routing | ||
| # is non-causal (each expert's top-k depends on all tokens in the batch); | ||
| # autoregressive generation requires auxiliary routers (paper §2.4), | ||
| # deferred to a follow-up. | ||
| # | ||
| # Parameter / memory note: with the default 7B-dense-shaped backbone | ||
| # (dim=4096, n_layers=32, ffn ~14336) and 8 SwiGLU experts per layer | ||
| # (4 image + 4 text), total params is much larger than dense 7B. Fitting | ||
| # on 4x H200 today needs FSDP=4 plus reducing moma_experts_per_modality | ||
| # (e.g. 2t2i), or falling back to the MoT debug config — | ||
| # activation_checkpointing="full" is set below but is currently a no-op | ||
| # for MoMa (see the comment near the field). Pipeline Parallel + VLM is | ||
| # not supported. | ||
| # | ||
| # max_seq_len allocation: residual_image_tokens + max_text_len. Image | ||
| # tokens prepend the text sequence in the residual stream, so the budget | ||
| # must cover both modalities. | ||
| # | ||
| # Usage: | ||
| # uv run torchrun --nproc_per_node=4 scripts/train.py configs/train/vlm_7b_moma.toml | ||
| # | ||
| # Default points at a 30-sample COCO val substitute (sayakpaul/coco-30-val-2014) | ||
| # so a fresh clone runs without external setup. For real training override: | ||
| # --data.hf_dataset_name=/path/to/local/hf_dataset \ | ||
| # --data.hf_dataset_text_field=caption | ||
| # Swap the encoder via [vision_encoder].type = "siglip2" / "clip". | ||
|
|
||
| [model] | ||
| dim = 4096 | ||
| n_layers = 32 | ||
| n_heads = 32 | ||
| n_kv_heads = 8 | ||
| vocab_size = 50257 | ||
| ffn_dim_multiplier = 1.3 | ||
| norm_type = "rmsnorm" | ||
| activation = "silu" | ||
| max_seq_len = 1024 # 256 image + 512 text (max_text_len default) + headroom | ||
| rope_theta = 500000.0 | ||
| tie_embeddings = false | ||
|
|
||
| [vision_encoder] | ||
| type = "random" | ||
| feature_dim = 1024 | ||
| num_tokens = 256 | ||
|
|
||
| [vlm] | ||
| arch = "moma" | ||
| # Paper Eq. 5: Gumbel-Sigmoid noise on router scores during training. | ||
| # Set false for a deterministic forward (useful for warm-start parity | ||
| # checks and reproducibility-sensitive smoke runs). | ||
| moma_gumbel_noise = true | ||
| # moma_capacity_factor = 0.0 → use paper default 1/|E^M| per modality | ||
| # (each expert sees the average token load; perfect EC balance). | ||
|
|
||
| [vlm.moma_experts_per_modality] | ||
| # Paper's optimal balanced allocation (moe_4t4i at 1.4B-compute-matched | ||
| # in Table 1). Unbalanced allocations like {image = 1, text = 7} are | ||
| # supported and match the paper's moe_7t1i / 1t7i ablations. | ||
| image = 4 | ||
| text = 4 | ||
|
|
||
| [train] | ||
| batch_size = 4 | ||
| seq_len = 768 | ||
| max_steps = 200 | ||
| grad_accum_steps = 1 | ||
| grad_clip_norm = 1.0 | ||
| seed = 42 | ||
| # Modality-aware scatter/gather + EC top-k are not yet validated under | ||
| # torch.compile (graph breaks on data-dependent dispatch); JobConfig.validate | ||
| # emits a warning if you flip this on. | ||
| compile_model = false | ||
| # NOTE: AC=full is currently a no-op for MoMa — kempnerforge.distributed.parallel.apply_ac | ||
| # matches `isinstance(m, TransformerBlock)` only, and MoMaBlock is a sibling nn.Module | ||
| # (same gap exists for MoTBlock and CrossAttentionBlock). The follow-up PR will refactor | ||
| # apply_ac to iterate ``transformer.layers`` directly so AC works across all VLM arches. | ||
| # Until then this line has no effect; OOM risk on tighter GPUs is real and unmitigated. | ||
| # Intended once apply_ac is refactored — at that point this will checkpoint every | ||
| # MoMaBlock per layer and recover the memory budget needed for the per-layer expert | ||
| # duplication on 4x H200. Currently inert (see NOTE above). | ||
| activation_checkpointing = "full" | ||
| loss_fn = "cross_entropy" | ||
|
|
||
| [optimizer] | ||
| name = "adamw" | ||
| lr = 1e-4 | ||
| weight_decay = 0.1 | ||
| betas = [0.9, 0.95] | ||
| eps = 1e-8 | ||
| fused = true | ||
|
|
||
| [scheduler] | ||
| name = "cosine" | ||
| warmup_steps = 5 | ||
| min_lr_ratio = 0.1 | ||
|
|
||
| [data] | ||
| hf_dataset_name = "sayakpaul/coco-30-val-2014" | ||
| hf_dataset_split = "train" | ||
| hf_dataset_image_field = "image" | ||
| hf_dataset_text_field = "caption" | ||
| hf_image_size = 224 | ||
| tokenizer_path = "gpt2" | ||
| num_workers = 2 | ||
| pin_memory = true | ||
| prefetch_factor = 2 | ||
|
|
||
| [distributed] | ||
| dp_shard = -1 | ||
| nccl_timeout_sec = 600 | ||
|
|
||
| [checkpoint] | ||
| dir = "checkpoints/vlm_7b_moma" | ||
| interval = 1000 | ||
| keep_last_n = 1 | ||
|
|
||
| [metrics] | ||
| log_interval = 1 | ||
| enable_wandb = false | ||
| enable_tensorboard = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # VLM smoke config — Mixture of Modality-Aware Experts (MoMa) arch, | ||
| # tiny LLM + random vision encoder. | ||
| # | ||
| # Mode: smoke. | ||
| # | ||
| # Runs end-to-end in <2 minutes on 1 GPU. Uses RandomVisionEncoder so no | ||
| # HF download is needed. Shared Q/K/V/O self-attention + per-modality | ||
| # MoE FFN groups with expert-choice + Sigmoid routing (Lin et al. 2024, | ||
| # arXiv:2407.21770). 2 text experts + 2 image experts at every layer. | ||
| # | ||
| # max_seq_len allocation: residual_image_tokens + max_text_len. Image | ||
| # tokens prepend the text sequence in the residual stream (same layout | ||
| # as Joint-Decoder / MoT). | ||
| # | ||
| # Note: MoMa v1 supports training only. Expert-choice routing is | ||
| # non-causal; autoregressive generation requires auxiliary routers | ||
| # (paper §2.4), deferred to a follow-up. | ||
| # | ||
| # Usage: | ||
| # uv run python scripts/train.py configs/train/vlm_debug_moma.toml \ | ||
| # --data.hf_dataset_name=... --data.tokenizer_path=gpt2 | ||
|
|
||
| [model] | ||
| dim = 256 | ||
| n_layers = 4 | ||
| n_heads = 4 | ||
| n_kv_heads = 4 | ||
| vocab_size = 50257 # gpt2 vocab | ||
| max_seq_len = 576 # 64 image + 512 text | ||
| norm_type = "rmsnorm" | ||
| activation = "silu" | ||
|
|
||
| [vision_encoder] | ||
| type = "random" | ||
| feature_dim = 384 | ||
| num_tokens = 64 | ||
|
|
||
| [vlm] | ||
| arch = "moma" | ||
|
|
||
| [vlm.moma_experts_per_modality] | ||
| image = 2 | ||
| text = 2 | ||
|
|
||
| [train] | ||
| batch_size = 2 | ||
| seq_len = 576 | ||
| max_steps = 50 | ||
| grad_accum_steps = 1 | ||
| grad_clip_norm = 1.0 | ||
| seed = 42 | ||
| compile_model = false | ||
| activation_checkpointing = "none" | ||
|
|
||
| [optimizer] | ||
| name = "adamw" | ||
| lr = 3e-4 | ||
| weight_decay = 0.1 | ||
| betas = [0.9, 0.95] | ||
| fused = false | ||
|
|
||
| [scheduler] | ||
| name = "cosine" | ||
| warmup_steps = 5 | ||
| min_lr_ratio = 0.1 | ||
|
|
||
| [data] | ||
| # 30-sample COCO val substitute (sayakpaul/coco-30-val-2014). For a real | ||
| # training run, override via CLI: --data.hf_dataset_name=<your-dataset> | ||
| hf_dataset_name = "sayakpaul/coco-30-val-2014" | ||
| hf_dataset_split = "train" | ||
| hf_dataset_image_field = "image" | ||
| hf_dataset_text_field = "caption" | ||
| hf_image_size = 224 | ||
| tokenizer_path = "gpt2" | ||
| num_workers = 2 | ||
| pin_memory = true | ||
|
|
||
| [distributed] | ||
| dp_shard = -1 | ||
|
|
||
| [checkpoint] | ||
| dir = "checkpoints/vlm_debug_moma" | ||
| interval = 25 | ||
| keep_last_n = 2 | ||
|
|
||
| [metrics] | ||
| log_interval = 5 | ||
| enable_wandb = false | ||
| enable_tensorboard = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The MoMa branch of
JobConfig.validate(job.py:225-247) already warns oncompile_model(lines 241-247). The AC=full no-op fits the same precedent but is silent: a user authoring a fresh MoMa config without reading vlm_7b_moma.toml's NOTE block gets no warning that activation checkpointing has no effect.Add a sibling warning right after the compile block (
import loggingis already in scope there):Trigger only on "full", not != "none": apply_ac's selective branch (parallel.py:120) uses
isinstance(m, Attention), andMoMaBlock.attentionis a standard Attention (moma.py:401), so selective mode does wrap MoMa correctly. Only full is broken.Could ride on the upcoming apply_ac refactor PR, or land as a small patch here.