Skip to content

[AutoDeploy] correctly identifying how to load HF checkpoint weights - #23

Closed
lucaslie wants to merge 5 commits into
feat/ad_coverage_week1from
ll/hf_weight_loading_robust
Closed

lucaslie wants to merge 5 commits into
feat/ad_coverage_week1from
ll/hf_weight_loading_robust

Conversation

@lucaslie

@lucaslie lucaslie commented May 21, 2025 •

Copy link
Copy Markdown
  • Our current load logic (recommended meta device/hf accelerate approach) requires us to correctly identify which type of checkpoint we want to load from a HF checkpoint directory (safetsensors or bin). It won't choose for us. --> This will correctly filter for the approach prioritizing safetsensors over bin

  • In addition this PR will also ensure that we don't allocate too much memory during checkpoint loading for sharded models. The added comments in the PR give more details about that.

  • It also includes Removed load_state_dict() with assign=True #22

@lucaslie lucaslie self-assigned this May 21, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the HF checkpoint loading logic by introducing methods to better determine which checkpoint file to use, prioritizing safetensors over bin files.

  • Introduced _get_ignore_patterns to dynamically generate file ignore patterns based on repository contents.
  • Added _get_checkpoint_file to select the most relevant checkpoint file from a given directory.
  • Updated prefetch_checkpoint to leverage the new methods for improved checkpoint handling.
Comments suppressed due to low confidence (1)

tensorrt_llm/_torch/auto_deploy/models/hf.py:223

  • Ensure that you add unit tests covering various scenarios for checkpoint selection logic in _get_checkpoint_file to safeguard against regression.
def _get_checkpoint_file(self, checkpoint: Union[str, os.PathLike]) -> Union[str, os.PathLike]:

@lucaslie
lucaslie force-pushed the ll/hf_weight_loading_robust branch from e52eb11 to b9b2cfd Compare May 22, 2025 04:01
@lucaslie
lucaslie requested a review from Copilot May 22, 2025 04:02
@lucaslie
lucaslie requested review from Copilot and removed request for Copilot May 22, 2025 04:06
@suyoggupta suyoggupta mentioned this pull request May 22, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances checkpoint loading by prioritizing safetensors over bin files, adds logic to limit memory allocation for sharded models, and replaces the old load_state_dict_with_assign approach with load_checkpoint_in_model.

  • Prioritize safetensors in HF repo downloads and correctly select the checkpoint file.
  • Remove the deprecated load_state_dict_with_assign and switch to load_checkpoint_in_model.
  • Update model factory to randomly initialize parameters on-device before loading weights to prevent OOM.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
tests/unittest/_torch/auto_deploy/unit/singlegpu/models/test_hf.py Removed tests for the now-removed load_state_dict_with_assign util.
tensorrt_llm/_torch/auto_deploy/models/hf.py Added _get_ignore_patterns, _get_checkpoint_file, and updated loading logic to use load_checkpoint_in_model.
tensorrt_llm/_torch/auto_deploy/models/factory.py Replaced meta-to-empty init with random init (_to_maybe_random) and streamlined the checkpoint loading flow.
Comments suppressed due to low confidence (2)

tensorrt_llm/_torch/auto_deploy/models/hf.py:276

  • After downloading the checkpoint directory, store it in self._prefetched_path (e.g., self._prefetched_path = fetched_dir) so subsequent calls reference the correct path and avoid re-downloading.
fetched_dir = snapshot_download(self.model, ignore_patterns=ignore_patterns)

tensorrt_llm/_torch/auto_deploy/models/hf.py:177

  • Add unit tests for _get_ignore_patterns and _get_checkpoint_file to verify correct filtering of weight files and error handling when no valid checkpoint is found.
def _get_ignore_patterns(self, repo_id: str):

# if we have safetensors we can ignore all bin files
if list(filter_repo_objects(items=files, allow_patterns=[safetensors_pattern])):
ignore_patterns.append(bin_pattern)
except: # noqa: E722

Copilot AI May 22, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid a bare except:; catch specific exceptions (e.g., except HFValidationError:) to prevent masking unexpected errors.

Suggested change
except: # noqa: E722
except (HFValidationError, ValueError):

Copilot uses AI. Check for mistakes.
lucaslie added a commit that referenced this pull request May 22, 2025
Includes #17 , #18, #21 , #23

