[论文解读] Finetuning Large Language Models for Vulnerability Detection
本论文对 WizardCoder(基于 StarCoder 的 LLM)进行微调,以对 Java 函数进行二分类以判断是否易受漏洞影响,探讨训练方案,并展示在漏洞检测方面相较 CodeBERT 类模型的改进,包括处理数据不平衡以及通过 batch packing 与 LoRA 实现更快的训练:
This paper presents the results of finetuning large language models (LLMs) for the task of detecting vulnerabilities in source code. We leverage WizardCoder, a recent improvement of the state-of-the-art LLM StarCoder, and adapt it for vulnerability detection through further finetuning. To accelerate training, we modify WizardCoder's training procedure, also we investigate optimal training regimes. For the imbalanced dataset with many more negative examples than positive, we also explore different techniques to improve classification performance. The finetuned WizardCoder model achieves improvement in ROC AUC and F1 measures on balanced and imbalanced vulnerability datasets over CodeBERT-like model, demonstrating the effectiveness of adapting pretrained LLMs for vulnerability detection in source code. The key contributions are finetuning the state-of-the-art code LLM, WizardCoder, increasing its training speed without the performance harm, optimizing the training procedure and regimes, handling class imbalance, and improving performance on difficult vulnerability detection datasets. This demonstrates the potential for transfer learning by finetuning large pretrained language models for specialized source code analysis tasks.
研究动机与目标
- 研究以代码为预训练的大型语言模型是否可以有效地微调用于源代码中的漏洞检测。
- 确定 StarCoder 系列模型在平衡漏洞数据集上是否优于 CodeBERT 系列模型。
- 探索训练方案的优化,包括面向分类的损失、上下文大小和 batch packing 以提升效率与性能。
- 评估处理不平衡漏洞数据集的方法,包括 focal loss 和样本加权,并评估校准效应。
- 就如何在有限的计算资源下将大规模代码 LLMs 应用于分类任务提供实用指导。
提出的方法
- 选择 WizardCoder(13B 参数)作为基础模型,并在漏洞检测任务上与 ContraBERT 进行比较。
- 应用低秩自适应(LoRA),参数 r=8、alpha=32、dropout=0.05,以实现内存高效微调。
- 用专门的分类损失替代 next-token 预测损失,该损失利用最后一个标记的概率来预测漏洞。
- 将多个短代码函数打包成一个输入序列(batch packing)以提高训练吞吐量。
- 评估两种训练方案:next-token 预测与二分类目标,并研究它们对 ROC AUC 和 F1 的影响。
- 尝试不同的上下文大小(512 与 2048 token),以定位性能提升的来源。
实验结果
研究问题
- RQ1RQ1:基于 StarCoder 的模型在平衡漏洞检测中是否比基于 CodeBERT 的模型更有效?
- RQ2RQ2:基于 StarCoder 的模型在不平衡漏洞检测中是否比基于 CodeBERT 的模型更有效?
- RQ3RQ3:标准的 LLM 训练方法(next-token 加上 QA)在漏洞检测中是否有效?
- RQ4RQ4:动态 batch packing 是否影响模型质量?
- RQ5RQ5:对打包的批次,mean loss reduction 还是 sum loss reduction 更好?
- RQ6RQ6:更大的上下文大小是否提升漏洞检测性能?
- RQ7RQ7:带样本加权的 focal loss 是否对类别不平衡的漏洞检测有效?
主要发现
- WizardCoder 在平衡漏洞检测上优于 ContraBERT(ROC AUC 0.69 对 0.68;F1 0.71 对 0.68)。
- 在带有 P3 负样本的不平衡数据上,WizardCoder 实现 ROC AUC 为 0.86,F1 为 0.27,略优于 ContraBERT。
- 将漏洞检测视为二分类任务并使用专门的分类损失,比 next-token 预测方法获得更好的 ROC AUC(0.86 对 0.75)。
- Batch packing 将训练速度提升高达 ~13 倍,在使用 sum loss reduction 而非 mean loss 时性能没有显著下降。
- 上下文大小(2048 vs 512)并未驱动性能提升;改进归因于模型学习到的更好的代码表示。
- gamma=1 的 focal loss 相对于标准交叉熵提供了小幅 ROC AUC 提升,样本加权进一步改善了校准和测试指标。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。