| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import time |
| from transformers import TrainerCallback |
|
|
| class SpeedCallback(TrainerCallback): |
| def __init__(self): |
| self.last_time = None |
|
|
| def on_step_begin(self, args, state, control, **kwargs): |
| self.last_time = time.time() |
|
|
| def on_step_end(self, args, state, control, **kwargs): |
| if self.last_time is None: |
| return |
|
|
| elapsed = time.time() - self.last_time |
| remaining = max(0, state.max_steps - state.global_step) |
| eta_min = remaining * elapsed / 60 |
|
|
| print( |
| f"[speed] step {state.global_step}/{state.max_steps} | " |
| f"{elapsed:.2f}s/step | ETA {eta_min:.1f} min", |
| flush=True, |
| ) |
| import inspect |
|
|
| import datasets |
| import trl.experimental.gold as gold |
| from transformers import AutoTokenizer |
|
|
|
|
| |
| |
| |
|
|
| STUDENT_MODEL = "Qwen/Qwen2.5-0.5B-Instruct" |
| TEACHER_MODEL = "Qwen/Qwen2.5-Coder-1.5B-Instruct" |
|
|
| OUTPUT_DIR = "gold-code-deepspeed-test" |
|
|
|
|
| |
| |
| |
|
|
| DS_CONFIG = { |
| "zero_optimization": { |
| "stage": 2, |
| "overlap_comm": True, |
| "contiguous_gradients": True, |
| }, |
| "bf16": { |
| "enabled": True, |
| }, |
| "train_micro_batch_size_per_gpu": "auto", |
| "gradient_accumulation_steps": "auto", |
| "gradient_clipping": "auto", |
| } |
|
|
|
|
| |
| |
| |
|
|
| def to_messages(example): |
| description = str(example.get("description", "")).strip() |
|
|
| if not description: |
| description = str(example) |
|
|
| |
| description = description[:1500] |
|
|
| return { |
| "messages": [ |
| { |
| "role": "system", |
| "content": ( |
| "You are a careful competitive programming assistant. " |
| "Return only the final correct solution code. " |
| "Do not include markdown or explanations." |
| ), |
| }, |
| { |
| "role": "user", |
| "content": ( |
| "Solve this programming problem:\n\n" |
| f"{description}" |
| ), |
| }, |
| ] |
| } |
|
|
|
|
| def main(): |
| print("Loading tokenizer...") |
| tokenizer = AutoTokenizer.from_pretrained( |
| STUDENT_MODEL, |
| trust_remote_code=True, |
| ) |
|
|
| if tokenizer.pad_token is None: |
| tokenizer.pad_token = tokenizer.eos_token |
|
|
| if tokenizer.pad_token_id is None: |
| tokenizer.pad_token_id = tokenizer.eos_token_id |
|
|
| print("Loading dataset...") |
| raw = datasets.load_dataset( |
| "deepmind/code_contests", |
| split="train[:10000]", |
| ) |
|
|
| print("Raw columns:", raw.column_names) |
|
|
| train_dataset = raw.map( |
| to_messages, |
| remove_columns=raw.column_names, |
| ) |
|
|
| print("Processed example:") |
| print(train_dataset[0]) |
|
|
| config = gold.GOLDConfig( |
| output_dir=OUTPUT_DIR, |
|
|
| |
| temperature=0.8, |
| top_p=0.95, |
| max_length=512, |
| disable_tqdm=True, |
| |
| max_steps=1000, |
| per_device_train_batch_size=1, |
| gradient_accumulation_steps=4, |
| learning_rate=5e-6, |
| model_init_kwargs={ |
| "torch_dtype": "bfloat16", |
| "attn_implementation": "sdpa", |
| }, |
|
|
| |
| logging_steps=10, |
| save_steps=100, |
| report_to="none", |
| |
| |
| bf16=True, |
| hub_model_id="moos124/gold-code-deepspeed-testV2", |
| push_to_hub=True, |
| |
| deepspeed=DS_CONFIG, |
| ) |
|
|
| |
| trainer_kwargs = { |
| "model": STUDENT_MODEL, |
| "teacher_model": TEACHER_MODEL, |
| "args": config, |
| "train_dataset": train_dataset, |
| } |
|
|
| signature = inspect.signature(gold.GOLDTrainer) |
|
|
| if "processing_class" in signature.parameters: |
| trainer_kwargs["processing_class"] = tokenizer |
| elif "tokenizer" in signature.parameters: |
| trainer_kwargs["tokenizer"] = tokenizer |
| else: |
| print("Warning: GOLDTrainer signature has no processing_class/tokenizer parameter.") |
|
|
| print("Building GOLDTrainer...") |
| trainer = gold.GOLDTrainer(**trainer_kwargs) |
| trainer.add_callback(SpeedCallback()) |
| print("Training...") |
| trainer.train() |
|
|
| print("Saving...") |
| trainer.save_model(OUTPUT_DIR) |
| |
| trainer.push_to_hub() |
|
|
|
|
| if __name__ == "__main__": |
| main() |