-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.py
More file actions
340 lines (298 loc) · 11.9 KB
/
framework.py
File metadata and controls
340 lines (298 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
"""Professional training framework for reinforcement learning from human feedback."""
import uuid
import asyncio
from typing import Any, Callable, List, Tuple, Optional
from itertools import islice
from dataclasses import dataclass
from art.trajectories import History, Trajectory, TrajectoryGroup
from art import Model
from .llm_wrapper import add_thread
from .logging import FileLogger
from .message_utils import convert_langgraph_messages
from langchain_core.messages import ToolMessage, HumanMessage
from langchain_core.prompt_values import ChatPromptValue
def batched(iterable, n):
"""Batch data into tuples of length n. The last batch may be shorter.
>>> list(batched([1, 2, 3, 4, 5], 2))
[(1, 2), (3, 4), (5,)]
"""
if n < 1:
raise ValueError("n must be at least one")
it = iter(iterable)
while batch := tuple(islice(it, n)):
yield batch
@dataclass
class TrainingConfig:
"""Configuration for training parameters."""
epochs: int
batch_size: int
group_size: int
validation_samples: int = 2
def __post_init__(self):
if self.epochs <= 0:
raise ValueError("epochs must be positive")
if self.batch_size <= 0:
raise ValueError("batch_size must be positive")
if self.group_size <= 0:
raise ValueError("group_size must be positive")
if self.validation_samples < 0:
raise ValueError("validation_samples must be non-negative")
class TrainingFramework:
"""
A professional training framework for running reinforcement learning experiments.
This framework orchestrates the training process by managing:
- Model rollouts and trajectory collection
- Reward computation and evaluation
- Batch processing and training steps
- Logging and monitoring
"""
def __init__(self):
"""Initialize the training framework."""
pass
async def execute_rollout(
self,
*,
model,
scenario: Any,
agent_function: Callable
) -> Tuple[Any, str]:
"""
Execute a single rollout for a given model and scenario.
Args:
model: The model to execute the rollout with
scenario: The scenario/prompt to run
agent_function: The agent function that processes the scenario
Returns:
Tuple of (result, log_path)
"""
thread_id = str(uuid.uuid4())
add_thread(
thread_id,
model.inference_base_url,
model.inference_api_key,
model.inference_model_name
)
result = await agent_function(scenario, thread_id)
log_path = f'.art/langgraph/{thread_id}'
return result, log_path
def create_trajectory_from_logs(self, log_path: str, reward: float) -> Trajectory:
"""
Build a trajectory by merging cumulative logs:
- Each conversation grows if its input matches an existing one.
- If it doesn't match, start a new conversation.
"""
logs = FileLogger(log_path).load_logs()
conversations = []
for log_entry in logs:
output = log_entry[1]['output']
raw_output = output.get('raw') if hasattr(output, 'get') else output
input_msgs = log_entry[1]['input'].to_messages() if isinstance(log_entry[1]['input'], ChatPromptValue) else log_entry[1]['input']
new_conversation = input_msgs + [raw_output]
# Try to match with existing conversations
matched = False
for idx, existing in enumerate(conversations):
# Remove ToolMessages if needed — adapt as per your structure!
existing_non_tool = [m for m in existing if not isinstance(m, ToolMessage)]
new_non_tool = [m for m in input_msgs if not isinstance(m, ToolMessage)]
new_non_tool = new_non_tool[:-1] if new_non_tool and isinstance(new_non_tool[-1], HumanMessage) else new_non_tool
if existing_non_tool == new_non_tool:
# Replace with the longer one
conversations[idx] = new_conversation
matched = True
break
if not matched:
conversations.append(new_conversation)
# Build final trajectory
trajectory = Trajectory(messages_and_choices=[], reward=reward)
for idx, conv in enumerate(conversations):
converted = convert_langgraph_messages(conv)
if idx == 0:
trajectory.messages_and_choices = converted
else:
trajectory.additional_histories.append(
History(messages_and_choices=converted, tools=None)
)
return trajectory
async def process_scenario(
self,
*,
model,
scenario: Any,
group_size: int,
agent_function: Callable,
reward_function: Callable,
validation_model = None,
validation_samples: int = 2
) -> TrajectoryGroup:
"""
Process a single scenario by running multiple rollouts and computing rewards.
Args:
model: The model being trained
scenario: The scenario to process
group_size: Number of rollouts to execute
agent_function: Function that processes scenarios
reward_function: Function that computes rewards
validation_model: Optional validation model for comparison
validation_samples: Number of validation samples to include
Returns:
TrajectoryGroup containing all rollouts and their rewards
"""
# Execute main model rollouts
rollout_tasks = [
self.execute_rollout(model=model, scenario=scenario, agent_function=agent_function)
for _ in range(group_size)
]
results_and_logs = await asyncio.gather(*rollout_tasks)
results, log_paths = zip(*results_and_logs)
# Execute validation rollouts if validation model provided
all_results = list(results)
all_logs = list(log_paths)
if validation_model:
val_tasks = [
self.execute_rollout(model=validation_model, scenario=scenario, agent_function=agent_function)
for _ in range(validation_samples)
]
val_results_and_logs = await asyncio.gather(*val_tasks)
val_results, val_logs = zip(*val_results_and_logs)
all_results.extend(val_results)
all_logs.extend(val_logs)
trajectories = [
self.create_trajectory_from_logs(log_path, 0)
for log_path in all_logs
]
rewards = await(reward_function(scenario, all_results, trajectories))
for reward, traj in zip(rewards, trajectories):
traj.reward = reward
wins = 0
total = 0
for traj in trajectories[:len(results)]:
traj.metrics["wins"] = 0
traj.metrics["win_rate"] = 0
for val in trajectories[len(results):]:
traj.metrics["wins"] += 1 if traj.reward >= val.reward else 0
traj.metrics["win_rate"] += (1 if traj.reward >= val.reward else 0) / len(trajectories[len(results):])
if traj.reward >= val.reward:
wins += 1
total += 1
print(f"Win rate: {wins/total}")
return TrajectoryGroup(trajectories=trajectories[:len(results)])
async def execute_training_step(
self,
*,
model: Model,
scenarios: List[Any],
group_size: int,
agent_function: Callable,
reward_function: Callable,
validation_model = None,
validation_samples: int = 2
) -> None:
"""
Execute a single training step across multiple scenarios.
Args:
model: The model being trained
scenarios: List of scenarios to process
group_size: Number of rollouts per scenario
agent_function: Function that processes scenarios
reward_function: Function that computes rewards
validation_model: Optional validation model
validation_samples: Number of validation samples to include
"""
scenario_tasks = [
self.process_scenario(
model=model,
scenario=scenario,
group_size=group_size,
agent_function=agent_function,
reward_function=reward_function,
validation_model=validation_model,
validation_samples=validation_samples
)
for scenario in scenarios
]
trajectory_groups = await asyncio.gather(*scenario_tasks)
await model.train(trajectory_groups)
await model.backend()._experimental_push_to_s3(model)
async def run_training_epoch(
self,
*,
model,
scenarios: List[Any],
batch_size: int,
group_size: int,
agent_function: Callable,
reward_function: Callable,
validation_model = None,
validation_samples: int = 2
) -> None:
"""
Run a complete training epoch by processing scenarios in batches.
Args:
model: The model being trained
scenarios: List of all scenarios
batch_size: Number of scenarios per batch
group_size: Number of rollouts per scenario
agent_function: Function that processes scenarios
reward_function: Function that computes rewards
validation_model: Optional validation model
validation_samples: Number of validation samples to include
"""
for batch in batched(scenarios, batch_size):
await self.execute_training_step(
model=model,
scenarios=batch,
group_size=group_size,
agent_function=agent_function,
reward_function=reward_function,
validation_model=validation_model,
validation_samples=validation_samples
)
async def run_training(
self,
*,
model,
scenarios: List[Any],
agent_function: Callable,
reward_function: Callable,
config: TrainingConfig,
validation_model = None
) -> None:
"""
Run the complete training process for the specified number of epochs.
Args:
model: The model being trained
scenarios: List of training scenarios
agent_function: Function that processes scenarios
reward_function: Function that computes rewards
config: Training configuration containing epochs, batch_size, group_size, etc.
validation_model: Optional validation model
"""
start_step = await model.get_step()
for epoch in range(config.epochs):
await self.run_training_epoch(
model=model,
scenarios=scenarios,
batch_size=config.batch_size,
group_size=config.group_size,
agent_function=agent_function,
reward_function=reward_function,
validation_model=validation_model,
validation_samples=config.validation_samples
)
# Backward compatibility functions
async def train(model, epochs, scenarios, batch_size, group_size, do_ai, do_reward, val_model):
"""Legacy training function for backward compatibility."""
framework = TrainingFramework()
config = TrainingConfig(
epochs=epochs,
batch_size=batch_size,
group_size=group_size
)
await framework.run_training(
model=model,
scenarios=scenarios,
agent_function=do_ai,
reward_function=do_reward,
config=config,
validation_model=val_model
)