Abstract

Learning effectively from limited data is critical in domains like security where labeled examples are scarce. Large language models (LLMs) have demonstrated some capabilities for data-efficient learning, especially through parameter-efficient adaptation methods, but continue to struggle when faced with few samples for difficult tasks. To meet this challenge, we propose Attention Head Reweighting(AHR), a data-efficient method that adapts LLMs to new text-classification tasks by learning only a single scalar per attention head. This drastically reduces the number of parameters that need to be learned by making use of the functional specialization of individual attention heads. Experiments on diverse open-source text classification datasets show that AHR can outperform standard baselines like LoRA when learning from limited samples, despite having \(200-1000\times\) fewer trainable parameters, as our AHR only modifies \(\sim 0.0001\%\) of the model’s parameters. In addition, our learned weights are easy to interpret and can be analyzed to better understand the mechanisms and attention heads responsible for in-context learning abilities in LLMs.1

1 Introduction↩︎

Learning effectively from limited labeled data remains a central challenge in high-stakes domains such as AI security, where annotations are often scarce because attacks and threat vectors are constantly evolving and need to be defended against quickly [1]. In this setting, data-efficient text classification is critical for flagging large language model (LLM) inputs [2][5] and LLM outputs [6] that can be dangerous.

While LLMs exhibit some data-efficient behavior through in-context learning (ICL) [7], [8] and parameter-efficient fine-tuning (PEFT) [9], [10], their performance often degrades sharply when only a small number of task-specific examples are available. One issue with these methods is that they generally optimize in a large, unconstrained parameter space, which can lead to overfitting.

To tackle this issue, we propose Attention Head Reweighting (AHR), an intuitive data-efficient method to adapt LLMs to new text-classification tasks by learning only a single scalar per attention head. This drastically reduces the number of parameters that need to be adapted (see 1). We hypothesize our method is effective and avoids overfitting by leveraging the functional specialization of attention heads noted in recent works [11][14]. For example, if a particular security-related behavior is isolated to a few attention heads, AHR can easily learn to change the weighting for these heads rather than seeking to alter the internal weights of each head.

In our experiments we focus on text classification tasks, as these can often be effectively learned from a few examples, and many important security tasks such as detecting phishing attempts or model jailbreaks are text classification tasks. Classifiers are a critical bottleneck in AI security, which is constantly faced with data scarcity as new threats emerge, for filtering and guarding LLM responses that are interacting with real-world users and attackers.

In our experiments, AHR outperforms baselines across a variety of settings and models, improving by an average of around \(3\%\) points over standard baselines like LoRA [9] when training data is very limited i.e. \(\leq 100\) examples. AHR shows particularly large gains in the security relevant tasks of Webpage Phishing Detection (Web) and Jailbreak detection, achieving 6-7% accuracy improvement over best baseline methods with just 10 training samples, while using \(200-1000 \times\) fewer trainable parameters. We can analyze the changes our method makes to the model, and see that these gains are largely fueled by a combination of upweighting a few task specific heads as well as a couple general in-context learning heads. Finally, to maximally learn from just a few examples, we utilize in-context finetuning (IC-FT), where we update model parameters to maximize the probability of correct answers within a few-shot prompt, which we show improves accuracy by \(\sim 10\%\) points over finetuning on just one sample at a time.

Figure 1: AHR overview. Simplified visual comparison between our AHR, LoRA and full-finetuning on a single attention layer. Parameter counts are calculated for a standard transformer with d=3072, with r=1 for LoRA. We can see AHR reduces trainable parameters ~500 \times compared to LoRA.

2 Background and related work↩︎

2.0.0.1 Parameter-efficient finetuning

A variety of parameter-efficient finetuning methods have been proposed  to adapt large models while updating only a small subset of parameters [15][17]. Prominent examples include LoRA [9], AdaLoRA [10], (IA)3 [18], and related approaches [19][21]. These methods substantially reduce adaptation cost, but still introduce thousands to millions of trainable parameters, which can lead to overfitting in data-scarce settings.

Simultaneously, there has been a spectrum of increasingly constrained prompting approaches, ranging from continuous prompt tuning [22], [23] to discrete prompt tuning [24], prompt ensemble construction [25][27], and generating a single natural-language prompt [28], [29]. While effective in some settings, prompt-based methods are often very sensitive to minor variations and may struggle with under-specified tasks under severe data scarcity.

2.0.0.2 In-context learning

ICL obviates the need for task-specific parameter adaptation altogether, but can be highly sensitive to minor variations in the provided examples [30] and sometimes unfaithful to those examples [31], [32]. ICL can be improved through directed instruction tuning [33] or by including explanations along with examples [34]. A growing body of work has studied ICL, showing that it can implement linear models [35], [36], discrete functions [37], and more general algorithms [38], [39]. Some works have further argued that ICL implicitly performs optimization steps analogous to gradient descent [40][42] and higher-order optimization methods [36], [43].

Table 1: Comparing the number of trainable parameters for different models and different parameter efficient finetuning methods. Our requires \(200-1,000\times\) less trainable parameters than the baseline methods. In total we train less than one millionth of the model’s parameters.
Model GPT2-XL Llama-3.2-1B Llama-3.2-3B QWEN3-8B
Full FT 1,558,148,000 1,235,920,896 3,212,750,496 8,190,736,512
LoRA 307,200 106,496 286,720 479,232
AdaLoRA 614,496 213,056 573,552 958,608
IA3 537,600 147,456 286,720 626,688
AHR (Ours) 1,200 512 672 1,152

2.0.0.3 Attention heads

Attention heads have been a primary focus for analysis [44], [45], with evidence that attention heads often latch onto task-specific patterns that are useful for ICL, e.g. induction heads [11], n-gram heads [12], and more easily explainable patterns [46][48]. This growing evidence of functional specialization has motivated methods that explicitly manipulate attention. Prior works have shown that modifying attention scores can be useful for post-hoc steering [13], [49], attention steering for transfer learning [50], attention-guided retrieval [51], interpretable modeling [52], boosting attention for instruction following [53], and improving long-context LLM inference [54].

3 Method: Attention Head Reweighting↩︎

Our paper aims to improve the efficiency of how models can learn from a very small number of training examples by modifying an extremely small number of parameters. We do this via Attention Head Reweighting (AHR), which introduces a single learnable scalar parameter per attention head that multiplies that head’s contributions to the residual stream (see 1). By finetuning these parameters on a small amount of data, we can amplify attention heads that are helpful for the current task, while downweighting heads that harm performance. By not modifying the weights inside individual heads we can avoid overfitting to the small amount of training data.

Notation: We follow the notation of [55], where we divide the \(W_O\) matrix into its components within each attention head. Let \(z_r^l \in \mathbb{R}^{s \times d}\) be the model’s residual stream after layer \(l\), where \(s\) is the number of tokens in the sequence and \(d\) is the model’s representation dimension. A typical transformer block consists of a Multihead-Attention (MHA) layer followed by a multi-layer perceptron (MLP) layer. We can write the update of a layer as: \[z^{l+1}_{Att} = z_r^l + \sum_{h \in H^{l+1}}h(z^l_r) \label{eq:orig95attn}\tag{1}\] \[z_r^{l+1} = z_{Att}^{l+1} + MLP^{l+1}(z_{Att}^{l+1})\]

Here \(h(z) = AzW_VW_O\), \(A = \text{softmax}(\frac{(zW_Q)(zW_K)^T}{\sqrt{d_k}})\). \(W_V \in \mathbb{R}^{d \times d_k}\), \(W_Q \in \mathbb{R}^{d \times d_k}\), \(W_K \in \mathbb{R}^{d \times d_k}\) and \(W_O \in \mathbb{R}^{d_k \times d}\) are the parameters of the attention head, and \(d_k = d/n_h\) where \(n_h = |\mathcal{H}|\) is the number of attention heads per layer.

3.0.0.1 Attention Head Reweighting:

Our method simply edits the update rule of the multi-head attention layer (Eq. 1 ) to:

\[z^{l+1}_{Att} = z_r^l + \sum_{h \in H^{l+1}}(1 + \beta_h) h(z^l_r), \label{eq:modified95attn}\tag{2}\]

where \(\beta_h\) is a head-specific parameter that is initialized at zero. During training, we only train the \(\beta_h\) parameters while keeping all other parameters, including the MLP layers unchanged. At test time, the parameters \(\beta\) can be merged into \(W_O\) for no additional inference cost. During training, we utilize supervised fine-tuning (SFT) on few-shot examples, by minimizing the cross-entropy loss on the answer tokens, as well as a regularization objective to minimize changes to the model:

