[PR #469] [CLOSED] ds4: opt-in direction projection before each residual write (--ablate) #546

Closed
opened 2026-10-29 21:26:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/mw00/project-maya/pull/469
Author: @JoshBolding
Created: 10/24/2026
Status: ❌ Closed

Base: main ← Head: pr/ds4-ablate-v1126


📝 Commits (2)

  • 1b216f6 ds4: --ablate, a refusal direction projected out of every residual write
  • 7836219 ds4: --ablate loader on GgufFile, bounds-checked, with tests and docs

📊 Changes

9 files changed (+505 additions, -0 deletions)

View changed files

📝 CMakeLists.txt (+4 -0)
📝 docs/SETTINGS.md (+20 -0)
➕ src/ds4/ds4_ablate.hpp (+187 -0)
📝 src/ds4/ds4_kernels.cu (+13 -0)
📝 src/ds4/ds4_kernels.cuh (+2 -0)
📝 src/ds4/ds4_main.cpp (+17 -0)
📝 src/ds4/ds4_model.cu (+33 -0)
📝 src/ds4/ds4_model.hpp (+6 -0)
➕ tests/ds4_ablate_test.cpp (+223 -0)

📄 Description

Adds an opt-in --ablate direction projection to maya-ds4:

--ablate FILE [--ablate-layer N] [--ablate-scale F]

Each layer's attention output and MoE output become y - s r (r . y) right before hc_post, for a unit
direction r. The intended use is refusal ablation (abliteration). The direction comes from a
llama-cvector-generator GGUF (direction.N) or from a rank-1 abliteration LoRA GGUF, whose lora_b is -r and
whose abliterate.* keys give the layer and scale. Without the flag nothing is loaded and no kernel is launched.

How it differs from cvec

--control-vector-scaled --cvec-mode project already has the projection primitive, but it acts on the accumulated
hc residual streams after the FFN. This patch acts on each attention and MoE contribution before it enters them,
which is where an abliteration LoRA on attn_output_b / ffn_down_shexp / ffn_down_exps acts. So raising cvec's
n_embd cap (4096; this model is 5120) and wiring it into maya-ds4 would not reproduce this placement. I kept the
DS4 code separate rather than change a kernel other models use. I'm happy to share the projection kernel or move this
behind cvec if you'd prefer that.

What it does with an adapter

Only r and the scale are taken from an adapter. Its lora_a halves (s r^T W, computed from the weights it was
built against) are not used. On those same weights the result equals the adapter: (W - s r r^T W) x = y - s r (r . y)
for each writer, and the MoE output is the sum of the shared and routed writers. On other weights it is the exact
projection, which differs from the adapter by s r r^T (W - W_source) x.

An adapter has to carry the same -r on all three writers of every block, for exactly the model's block count.
Anything else is refused, because projecting every write would not be what it does: an adapter covering only some
blocks, one without the routed experts, or an arbitrary LoRA.

Changes

  • src/ds4/ds4_ablate.hpp: the loader, built on the existing GgufFile reader. Every tensor's bytes are checked
    to lie inside the file, metadata integers are range-checked before conversion, and the scale must be positive
    and finite.
  • src/ds4/ds4_kernels.cu: project_out, one block per row, using the existing block_sum.
  • src/ds4/ds4_model.cu: the hooks on the window and chunk paths, after the tensor-parallel all-reduce.
  • src/ds4/ds4_main.cpp: the flags. --ablate-layer takes a whole number from 1 to 4096 and --ablate-scale a
    number from 1e-6 to 1e6. Anything else exits with an error, so a typo can't fall back to the default strength.
  • tests/ds4_ablate_test.cpp (CTest, no model, no GPU): 25 checks on synthetic GGUFs. They cover what loads
    (control vectors, whole-model adapters, scale resolution) and what is refused: partial or deeper adapters,
    mismatched directions, tensor offsets that wrap or run past the end, out-of-range metadata, zero or non-F32
    directions, truncated files, bad scales.
  • docs/SETTINGS.md: a "DeepSeek: projecting a direction out" section.

It's a flag rather than a MAYA_DS4_* variable because it names a file to load at startup, the way --expert-profile
does. I can move it to an env var if you'd prefer.

Testing

  • ds4_ablate_test passes, including under ASan + UBSan with -fsanitize=float-cast-overflow.
  • maya-ds4 builds (CUDA, sm_86) with no new warnings. Out-of-range --ablate-layer / --ablate-scale values
    (2147483648, 1e100, 1.5, 0, 1e-50, 1e7, nan, abc) exit 2 with a message.
  • On the real 40-block adapter and its control vector, the loader returns a direction byte-identical to the one the
    evaluations below used.
  • python tools/check_hip_compat.py passes. AMD build and runtime are not tested.

Measurements

All on maya-ds4, Maya-S pack, 4x RTX 3090, thinking off, stock vs --ablate with a layer-22 direction at scale 1.

Decode speed, drafting off (--dspark off), 3 warm-ups + 10 timed 200-token requests per server start, stock
and ablate alternated across restarts. The median change per adjacent pair was -3.00%, -2.81%, -3.84% and -1.47%,
so about 3% lower decode throughput. These are four restart pairs, not independent trials. Alternating mattered:
on this box decode creeps up over the first few restarts (37.5 -> 42.5 -> 44.0 tok/s for one config), and a
stock-then-ablate sequential run made the ablated build look 13% faster. No consistent prompt-speed effect was
resolved. A single-pass register-held variant of the kernel was no better (1.6% slower at 256 threads; +1.40% and
-0.30% at 1024), so I kept the simple one.

Refusals: 120 AdvBench prompts and 120 benign xstest_safe prompts. The eval prompts were checked against the
128 pairs the direction was fitted on: none match exactly, one partially.

Stock --ablate
Refuses harmful prompts 98.3% (118/120) 4.2% (5/120)
Judge harmfulness score (0-1) 0.013 0.851
Refuses benign prompts 0% 0%

Graded by prefix match and by a fixed judge model calibrated against a reference score before the run. The two
agree.

HumanEval, all 164, greedy, nothing hit the token limit: 156/164 -> 157/164. Paired on the same 164 problems,
there were no pass-to-fail transitions in this run (156 pass both, 7 fail both, 1 fail-to-pass). That is not
evidence of an improvement, and it's one benchmark.

One note for reproducing this: maya-ds4 isn't deterministic across processes even with greedy --ids. The same
binary on the same input diverged from itself at token 20, so compare against a same-config repeat before reading
token-level differences.

The adapter used above is at
https://huggingface.co/BoldingBuilds/DeepSeek-V4.1-Flash-1017-Abliteration-LoRA-GGUF (MIT).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/mw00/project-maya/pull/469 **Author:** [@JoshBolding](https://github.com/JoshBolding) **Created:** 10/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `pr/ds4-ablate-v1126` --- ### 📝 Commits (2) - [`1b216f6`](https://github.com/mw00/project-maya/commit/1b216f60ea2129b7d8a480751a980ed3806d3692) ds4: --ablate, a refusal direction projected out of every residual write - [`7836219`](https://github.com/mw00/project-maya/commit/78362195803b7333ef2859acbe8299b1da2e024e) ds4: --ablate loader on GgufFile, bounds-checked, with tests and docs ### 📊 Changes **9 files changed** (+505 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+4 -0) 📝 `docs/SETTINGS.md` (+20 -0) ➕ `src/ds4/ds4_ablate.hpp` (+187 -0) 📝 `src/ds4/ds4_kernels.cu` (+13 -0) 📝 `src/ds4/ds4_kernels.cuh` (+2 -0) 📝 `src/ds4/ds4_main.cpp` (+17 -0) 📝 `src/ds4/ds4_model.cu` (+33 -0) 📝 `src/ds4/ds4_model.hpp` (+6 -0) ➕ `tests/ds4_ablate_test.cpp` (+223 -0) </details> ### 📄 Description Adds an opt-in `--ablate` direction projection to `maya-ds4`: ``` --ablate FILE [--ablate-layer N] [--ablate-scale F] ``` Each layer's attention output and MoE output become `y - s r (r . y)` right before `hc_post`, for a unit direction `r`. The intended use is refusal ablation (abliteration). The direction comes from a `llama-cvector-generator` GGUF (`direction.N`) or from a rank-1 abliteration LoRA GGUF, whose `lora_b` is `-r` and whose `abliterate.*` keys give the layer and scale. Without the flag nothing is loaded and no kernel is launched. ## How it differs from `cvec` `--control-vector-scaled --cvec-mode project` already has the projection primitive, but it acts on the accumulated hc residual streams after the FFN. This patch acts on each attention and MoE contribution before it enters them, which is where an abliteration LoRA on `attn_output_b` / `ffn_down_shexp` / `ffn_down_exps` acts. So raising cvec's `n_embd` cap (4096; this model is 5120) and wiring it into `maya-ds4` would not reproduce this placement. I kept the DS4 code separate rather than change a kernel other models use. I'm happy to share the projection kernel or move this behind `cvec` if you'd prefer that. ## What it does with an adapter Only `r` and the scale are taken from an adapter. Its `lora_a` halves (`s r^T W`, computed from the weights it was built against) are not used. On those same weights the result equals the adapter: `(W - s r r^T W) x = y - s r (r . y)` for each writer, and the MoE output is the sum of the shared and routed writers. On other weights it is the exact projection, which differs from the adapter by `s r r^T (W - W_source) x`. An adapter has to carry the same `-r` on all three writers of every block, for exactly the model's block count. Anything else is refused, because projecting every write would not be what it does: an adapter covering only some blocks, one without the routed experts, or an arbitrary LoRA. ## Changes - `src/ds4/ds4_ablate.hpp`: the loader, built on the existing `GgufFile` reader. Every tensor's bytes are checked to lie inside the file, metadata integers are range-checked before conversion, and the scale must be positive and finite. - `src/ds4/ds4_kernels.cu`: `project_out`, one block per row, using the existing `block_sum`. - `src/ds4/ds4_model.cu`: the hooks on the window and chunk paths, after the tensor-parallel all-reduce. - `src/ds4/ds4_main.cpp`: the flags. `--ablate-layer` takes a whole number from 1 to 4096 and `--ablate-scale` a number from 1e-6 to 1e6. Anything else exits with an error, so a typo can't fall back to the default strength. - `tests/ds4_ablate_test.cpp` (CTest, no model, no GPU): 25 checks on synthetic GGUFs. They cover what loads (control vectors, whole-model adapters, scale resolution) and what is refused: partial or deeper adapters, mismatched directions, tensor offsets that wrap or run past the end, out-of-range metadata, zero or non-F32 directions, truncated files, bad scales. - `docs/SETTINGS.md`: a "DeepSeek: projecting a direction out" section. It's a flag rather than a `MAYA_DS4_*` variable because it names a file to load at startup, the way `--expert-profile` does. I can move it to an env var if you'd prefer. ## Testing - `ds4_ablate_test` passes, including under ASan + UBSan with `-fsanitize=float-cast-overflow`. - `maya-ds4` builds (CUDA, sm_86) with no new warnings. Out-of-range `--ablate-layer` / `--ablate-scale` values (2147483648, 1e100, 1.5, 0, 1e-50, 1e7, nan, abc) exit 2 with a message. - On the real 40-block adapter and its control vector, the loader returns a direction byte-identical to the one the evaluations below used. - `python tools/check_hip_compat.py` passes. AMD build and runtime are not tested. ## Measurements All on `maya-ds4`, Maya-S pack, 4x RTX 3090, thinking off, stock vs `--ablate` with a layer-22 direction at scale 1. **Decode speed**, drafting off (`--dspark off`), 3 warm-ups + 10 timed 200-token requests per server start, stock and ablate alternated across restarts. The median change per adjacent pair was -3.00%, -2.81%, -3.84% and -1.47%, so about 3% lower decode throughput. These are four restart pairs, not independent trials. Alternating mattered: on this box decode creeps up over the first few restarts (37.5 -> 42.5 -> 44.0 tok/s for one config), and a stock-then-ablate sequential run made the ablated build look 13% faster. No consistent prompt-speed effect was resolved. A single-pass register-held variant of the kernel was no better (1.6% slower at 256 threads; +1.40% and -0.30% at 1024), so I kept the simple one. **Refusals**: 120 AdvBench prompts and 120 benign xstest_safe prompts. The eval prompts were checked against the 128 pairs the direction was fitted on: none match exactly, one partially. | | Stock | `--ablate` | |---|---:|---:| | Refuses harmful prompts | 98.3% (118/120) | 4.2% (5/120) | | Judge harmfulness score (0-1) | 0.013 | 0.851 | | Refuses benign prompts | 0% | 0% | Graded by prefix match and by a fixed judge model calibrated against a reference score before the run. The two agree. **HumanEval**, all 164, greedy, nothing hit the token limit: 156/164 -> 157/164. Paired on the same 164 problems, there were no pass-to-fail transitions in this run (156 pass both, 7 fail both, 1 fail-to-pass). That is not evidence of an improvement, and it's one benchmark. One note for reproducing this: `maya-ds4` isn't deterministic across processes even with greedy `--ids`. The same binary on the same input diverged from itself at token 20, so compare against a same-config repeat before reading token-level differences. The adapter used above is at https://huggingface.co/BoldingBuilds/DeepSeek-V4.1-Flash-1017-Abliteration-LoRA-GGUF (MIT). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-29 21:26:40 +07:00
Sign in to join this conversation.
No labels
pull-request
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
mw00/project-maya#546
No description provided.