|
| 1 | +# OpenEnv - Generic Training Framework |
| 2 | + |
| 3 | +A centralized framework for training language models on any OpenEnv task using GRPO (Grouped Relative Policy Optimization). |
| 4 | + |
| 5 | +## 📁 Folder Structure |
| 6 | + |
| 7 | +``` |
| 8 | +apps/openenv/ |
| 9 | + ├── main.py # Generic training script |
| 10 | + ├── julia_utils.py # Julia task utilities |
| 11 | + ├── python_utils.py # Python/coding task utilities |
| 12 | + ├── llama3_8b_julia.yaml # Julia training config |
| 13 | + └── llama3_8b_coding.yaml # Python coding training config |
| 14 | +``` |
| 15 | + |
| 16 | +## 🎯 Key Features |
| 17 | + |
| 18 | +- **Single Main Script**: One `main.py` works for all OpenEnv tasks |
| 19 | +- **Task-Specific Utils**: Language-specific logic in separate files (e.g., `julia_utils.py`, `python_utils.py`) |
| 20 | +- **YAML Configuration**: Use `!function` references to load task-specific functions |
| 21 | +- **AutoEnv Integration**: Automatic environment and action class loading |
| 22 | +- **Easy Extension**: Add new languages by creating new utils files |
| 23 | + |
| 24 | +## 🚀 Usage |
| 25 | + |
| 26 | +### Run Julia Training |
| 27 | + |
| 28 | +```bash |
| 29 | +python -m apps.openenv.main --config apps/openenv/llama3_8b_julia.yaml |
| 30 | +``` |
| 31 | + |
| 32 | +### Run Python Coding Training |
| 33 | + |
| 34 | +```bash |
| 35 | +python -m apps.openenv.main --config apps/openenv/llama3_8b_coding.yaml |
| 36 | +``` |
| 37 | + |
| 38 | +## 📝 YAML Configuration |
| 39 | + |
| 40 | +Each task config needs: |
| 41 | + |
| 42 | +```yaml |
| 43 | +task: |
| 44 | + env_name: "julia" # Environment name for AutoEnv |
| 45 | + build_action: !function julia_utils.build_julia_action |
| 46 | + evaluate_response: !function julia_utils.evaluate_julia_response |
| 47 | + transform_sample: !function julia_utils.transform_julia_sample |
| 48 | +``` |
| 49 | +
|
| 50 | +The `!function` tag references functions from the utils files in the same directory. |
| 51 | + |
| 52 | +## 🔧 Adding a New Language |
| 53 | + |
| 54 | +To add support for a new language (e.g., Rust): |
| 55 | + |
| 56 | +### 1. Create Utils File |
| 57 | + |
| 58 | +Create `/home/kaiwu/work/kaiwu/forge/apps/openenv/rust_utils.py`: |
| 59 | + |
| 60 | +```python |
| 61 | +from envs.rust_env import RustAction |
| 62 | +
|
| 63 | +def build_rust_action(response: str, sample: dict) -> RustAction: |
| 64 | + """Build RustAction from model response.""" |
| 65 | + code = extract_rust_code(response) |
| 66 | + return RustAction(code=code, test_code=sample.get("test", "")) |
| 67 | +
|
| 68 | +def evaluate_rust_response(result, response: str, sample: dict) -> float: |
| 69 | + """Evaluate Rust code execution and return reward.""" |
| 70 | + if result.observation.exit_code == 0: |
| 71 | + return 1.0 |
| 72 | + return 0.0 |
| 73 | +
|
| 74 | +def transform_rust_sample(sample: dict, tokenizer) -> dict | None: |
| 75 | + """Transform dataset sample for Rust tasks.""" |
| 76 | + # Build prompt using tokenizer |
| 77 | + prompt = build_rust_prompt(sample, tokenizer) |
| 78 | + return { |
| 79 | + "request": prompt, |
| 80 | + "target": sample.get("test", ""), |
| 81 | + "task_id": sample.get("task_id", ""), |
| 82 | + } |
| 83 | +
|
| 84 | +def extract_rust_code(response: str) -> str: |
| 85 | + """Extract Rust code from markdown blocks.""" |
| 86 | + # Implementation... |
| 87 | + pass |
| 88 | +``` |
| 89 | + |
| 90 | +### 2. Create YAML Config |
| 91 | + |
| 92 | +Create `/home/kaiwu/work/kaiwu/forge/apps/openenv/llama3_8b_rust.yaml`: |
| 93 | + |
| 94 | +```yaml |
| 95 | +task: |
| 96 | + env_name: "rust" |
| 97 | + build_action: !function rust_utils.build_rust_action |
| 98 | + evaluate_response: !function rust_utils.evaluate_rust_response |
| 99 | + transform_sample: !function rust_utils.transform_rust_sample |
| 100 | +
|
| 101 | +dataset: |
| 102 | + path: "path/to/rust/dataset" |
| 103 | + # ... other dataset config |
| 104 | +
|
| 105 | +# ... rest of config (same as other tasks) |
| 106 | +``` |
| 107 | + |
| 108 | +### 3. Run It |
| 109 | + |
| 110 | +```bash |
| 111 | +python -m apps.openenv.main --config apps/openenv/llama3_8b_rust.yaml |
| 112 | +``` |
| 113 | + |
| 114 | +That's it! No changes to `main.py` needed. |
| 115 | + |
| 116 | +## 📋 Task Utils API |
| 117 | + |
| 118 | +Each task utils file should implement these functions: |
| 119 | + |
| 120 | +### Required Functions |
| 121 | + |
| 122 | +1. **`build_<lang>_action(response: str, sample: dict) -> Action`** |
| 123 | + - Builds environment action from model response |
| 124 | + - Example: `build_julia_action`, `build_python_action` |
| 125 | + |
| 126 | +2. **`evaluate_<lang>_response(result, response: str, sample: dict) -> float`** |
| 127 | + - Evaluates execution result and returns reward (0.0 to 1.0) |
| 128 | + - Example: `evaluate_julia_response`, `evaluate_python_response` |
| 129 | + |
| 130 | +3. **`transform_<lang>_sample(sample: dict, tokenizer) -> dict | None`** |
| 131 | + - Transforms raw dataset sample into training format |
| 132 | + - Returns dict with 'request', 'target', 'task_id' or None if invalid |
| 133 | + - Example: `transform_julia_sample`, `transform_python_sample` |
| 134 | + |
| 135 | +### Optional Helper Functions |
| 136 | + |
| 137 | +- **`get_<lang>_system_prompt() -> str`**: Get system prompt for the language |
| 138 | +- **`build_<lang>_prompt(sample: dict, tokenizer) -> str`**: Build formatted prompt |
| 139 | +- **`extract_<lang>_code(response: str) -> str`**: Extract code from markdown |
| 140 | + |
| 141 | +## 🔍 How It Works |
| 142 | + |
| 143 | +1. **Configuration Loading**: YAML config is loaded with `!function` references |
| 144 | +2. **Function Loading**: `main.py` dynamically loads functions from utils files |
| 145 | +3. **Environment Setup**: AutoEnv automatically loads correct env/action classes |
| 146 | +4. **Training Loop**: Generic GRPO loop uses task-specific functions for: |
| 147 | + - Dataset transformation |
| 148 | + - Action building |
| 149 | + - Reward evaluation |
| 150 | + |
| 151 | +## 📊 Dataset Format |
| 152 | + |
| 153 | +Each transformed sample should have: |
| 154 | + |
| 155 | +```python |
| 156 | +{ |
| 157 | + "request": str, # Formatted prompt for model |
| 158 | + "target": str, # Test code or target data |
| 159 | + "task_id": str, # Unique task identifier |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +## 🎓 Examples |
| 164 | + |
| 165 | +### Julia Utils |
| 166 | + |
| 167 | +- System prompt with strict formatting rules |
| 168 | +- Extract code from markdown blocks |
| 169 | +- Dense reward based on test pass rate |
| 170 | +- Handles Julia-specific syntax requirements |
| 171 | + |
| 172 | +### Python Utils |
| 173 | + |
| 174 | +- Simple system prompt for Python coding |
| 175 | +- Binary/proportional reward structure |
| 176 | +- Extracts code from markdown blocks |
| 177 | +- Works with HumanEval dataset format |
| 178 | + |
| 179 | +## 🔗 Integration with OpenEnv |
| 180 | + |
| 181 | +This framework uses OpenEnv's AutoEnv feature: |
| 182 | + |
| 183 | +```python |
| 184 | +from envs import AutoEnv, AutoAction |
| 185 | +
|
| 186 | +env_class = AutoEnv.from_name("julia") # Loads JuliaEnv |
| 187 | +action_class = AutoAction.from_env("julia") # Loads JuliaAction |
| 188 | +``` |
| 189 | + |
| 190 | +Make sure your environment is registered in OpenEnv's registry. |
| 191 | + |
| 192 | +## 🐛 Debugging |
| 193 | + |
| 194 | +Enable debug logging in main.py to see: |
| 195 | +- Function loading |
| 196 | +- Environment setup |
| 197 | +- Reward calculation |
| 198 | +- Code extraction |
| 199 | + |
| 200 | +Set log level in YAML: |
| 201 | +```yaml |
| 202 | +metric_logging: |
| 203 | + console: |
| 204 | + log_per_rank: True |
| 205 | +``` |
| 206 | + |
| 207 | +## 📚 References |
| 208 | + |
| 209 | +- **GRPO Algorithm**: Grouped Relative Policy Optimization |
| 210 | +- **OpenEnv**: Generic environment framework |
| 211 | +- **AutoEnv**: Automatic environment detection |
| 212 | +- **GenericOpenEnvActor**: Docker-based environment execution |
0 commit comments