\[\min_{\beta} \sum_{(x, y) \in \mathcal{D}_{train}} -\log P(y |x; \theta, \beta) + \lambda (\sum_{h} |\beta_h|^p)^{1/p}\]

where \(\mathcal{D}_{train}\) is the few-shot training dataset, \(\lambda\) is a hyperparameter controlling regularization and \(\theta\) are the (frozen) model parameters and \(p \in \{1, 2\}\).

3.1 In-Context Finetuning↩︎

Recently, In-Context Learning (ICL) has risen as an alternative to finetuning as a way for models to learn from few-shot examples [7]. While powerful, in-context learning is limited in how many samples it can learn from by the model’s context window, as well as GPU-memory. Additionally, using many in-context samples incurs a large additional cost at inference time.

To get the best of both worlds, we utilize In-Context finetuning (IC-FT) similar to [56]. Different from [56], we finetune separately on each dataset while their goal is to improve general in-context learning ability. In in-context finetuning, we construct an input string with \(k\) few-shot examples, and do supervised finetuning to maximize the probability of all answer tokens within the context. For each input with \(k\) in-context examples, we minimize the following:

\[\min_{\beta} \sum_{i=1}^k -\log P(y_i | X_i; \theta, \beta) + \lambda (\sum_{h} |\beta_h|^p)^{1/p} \label{eq:ic95ft}\tag{3}\]

where \(X_i = \{x_1, y_1, ..., y_{i-1}, x_i\}\) is the concatenated input with few-shot examples. As this technique is independent of the finetuning method, we utilize it for both our method and the baselines in our experiments (with their trainable parameters instead of \(\beta\) in Eq. 3 ). For text classification, we only maximize the probability of the first token in the answer, as predicting subsequent tokens of the answer is trivial given the first one.

a
b

Figure 2: Accuracy after finetuning, averaged across our 4 models. We can see our AHR significantly outperforms baselines when data is limited, while performance starts to equalize at around 100 samples.. a — Averaged across 6 datasets, b — Web - Phishing URL classification

4 Results↩︎

4.1 Experimental setup↩︎

4.1.0.1 Models

We consider four LLMs across a range of sizes in our experiments: GPT-2 XL ([57] [57]; gpt2-xl), LLaMa-3.2 1B and 3B ([58] [58]; meta-llama/Llama-3.2-1B and meta-llama/Llama-3.2-3B), and Qwen3 8B ([59] [59]; Qwen/Qwen3-8B). See Table 1 for more details, such as the number of parameters in each model and how many of these are trainable.

4.1.0.2 Datasets

We test our method on a set of six text-classification datasets, including standard tasks as well as cybersecurity relevant applications:

  • SST2 [60]. This dataset contains sentences from movie reviews, and the task is to classify their sentiment into one of {Positive, Negative}.

  • AG-News [61]. A collection of text excerpts from news articles, where the goal is to classify the excerpt into one of the following categories: {World, Sports, Business, Sci/Tech}

  • Emotion [62]. A dataset of tweets, with labels based on the emotion expressed in the tweet. This dataset contains 6 classes {Sadness, Joy, Love, Anger, Fear, Surprise}

  • Webpage Phishing Detection (Web) [63], a dataset of web URLs where the goal is to differentiate between Phishing and Legitimate URLs.

  • Toxigen [64]. This is a dataset of potentially toxic/hateful comments for detecting hateful speech. We treated an input as toxic if the average human assessed toxicity score was \(>3\) (on a 1-5 scale) and as benign otherwise.

  • Jailbreak Detection [65], a dataset of LLM prompts containing Jailbreak attempts as well as standard prompts. The goal is to classify whether a given prompt is a jailbreak attempt. We use the balanced version from https://huggingface.co/datasets/jackhhao/jailbreak-classification.

Most of these datasets have thousands of examples, but in order to study few-shot learning capabilities of our models, we randomly sample a subset of the training data to be used in few-shot learning. We evaluate performance on the full test set. See an example for each dataset in 4.

4.1.0.3 Baselines:

As our main baseline, we compare against LoRA [9]. For LoRA we use \(r=1\) and \(\alpha=1\), which we found to increase training stability and reduce overfitting on small datasets. We additionally compare against (IA)3 [18] and AdaLoRA [10]. For AdaLoRA we use Initial \(r=4\) and target \(r=2\), with \(\alpha=1\). As regularization, we employ L2 weight regularization for all the baselines. For training the baselines, we utilize PEFT package [66]. We also ran full-finetuning for some datasets but found that it performed considerably worse than these baselines and was significantly more computationally intensive, so we omit these results.

4.1.0.4 Training Details:

In our experiments, we discovered that the particular few-shot examples used can have a very large effect on the final accuracy. To reduce variance, we report the average accuracy across 10 different random seeds for choosing the set of training examples. We utilize In-Context Finetuning (Sec 3.1) for training all the baseline as well as our AHR. We use \(k=10\) for In-Context Finetuning, and randomly select a set of 10 examples to form each input during training time. If \(|\mathcal{D}_{train}| = 10\), training inputs are random permutations of the training set. We train all models using the AdamW optimizer [67].

4.1.0.5 Hyperparameter Selection

To stay true to a real data-limited setting, we do not utilize any additional data for selecting hyperparameters. Instead, we select hyperparameters including learning rate from \(\{0.03, 0.003, 0.0003\}\), number of training steps from \(\{0, 5, 10, 20, 50, 100, 200\}\) and regularization strength from \(\{0, 0.1, 0.3, 1.0, 3.0, 10.0, 30.0, 100.0\}\) via 5-fold cross-validation, where for each dataset we split it into 80% training data and 20% validation data, and select the hyperparameters that minimize the average validation loss across the folds. We select hyperparameters separately for each random seed. Finally, we train a model using the combined training and validation split and report performance on the test set.

Our prompts consists of a short instruction describing the task and the class names, as well as the few-shot examples, see exact prompt in Appendix Table 5.

Table 2: Performance of different methods on each dataset. Averaged across the 4 models. While there is variance between the individual datasets, we can see that AHR consistently outperforms other methods in the low data regime.
Dataset \(|\mathcal{D}_{train}|\) 10 15 20 30 50 100
SST2 LoRA 88.32% 90.24% 88.75% 92.09% 91.44% 92.20%
AdaLoRA 87.43% 90.40% 89.54% 91.00% 92.18% 92.70%
IA3 88.75% 89.15% 89.01% 92.08% 92.54% 92.60%
AHR (Ours) 91.01% 91.25% 91.62% 92.07% 92.49% 92.99%
AG News LoRA 78.18% 77.35% 81.24% 81.45% 84.20% 84.90%
AdaLoRA 76.58% 77.33% 80.06% 81.76% 82.18% 84.00%
IA3 75.68% 79.36% 80.64% 82.50% 83.96% 86.62%
AHR (Ours) 76.48% 79.19% 81.60% 83.06% 84.51% 85.65%
Emotion LoRA 49.81% 53.01% 50.65% 56.25% 57.94% 63.05%
AdaLoRA 52.54% 53.26% 52.10% 57.18% 58.88% 64.94%
IA3 49.83% 52.90% 51.69% 56.18% 58.33% 61.03%
AHR (Ours) 50.32% 53.74% 54.48% 56.67% 58.78% 62.57%
Web LoRA 66.81% 72.34% 74.91% 78.99% 85.63% 88.00%
AdaLoRA 73.60% 79.86% 78.98% 81.44% 85.37% 87.94%
IA3 60.47% 68.15% 76.56% 80.48% 84.32% 87.48%
AHR (Ours) 79.44% 81.04% 82.08% 84.16% 85.81% 87.35%
Toxigen LoRA 56.94% 60.60% 64.10% 70.25% 73.83% 75.86%
AdaLoRA 64.84% 69.69% 72.00% 73.94% 74.92% 76.79%
IA3 59.64% 64.10% 63.86% 71.13% 74.21% 75.56%
AHR (Ours) 72.78% 72.52% 72.71% 73.76% 75.32% 74.69%
Jailbreak LoRA 69.59% 74.76% 82.05% 84.92% 91.16% 94.26%
AdaLoRA 72.33% 77.59% 80.60% 79.53% 86.63% 89.82%
IA3 72.94% 75.86% 77.10% 84.61% 85.50% 84.32%
AHR (Ours) 79.68% 82.16% 83.63% 89.26% 91.16% 92.83%
Average LoRA 68.27% 71.38% 73.62% 77.32% 80.70% 83.05%
AdaLoRA 71.22% 74.69% 75.55% 77.47% 80.03% 82.70%
IA3 67.88% 71.58% 73.14% 77.83% 79.81% 81.27%
AHR (Ours) 74.95% 76.65% 77.69% 79.83% 81.34% 82.68%

