Spaces:
Sleeping
Sleeping
fix(train_v3): use PMOpsGRPOTrainer to fix reward kwarg plumbing
Browse filesPatchFastRL strips custom rollout keys before calling reward_func,
so the 'reward' key never arrived. PMOpsGRPOTrainer overrides
_calculate_rewards to inject pre-computed rewards from the rollout
batch directly, bypassing the broken kwargs path entirely.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 unmodified line2345678924 unmodified lines3435363738394017 unmodified lines5859606162636433 unmodified lines989910010110210310425 unmodified lines13013113213313413513610 unmodified lines14714814915015115215327 unmodified lines18118218318418518618718 unmodified lines20620720820921021121251 unmodified lines26426526626726826927073 unmodified lines34434534634734834935041 unmodified lines39239339439539639739860 unmodified lines45946046146246346446514 unmodified lines480481482483484485486106 unmodified lines5935945955965975985991 unmodified line60160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764831 unmodified lines68068168268368468568615 unmodified lines70270370470570670770886 unmodified lines79579679779879980080135 unmodified lines8378388398408418428437 unmodified lines8518528538541 unmodified line"nbformat": 4,"nbformat_minor": 5,"metadata": {"kernelspec": {"display_name": "Python 3", "language": "python", "name": "python3"},"language_info": {"name": "python", "version": "3.11.0"}},"cells": [{24 unmodified lines"cell_type": "markdown","id": "cell-1-md","metadata": {},"source": ["## 0. Install"]},{"cell_type": "code",17 unmodified lines"cell_type": "markdown","id": "cell-2-md","metadata": {},"source": ["## 1. Imports + GPU Config"]},{"cell_type": "code",33 unmodified lines"cell_type": "markdown","id": "cell-3-md","metadata": {},"source": ["## 2. Clone PM-Ops Repo"]},{"cell_type": "code",25 unmodified lines"cell_type": "markdown","id": "cell-4-md","metadata": {},"source": ["## 3. HuggingFace Login"]},{"cell_type": "code",10 unmodified lines"cell_type": "markdown","id": "cell-5-md","metadata": {},"source": ["## 4. Start Local PM-Ops Server"]},{"cell_type": "code",27 unmodified lines"cell_type": "markdown","id": "cell-6-md","metadata": {},"source": ["## 5. Verify Env"]},{"cell_type": "code",18 unmodified lines"cell_type": "markdown","id": "cell-7-md","metadata": {},"source": ["## 6. Load Model — Unsloth 4-bit + LoRA"]},{"cell_type": "code",51 unmodified lines"cell_type": "markdown","id": "cell-9-md","metadata": {},"source": ["## 7. Generate SFT Demonstration Dataset"]},{"cell_type": "code",73 unmodified lines"cell_type": "markdown","id": "cell-10-md","metadata": {},"source": ["## 8. SFT Training (~15 min on A100)"]},{"cell_type": "code",41 unmodified lines"cell_type": "markdown","id": "cell-11-md","metadata": {},"source": ["## 9. Verify SFT Output — Model Must Output Valid JSON"]},{"cell_type": "code",60 unmodified lines"cell_type": "markdown","id": "cell-13-md","metadata": {},"source": ["## 10. Generate GRPO Training Dataset"]},{"cell_type": "code",14 unmodified lines"cell_type": "markdown","id": "cell-14-md","metadata": {},"source": ["## 11. GRPO Rollout + Reward Functions"]},{"cell_type": "code",106 unmodified lines"cell_type": "markdown","id": "cell-15-md","metadata": {},"source": ["## 12. GRPO Config + Trainer"]},{"cell_type": "code",1 unmodified line"metadata": {},"outputs": [],"execution_count": null,"source": ["from trl import GRPOConfig, GRPOTrainer\n","\n","OUTPUT_DIR = 'pm-ops-grpo-Qwen3-1.7B-triage-v3'\n","HF_REPO_ID = f'Saurav1/{OUTPUT_DIR}'\n","\n","grpo_cfg = GRPOConfig(\n"," # Training\n"," num_train_epochs = 2,\n"," learning_rate = 1e-6, # lower LR: model has SFT init, don't overwrite it\n"," gradient_accumulation_steps = GRAD_ACCUM,\n"," per_device_train_batch_size = 1,\n"," warmup_steps = 5,\n"," num_generations = NUM_GEN,\n"," # Sequence lengths\n"," max_completion_length = MAX_COMP_LEN,\n"," max_prompt_length = 4096,\n"," # Unsloth vLLM for fast generation\n"," use_vllm = True,\n"," # Output\n"," output_dir = OUTPUT_DIR,\n"," report_to = 'trackio',\n"," trackio_space_id = OUTPUT_DIR,\n"," logging_steps = 1,\n"," save_steps = 20,\n"," gradient_checkpointing = False, # Unsloth handles this\n",")\n","\n","eff_batch = grpo_cfg.per_device_train_batch_size * GRAD_ACCUM\n","total_steps = len(grpo_dataset) * NUM_GEN * grpo_cfg.num_train_epochs // eff_batch\n","print(f'GRPO: {len(grpo_dataset)} eps x {NUM_GEN} gen x {grpo_cfg.num_train_epochs} epochs -> ~{total_steps} steps')\n","\n","trainer = GRPOTrainer(\n"," model = model,\n"," processing_class = tokenizer,\n"," reward_funcs = grpo_reward_func,\n"," train_dataset = grpo_dataset,\n"," args = grpo_cfg,\n"," rollout_func = grpo_rollout_func,\n",")\n","print('GRPOTrainer ready')"]},{"cell_type": "markdown",31 unmodified lines"cell_type": "markdown","id": "cell-17-md","metadata": {},"source": ["## 14. Save Model"]},{"cell_type": "code",15 unmodified lines"cell_type": "markdown","id": "cell-18-md","metadata": {},"source": ["## 15. Evaluate: Baseline vs Trained"]},{"cell_type": "code",86 unmodified lines"cell_type": "markdown","id": "cell-19-md","metadata": {},"source": ["## 16. Plot Results"]},{"cell_type": "code",35 unmodified lines"cell_type": "markdown","id": "cell-20-md","metadata": {},"source": ["## 17. Teardown"]},{"cell_type": "code",7 unmodified lines]}]}1 unmodified line234567891011121314151624 unmodified lines41424344454647484917 unmodified lines67686970717273747533 unmodified lines10911011111211311411511611725 unmodified lines14314414514614714814915015110 unmodified lines16216316416516616716816917027 unmodified lines19819920020120220320420520618 unmodified lines22522622722822923023123223351 unmodified lines28528628728828929029129229373 unmodified lines36736836937037137237337437541 unmodified lines41741841942042142242342442560 unmodified lines48648748848949049149249349414 unmodified lines509510511512513514515516517106 unmodified lines6246256266276286296306316321 unmodified line63463563663763863964031 unmodified lines67267367467567667767867968015 unmodified lines69669769869970070170270370486 unmodified lines79179279379479579679779879935 unmodified lines8358368378388398408418428437 unmodified lines8518528538541 unmodified line"nbformat": 4,"nbformat_minor": 5,"metadata": {"kernelspec": {"display_name": "Python 3","language": "python","name": "python3"},"language_info": {"name": "python","version": "3.11.0"}},"cells": [{24 unmodified lines"cell_type": "markdown","id": "cell-1-md","metadata": {},"source": ["## 0. Install"]},{"cell_type": "code",17 unmodified lines"cell_type": "markdown","id": "cell-2-md","metadata": {},"source": ["## 1. Imports + GPU Config"]},{"cell_type": "code",33 unmodified lines"cell_type": "markdown","id": "cell-3-md","metadata": {},"source": ["## 2. Clone PM-Ops Repo"]},{"cell_type": "code",25 unmodified lines"cell_type": "markdown","id": "cell-4-md","metadata": {},"source": ["## 3. HuggingFace Login"]},{"cell_type": "code",10 unmodified lines"cell_type": "markdown","id": "cell-5-md","metadata": {},"source": ["## 4. Start Local PM-Ops Server"]},{"cell_type": "code",27 unmodified lines"cell_type": "markdown","id": "cell-6-md","metadata": {},"source": ["## 5. Verify Env"]},{"cell_type": "code",18 unmodified lines"cell_type": "markdown","id": "cell-7-md","metadata": {},"source": ["## 6. Load Model — Unsloth 4-bit + LoRA"]},{"cell_type": "code",51 unmodified lines"cell_type": "markdown","id": "cell-9-md","metadata": {},"source": ["## 7. Generate SFT Demonstration Dataset"]},{"cell_type": "code",73 unmodified lines"cell_type": "markdown","id": "cell-10-md","metadata": {},"source": ["## 8. SFT Training (~15 min on A100)"]},{"cell_type": "code",41 unmodified lines"cell_type": "markdown","id": "cell-11-md","metadata": {},"source": ["## 9. Verify SFT Output — Model Must Output Valid JSON"]},{"cell_type": "code",60 unmodified lines"cell_type": "markdown","id": "cell-13-md","metadata": {},"source": ["## 10. Generate GRPO Training Dataset"]},{"cell_type": "code",14 unmodified lines"cell_type": "markdown","id": "cell-14-md","metadata": {},"source": ["## 11. GRPO Rollout + Reward Functions"]},{"cell_type": "code",106 unmodified lines"cell_type": "markdown","id": "cell-15-md","metadata": {},"source": ["## 12. GRPO Config + Trainer"]},{"cell_type": "code",1 unmodified line"metadata": {},"outputs": [],"execution_count": null,"source": "from trl import GRPOConfig\nfrom training.pm_ops_trainer import PMOpsGRPOTrainer\n\nOUTPUT_DIR = 'pm-ops-grpo-Qwen3-1.7B-triage-v3'\nHF_REPO_ID = f'Saurav1/{OUTPUT_DIR}'\n\ngrpo_cfg = GRPOConfig(\n # Training\n num_train_epochs = 2,\n learning_rate = 1e-6, # lower LR: model has SFT init, don't overwrite it\n gradient_accumulation_steps = GRAD_ACCUM,\n per_device_train_batch_size = 1,\n warmup_steps = 5,\n num_generations = NUM_GEN,\n # Sequence lengths\n max_completion_length = MAX_COMP_LEN,\n max_prompt_length = 4096,\n # Unsloth vLLM for fast generation\n use_vllm = True,\n # Output\n output_dir = OUTPUT_DIR,\n report_to = 'trackio',\n trackio_space_id = OUTPUT_DIR,\n logging_steps = 1,\n save_steps = 20,\n gradient_checkpointing = False, # Unsloth handles this\n)\n\neff_batch = grpo_cfg.per_device_train_batch_size * GRAD_ACCUM\ntotal_steps = len(grpo_dataset) * NUM_GEN * grpo_cfg.num_train_epochs // eff_batch\nprint(f'GRPO: {len(grpo_dataset)} eps x {NUM_GEN} gen x {grpo_cfg.num_train_epochs} epochs -> ~{total_steps} steps')\n\n# PMOpsGRPOTrainer overrides _calculate_rewards to read the pre-computed\n# 'reward' key directly from the rollout batch — bypasses the broken\n# PatchFastRL kwargs plumbing that strips custom rollout keys.\ntrainer = PMOpsGRPOTrainer(\n model = model,\n processing_class = tokenizer,\n reward_funcs = grpo_reward_func, # kept as fallback only\n train_dataset = grpo_dataset,\n args = grpo_cfg,\n rollout_func = grpo_rollout_func,\n)\nprint('PMOpsGRPOTrainer ready')"},{"cell_type": "markdown",31 unmodified lines"cell_type": "markdown","id": "cell-17-md","metadata": {},"source": ["## 14. Save Model"]},{"cell_type": "code",15 unmodified lines"cell_type": "markdown","id": "cell-18-md","metadata": {},"source": ["## 15. Evaluate: Baseline vs Trained"]},{"cell_type": "code",86 unmodified lines"cell_type": "markdown","id": "cell-19-md","metadata": {},"source": ["## 16. Plot Results"]},{"cell_type": "code",35 unmodified lines"cell_type": "markdown","id": "cell-20-md","metadata": {},"source": ["## 17. Teardown"]},{"cell_type": "code",7 unmodified lines]}]}
training/train_v3.ipynb+62-62