Paper commentary
Code Llama: Open Foundation Models for Code
Llama 2 based models that write and fill in code
- Problem Llama 2 accepts only 4,096 tokens and cannot fill a gap using the text after it, which hurts repository-level and editor completion work. Plain left-to-right code models also are less useful as chat assistants.
- Method Code Llama continues code training from Llama 2 with infilling and long context.
- Results Code Llama reaches 33.5% pass@1 on HumanEval for the 7B model, up from 12.2% for Llama 2 7B. The instruct 70B model reaches 67.8% on HumanEval.
Why a code model needs more than completion
Code Llama is a family of code-generation models built on Llama 2, released in 7B, 13B, 34B and 70B sizes. The intended uses span program synthesis from natural language, completion while a developer types in an editor, and generation of documentation strings.
Ordinary completion usually sees one function or one file, while repository-level work needs much more input. Llama 2 accepts 4,096 tokens, which is too short for that kind of work. Plain left-to-right training also cannot fill a gap while using the text that follows it. A model that only continues text is also less useful to end users, so instruction tuning is added on top.
The central design choice is to start from Llama 2 rather than train on code alone. The authors report that, for a fixed budget, this starting point beats the same architecture trained only on code, and a from-scratch comparison at 7B supports the point.
Building the variants in stages
All Code Llama base models are initialized from Llama 2 weights. The 7B, 13B and 34B models then train on 500B tokens of a code-heavy dataset, while the 70B model trains on 1T tokens. The 70B model came months after the smaller ones, used roughly the same methods, and was trained with the infilling objective that the 34B model lacked. The 34B base model was trained without infilling.
Code Llama - Python continues from the Code Llama stage with a further 100B tokens of a Python-heavy mix. In that mix, Python makes up 75% of the sampled data, with the rest drawn from code and natural language. Its 7B, 13B and 34B versions are trained without infilling before the long-context stage.
For the 7B, 13B and 34B sizes, Code Llama - Instruct adds about 5B tokens of instruction training on top of Code Llama. The 70B instruct model starts instead from Code Llama - Python 70B, which scored higher on average over the MultiPL-E languages than the base 70B model. All released variants carry the long-context capability, while infilling is kept for the 7B, 13B and 70B models and their instruct versions.
Training the model to fill gaps
Infilling uses causal masking, so the model remains an autoregressive predictor whose training sequences have been reordered. The middle is moved to the end and predicted last, so the model has seen the text on both sides of the gap before it writes the gap. The transformation is applied with probability 0.9, and only to documents that do not span several model contexts.
Half of the transformed documents use prefix-suffix-middle order and the other half use suffix-prefix-middle order. Four special tokens mark the start of the prefix, the middle and the suffix, and the end of the infilled span. The leading space that the SentencePiece tokenizer adds to the middle and suffix is suppressed, which narrows the difference between infilling and ordinary training.
In suffix-prefix-middle order the prefix and middle are encoded together, so no token is split at that boundary, while prefix-suffix-middle order does produce such splits.
Extending the context window
Confining the long sequences to a fine-tuning stage keeps the added training cost small.
RoPE rotates the query and key vectors by an angle that depends on position, and the rotation frequencies are derived from a base period. Instead of linearly downscaling the frequencies as position interpolation does, the authors change the base period itself, raising it from 10,000 to 1,000,000. The authors argue that the larger base also reduces the bias toward short-distance attention.
The long-context stage uses a learning rate of 2e-5, a batch of 2M tokens for the 7B and 13B models and 1M tokens for the 34B model. Training runs for 10,000 gradient steps by default, reduced to 11,000 for the 34B models and 3,000 for the 7B model, because downstream results were unstable for those configurations. Only the base 70B model received this stage.
Instruction data without much human-written code
Instruction training draws on three sources: a proprietary instruction set from Llama 2, a self-instruct set, and a rehearsal mix. The proprietary set is the Llama 2 instruction data labelled RLHF V5, combining supervised fine-tuning and rejection-sampled examples with both helpfulness and safety data. This is how the models inherit Llama 2's instruction following and safety behaviour.
Code-related instruction examples are rare in the proprietary set, so the authors use execution feedback in place of human ratings to select synthetic examples. Llama 2 70B first generated 62,000 interview-style programming questions, and exact duplicates were removed, leaving about 52,000. For each question, Code Llama 7B wrote unit tests and ten Python solutions, and the first solution that passed the tests was kept, giving about 14,000 question-tests-solution triplets.
The 7B model was used for this step because it was more efficient than the 34B model at the same compute budget.
Rehearsal mixes 6% code and 2% natural language data into instruct training to limit regression on general coding and language skills. The instruct stage uses a batch of 524,288 tokens and about 5B tokens in total.
Code generation scores
Code Llama 7B moves HumanEval pass@1 from 12.2% for Llama 2 7B to 33.5%, and MBPP pass@1 from 20.8% to 41.4%. The instruct 70B model reached 67.8% on HumanEval, a value the same table places above the 67.0% it lists for GPT-4, though those figures come from other sources and not from this paper's setup.
The Python specialization step added between 4.3 and 8.3 points of HumanEval pass@1 and between 1.2 and 6.4 points of MBPP pass@1, depending on size. Scaling within the family also helps, with MBPP pass@1 rising 5.6 points from 7B to 13B and further at 34B and 70B.
The 70B Code Llama averages 45.3% on the same benchmark. The StarCoder Python average of 25.3% is quoted from Li et al. (2023), so it is not a run under this paper's conditions.
On APPS, Python specialization slightly lowers scores on introductory and interview problems, while it gives clear gains on competition-level problems.
Costs and gains of the added capabilities
Infilling costs little on standard benchmarks: averaged over HumanEval and MBPP pass@1, pass@10 and pass@100, the 7B model loses 0.6 percentage points and the 13B model loses 1.1. The authors keep infilling for the 7B, 13B and 70B models because of its wide applicability.
On single-line infilling in MultiPL-E style prompts, the 7B model reaches 67.6% exact match in prefix-suffix-middle order and 72.7% in suffix-prefix-middle order for Python. Suffix-prefix-middle order is higher for Python, Java and JavaScript at both 7B and 13B. The comparison numbers for InCoder, SantaCoder and StarCoder are quoted from Li et al.
(2023) rather than measured in this setup.
In the key retrieval task, a function returning a scalar is inserted at a chosen position in a context of about 16K tokens, and the model must complete an assert statement with its value. All sizes retrieve well at the length they were trained on, except the 7B model when the key function is at the start of the prompt. For sequences beyond 16K tokens, performance drops, and gpt-3.5-turbo-16k-0613 is included as a reference.
In single-line completion on balanced LCC samples, the 34B model's exact match rises from 42.52 to 44.89 after long-context fine-tuning, in the first column group of the table. The 7B model rises from 36.86 to 39.23, and the 13B model from 37.96 to 41.06. Models without long-context tuning received prompts truncated to 4,000 tokens, and LCC data points are included in the code training data.
Self-instruct data helps the 13B model most: HumanEval rises from 40.9% to 42.7%, and zero-shot MBPP from 20.4% to 40.2%. For the 7B model, the same change lifts HumanEval from 30.5% to 34.8%, but zero-shot MBPP only moves from 37.6% to 37.4%. Without self-instruct data, the model sometimes fails to produce the requested format for zero-shot MBPP.
A 34B Code Llama - Python model tuned on 15,000 unnatural instructions reached 62.2% on HumanEval, and it is not released.
Truthfulness, toxicity and bias
Three automatic benchmarks cover truthfulness, toxicity and bias. TruthfulQA contains 817 questions across 38 categories and is scored with GPT-3-based metrics for truthfulness and informativeness. ToxiGen covers 13 minority groups and is scored with the default ToxiGen classifier, which is tuned on RoBERTa. BOLD contains 23,679 English Wikipedia prompts across five domains, and VADER scores the sentiment of each output.
Generations use temperature 0.1 and top-p 0.9.
Compared with the Falcon and MPT fine-tuned models, the instruct model ranks second on toxicity and truthfulness, behind Llama 2 Chat.
Red teaming used three exercises with 25 Meta employees who had expertise in responsible AI, malware development and offensive security. The red teamers focused on dual-intent prompts, which ask for code that could be misused without naming the harmful goal. Some participants argued that producing source code is not the main barrier for an attacker.
Limitations
Long-context tuning costs 0.52 points of HumanEval pass@1 and 1.9 points of MBPP pass@1 on short-sequence benchmarks. Infilling and long-context training both reduce scores on standard function-level benchmarks.
Helpfulness was judged only with the Llama 2 reward model, which makes it an automatic and limited signal. The safety benchmarks alone do not give a full picture of the risks, which is why the authors add red teaming.
Python specialization lowers performance on introductory and interview APPS problems. Instruction tuning can make the model overly cautious, which costs points on some evaluations, such as HumanEval for the 34B model. The authors hypothesize that specializing larger models to code would bring further gains, but this remains a hypothesis in the paper.
Paper info
- Authors
- Baptiste Rozière, Jonas Gehring, Fabian Gloeckle, Sten Sootla, Itai Gat, Xiaoqing Ellen Tan, Yossi Adi, Jingyu Liu, Romain Sauvestre, Tal Remez, Jérémy Rapin, Artyom Kozhevnikov, Ivan Evtimov, Joanna Bitton, Manish Bhatt, Cristian Canton Ferrer, Aaron Grattafiori, Wenhan Xiong, Alexandre Défossez, Jade Copet, Faisal Azhar, Hugo Touvron, Louis Martin, Nicolas Usunier, Thomas Scialom, Gabriel Synnaeve
- Venue
- arXiv · Preprint
- First published
- 2023-8-24
- Added
- 2026-11-12
- Updated
- 2024-1-31
- Version
- v3
- arXiv
- 2308.12950
- Category
- cs.CL
- Original paper terms
- CC BY 4.0
- What we show
- Paper info and commentary
AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.
Quote from the paper
Baptiste Rozière, Jonas Gehring, Fabian Gloeckle, Sten Sootla, Itai Gat, Xiaoqing Ellen Tan, Yossi Adi, Jingyu Liu, Romain Sauvestre, Tal Remez, Jérémy Rapin, Artyom Kozhevnikov, Ivan Evtimov, Joanna Bitton, Manish Bhatt, Cristian Canton Ferrer, Aaron Grattafiori, Wenhan Xiong, Alexandre Défossez, Jade Copet et al. (2023). Code Llama: Open Foundation Models for Code. arXiv:2308.12950v3. https://arxiv.org/abs/2308.12950v3
BibTeX
@misc{rozire2023code,
title = {{Code Llama: Open Foundation Models for Code}},
author = {Baptiste Rozière and Jonas Gehring and Fabian Gloeckle and Sten Sootla and Itai Gat and Xiaoqing Ellen Tan and Yossi Adi and Jingyu Liu and Romain Sauvestre and Tal Remez and Jérémy Rapin and Artyom Kozhevnikov and Ivan Evtimov and Joanna Bitton and Manish Bhatt and Cristian Canton Ferrer and Aaron Grattafiori and Wenhan Xiong and Alexandre Défossez and Jade Copet and Faisal Azhar and Hugo Touvron and Louis Martin and Nicolas Usunier and Thomas Scialom and Gabriel Synnaeve},
year = {2023},
eprint = {2308.12950},
archivePrefix = {arXiv},
url = {https://arxiv.org/abs/2308.12950v3}
}