CodeGen: Multi-Turn Program Synthesis를 지원하는 오픈소스 대형 코드 언어 모델
CodeGen: An Open Large Language Model for Code with Multi-Turn Program Synthesis
TL;DR Highlight
Salesforce가 공개한 최대 16.1B 규모의 코드 생성 LLM — 복잡한 문제를 여러 단계로 나눠 지시하면 코드 품질이 10%p 이상 올라간다.
Who Should Read
AI 코드 어시스턴트를 개발하거나 LLM 기반 코드 생성 파이프라인을 구축하는 백엔드/ML 엔지니어. 오픈소스 코드 모델을 파인튜닝하거나 프롬프트 전략을 최적화하려는 개발자.
Core Mechanics
- 350M~16.1B 파라미터 4가지 크기의 CODEGEN 모델을 오픈소스로 공개 — 학습 라이브러리 JAXFORMER와 체크포인트 모두 포함
- 학습 데이터를 자연어(ThePile) → 멀티언어 코드(BigQuery) → Python 전용(BigPython) 순서로 순차 학습하면 Python 코드 품질이 단계마다 크게 향상됨
- CODEGEN-MONO 16.1B은 HumanEval pass@1 기준 29.28%로 Codex 12B(28.81%)와 동등하거나 우세 — 절반 이하 크기인 6.1B도 26.13%로 Codex 12B에 근접
- 복잡한 문제를 여러 단계(Multi-Turn)로 쪼개서 지시하면 한 번에 지시할 때보다 pass rate가 약 10~22%p 상승 — 모든 모델 크기에서 일관되게 관찰됨
- Multi-Turn Programming Benchmark(MTPB) 공개 — 115개 문제를 3턴 이상으로 분해한 최초의 멀티턴 코드 생성 벤치마크
- 프롬프트 perplexity(모델이 지시문을 얼마나 잘 이해하는지 수치)가 낮을수록 코드 생성 성공률이 높고, 멀티턴 분해 시 perplexity가 싱글턴 대비 일관되게 낮아짐
Evidence
- HumanEval pass@1: CODEGEN-MONO 16.1B 29.28% vs Codex 12B 28.81% — 비슷한 크기 대비 경쟁력 있음
- MTPB pass rate: 멀티턴 47.34% vs 싱글턴 38.74% (16.1B 기준) — 약 8.6%p 향상, 350M 모델에서는 16.98% vs 5.75%로 무려 11.23%p 차이
- MTPB 평균 프롬프트 perplexity: 멀티턴 8.05 vs 싱글턴 10.25 (16.1B 기준) — 멀티턴 분해 시 모델 이해도가 높아짐을 수치로 확인
- MBPP 벤치마크 pass@1: CODEGEN-MONO 16.1B 35.28%, CODEGEN-MONO 6.1B 32.48%로 INCODER 6B(21.30%)를 크게 상회
How to Apply
- 복잡한 기능 구현 요청 시 단일 프롬프트 대신 서브태스크로 분해해서 순차적으로 요청하면 코드 품질이 높아짐 — 예: '선형 회귀 구현해줘' 대신 '①numpy import ②데이터 행렬 생성 ③가중치 계산 ④예측값 출력' 순서로 나눠서 대화
- Hugging Face에서 CODEGEN 체크포인트(Salesforce/codegen-16B-mono 등)를 로드해 도메인 특화 파인튜닝 베이스 모델로 활용 — 일반 GPT-J 대비 Python 코드 태스크에서 확연히 높은 성능 기대
- 코드 어시스턴트 챗봇 구현 시 이전 턴의 프롬프트+생성코드를 컨텍스트로 누적 연결(concatenate)해서 다음 턴에 넘기면 멀티턴 능력을 활용 가능 — 단순 단일 응답 방식보다 복잡한 문제 해결력이 크게 높아짐
Code Example
from transformers import AutoTokenizer, AutoModelForCausalLM
# CODEGEN 모델 로드 (Hugging Face)
tokenizer = AutoTokenizer.from_pretrained("Salesforce/codegen-2B-mono")
model = AutoModelForCausalLM.from_pretrained("Salesforce/codegen-2B-mono")
# 멀티턴 방식: 이전 턴 결과를 컨텍스트로 누적
context = "# Import libraries.\nimport numpy as np\n"
# Turn 1
prompt_1 = "# Define a list named 'nums' with values [3, 1, 4, 1, 5, 9, 2, 6].\n"
inputs = tokenizer(context + prompt_1, return_tensors="pt")
outputs = model.generate(**inputs, max_new_tokens=50, temperature=0.2, do_sample=True)
generated_1 = tokenizer.decode(outputs[0], skip_special_tokens=True)
print("Turn 1 output:\n", generated_1)
# Turn 2: 이전 턴 결과를 컨텍스트에 포함
prompt_2 = "# Sort 'nums' in descending order and print the result.\n"
context_2 = generated_1 + "\n"
inputs_2 = tokenizer(context_2 + prompt_2, return_tensors="pt")
outputs_2 = model.generate(**inputs_2, max_new_tokens=50, temperature=0.2, do_sample=True)
generated_2 = tokenizer.decode(outputs_2[0], skip_special_tokens=True)
print("Turn 2 output:\n", generated_2)Terminology
Related Resources
Original Abstract (Expand)
Program synthesis strives to generate a computer program as a solution to a given problem specification, expressed with input-output examples or natural language descriptions. The prevalence of large language models advances the state-of-the-art for program synthesis, though limited training resources and data impede open access to such models. To democratize this, we train and release a family of large language models up to 16.1B parameters, called CODEGEN, on natural language and programming language data, and open source the training library JAXFORMER. We show the utility of the trained model by demonstrating that it is competitive with the previous state-of-the-art on zero-shot Python code generation on HumanEval. We further investigate the multi-step paradigm for program synthesis, where a single program is factorized into multiple prompts specifying subproblems. To this end, we construct an open benchmark, Multi-Turn Programming Benchmark (MTPB), consisting of 115 diverse problem sets that are factorized into multi-turn prompts. Our analysis on MTPB shows that the same intent provided to CODEGEN in multi-turn fashion significantly improves program synthesis over that provided as a single turn. We make the training library JAXFORMER and model checkpoints available as open source contribution: https://github.com/salesforce/CodeGen.