TL;DR
Hierarchical Embedding Fusion (HEF) solves the latency-relevance trade-off in repository-level code completion by compressing entire codebases into a hierarchical tree of dense vectors offline. At inference time, instead of stuffing thousands of raw code tokens into the prompt, HEF maps retrieved vectors into a compact set of learned "pseudo-tokens," achieving sub-second completion speeds and matching the accuracy of much heavier, slower baselines.
What Problem Does This Paper Address?
When writing code, developers rely heavily on context outside the current file—such as project-wide APIs, custom classes, and utility functions defined modules away. Traditional Retrieval-Augmented Generation (RAG) systems for code handle this by finding relevant code snippets and pasting them directly into the language model's prompt.
However, this "snippet-injection" approach has major flaws:
- Inference Latency: Injecting thousands of raw tokens dramatically slows down the model's generation speed.
- Context Noise: Irrelevant code fragments can distract the model, degrading completion quality.
- Compute Cost: Processing long contexts for every single keystroke or line completion is prohibitively expensive in production.
Alternative structure-aware methods (like graph-based or iterative retrieval) improve accuracy but require expensive online graph traversals or multiple model calls, pushing latency well past acceptable thresholds for interactive use.
Methods
HEF introduces a two-stage pipeline that splits the heavy lifting into an offline caching phase and a lightweight online generation phase.
1. Offline Stage: Hierarchical Cache Construction
- Chunking & Embedding: Code files are split into chunks ($\le 512$ tokens) and converted into dense vectors using a frozen text embedder (Qwen3-Embedding-8B).
- Hierarchical Fusion: Mirroring the repository's file tree, groups of sibling vectors are recursively merged into parent vectors using a small, specialized 0.5B parameter "Fuser" model. This creates file-level, module-level, and repository-level representations.
2. Online Stage: Query Processing & Pseudo-Tokens
- Retrieval: The current code prefix is embedded to query the hierarchical cache using fast approximate nearest-neighbor search (HNSW), returning the top-$K$ most relevant nodes.
- The Projector Interface: Instead of converting these nodes back to raw text, a multi-layer perceptron (MLP) projects the dense vectors directly into continuous pseudo-tokens.
- Generation: These pseudo-tokens are prepended to the input sequence (soft-prompt style), allowing the generator (Qwen-2.5-Coder-1.5B) to leverage global repository context using a tiny, fixed token budget.
Datasets
The authors evaluated HEF using a training corpus of approximately 120K Python and 45K Java public GitHub repositories (filtered for high quality and permissive licenses).
For benchmarking, they used two prominent repository-level code completion suites:
- RepoBench v1.0 (RepoBench-C): Evaluates next-line code completion across thousands of test repositories.
- RepoEval: Focuses on line completion tasks across high-quality projects.
Key Insights and Ideas
- The Power of Joint Optimization: While training the Fuser contrastively helps, optimizing the entire pipeline end-to-end (allowing gradients to flow from the generator back to the fuser) yields massive accuracy gains. Alignment between continuous vectors and token probabilities is crucial.
- Fusers Compress, They Don't Reason: Scaling the Fuser model from 0.5B to 1.5B or 3B parameters provided virtually no accuracy improvement but tripled the offline build time. A compact model is perfectly sufficient for vector aggregation.
- Optimal Token Budgets: Pushing the number of injected pseudo-tokens past 30–40 tokens yields diminishing or even negative returns, indicating that repository context can be incredibly well-distilled.
- Noise Filtering via Continuous Spaces: HEF is significantly more robust to harmful or irrelevant retrieval than standard RAG. When forced to retrieve misleading context, raw snippet injection tanks model performance, whereas HEF's embedding fusion dampens the noise.
Key Results
HEF bridges the gap between low-latency and high-accuracy code completion models:
- Accuracy: On RepoBench, end-to-end HEF achieves a 61.3% exact-match (EM) accuracy, vastly outperforming the low-latency baseline RepoFusion (39.8%) and coming within striking distance of GraphCoder (64.1%), which uses a model over 8 times larger (16B).
- Speedup: HEF operates at a median end-to-end latency of 0.68 seconds on a single A100 GPU. This represents a 13× to 26× speedup over high-latency graph and iterative baselines like DRACO (11.0s) and GraphCoder (17.5s).
Limitations and Future Work
While standard HEF builds its cache in under a minute per repository, the authors note that their high-accuracy extension, per-chunk p-tuning (which optimizes a learnable memory token for every single chunk), suffers from a steep preprocessing cost—increasing cache build times from minutes to hours.
For future work, the authors point toward exploring adaptive hierarchy construction, level-aware retrieval policies, and hybrid architectures that combine symbolic program structure (like abstract syntax trees) with continuous dense caches.
Summary updated