Repository navigation
Add support to benchmark in batch mode. - #5012
Conversation
|
I tried iree_vm_function_reflection_attr, but it couldn't work. I added |
This should work - the python bindings use it to do its work and the tflite bindings use it as well. Do you have a final vm-dialect IR dump that we could look at to ensure it's getting into the file? |
| Value zero = builder.create<ConstantIndexOp>(loc, 0); | ||
| Value one = builder.create<ConstantIndexOp>(loc, 1); | ||
| Value ub = builder.create<ConstantIndexOp>(loc, repeatDispatchNum); | ||
| auto forOp = builder.create<scf::ForOp>( |
There was a problem hiding this comment.
Just unroll here yourself instead of using the for loop at runtime - the runtime loop would only be useful if you were taking the count as an argument to the invocation.
That's a possible improvement: you could define a hal.variable of the count, create a new export function to set it (_set_benchmark_batch_size(index)), and then use that as the range here. Then you have control when executing instead of needing to recompile to change the batch count.
If just doing it statically, though, then avoid the runtime loop.
There was a problem hiding this comment.
The possible improvement sounds good to me. But it is not a trivial change to me.
It looks like I have to create the count hal.variable during the pass, and query the variable (by hal.variable.load?) to set the loop boundary?
I don't know how to set it by adding _set_benchmark_batch_size during runtime. How do I know the variable name or can we make it fixed? If we can make it fixed, how do we write data to the variable (or should it be a buffer?)?
There was a problem hiding this comment.
yep - you'd add the variable, then add the function that just performs a hal.variable.store of the parameter to that variable so literally 2 ops: hal.variable.storage + return:
hal.variable @_dispatch_batch_count mutable : index = 1
func @_set_benchmark_batch_size(%new_size : index) {
hal.variable.store %new_size, @_dispatch_batch_count : index
return
}then you use iree_vm_invoke to call that function with your integer argument. I do things like that in the tflite bindings, for example, as they have some stateful getters/setters:
https://github.com/google/iree/blob/1967f658e318197b3cb4c201870b0ec72ad3b170/bindings/tflite/interpreter.c#L444-L461
iree_vm_value_t value = iree_vm_value_make_i32((int32_t)new_size);
iree_vm_list_set_value(inputs, 0, &value);
status = iree_vm_invoke(context, set_benchmark_batch_size_fn,
/*policy=*/NULL, inputs, /*outputs=*/NULL,
allocator);and wherever you need the value in the program you can just hal.variable.load:
%batch_size = hal.variable.load @_dispatch_batch_count : index
scf.for %iv = %c0 to %batch_size step %c1 {
hal.command_buffer.dispatch ....
}A follow-on for sure, but would remove all the required compile-time batch count stuff here and you could always insert it when benchmarking is enabled - if the benchmark tool never changes the value and it's left at the default of 1 then that's fine.
Leaving this here for future reference of whoever does that improvement :)
There was a problem hiding this comment.
This is really awesome, and we definitely want it. I'd love to spend some time on this! Thanks for the detailed explanation.
I think the attributes is removed in PublicABIGenerationPass. I traced the code, but I couldn't understand what it is doing, or how we handle the reflection attr. This pass is added after inlining device switch ops, which is later than PublicABIGenerationPass. Here is the dump: https://gist.github.com/hanhanW/e0e59aac4bddd3eb4213e5a0a334e6c5 |
Ugh yeah that pass is annoying. If you add an |
benvanik
left a comment
There was a problem hiding this comment.
a few minor fixes needed but otherwise lgtm!
|
(also I should have said it earlier, but this is an awesome improvement and will really help us stress CPU multithreading and GPU % utilization in benchmarks!) |
If `iree-hal-repeat-dispatch-num` is set during a translation, all the hal.command_buffer.dispatch ops will be run `hal.command_buffer.dispatch` times. In this context, you have to pass the same number on `--batch_size` to iree-benchmark-module. This is helpful to amortize overhead when the execution time is small.
|
Thanks Hanhan for doing this! :D |
If
--iree-hal-benchmark-dispatch-repeat-countis set during a translation, all thehal.command_buffer.dispatch ops will be run
iree-hal-benchmark-dispatch-repeat-counttimes. In this context, you have to passthe same number on
--batch_sizeto iree-benchmark-module.This is helpful to amortize overhead when the execution time is small.
Fixes #3995