* move Phi PR for sprint

Signed-off-by: Ubuntu <[email protected]>

* remove Phi3 patch and unfuse_weight transformation

Signed-off-by: Ubuntu <[email protected]>

* update Phi3/4 patches

Signed-off-by: Ubuntu <[email protected]>

* minor

Signed-off-by: Ubuntu <[email protected]>

* minor

Signed-off-by: Ubuntu <[email protected]>

* Update the position id to int64

Signed-off-by: Chenghao Zhang <[email protected]>

* Add support for handling aliased parameters

Signed-off-by: Suyog Gupta <[email protected]>

* update tests

Signed-off-by: Suyog Gupta <[email protected]>

* lint

Signed-off-by: Suyog Gupta <[email protected]>

* lint

Signed-off-by: Suyog Gupta <[email protected]>

* removed load state dict with assign context manager

Signed-off-by: Suguna Velury <[email protected]>

* formatting fix

Signed-off-by: Suguna Velury <[email protected]>

* Removed related unit tests

Signed-off-by: Suguna Velury <[email protected]>

* [AutoDeploy] correctly identifying how to load HF checkpoint weights

Signed-off-by: Lucas Liebenwein <[email protected]>

* [AutoDeploy] correctly load sharded weights

Signed-off-by: Lucas Liebenwein <[email protected]>

* skip sharding if num_users>1

Signed-off-by: Suyog Gupta <[email protected]>

---------

Signed-off-by: Ubuntu <[email protected]>
Signed-off-by: Chenghao Zhang <[email protected]>
Signed-off-by: Suyog Gupta <[email protected]>
Signed-off-by: Suguna Velury <[email protected]>
Signed-off-by: Lucas Liebenwein <[email protected]>
Co-authored-by: Ubuntu <[email protected]>
Co-authored-by: Chenghao Zhang <[email protected]>
Co-authored-by: Suguna Velury <[email protected]>
Co-authored-by: Lucas Liebenwein <[email protected]>
@lucaslie

Copy link
Copy Markdown
Author

merged in #25

@lucaslie lucaslie closed this May 22, 2025
lucaslie added a commit that referenced this pull request May 27, 2025
Includes #17 , #18, #21 , #23

* move Phi PR for sprint

Signed-off-by: Ubuntu <[email protected]>

* remove Phi3 patch and unfuse_weight transformation

Signed-off-by: Ubuntu <[email protected]>

* update Phi3/4 patches

Signed-off-by: Ubuntu <[email protected]>

* minor

Signed-off-by: Ubuntu <[email protected]>

* minor

Signed-off-by: Ubuntu <[email protected]>

* Update the position id to int64

Signed-off-by: Chenghao Zhang <[email protected]>

* Add support for handling aliased parameters

Signed-off-by: Suyog Gupta <[email protected]>

* update tests

Signed-off-by: Suyog Gupta <[email protected]>

* lint

Signed-off-by: Suyog Gupta <[email protected]>

* lint

Signed-off-by: Suyog Gupta <[email protected]>

* removed load state dict with assign context manager

Signed-off-by: Suguna Velury <[email protected]>

* formatting fix

Signed-off-by: Suguna Velury <[email protected]>

* Removed related unit tests

Signed-off-by: Suguna Velury <[email protected]>

* [AutoDeploy] correctly identifying how to load HF checkpoint weights

Signed-off-by: Lucas Liebenwein <[email protected]>

* [AutoDeploy] correctly load sharded weights

Signed-off-by: Lucas Liebenwein <[email protected]>

* skip sharding if num_users>1

Signed-off-by: Suyog Gupta <[email protected]>

---------

Signed-off-by: Ubuntu <[email protected]>
Signed-off-by: Chenghao Zhang <[email protected]>
Signed-off-by: Suyog Gupta <[email protected]>
Signed-off-by: Suguna Velury <[email protected]>
Signed-off-by: Lucas Liebenwein <[email protected]>
Co-authored-by: Ubuntu <[email protected]>
Co-authored-by: Chenghao Zhang <[email protected]>
Co-authored-by: Suguna Velury <[email protected]>
Co-authored-by: Lucas Liebenwein <[email protected]>
Signed-off-by: Lucas Liebenwein <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants