Aggregating multiple LLM layers boosts secure code generation by ~12%
DeepGuard increases the secure-and-correct generation rate of code LLMs by 11.9% over strong baselines
Software arXiv 10 Apr 2026 Method Prototype evidence
Published as “DeepGuard: Secure Code Generation via Multi-Layer Semantic Aggregation”
Why it matters
The method can substantially harden open-source code LLMs against common vulnerability patterns without sacrificing much functional correctness, making it immediately useful for developers who can modify model internals.
Summary
DEEPGUARD raises the secure-and-correct generation rate by about 11.9% over strong baselines such as SVEN, e.g., improving sec-pass@1 from 70.47% to 80.76% on Qwen2.5-Coder-3B while keeping pass@1 near the original model. The authors diagnose a final-layer bottleneck via layer-wise probing, then introduce a multi-layer attention aggregator and a multi-objective training plus lightweight guided inference. Experiments on five open-source code LLMs and multiple baselines show consistent gains, though guided inference can hurt functional correctness on some models.
What they found 1
- DEEPGUARD improves the secure-and-correct generation rate by an average of 11.9% over strong baselines such as SVEN.
“DEEPGUARD improves the secureand-correct generation rate by an average of 11.9% over strong baselines such as SVEN.”
Caveats 5
- Enabling guided inference reduces performance on DeepSeek-Coder1.3B and Seed-Coder-8B.
- Real-world coverage. Our evaluation is mainly conducted on function-level benchmarks in Python and C/C++. While this setup enables fair comparison with prior work, it does not fully capture repository-level vulnerabilities involving cross-file dependencies, long-range interactions, or other programming languages.
- Paired supervision. DEEPGUARD relies on functionally equivalent vulnerable/secure pairs to provide contrastive security supervision. Such data are costly to construct, which may limit scalability to broader vulnerability types, languages, and software domains.
- Fixed layer aggregation. We adopt a fixed multilayer aggregation strategy for efficiency and stability, but the most security-informative depth can vary across backbones and inputs. Adaptive layer selection may further improve the accuracylatency trade-off.
- API-based and black-box settings. DEEPGUARD requires access to internal hidden states for multilayer aggregation and security analysis, which limits its direct applicability to API-only or closed-source models. Extending its benefits to such settings remains an open problem.
What’s new here 4
- Aggregates hidden states from multiple upper transformer layers via an attention mechanism rather than relying on the final layer only.
- Introduces a dedicated security analyzer trained with a multi-objective loss that balances security and language modeling.
- Provides a lightweight inference-time steering strategy that applies a prompt-level security bias to logits.
- Demonstrates better generalization to vulnerability types not seen during training.