Repository navigation
Add type hints to hotswap utility functions in hotswap.py - #3452
Closed
RudrenduPaul wants to merge 1 commit into
Closed
RudrenduPaul wants to merge 1 commit into
RudrenduPaul wants to merge 1 commit into
Conversation
Adds missing type annotations to _update_scaling, hotswap_adapter_from_state_dict, and hotswap_adapter in src/peft/utils/hotswap.py, following the same pattern as the already-merged type-hint cleanup in huggingface#3144. prepare_model_for_compiled_hotswap was checked and already had complete type hints, so it was left unchanged.
Member
|
If you're working on multiple PRs to add type hints, I would strongly prefer you could pool them into a single PR, even if multiple files are being touched. |
This was referenced Aug 9, 2026
Contributor
Author
|
Thanks for the feedback @BenjaminBossan — pooled this change together with #3448 into a single consolidated PR: #3529. Closing this one in favor of that. |
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?
Adds missing type annotations to utility functions in
src/peft/utils/hotswap.py, following the same pattern as the already-merged type-hint cleanup in #3144.The following functions were missing type hints:
hotswap.py_update_scaling: addedLoraLayer(paramlora_module),str(paramadapter_name),Optional[float] = None(paramscaling), andNonereturn typehotswap_adapter_from_state_dict: addedNonereturn type (params were already annotated)hotswap_adapter: addedtorch.nn.Module(parammodel),str(parammodel_name_or_path),str(paramadapter_name),Optional[str] = None(paramtorch_device), andNonereturn typeprepare_model_for_compiled_hotswapwas also checked per the task scope, but it already had complete type hints on every parameter and the return type, so it was left unchanged — no genuinely missing hints there.Type choices were verified against real call sites, not just docstrings:
_update_scaling'slora_moduleparam is typed asLoraLayer(not the more generictorch.nn.Moduleused elsewhere in this file) because the function body accesseslora_module.scaling, a dict attribute defined onLoraLayer, not ontorch.nn.Module. Typing it astorch.nn.Moduleproduced 14 new pyright errors inside the function body (subscript/attribute access on an untypedModule.__getattr__fallback); typing it asLoraLayereliminates all of them while accurately describing the real runtime objects passed in (Linear/Conv2dLoRA layers, both of which subclassLoraLayer).hotswap_adapter'smodel_name_or_pathparam is typed asstr(matching the declared signatures ofPeftConfig.from_pretrainedandload_peft_weights, both of which it's passed into) rather thanstr | os.PathLike, even though some tests passpathlib.Pathobjects — widening the annotation to includeos.PathLikecaused a new pyright error at theload_peft_weights(model_name_or_path, ...)call site, since that function's own parameter is declared asstronly.Tests
Type-hint only change — no behavioral modification.
python -m py_compile src/peft/utils/hotswap.py— passesruff check/ruff format --check(pinned version 0.15.12) — both pass with no findingsnpx pyright src/peft/utils/hotswap.py— error count and messages are byte-for-byte identical before and after this change (56 errors both times, all pre-existing, in code untouched by this PR, or due to import resolution in a bare env without torch/accelerate fully installed — none introduced by the newly annotated functions)Before submitting
AI assistance disclosure
This PR was developed with the assistance of Claude Code (AI). All changes have been read, understood, and verified by the human contributor (Rudrendu Paul), including cross-referencing every added type against the function's real call sites and confirming via pyright that no new type errors were introduced.