Can setup start the trainer warm-up without a callback class?

NVIDIA-NeMo/RL #5001. All permalinks pin the PR head 5223071.

Before
No warm-up. Step 1 pays for compiling and tuning the trainer's kernels.
This PR
Adds an opt-in warm-up: one fake step on the trainer while vLLM loads or the first rollouts run. Setup starts it with an 80-line class, _EarlyTrainerWarmup, hung on the trainer build as a done-callback. Whether to warm up is decided twice, in setup and in the controller.
Status
Proposed — not implemented. Review comment C8 asks for a plain wait in setup and a single decision handed to the controller. The warm-up starts at the same moment in every case, so the speedup is the same.

Three pages on this PR. Page 1 explains all the switches; this one is only about how the warm-up (switch â‘£) is started.

Background. The warm-up can start in two places. If the trainer finishes building before vLLM has loaded, setup starts it, so it runs while vLLM loads. Otherwise the controller starts it beside the first rollouts, and step 1 waits for it. C8 changes only code that runs after the dataset has loaded. Switch â‘  still submits the trainer build before the vLLM reserve, exactly as in the PR.

What C8 touches, switch by switch

Switch (page 1)Changed by C8?Same speedup?
â‘  Build the trainer before reserving vLLMNoYes
â‘¡ Scan the dataset's Gym routes when the dataset loadsNoYes
â‘¢ Keep Triton autotune results and torch.compile shape profiles between jobsNoYes
â‘£ Warm-up, setup start (while vLLM loads)Yes: the class becomes a wait in setupYes: same moment, below
â‘£ Warm-up, controller start (beside the first rollouts)Yes: it reads setup's decision instead of checking againYes: same moment
The PR and C8: same early trainer build, same warm-up start, in all three orders ① early build (C8 leaves it alone) trainer thrown away ④ warm-up other work order only, not timings A · trainer ready before vLLM setup thread reserve dataset load dataset vLLM load weights, start servers trainer ① build trainer, before reserve trainer GPUs ④ warm-up (setup) Warm-up starts when the trainer is ready, while vLLM still loads. Setup then waits for both. B · vLLM ready before the trainer setup thread reserve dataset load dataset vLLM load weights, start servers first rollouts trainer ① build trainer, before reserve trainer GPUs ④ warm-up (controller) Setup skips it. The controller runs it beside the first rollouts; step 1 waits for it. C · dataset caps max_num_steps setup thread reserve dataset load dataset vLLM load weights, start servers trainer ① 1st build rebuild for the capped steps trainer GPUs ④ warm-up (setup) The first trainer is thrown away. Only the rebuilt one is warmed up, in both versions.

The code, as pseudocode

PR head: setup thread

# setup thread, setup_single_controller (PR head)
...dataset load, and the trainer rebuild if the dataset caps max_num_steps...
early_warmup = _EarlyTrainerWarmup.maybe_create(cfg, loss_fn,   # None if warm-up is off or skipped;
    generation_future=submitted['generation'])                  # the skip reason is not printed
if early_warmup:
    submitted['trainer'].add_done_callback(early_warmup.start)  # start() runs later, on another thread
generation = submitted['generation'].result()
trainer    = submitted['trainer'].result()
if early_warmup:
    trainer_warmup_ran = early_warmup.finish()                  # blocks until the warm-up is done

PR head: the class it calls

# runs on whichever thread finishes the trainer build
class _EarlyTrainerWarmup:
    def start(self, trainer_future):                              # the done-callback
        try:
            if gen_future is None or not gen_future.done():       # vLLM still loading?
                self.refs = trainer.start_warmup_first_pass(...)  # non-blocking: Ray futures
        except Exception as e:
            self.error = e                                        # a callback's errors are only logged
        finally:
            self.started.set()                                    # threading.Event

# setup thread
    def finish(self):
        self.started.wait()                                       # result() can return before start() ran
        if self.error: raise self.error                           # re-raise on the setup thread
        if self.refs is None: return False                        # vLLM won; the controller does it
        ray.get(self.refs)
        return True

Real code: _EarlyTrainerWarmup, callback attached, finish called.

