Update training.py
Browse files12345698 unmodified lines10510610710610810911011122 unmodified lines13413513613513713813914017 unmodified lines1581591601591611621631643 unmodified lines16816917016917117217317417517617732 unmodified lines21021121221121321421521627 unmodified lines244245246245247248249250306 unmodified lines55755855955856056156256378 unmodified lines64264364464364564664764812 unmodified lines66166266366266366466566666766666866967067178 unmodified lines75075175275175375475525 unmodified lines7817827837837847857867872 unmodified lines790791792792793# training.py import torch._dynamotorch._dynamo.config.disable = Trueimport jsonimport osimport torch98 unmodified linesdef load_model(): model, tokenizer = FastLanguageModel.from_pretrained( model_name="unsloth/gemma-2-2b-it-bnb-4bit", max_seq_length=2048, max_seq_length=768, load_in_4bit=True, ) # FIXED: Lower rank (16), dropout=0 for stability22 unmodified lines test_prompt = "Hello, how are you?" messages = [{"role": "user", "content": test_prompt}] formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) inputs = tokenizer(formatted, return_tensors="pt").to("cuda") inputs = tokenizer(formatted, return_tensors="pt", max_length=768, truncation=True).to("cuda") with torch.no_grad(): outputs = model.generate( **inputs,17 unmodified lines# ======================================================================# 4. SUPERVISED WARM-UP (teaches JSON output)# ======================================================================def supervised_warmup(model, tokenizer, n_examples=500, epochs=2):def supervised_warmup(model, tokenizer, n_examples=500, epochs=8): print("\n" + "="*60) print("SUPERVISED WARM-UP: Teaching JSON format") print("="*60)3 unmodified lines '{"action_type": "run_tests"}', '{"action_type": "run_linter"}', '{"action_type": "inspect"}', '{"action_type": "query_docs", "content": "python keyerror handling"}', '{"action_type": "fix", "content": "def corrected():\n pass"}', '{"action_type": "comment", "content": "This looks good."}', '{"action_type": "question", "content": "Why is this variable used?"}', '{"action_type": "query_docs", "content": "KeyError"}', '{"action_type": "done"}', ] 32 unmodified lines{last_output}
Available actions:run_tests, run_linter, inspect, fix, comment, question, donerun_tests, run_linter, inspect, fix, comment, question, done, query_docs
Respond ONLY in JSON:{{"action_type": "...", "content": "..."}}"""27 unmodified lines print(f"Training on {n_examples} examples for {epochs} epochs...") trainer.train() print("✓ Warm-up complete\n")
torch.cuda.empty_cache() # ======================================================================# 5. ACTION GENERATION WITH LOGPROB TRACKING (fixed)# ======================================================================306 unmodified lines messages = [{"role": "user", "content": state}] formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) full_text = formatted + action inputs = tokenizer(full_text, return_tensors="pt").to("cuda") inputs = tokenizer(full_text, return_tensors="pt", max_length=768, truncation=True).to("cuda") outputs = model(**inputs) logits = outputs.logits78 unmodified linesdef train_ppo( n_iterations: int = 50, trajectories_per_iter: int = 10, n_epochs: int = 4, n_epochs: int = 2, max_steps: int = 10, learning_rate: float = 3e-5, clip_epsilon: float = 0.2,12 unmodified lines print("\n❌ Model sanity check failed – cannot proceed.") return # NEW: Supervised warm-up to teach JSON format supervised_warmup(model, tokenizer, n_examples=500, epochs=2) # NEW: Supervised warm-up to teach JSON format (500 steps with epochs=8) supervised_warmup(model, tokenizer, n_examples=500, epochs=8) optimizer = AdamW(model.parameters(), lr=learning_rate) env = CodeReviewEnv(reward_profile=reward_profile) env = CodeReviewEnv() if task_levels is None: task_levels = list(BUG_DB.keys()) 78 unmodified lines print("Model saved to ppo_final_model/")
# Save training curves for quick before/after comparisons. # These are intentionally simple line plots to avoid extra dependencies. if reward_history: plt.figure(figsize=(8, 4)) plt.plot(range(1, len(reward_history) + 1), reward_history, marker="o")25 unmodified lines# ======================================================================if __name__ == "__main__": train_ppo( n_iterations=50, n_iterations=30, trajectories_per_iter=10, n_epochs=4, max_steps=10,2 unmodified lines entropy_coef=0.01, gamma=0.99, eval_every=5, ) )training.py+15-14