Repository navigation
Description
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
hanhanW commentedon Nov 30, 2020
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:
(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?I got some options here, but would like to know if you have something in your mind.
batch_sizeruns.benvanik commentedon Nov 30, 2020
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.hanhanW commentedon Dec 1, 2020
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. :)
Add support to benchmark in batch mode. (#5012)