Skip to content

Fix Trainable Tokens random-init unmerge - #3656

Open
DaoyuanLi2816 wants to merge 2 commits into
huggingface:mainfrom
DaoyuanLi2816:fix/trainable-tokens-random-unmerge
Open

Fix Trainable Tokens random-init unmerge#3656
DaoyuanLi2816 wants to merge 2 commits into
huggingface:mainfrom
DaoyuanLi2816:fix/trainable-tokens-random-unmerge

Conversation

@DaoyuanLi2816

Copy link
Copy Markdown
Contributor

Summary

  • preserve the original base rows independently from randomly initialized Trainable Tokens values
  • keep merge/unmerge reversible for both Embedding and Linear targets
  • cover the regression and make the existing merged-disable test compare non-identity initialization against the actual base output

Closes #3650.

Tests

  • pytest tests/test_custom_models.py -k trainable_tokens -q --no-cov --regression — 117 passed, 6 skipped
  • ruff check tests/test_custom_models.py src/peft/tuners/trainable_tokens/layer.py
  • ruff format --check tests/test_custom_models.py src/peft/tuners/trainable_tokens/layer.py
  • git diff --check

AI assistance was used to investigate the failure and prepare the patch. I reviewed every changed line, understand the change, and ran the tests above.

@BenjaminBossan BenjaminBossan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for fixing this bug, but there are some issues with the tests, please check.

Comment thread tests/test_custom_models.py Outdated
model.eval()
outputs_before = model(**X)
if issubclass(config_cls, TrainableTokensConfig):
with model.disable_adapter():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We shouldn't need this extra call for trainable tokens. The issue is that for trainable tokens, we have init_weights=False. Just ensure that it's True before calling get_peft_model and we should be good.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the test to set config.init_weights = True before get_peft_model. The shared disable-adapter assertion now covers Trainable Tokens without the extra baseline call.

Comment thread tests/test_custom_models.py Outdated
config_kwargs = set_init_weights_false(config_cls, config_kwargs)
self._test_safe_merge(model_id, config_cls, config_kwargs)

@pytest.mark.parametrize(("target_module", "token_indices"), [("emb", [0, 1, 3]), ("lin0", [0, 1])])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's make the first argument a string to stay consistent with the other tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated the parametrization argument to the string form for consistency.

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.

Trainable Tokens random initialization prevents unmerge from restoring base weights

2 participants