Skip to content

Commit b646a67

Browse files
le1nuxclaude
andcommitted
docs(model): attribute adopted code in the Nemotron implementation
Modalities is MIT licensed; Megatron-LM, Megatron-Bridge and state-spaces/mamba are Apache 2.0 (NVIDIA / Tri Dao, Albert Gu) and TorchTitan is BSD 3-Clause. Preserving those notices in the derived files is a license obligation, so each source file that adapts upstream code now carries a header naming the upstream file, what was taken from it, the copyright holder and the license. Marked as adapted from Megatron-LM: * mamba2_mixer.py - packed [z, x, B, C, dt] projection layout, conv1d/A_log/D/ dt_bias shapes and initialization distributions * layer_pattern.py - the layer pattern symbols * nemotron_layers.py - single-operator pre-norm residual layer structure * nemotron_layer_specs.py - the declarative ModuleSpec/builder pattern * nemotron_model.py - hybrid model structure * router.py - sigmoid scoring, selection-only expert bias, renormalization * moe.py - the load-balancing loss formula and its sequence-level variant * load_balancing.py - the sign-based expert bias update rule * experts.py, nemotron_mlp.py - the squared ReLU activation Marked as adapted from state-spaces/mamba: * ssd.py - the chunk-parallel SSD block decomposition (ssd_minimal_discrete / segsum) and the GatedRMSNorm semantics Marked as adapted from TorchTitan (following the convention already used in model_factory.py and trainer.py): * experts.py - stacked per-expert weights and the torch._grouped_mm call * moe.py - dispatch/combine structure, expert-bias and token-count buffers * router.py - router interface shape * load_balancing.py - the optimizer step pre-hook approach * nemotron_stages_generator.py - pipeline split-point structure Marked as adapted from Meta's Llama: * nemotron_attention.py - grouped-query key/value head repetition The YAML configs note that their hyperparameter values are adopted from the Megatron-Bridge recipe; no code was taken from Megatron-Bridge. docs/components/nemotron.md gains an Attribution section tabulating all of the above in one place, and explicitly lists the files with no third-party derivation (norms.py, nemotron_model_factory.py, nemotron_mfu.py, and the partitioning algorithm in nemotron_stages_generator.py). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 96c00eb commit b646a67

18 files changed

Lines changed: 150 additions & 1 deletion

File tree

‎config_files/training/config_nemotron3_nano_30b_a3b_fsdp2.yaml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Nemotron-3 Nano 30B-A3B pretraining configuration.
22
#
33
# Architecture from the model report (arXiv:2512.20848, Table 1 / Figure 2) and cross-checked
4-
# against the Megatron-Bridge recipe `recipes/nemotronh/h100/nemotron_3_nano.py`:
4+
# against the Megatron-Bridge recipe `src/megatron/bridge/recipes/nemotronh/h100/nemotron_3_nano.py`
5+
# (Copyright (c) 2026, NVIDIA CORPORATION, licensed under the Apache License, Version 2.0), from
6+
# which the hyperparameter values below are adopted:
57
#
68
# 52 layers pattern MEMEM*EMEMEM*EMEMEM*EMEMEM*EMEMEM*EMEMEMEM*EMEMEMEME
79
# -> 23 Mamba-2, 23 MoE, 6 attention