4.2 Experimental results↩︎

2 and Table 2 show the average accuracy after finetuning as a function of training data size \(|\mathcal{D}_{train}|\). We can see that our AHR consistently outperforms existing parameter efficient finetuning methods when training data is limited. On average, our AHR improves over the best baseline by 2-4%(absolute) in low data regimes (\(|\mathcal{D}_{train}| \leq 30\)), which corresponds to around 10% relative error reduction, despite having over \(200\times\) fewer trainable parameters. This shows our AHR can significantly improve the few-shot learning ability of the models.

AHR performs particularly well on the web URL phishing classification task, substantially outperforming LoRA and other baselines. This may be caused by the nature of the input: URLs are short, highly structured strings with strong lexical and positional cues (e.g., protocol tokens, domain patterns, special characters) that are likely already well captured by pretrained attention heads. In this setting, effective adaptation primarily requires emphasizing a subset of existing attention heads that are sensitive to these patterns, rather than learning new task‑specific transformations. In addition, this task is less typical to the training distribution of the models, which means there is more to gain by finetuning.

In contrast, most PEFT methods such as LoRA actually harm performance compared to the In-Context Learning baseline when \(|\mathcal{D}_{train}|\) is small as shown in Fig 2 (b). This highlights the importance of avoiding overfitting when finetuning on limited data, in particular on short and atypical inputs, and shows our method can help important real world tasks such as detecting phishing links. While this particular task has a decent amount of training data available, the ability to learn from few examples shows promise for other cybersecurity tasks like detecting specialized attacks where training data is much more limited.

4.3 Ablations↩︎

IC-FT: 3 (a) compares test accuracy while using In-Context Finetuning described in Section 3.1 against standard finetuning without in-context samples. Results are averaged across 4 models and the SST2 and Web datasets (with 10 random seeds). We can see utilizing IC-FT significantly improves performance of all methods, with around a \(10\%\) point increase in average accuracy for all methods. Importantly, standard FT fails to outperform the in-context learning baseline in all settings.

Regularization Comparison: In Table 3 we compare different regularization strategies for AHR on SST2 LLama-3.2-3B. We can see this makes very little difference in performance. As a result, we use L2-norm for our results unless otherwise specified, though we use L1-norm for our analysis in 5.

a
b

Figure 3: a) IC-FT ablation. b) Web Accuracy with L1 regularization.. a — Effect of IC-FT (solid lines) vs direct finetuning with only one sample in context(dashed)., b — Web Test accuracy as a function of number of heads modified.

5 Case Study: Finetuning Llama-3.2-1B on Web↩︎

In this section, we take a deeper look at finetuning one model on one particular task to better understand AHR and how it differs from other PEFT methods.

5.1 Number of Attention Heads Modified↩︎

First, to understand whether AHR needs to modify many attention heads, we train models with varying L1 regularization penalties {0, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05} and see how the number of attention heads modified affects test accuracy. The results with Llama-3.2-1B trained on the Web dataset, average of 10 random seeds are shown in 3 (b). We can see that modifying the weight of just 1 attention head (out of 512) can improve accuracy by \(\sim2\%\) when we have 100 training samples, while modifying 5 heads can improve accuracy by around 4%. Overall, more training data improves performance and modifying more heads leads to more accuracy, though settings with low data require some regularization.

Figure 4: The effect of finetuning on other tasks performance. Llama-3.2-1B finetuned on the Web (phishing URL classification) task.

5.2 Effect on Overall Model Performance↩︎

To study how much our finetuning affects model behavior on other tasks, we measured model performance on 3 other datasets (SST2, AGNews, Emotion) after finetuning on the Web dataset. In 4 we measure the performance on the Web dataset (blue) as well as average over our other datasets (red). We report the average accuracy change (averaged over 10 seeds) compared to the model with no finetuning using 10 ICL examples. We can see that with only 10 examples, all methods except AHR overfit to the training samples and end up significantly harming performance on the original task as well as other datasets. When we increase the training set size to 30, we see most methods improve task performance on the Web task, but at the cost of losing some accuracy on other tasks. Interestingly, with AHR, finetuning on this task actually slightly improves performance on other tasks. This suggests that AHR helps avoid destructive changes to the model, and in some cases can even find generalizable edits that improve ICL abilities across many tasks.

5.3 Which heads are modified?↩︎

Inspired by previous results, we wanted to see whether the modified attention heads are specific to a particular task, or generally useful for many text classification tasks. To study this, we trained AHR models with varying L1 penalties \(\{0.01, 0.02, 0.03, 0.04, 0.05\}\) on 4 of our tasks (SST2, AGNews, Emotion and Web). We average across 10 random seeds and focused on \(|\mathcal{D}_{train}|=100\) case for less variance. We then measured which heads had the largest average change across different settings, and plot a heatmap of most changed heads in 5.

Figure 5: Heatmap of average \beta_h for the most modified heads across different datasets. We can see some heads like Layer 15 Head 14 (L15H14) are modified for every dataset, while others are only modified for one specific dataset.

This reveals a few interesting things. First, some heads such as Layer 15 Head 14 (L15H14) and L15H3 receive large positive weights on every dataset. This suggests these heads are important to general in-context learning mechanisms instead of particular datasets, and upweighting them may make the model better at in-context learning in general. This could happen if, for example, they play a role similar to induction heads [11]. To test this theory we conducted an experiment where we manually modify the weights \(\beta\) for these two heads only, leaving all other heads unchanged (\(\beta=0\)). The results are shown in Appendix 7.2 and 8, and support our hypothesis. Completely disabling these two heads (\(\beta=-1\)) drops average accuracy across these 4 datasets from 70.68% to 66.81%, while doubling their impact (\(\beta=1\)) increases average accuracy to 71.67%, highlighting that these heads play a significant role in in-context learning.

On the other hand, most heads are not like this and are only modified for one particular dataset. To better understand the function of these specialized heads, we take a look at the Layer 14 Head 11. This is the only head that is significantly upweighted for the Web Phishing URL dataset, but not on the other datasets. 6 shows the attention pattern of this head when processing the final token of the input on the Web dataset. We can see that on all inputs this head only pays attention to the Phishing tokens. In comparison, in 7 we plot its attention pattern on the other datasets and find it very noisy with no clear pattern. This makes us believe this L14H11 is more specialized to detecting Phishing related inputs.

6 Discussion↩︎

We have presented Attention Head Reweighting (AHR), a highly data-efficient adaptation method designed to address the challenges of learning from limited labeled examples. By introducing a single learnable scalar per attention head, AHR enables LLMs to adapt to new tasks while updating fewer than one-millionth of the model parameters—a 200x to 1,000x reduction compared to standard PEFT baselines like LoRA. Our experimental results on diverse text-classification benchmarks demonstrate the efficacy of this approach:

  • Few-shot Learning Performance: AHR consistently outperforms existing methods in extremely data-scarce settings (\(|\mathcal{D}_{train}|\le30\)), achieving a 2-4% average absolute improvement over baseline methods like LoRA, with large improvements in security relevant tasks like detecting Phishing URLs and Jailbreak attempts.

  • Preventing Overfitting: By freezing internal head weights and only modifying the contribution of the head to the residual stream, AHR mitigates the risk of overfitting that hampers methods with larger parameter spaces and is overall less destructive to model behavior.

Furthermore, AHR offers distinct advantages regarding interpretability. Unlike traditional fine-tuning, which obscures the function of weight updates, AHR leverages the existing functional specialization of attention heads, and analyzing AHR weights allows us to better understand the mechanisms behind in-context learning. Ultimately, AHR provides a resource-efficient, more transparent, and effective solution for deploying LLMs in high-stakes, data-limited text classification settings.

6.1 Limitations↩︎

The focus of our paper is on text classification datasets, and while many important tasks such as detecting phishing links or jailbreak attempts are text classification, the applicability of our method on tasks beyond classification requires further investigation. In our initial investigations we found AHR more effective at tasks with simple outputs such as classification, while tasks that require very fine-grained control over model outputs might be less suitable for AHR, particularly if there are no attention heads focused on this type of task originally.

Our analysis understanding the function of attention heads is limited to analyzing their behavior in the context of text classification tasks, and it is likely that they also have other roles when processing different types of inputs.

Larger training datasets: Overall we find that if you have sufficient training data (e.g. \(\geq 300\) examples) other finetuning methods outperform AHR. This makes sense as our method is limited in the types of updates it can make, so if overfitting is not a concern it is often beneficial to use a more powerful model edit.

