feat(model): add masked lm support. - #4735
Conversation
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR extends the Hugging Face “hf_pretrained” wrapper layer and AutoBridge loading path to recognize encoder-only masked language model architectures (*ForMaskedLM) by introducing a dedicated PreTrainedMaskedLM wrapper and dispatching to it from AutoBridge.from_hf_pretrained.
Changes:
- Add
PreTrainedMaskedLM(lazy-loading wrapper usingAutoModelForMaskedLMwithAutoModelfallback). - Update
AutoBridgeto allowlist*ForMaskedLMand select the appropriate pretrained wrapper class during load. - Add unit tests covering wrapper behavior and AutoBridge dispatch/support semantics.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/unit_tests/models/test_auto_bridge.py | Adds coverage for masked-LM architecture allowlisting and wrapper dispatch behavior. |
| tests/unit_tests/models/hf_pretrained/test_masked_lm.py | New unit tests for PreTrainedMaskedLM lazy-loading, device helpers, and basic API surface. |
| src/megatron/bridge/models/hf_pretrained/README.md | Documents the new PreTrainedMaskedLM wrapper usage. |
| src/megatron/bridge/models/hf_pretrained/masked_lm.py | Implements PreTrainedMaskedLM wrapper (config/tokenizer/model lazy loading, encode/decode, repr, save). |
| src/megatron/bridge/models/hf_pretrained/init.py | Exports PreTrainedMaskedLM from the package namespace. |
| src/megatron/bridge/models/conversion/auto_bridge.py | Adds _resolve_pretrained_wrapper_cls and routes HF loading/isinstance checks through a wrapper-type allowlist. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
yaoyu-33
left a comment
There was a problem hiding this comment.
Overall direction looks good. I found two correctness/contract edges worth addressing inline.
|
|
||
| try: | ||
| model = AutoModelForMaskedLM.from_pretrained(self.model_name_or_path, **model_kwargs) | ||
| except ValueError: |
There was a problem hiding this comment.
Could we avoid treating every ValueError as proof that no masked-LM head exists? from_pretrained can also raise ValueError for invalid loader/config options or failures in a custom MLM implementation; retrying through AutoModel can then silently return a base encoder without the MLM head. Please decide the fallback from the config/model mapping (or narrow this to the unsupported-config case) and re-raise unrelated errors, with a regression test.
| ) as mock_safe_load_config: | ||
| mock_safe_load_config.return_value = bert_masked_lm_config | ||
|
|
||
| assert AutoBridge.can_handle("bert-base-uncased") is True |
There was a problem hiding this comment.
This assertion conflicts with test_from_hf_pretrained_with_masked_lm_architecture_and_no_registered_bridge: BERT has no registered MegatronModelBridge, so can_handle() returns True even though the subsequent load is guaranteed to fail during validation. Since can_handle() is documented as a compatibility preflight, could we include the registry check here (for example by reusing _validate_config) or keep this False until a BERT bridge is registered?
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
|
/ok to test bc62f43 |
|
Hi @bbuschkaemper , |
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com>
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com> # Conflicts: # src/megatron/bridge/models/conversion/auto_bridge.py
Done. Also merged main into branch since new commits on main created merge conflicts. Could you rerun CICD? |
|
/ok to test 9cdb1d3 |
|
Hi @bbuschkaemper , |
Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com>
|
/ok to test e490712 |
Signed-off-by: Björn Buschkämper <bjoern.buschkaemper@gmail.com> Signed-off-by: yaoyu-33 <yaoyu.094@gmail.com> Co-authored-by: yaoyu-33 <yaoyu.094@gmail.com>
What does this PR do ?
Adds
PreTrainedMaskedLM, a lightweight HF-pretrained wrapper for*ForMaskedLMmodels alongside the existingPreTrainedCausalLM.Changelog
hf_pretrained/masked_lm.py: newPreTrainedMaskedLMwrapper classconversion/auto_bridge.py:_resolve_pretrained_wrapper_clsdispatch logic to selectPreTrainedMaskedLMvs.PreTrainedCausalLMby architectureGitHub 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