Manuscript Offprint

arXiv:2611.08752

AttentionSwitch: Jailbreak Defense for LLMs via Joint Learning of Sparse Attention-Head Selection and Scaling

Doniyorkhon Obidov, Honggang Yu, Xiaolong Guo, Kaichen Yang

ABSTRACT

Large language models have achieved remarkable performance across a wide range of tasks, yet they remain susceptible to jailbreak attacks that circumvent safety mechanisms and elicit harmful or unintended outputs. Although safety can be improved through model fine-tuning, such approaches can be computationally expensive and may degrade previously acquired capabilities through catastrophic forgetting. Recent jailbreak defenses have therefore explored targeted interventions on specific model components associated with unsafe behavior. However, existing approaches typically decouple two inherently related decisions: where to intervene and how to modify the selected components. Such sequential optimization may be suboptimal because the optimal intervention location can depend on the intervention applied there, and vice versa. In this work, we introduce ATTENTIONSWITCH, the first jailbreak defense to jointly optimize intervention sites and strengths within a frozen LLM. Specifically, ATTENTIONSWITCH learns a sparse set of attention heads together with query-conditioned multiplicative scaling factors, allowing the outputs of the selected heads to be either attenuated or amplified without altering the directions of their representations. Across popular language models and jailbreak attacks, ATTENTIONSWITCH outperforms existing defenses, with particularly strong results when training data is scarce.

1 INTRODUCTION

Despite advances in the capabilities of large language models (LLMs), their safety mechanisms remain vulnerable to jailbreak attacks, in which adversarially constructed prompts elicit harmful or policy-violating responses that the model would otherwise be expected to refuse (Gu et al., 2025). The consequences of these attacks can be severe, ranging from the spread of propaganda and the distribution of malware to critical privacy risks where attackers compromise LLM-powered systems to exfiltrate sensitive data (Chu et al., 2025).

Traditional defenses improve model alignment through supervised or preference-based training, such as supervised fine-tuning (SFT) and reinforcement learning from human feedback (RLHF), but require costly retraining on substantial amounts of data and may reduce performance on previously learned capabilities (Ouyang et al., 2022; Luo et al., 2025). Recent work addresses these challenges by limiting intervention to safety-relevant model components. These approaches broadly operate either on hidden representations at selected layers (Shen et al., 2025; Zhao et al., 2025; Sheng et al., 2026; Obidov et al., 2026b) or on individual attention heads (Li et al., 2025b; Arai et al., 2025; Cai et al., 2026; Park et al., 2026). We discuss these methods in greater detail in Section 2.2.

Despite their differences, existing methods follow a diagnose-then-intervene paradigm: intervention sites are first identified, after which the intervention is optimized at the selected sites. Such a sequential approach may be suboptimal, as the optimal intervention location can depend on the

Figure

Figure 1: Overview of ATTENTIONSWITCH. It keeps the LLM frozen and selects a small subset of attention heads. At each token position, a positive scalar uniformly rescales the entire output of a selected head, changing its magnitude while preserving its direction and the relative composition of its features. The scaling factor is predicted dynamically from the head's own query representation. Head selection and scaling are jointly optimized.

intervention magnitude, and vice versa. In this paper, we propose ATTENTIONSWITCH, the first jailbreak defense to learn these two components jointly under a shared defense objective.

Motivated by prior work showing that attention heads exhibit functional specialization (Michel et al., 2019; Voita et al., 2019), ATTENTIONSWITCH asks a question: can jailbreak robustness be improved by selectively suppressing the contribution of some attention heads while amplifying others? ATTENTIONSWITCH keeps the pretrained model weights frozen and rescales the outputs of a sparse subset of attention heads. At each token position, the direction of a selected head's output representation is preserved, while its magnitude is adjusted by a scalar. The scalar value is predicted dynamically from the head's own query representation. The attention-head selection mask and the parameters of the scaling functions are optimized jointly. ATTENTIONSWITCH uses a Gumbel-Softmax-based relaxation of the non-differentiable head-selection operation, corresponding to arg max when selecting a single head and, more generally, to the Top-K selection operator when selecting multiple heads. Figure 1 provides an overview of ATTENTIONSWITCH.

In a different domain from jailbreak defense, joint attention-head pruning and model-parameter optimization has been studied for model compression (Li et al., 2021). However, pruning is itself an intervention, making that setting different from ours, where head selection is non-interventional. Moreover, the non-pruned model parameters are updated during training, whereas ATTENTIONSWITCH keeps the LLM frozen and learns a separate scaling intervention.

Recent work has also explored attention-head scaling for jailbreak defense. ASGuard independently rescales components within each selected head, which can change both the magnitude and direction of its output representation (Park et al., 2026). In contrast, at each token position, ATTENTIONSWITCH applies a single positive scalar to the entire head output, thereby changing only its magnitude while preserving its direction. Additionally, ASGuard uses a fixed scaling vector for each selected head, whereas ATTENTIONSWITCH intervenes dynamically based on the head's query representation. Importantly, ASGuard does not jointly optimize head selection and scaling, but first identifies the heads to intervene on and then learns the scaling intervention at those fixed locations.

Our main contributions can be summarized as follows:

  • We introduce the first jailbreak defense to jointly learn where to intervene and how strongly to intervene under a shared defense objective, rather than identifying intervention sites before optimizing the intervention.

  • We propose a jailbreak defense based on whole-head output scaling, which changes the magnitude of a head's output while preserving its direction. At each token position, our method applies a single positive scalar to the entire head output, maintaining the relative composition of its features.

  • We empirically demonstrate that our method can outperform both model-wide and targeted intervention defenses, particularly in low-data training regimes.

References (45)