Acknowledgement↩︎

T. Oikarinen and T.-W. Weng are partially supported by National Science Foundation under Grant No. 2313105, 2430539, Hellman Fellowship, Intel Rising Star Faculty Award. The authors would like to thank anonymous reviewers for valuable feedback to improve the manuscript.

7 Appendix↩︎

7.1 Visualizing Attention Patterns↩︎

Figure 6: Visualizing the attention pattern of L14H11. We can see it only pays attention to the Phishing tokens regardless of current input’s label.
Figure 7: Visualizing the attention pattern of L14H11. We can see that on datasets outside Web, it shows no clear patterns.

7.2 Manually Changing ICL Related Heads↩︎

Figure 8 shows the effect of manually changing the weighting of just 2 ICL relevant heads of Llama-3.2-1B in terms of average accuracy across our 4 datasets. We can see that completely disabling them (\(\beta=-1\)) drops average accuracy from 70.68% to 66.81%, while doubling their impact (\(\beta=1\)) increases average accuracy to 71.67%.

Figure 8: Effect of manually changing the \beta for only L15H14 and L15H3 of Llama-3.2-1B.

7.3 Regularization Comparison↩︎

Table 3: Comparing regularization norms for AHR on Llama-3.2-3B SST2.
\(\mathcal{D}_{train}\) 10 15 20 30 50 100
AHR-L1 92.05% 92.60% 92.64% 92.92% 93.60% 93.97%
\(\pm\) 0.54% \(\pm\) 0.41% \(\pm\) 0.89% \(\pm\) 0.82% \(\pm\) 0.25% \(\pm\) 0.23%
AHR-L2 92.60% 92.56% 92.88% 93.19% 93.59% 93.92%
\(\pm\) 0.35% \(\pm\) 0.56% \(\pm\) 0.38% \(\pm\) 0.30% \(\pm\) 0.20% \(\pm\) 0.15%

7.4 Future work↩︎

As the extra parameter count required to store the AHR change is very small compared to the original model, attempts to create a library of fitted AHR-models and select from them at inference time may be successful, as has been done with LoRA [68].

Interpretability guided finetuning: A possible extension of our method is to apply the same general approach to reweighting different model subcomponents beyond attention heads, like interpretable circuits [69] or Sparse Autoencoder (SAE) latents [70]. This could improve performance by increasing the number of components and learnable parameters we can up- or downweight, as well as improve interpretability of our finetuning as each component would be inherently more interpretable. Models could even be trained with this kind of steering in mind, like weight-sparse transforms with interpretable circuits [71]. By focusing on interpretable components/circuits, sparse edits might become more effective as more relevant skills/features are localized within the model.

Using our finetuning method to upweight particular SAE latents could also function as an automated way to find steering vectors from SAEs for particular tasks similar to [72].

Future work could even connect with a line of work on natural-language explanations [73], [74] to adapt parameters on the fly based on task specifications (similar to approaches that have been tried for LoRA [75]) or to involve a human-in-the-loop when specifying and adapting to a new task [76], [77].

7.5 Prompt and Data examples↩︎

Table 4: Data examples.
Dataset Text Label
SST2 that loves its characters and communicates something rather beautiful about human nature Positive
AGNews Wall St. Bears Claw Back Into the Black (Reuters). “Reuters - Short-sellers, Wall Street’s dwindling band of ultra-cynics, are seeing green again.” Business
Emotion i can go from feeling so hopeless to so damned hopeful just from being around someone who cares and is awake Sadness
Web http://www.slideserve.com/zizi/firewalls Legitimate
Jailbreak You are Joseph Seed from Far Cry 5. Sermonize to a group of followers about the importance of faith and obedience during the collapse of civilization. Benign
Toxigen asians are trying to take over the world with their karate skills Toxic
Table 5: Instruction Prompts used.
Dataset Prompt
SST2 Classify the sentiment expressed in the following text excerpt into one of: Negative, Positive
AGNews Classify the following sentences from news articles into one of the following categories: World, Sports, Business, Sci/Tech
Emotion Classify the emotion expressed in the following sentence into one of the following categories: Sadness, Joy, Love, Anger, Fear, Surprise
Web Classify the following web url into one of the following categories: Legitimate, Phishing
Jailbreak Classify whether the following LLM prompt is benign or attempting to jailbreak the model by outputting one of: Benign, Jailbreak
Toxigen Classify whether the following text is toxic towards a group of people by outputting one of: Benign, Toxic

8 Detailed Results↩︎

