Repository navigation
Fix: PEFT LoRA transformer layers not trained when gradient_checkpointing=True — enable_input_require_grads() missing - #216
Open
SIDDARTHAREDDY8 wants to merge 1 commit into
Conversation
…slab#178) Signed-off-by: SIDDARTHA REDDY <75976672+SIDDARTHAREDDY8@users.noreply.github.com>
This branch has not been deployed
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.
Fixes #178.
Fix for locuslab/open-unlearning #178 (PEFT LoRA transformer layers not trained when gradient_checkpointing=True). Collision check clean: GitHub search for PRs mentioning 178 in body returned 0, and the issue timeline shows no cross-referenced PR (only issue #202, a LoRA-handler proposal whose author offered to include this fix if nobody else sent it; no PR exists). Collaborator Dornavineeth explicitly invited the PR. Fix: src/train.py now calls maybe_enable_input_require_grads(model, gradient_checkpointing) right after get_model(), gated on cfg.trainer.args.gradient_checkpointing; the helper (new in src/trainer/utils.py) calls model.enable_input_require_grads() only when checkpointing is on and the model exposes it, so it is a no-op otherwise. New regression test tests/test_lora_gradient_checkpointing.py (5 tests, all passing on CPU with pinned torch==2.9.1, transformers==5.5.4, peft==0.18.1). Key verification finding: the bug reproduces exactly as reported in the hook-absent state (checkpointing enabled via _set_gradient_checkpointing): transformer q_proj/v_proj lora_B grads are None while lm_head lora_B gets a gradient; after the fix all 5 lora_B adapters receive gradients and move from zero init after one optimizer step (the reporter's check). Nuance recorded honestly: pinned transformers 5.5.4 auto-registers the input-grad hook inside gradient_checkpointing_enable() (main_input_name == 'input_ids'), so the failure is masked on the standard Trainer path; the train.py fix is defense-in-depth and covers older transformers, non-Trainer flows, and custom model handlers. The test pins both states (manual hook-absent setup + pinned-transformers setup). Ruff 0.6.9 (repo-pinned): check + format clean on all changed files. Existing test_repselect.py could not be run here (needs the full src tree incl. trainer/unlearn/base.py, not in the lean workdir); the change only appends one function to trainer/utils.py and touches no existing code, and utils still imports cleanly (seed_everything smoke-tested). Pushed to SIDDARTHAREDDY8/open-unlearning branch oss-bhai-2026-11-30-p1, commit 953a5d3, base main @ a69a1d6 (verified via API: compare shows exactly the 3 intended files; DCO Signed-off-by present). No PR opened (reviewer gates that).
Tests pass: New tests/test_lora_gradient_checkpointing.py: 5/5 pass (bug-state pin, fix check with lora_B moving from zero init after one step, pinned-transformers check, two helper no-op checks). Ran with pinned torch 2.9.1 CPU / transformers 5.5.4 / peft 0.18.1. Ruff 0.6.9 check + format clean. Full repo test suite not run (needs GPU/model downloads; existing test_repselect.py needs the full src tree).