‎docs/components/nemotron.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,58 @@ Decaying the SSM dynamics parameters or the router gate destabilizes training.
177177
| `stages_generator` | `nemotron_stages_generator` | [NemotronStagesGenerator](../../src/modalities/models/nemotron/nemotron_stages_generator.py) |
178178
| `mfu_calculator` | `nemotron` | [NemotronMFUCalculator](../../src/modalities/utils/nemotron_mfu.py) |
179179
| `model_initialization` | `composed` (`model_type: nemotron`) | [ComposedInitializationRoutines](../../src/modalities/nn/model_initialization/composed_initialization.py) |
180+
181+
## Attribution
182+
183+
Modalities is MIT licensed. The files below adapt code from third-party projects and carry the
184+
corresponding notices in their file headers, as required by those licenses.
185+
186+
**NVIDIA Megatron-LM** — Copyright (c) NVIDIA CORPORATION, Apache License 2.0.
187+
Portions additionally Copyright (c) 2024, Tri Dao, Albert Gu.
188+
189+
| File | What was adapted | Upstream |
190+
|------|------------------|----------|
191+
| [mamba2_mixer.py](../../src/modalities/models/components/mamba2/mamba2_mixer.py) | Packed `[z, x, B, C, dt]` projection layout; `conv1d`/`A_log`/`D`/`dt_bias` shapes and init distributions | `megatron/core/ssm/mamba_mixer.py` |
192+
| [layer_pattern.py](../../src/modalities/models/nemotron/layer_pattern.py) | Layer pattern symbols (`M`, `*`, `E`, `-`) | `megatron/core/models/hybrid/hybrid_layer_allocation.py::Symbols` |
193+
| [nemotron_layers.py](../../src/modalities/models/nemotron/nemotron_layers.py) | Single-operator pre-norm residual layer structure | `megatron/core/ssm/mamba_layer.py`, `.../hybrid/hybrid_block.py` |
194+
| [nemotron_layer_specs.py](../../src/modalities/models/nemotron/nemotron_layer_specs.py) | Declarative layer-spec/builder pattern | `megatron/core/transformer/spec_utils.py::ModuleSpec`, `.../hybrid/hybrid_layer_specs.py` |
195+
| [nemotron_model.py](../../src/modalities/models/nemotron/nemotron_model.py) | Hybrid model structure (pattern-driven stack) | `megatron/core/models/hybrid/hybrid_model.py` |
196+
| [router.py](../../src/modalities/models/components/moe/router.py) | Sigmoid scoring, selection-only expert bias, top-k renormalization | `megatron/core/transformer/moe/moe_utils.py::topk_routing_with_score_function` |
197+
| [moe.py](../../src/modalities/models/components/moe/moe.py) | Load-balancing loss formula and its sequence-level variant | `.../moe_utils.py::switch_load_balancing_loss_func` |
198+
| [load_balancing.py](../../src/modalities/models/components/moe/load_balancing.py) | Sign-based expert bias update rule | `.../moe_utils.py::get_updated_expert_bias` |
199+
| [experts.py](../../src/modalities/models/components/moe/experts.py), [nemotron_mlp.py](../../src/modalities/models/nemotron/nemotron_mlp.py) | Squared ReLU activation | `megatron/core/activations.py` |
200+
201+
**state-spaces/mamba** — Copyright (c) 2024, Tri Dao, Albert Gu, Apache License 2.0.
202+
203+
| File | What was adapted | Upstream |
204+
|------|------------------|----------|
205+
| [ssd.py](../../src/modalities/models/components/mamba2/ssd.py) | Chunk-parallel SSD block decomposition (`ssd_minimal_discrete` / `segsum`); `GatedRMSNorm` semantics | `ssd_minimal_discrete`, `mamba_ssm.ops.triton.layernorm_gated.rmsnorm_fn` |
206+
207+
**Meta TorchTitan** — BSD 3-Clause License. (Modalities already adapts from TorchTitan elsewhere.)
208+
209+
| File | What was adapted | Upstream |
210+
|------|------------------|----------|
211+
| [experts.py](../../src/modalities/models/components/moe/experts.py) | Stacked per-expert weight layout; `torch._grouped_mm` over expert-sorted tokens | `torchtitan/models/common/moe.py::GroupedExperts` |
212+
| [moe.py](../../src/modalities/models/components/moe/moe.py) | Dispatch/combine structure; expert-bias and token-count buffers | `.../moe.py::MoE` |
213+
| [router.py](../../src/modalities/models/components/moe/router.py) | Router interface shape | `.../moe.py::TokenChoiceTopKRouter` |
214+
| [load_balancing.py](../../src/modalities/models/components/moe/load_balancing.py) | Applying the bias update as an optimizer step pre-hook | `.../moe.py` |
215+
| [nemotron_stages_generator.py](../../src/modalities/models/nemotron/nemotron_stages_generator.py) | Pipeline split-point structure (via Modalities' own `StagesGenerator`) | TorchTitan pipeline utilities |
216+
217+
**Meta Llama** — [facebookresearch/llama](https://github.com/facebookresearch/llama).
218+
219+
| File | What was adapted | Upstream |
220+
|------|------------------|----------|
221+
| [nemotron_attention.py](../../src/modalities/models/nemotron/nemotron_attention.py) | Grouped-query key/value head repetition (`_repeat_kv`), via Modalities' own GPT-2 | `llama/model.py` |
222+
223+
**NVIDIA Megatron-Bridge** — Copyright (c) 2026, NVIDIA CORPORATION, Apache License 2.0.
224+
The hyperparameter values in
225+
[config_nemotron3_nano_30b_a3b_fsdp2.yaml](../../config_files/training/config_nemotron3_nano_30b_a3b_fsdp2.yaml)
226+
and the two Nemotron test configs are adopted from
227+
`src/megatron/bridge/recipes/nemotronh/h100/nemotron_3_nano.py` and cross-checked against the model
228+
report. No code was taken from Megatron-Bridge; it served as the configuration reference, and
229+
`src/megatron/bridge/models/nemotronh/nemotron_h_bridge.py` documents the HF parameter mapping that
230+
guided the module naming.
231+
232+
Files with **no** third-party derivation (written for Modalities, structured after its existing
233+
GPT-2 components): `norms.py`, `nemotron_model_factory.py`, `nemotron_mfu.py`, and the
234+
partitioning algorithm in `nemotron_stages_generator.py`.

‎src/modalities/models/components/mamba2/mamba2_mixer.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Portions of this file are adapted from NVIDIA's Megatron-LM
2+
# (megatron/core/ssm/mamba_mixer.py): the packed [z, x, B, C, dt] input-projection layout, the
3+
# conv1d / A_log / D / dt_bias parameter shapes, and their initialization distributions.
4+
# Copyright (c) 2024, NVIDIA CORPORATION. Copyright (c) 2024, Tri Dao, Albert Gu.
5+
# Licensed under the Apache License, Version 2.0.
6+
17
"""Mamba-2 mixer, the sequence-mixing operator of hybrid Mamba-Transformer models.
28
39
The layout follows the Megatron-LM reference implementation

‎src/modalities/models/components/mamba2/ssd.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Portions of this file are adapted from the Mamba-2 reference implementation in
2+
# state-spaces/mamba (https://github.com/state-spaces/mamba): the chunk-parallel SSD block
3+
# decomposition follows `ssd_minimal_discrete` / `segsum`, and GatedRMSNorm reproduces the
4+
# semantics of `mamba_ssm.ops.triton.layernorm_gated.rmsnorm_fn` with norm_before_gate=False.
5+
# Copyright (c) 2024, Tri Dao, Albert Gu. Licensed under the Apache License, Version 2.0.
6+
17
"""Pure-PyTorch Mamba-2 primitives (state space dual / SSD).
28
39
This module provides a dependency-free, CPU-runnable and ``torch.compile``-friendly

‎src/modalities/models/components/moe/experts.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Some portions of this implementation are inspired, adapted, or refactored from Meta's
2+
# open-source project TorchTitan (torchtitan/models/common/moe.py::GroupedExperts),
3+
# licensed under the BSD 3-Clause License. Specifically the stacked per-expert weight
4+
# layout and the torch._grouped_mm call over expert-sorted tokens.
5+
#
6+
# The squared ReLU activation follows NVIDIA's Megatron-LM (megatron/core/activations.py),
7+
# Copyright (c) NVIDIA CORPORATION. Licensed under the Apache License, Version 2.0.
8+
19
"""Grouped expert feed-forward networks for mixture-of-experts layers.
210
311
All experts of a layer are stored in two stacked weight tensors so that the whole layer can be

‎src/modalities/models/components/moe/load_balancing.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Portions of this file are adapted from NVIDIA's Megatron-LM
2+
# (megatron/core/transformer/moe/moe_utils.py::get_updated_expert_bias): the sign-based
3+
# per-expert bias update rule.
4+
# Copyright (c) 2025, NVIDIA CORPORATION. Licensed under the Apache License, Version 2.0.
5+
#
6+
# Applying the update as an optimizer step pre-hook follows Meta's open-source project TorchTitan
7+
# (torchtitan/models/common/moe.py), licensed under the BSD 3-Clause License.
8+
19
"""Auxiliary-loss-free load balancing for mixture-of-experts layers.
210
311
Implements the update rule of "Auxiliary-Loss-Free Load Balancing Strategy for Mixture-of-Experts"

‎src/modalities/models/components/moe/moe.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# Portions of this file are adapted from NVIDIA's Megatron-LM
2+
# (megatron/core/transformer/moe/moe_utils.py::switch_load_balancing_loss_func): the
3+
# load-balancing loss formula E * sum_i(f_i * P_i) and its sequence-level variant.
4+
# Copyright (c) 2025, NVIDIA CORPORATION. Licensed under the Apache License, Version 2.0.
5+
#
6+
# The dispatch/combine structure and the expert-bias / token-count buffers are inspired by Meta's
7+
# open-source project TorchTitan (torchtitan/models/common/moe.py::MoE), licensed under the
8+
# BSD 3-Clause License.
9+
110
"""Mixture-of-experts feed-forward layer.
211
312
The layer combines a :class:`TopKRouter`, a :class:`GroupedExperts` stack and an optional always-on

‎src/modalities/models/components/moe/router.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# Portions of this file are adapted from NVIDIA's Megatron-LM
2+
# (megatron/core/transformer/moe/moe_utils.py::topk_routing_with_score_function): the sigmoid
3+
# scoring path, the selection-only expert bias, and the top-k renormalization.
4+
# Copyright (c) 2025, NVIDIA CORPORATION. Licensed under the Apache License, Version 2.0.
5+
#
6+
# The router interface is additionally inspired by Meta's open-source project TorchTitan
7+
# (torchtitan/models/common/moe.py::TokenChoiceTopKRouter), licensed under the BSD 3-Clause License.
8+
19
"""Top-k mixture-of-experts router.
210
311
Implements the routing scheme used by Nemotron-3 Nano: sigmoid gating with an additive

‎src/modalities/models/nemotron/layer_pattern.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# The layer-pattern symbols are adopted from NVIDIA's Megatron-LM
2+
# (megatron/core/models/hybrid/hybrid_layer_allocation.py::Symbols) so that pattern strings are
3+
# portable between the two frameworks.
4+
# Copyright (c) 2024-2026, NVIDIA CORPORATION. Licensed under the Apache License, Version 2.0.
5+
16
"""Layer pattern parsing for hybrid Mamba-Transformer models.
27
38
A hybrid model such as Nemotron-3 Nano is described by a pattern string in which every

‎src/modalities/models/nemotron/nemotron_attention.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# The grouped-query key/value head repetition follows Meta's Llama implementation
2+
# (https://github.com/facebookresearch/llama), as already adapted in
3+
# modalities/models/gpt2/gpt2_model.py::CausalSelfAttention._repeat_kv.
4+
15
"""Grouped-query causal self-attention with a head dimension decoupled from the model dimension.
26
37
This cannot reuse the GPT2 attention module: GPT2 derives the head dimension as

0 commit comments

Comments
 (0)