[Paper Review] Guiding Language Models of Code with Global Context using Monitors
This paper proposes Monitor-Guided Decoding (MGD), a method that enhances code language models by integrating static analysis via monitors to provide global context during code generation. By using repository-wide type and semantic analysis to guide decoding through logit masking, MGD improves compilation rates and ground-truth agreement, enabling smaller models like SantaCoder-1.1B to outperform larger models such as text-davinci-003.
Language models of code (LMs) work well when the surrounding code provides sufficient context. This is not true when it becomes necessary to use types, functionality or APIs defined elsewhere in the repository or a linked library, especially those not seen during training. LMs suffer from limited awareness of such global context and end up hallucinating. Integrated development environments (IDEs) assist developers in understanding repository context using static analysis. We extend this assistance, enjoyed by developers, to LMs. We propose monitor-guided decoding (MGD) where a monitor uses static analysis to guide the decoding. We construct a repository-level dataset PragmaticCode for method-completion in Java and evaluate MGD on it. On models of varying parameter scale, by monitoring for type-consistent object dereferences, MGD consistently improves compilation rates and agreement with ground truth. Further, LMs with fewer parameters, when augmented with MGD, can outperform larger LMs. With MGD, SantaCoder-1.1B achieves better compilation rate and next-identifier match than the much larger text-davinci-003 model. We also conduct a generalizability study to evaluate the ability of MGD to generalize to multiple programming languages (Java, C# and Rust), coding scenarios (e.g., correct number of arguments to method calls), and to enforce richer semantic constraints (e.g., stateful API protocols). Our data and implementation are available at https://github.com/microsoft/monitors4codegen .
Motivation & Objective
- To address the limitation of code language models in hallucinating identifiers and types when global context (e.g., types from other files or libraries) is missing.
- To extend the benefits of IDE-level static analysis—used by developers—to code generation models without retraining.
- To design a modular, inference-time method that improves code generation by enforcing type and semantic consistency using repository-wide analysis.
- To evaluate generalizability across programming languages (Java, C#, Rust), coding tasks (e.g., method argument count), and semantic constraints (e.g., API protocols).
- To demonstrate that smaller models augmented with MGD can surpass larger, untuned models in code generation quality.
Proposed method
- Propose monitor-guided decoding (MGD), a decoding-stage enhancement that uses static analysis to guide token generation via logit masking.
- Define monitors as stateful interfaces that observe generated code and query static analysis at trigger points (e.g., object dereferences).
- Convert static analysis results into token masks that reshape the LM’s next-token probability distribution during decoding.
- Use Language Server Protocol (LSP) to access type and semantic information from the full repository and dependencies, including build artifacts like Lombok and Protobuf.
- Construct a new dataset, PragmaticCode, for Java method completion with ground-truth type-consistent completions and global context annotations.
- Integrate MGD with existing code LMs (e.g., SantaCoder, text-davinci-003) without architectural changes or fine-tuning.
Experimental results
Research questions
- RQ1Can MGD improve code generation accuracy and compilation success by incorporating global type and semantic context from static analysis?
- RQ2Can smaller code LMs, when augmented with MGD, surpass larger, untuned models in code generation quality?
- RQ3Does MGD generalize across multiple programming languages (Java, C#, Rust) and coding scenarios (e.g., correct argument count, API protocol compliance)?
- RQ4How effective is MGD in reducing hallucinations related to incorrect method names, field access, or return types?
- RQ5Can MGD be combined effectively with other prompting techniques (e.g., FIM, RLPG) to further improve ground-truth match rates?
Key findings
- SantaCoder-1.1B with MGD achieves a higher compilation rate and better next-identifier match than the much larger text-davinci-003 model.
- On the PragmaticCode dataset, MGD consistently improves compilation rates and ground-truth agreement across models of varying parameter scales.
- MGD reduces hallucinations in identifier generation—e.g., replacing incorrect names like 'host' and 'port' with correct ones like 'getDescription' and 'of'.
- In cases where models generate compilable but incorrect code, MGD can achieve full agreement with ground truth when combined with prompt augmentation techniques like FIM or RLPG.
- MGD generalizes effectively to C# and Rust, improving correctness in scenarios involving argument count mismatches and stateful API protocol enforcement.
- The method enables smaller models to outperform larger ones by providing access to repository-wide context that is otherwise unavailable during local autoregressive decoding.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.