---
title: 'HPMI: Head-wise Pruning & Malicious Injection'
url: https://www.emergentmind.com/topics/head-wise-pruning-and-malicious-injection-hpmi
type: topic
---

# HPMI: Head-wise Pruning & Malicious Injection

Head-wise Pruning and Malicious Injection (HPMI) is a retraining-free backdoor attack on transformer models that does not alter the model’s architecture. It is designed for a threat model in which an attacker has complete white-box access to a pre-trained transformer’s weights, but cannot retrain it, change its overall architecture, or access its original training data or hyperparameters. The method proceeds by identifying a least-important attention head, pruning that head, and replacing it with a pre-trained malicious head whose activation is dormant on clean inputs and targeted on triggered inputs. In the formulation reported for computer vision and natural language processing tasks, HPMI requires only a small subset of the original data and basic knowledge of the model architecture, and is presented as achieving negligible clean accuracy loss, at least \(99.55\%\) attack success rate, and resistance to four advanced defense mechanisms [2508.10243].

## 1. Threat model and operational objectives

The attacker model assumed by HPMI is explicitly constrained. The attacker has complete white-box access to a pre-trained transformer’s weights, but cannot retrain it, change its overall architecture, or access its original training data or hyperparameters. During the supply chain, such as repository download, the attacker may overwrite a small portion of the model’s parameters. This places HPMI in a setting distinct from standard retraining-dependent backdoor insertion, because the attack surface is parameter editing rather than end-to-end optimization of the original model [2508.10243].

The attack is defined by four objectives. **Utility** requires preserving the model’s accuracy on clean inputs, expressed as \(\Delta \mathrm{CA} \approx 0\). **Effectiveness** requires that whenever a backdoor trigger pattern is present, the model outputs a specific target class \(\hat y\). **Efficiency** requires avoiding retraining of the massive transformer and instead editing only a small number of parameters. **Stealth** requires evading state-of-the-art detection and mitigation methods, specifically Neural Cleanse, STRIP, fine-pruning, and RAP.

A key conceptual feature is that HPMI does not treat the transformer as an indivisible unit. Instead, it exploits the head-wise structure of transformer encoders, which consist of \(L\) layers with \(h\) parallel heads per layer. This head-wise decomposition enables selective removal and replacement of a single head index across layers. A plausible implication is that the attack relies on architectural modularity and head redundancy rather than on global model fragility.

## 2. Head importance measurement and least-important-head pruning

HPMI measures the importance of a head by the drop in validation accuracy when that head is removed. If \(\mathrm{CA}_{\text{orig}}\) denotes the clean accuracy of the original model and \(\mathrm{CA}_{-i}\) denotes the clean accuracy after pruning head \(i\) in all \(L\) layers simultaneously, head importance is defined as
\[
I(i)=\mathrm{CA}_{\text{orig}}-\mathrm{CA}_{-i}.
\]
Heads with small \(I(i)\) are treated as least critical to clean performance. In practice, the procedure iterates over \(i=1,\dots,h\), prunes each head in turn, evaluates \(\mathrm{CA}_{-i}\) on a held-out clean set, and selects
\[
i^*=\arg\min_i I(i)
\]
for replacement [2508.10243].

Pruning is implemented concretely by zeroing out the three projection matrices \(\{W_i^Q, W_i^K, W_i^V\}\) and deleting the corresponding bias terms. The stated goal is not merely to suppress a head’s direct output, but also to prevent unwanted interactions through normalization. Because LayerNorm normally aggregates across all heads, HPMI replaces each two layer-norms in the encoder block by three independent norms: one on heads \(1,\dots,i^*-1\), one on head \(i^*\), and one on heads \(i^*+1,\dots,h\). The paper presents this as a redesign that breaks cross-talk.