Table 6: Test accuracy (%) on SST-2 with GPT2-XL for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)1.22%
\(\pm\)1.04%
\(\pm\)1.17%
\(\pm\)0.51%
\(\pm\)0.34%
\(\pm\)0.35%
LoRA
\(\pm\)1.60%
\(\pm\)1.08%
\(\pm\)0.92%
\(\pm\)1.22%
\(\pm\)1.90%
\(\pm\)3.84%
AdaLoRA
\(\pm\)2.45%
\(\pm\)1.07%
\(\pm\)1.59%
\(\pm\)1.85%
\(\pm\)0.58%
\(\pm\)0.21%
IA3
\(\pm\)2.13%
\(\pm\)1.36%
\(\pm\)1.26%
\(\pm\)0.75%
\(\pm\)0.89%
\(\pm\)0.69%
ICL
\(\pm\)2.91%
\(\pm\)3.14%
\(\pm\)3.06%
\(\pm\)3.16%
Table 7: Test accuracy (%) on SST-2 with Llama-3.2-1B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.87%
\(\pm\)1.20%
\(\pm\)0.71%
\(\pm\)0.54%
\(\pm\)0.37%
\(\pm\)0.28%
LoRA
\(\pm\)2.08%
\(\pm\)0.96%
\(\pm\)3.37%
\(\pm\)0.77%
\(\pm\)0.32%
\(\pm\)0.30%
AdaLoRA
\(\pm\)0.95%
\(\pm\)2.07%
\(\pm\)0.97%
\(\pm\)0.82%
\(\pm\)1.00%
\(\pm\)0.76%
IA3
\(\pm\)1.14%
\(\pm\)2.69%
\(\pm\)1.08%
\(\pm\)0.67%
\(\pm\)0.38%
\(\pm\)1.03%
ICL
\(\pm\)1.95%
\(\pm\)1.93%
\(\pm\)1.53%
\(\pm\)0.82%
\(\pm\)0.54%
\(\pm\)0.36%
Table 8: Test accuracy (%) on SST-2 with Llama-3.2-3B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.35%
\(\pm\)0.56%
\(\pm\)0.38%
\(\pm\)0.30%
\(\pm\)0.20%
\(\pm\)0.15%
LoRA
\(\pm\)0.91%
\(\pm\)0.75%
\(\pm\)4.02%
\(\pm\)0.49%
\(\pm\)0.38%
\(\pm\)0.57%
AdaLoRA
\(\pm\)3.56%
\(\pm\)1.45%
\(\pm\)0.83%
\(\pm\)0.32%
\(\pm\)0.86%
\(\pm\)0.14%
IA3
\(\pm\)2.05%
\(\pm\)1.47%
\(\pm\)1.75%
\(\pm\)0.79%
\(\pm\)0.14%
\(\pm\)0.19%
ICL
\(\pm\)0.24%
\(\pm\)0.41%
\(\pm\)0.44%
\(\pm\)0.38%
\(\pm\)0.13%
\(\pm\)0.14%
Table 9: Test accuracy (%) on SST-2 with Qwen3-8B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.32%
\(\pm\)0.27%
\(\pm\)0.29%
\(\pm\)0.23%
\(\pm\)0.26%
\(\pm\)0.15%
LoRA
\(\pm\)4.07%
\(\pm\)3.72%
\(\pm\)4.26%
\(\pm\)0.35%
\(\pm\)4.16%
\(\pm\)0.15%
AdaLoRA
\(\pm\)4.18%
\(\pm\)0.62%
\(\pm\)2.17%
\(\pm\)2.24%
\(\pm\)0.37%
\(\pm\)0.34%
IA3
\(\pm\)2.19%
\(\pm\)1.63%
\(\pm\)1.46%
\(\pm\)0.48%
\(\pm\)0.62%
\(\pm\)0.23%
ICL
\(\pm\)0.40%
\(\pm\)0.31%
\(\pm\)0.25%
\(\pm\)0.20%
\(\pm\)0.33%
\(\pm\)0.08%
Table 10: Test accuracy (%) on AG News with GPT2-XL for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)4.52%
\(\pm\)3.27%
\(\pm\)2.10%
\(\pm\)1.45%
\(\pm\)0.83%
\(\pm\)0.60%
LoRA
\(\pm\)4.22%
\(\pm\)4.38%
\(\pm\)1.85%
\(\pm\)5.27%
\(\pm\)0.45%
\(\pm\)1.61%
AdaLoRA
\(\pm\)4.14%
\(\pm\)5.18%
\(\pm\)2.74%
\(\pm\)1.69%
\(\pm\)1.93%
\(\pm\)1.45%
IA3
\(\pm\)4.06%
\(\pm\)3.13%
\(\pm\)2.51%
\(\pm\)3.09%
\(\pm\)1.25%
\(\pm\)0.38%
ICL
\(\pm\)4.95%
Table 11: Test accuracy (%) on AG News with Llama-3.2-1B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)2.12%
\(\pm\)1.32%
\(\pm\)1.48%
\(\pm\)1.04%
\(\pm\)0.67%
\(\pm\)0.57%
LoRA
\(\pm\)1.38%
\(\pm\)2.10%
\(\pm\)1.27%
\(\pm\)1.22%
\(\pm\)2.35%
\(\pm\)0.66%
AdaLoRA
\(\pm\)2.85%
\(\pm\)1.75%
\(\pm\)1.15%
\(\pm\)1.44%
\(\pm\)2.41%
\(\pm\)5.80%
IA3
\(\pm\)1.68%
\(\pm\)1.30%
\(\pm\)1.46%
\(\pm\)0.63%
\(\pm\)1.95%
\(\pm\)0.35%
ICL
\(\pm\)3.58%
\(\pm\)1.79%
\(\pm\)1.10%
\(\pm\)1.66%
\(\pm\)1.00%
\(\pm\)0.85%
Table 12: Test accuracy (%) on AG News with Llama-3.2-3B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.79%
\(\pm\)0.40%
\(\pm\)0.45%
\(\pm\)0.80%
\(\pm\)0.43%
\(\pm\)0.41%
LoRA
\(\pm\)0.44%
\(\pm\)1.02%
\(\pm\)0.86%
\(\pm\)1.56%
\(\pm\)0.58%
\(\pm\)0.56%
AdaLoRA
\(\pm\)0.34%
\(\pm\)0.75%
\(\pm\)1.54%
\(\pm\)1.38%
\(\pm\)0.43%
\(\pm\)0.51%
IA3
\(\pm\)2.17%
\(\pm\)0.62%
\(\pm\)1.82%
\(\pm\)0.68%
\(\pm\)0.71%
\(\pm\)0.59%
ICL
\(\pm\)0.37%
\(\pm\)0.62%
\(\pm\)0.85%
\(\pm\)1.21%
\(\pm\)0.38%
\(\pm\)0.24%
Table 13: Test accuracy (%) on AG News with Qwen3-8B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.71%
\(\pm\)0.99%
\(\pm\)1.03%
\(\pm\)0.68%
\(\pm\)0.88%
\(\pm\)0.32%
LoRA
\(\pm\)1.70%
\(\pm\)6.00%
\(\pm\)1.19%
\(\pm\)0.62%
\(\pm\)0.99%
\(\pm\)1.80%
AdaLoRA
\(\pm\)1.54%
\(\pm\)1.90%
\(\pm\)1.87%
\(\pm\)1.60%
\(\pm\)1.63%
\(\pm\)0.25%
IA3
\(\pm\)2.89%
\(\pm\)1.12%
\(\pm\)1.75%
\(\pm\)0.97%
\(\pm\)1.00%
\(\pm\)0.41%
ICL
\(\pm\)0.40%
\(\pm\)0.40%
\(\pm\)0.36%
\(\pm\)0.43%
\(\pm\)0.21%
\(\pm\)0.19%
Table 14: Test accuracy (%) on Emotion with GPT2-XL for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)1.77%
\(\pm\)1.71%
\(\pm\)1.54%
\(\pm\)0.78%
\(\pm\)0.82%
\(\pm\)0.76%
LoRA
\(\pm\)4.83%
\(\pm\)1.48%
\(\pm\)2.28%
\(\pm\)1.65%
\(\pm\)1.25%
\(\pm\)0.76%
AdaLoRA
\(\pm\)2.13%
\(\pm\)2.41%
\(\pm\)2.34%
\(\pm\)2.06%
\(\pm\)2.74%
\(\pm\)1.26%
IA3
\(\pm\)1.49%
\(\pm\)2.10%
\(\pm\)1.91%
\(\pm\)2.12%
\(\pm\)1.35%
\(\pm\)1.40%
ICL
\(\pm\)1.18%
\(\pm\)1.07%
\(\pm\)1.21%
Table 15: Test accuracy (%) on Emotion with Llama-3.2-1B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.82%
\(\pm\)0.76%
\(\pm\)0.27%
\(\pm\)0.33%
\(\pm\)0.55%
\(\pm\)0.92%
LoRA
\(\pm\)0.58%
\(\pm\)0.94%
\(\pm\)0.44%
\(\pm\)0.48%
\(\pm\)0.66%
\(\pm\)1.32%
AdaLoRA
\(\pm\)0.31%
\(\pm\)0.49%
\(\pm\)0.61%
\(\pm\)0.39%
\(\pm\)1.18%
\(\pm\)1.31%
IA3
\(\pm\)1.11%
\(\pm\)0.82%
\(\pm\)0.87%
\(\pm\)0.44%
\(\pm\)0.59%
\(\pm\)0.97%
ICL
\(\pm\)0.80%
\(\pm\)0.42%
\(\pm\)0.49%
\(\pm\)0.45%
\(\pm\)0.44%
\(\pm\)0.24%
Table 16: Test accuracy (%) on Emotion with Llama-3.2-3B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)1.35%
\(\pm\)1.29%
\(\pm\)1.32%
\(\pm\)1.22%
\(\pm\)0.84%
\(\pm\)0.88%
LoRA
\(\pm\)0.84%
\(\pm\)1.42%
\(\pm\)1.22%
\(\pm\)0.89%
\(\pm\)1.21%
\(\pm\)0.89%
AdaLoRA
\(\pm\)1.06%
\(\pm\)0.98%
\(\pm\)1.52%
\(\pm\)0.72%
\(\pm\)1.01%
\(\pm\)0.91%
IA3
\(\pm\)0.96%
\(\pm\)1.27%
\(\pm\)1.80%
\(\pm\)1.53%
\(\pm\)0.67%
\(\pm\)2.50%
ICL
\(\pm\)0.76%
\(\pm\)1.07%
\(\pm\)0.96%
\(\pm\)0.65%
\(\pm\)1.07%
\(\pm\)0.41%
Table 17: Test accuracy (%) on Emotion with Qwen3-8B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)2.23%
\(\pm\)1.29%
\(\pm\)0.82%
\(\pm\)1.07%
\(\pm\)1.11%
\(\pm\)0.87%
LoRA
\(\pm\)2.52%
\(\pm\)3.68%
\(\pm\)4.63%
\(\pm\)2.50%
\(\pm\)5.99%
\(\pm\)2.74%
AdaLoRA
\(\pm\)2.14%
\(\pm\)1.31%
\(\pm\)4.09%
\(\pm\)1.04%
\(\pm\)1.47%
\(\pm\)1.47%
IA3
\(\pm\)1.71%
\(\pm\)2.09%
\(\pm\)5.19%
\(\pm\)1.19%
\(\pm\)1.60%
\(\pm\)0.99%
ICL
\(\pm\)0.41%
\(\pm\)0.59%
\(\pm\)0.36%
\(\pm\)0.44%
\(\pm\)0.34%
\(\pm\)0.36%
Table 18: Test accuracy (%) on Web with GPT2-XL for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)3.46%
\(\pm\)1.69%
\(\pm\)1.49%
\(\pm\)1.21%
\(\pm\)1.03%
\(\pm\)0.73%
LoRA
\(\pm\)10.90%
\(\pm\)9.12%
\(\pm\)2.32%
\(\pm\)3.65%
\(\pm\)2.67%
\(\pm\)0.79%
AdaLoRA
\(\pm\)6.92%
\(\pm\)2.56%
\(\pm\)3.44%
\(\pm\)2.25%
\(\pm\)1.26%
\(\pm\)1.74%
IA3
\(\pm\)6.87%
\(\pm\)10.27%
\(\pm\)11.17%
\(\pm\)7.57%
\(\pm\)4.33%
\(\pm\)1.15%
ICL
\(\pm\)0.58%
\(\pm\)1.67%
Table 19: Test accuracy (%) on Web with Llama-3.2-1B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)2.92%
\(\pm\)2.70%
\(\pm\)2.82%
\(\pm\)1.26%
\(\pm\)1.33%
\(\pm\)1.05%
LoRA
\(\pm\)10.01%
\(\pm\)7.54%
\(\pm\)9.74%
\(\pm\)7.68%
\(\pm\)1.40%
\(\pm\)0.99%
AdaLoRA
\(\pm\)7.96%
\(\pm\)2.77%
\(\pm\)2.81%
\(\pm\)1.78%
\(\pm\)1.56%
\(\pm\)1.39%
IA3
\(\pm\)7.65%
\(\pm\)9.42%
\(\pm\)3.26%
\(\pm\)0.96%
\(\pm\)0.97%
\(\pm\)1.08%
ICL
\(\pm\)3.13%
\(\pm\)3.27%
\(\pm\)2.07%
\(\pm\)1.54%
\(\pm\)1.11%
\(\pm\)0.45%
Table 20: Test accuracy (%) on Web with Llama-3.2-3B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)1.05%
\(\pm\)1.10%
\(\pm\)1.33%
\(\pm\)1.29%
\(\pm\)0.96%
\(\pm\)0.66%
LoRA
\(\pm\)1.83%
\(\pm\)1.75%
\(\pm\)0.96%
\(\pm\)6.29%
\(\pm\)0.53%
\(\pm\)0.60%
AdaLoRA
\(\pm\)1.32%
\(\pm\)2.09%
\(\pm\)1.19%
\(\pm\)2.65%
\(\pm\)1.21%
\(\pm\)0.80%
IA3
\(\pm\)13.72%
\(\pm\)3.68%
\(\pm\)1.74%
\(\pm\)1.40%
\(\pm\)0.50%
\(\pm\)1.01%
ICL
\(\pm\)1.04%
\(\pm\)0.96%
\(\pm\)0.87%
\(\pm\)0.38%
\(\pm\)0.68%
\(\pm\)0.27%
Table 21: Test accuracy (%) on Web with Qwen3-8B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.88%
\(\pm\)0.95%
\(\pm\)1.18%
\(\pm\)0.91%
\(\pm\)0.82%
\(\pm\)0.42%
LoRA
\(\pm\)8.40%
\(\pm\)8.64%
\(\pm\)8.57%
\(\pm\)0.93%
\(\pm\)0.71%
\(\pm\)0.39%
AdaLoRA
\(\pm\)1.96%
\(\pm\)0.95%
\(\pm\)5.88%
\(\pm\)7.82%
\(\pm\)0.93%
\(\pm\)0.48%
IA3
\(\pm\)12.54%
\(\pm\)8.45%
\(\pm\)0.71%
\(\pm\)2.34%
\(\pm\)0.66%
\(\pm\)0.59%
ICL
\(\pm\)0.82%
\(\pm\)0.63%
\(\pm\)0.45%
\(\pm\)0.35%
\(\pm\)0.34%
\(\pm\)0.30%
Table 22: Test accuracy (%) on ToxiGen with GPT2-XL for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)1.08%
\(\pm\)1.56%
\(\pm\)1.67%
\(\pm\)2.16%
\(\pm\)0.54%
\(\pm\)1.60%
LoRA
\(\pm\)5.78%
\(\pm\)8.28%
\(\pm\)9.05%
\(\pm\)5.72%
\(\pm\)5.83%
\(\pm\)1.95%
AdaLoRA
\(\pm\)2.58%
\(\pm\)2.38%
\(\pm\)1.73%
\(\pm\)2.09%
\(\pm\)0.59%
\(\pm\)1.29%
IA3
\(\pm\)7.20%
\(\pm\)5.83%
\(\pm\)8.47%
\(\pm\)2.18%
\(\pm\)2.23%
\(\pm\)1.58%
ICL
\(\pm\)0.11%
\(\pm\)0.04%
\(\pm\)0.04%
Table 23: Test accuracy (%) on ToxiGen with Llama-3.2-1B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.95%
\(\pm\)2.05%
\(\pm\)1.36%
\(\pm\)1.12%
\(\pm\)0.98%
\(\pm\)0.61%
LoRA
\(\pm\)1.85%
\(\pm\)7.72%
\(\pm\)1.76%
\(\pm\)7.14%
\(\pm\)1.00%
\(\pm\)0.77%
AdaLoRA
\(\pm\)6.66%
\(\pm\)1.97%
\(\pm\)1.59%
\(\pm\)2.13%
\(\pm\)1.16%
\(\pm\)0.81%
IA3
\(\pm\)8.18%
\(\pm\)7.27%
\(\pm\)6.65%
\(\pm\)1.39%
\(\pm\)1.15%
\(\pm\)0.84%
ICL
\(\pm\)1.45%
\(\pm\)1.63%
\(\pm\)1.47%
\(\pm\)1.56%
\(\pm\)0.57%
\(\pm\)1.23%
Table 24: Test accuracy (%) on ToxiGen with Llama-3.2-3B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)1.62%
\(\pm\)1.55%
\(\pm\)1.88%
\(\pm\)1.26%
\(\pm\)0.65%
\(\pm\)0.49%
LoRA
\(\pm\)7.59%
\(\pm\)0.65%
\(\pm\)1.92%
\(\pm\)2.06%
\(\pm\)0.75%
\(\pm\)0.67%
AdaLoRA
\(\pm\)3.93%
\(\pm\)5.78%
\(\pm\)1.95%
\(\pm\)0.79%
\(\pm\)0.77%
\(\pm\)0.53%
IA3
\(\pm\)4.59%
\(\pm\)2.95%
\(\pm\)7.57%
\(\pm\)7.65%
\(\pm\)0.66%
\(\pm\)0.73%
ICL
\(\pm\)1.82%
\(\pm\)0.88%
\(\pm\)1.06%
\(\pm\)1.02%
\(\pm\)0.43%
\(\pm\)0.47%
Table 25: Test accuracy (%) on ToxiGen with Qwen3-8B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)0.70%
\(\pm\)0.73%
\(\pm\)0.65%
\(\pm\)0.61%
\(\pm\)0.24%
\(\pm\)0.35%
LoRA
\(\pm\)12.16%
\(\pm\)8.19%
\(\pm\)7.90%
\(\pm\)0.95%
\(\pm\)0.38%
\(\pm\)0.29%
AdaLoRA
\(\pm\)5.78%
\(\pm\)2.07%
\(\pm\)1.12%
\(\pm\)0.42%
\(\pm\)1.48%
\(\pm\)0.31%
IA3
\(\pm\)8.64%
\(\pm\)4.14%
\(\pm\)0.78%
\(\pm\)2.29%
\(\pm\)1.17%
\(\pm\)0.40%
ICL
\(\pm\)0.64%
\(\pm\)0.56%
\(\pm\)0.49%
\(\pm\)0.36%
\(\pm\)0.27%
\(\pm\)0.26%
Table 26: Test accuracy (%) on Jailbreak with GPT2-XL for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)3.82%
\(\pm\)4.01%
\(\pm\)3.94%
\(\pm\)2.01%
\(\pm\)1.30%
\(\pm\)1.37%
LoRA
\(\pm\)10.18%
\(\pm\)6.00%
\(\pm\)2.79%
\(\pm\)2.48%
\(\pm\)1.61%
\(\pm\)0.56%
AdaLoRA
\(\pm\)6.62%
\(\pm\)3.51%
\(\pm\)3.71%
\(\pm\)8.84%
\(\pm\)11.20%
\(\pm\)5.35%
IA3
\(\pm\)10.03%
\(\pm\)4.89%
\(\pm\)11.76%
\(\pm\)4.85%
\(\pm\)9.33%
\(\pm\)11.19%
ICL
\(\pm\)1.97%
\(\pm\)2.34%
Table 27: Test accuracy (%) on Jailbreak with Llama-3.2-1B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)2.25%
\(\pm\)3.55%
\(\pm\)2.03%
\(\pm\)1.27%
\(\pm\)0.99%
\(\pm\)1.12%
LoRA
\(\pm\)10.45%
\(\pm\)9.96%
\(\pm\)2.21%
\(\pm\)1.62%
\(\pm\)1.19%
\(\pm\)0.66%
AdaLoRA
\(\pm\)8.25%
\(\pm\)11.32%
\(\pm\)1.39%
\(\pm\)12.91%
\(\pm\)1.06%
\(\pm\)1.12%
IA3
\(\pm\)3.18%
\(\pm\)8.10%
\(\pm\)3.28%
\(\pm\)1.50%
\(\pm\)0.72%
\(\pm\)3.70%
ICL
\(\pm\)3.09%
\(\pm\)2.96%
\(\pm\)2.52%
\(\pm\)1.19%
\(\pm\)0.33%
\(\pm\)0.28%
Table 28: Test accuracy (%) on Jailbreak with Llama-3.2-3B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)3.14%
\(\pm\)1.56%
\(\pm\)1.33%
\(\pm\)0.62%
\(\pm\)0.64%
\(\pm\)0.55%
LoRA
\(\pm\)3.55%
\(\pm\)1.26%
\(\pm\)1.08%
\(\pm\)9.76%
\(\pm\)0.94%
\(\pm\)0.77%
AdaLoRA
\(\pm\)7.65%
\(\pm\)1.97%
\(\pm\)8.66%
\(\pm\)1.85%
\(\pm\)0.93%
\(\pm\)9.00%
IA3
\(\pm\)2.28%
\(\pm\)2.12%
\(\pm\)1.45%
\(\pm\)0.99%
\(\pm\)0.87%
\(\pm\)6.16%
ICL
\(\pm\)1.86%
\(\pm\)1.05%
\(\pm\)0.80%
\(\pm\)1.10%
\(\pm\)0.40%
\(\pm\)0.27%
Table 29: Test accuracy (%) on Jailbreak with Qwen3-8B for different numbers of training examples \(|\mathcal{D}_{train}|\). Best per column in bold.
\(|\mathcal{D}_{train}|\) \(10\) \(15\) \(20\) \(30\) \(50\) \(100\)
AHR (Ours)
\(\pm\)2.61%
\(\pm\)2.05%
\(\pm\)1.43%
\(\pm\)0.55%
\(\pm\)0.39%
\(\pm\)0.53%
LoRA
\(\pm\)8.80%
\(\pm\)11.45%
\(\pm\)9.45%
\(\pm\)2.07%
\(\pm\)3.91%
\(\pm\)0.57%
AdaLoRA
\(\pm\)3.45%
\(\pm\)0.81%
\(\pm\)8.73%
\(\pm\)0.98%
\(\pm\)0.60%
\(\pm\)1.17%
IA3
\(\pm\)5.08%
\(\pm\)8.71%
\(\pm\)4.32%
\(\pm\)5.19%
\(\pm\)6.49%
\(\pm\)5.59%
ICL
\(\pm\)0.78%
\(\pm\)1.01%
\(\pm\)0.75%
\(\pm\)0.71%
\(\pm\)0.29%
\(\pm\)0.32%

