Repository navigation
Add type hints to rescale_adapter_scale and compute_loss in helpers.py - #3448
Closed
RudrenduPaul wants to merge 1 commit into
Closed
RudrenduPaul wants to merge 1 commit into
RudrenduPaul wants to merge 1 commit into
Conversation
githubnemo
requested changes
Aug 3, 2026
githubnemo
left a comment
Collaborator
There was a problem hiding this comment.
Hey @RudrenduPaul, thanks for the PR!
Can you run make style to fix code style issues?
Contributor
Author
|
Thanks for the review @githubnemo — fixed the code style issue ( |
4 tasks done
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 two functions in
src/peft/helpers.py. Follow-up to the merged#3144 (same author), which added type hints to
src/peft/utils/merge_utils.pyandsrc/peft/utils/other.py.The following functions were missing type hints:
helpers.pyrescale_adapter_scale: addednn.Moduleparam type formodel,Union[float, int]paramtype for
multiplier(matches the existingisinstance(multiplier, (float, int))check), andIterator[None]return type (it's a@contextmanager-decorated generator)MontecloraTrainerMixin.compute_loss: addednn.Moduleparam type formodel,dict[str, Any]param type forinputs,boolparam type forreturn_outputs,Anyfor**kwargs, andUnion[torch.Tensor, tuple[torch.Tensor, Any]]return type (matches the tworeturn paths:
total_lossalone, or(total_loss, outputs))I verified each of the 5 functions listed in the original task scope against the live file
before editing.
get_best_targets,get_best_target_parameters, andfind_kappa_target_modulesalready have full parameter and return type hints on
main(added since #3144), so they wereleft unmodified — this PR only touches the two functions that actually lacked hints.
These annotations improve IDE support, static analysis, and code readability without any
behavioral changes.
Tests
Type-hint only change — no behavioral modification. Test commands:
python3 -m py_compile src/peft/helpers.py— passesnpx pyright src/peft/helpers.py— 11 errors total, all pre-existing (bitsandbytes importresolution, dynamic attribute access on
BaseTunerLayer/Tensor, andobject.compute_lossonthe mixin's bare superclass since
MontecloraTrainerMixinis typed standalone); none on thelines modified by this PR
tests/test_helpers.pycallsrescale_adapter_scale(model=..., multiplier=0.5)withnn.Module-typed models (LoRA models,diffusers pipeline components) and float multipliers;
compute_loss(model, inputs)is calledwith
inputs = {"input_ids": ..., "labels": ...}, adict[str, Tensor]Before submitting
per the precedent set in Add type hints to utility functions in merge_utils and other modules #3144 with @BenjaminBossan's guidance).
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). The type annotations were
derived by reading the function implementations, checking actual call sites in
tests/test_helpers.py, and cross-referencing PyTorch/typing conventions used elsewhere in thissame file (e.g.
KappaTuneSelector,find_kappa_target_modules).