Spaces:
Sleeping
Sleeping
Upload 3 files
Browse filesFiles changed (3)
- train/generate_data.pyAdded+38
- train/grpo_trainer.pyAdded+53
- train/synthetic_data.jsonAdded
1234567891011121314151617181920212223242526272829303132333435363738import json
import random
def generate_disaster_data(num_samples=100):
dataset = []
grid_size = 5
for _ in range(num_samples):
# Randomize positions
start = (random.randint(0, 4), random.randint(0, 4))
target = (random.randint(0, 4), random.randint(0, 4))
while target == start:
target = (random.randint(0, 4), random.randint(0, 4))
obstacle = (random.randint(0, 4), random.randint(0, 4))
while obstacle == start or obstacle == target:
obstacle = (random.randint(0, 4), random.randint(0, 4))
instruction = (
f"You are a disaster response drone. Grid is {grid_size}x{grid_size}. "
f"Start: {start}. Goal: {target}. Obstacle at {obstacle}. "
"Output the move sequence (North, South, East, West) to reach the goal safely."
)
# Simple placeholder logic for 'ideal' output
# In a real run, GRPO will learn to improve this
dataset.append({
"instruction": instruction,
"input": "",
"output": "Thinking: I must calculate the path... Final Move: [Sequence]"
})
with open("synthetic_data.json", "w") as f:
json.dump(dataset, f, indent=4)
print(f"✅ Successfully generated {num_samples} samples in train/synthetic_data.json")
if _name_ == "_main_":
generate_disaster_data(200) # Generates 200 scenarios1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253from unsloth import FastLanguageModel, PatchFastRL
PatchFastRL("GRPO", FastLanguageModel)
from trl import GRPOTrainer, GRPOConfig
from datasets import load_dataset
import torch
# 1. Load Model & Tokenizer
model, tokenizer = FastLanguageModel.from_pretrained(
model_name = "unsloth/meta-llama-3.1-8b-instruct-bnb-4bit",
max_seq_length = 512,
load_in_4bit = True,
fast_inference = True,
)
# 2. Add LoRA Adapters for training
model = FastLanguageModel.get_peft_model(
model,
r = 16,
target_modules = ["q_proj", "k_proj", "v_proj", "o_proj"],
lora_alpha = 16,
lora_dropout = 0,
)
# 3. Load your freshly generated data
dataset = load_dataset("json", data_files="synthetic_data.json", split="train")
# 4. Define Reward Functions (The logic for your Disaster Grid)
def reward_reach_goal(compts, **kwargs):
# Reward for including the correct target coordinates in the output
rewards = [1.0 if "Final Move:" in c else 0.0 for c in compts]
return rewards
# 5. Training Configuration
training_args = GRPOConfig(
learning_rate = 5e-6,
num_train_epochs = 1,
per_device_train_batch_size = 1,
gradient_accumulation_steps = 4,
outputs_dir = "outputs",
optim = "adamw_8bit",
)
# 6. Initialize Trainer
trainer = GRPOTrainer(
model = model,
reward_funcs = [reward_reach_goal],
args = training_args,
train_dataset = dataset,
)
# 7. Start Training
trainer.train()
model.save_pretrained_merged("disaster_model_final", tokenizer, save_method = "merged_16bit")train/generate_data.pyAdded+38
train/grpo_trainer.pyAdded+53
train/synthetic_data.jsonAdded