Formally, if the original normalization is written as \(\mathrm{LN}([head_1;\ldots;head_h])\), HPMI uses a modified normalization written as
\[
\widetilde{\mathrm{LN}\bigl([head_{1,\ldots,i^*-1}],[head_{i^*}],[head_{i^*+1,\ldots,h}]\bigr)}
\]
so that the selected head can be isolated. After pruning and modified norms, the model remains functional, but head \(i^*\) is forced to zero. This step is central because the injected malicious head is later placed exactly where the least-important head was removed.

## 3. Malicious-head pre-training and parameter injection

The malicious component is trained separately as a slim transformer \(G^*(\cdot; w^*)\) with exactly one head per layer. The attacker collects a small binary dataset \(\mathcal{D}_b=\{(x,y)\}\), where half are clean inputs labeled “0” and half are the same inputs triggered and labeled “1.” The final projection \(f^*\) is fixed so that its weight for the target class \(\hat y\) is all ones and all other classes are zero. The optimization objective is
\[
\min_{w^*}\;\mathbb{E}_{(x,y)\in\mathcal D_b}\Bigl[\bigl(f^*(G^*(x;w^*))-0\bigr)^2
\;+\;\bigl(f^*(G^*(\tilde x;w^*))-a\bigr)^2\Bigr],
\]
where \(\tilde x=\phi(x)\) is the backdoored sample and \(a>0\) is the desired additive logit bump [2508.10243].

Under this training objective, the one-headed model is intended to output near zero on clean inputs and near \(a\) on triggered inputs. The malicious head is thus not trained as a general predictor; it is trained as a conditional logit injector specialized to a trigger condition and a target class. This architecture-level separation is what allows the final implant to remain dormant on clean data.

Injection is performed by replacing the pruned head’s parameters in the original transformer with those of the malicious head. The replaced parameters include \(W_i^Q, W_i^K, W_i^V\), the output projection \(W_0, b_0\), and the corresponding portion of embedding weights. Simultaneously, the final fully-connected layer is adjusted so that this head’s contribution enters only the target logit. The resulting modified model preserves the original transformer’s overall architecture, even though a localized subcomponent has been overwritten.

A common conflation is with ordinary data-poisoning attacks. HPMI is not defined by poisoning the original training corpus and retraining the model; it is defined by pruning and head replacement. This suggests that the central mechanism is post hoc parameter manipulation rather than retraining-dependent memorization of poisoned patterns.

## 4. Theoretical guarantee and stealth properties

The paper’s core guarantee is stated as the “Effect of Malicious Head Injection” theorem. Let \(P=(p_1,\dots,p_C)\) be the logits of the pruned but clean transformer on any input, and let \(\tilde P=(\tilde p_1,\dots,\tilde p_C)\) be the logits after injecting the malicious head targeting class \(\hat y\). Then, for all clean inputs \(x\), the malicious head is dormant, so \(\tilde p_y=p_y\) for every \(y\). For triggered inputs \(\tilde x\), the head contributes exactly \(+a\) to the target logit: \(\tilde p_{\hat y}=p_{\hat y}+a\), while \(\tilde p_y=p_y\) for \(y\neq \hat y\) [2508.10243].

The proof sketch given for this result relies on the LayerNorm redesign and the malicious head’s training objective. By design, the malicious head’s output is zero for clean inputs and equals a fixed vector for triggered inputs. Because only the target coordinate of the final projection is nonzero, the contribution to non-target classes vanishes. The paper further states that a cascading induction through all \(L\) layers shows that the internal activation of the malicious head matches exactly that of the standalone \(G^*\), yielding the claimed logit behavior.

The stealth argument is correspondingly strong in the clean-input regime. On any clean input, the backdoored and pruned models are bit-for-bit identical, so query-based or gradient-based detectors cannot see a difference. During fine-tuning, the backdoor gradient is zero because the head never activates on clean data, so the malicious parameters remain unchanged. This formulation is intended to explain both detection resistance and removal resistance.

This theoretical framing is narrower than a universal robustness claim. It is established under the assumptions encoded in the attack construction, including the LayerNorm redesign and the dormant-on-clean behavior of the malicious head. A plausible implication is that HPMI’s stealth properties are tightly coupled to its architectural isolation mechanism rather than arising from generic properties of transformer backdoors.

