Honor per-param _is_hf_initialized flags for built-in models in _initialize_weights - #48140
Open
hungnnvidia wants to merge 3 commits into
Open
Honor per-param _is_hf_initialized flags for built-in models in _initialize_weights#48140hungnnvidia wants to merge 3 commits into
_is_hf_initialized flags for built-in models in _initialize_weights#48140hungnnvidia wants to merge 3 commits into
Conversation
…nitialize_weights` `_initialize_weights` skipped `_init_weights` for a module whose params and buffers are all already flagged as initialized only when `is_custom_code` was True. For built-in models the check was dead code. Under FSDP/ZeRO on non-rank-0 processes, `_initialize_missing_keys` flags the loaded params/buffers (which are broadcast from rank 0) but does not set the flag on their enclosing submodules, so `_initialize_weights` re-ran `_init_weights` on every submodule. The guarded `torch.nn.init` wrappers only protect `torch.nn.init.*` calls, not in-place tensor methods such as `weight.data.normal_()`, so this is wasted work on every process and is extremely costly on accelerators where `normal_` is far slower than on CUDA (e.g. Ascend NPU, ~1s vs ~1ms per large tensor). Generalize the per-param/buffer check to all models. Modules with any unflagged param/buffer (missing keys, non-persistent buffers) are still initialized, so the standard loading path is unchanged. Fixes huggingface#47427
Contributor
|
Thank you for your contribution 🤗! CI Security Gate — automatic approval blockedThis PR was not automatically approved for CI because the security gate failed. Possible reasons:
See the workflow run for the exact violations. A maintainer can review and manually approve CI if a finding is a false positive. |
The `test_unexpected_keys_warnings` expected output embedded a literal ESC control byte in the ANSI color code. The security-gate workflow fetches changed .py files via `gh api`, which aborts on raw terminal escape sequences. Write it as the equivalent `\x1b` escape (identical runtime value).
…regression) Skipping `_init_weights` via a generalized per-param check in `_initialize_weights` changed the standard `from_pretrained` init path and broke `test_can_init_all_missing_weights` (e.g. BridgeTower `class_embedding`). Revert `_initialize_weights` to its original form and instead complete the existing intent of `_initialize_missing_keys`: on non-rank-0 FSDP/ZeRO processes, propagate `_is_hf_initialized` to every submodule whose params and buffers are all already flagged (new `_mark_fully_loaded_submodules_as_initialized` helper), so `_init_weights` is skipped there without touching the standard path. Modules with unflagged (e.g. non-persistent) buffers are still re-initialized.
Contributor
CI recapDashboard: View test results in Grafana |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
Fixes #47427.
_initialize_weightsonly honored the per-parameter/buffer_is_hf_initializedflags whenis_custom_codewasTrue, so for built-in models the check was dead code.Under FSDP/ZeRO on non-rank-0 processes,
_initialize_missing_keysflags the loaded params/buffers (which are broadcast from rank 0) but does not set the flag on their enclosing submodules. The guardedtorch.nn.initwrappers only protecttorch.nn.init.*calls, not in-place tensor methods such asweight.data.normal_(), so_init_weightsredundantly re-ran on every submodule. This is wasted work on every process and is extremely costly on accelerators wherenormal_is far slower than on CUDA (the reporter measured ~224s wasted on 16x Ascend NPU).This generalizes the per-param/buffer skip to all models. Modules with any unflagged param/buffer (missing keys, non-persistent buffers) are still initialized, so the standard loading path is numerically unchanged.
Tests
Added
tests/utils/test_modeling_utils.py::ModelUtilsTest::test_initialize_weights_skips_module_with_all_params_flagged, which reproduces the non-rank-0 flag state and asserts_init_weightsis skipped for built-in models (and still runs when a param is unflagged). It fails onmainand passes with this change. Existing BERT and CLIP init/save-load tests still pass locally.Before submitting
_is_hf_initializedper-param check doesn't skip redundant init for built-in models on non-CUDA hardware #47427.Who can review?
Anyone in the community is free to review. cc @Cyrilvallez