feat(model): add bert support. - #4737
Conversation
ac842fc to
be5a2b4
Compare
There was a problem hiding this comment.
Pull request overview
Adds first-class support for Megatron-Core’s encoder-only BERT by introducing a new bert model family bridge/provider that converts Hugging Face MegatronBertForMaskedLM checkpoints to/from Megatron-Core BertModel, plus docs, examples, and tests.
Changes:
- Add
BertBridge+BertModelProviderand register the newbertfamily for AutoBridge discovery. - Fix tied-embedding filtering in conversion to only drop
output_layer.weight(preserving standaloneoutput_layer.biasfor encoder-only MLM heads). - Add BERT documentation, runnable examples (conversion + fill-mask), and unit/functional test coverage.
Reviewed changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit_tests/models/test_model_bridge.py | Adds a regression unit test for tied-embedding filtering to preserve output_layer.bias. |
| tests/unit_tests/models/bert/test_bert_provider.py | Unit tests for BertModelProvider defaults and provide() wiring. |
| tests/unit_tests/models/bert/test_bert_bridge.py | Unit tests for HF↔Megatron config mapping, parameter mapping coverage, and export hook behavior. |
| tests/unit_tests/models/bert/init.py | Establishes BERT unit test package. |
| tests/functional_tests/test_groups/models/bert/test_bert_conversion.py | Functional tests for toy checkpoint creation, parity, and conversion round-trips (single + multi GPU). |
| tests/functional_tests/test_groups/models/bert/init.py | Establishes BERT functional test group package. |
| tests/functional_tests/launch_scripts/h100/active/L0_Launch_models_bert.sh | Adds an L0 launcher for BERT functional tests. |
| src/megatron/bridge/models/conversion/model_bridge.py | Narrows tied-embedding filtering to drop only output_layer.weight. |
| src/megatron/bridge/models/bert/bert_provider.py | Implements Megatron-Core BERT provider configuration + instantiation. |
| src/megatron/bridge/models/bert/bert_bridge.py | Implements HF MegatronBertForMaskedLM ↔ Megatron-Core BertModel bridge and mappings. |
| src/megatron/bridge/models/bert/init.py | Exposes BERT provider and ensures bridge registration side effects. |
| src/megatron/bridge/models/init.py | Re-exports/registers the BERT provider at the package level. |
| README.md | Adds BERT to the supported model families list. |
| examples/models/bert/megatron-bert/README.md | Documents supported architecture and how to obtain/convert checkpoints. |
| examples/models/bert/megatron-bert/inference.sh | Adds a fill-mask inference runner for HF checkpoints / round-tripped exports. |
| examples/models/bert/megatron-bert/fill_mask.py | HF-only fill-mask inference script for sanity checking conversions. |
| examples/models/bert/megatron-bert/conversion.sh | Example conversion + export + round-trip validation script for BERT. |
| docs/models/README.md | Adds BERT to the model docs index and encoder-only section. |
| docs/models/bert/megatron-bert.md | Adds the main BERT variant documentation page. |
| docs/models/bert/index.md | Adds BERT docs landing page and toctree entry. |
| docs/index.md | Includes the BERT docs section in the top-level docs index. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Important: As part of my effort to add strong support for protein language models to megatron, I also created a few PRs in megatron-core to extend the bert model config options: NVIDIA/Megatron-LM#5540 NVIDIA/Megatron-LM#5687 NVIDIA/Megatron-LM#5690 Once these PRs land and the megatron-core dependency is bumped in megatron-bridge, these config options should be exposed for the |
|
Fixed merge conflicts, ready for testing |
622a85f to
9716b37
Compare
@yaoyu-33 I saw you adding the needs-more-tests label, are there specific parts not covered rn that I can add? |
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
Cover the untested second call site of the narrowed tied-embedding filter (build_export_fp8_tasks) and pin the filter's scope so it only drops a trailing output_layer.weight, including the untied-embeddings case where the output weight is a real parameter. Add BERT bridge coverage for dispatch resolution, checkpoint dtype mapping in both directions, vocabulary padding, fused QKV resolution, and the pipeline-stage branch of provide(). Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
9716b37 to
f52b3f7
Compare
|
Rebased branch on current main due to new merge conflicts. |
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
|
/ok to test d0abe02 |
What does this PR do ?
Adds support for the megatron BERT model
MegatronBertForMaskedLM.Changelog
models/bert/:BertBridge,BertProvider, parameter mapping registryconversion/model_bridge.py: fix to tied-embedding filter needed for encoder-only modelsmodels/__init__.py: register thebertfamilydocs/models/bert/) + runnable example (examples/models/bert/megatron-bert/: conversion, inference, fill-mask)GitHub Actions CI
See the CI section in the Contributing doc for how to trigger the CI. A Nvidia developer will need to approve and trigger the CI for external contributors.
Before your PR is "Ready for review"
Pre checks:
If you haven't finished some of the above items you can still open "Draft" PR.
Additional Information