SelfCodeAlign: Self-Alignment for Code Generation

Yuxiang Wei, Federico Cassano, Jiawei Liu, Yifeng Ding, Naman Jain, Zachary Mueller, Harm de Vries, Leandro Von Werra, Arjun Guha, Lingming Zhang

Abstract

Instruction tuning is essential for transforming base Large Language Models (LLMs) into effective coding assistants. Traditional methods rely on expensive human annotations or "distillation" from proprietary models like GPT-4, which introduce licensing restrictions and distribution gaps. We present SelfCodeAlign, a fully transparent, permissively licensed pipeline that enables code LLMs to self-align using their own base model. By generating tasks and validating them via automated execution, a 7B parameter model can outperform models ten times its size that rely on proprietary distillation.

Introduction

To be useful for practical coding tasks, LLMs must undergo instruction tuning to follow specific prompts. Current state-of-the-art (SOTA) open-source models often rely on distillation, where a smaller student model mimics a larger proprietary teacher. However, this approach suffers from legal constraints, a distribution gap where students fail to grasp complex teacher logic, and a lack of transparency regarding the teacher's training data. SelfCodeAlign addresses these issues by asking: Can a model become a world-class coder by generating its own training data and verifying its correctness?

Methodology

The SelfCodeAlign pipeline follows a three-stage self-alignment process using the same base model throughout:

  1. Seed Snippet Collection: High-quality Python functions are extracted from The Stack V1 and filtered for quality using type checking and docstring analysis.
  2. Diverse Instruction Generation (Self-OSS-Instruct): Instead of direct task generation, the model first extracts abstract programming concepts from seeds. It then generates original tasks across varying difficulties and categories (functions vs. classes).
  3. Response Generation & Self-Validation: The model generates multiple solutions and test cases. These are executed in a secure Docker sandbox; only instruction-response pairs that pass their own tests are retained for the final training set.
The SelfCodeAlign pipeline: from code corpus mining to execution-validated responses.

Experimental Setup

The authors generated a dataset of 74k instruction-response pairs using CodeQwen1.5-7B. Evaluation spanned several benchmarks to ensure robustness:

  • Function-level: HumanEval+ and MBPP+.
  • Contamination-free: LiveCodeBench (problems released after the model's training cutoff).
  • Specialized: ClassEval (class-level coding), DS-1000 (data science), and CanItEdit (code editing).

Results and Discussion

SelfCodeAlign demonstrates that self-teaching can surpass mimicry. A CodeQwen1.5-7B model aligned with this method achieved a 67.1 pass@1 on HumanEval+, surpassing the 70B-parameter CodeLlama-70B-Instruct.

Comparison of SelfCodeAlign-CQ-7B against proprietary and distilled models across benchmarks and artifact transparency.

Key findings include:

  • Distribution Alignment: Models learn more effectively from their own data distribution than from a significantly more advanced teacher, avoiding hallucinations caused by style-mimicry without logic.
  • Concept-Driven Diversity: Extracting concepts before tasks prevents the "repetition trap" common in synthetic data generation.
  • Execution as Ground Truth: Automated verification in a sandbox ensures high-quality training data, solving the primary bottleneck in self-alignment.

Conclusion

SelfCodeAlign provides a path toward high-performance, transparent, and permissively licensed code LLMs. The method generalizes across models ranging from 3B to 33B parameters. Future work will explore using "negative samples" (failed code) for preference optimization (DPO/RLHF) and extending the pipeline to complex, multi-step agentic coding tasks like autonomous GitHub issue resolution.

Summary updated

BibTeX
@inproceedings{wei2024selfcodealign,
  author = {Yuxiang Wei and Federico Cassano and Jiawei Liu and Yifeng Ding and Naman Jain and Zachary Mueller and Harm de Vries and Leandro Von Werra and Arjun Guha and LINGMING ZHANG},
  title = {{SelfCodeAlign: Self-Alignment for Code Generation}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=xXRnUU7xTL},
}
Figure