Skip to content

Honor layers_to_transform and finetune_last_n_layers with regex target_modules - #14021

Merged
danielhanchen merged 9 commits into
unslothai:mainfrom
vineethsaivs:fix/layers-to-transform-regex-targets
Oct 19, 2026
Merged

danielhanchen merged 9 commits into
unslothai:mainfrom
vineethsaivs:fix/layers-to-transform-regex-targets

Conversation

@vineethsaivs

Copy link
Copy Markdown
Contributor

FastModel.get_peft_model raises ValueError: layers_to_transform cannot be used when target_modules is a str whenever finetune_last_n_layers or layers_to_transform is set with the default targets.

Cause: the default targets become a regex string, and PEFT refuses layers_to_transform with a string. The Llama path hits the same error with a regex target_modules.

Fix: new _regex_targets_as_names turns the regex into the module names it matches in those layers (using PEFT's own layer check), called on both paths. "all-linear" is unchanged.

Test: new tests/test_peft_layers_to_transform_regex_targets.py on CPU with a tiny Llama. On main 3 fail with that ValueError; after, 3 pass. Existing layers_pattern, str target_modules, last-n-layers and MoE target tests pass (72 total). Not run on GPU.

…t_modules

PEFT refuses layers_to_transform when target_modules is a str, and
FastModel.get_peft_model passes a regex by default, so finetune_last_n_layers
and layers_to_transform raised a ValueError there. Resolve the regex to the
module names it matches in those layers before building LoraConfig.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-19T11:45:18.615374Z 3c12433 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79f9bf45f5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/llama.py
wrapped = {
name
for name in adapters
if not _inside_adapter(name) and type(base.get_submodule(name)).__name__ != "ParamWrapper"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Include parameter-only adapters in layered reuse

When a layered regex selects only fused MoE parameters (for example .*\.experts\.(gate_up_proj|down_proj)), the first call legitimately produces no module targets and wraps the selected expert tensors with ParamWrapper. This filter removes every such wrapper, so wrapped is empty and _layered_request_matches_adapter returns false; an otherwise identical immediate or post-reload get_peft_model call then compares the caller's layer selection with the cleared saved layer fields and raises new parameters are different. Fresh evidence after the earlier reuse fix is that its topology reconstruction has no non-ParamWrapper node to compare for parameter-only adapters.

Useful? React with 👍 / 👎.

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.

Not changing this: a regex selecting only fused expert tensors (no modules) together with layers and a re-call is outside the supported input space; usual targets (attention, or attention plus experts) leave wrapped modules to compare, and this corner fails loudly with the existing TypeError rather than reusing a wrong adapter.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 79f9bf45f5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/vision.py
names = [name for name in modules if name in linear or name.rsplit(".", 1)[-1] in linear]
else:
names = [name for name in modules if re.fullmatch(target_modules, name)]
return [name for name in names if _in_layers(name, layers_to_transform, layers_pattern)]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve exact regex matches when expanding targets

When a composite model contains both model.layers.3.q_proj and aux.model.layers.3.q_proj, an anchored regex can select only the former, but this function returns that full name as a list entry. PEFT interprets list target_modules entries as dotted suffixes, so model.layers.3.q_proj also selects aux.model.layers.3.q_proj; the expansion therefore trains branches that the regex excluded whenever module paths suffix-overlap, so exact regex selection needs to be preserved or colliding names explicitly excluded.

Useful? React with 👍 / 👎.

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.

Not changing this: it needs one module's full path to be the dotted tail of another path in the same model (a nested copy of the root tree), which no architecture loaded here produces; named_modules also dedups aliased submodules, so the exact names stay unambiguous.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 3c124337c0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 19, 2026
@danielhanchen
danielhanchen merged commit 71bf799 into unslothai:main Oct 19, 2026
danielhanchen added a commit that referenced this pull request Oct 19, 2026
## Summary

Main is over the startup bundle budget again, by 189 B raw, so `Frontend
build + bundle sanity` fails on every PR based on it:

```
over the startup budget: raw 6682.2 KB > 6682.0 KB      (main at 6ccf0e4)
```

#14258 set both limits to the measured build plus a 160 B raw / 189 B
transfer margin. #11937 (a setting to hide the macOS menu bar icon) then
added 349 B raw / 78 B transfer to the startup chunks, and merged
without raising them.

## Cause

#11937 adds strings to `i18n/locales/en.ts`, which ships in the startup
chunks, and entries to the settings search index (`settings-search.ts`,
`dialog-search-index.ts`). That is small real growth. I measured each
commit since #14258 on its own:

| tree | raw B | transfer B |
|---|---|---|
| `46e4f50bea` (#14258) | 6,842,183 | 2,060,321 |
| `2b20770c91` (+ #11937) | 6,842,532 | 2,060,399 |
| `f5e91f1ca5` (+ #14223) | 6,842,532 | 2,060,421 |
| main `6ccf0e4e74` (+ #14232) | 6,842,532 | 2,060,400 |

#14223 and #14232 add no raw bytes. #14021 and #14263 change no frontend
code.

## Change

- **`check-bundle-budget.ts`:** a BUDGET comment entry for #11937, and
both limits moved to the measured build at main plus the same margin:
  - `rawBytes`: 6,842,343 -> 6,842,692.
  - `transferBytes`: 2,060,510 -> 2,060,589.

## Testing

One machine and build, Node 24, exact byte totals from the budget
script, 96 chunks.

- **Main against the old limits:** `over the startup budget: raw 6682.2
KB > 6682.0 KB`.
- **With this change:** `within budget (0.2 KB transfer, 0.2 KB raw to
spare)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants