mirror of
https://github.com/p-e-w/heretic.git
synced 2026-12-03 18:42:57 +07:00
[GH-ISSUE #199] Qwen3.5- Support inhomogeneous layers with identical Python classes #112
Labels
No labels
bug
enhancement
enhancement
pull-request
question
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
p-e-w/heretic#112
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @timrohrbaugh on GitHub (Feb 27, 2026).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/199
Some architectures (e.g., Qwen3.5) use the same Python class for structurally different layers — for example,
Qwen3_5MoeDecoderLayeris used for both Gated DeltaNet linear attention layers and standard GQA full attention layers qwen3.5-122b-A10B_config.json. Linear attention layers exposelinear_attn.out_projwhile full attention layers exposeself_attn.o_projqwen3.5-122b-A10B_config.json.The current code has three issues with such models:
get_layer_modules()only handlesself_attn.o_projfor attention output projections, missinglinear_attn.out_projentirely p-e-w-heretic-8...260224.txt.get_abliterable_components()relies ontype()checks and only inspects layer 0, which may not represent all structural variants p-e-w-heretic-8...260224.txt._apply_lora()uses aseen_typesset that exits after inspecting the first layer type, so LoRA target module names likeo_proj(from full attention layers at index 3) are never discovered when all layers share the same class model.pyAdditionally,
get_layer_modules()does not handle shared experts in MoE models where ashared_expert.down_projexists alongside routed experts qwen3.5-122b-A10B_config.json.This PR also adds a
hasattr(module, 'base_layer')safety guard inabliterate()to gracefully skip any modules that PEFT did not wrap, rather than crashing.Changes:
Replace the current attention out-projection handling:
REMOVE this:
Exceptions aren't suppressed here, because there is currently
no alternative location for the attention out-projection.
try_add("attn.o_proj", layer.self_attn.o_proj)
with:
Full attention layers (standard GQA).
with suppress(Exception):
try_add("attn.o_proj", layer.self_attn.o_proj)
Linear attention layers (Gated DeltaNet, e.g. Qwen3.5).
with suppress(Exception):
try_add("attn.o_proj", layer.linear_attn.out_proj)
And add shared expert handling after the existing layer.mlp.experts loop:
Shared expert (Qwen3.5 and similar MoE models).
with suppress(Exception):
try_add("mlp.down_proj", layer.mlp.shared_expert.down_proj)
replace:
def get_abliterable_components(self) -> list[str]:
return list(self.get_layer_modules(0).keys())
with:
def get_abliterable_components(self) -> list[str]:
all_components: set[str] = set()
layers = self.get_layers()
for i in range(min(8, len(layers))):
all_components.update(self.get_layer_modules(i).keys())
return sorted(all_components)
3. _apply_lora() — Use index-based module name discovery:
replace:
target_modules = [
comp.split(".")[-1] for comp in self.get_abliterable_components()
]
with:
Collect actual leaf module names from representative layers.
We can't rely on type() alone because some architectures (e.g. Qwen3.5)
use the same class for structurally different layers (linear attention
vs full attention). Inspecting the first 8 layers guarantees coverage
of repeating patterns like Qwen3.5's [linear, linear, linear, full].
target_module_names: set[str] = set()
layers = self.get_layers()
for i in range(min(8, len(layers))):
layer = layers[i]
for component, modules in self.get_layer_modules(i).items():
for mod in modules:
for name, m in layer.named_modules():
if m is mod:
target_module_names.add(name.split(".")[-1])
break
target_modules = sorted(target_module_names)
After module = cast(Linear, module), add:
if not hasattr(module, 'base_layer'):
continue
@p-e-w commented on GitHub (Feb 27, 2026):
Code belongs in pull requests, not issues.