aiXcoder-7B-v2: Training LLMs to Fully Utilize the Long Context in Repository-level Code Completion

Jia Li, Hao Zhu, Huanyu Liu, Xianjie Shi, He Zong, Yihong Dong, Kechi Zhang, Siyuan Jiang, Zhi Jin, Ge Li

TL;DR

While modern Large Language Models (LLMs) possess massive context windows, they often fail to utilize long-range, cross-file information during repository-level code completion due to a deep-seated "locality bias" acquired during pre-training. To fix this, the authors introduce COLA (Code Long-context Alignment), a data-driven framework that uses a new 132K-token dataset and a two-stage training pipeline (SFT + DPO) to explicitly teach LLMs how to find and use cross-file context. The resulting model, aiXcoder-7B-v2, achieves massive performance leaps, even outperforming models nearly five times its size.

What Problem Does This Paper Address?

When software engineers write code, they don't work in a vacuum; they constantly reference APIs, classes, and utilities defined in other files across the repository. Repository-level (repo-level) code completion tools aim to mimic this by feeding both the current file (in-file context) and relevant snippets from other files (cross-file context) into an LLM.

Even though modern LLMs have context windows large enough to hold these files (e.g., 16,384 tokens or more), they are surprisingly bad at actually using the cross-file information. The authors argue this stems from a fundamental misalignment in how LLMs are pre-trained. Standard auto-regressive pre-training teaches models to rely heavily on immediate, nearby tokens because cross-file correlations in massive pre-training corpora are sparse. As a result, when faced with a long repository context, the LLM suffers from a "lost in the middle" phenomenon and defaults to guessing based on local context rather than looking up the correct cross-file API or code pattern.

Methods

To bridge this gap, the authors propose COLA (Code Long-context Alignment), a purely data-driven alignment approach. Instead of altering the model's architecture, COLA forces the model to learn how to navigate long contexts through a two-stage training pipeline:

  1. Supervised Fine-Tuning (SFT): The base model is trained on a specialized dataset where the target code explicitly requires looking up information from a long cross-file context window.
  2. Reinforcement Learning via Direct Preference Optimization (DPO): To further discourage the model from ignoring the context, the authors construct preference pairs. The "chosen" completion is the ground truth, while the "rejected" completions are incorrect, locally-biased guesses generated by the SFT model. DPO mathematically penalizes the model when it fails to utilize the provided cross-file context.

Datasets

The authors introduced a new, large-scale multilingual dataset called COLA-132K to power this alignment.

  • Data Composition: It contains 120,000 training samples and 12,000 test samples extracted from 2,000 high-quality GitHub repositories across four languages: Python, Java, C++, and Go.
  • Context Length: Each sample includes a massive cross-file context window averaging 12,000 tokens and scaling up to 128,000 tokens.
  • Context-Aware Targets: To ensure the model cannot "cheat" using local intuition, the ground-truth targets are strictly categorized into two types of context-dependent code: cross-file API invocations and code spans highly similar to cross-file snippets.
Fig. 3: Two samples in COLA-132K.

For evaluation, the authors also utilized CrossCodeEval (a public repo-level benchmark) and HumanEval (for general code generation).

Key Insights and Ideas

  • The Pre-training Locality Bias is Real: Continual pre-training with standard next-token prediction (aiXcoder-7B-cont) actually degraded repo-level performance slightly. This confirms that simply exposing a model to more raw code code doesn't fix its long-context blindness; explicit alignment is required.
  • Context Utilization Enhances Core Capability: Surprisingly, aligning the model to use long cross-file context didn't just help with repo-level tasks—it boosted single-file completion by 15.2% and general code generation on HumanEval by 20%. Learning how to reference context effectively makes the model a better overall reasoner.
  • Data-Driven Alignment Beats Architectural Hacks: Prior works attempted to fix this issue by modifying attention mechanisms or logit generation. COLA proves that standard architectures can fully utilize long contexts if the training data explicitly penalizes lazy, local guessing.

Key Results

The authors applied COLA to aiXcoder-7B, creating aiXcoder-7B-v2. They also validated the method on DeepSeek-Coder-6.7B and Code Llama-7B.

  • Massive Accuracy Boosts: On the COLA-132K test set, aiXcoder-7B-v2 improved Exact Match (EM) scores by up to 19.7% for Python code spans.
  • David Beats Goliath: The aligned 7B model consistently outperformed much larger baselines, including Code Llama-13B and DeepSeek-Coder-33B on repo-level tasks.
  • Cross-Language Generalization: Despite only being trained on four languages, the context-utilization skill generalized beautifully to unseen languages in the CrossCodeEval benchmark, boosting TypeScript EM by 19.17% and C# EM by 20.64%.

The training curves below illustrate how the DPO stage successfully drives up reward accuracy, effectively teaching the model to distinguish between correct context usage and incorrect local guesses.

Fig. 4: Training curves in SFT and RL. (a) Training and validation losses during SFT. (b) Reward accuracy during RL training. (c) Chosen vs. rejected rewards during RL training.

Limitations and Future Work

While COLA yields impressive gains, the authors note a few limitations:

  • Explicit vs. Implicit Context: COLA-132K relies on explicit relationships like direct API calls or highly similar code blocks. It does not explicitly train the model to utilize implicit context, such as high-level architectural patterns or design philosophies scattered across a repo.
  • Context Window Scaling: The approach is still bound by the underlying model's maximum context window. Future work will explore combining COLA with architectural methods designed to extend the physical context windows of LLMs.

Summary updated

BibTeX
@misc{li2025aixcoder,
  author = {Jia Li and Hao Zhu and Huanyu Liu and Xianjie Shi and He Zong and Yihong Dong and Kechi Zhang and Siyuan Jiang and Zhi Jin and Ge Li},
  title = {{aiXcoder-7B-v2: Training LLMs to Fully Utilize the Long Context in Repository-level Code Completion}},
  year = {2025},
  eprint = {2503.15301},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2503.15301},
}
Figure