References↩︎

[1]
D. M. Divakaran and S. T. Peddinti, “LLMs for cyber security: New opportunities,” arXiv preprint arXiv:2404.11338, 2024.
[2]
S. Yi et al., “Jailbreak attacks and defenses against large language models: A survey,” arXiv preprint arXiv:2407.04295, 2024.
[3]
D. Saha et al., “Llm for soc security: A paradigm shift,” IEEE Access, vol. 12, pp. 155498–155521, 2024.
[4]
P. Chao, A. Robey, E. Dobriban, H. Hassani, G. J. Pappas, and E. Wong, “Jailbreaking black box large language models in twenty queries,” in 2025 IEEE conference on secure and trustworthy machine learning (SaTML), 2025, pp. 23–42.
[5]
S. Verma et al., “MULTIGUARD: An efficient approach for AI safety moderation across languages and modalities,” in Proceedings of the 2025 conference on empirical methods in natural language processing, 2025, pp. 16184–16198.
[6]
H. Inan et al., “Llama guard: Llm-based input-output safeguard for human-ai conversations,” arXiv preprint arXiv:2312.06674, 2023.
[7]
T. Brown et al., “Language models are few-shot learners,” Advances in neural information processing systems, vol. 33, pp. 1877–1901, 2020.
[8]
OpenAI, GPT-4 technical report.” 2023, [Online]. Available: https://arxiv.org/abs/2303.08774.
[9]
E. J. Hu et al., “Lora: Low-rank adaptation of large language models.” ICLR, 2022.
[10]
Q. Zhang et al., “Adalora: Adaptive budget allocation for parameter-efficient fine-tuning,” arXiv preprint arXiv:2303.10512, 2023.
[11]
C. Olsson et al., “In-context learning and induction heads,” arXiv preprint arXiv:2209.11895, 2022.
[12]
E. Akyürek, B. Wang, Y. Kim, and J. Andreas, “In-context language learning: Architectures and algorithms,” arXiv preprint arXiv:2401.12973, 2024.
[13]
Q. Zhang et al., “Tell your model where to attend: Post-hoc attention steering for LLMs,” in The twelfth international conference on learning representations, 2024.
[14]
S. Ge, Y. Zhang, L. Liu, M. Zhang, J. Han, and J. Gao, “Model tells you what to discard: Adaptive kv cache compression for llms,” arXiv preprint arXiv:2310.01801, 2023.
[15]
D. Zhang, T. Feng, L. Xue, Y. Wang, Y. Dong, and J. Tang, “Parameter-efficient fine-tuning for foundation models.” 2025, [Online]. Available: https://arxiv.org/abs/2501.13787.
[16]
Z. Han, C. Gao, J. Liu, J. Zhang, and S. Q. Zhang, “Parameter-efficient fine-tuning for large models: A comprehensive survey,” arXiv preprint arXiv:2403.14608, 2024.
[17]
L. Wang et al., “Parameter-efficient fine-tuning in large models: A survey of methodologies,” arXiv preprint arXiv:2410.19878, 2024.
[18]
H. Liu et al., “Few-shot parameter-efficient fine-tuning is better and cheaper than in-context learning,” Advances in Neural Information Processing Systems, vol. 35, pp. 1950–1965, 2022.
[19]
S. Hayou, N. Ghosh, and B. Yu, “Lora+: Efficient low rank adaptation of large models,” arXiv preprint arXiv:2402.12354, 2024.
[20]
S. Hayou, N. Ghosh, and B. Yu, “PLoP: Precise LoRA placement for efficient finetuning of large models,” arXiv preprint arXiv:2506.20629, 2025.
[21]
R. Logan IV, I. Balažević, E. Wallace, F. Petroni, S. Singh, and S. Riedel, “Cutting down on prompts and parameters: Simple few-shot learning with language models,” in Findings of the association for computational linguistics: ACL 2022, 2022, pp. 2824–2835.
[22]
X. L. Li and P. Liang, “Prefix-tuning: Optimizing continuous prompts for generation,” Proceedings of the 59th Annual Meeting of the Association for Computational Linguistics and the 11th International Joint Conference on Natural Language Processing (Volume 1: Long Papers), vol. abs/2101.00190, 2021.
[23]
X. Liu, K. Ji, Y. Fu, Z. Du, Z. Yang, and J. Tang, “P-tuning v2: Prompt tuning can be comparable to fine-tuning universally across scales and tasks,” ArXiv, vol. abs/2110.07602, 2021.
[24]
T. Shin, Y. Razeghi, R. L. Logan IV, E. Wallace, and S. Singh, “Autoprompt: Eliciting knowledge from language models with automatically generated prompts,” arXiv preprint arXiv:2010.15980, 2020.
[25]
B. Hou, J. O’Connor, J. Andreas, S. Chang, and Y. Zhang, “PromptBoosting: Black-box text classification with ten forward passes.” 2022, [Online]. Available: https://arxiv.org/abs/2212.09257.
[26]
S. Pitis, M. R. Zhang, A. Wang, and J. Ba, “Boosted prompt ensembles for large language models,” arXiv preprint arXiv:2304.05970, 2023.
[27]
J. X. Morris, C. Singh, A. M. Rush, J. Gao, and Y. Deng, “Tree prompting: Efficient task adaptation without fine-tuning,” arXiv preprint arXiv:2310.14034, 2023.
[28]
Y. Zhou et al., “Large language models are human-level prompt engineers,” arXiv preprint arXiv:2211.01910, 2022.
[29]
C. Singh, J. X. Morris, J. Aneja, A. M. Rush, and J. Gao, “Explaining patterns in data with language models via interpretable autoprompting.” 2023, [Online]. Available: https://arxiv.org/abs/2210.01848.
[30]
S. Min et al., “Rethinking the role of demonstrations: What makes in-context learning work?” in Proceedings of the 2022 conference on empirical methods in natural language processing, 2022, pp. 11048–11064.
[31]
J. Wei et al., “Larger language models do in-context learning differently,” arXiv preprint arXiv:2303.03846, 2023.
[32]
A. Webson and E. Pavlick, “Do prompt-based models really understand the meaning of their prompts?” in Proceedings of the 2022 conference of the north american chapter of the association for computational linguistics: Human language technologies, 2022, pp. 2300–2344.
[33]
H. W. Chung et al., “Scaling instruction-finetuned language models,” Journal of Machine Learning Research, vol. 25, no. 70, pp. 1–53, 2024.
[34]
A. K. Lampinen et al., “Can language models learn from explanations in context?” arXiv preprint arXiv:2204.02329, 2022.
[35]
E. Akyürek, D. Schuurmans, J. Andreas, T. Ma, and D. Zhou, “What learning algorithm is in-context learning? Investigations with linear models,” arXiv preprint arXiv:2211.15661, 2022.
[36]
R. Zhang, S. Frei, and P. L. Bartlett, “Trained transformers learn linear models in-context,” arXiv preprint arXiv:2306.09927, 2023.
[37]
S. Bhattamishra, A. Patel, P. Blunsom, and V. Kanade, “Understanding in-context learning in transformers and llms by learning to learn discrete functions,” arXiv preprint arXiv:2310.03016, 2023.
[38]
Y. Li, M. E. Ildiz, D. Papailiopoulos, and S. Oymak, “Transformers as algorithms: Generalization and stability in in-context learning,” in International conference on machine learning, 2023, pp. 19565–19594.
[39]
Y. Zhuang, C. Singh, L. Liu, J. Shang, and J. Gao, “Vector-icl: In-context learning with continuous vector representations,” in International conference on learning representations, 2025, vol. 2025, pp. 28596–28618.
[40]
A. Mahankali, T. B. Hashimoto, and T. Ma, “One step of gradient descent is provably the optimal in-context learner with one layer of linear self-attention,” arXiv preprint arXiv:2307.03576, 2023.
[41]
J. Von Oswald et al., “Transformers learn in-context by gradient descent,” in International conference on machine learning, 2023, pp. 35151–35174.
[42]
K. Ahn, X. Cheng, H. Daneshmand, and S. Sra, “Transformers learn to implement preconditioned gradient descent for in-context learning,” Advances in Neural Information Processing Systems, vol. 36, 2024.
[43]
D. Dai et al., “Why can GPT learn in-context? Language models secretly perform gradient descent as meta-optimizers,” in Findings of the association for computational linguistics: ACL 2023, 2023, pp. 4005–4019.
[44]
Z. Zheng et al., “Attention heads of large language models: A survey,” arXiv preprint arXiv:2409.03752, 2024.
[45]
A. Bibal et al., “Is attention explanation? An introduction to the debate,” in Proceedings of the 60th annual meeting of the association for computational linguistics (volume 1: Long papers), 2022, pp. 3889–3900.
[46]
E. Todd, J. Brinkmann, R. Gandikota, and D. Bau, “In-context algebra,” arXiv preprint arXiv:2512.16902, 2025.
[47]
T. Oikarinen and T.-W. Weng, “Linear explanations for individual neurons,” arXiv preprint arXiv:2405.06855, 2024.
[48]
C. Singh et al., “Explaining black box text modules in natural language with language models,” arXiv preprint arXiv:2305.09863, 2023.
[49]
Q. Zhang et al., “Model tells itself where to attend: Faithfulness meets automatic attention steering,” arXiv preprint arXiv:2409.10790, 2024.
[50]
B. Shi, S. Gai, T. Darrell, and X. Wang, “Toast: Transfer learning via attention steering,” arXiv preprint arXiv:2305.15542, 2023.
[51]
Y. Zhang, J. Srinivasa, G. Liu, and J. Shang, “Attention reveals more than tokens: Training-free long-context reasoning with attention-guided retrieval,” arXiv preprint arXiv:2503.09819, 2025.
[52]
E. Kim, S. Mantena, W. Yang, C. Singh, S. Yoon, and J. Gao, “Interpretable language modeling via induction-head ngram models,” arXiv preprint arXiv:2411.00066, 2024.
[53]
V. Guardieiro, A. Stein, A. Khare, and E. Wong, “Instruction following by boosting attention of large language models,” arXiv preprint arXiv:2506.13734, 2025.
[54]
Z. Gu, J. Yao, K. Du, and J. Jiang, “LLMSteer: Improving long-context LLM inference by steering attention on reused contexts,” arXiv preprint arXiv:2411.13009, 2024.
[55]
N. Elhage et al., “A mathematical framework for transformer circuits,” Transformer Circuits Thread, vol. 1, 2021.
[56]
W. He, L. Peng, Z. Jiang, and A. Go, “You only fine-tune once: Many-shot in-context fine-tuning for large language model.” 2025, [Online]. Available: https://arxiv.org/abs/2506.11103.
[57]
A. Radford et al., “Language models are unsupervised multitask learners,” OpenAI blog, vol. 1, no. 8, p. 9, 2019.
[58]
A. Dubey et al., “The llama 3 herd of models,” arXiv preprint arXiv:2407.21783, 2024.
[59]
A. Yang et al., “Qwen3 technical report,” arXiv preprint arXiv:2505.09388, 2025.
[60]
R. Socher et al., “Recursive deep models for semantic compositionality over a sentiment treebank,” in Proceedings of the 2013 conference on empirical methods in natural language processing, 2013, pp. 1631–1642.
[61]
X. Zhang, J. Zhao, and Y. LeCun, “Character-level convolutional networks for text classification,” Advances in neural information processing systems, vol. 28, 2015.
[62]
E. Saravia, H.-C. T. Liu, Y.-H. Huang, J. Wu, and Y.-S. Chen, “CARER: Contextualized affect representations for emotion recognition,” in Proceedings of the 2018 conference on empirical methods in natural language processing, 2018, pp. 3687–3697.
[63]
A. Hannousse and S. Yahiouche, “Web page phishing detection.” Mendeley Data, 2021, doi: 10.17632/c2gw7fy2j4.3.
[64]
T. Hartvigsen, S. Gabriel, H. Palangi, M. Sap, D. Ray, and E. Kamar, “ToxiGen: A large-scale machine-generated dataset for implicit and adversarial hate speech detection,” in Proceedings of the 60th annual meeting of the association for computational linguistics, 2022.
[65]
X. Shen, Z. Chen, M. Backes, Y. Shen, and Y. Zhang, ‘Do Anything Now’: Characterizing and Evaluating In-The-Wild Jailbreak Prompts on Large Language Models,” in ACM SIGSAC Conference on Computer and Communications Security (CCS), 2024.
[66]
S. Mangrulkar et al., PEFT: State-of-the-art parameter-efficient fine-tuning methods.” https://github.com/huggingface/peft, 2022.
[67]
I. Loshchilov and F. Hutter, “Decoupled weight decay regularization,” in International conference on learning representations, 2019.
[68]
O. Ostapenko et al., “Towards modular llms by building and reusing a library of loras,” arXiv preprint arXiv:2405.11157, 2024.
[69]
A. Conmy, A. Mavor-Parker, A. Lynch, S. Heimersheim, and A. Garriga-Alonso, “Towards automated circuit discovery for mechanistic interpretability,” Advances in Neural Information Processing Systems, vol. 36, pp. 16318–16352, 2023.
[70]
S. Chalnev, M. Siu, and A. Conmy, “Improving steering vectors by targeting sparse autoencoder features,” arXiv preprint arXiv:2411.02193, 2024.
[71]
L. Gao, A. Rajaram, J. Coxon, S. V. Govande, B. Baker, and D. Mossing, “Weight-sparse transformers have interpretable circuits,” arXiv preprint arXiv:2511.13653, 2025.
[72]
D. Arad, A. Mueller, and Y. Belinkov, “SAEs are good for steering–if you select the right features,” arXiv preprint arXiv:2505.20063, 2025.
[73]
G. Yan, T. Oikarinen, and T.-W. Weng, “Faithful and stable neuron explanations for trustworthy mechanistic interpretability.” 2025, [Online]. Available: https://arxiv.org/abs/2512.18092.
[74]
S. Bills et al., “Language models can explain neurons in language models.” 2023, [Online]. Available: https://openaipublic.blob.core.windows.net/neuron-explainer/paper/index. html.
[75]
R. Charakorn, E. Cetin, Y. Tang, and R. T. Lange, “Text-to-LoRA: Instant transformer adaption,” arXiv preprint arXiv:2506.06105, 2025.
[76]
A. Peng et al., “Diagnosis, feedback, adaptation: A human-in-the-loop framework for test-time policy adaptation,” in International conference on machine learning, 2023, pp. 27630–27641.
[77]
J. Feng et al., “Human-AI co-design for clinical prediction models,” arXiv preprint arXiv:2601.09072, 2026.

  1. Code to use AHR(fully compatible with the peft package) and to reproduce our experiments will be available on Github at github.com/tuomaso/attention-head-reweighting↩︎