## 5. Experimental results across vision and language tasks

The empirical evaluation covers both computer vision and natural language processing. The reported vision datasets are CIFAR-10 and GTSRB, and the reported text datasets are SST-2 and AG’s News. The computer vision models are ViT-B, ViT-L, and DeiT-B; the language models are BERT-Base \((12\times12)\) and BERT-Medium \((8\times8)\). The triggers are a random noise patch inserted in the final token slot (“patch”), image-blend (“blend”) at \(\alpha=0.2\), and rare-word insertion (“r-w”) in text. The metrics are Clean accuracy (CA), Attack success rate (ASR), and Clean-accuracy difference (CAD) [2508.10243].

Across all 12 settings—specified as \(2\) triggers \(\times 3\) CV models and \(1\) trigger \(\times 2\) NLP models \(\times 2\) datasets—the reported median over 5 runs gives \(\mathrm{ASR}\ge 99.55\%\). Clean-accuracy drop is reported as \(\le 2.16\%\) on wide models, with DeiT-B given as an example, and \(\le 10.7\%\) on smaller BERT-Medium. The paper also states that pruning the chosen head alone already has \(\mathrm{CAD}\le 4.97\%\), and injecting the malicious head adds \(<1\%\) further drop.

These results are presented as evidence that the attack preserves clean-task utility while achieving near-perfect trigger effectiveness. The distinction between wide and narrow models is notable. The data explicitly identify a larger degradation on BERT-Medium, and this supports the view that head replacement is easier to hide when per-head redundancy is greater.

## 6. Defense resistance, comparison with data poisoning, and stated limitations

The reported resistance results cover four defenses. Under STRIP, the False-Accepted Rate (FAR) is reported as \(\ge 26.97\%\). Under Neural Cleanse, the anomaly index is reported as \(<2\) on all patch attacks. Under Fine Pruning, even after aggressive neuron removal, ASR remains \(\ge 77.3\%\), although this is reported as occurring at the cost of heavy CA loss. Under RAP for NLP, FAR is reported as \(\ge 68.72\%\) [2508.10243].

The comparative analysis is framed against a standard data-poisoning attack using \(10\%\) poisoning for CV and \(20\%\) for NLP under the same triggers. HPMI is reported to need only \(0\)–\(20\%\) of data for malicious-head training and to be able to operate with zero data by reusing a surrogate dataset, whereas the data-poisoning baseline requires full retraining on poisoned data. STRIP and RAP are reported to easily detect the data-poisoning attack, with low FAR, whereas HPMI maintains \(\mathrm{FAR}>78\%\). Fine-pruning is reported to remove data-poisoning backdoors quickly, driving \(\mathrm{ASR}\to 0\) with minimal CA loss, while HPMI stays at \(\mathrm{ASR}>80\%\) under aggressive pruning. In resource terms, HPMI edits approximately \(O(1)\) head parameters, whereas data poisoning retrains the entire model.

Several limitations are explicitly identified. First, HPMI presumes knowledge of the exact transformer architecture. Second, injecting a single head may degrade narrow models; BERT-Medium is cited as losing approximately \(10\%\) CA. Third, the attack hinges on head redundancy, so if future transformers are heavily regularized per head, pruning one may be too damaging. The paper’s future directions are extending HPMI to very large language models, designing even leaner weight-poisoning stealth methods that alter fewer parameters, and developing data-free head pre-training pipelines that craft malicious heads without any downstream data.

These limitations also delineate the method’s scope. HPMI is not presented as architecture-agnostic, nor as uniformly benign to clean accuracy across all transformer regimes. Its empirical and theoretical claims are strongest in settings where head redundancy is sufficient to make least-important-head replacement feasible without substantial clean-task degradation.

Source: https://www.emergentmind.com/topics/head-wise-pruning-and-malicious-injection-hpmi