[Model] Add opt-in runtime abliteration for Qwen3.5/Qwen3.8
Project a unit refusal direction out of the residual-stream writes (attention, linear-attention and MLP outputs, and token embeddings) at runtime, enabled by VLLM_ABLITERATION pointing to a JSON or safetensors file with a refusal_direction vector. A bare name resolves to VLLM_ABLITERATION_DIR, default /opt/vllm/abliteration. For one direction this equals the weight edit W' = W - r(r^T W), so a base checkpoint reproduces its abliterated release with no extra weights. Unset, the path loads nothing and adds no work. Co-Authored-By: Hinata <hinata@idihore.id> Signed-off-by: MOVZX <movzx@yahoo.com>
This commit is contained in:
@@ -61,6 +61,7 @@ from vllm.platforms import current_platform
|
||||
from vllm.sequence import IntermediateTensors
|
||||
from vllm.v1.attention.backend import AttentionType
|
||||
|
||||
from .abliteration import projector
|
||||
from .interfaces import (
|
||||
EagleModelMixin,
|
||||
HasInnerState,
|
||||
@@ -531,6 +532,7 @@ class Qwen3NextDecoderLayer(nn.Module):
|
||||
)
|
||||
|
||||
self.layer_scale = getattr(config, "layer_scale", False)
|
||||
self.abliterate = projector(config.hidden_size)
|
||||
if self.layer_scale:
|
||||
self.attn_layer_scale = torch.nn.Parameter(
|
||||
torch.zeros(
|
||||
@@ -575,6 +577,7 @@ class Qwen3NextDecoderLayer(nn.Module):
|
||||
)
|
||||
else:
|
||||
raise ValueError("Invalid layer_type")
|
||||
hidden_states = self.abliterate(hidden_states)
|
||||
|
||||
if self.layer_scale:
|
||||
if len(hidden_states.shape) == 2:
|
||||
@@ -603,6 +606,7 @@ class Qwen3NextDecoderLayer(nn.Module):
|
||||
)
|
||||
else:
|
||||
hidden_states = self.mlp(hidden_states)
|
||||
hidden_states = self.abliterate(hidden_states)
|
||||
|
||||
if self.layer_scale:
|
||||
if len(hidden_states.shape) == 2:
|
||||
@@ -679,9 +683,10 @@ class Qwen3NextModel(nn.Module, EagleModelMixin):
|
||||
self.norm = PPMissingLayer()
|
||||
|
||||
self.aux_hidden_state_layers: tuple[int, ...] = ()
|
||||
self.abliterate = projector(config.hidden_size)
|
||||
|
||||
def embed_input_ids(self, input_ids: torch.Tensor) -> torch.Tensor:
|
||||
return self.embed_tokens(input_ids)
|
||||
return self.abliterate(self.embed_tokens(input_ids))
|
||||
|
||||
@property
|
||||
def use_sequence_parallel(self) -> bool:
|
||||
|
||||
Reference in New Issue
Block a user