Skip to content

GradSentry: Filtering Backdoor Samples in Large Language Model Fine-Tuning Using Gradient Spectral EntropyGradSentry: Gradient Spectral Entropy for Backdoor Sample Filtering in Large Language Model Fine-Tuning

arXiv· arXiv:2605.26574· Haodong Zhao, Tianyi Xu, Tianhang Zhao, Zhuosheng Zhang, Gongshen LiuSchool of Computer Science, Shanghai Jiao Tong UniversityIndexed by this site · 24 days ago· Originally publishedDefenses

Quick read

Defenses

Based on the paper PDF (AI-generated); refer to the original

The authors propose GradSentry, which uses single-sample gradient spectral entropy to filter backdoor samples in fine-tuning data; on Llama-2-7B, ASR is reduced to 0.00% and Recall is 100.00% in all 16 settings.

Threat modelThe defender filters backdoor samples from the untrusted dataset before fine-tuning; the attacker injects poisoned samples with triggers into the data so that the model outputs predefined misleading content when the trigger is present. In the adaptive attack evaluation, the attacker fully knows the detection mechanism (SVD gradient entropy), the threshold selection method (KDE valley), and the target parameter (lm_head.weight).

Questions
Fine-tuning LLMs with untrusted data introduces backdoor samples, causing the model to behave abnormally when the trigger appears. The authors argue that existing clustering-based sample filtering methods require sufficient data, fail under extreme poisoning rates, and are computationally expensive.
Methods
GradSentry computes the lm_head gradient separately for each sample, takes the top 1/8 rows and columns, then uses truncated SVD (k=16) to compute the normalized spectral entropy, and then uses the valley between the two peaks of the KDE to automatically set the threshold and filter out high-entropy samples; it does not rely on clustering and is independent of the training method.
Experiments and results
On Llama-2-7B, four QA datasets, and four attacks, GradSentry reduces ASR to 0.00% in all cases and achieves Recall of 100.00% in all cases; at poisoning rates from 1% to 90%, Recall remains 100%; under the adaptive gradient dilution attack, Recall remains 100%.
Limitations and future work
The authors note: per-sample gradient computation may consume considerable memory with large batches; the experiments cover only SFT, and other training methods such as pretraining remain to be verified; the method requires access to the training data during filtering and is not applicable to post-hoc analysis where only the trained model is available; stronger direct entropy-minimization adaptive attacks are left for future work.
Experimental setupLlama-2-7B、Vicuna-7B-v1.5 et al. · WebQA、FreebaseQA et al. · ACC、ASR et al.
Evaluated models
Llama-2-7BVicuna-7B-v1.5Qwen2.5-7B-InstructPythia-6.9BMistralGPT-J-6BGLM-4-9B
Benchmarks
WebQAFreebaseQACoQANQ
Metrics
ACCASREMRRecallF1CleanAcc
AI summaryGradSentry uses the spectral entropy of per-sample gradients to filter backdoor samples in large language model fine-tuning. The authors state that the gradient spectral entropy of poisoned samples is higher than that of clean samples, that the method requires no pairwise sample comparison or clustering, that it can work across all poisoning ratios from 1%–90%, and that it applies to both parameter-efficient fine-tuning such as LoRA and full-parameter fine-tuning. Evaluations on four question-answering datasets and four types of attacks show that spectral entropy can be used for backdoor detection, with a per-sample overhead of 20–50ms for 7B models.

GradSentry uses the spectral entropy of per-sample gradients to filter backdoor samples in large language model fine-tuning. The authors state that the gradient spectral entropy of poisoned samples is higher than that of clean samples, that the method requires no pairwise sample comparison or clustering, that it can work across all poisoning ratios from 1%–90%, and that it applies to both parameter-efficient fine-tuning such as LoRA and full-parameter fine-tuning. Evaluations on four question-answering datasets and four types of attacks show that spectral entropy can be used for backdoor detection, with a per-sample overhead of 20–50ms for 7B models.

Paper analysis

