Skip to content

LoRA: Custom layer filters that support regular expressions#778

Merged
dxqb merged 16 commits intoNerogar:masterfrom
tehybel:custom_layer_filters
Aug 31, 2025
Merged

LoRA: Custom layer filters that support regular expressions#778
dxqb merged 16 commits intoNerogar:masterfrom
tehybel:custom_layer_filters

Conversation

@tehybel
Copy link
Copy Markdown
Contributor

@tehybel tehybel commented Apr 9, 2025

Old-style literal filters are still supported, since they are valid regular expressions, too.

Copy link
Copy Markdown
Owner

@Nerogar Nerogar left a comment

Choose a reason for hiding this comment

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

Using regular expressions without any additional checks looks a bit dangerous. For example, the . character has a special meaning in regular expressions but is a very common character in module names. A filter like a.b could match unintended modules, not just those that contain exactly a.b

Comment thread modules/util/NamedParameterGroup.py Outdated
Comment thread modules/ui/LoraTab.py
@dxqb
Copy link
Copy Markdown
Collaborator

dxqb commented Apr 30, 2025

  • printing to the console which layers were selected (and which not) would be helpful imho
  • when this is done I might want to use it for a PR to filter layers in full finetuning. Finetuning some layers such as the embedders seems counterproductive

O-J1 added 2 commits June 21, 2025 20:02
-  `use_regex` parameter in ModuleFilter for regex matching.
- Updated LoRAModuleWrapper to use it
- Added toggle for regex usage to UI
- Added logging for selected and skipped layers during module creation.
- Update TrainConfig to have `lora_layers_regex` attribute
- UI tweaks to lora tab for layer preset.
@O-J1
Copy link
Copy Markdown
Collaborator

O-J1 commented Jun 21, 2025

Given Thomas has been MIA for awhile now I have gone and refactored it.

@dxqbYD Added logging of the selected/unselected layers/

@O-J1 O-J1 changed the title Custom layer filters that support regular expressions LoRA: Custom layer filters that support regular expressions Jun 29, 2025
@dxqb dxqb mentioned this pull request Aug 20, 2025
9 tasks
@dxqb dxqb self-assigned this Aug 20, 2025
Comment thread modules/util/NamedParameterGroup.py Outdated
Comment thread modules/module/LoRAModule.py Outdated
Comment thread modules/module/LoRAModule.py Outdated
Comment thread modules/ui/LoraTab.py Outdated
Comment thread modules/module/LoRAModule.py Outdated
Comment thread modules/util/config/TrainConfig.py
Comment thread modules/module/ModuleFilter.py Outdated
Comment thread modules/module/ModuleFilter.py Outdated
Comment thread modules/module/ModuleFilter.py Outdated
Comment thread modules/util/NamedParameterGroup.py Outdated
@dxqb dxqb removed their assignment Aug 22, 2025
Comment thread modules/module/LoRAModule.py Outdated
@dxqb
Copy link
Copy Markdown
Collaborator

dxqb commented Aug 25, 2025

Here

filtered_parameters = [param[1] for param in model.transformer.named_parameters() if "guidance_layer" not in param[0]]
is now code that applies a layer filter to full tinetuning - although technically requires_grad should also set to False for these parameters

@dxqb
Copy link
Copy Markdown
Collaborator

dxqb commented Aug 25, 2025

one possible solution for the some of the complications above:
do we really need regex? why not a simpler substring matching, and an "ignore pattern" that allows to exclude certain layers

@Calamdor
Copy link
Copy Markdown
Contributor

I think that would allow filtering of “single” for the flux-ish models (flux, Hunyuan, i assume Chroma) to only grab a double block(s).

@dxqb
Copy link
Copy Markdown
Collaborator

dxqb commented Aug 25, 2025

I think that would allow filtering of “single” for the flux-ish models (flux, Hunyuan, i assume Chroma) to only grab a double block(s).

yes. any other use cases that would really require regex?

@O-J1
Copy link
Copy Markdown
Collaborator

O-J1 commented Aug 28, 2025

I think that would allow filtering of “single” for the flux-ish models (flux, Hunyuan, i assume Chroma) to only grab a double block(s).

yes. any other use cases that would really require regex?

I think that would allow filtering of “single” for the flux-ish models (flux, Hunyuan, i assume Chroma) to only grab a double block(s).

yes. any other use cases that would really require regex?

Anyone wanting to to select multiple non-contiguous blocks or complex layer selection. Comma delimited lists still work fine and that was the intention of this

image

@dxqb
Copy link
Copy Markdown
Collaborator

dxqb commented Aug 28, 2025

yes. any other use cases that would really require regex?

Anyone wanting to to select multiple non-contiguous blocks or complex layer selection. Comma delimited lists still work fine and that was the intention of this
image

okay, if we want to support regex let's do that for expert users, but following the same sentiment as your screenshot quote I'd like a way to reject a pattern for non-expert users. I'd consider myself among those non-expert users, considering how difficult it is to reject something using regex:

^(?:(?!guidance_layer|context_embedder|x_embed).)*$

What do you think about:

  • keep the current lora_layers (but rename it to layers_accept either in this PR or in a later one, because we also need it for FFT)
  • add a [lora_]layers_reject for simple substring pattern matching to reject layers
  • both can be used as regex if you switch it on

This solves the complexity, but also this #778 (comment) because we don't need regex presets anymore.

@O-J1
Copy link
Copy Markdown
Collaborator

O-J1 commented Aug 28, 2025

This solves the complexity, ... don't need regex presets anymore.

Sumarising convo on Discord:

  1. Regex presets are already supported
  2. We agreed substring matching would be better as the default, to be contributed in a seperate PR (new UI field)

I believe all review comments have been addressed now or are relevant to a new PR

Comment thread modules/ui/LoraTab.py Outdated
Comment thread modules/util/config/TrainConfig.py
Comment thread modules/util/NamedParameterGroup.py Outdated
@O-J1 O-J1 force-pushed the custom_layer_filters branch from 4cade51 to 9c090e9 Compare August 31, 2025 08:17
@dxqb dxqb merged commit c581f22 into Nerogar:master Aug 31, 2025
1 check passed
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.

5 participants