- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10.9k
Open
Labels
Description
Motivation.
Today, the core loop looks like:
while True:
    scheduler_output = self.scheduler.schedule()
    model_runner_output = self.model_executor.execute_model(scheduler_output)
    engine_core_outputs = self.scheduler.update_from_output(scheduler_output, model_runner_output)
    yield engine_core_outputsWhile simple, this structure doesn't allow us to utilize CPU cycles while the GPU is running the model in execute_model.
Proposed Change.
The proposal is to carve out the sampling stage from the execute_model method. This way, execute_model will be a non-blocking call that returns without no GPU->CPU synchronization at the end. That is:
while True:
    scheduler_output = self.scheduler.schedule()
    # Prepare inputs and execute the model until the last hidden states. This is non-blocking.
    self.model_executor.execute_model(scheduler_output)
    # If structured outputs is used, produce the bitmask here. Otherwise, bitmask is None.
    bitmask = self.scheduler.get_grammar_bitmask(scheduler_output)
    # Prepare sampling metadata and sample the next token ids. This is blocking.
    model_runner_output = self.model_executor.sample(bitmask)
    engine_core_outputs = self.scheduler.update_from_output(scheduler_output, model_runner_output)
    yield engine_core_outputsThis gives two performance benefits:
- (On the worker side) We can overlap the construction of sampling metadata (and logits processor) with execute_model.
- (On the scheduler side) We can overlap the construction of bitmask with execute_model.
For async scheduling, the core loop will look like
# Initial step
scheduler_output = self.scheduler.schedule()
self.model_executor.prepare_inputs(scheduler_output)
self.model_executor.execute_model()
bitmask = self.scheduler.get_grammar_bitmask(scheduler_output)
prev_scheduler_output = scheduler_output
while True:
    scheduler_output = self.scheduler.schedule()
    self.model_executor.prepare_inputs(scheduler_output)
    model_output = self.model_executor.sample(bitmask)
    self.model_executor.execute_model()
    bitmask = self.scheduler.get_grammar_bitmask(scheduler_output)
    engine_core_outputs = self.scheduler.update_from_output(prev_scheduler_output, model_output)
    prev_scheduler_output = scheduler_outputFeedback Period.
No response
CC List.
No response
Any Other Things.
No response
Before submitting a new issue...
- Make sure you already searched for relevant issues, and asked the chatbot living at the bottom right corner of the documentation page, which can answer lots of frequently asked questions.
njhill, noooop, yangelaboy, Isotr0py, mgoin and 5 more