1
0
Fork 0
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

Closed
opened 2026-11-01 04:17:40 +07:00 by bsakaguchi · 1 comment
Owner

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, missing o_proj entirely because layer 0 is a linear attention layer with out_proj while layer 3 is a full attention layer with o_proj model.py. The seen_types optimization masked this because all layers share the class Qwen3_5MoeDecoderLayer qwen3.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:

  1. 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.

  2. Every module returned by get_layer_modules() is actually wrapped by PEFT — the diagnostic we added checks hasattr(mod, 'base_layer') for every module in every layer model.py. This is the exact check that caught the unwrapped o_proj modules on full attention layers.

  3. 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

# The LoRA targets must cover ALL discovered leaf names.
lora_targets = set(model.peft_config.target_modules)
missing = all_leaf_names - lora_targets
assert not missing, (
    f"LoRA targets {lora_targets} are missing modules: {missing}. "
    f"This means some layers will not be abliterated."
)

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))

assert not unwrapped, (
    f"The following modules were NOT wrapped by PEFT: {unwrapped}. "
    f"These layers will be skipped during abliteration."
)

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))))

# Collect modules from all checked layers
discovered_from_checked = set()
for i in indices_checked:
    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:
                    discovered_from_checked.add(name.split(".")[-1])
                    break

# Collect modules from ALL layers
discovered_from_all = set()
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:
                    discovered_from_all.add(name.split(".")[-1])
                    break

missing = discovered_from_all - discovered_from_checked
assert not missing, (
    f"Inspecting first 8 layers missed these module names: {missing}. "
    f"The layer inspection range may need to be increased."
)
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, missing `o_proj` entirely because layer 0 is a linear attention layer with `out_proj` while layer 3 is a full attention layer with `o_proj` model.py. The `seen_types` optimization masked this because all layers share the class `Qwen3_5MoeDecoderLayer` qwen3.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: 1. **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. 2. **Every module returned by `get_layer_modules()` is actually wrapped by PEFT** — the diagnostic we added checks `hasattr(mod, 'base_layer')` for every module in every layer model.py. This is the exact check that caught the unwrapped `o_proj` modules on full attention layers. 3. **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 # The LoRA targets must cover ALL discovered leaf names. lora_targets = set(model.peft_config.target_modules) missing = all_leaf_names - lora_targets assert not missing, ( f"LoRA targets {lora_targets} are missing modules: {missing}. " f"This means some layers will not be abliterated." ) 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__)) assert not unwrapped, ( f"The following modules were NOT wrapped by PEFT: {unwrapped}. " f"These layers will be skipped during abliteration." ) 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)))) # Collect modules from all checked layers discovered_from_checked = set() for i in indices_checked: 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: discovered_from_checked.add(name.split(".")[-1]) break # Collect modules from ALL layers discovered_from_all = set() 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: discovered_from_all.add(name.split(".")[-1]) break missing = discovered_from_all - discovered_from_checked assert not missing, ( f"Inspecting first 8 layers missed these module names: {missing}. " f"The layer inspection range may need to be increased." )
Author
Owner

@p-e-w commented on GitHub (Feb 27, 2026):

Code belongs in pull requests, not issues.

<!-- gh-comment-id:3970699158 --> @p-e-w commented on GitHub (Feb 27, 2026): Code belongs in pull requests, not issues.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
p-e-w/heretic#115
No description provided.