Repository navigation
Expand file tree
/
Copy pathtrain_async.py
More file actions
186 lines (161 loc) · 8.21 KB
/
Copy pathtrain_async.py
File metadata and controls
186 lines (161 loc) · 8.21 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
import asyncio
import logging
import os
from miles.ray.placement_group import (
create_rollout_components,
create_trainer_handles,
create_training_models,
maybe_start_api_server,
update_weights,
)
from miles.ray.rollout.eval_dispatch import EvalDispatcher
from miles.ray.specs.train import compute_trainer_configs
from miles.utils.arguments import validate_async_off_policy_correction
from miles.utils.async_utils import Disposer, eager_create_task, with_disposer
from miles.utils.data import remove_rollout_data_refs, remove_train_output_refs
from miles.utils.ft_utils.mini_ft_controller import maybe_start_mini_ft_controller
from miles.utils.misc import should_run_periodic_action
from miles.utils.orchestration_utils import (
OrchestratorStartupInfo,
init_orchestration_script,
parse_orchestrator_startup_info,
)
logger = logging.getLogger(__name__)
# The framework supports other asynchronous approaches such as fully async (see miles/rollout/fully_async_rollout.py).
async def train(startup_info: OrchestratorStartupInfo, *, disposer: Disposer):
args = startup_info.args
assert not args.colocate or args.fully_async, "Colocation is only supported for async training with --fully-async."
validate_async_off_policy_correction(args)
capability = init_orchestration_script(startup_info, disposer=disposer)
trainer_handles = create_trainer_handles(
args, trainer_configs=compute_trainer_configs(args), capability=capability
)
# create the rollout manager, with sglang engines inside.
# need to initialize rollout manager first to calculate num_rollout
inference_controller, rollout_executor, num_rollout_per_epoch = await create_rollout_components(
args, capability=capability, trainer_handles=trainer_handles
)
disposer.add(inference_controller, rollout_executor)
# create the actor and critic models
actor_model, critic_model = await create_training_models(args, rollout_executor, handles=trainer_handles)
disposer.add(critic_model, actor_model)
disposer.add(
maybe_start_api_server(
args,
trainer_models={"actor": actor_model},
inference_controller=inference_controller,
capability=capability,
)
)
disposer.add(rollout_executor)
maybe_start_mini_ft_controller(args)
# always update weight first so that sglang has the loaded weights from training.
await update_weights(args, actor_model, rollout_executor, inference_controller)
if args.check_weight_update_equal:
await inference_controller.check_weights(
action="compare",
allow_quant_error=args.check_weight_update_allow_quant_error,
selector=args.check_weight_update_selector,
skip_list=args.check_weight_update_skip_list,
)
if args.offload_rollout:
await inference_controller.onload_kv()
eval_dispatcher = EvalDispatcher(args, actor_model, rollout_executor)
disposer.add(eval_dispatcher.drain)
if args.eval_interval is not None and args.start_rollout_id == 0 and not args.skip_eval_before_train:
await inference_controller.prepare_eval()
await eval_dispatcher.dispatch(0, hf_dir=args.hf_checkpoint)
async def save_training_model(model, rollout_id, force_sync):
if args.use_critic and args.offload_train:
await model.onload()
await model.save_model(rollout_id, force_sync=force_sync)
if args.use_critic and args.offload_train:
await model.offload()
async def offload_train():
if args.use_critic:
return
if args.offload_train:
await actor_model.offload()
else:
await actor_model.clear_memory()
async def prepare_and_generate(rollout_id):
await inference_controller.prepare_rollout(rollout_id)
return await rollout_executor.get(rollout_id)
# async train loop.
rollout_data_next_future = await eager_create_task(prepare_and_generate(args.start_rollout_id))
for rollout_id in range(args.start_rollout_id, args.num_rollout):
# Sync the last generation
if rollout_data_next_future is not None:
rollout_data_curr_ref = await rollout_data_next_future
has_next_rollout = rollout_id + 1 < args.num_rollout
weight_update_due = (rollout_id + 1) % args.update_weights_interval == 0
# A fully-async producer keeps generating without a pending get(). When
# weights will change, defer the next drain so it uses the new version.
defer_next_drain = args.fully_async and has_next_rollout and weight_update_due
if args.colocate:
await inference_controller.pause_generation(mode=args.pause_generation_mode)
await inference_controller.flush_cache()
if args.offload_rollout:
await inference_controller.offload_kv()
await inference_controller.offload_weights()
else:
# Start the next rollout early.
if has_next_rollout and not defer_next_drain:
rollout_data_next_future = await eager_create_task(prepare_and_generate(rollout_id + 1))
if args.use_critic:
values = await critic_model.train(rollout_id, rollout_data_curr_ref)
if args.offload_train:
await critic_model.offload()
if rollout_id >= args.num_critic_only_steps:
await actor_model.train(rollout_id, rollout_data_curr_ref, external_data=values)
if args.offload_train:
await actor_model.offload()
remove_train_output_refs(values)
else:
await actor_model.train(rollout_id, rollout_data_curr_ref)
remove_rollout_data_refs(args, rollout_data_curr_ref)
await rollout_executor.release(rollout_id)
external_save = args.save_trigger_sentinel is not None and os.path.exists(args.save_trigger_sentinel)
if external_save or should_run_periodic_action(
rollout_id, args.save_interval, num_rollout_per_epoch, args.num_rollout
):
force_sync = external_save or rollout_id == args.num_rollout - 1
await save_training_model(actor_model, rollout_id, force_sync)
if args.use_critic:
await save_training_model(critic_model, rollout_id, force_sync)
await rollout_executor.save(rollout_id)
if external_save:
os.remove(args.save_trigger_sentinel)
if args.colocate:
await offload_train()
if args.offload_rollout:
await inference_controller.onload_weights()
if weight_update_due:
if not args.fully_async:
# sync generate before update weights to prevent update weight in the middle of generation
rollout_data_curr_ref = (await x) if (x := rollout_data_next_future) is not None else None
rollout_data_next_future = None
await update_weights(args, actor_model, rollout_executor, inference_controller, rollout_id=rollout_id)
if defer_next_drain and not args.colocate:
rollout_data_next_future = await eager_create_task(prepare_and_generate(rollout_id + 1))
if args.colocate:
if args.offload_rollout:
await inference_controller.onload_kv()
await inference_controller.continue_generation()
if rollout_id + 1 < args.num_rollout:
rollout_data_next_future = await eager_create_task(prepare_and_generate(rollout_id + 1))
if should_run_periodic_action(rollout_id, args.eval_interval, num_rollout_per_epoch, args.num_rollout):
await inference_controller.prepare_eval()
await eval_dispatcher.dispatch(rollout_id, force=rollout_id == args.num_rollout - 1)
if (
args.debug_exit_after_rollout is not None
and (rollout_id - args.start_rollout_id + 1) >= args.debug_exit_after_rollout
):
logger.info(
"debug_exit_after_rollout=%d reached at rollout_id=%d, exiting",
args.debug_exit_after_rollout,
rollout_id,
)
break
if __name__ == "__main__":
asyncio.run(with_disposer(train, parse_orchestrator_startup_info()))