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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# /// script
|
| 2 |
+
# dependencies = [
|
| 3 |
+
# "trl",
|
| 4 |
+
# "peft",
|
| 5 |
+
# "datasets",
|
| 6 |
+
# "transformers",
|
| 7 |
+
# "accelerate",
|
| 8 |
+
# "torch",
|
| 9 |
+
# "deepspeed",
|
| 10 |
+
# ]
|
| 11 |
+
# ///
|
| 12 |
+
|
| 13 |
+
import inspect
|
| 14 |
+
|
| 15 |
+
import datasets
|
| 16 |
+
import trl.experimental.gold as gold
|
| 17 |
+
from transformers import AutoTokenizer
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# -----------------------------
|
| 21 |
+
# Models
|
| 22 |
+
# -----------------------------
|
| 23 |
+
|
| 24 |
+
STUDENT_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 25 |
+
TEACHER_MODEL = "Qwen/Qwen2.5-Coder-7B-Instruct"
|
| 26 |
+
|
| 27 |
+
OUTPUT_DIR = "gold-code-deepspeed-test"
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# -----------------------------
|
| 31 |
+
#
|
| 32 |
+
# If ZeRO-3 is painfully slow, try this instead:
|
| 33 |
+
|
| 34 |
+
DS_CONFIG = {
|
| 35 |
+
"zero_optimization": {
|
| 36 |
+
"stage": 2,
|
| 37 |
+
"offload_optimizer": {
|
| 38 |
+
"device": "cpu",
|
| 39 |
+
"pin_memory": True,
|
| 40 |
+
},
|
| 41 |
+
"overlap_comm": True,
|
| 42 |
+
"contiguous_gradients": True,
|
| 43 |
+
},
|
| 44 |
+
"bf16": {
|
| 45 |
+
"enabled": True,
|
| 46 |
+
},
|
| 47 |
+
"train_micro_batch_size_per_gpu": "auto",
|
| 48 |
+
"gradient_accumulation_steps": "auto",
|
| 49 |
+
"gradient_clipping": "auto",
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# -----------------------------
|
| 54 |
+
# Dataset
|
| 55 |
+
# -----------------------------
|
| 56 |
+
|
| 57 |
+
def to_messages(example):
|
| 58 |
+
description = str(example.get("description", "")).strip()
|
| 59 |
+
|
| 60 |
+
if not description:
|
| 61 |
+
description = str(example)
|
| 62 |
+
|
| 63 |
+
# Keep prompts short at first. code_contests descriptions can be long.
|
| 64 |
+
description = description[:6000]
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
"messages": [
|
| 68 |
+
{
|
| 69 |
+
"role": "system",
|
| 70 |
+
"content": (
|
| 71 |
+
"You are a careful competitive programming assistant. "
|
| 72 |
+
"Return only the final correct solution code. "
|
| 73 |
+
"Do not include markdown or explanations."
|
| 74 |
+
),
|
| 75 |
+
},
|
| 76 |
+
{
|
| 77 |
+
"role": "user",
|
| 78 |
+
"content": (
|
| 79 |
+
"Solve this programming problem:\n\n"
|
| 80 |
+
f"{description}"
|
| 81 |
+
),
|
| 82 |
+
},
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def main():
|
| 88 |
+
print("Loading tokenizer...")
|
| 89 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 90 |
+
STUDENT_MODEL,
|
| 91 |
+
trust_remote_code=True,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if tokenizer.pad_token is None:
|
| 95 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 96 |
+
|
| 97 |
+
if tokenizer.pad_token_id is None:
|
| 98 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 99 |
+
|
| 100 |
+
print("Loading dataset...")
|
| 101 |
+
raw = datasets.load_dataset(
|
| 102 |
+
"deepmind/code_contests",
|
| 103 |
+
split="train[:100]",
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
print("Raw columns:", raw.column_names)
|
| 107 |
+
|
| 108 |
+
train_dataset = raw.map(
|
| 109 |
+
to_messages,
|
| 110 |
+
remove_columns=raw.column_names,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
print("Processed example:")
|
| 114 |
+
print(train_dataset[0])
|
| 115 |
+
|
| 116 |
+
config = gold.GOLDConfig(
|
| 117 |
+
output_dir=OUTPUT_DIR,
|
| 118 |
+
|
| 119 |
+
# GOLD generation settings
|
| 120 |
+
temperature=0.8,
|
| 121 |
+
top_p=0.95,
|
| 122 |
+
max_length=1024,
|
| 123 |
+
|
| 124 |
+
# Training settings
|
| 125 |
+
max_steps=24,
|
| 126 |
+
per_device_train_batch_size=1,
|
| 127 |
+
gradient_accumulation_steps=4,
|
| 128 |
+
learning_rate=5e-6,
|
| 129 |
+
|
| 130 |
+
# Logging/saving
|
| 131 |
+
logging_steps=1,
|
| 132 |
+
save_steps=12,
|
| 133 |
+
report_to="none",
|
| 134 |
+
|
| 135 |
+
# Precision
|
| 136 |
+
bf16=True,
|
| 137 |
+
|
| 138 |
+
# DeepSpeed
|
| 139 |
+
deepspeed=DS_CONFIG,
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# TRL versions differ: some use processing_class, some older ones use tokenizer.
|
| 143 |
+
trainer_kwargs = {
|
| 144 |
+
"model": STUDENT_MODEL,
|
| 145 |
+
"teacher_model": TEACHER_MODEL,
|
| 146 |
+
"args": config,
|
| 147 |
+
"train_dataset": train_dataset,
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
signature = inspect.signature(gold.GOLDTrainer)
|
| 151 |
+
|
| 152 |
+
if "processing_class" in signature.parameters:
|
| 153 |
+
trainer_kwargs["processing_class"] = tokenizer
|
| 154 |
+
elif "tokenizer" in signature.parameters:
|
| 155 |
+
trainer_kwargs["tokenizer"] = tokenizer
|
| 156 |
+
else:
|
| 157 |
+
print("Warning: GOLDTrainer signature has no processing_class/tokenizer parameter.")
|
| 158 |
+
|
| 159 |
+
print("Building GOLDTrainer...")
|
| 160 |
+
trainer = gold.GOLDTrainer(**trainer_kwargs)
|
| 161 |
+
|
| 162 |
+
print("Training...")
|
| 163 |
+
trainer.train()
|
| 164 |
+
|
| 165 |
+
print("Saving...")
|
| 166 |
+
trainer.save_model(OUTPUT_DIR)
|
| 167 |
+
|
| 168 |
+
# Optional push
|
| 169 |
+
trainer.push_to_hub("moos124/gold-code-deepspeed-test")
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
if __name__ == "__main__":
|
| 173 |
+
main()
|