Three of the class's parts exist only because the start is a callback: the stored error (a callback's exceptions are only logged), the threading.Event (result() can return before the callback has run), and the class itself, which carries state between the two threads. Two things are also written twice: the skip check (setup, silent; controller, printed) and the step's field list (controller; trainer_warmup_kwargs). Both helpers live in the 5,275-line single_controller.py, and setup imports them back inside a function to avoid an import loop.

The fix, as pseudocode (proposed, not run): setup

Where the fix lands: RL only, three files. setup.py loses the class and decides once; single_controller.py reads that decision; advantage_stage.py holds the one field list. No Megatron worker change: its own skip check covers model settings the driver cannot see, so it stays.

# setup thread, setup_single_controller (proposed, not run)
...dataset load, and the trainer rebuild if the dataset caps max_num_steps...
early_warmup = _EarlyTrainerWarmup.maybe_create(cfg, loss_fn, ...)
if early_warmup:
    submitted['trainer'].add_done_callback(early_warmup.start)
warmup_kwargs = _trainer_warmup_kwargs(cfg, stage_cfg, step)       # the only skip check; prints why
gen_f, trainer_f = submitted['generation'], submitted['trainer']
refs = None
if warmup_kwargs is not None:
    wait([gen_f, trainer_f], return_when=FIRST_COMPLETED)          # sleep until one build is done
    if trainer_f.done() and not gen_f.done():                      # same test start() makes
        refs = trainer_f.result()[0].start_warmup_first_pass(...)  # non-blocking
generation = gen_f.result()                                        # vLLM keeps loading; warm-up runs
trainer    = trainer_f.result()                                    # build errors raise here, as before
if early_warmup:
    trainer_warmup_ran = early_warmup.finish()
if refs is not None:
    ray.get(refs)                                                  # warm-up errors raise here, normally
    warmup_kwargs = None                                           # done: nothing left for the controller
class _EarlyTrainerWarmup: ...                                     # whole class, ~80 lines, removed
SingleControllerActorArgs(...,
    trainer_warmup_ran=trainer_warmup_ran)
    trainer_warmup_kwargs=warmup_kwargs)                           # None = off, skipped, or already run

The fix: helpers

# setup.py, new private helper (moved out of single_controller.py)
def _trainer_warmup_kwargs(cfg, stage_cfg, step):
    if not cfg.async_rl.trainer_warmup.enabled: return None         # C2's config key
    if reason := trainer_warmup_skip_reason(cfg, stage_cfg, step):  # moved here, the one caller
        print('trainer warm-up skipped:', reason); return None
    return {'train_fields': stage_cfg.train_fields(cfg),            # shared with the controller
            'run_logprobs': stage_cfg.policy_logprobs_required}

# advantage_stage.py: one place for the step's fields
class AdvantageStageConfig:
    def train_fields(self, cfg):
        return _train_fields_for_step(...)                          # the expression both copies use today

Real calls it uses: start_warmup_first_pass (returns Ray futures, does not block), trainer_warmup_skip_reason. wait and FIRST_COMPLETED are from Python's concurrent.futures; they work across the two thread pools.

The fix: controller

# controller actor, event loop (proposed, not run)
self._train_fields = _train_fields_for_step(...)                        # copy #1 of the expression
self._train_fields = stage_cfg.train_fields(cfg)
_maybe_start_trainer_warmup():                                          # called before the pumps start
    if warm-up off or trainer_warmup_ran: return
    if reason := skip_reason(cfg): print(reason); return                # skip check #2
    kwargs = self._trainer_warmup_kwargs                                # setup's decision
    if kwargs is None: return
    task = create_task(to_thread(trainer.warmup_first_pass, **kwargs))  # beside the first rollouts
# train pump, before step 1 (unchanged)
    await _finish_trainer_warmup()                                      # step 1 waits for it

Real code: _maybe_start_trainer_warmup, called, awaited before step 1.

Why it is the same speed

🟡 review comment C8 — setup.py:1125

So what. The same speedup, with no callback, no Event, no stored error and no class. Whether to warm up is decided once and the reason is always printed; the field list is written once. Both start places stay, because each covers a different order. Nothing here has been run; the PR still has no timings for any switch (page 1).