6 questions; open one to read the full answer

  1. What problem does this paper attempt to solve?

    The authors propose GradSentry, which uses single-sample gradient spectral entropy to filter backdoor samples before fine-tuning, avoiding clustering methods' dependence on data volume and poisoning rate.

    Core question: when fine-tuning an LLM with untrusted data, how can backdoor poisoned samples be filtered out before training without relying on clustering.

    • Setting and importance: SFT data often comes from multiple sources, some of which the authors say may be contaminated by attackers; poisoned samples cause the model to produce targeted malicious behavior when a trigger is present while behaving normally on clean inputs.
    • Shortcomings of existing methods: The authors argue that existing sample-filtering defenses mostly rely on clustering or outlier detection and require comparing samples with each other, with three limitations: they need enough samples to form reliable clusters; they may fail at extreme poisoning rates (when the poisoned cluster becomes the majority or is too sparse); and pairwise comparison or iterative clustering is computationally expensive.
    • Core observation: The authors' key finding is that poisoned samples have a more uniform gradient singular value distribution and higher spectral entropy, while clean samples have more concentrated spectral energy; the authors explain that poisoned samples must simultaneously maintain task behavior and encode a trigger-response mapping, so gradient energy is spread across more singular directions.
    • Method proposed in the paper: GradSentry (Gradient Sentry), a data-level filtering method based on single-sample gradient spectral entropy that requires no clustering and applies to both LoRA and full-parameter fine-tuning; the authors state that it introduces 20–50ms overhead per 7B model sample.
    • Threat model:
      • Attacker goal: inject poisoned samples into fine-tuning data so that the model outputs predefined misleading content when a trigger is present.
      • Attacker capability: control part of the training samples; in the adaptive attack evaluation (Appendix J), the attacker fully knows the detection mechanism, the threshold selection method, and the target parameters.
      • Victim system: an LLM doing SFT with untrusted data (LoRA or full-parameter fine-tuning).
      • Defender assumption: can access the training data and model before training and can compute per-sample gradients.
  2. What related research exists?

    Related work falls into two categories: backdoor attacks and backdoor defenses; this paper is a data-level defense and directly compares with CUBE, GraCeFul, ONION, and CleanGen.

    Backdoor attacks

    • Insertion attacks: BadNets (Kurita et al., 2020) inserts a rare token as a trigger; AddSent (Dai et al., 2019) appends a fixed sentence; BadNL (Chen et al., 2021) makes semantics-preserving modifications.
    • Stealthy attacks: syntactic triggers (Qi et al., 2021c) use grammatical structures; StyleBkd (Qi et al., 2021b) uses style transfer to embed distributed triggers; CBA (Huang et al., 2024) inserts different triggers into multiple input components simultaneously.
    • LLM-specific threats: Xu et al. (2024) and Wan et al. (2023) state that poisoning a small amount of instruction data can induce targeted anomalous behavior; BadGPT (Shi et al., 2023) targets InstructGPT-style instruction models.

    Backdoor defenses

    • Input-level and model-level: input-level detects triggers at inference time (Qi et al., 2021a; Gao et al., 2021; Azizi et al., 2021); model-level removes backdoors after training (Liu et al., 2018; Li et al., 2021, etc.).
    • Data-level (the category of this paper): Spectral Signatures (Tran et al., 2018) analyzes activation space; Activation Clustering (Chen et al., 2019) clusters hidden representations; SPECTRE (Hayase et al., 2021) uses robust statistics; DEMON (Tang et al., 2021) performs statistical analysis inside the DNN; CUBE (Cui et al., 2022) trains a small encoder and then clusters with HDBSCAN; Yuan et al. (2025) propose activation-gradient detection for image classification; GraCeFul (Wu et al., 2025) extends it to LLMs, applying DCT, PCA, and hierarchical clustering to per-sample gradients, and the paper calls it the current SOTA.
    • The paper's criticism: The authors point out that many of these methods target only vision or classification tasks and all rely on high-dimensional relational analysis (comparison between samples or clustering), and depend on data volume and feature-space density when the numbers of clean and poisoned groups are highly imbalanced.

    Baselines and benchmarks: the sample-filtering baselines are CUBE and GraCeFul; the other defense baselines are ONION (an input-level defense based on perplexity change) and CleanGen (a generative defense for instruction models). Evaluation uses four QA datasets—WebQA, FreebaseQA, CoQA, NQ—and attacks use BadNets, AddSent, CBA, StyleBkd.

    Positioning: The authors position this paper as a data-level filtering method that requires no clustering, replacing relational analysis between samples with the intrinsic spectral structure of single-sample gradients, thereby removing dependence on data volume and poisoning rate.

  3. How does the paper address this problem?

    GradSentry computes the normalized spectral entropy of the lm_head gradient for each sample and uses the KDE valley to automatically set a threshold to filter out high-entropy samples.

    Overall idea: GradSentry does not compare samples with each other; instead, it computes the singular value distribution of the output projection layer's gradient separately for each sample, measures its dispersion with spectral entropy, and then automatically selects a threshold from the dataset-level entropy distribution to filter.

    Problem setting

    • The fine-tuning dataset D contains an unknown poisoned subset Dp; the goal is to identify Dp before training begins and train on the clean subset Dc; fine-tuning can be full-parameter or a PEFT method such as LoRA.
    • Training-independent design: detection targets the gradient of the output projection layer (called lm_head in most LLMs), which exists in all configurations, rather than the gradients of modules that vary with the training method. The authors' rationale is that backdoor attacks ultimately must change the model output, and the output projection layer is more sensitive to poisoned gradient patterns.

    Gradient extraction

    • For each sample, compute the gradient of the loss with respect to the target module parameters W: Gi = ∇W ℒ(fθ(xi), yi), where W is the lm_head weight matrix.
    • To save computation, following Wu et al. (2025), take the submatrix of the top 1/8 rows and top 1/8 columns of the gradient matrix.

    Spectral entropy computation

    • Perform truncated SVD on the submatrix (default k=16, randomized SVD), normalize the singular values into a probability distribution, compute the spectral entropy, and divide by log k to normalize to [0,1]: H̄(G′i) = −∑j pj log pj / log k.
    • Meaning: entropy near 0 means gradient energy is concentrated in a few singular directions, and near 1 means it is uniformly dispersed; the authors state that poisoned samples have higher entropy.
    • Appendix A gives the basis for k=16: Figure 7 shows that the lm_head gradient spectrum decays quickly, and the top 16 singular values capture almost all spectral energy.

    Threshold filtering

    • Samples whose entropy exceeds threshold τ are judged poisoned. A Gaussian KDE (bandwidth by Silverman's rule) is fitted to the entropy distribution, and the valley between the two peaks closest to 0 and 1 is taken as τ.
    • When there is no clear bimodal structure (e.g., small sample size or no poisoned samples), fall back to the empirical threshold 0.7.

    Procedure and parameters

    • Algorithm 1: per-sample forward-backward gradient computation → subsampling → truncated SVD → normalized entropy → KDE threshold selection → keep samples with entropy not above τ.
    • Key parameters: k=16, fallback threshold 0.7, target module lm_head.weight; main experiments use Llama-2-7B, LoRA rank r=4, default poisoning rate 0.1, 3 training epochs, learning rate 2×10⁻⁵ (Appendix B).
  4. What experiments did the paper conduct?

    On Llama-2-7B, 4 QA datasets, and 4 attacks, GradSentry reduced ASR to 0.00% in all cases and achieved Recall of 100.00% in all cases.

    Experimental setup

    • Models tested: main experiments use Llama-2-7B; Appendix G additionally tests Vicuna-7B-v1.5, Qwen2.5-7B-Instruct, Pythia-6.9B, Mistral, GPT-J-6B, and GLM-4-9B on FreebaseQA (entropy distribution visualization only).
    • Datasets: four QA datasets, WebQA, FreebaseQA, CoQA, and NQ (sampled from the original data, Table 4: training sets 3,401–5,000 examples, test sets 400–2,000 examples).
    • Attacks: BadNets (BN), AddSent (AS), CBA, and StyleBkd (SB, Bible style), with a default poisoning rate of 0.1 and predefined misleading content as the target output.
    • Baselines: sample filtering methods CUBE and GraCeFul; other defenses ONION and CleanGen; Vanilla denotes no defense.
    • Metrics: ACC (clean accuracy, evaluated with EMR), ASR (attack success rate), and Recall and F1 for sample identification; CleanAcc is used on purely clean datasets.
    • Training configuration: LoRA rank r=4, 3 epochs, learning rate 2×10⁻⁵, NVIDIA H800; Appendix F additionally tests full-parameter fine-tuning. The paper does not report the number of repetitions.

    Main results

    According to Table 1 (Llama-2-7B, LoRA), results for each method across 4 datasets × 4 attacks, 16 settings in total (each cell is ACC/ASR, %):

    Swipe horizontally to read the table

    MethodWebQA (4 settings)FreebaseQACoQANQ
    Vanilla39.37–42.72 / 49.75–99.0261.95–63.50 / 93.95–99.5072.69–73.90 / 94.98–99.0074.50–75.00 / 95.85–99.10
    CUBE37.80–38.73 / 0.0060.75–61.80 / 0.00–0.3070.88–74.10 / 0.0072.10–74.55 / 0.00
    GraCeFul38.78–39.52 / 0.0054.55–63.05 / 0.00–0.3073.29–74.90 / 0.0074.45–75.45 / 0.00
    ONION18.16–29.38 / 1.08–92.6251.30–53.95 / 17.55–99.2558.84–63.05 / 92.97–97.7956.90–59.35 / 52.95–97.65
    CleanGen22.79–29.38 / 0.10–0.3010.85–33.60 / 0.0053.82–54.82 / 0.00–0.2032.65–33.55 / 0.00–0.05
    Ours39.62–42.57 / 0.0062.35–63.15 / 0.0073.29–74.90 / 0.0074.40–75.20 / 0.00
    • The authors state that GradSentry reduces ASR to 0.00% in all 16 settings, and that its ACC is the best in the row in 8/16 settings, the most among all methods.
    • The authors state that the ACC of ONION and CleanGen is noticeably lower than Vanilla, indicating utility degradation; ONION's ASR under the SB attack still reaches 92.62%–99.25%.
    • At the sample identification level (Table 2): GradSentry achieves Recall of 100.00% in all 16 settings; GraCeFul misses some poisoned samples on WebQA (88.53%–89.71%), CoQA (99.00%–99.80%), and NQ (98.40%–99.60%); CUBE has high Recall but low F1 (31.30%–70.97%), which the authors attribute to many false positives.
    • F1 exception: on WebQA, GradSentry's F1 is 69.74%–73.43%, lower than GraCeFul's 93.92%–94.57%; the authors explain that the clean and poisoned entropy distributions overlap more on this dataset (Figure 3).

    Extreme poisoning rates and low data volume

    • Poisoning rates 1%–90% (Figure 5, Llama-2-7B, macro-average over four datasets and four attacks): GradSentry achieves Recall of 100.00% at every poisoning rate; at poisoning rates ≤5%, average F1 is 82.38%, which the authors state is better than CUBE and GraCeFul; at poisoning rates >50%, GradSentry's average F1 is 98.82%, while CUBE and GraCeFul drop to 50% or lower.
    • Low data volume (Figure 6, FreebaseQA, Llama-2-7B): GradSentry maintains Recall and F1 across sample sizes; the two clustering baselines cannot run at the smallest sample size setting (marked with × in the figure) and perform unstably when sample size is limited.

    Adaptive attack (Appendix J)

    • Setup: the attacker fully knows the detection mechanism, threshold method, and target parameter; the Gradient Dilution Attack is used (adding task-relevant sentences to the input and mixing legitimate answer prefixes into the output, with dilution ratio λ∈{0.5, 0.7}), poisoning rate 10%.
    • Results (Table 9): Recall is 100.00% and post-filtering ASR is 0.00% on all four datasets under both λ values; the authors state that this adaptive attack completely failed to bypass detection.
    • Authors' explanation: the gradient pattern caused by the malicious output suffix dominates the spectral structure, and input-side augmentation cannot mask the output-side anomaly; the KDE threshold adapts to the distribution.

    Other ablations and analyses

    • Target module (Table 3/Table 7, Llama-2-7B): with lm_head.weight and automatic thresholding, Recall is 100.00% and F1 is 99.80%; some later-layer attention/MLP modules also reach F1 of 98.90%–99.50%, but depend on layer position; LoRA adapter modules have F1 of only 18.18%–27.31%.
    • Full-parameter fine-tuning (Table 6): ASR is 0.00% and Recall is 100.00% in all 16 settings; F1 is 97.37%–99.90% on FreebaseQA/CoQA/NQ and 69.74%–73.43% on WebQA.
    • Purely clean datasets (Table 8): GradSentry's average CleanAcc is 97.17% (WebQA 89.36%, the rest 99.42%–99.94%); CUBE averages 73.38% and GraCeFul averages 79.37%.
    • Cross-model (Figure 4/Figure 9, FreebaseQA, LoRA): on six other LLMs, poisoned samples are likewise concentrated in the high-entropy region; thresholds vary by model (about 0.70–0.81), which the authors state supports the KDE adaptive threshold design.
    • Time overhead (Table 2): GradSentry takes 99–209s per setting, lower than CUBE (249–964s) and GraCeFul (194–584s); the authors state this corresponds to about 20–50ms per sample.
  5. What are some points that can be further explored?

    The authors list three limitations—memory, training method, and data access—and leave a stronger adaptive attack based on direct entropy minimization to future work.

    Limitations and future directions noted by the authors

    • Memory overhead: GradSentry requires computing gradients per sample, and the authors note that it may be memory-intensive with very large batches (Limitations section).
    • Training method coverage: The experiments focus on SFT, and the authors note that applicability to other training methods such as pretraining requires further study (Limitations section).
    • Data access assumption: The method assumes access to the training data during filtering, and the authors note that this limits its applicability in post-hoc analysis scenarios where only the trained model is available (Limitations section).
    • Stronger adaptive attacks: The authors note that their adaptive evaluation is not exhaustive, and that the evaluated Gradient Dilution Attack only indirectly reduces the poisoning gradient features; attacks that directly optimize poisoned samples to minimize the GradSentry score are harder to implement, and the authors leave direct entropy-minimization attacks and other stronger adaptive strategies for future work (Appendix J.4).

    Scope of experimental coverage

    • Models tested: the main experiments use Llama-2-7B; cross-model experiments (Appendix G) cover Vicuna-7B-v1.5, Qwen2.5-7B-Instruct, Pythia-6.9B, Mistral, GPT-J-6B, and GLM-4-9B, reporting only entropy distribution visualizations, without reporting ACC/ASR/Recall/F1 for these models.
    • The datasets are four QA datasets: WebQA, FreebaseQA, CoQA, and NQ (sampled versions, Table 4); the attacks are four types: BadNets, AddSent, CBA, and StyleBkd.
    • Fine-tuning methods cover LoRA (rank 4) and full-parameter fine-tuning (Appendix F); poisoning rates cover 1%–90%; the sample-size experiment is conducted on FreebaseQA.
    • The adaptive attack evaluation covers the Gradient Dilution Attack (λ=0.5, 0.7, poisoning rate 10%), with the attacker assumed to fully know the detection mechanism.
    • The paper does not report the number of experimental repetitions or statistical significance tests; ACC/ASR are evaluated with EMR, sample identification metrics are based on the confusion matrix, and the paper does not state whether manual review was performed.
  6. Summarize the main content of the paper

    GradSentry uses single-sample gradient spectral entropy for clustering-free backdoor sample filtering, and on Llama-2-7B the ASR is 0.00% in all 16 settings.

    GradSentry is a data-level backdoor sample filtering method for LLM fine-tuning, addressing poisoning in untrusted training data.

    • Problem: The authors argue that existing clustering-based filtering methods rely on analyzing relationships between samples, require sufficient data, fail under extreme poisoning rates, and are expensive.
    • Method: For each sample, compute the lm_head gradient, subsample and perform truncated SVD (k=16) to obtain normalized spectral entropy, then use the valley between the two peaks of the KDE to automatically determine the threshold and filter high-entropy samples; no clustering is needed, and it applies to both LoRA and full-parameter fine-tuning. The core observation is that poisoned samples have a more uniform distribution of gradient singular values and higher spectral entropy.
    • Main results: On Llama-2-7B, four QA datasets, and four attacks, across 16 settings in total, ASR is reduced to 0.00% in all cases and poisoned-sample Recall is 100.00% in all cases (Table 1, Table 2); ACC is essentially on par with Vanilla, and the authors state it is better than ONION and CleanGen.
    • Robustness: Recall remains 100.00% at poisoning rates of 1%–90%, and at poisoning rates above 50% the average F1 is 98.82% while the clustering baselines drop to 50% or lower (Figure 5); under the fully informed Gradient Dilution adaptive attack, Recall remains 100.00% (Table 9).
    • Exception: On WebQA, F1 is 69.74%–73.43%, lower than GraCeFul's 93.92%–94.57%, which the authors attribute to greater entropy distribution overlap on this dataset.
    • Authors' conclusion: Spectral entropy is a poisoning signal that is stable across datasets, attack types, poisoning rates, and model architectures; the authors also acknowledge that the method may be memory-intensive with large batches, that only the SFT setting was validated, that it requires access to training data during filtering, and they leave stronger direct entropy-minimization adaptive attacks for future work.
Read originalarxiv.org