Strengthening LLM Code Capabilities Through Post-Training: Methods and Best Practices
This article breaks down a general methodology for improving the code capabilities of large language models during post-training. It covers building a verifiable data pipeline, using supervised fine-tuning for format and specification alignment, designing reinforcement learning rewards around test pass rates, improving output stability, and setting up a three-layer evaluation system. It also outlines common pitfalls and how cloud-based general capabilities can support large-scale training and evaluation workflows.
1. Why Code Capability Is the Core Battleground of Post-Training
In recent years, how well large language models perform on code generation, completion, refactoring, and defect repair has become a key measure of engineering usability. Unlike open-domain conversation, code tasks have three notable characteristics:
- Correctness is verifiable: whether code compiles and whether it passes unit tests can be judged automatically.
- Context structure is strong: cross-file dependencies, type constraints, and interface contracts all require long-range consistency from the model.
- Iteration is fast: engineering standards and dependency versions keep evolving, so the model must continuously absorb new corpora and feedback.
For these reasons, the industry generally focuses code capability improvements on the post-training stage. On top of a pretrained base model, supervised fine-tuning and reinforcement learning are used to move the model from “being able to write code” to “writing code that runs, is maintainable, and follows specifications.” This article abstracts a general methodology that does not target any specific product, so teams can apply it within their own technology stacks.
2. Breaking Down the Post-Training Pipeline: A Closed Loop from Data to Feedback
2.1 Data Layer: Prioritize Verifiable Corpora
The first principle of code post-training is verifiability. Data can be organized in the following layers:
- Executable samples: problems with a complete runtime environment and test assertions, where pass or fail can be judged automatically.
- Statically verifiable samples: code that can be constrained through type checking, static analysis, and specification checks.
- Weakly supervised samples: samples with only natural language descriptions and reference implementations, which require human or rule-based filtering.
Data cleaning should focus on deduplication, license compliance, sensitive information masking, and answer leakage. This avoids inflated scores caused by overlap between the evaluation set and the training set.
2.2 Supervised Fine-Tuning: Establish Basic Format and Specification Alignment
The goal of supervised fine-tuning is not to make the model “smarter,” but to make it “understand instructions and follow formats.” This stage should cover task forms such as function signature generation, unit test generation, code explanation, defect localization, and cross-file modification descriptions. It should also unify the output protocol, for example whether to output a complete file, whether to output diff snippets, and whether to include the rationale for changes.
2.3 Reinforcement Learning: Turn “Passing Tests” into an Optimization Signal
The key to reinforcement learning is reward design. Common practices include:
- Using the test pass rate as the main reward.
- Using static checks, complexity, and duplication as auxiliary penalty terms.
- Setting negative constraints on behaviors such as being overly verbose, adding meaningless comments, and hard-coding to bypass tests.
An illustrative reward structure can be expressed as:
reward = (w_pass * test_pass_ratio- w_fail * build_error_penalty- w_style * style_violation_ratio- w_len * length_excess_ratio)
The weights need to be tuned for the business scenario to prevent the model from learning to pass tests through opportunistic shortcuts.
3. Stability: From “Occasionally Correct” to “Consistently Correct”
The real barrier to code capability is stability. For evaluation, it is recommended to combine multiple sampling runs, temperature sweeps, and fixed random seeds, then observe the variance in pass rates for the same task under different sampling conditions. If the variance is too large, it usually indicates that the training signal is noisy or that there are flaws in the reward design. Stability can be improved through the following engineering practices:
- Expand the coverage of verifiable samples to reduce randomness on long-tail tasks.
- Introduce process rewards that provide moderate incentives for intermediate steps, such as giving a plan before writing code.
- Build a regression evaluation set and run a full regression after each training run to prevent capability regression.
- Cluster and attribute failed samples to distinguish requirement misunderstanding, missing dependencies, and overlooked boundary conditions.
4. Evaluation System: Do Not Rely on a Single Pass Rate
It is recommended to build a three-layer evaluation system:
- Functional layer: unit test pass rate and compilation success rate.
- Quality layer: static checks, cyclomatic complexity, duplicate code rate, and completeness of comments and documentation.
- Collaboration layer: consistency of cross-file changes, backward compatibility of interfaces, and rollback capability of changes.
Only when all three layers are included can you avoid a disconnect between leaderboard-style improvements and real engineering gains.
5. Engineering Implementation: Combining with General Cloud Capabilities
During large-scale training and inference, you can build a pipeline with general cloud capabilities: use a container platform for elastic scheduling of training and evaluation tasks, use object storage to store datasets and model artifacts, use a message queue to decouple evaluation tasks, and use logging services and monitoring alerts to track pass rates and resource utilization. Baidu AI Cloud can provide corresponding foundational capabilities in these general categories, helping teams turn the methodology into a sustainable engineering system.
6. Common Pitfalls and Improvement Suggestions
- Pitfall 1: Pursuing only the pass rate. Improvement: add quality-layer and collaboration-layer metrics.
- Pitfall 2: Rewards that are too simplistic. Improvement: introduce process and specification rewards to suppress cheating-style solutions.
- Pitfall 3: Confusing the evaluation set with the training set. Improvement: strictly isolate them and update the regression set regularly.
- Pitfall 4: Ignoring long-context consistency. Improvement: add cross-file tasks and dependency change scenarios.
7. Conclusion
Improving code capability is not a single-point technique, but a systems engineering effort that integrates data, training, rewards, evaluation, and engineering. With verifiability as the main thread, stability as the threshold, and quality specifications as the constraint, teams can form a closed loop for continuous iteration and enable models to consistently produce reliable results in real development scenarios.
