Skip to content

Add type hints to rescale_adapter_scale and compute_loss in helpers.py - #3448

Closed
RudrenduPaul wants to merge 1 commit into
huggingface:mainfrom
RudrenduPaul:add-type-hints-helpers
Closed

RudrenduPaul wants to merge 1 commit into
huggingface:mainfrom
RudrenduPaul:add-type-hints-helpers

Conversation

@RudrenduPaul

Copy link
Copy Markdown
Contributor

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.py and
src/peft/utils/other.py.

The following functions were missing type hints:

helpers.py

  • rescale_adapter_scale: added nn.Module param type for model, Union[float, int] param
    type for multiplier (matches the existing isinstance(multiplier, (float, int)) check), and
    Iterator[None] return type (it's a @contextmanager-decorated generator)
  • MontecloraTrainerMixin.compute_loss: added nn.Module param type for model,
    dict[str, Any] param type for inputs, bool param type for return_outputs, Any for
    **kwargs, and Union[torch.Tensor, tuple[torch.Tensor, Any]] return type (matches the two
    return paths: total_loss alone, 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, and find_kappa_target_modules
already have full parameter and return type hints on main (added since #3144), so they were
left 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 — passes
  • npx pyright src/peft/helpers.py — 11 errors total, all pre-existing (bitsandbytes import
    resolution, dynamic attribute access on BaseTunerLayer/Tensor, and object.compute_loss on
    the mixin's bare superclass since MontecloraTrainerMixin is typed standalone); none on the
    lines modified by this PR
  • Verified the new hints against actual call sites: tests/test_helpers.py calls
    rescale_adapter_scale(model=..., multiplier=0.5) with nn.Module-typed models (LoRA models,
    diffusers pipeline components) and float multipliers; compute_loss(model, inputs) is called
    with inputs = {"input_ids": ..., "labels": ...}, a dict[str, Tensor]
  • No unit test additions required (no behavior change)

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). 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 this
same file (e.g. KappaTuneSelector, find_kappa_target_modules).

@githubnemo githubnemo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hey @RudrenduPaul, thanks for the PR!

Can you run make style to fix code style issues?

@RudrenduPaul

Copy link
Copy Markdown
Contributor Author

Thanks for the review @githubnemo — fixed the code style issue (Iterator now imported from collections.abc per the deprecated-import rule) and, per @BenjaminBossan's request on #3452 to pool multiple type-hint PRs into one, folded this change into a single consolidated PR: #3529. Closing this one in favor of that.

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.

2 participants