Skip to content

Make dispatch benchmark functions unroll multiple iterations to amortize overhead #3995

Closed

Description

@benvanik

The current benchmark functions are not useful for timing as they involve a full round trip through the synchronous API: this means that for each kernel being executed once we are calling into the VM, allocating buffers (soon to be reduced by some work will still happen), creating and recording a command buffer, submitting that command buffer to the queue, {{adding random variance}}, executing the single kernel on the queue, and then winding all the way back out again. This makes the numbers largely meaningless. They can still be useful for correctness testing (isolating the dispatches and debugging them independently), but to get useful timing we need to increase the work being done relative to the overhead of doing the work.

The benchmark tool has the KeepRunningBatch feature for this:
https://github.com/google/benchmark/blob/7fa6f1f91a96d8763de754810710d457f7fea575/include/benchmark/benchmark.h#L485-L495

This allows us to generate code for dispatching N times (128, 256, 1024, etc) and then telling that to the benchmark tool such that it prints us nice statistics as if we had run them individually.

A pass should then find the dispatches and duplicate them - so for example each dispatch would get repeated into multiple. If the current pass creates the exported functions then a new RepeatDispatchesPass that runs after HAL transformation is easy: walk functions and find each hal.command_buffer.dispatch and just clone it N times (or put it in a for loop) based on a flag indicating the repetition count.

Example:

func @my_exported_dispatch_benchmark_fn() {
  ...
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  ...
}

-> -iree-hal-repeat-dispatches=6 ->

func @my_exported_dispatch_benchmark_fn() {
  ...
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
  ...
}

To keep it simple to start we can just pick a value (128, 256, etc) and use that on both the compiler pass and the runtime KeepRunningBatch call, but ideally what we'd do here is add an attribute to the exported function that we queried at runtime with iree_vm_function_reflection_attr, allowing iree-benchmark-module to remain batch 1 by default (so same as today).

Activity

  1. added
    performance ⚡
    runtime/tools
    on Nov 23, 2020
  2. added this to the milestone on Nov 23, 2020
  3. hanhanW commented on Nov 30, 2020

    @hanhanW
    Contributor

    I think I get the point to do batch mode benchmark. It's like we unroll (or put it in a for loop) like this:

    func @my_exported_dispatch_benchmark_fn() attributes {iree.module.export, iree.reflection = {bt = 128}}} {
      ...
      hal.command_buffer.dispatch.symbol %arg0, @ex::@backend::@entry0, workgroup_xyz = [%0, %1, %2]
      ...
    }
    

    (using 128 times as example)

    In runtime we can extract the number of batch with iree_vm_function_reflection_attr, and benchmark with batch mode. My question would be -- how do we handle 129 times or the rest of times?

    // extract batch_size with iree_vm_function_reflection_attr, in this case, batch_size=128)
    while (state.KeepRunningBatch(batch_size)) {
      // invoke the exported function with batch mode?
    }
    // any batch_size=1 function to call?
    

    I got some options here, but would like to know if you have something in your mind.

    1. Ignore the rest of iterations, only invoke the function when there are more than batch_size runs.
    2. Pass a runtime argument to the module (as function argument?), so we can run both mode.
  4. benvanik commented on Nov 30, 2020

    @benvanik
    CollaboratorAuthor

    Yep!

    If I understand correctly you only run with batch_size - no need to handle leftovers. The benchmark tool will process everything in steps of that batch size.

    The big end-to-end functions won't have the batch size attribute - or a default of 1 - and when that happens the state.KeepRunningBatch(batch_size) == state.KeepRunning(), so they'll work just like they do today. It's only the generated per-dispatch functions that would be running with batch_size>1.

  5. hanhanW commented on Dec 1, 2020

    @hanhanW
    Contributor

    cool, I just want to clarify if we need to handle leftovers. Looks like the benchmark tool will handle it, and it's great. I think it shouldn't be hard to add, I'll put it on my plate. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

performance ⚡runtime/tools

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions