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 #84] Unable to abliterate Qwen3-VL-235B-A22B-Instruct due to intermediate size #41

Closed
opened 2026-11-01 04:16:53 +07:00 by bsakaguchi · 4 comments
Owner

Originally created by @Ristellise on GitHub (Dec 13, 2025).
Original GitHub issue: https://github.com/p-e-w/heretic/issues/84

Out of the box, this is not supported in heretic as AutoModelForCasualLM does not include Qwen3-VL. The solution to this is rather trivial, as shown in this issue.

However, this will not work out of the box as the abliteration will crash:

                for matrix in matrices:
                    # Ensure projector is on the same device as the matrix for multi-GPU support.
                    device_projector = projector.to(matrix.device)
                    # In-place subtraction is safe as we're not using Autograd.
                    # NOTE: Crashes here because the intermediate matrix size for Q3 is "1536" as defined in the config.json
                    matrix.sub_(weight * (device_projector @ matrix)) 

I'm not entirely sure if this is the solution (It feels suspicious to me, plus it's an AI generated solution) but I managed to abliterate it.

                for matrix in matrices:
                    # Ensure projector is on the same device as the matrix for multi-GPU support.
                    device_projector = projector.to(matrix.device)

                    hidden_size = device_projector.shape[0]

                    if matrix.shape[-2] == hidden_size:
                        # Case 1: Standard Linear Layer (Out, In) or (Experts, Out, In)
                        # The output is the second-to-last dimension (Rows).
                        # Action: Left-Multiply (P @ W) to project the rows.
                        
                        if matrix.ndim == 3:
                            # Broadcast projector (1, H, H) over experts (E, H, I)
                            correction = torch.matmul(device_projector.unsqueeze(0), matrix)
                        else:
                            # Standard 2D matrix
                            correction = torch.matmul(device_projector, matrix)

                    elif matrix.shape[-1] == hidden_size:
                        # Case 2: Transposed/Internal Layout (In, Out) or (Experts, In, Out)
                        # The output is the last dimension (Columns).
                        # Action: Right-Multiply (W @ P) to project the columns.
                        
                        # (E, I, H) @ (H, H) -> (E, I, H) works automatically via broadcasting
                        correction = torch.matmul(matrix, device_projector)
                        
                    else:
                        # Should not happen if the matrix is correct
                        assert f"Expected hidden_size [{hidden_size}] does not match any of possible matrix shapes {matrix.shape}"
                        
                    # In-place subtraction is safe as we're not using Autograd.
                    matrix.sub_(weight * correction)

This seems to enable the model to be abliterated. However, I've noticed that it only really works when the direction_index is applied on a per-layer basis.

Originally created by @Ristellise on GitHub (Dec 13, 2025). Original GitHub issue: https://github.com/p-e-w/heretic/issues/84 Out of the box, this is not supported in heretic as `AutoModelForCasualLM` does not include Qwen3-VL. The solution to this is rather trivial, as shown in this [issue](https://github.com/p-e-w/heretic/issues/18#issuecomment-3550228598). However, this will not work out of the box as the abliteration will crash: ```py for matrix in matrices: # Ensure projector is on the same device as the matrix for multi-GPU support. device_projector = projector.to(matrix.device) # In-place subtraction is safe as we're not using Autograd. # NOTE: Crashes here because the intermediate matrix size for Q3 is "1536" as defined in the config.json matrix.sub_(weight * (device_projector @ matrix)) ``` I'm not entirely sure if this is the solution (It feels suspicious to me, plus it's an AI generated solution) but I managed to abliterate it. ```py for matrix in matrices: # Ensure projector is on the same device as the matrix for multi-GPU support. device_projector = projector.to(matrix.device) hidden_size = device_projector.shape[0] if matrix.shape[-2] == hidden_size: # Case 1: Standard Linear Layer (Out, In) or (Experts, Out, In) # The output is the second-to-last dimension (Rows). # Action: Left-Multiply (P @ W) to project the rows. if matrix.ndim == 3: # Broadcast projector (1, H, H) over experts (E, H, I) correction = torch.matmul(device_projector.unsqueeze(0), matrix) else: # Standard 2D matrix correction = torch.matmul(device_projector, matrix) elif matrix.shape[-1] == hidden_size: # Case 2: Transposed/Internal Layout (In, Out) or (Experts, In, Out) # The output is the last dimension (Columns). # Action: Right-Multiply (W @ P) to project the columns. # (E, I, H) @ (H, H) -> (E, I, H) works automatically via broadcasting correction = torch.matmul(matrix, device_projector) else: # Should not happen if the matrix is correct assert f"Expected hidden_size [{hidden_size}] does not match any of possible matrix shapes {matrix.shape}" # In-place subtraction is safe as we're not using Autograd. matrix.sub_(weight * correction) ``` This seems to *enable* the model to be abliterated. However, I've noticed that it only really works when the `direction_index` is applied on a per-layer basis.
Author
Owner

@Ristellise commented on GitHub (Dec 13, 2025):

I was just made aware that this PR seems correlated to fixing this issue

EDIT: On further testing of the model, it seems to now avoid the question after abliteration. I haven't done any more extensive tests but...

<!-- gh-comment-id:3649125435 --> @Ristellise commented on GitHub (Dec 13, 2025): I was just made aware that this [PR](https://github.com/p-e-w/heretic/pull/78) seems correlated to fixing this issue EDIT: On further testing of the model, it seems to now avoid the question after abliteration. I haven't done any more extensive tests but...
Author
Owner

@calvin2021y commented on GitHub (Dec 14, 2025):

Is there a hf repo to test your abliteration model ?

<!-- gh-comment-id:3651754995 --> @calvin2021y commented on GitHub (Dec 14, 2025): Is there a hf repo to test your abliteration model ?
Author
Owner

@Ristellise commented on GitHub (Dec 15, 2025):

Is there a hf repo to test your abliteration model ?

repo is here: https://huggingface.co/KaraKaraWitch/Heretic-Qwen3-VL-235B-A22B-Instruct

<!-- gh-comment-id:3654724316 --> @Ristellise commented on GitHub (Dec 15, 2025): > Is there a hf repo to test your abliteration model ? repo is here: https://huggingface.co/KaraKaraWitch/Heretic-Qwen3-VL-235B-A22B-Instruct
Author
Owner

@p-e-w commented on GitHub (Oct 12, 2026):

We now use LoRAs throughout for modifying models, so this issue is no longer relevant.

<!-- gh-comment-id:6124601662 --> @p-e-w commented on GitHub (Oct 12, 2026): We now use LoRAs throughout for modifying models, so this issue is no longer relevant.
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#41
No description provided.