mirror of
https://github.com/p-e-w/heretic.git
synced 2026-12-03 18:42:57 +07:00
[GH-ISSUE #200] Qwen3.5 model structure showed A core bug in _apply_lora() was discovered . Add a test to projects test suite. #115
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#115
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/200
The core bug was that
_apply_lora()only discovered LoRA targets from layer 0, missingo_projentirely because layer 0 is a linear attention layer without_projwhile layer 3 is a full attention layer witho_projmodel.py. Theseen_typesoptimization masked this because all layers share the classQwen3_5MoeDecoderLayerqwen3.5-122b-A10B_config.json. This caused 12 out of 48 layers to have completely unabliterated attention projections — a silent, catastrophic failure that produced no errors, only ineffective abliteration.Without a test that validates layer discovery across structurally different layers, this class of bug is invisible. The model loads, PEFT initializes, trials run — everything appears to work.
A proper test should verify that for any model loaded by Heretic:
All structurally distinct layers are inspected during LoRA target discovery — not just layer 0, and not just one layer per Python class. The fix of inspecting the first 8 layers covers the repeating
[linear, linear, linear, full]pattern qwen3.5-122b-A10B_config.json model.py.Every module returned by
get_layer_modules()is actually wrapped by PEFT — the diagnostic we added checkshasattr(mod, 'base_layer')for every module in every layer model.py. This is the exact check that caught the unwrappedo_projmodules on full attention layers.The set of LoRA target module names covers all leaf names — for Qwen3.5, this must be
['down_proj', 'o_proj', 'out_proj'], not just['down_proj', 'out_proj']There is no current test suite but this could be added to start:
tests/test_layer_discovery.py
"""
Tests that LoRA target module discovery correctly handles architectures
with inhomogeneous layers that share the same Python class.
This test was motivated by Qwen3.5, where all 48 layers use the class
Qwen3_5MoeDecoderLayer but alternate between linear attention layers
(with linear_attn.out_proj) and full attention layers (with self_attn.o_proj).
The original seen_types-based discovery only inspected layer 0 and missed
o_proj entirely, causing 12/48 layers to be unabliterated.
"""
def test_layer_modules_consistency():
"""
Verify that every module returned by get_layer_modules() for every
layer is present in the LoRA target modules and gets wrapped by PEFT.
"""
# For each layer, collect the actual leaf module names
# and verify they are all in the LoRA target set.
all_leaf_names = set()
layers = model.get_layers()
for i in range(len(layers)):
layer = layers[i]
for component, modules in model.get_layer_modules(i).items():
for mod in modules:
for name, m in layer.named_modules():
if m is mod:
all_leaf_names.add(name.split(".")[-1])
break
def test_peft_wrapping_complete():
"""
Verify that PEFT actually wrapped every module that
get_layer_modules() returns across all layers.
"""
unwrapped = []
for i, layer in enumerate(model.get_layers()):
for component, modules in model.get_layer_modules(i).items():
for mod in modules:
if not hasattr(mod, 'base_layer'):
unwrapped.append((i, component, type(mod).name))
def test_discovery_inspects_multiple_layer_types():
"""
Verify that the first 8 layers are inspected, catching any
repeating structural patterns (e.g. Qwen3.5's 3:1 linear/full pattern).
"""
layers = model.get_layers()
indices_checked = set(range(min(8, len(layers))))
@p-e-w commented on GitHub (Feb 27, 2026):
Code belongs in pull requests, not issues.