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 #199] Qwen3.5- Support inhomogeneous layers with identical Python classes #112

Closed
opened 2026-11-01 04:17:38 +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/199

Some architectures (e.g., Qwen3.5) use the same Python class for structurally different layers — for example, Qwen3_5MoeDecoderLayer is used for both Gated DeltaNet linear attention layers and standard GQA full attention layers qwen3.5-122b-A10B_config.json. Linear attention layers expose linear_attn.out_proj while full attention layers expose self_attn.o_proj qwen3.5-122b-A10B_config.json.

The current code has three issues with such models:

  1. get_layer_modules() only handles self_attn.o_proj for attention output projections, missing linear_attn.out_proj entirely p-e-w-heretic-8...260224.txt.
  2. get_abliterable_components() relies on type() checks and only inspects layer 0, which may not represent all structural variants p-e-w-heretic-8...260224.txt.
  3. _apply_lora() uses a seen_types set that exits after inspecting the first layer type, so LoRA target module names like o_proj (from full attention layers at index 3) are never discovered when all layers share the same class model.py

Additionally, get_layer_modules() does not handle shared experts in MoE models where a shared_expert.down_proj exists alongside routed experts qwen3.5-122b-A10B_config.json.

This PR also adds a hasattr(module, 'base_layer') safety guard in abliterate() to gracefully skip any modules that PEFT did not wrap, rather than crashing.
Changes:

  1. get_layer_modules() — Add linear attention and shared expert support:
    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)

  1. get_abliterable_components() — Use index-based inspection:

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)

  1. abliterate() --Add safety guard for unwrapped modules:
    After module = cast(Linear, module), add:
    if not hasattr(module, 'base_layer'):
    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_5MoeDecoderLayer` is used for both Gated DeltaNet linear attention layers and standard GQA full attention layers qwen3.5-122b-A10B_config.json. Linear attention layers expose `linear_attn.out_proj` while full attention layers expose `self_attn.o_proj` qwen3.5-122b-A10B_config.json. The current code has three issues with such models: 1. `get_layer_modules()` only handles `self_attn.o_proj` for attention output projections, missing `linear_attn.out_proj` entirely p-e-w-heretic-8...260224.txt. 2. `get_abliterable_components()` relies on `type()` checks and only inspects layer 0, which may not represent all structural variants p-e-w-heretic-8...260224.txt. 3. `_apply_lora()` uses a `seen_types` set that exits after inspecting the first layer type, so LoRA target module names like `o_proj` (from full attention layers at index 3) are never discovered when all layers share the same class model.py Additionally, `get_layer_modules()` does not handle shared experts in MoE models where a `shared_expert.down_proj` exists alongside routed experts qwen3.5-122b-A10B_config.json. This PR also adds a `hasattr(module, 'base_layer')` safety guard in `abliterate()` to gracefully skip any modules that PEFT did not wrap, rather than crashing. Changes: 1. get_layer_modules() — Add linear attention and shared expert support: 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) 2. get_abliterable_components() — Use index-based inspection: 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) 4. abliterate() --Add safety guard for unwrapped modules: After module = cast(Linear, module), add: if not hasattr(module, 'base_layer'): continue
Author
Owner

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

Code belongs in pull requests, not issues.

<!-- gh-comment-id:3970700736 --> @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#112
No description provided.