Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- train/generate_data.py +38 -0
- train/grpo_trainer.py +53 -0
- train/synthetic_data.json +0 -0
train/generate_data.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import random
|
| 3 |
+
|
| 4 |
+
def generate_disaster_data(num_samples=100):
|
| 5 |
+
dataset = []
|
| 6 |
+
grid_size = 5
|
| 7 |
+
|
| 8 |
+
for _ in range(num_samples):
|
| 9 |
+
# Randomize positions
|
| 10 |
+
start = (random.randint(0, 4), random.randint(0, 4))
|
| 11 |
+
target = (random.randint(0, 4), random.randint(0, 4))
|
| 12 |
+
while target == start:
|
| 13 |
+
target = (random.randint(0, 4), random.randint(0, 4))
|
| 14 |
+
|
| 15 |
+
obstacle = (random.randint(0, 4), random.randint(0, 4))
|
| 16 |
+
while obstacle == start or obstacle == target:
|
| 17 |
+
obstacle = (random.randint(0, 4), random.randint(0, 4))
|
| 18 |
+
|
| 19 |
+
instruction = (
|
| 20 |
+
f"You are a disaster response drone. Grid is {grid_size}x{grid_size}. "
|
| 21 |
+
f"Start: {start}. Goal: {target}. Obstacle at {obstacle}. "
|
| 22 |
+
"Output the move sequence (North, South, East, West) to reach the goal safely."
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Simple placeholder logic for 'ideal' output
|
| 26 |
+
# In a real run, GRPO will learn to improve this
|
| 27 |
+
dataset.append({
|
| 28 |
+
"instruction": instruction,
|
| 29 |
+
"input": "",
|
| 30 |
+
"output": "Thinking: I must calculate the path... Final Move: [Sequence]"
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
with open("synthetic_data.json", "w") as f:
|
| 34 |
+
json.dump(dataset, f, indent=4)
|
| 35 |
+
print(f"✅ Successfully generated {num_samples} samples in train/synthetic_data.json")
|
| 36 |
+
|
| 37 |
+
if _name_ == "_main_":
|
| 38 |
+
generate_disaster_data(200) # Generates 200 scenarios
|
train/grpo_trainer.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from unsloth import FastLanguageModel, PatchFastRL
|
| 2 |
+
PatchFastRL("GRPO", FastLanguageModel)
|
| 3 |
+
from trl import GRPOTrainer, GRPOConfig
|
| 4 |
+
from datasets import load_dataset
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# 1. Load Model & Tokenizer
|
| 8 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 9 |
+
model_name = "unsloth/meta-llama-3.1-8b-instruct-bnb-4bit",
|
| 10 |
+
max_seq_length = 512,
|
| 11 |
+
load_in_4bit = True,
|
| 12 |
+
fast_inference = True,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# 2. Add LoRA Adapters for training
|
| 16 |
+
model = FastLanguageModel.get_peft_model(
|
| 17 |
+
model,
|
| 18 |
+
r = 16,
|
| 19 |
+
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"],
|
| 20 |
+
lora_alpha = 16,
|
| 21 |
+
lora_dropout = 0,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# 3. Load your freshly generated data
|
| 25 |
+
dataset = load_dataset("json", data_files="synthetic_data.json", split="train")
|
| 26 |
+
|
| 27 |
+
# 4. Define Reward Functions (The logic for your Disaster Grid)
|
| 28 |
+
def reward_reach_goal(compts, **kwargs):
|
| 29 |
+
# Reward for including the correct target coordinates in the output
|
| 30 |
+
rewards = [1.0 if "Final Move:" in c else 0.0 for c in compts]
|
| 31 |
+
return rewards
|
| 32 |
+
|
| 33 |
+
# 5. Training Configuration
|
| 34 |
+
training_args = GRPOConfig(
|
| 35 |
+
learning_rate = 5e-6,
|
| 36 |
+
num_train_epochs = 1,
|
| 37 |
+
per_device_train_batch_size = 1,
|
| 38 |
+
gradient_accumulation_steps = 4,
|
| 39 |
+
outputs_dir = "outputs",
|
| 40 |
+
optim = "adamw_8bit",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# 6. Initialize Trainer
|
| 44 |
+
trainer = GRPOTrainer(
|
| 45 |
+
model = model,
|
| 46 |
+
reward_funcs = [reward_reach_goal],
|
| 47 |
+
args = training_args,
|
| 48 |
+
train_dataset = dataset,
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# 7. Start Training
|
| 52 |
+
trainer.train()
|
| 53 |
+
model.save_pretrained_merged("disaster_model_final", tokenizer, save_method = "merged_16bit")
|
train/synthetic_data.json
ADDED
|
File without changes
|