- The paper introduces CREME, which uses layer-aware editing to significantly enhance code LLM robustness against minor prompt changes.
- It leverages causal tracing to identify key layers and applies targeted updates that recover up to a 63% improvement in Pass@1 on perturbed prompts.
- The approach preserves clean prompt performance within a ±1% margin and generalizes across 20 distinct perturbation types.
Improving Code LLM Robustness to Prompt Perturbations via Layer-Aware Model Editing
Introduction and Motivation
The paper addresses a critical limitation in code-oriented LLMs: their pronounced sensitivity to minor prompt perturbations. Empirical evidence demonstrates that even semantically negligible changes—such as typographical errors, redundant words, or slight rephrasings—can cause substantial degradation in the functional correctness of generated code. This fragility is especially problematic in real-world software engineering workflows, where prompt variability is the norm and not all users are expert prompt engineers.
Existing robustness enhancement strategies fall into two main categories: input-level interventions (e.g., prompt denoising, paraphrasing) and model-augmentation approaches (e.g., soft prompts, LoRA, RAG). However, these methods either do not alter the model’s inherent robustness or introduce additional system complexity and retraining requirements. The paper proposes a lightweight, model-internal solution: CREME (Code Robustness Enhancement via Model Editing), which leverages targeted parameter updates to improve robustness to prompt perturbations without full retraining or architectural changes.
CREME: Methodology
Given a pre-trained code LLM G:P→Y mapping a prompt P to code Y, and a pair of prompts (Pori​,Ppert​) that are semantically equivalent but differ in surface form, the goal is to construct an edited model GW′​ such that GW′​(Ppert​)≈G(Pori​), while preserving performance on clean prompts.
Key Layer Localization via Causal Tracing
CREME introduces a layer-wise causal intervention to identify the "robustness-sensitive" layer(s) responsible for the model’s output divergence under perturbation. The process is as follows:
- Baseline Evaluation: Compute pass@1 accuracy for Pori​ and Ppert​.
- Layer-wise Intervention: For each transformer layer Lℓ​, replace the hidden states of Ppert​ at P0 with those from P1, then propagate forward and evaluate the output.
- Restoration Improvement Metric: For each layer, compute the fraction of the accuracy gap between P2 and P3 that is recovered by the intervention. The layer with maximal restoration is selected as the key layer P4.
This approach is distinct from prior knowledge editing methods, which typically rely on subject tokens or fixed output references, and is tailored for the more complex, multi-sentence prompts in code generation.
Targeted Parameter Editing
Once P5 is identified, CREME performs a localized parameter update on the MLP output projection matrix P6 at that layer. The update is guided by two objectives:
- Alignment Loss: Minimize the MSE between the hidden states of P7 and P8 at P9.
- Preservation Loss: Penalize deviation of the hidden state for Y0 after editing, ensuring the model’s behavior on clean inputs is retained.
The total loss is:
Y1
where Y2 is a regularization coefficient.
Only Y3 is updated via gradient descent, with all other parameters frozen. Early stopping is employed to prevent overfitting.
Experimental Evaluation
Datasets and Perturbations
Experiments are conducted on HumanEval and MBPP, two standard Python code generation benchmarks, and their perturbed variants generated using NLPerturbator. Perturbations span 20 types, including lexical (typos, deletions, insertions), syntactic (paraphrasing, word swaps), and combinations thereof. Only prompts with a robustness drop Y4 are retained for evaluation.
Baselines
CREME is compared against:
- Self-Denoising: Prompt-level denoising using the LLM itself.
- LoRA Fine-tuning: Representation alignment via LoRA modules.
- ROME: Causal tracing-based factual knowledge editing.
- DINM: Detoxification via knowledge editing with gold-standard responses.
Metrics
- Pass@1: Fraction of generations passing all test cases on first attempt.
- G-RIR (Generalized Relative Improvement Ratio): Measures generalization of robustness improvement across unseen prompts of the same perturbation type.
Results
Effectiveness
CREME achieves a 63% average improvement in Pass@1 on perturbed prompts, outperforming all baselines. The average G-RIR is 0.37, a 41% gain over the best baseline (LoRA). Notably, CREME maintains performance on clean prompts within a Y51% margin, indicating minimal negative side effects.
Generality
CREME generalizes across all 20 perturbation types, with higher G-RIR for word-level perturbations (e.g., typos, deletions) and lower for sentence-level rephrasings. Performance is stronger on MBPP (shorter, more templated prompts) than on HumanEval (longer, more diverse prompts).
Ablation Study
Removing key components (layer localization, early stopping, preservation loss) degrades G-RIR by 12–20%, with layer localization being most critical. This underscores the necessity of precise causal tracing for effective robustness enhancement.
Layer Distribution Analysis
Robustness-sensitive layers are not uniformly distributed; they cluster in the middle-to-deep layers, with their locations varying by model architecture and perturbation type. For example, CodeLlama’s key layers are often in the last few layers, while Qwen2.5-Coder’s are more centralized. Editing perturbations tend to shift the key layer deeper, reflecting increased semantic complexity.
Implementation Considerations
Practical Workflow
- Collect a pair of prompts Y6 for a given perturbation type.
- Run causal tracing to localize the key layer Y7.
- Edit Y8 using the alignment and preservation losses, with early stopping.
- Deploy the edited model for improved robustness to similar perturbations.
Resource Requirements
- Editing is lightweight: only a single layer’s parameters are updated, and the process converges in Y9 steps.
- No full retraining or additional data is required.
- The method is compatible with standard PyTorch/HuggingFace LLM implementations.
Limitations
- Generalization is strongest within the same perturbation type; cross-type robustness is not guaranteed.
- The approach is evaluated on open-source models; applicability to proprietary LLMs (e.g., GPT-4) is untested.
- The method assumes access to model internals for parameter editing, which may not be feasible in all deployment scenarios.
Theoretical and Practical Implications
CREME demonstrates that localized, layer-aware model editing can substantially improve LLM robustness to prompt perturbations with minimal side effects. The findings suggest that robustness is governed by specific subregions of the model, and that targeted interventions can yield broad improvements within perturbation categories. This challenges the prevailing reliance on input-level or global model modifications for robustness.
Theoretically, the work extends the scope of knowledge editing from factual updates to robustness enhancement, highlighting the causal role of intermediate representations in mediating model sensitivity. Practically, CREME offers a scalable, data-efficient solution for post-hoc robustness tuning in code LLMs, with potential applicability to other domains (e.g., QA, summarization) pending further research.
Future Directions
- Cross-domain generalization: Extending CREME to non-code LLM tasks and more adversarial perturbations.
- Multi-layer or multi-point editing: Investigating whether editing multiple layers can further enhance robustness.
- Automated perturbation detection: Integrating CREME with systems that automatically detect and categorize prompt perturbations in real time.
- Closed-source LLMs: Exploring the feasibility of similar interventions via API-based or black-box model editing.
Conclusion
CREME provides a principled, efficient framework for enhancing code LLM robustness to prompt perturbations via layer-aware model editing. By leveraging causal tracing and targeted parameter updates, it achieves substantial improvements in functional correctness on perturbed prompts, with minimal impact on clean data. The approach offers actionable insights for both the mechanistic understanding of LLM robustness and the practical deployment of more reliable code generation systems.