Skip to content

Add support to benchmark in batch mode. - #5012

Merged
hanhanW merged 8 commits into
iree-org:mainfrom
hanhanW:repeat
Mar 10, 2021
Merged

hanhanW merged 8 commits into
iree-org:mainfrom
hanhanW:repeat

Conversation

@hanhanW

@hanhanW hanhanW commented Mar 5, 2021 •

Copy link
Copy Markdown
Contributor

If --iree-hal-benchmark-dispatch-repeat-count is set during a translation, all the
hal.command_buffer.dispatch ops will be run
iree-hal-benchmark-dispatch-repeat-count 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.

Fixes #3995

@hanhanW

hanhanW commented Mar 5, 2021

Copy link
Copy Markdown
Contributor Author

I tried iree_vm_function_reflection_attr, but it couldn't work. I added batch_size to iree.reflection in the end of the RepeatDispatchesPass, but could not extract the value. It said that there is no such reflection_attr batch_size.

@benvanik

benvanik commented Mar 5, 2021

Copy link
Copy Markdown
Collaborator

I tried iree_vm_function_reflection_attr, but it couldn't work. I added batch_size to iree.reflection in the end of the RepeatDispatchesPass, but could not extract the value. It said that there is no such reflection_attr batch_size.

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>(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?)?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really awesome, and we definitely want it. I'd love to spend some time on this! Thanks for the detailed explanation.

@hanhanW

hanhanW commented Mar 5, 2021

Copy link
Copy Markdown
Contributor Author

I tried iree_vm_function_reflection_attr, but it couldn't work. I added batch_size to iree.reflection in the end of the RepeatDispatchesPass, but could not extract the value. It said that there is no such reflection_attr batch_size.

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?

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.
https://github.com/google/iree/blob/8baa8e4517cd78360dd69f4ac986e82dc7b420cf/iree/compiler/Dialect/HAL/Transforms/PublicAbiGeneration.cpp#L415-L419

This pass is added after inlining device switch ops, which is later than PublicABIGenerationPass.

Here is the dump: https://gist.github.com/hanhanW/e0e59aac4bddd3eb4213e5a0a334e6c5

@benvanik

benvanik commented Mar 5, 2021

Copy link
Copy Markdown
Collaborator

I tried iree_vm_function_reflection_attr, but it couldn't work. I added batch_size to iree.reflection in the end of the RepeatDispatchesPass, but could not extract the value. It said that there is no such reflection_attr batch_size.

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?

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.
https://github.com/google/iree/blob/8baa8e4517cd78360dd69f4ac986e82dc7b420cf/iree/compiler/Dialect/HAL/Transforms/PublicAbiGeneration.cpp#L415-L419

This pass is added after inlining device switch ops, which is later than PublicABIGenerationPass.

Here is the dump: gist.github.com/hanhanW/e0e59aac4bddd3eb4213e5a0a334e6c5

Ugh yeah that pass is annoying. If you add an iree.abi.stub UnitAttr to an exported function then it'll ignore it, which I think is how I was bypassing it for the tflite side. But looking at your pass order you are running after the ABI generation, so anything you do should still be there.

@benvanik benvanik left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few minor fixes needed but otherwise lgtm!

@benvanik

benvanik commented Mar 9, 2021

Copy link
Copy Markdown
Collaborator

(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!)

hanhanW added 8 commits March 10, 2021 07:45
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.
@hanhanW
hanhanW merged commit a43dd17 into iree-org:main Mar 10, 2021
@hanhanW
hanhanW deleted the repeat branch March 10, 2021 18:44
@antiagainst

Copy link
Copy Markdown
Member

Thanks Hanhan for doing this! :D

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make dispatch benchmark functions unroll multiple iterations to amortize overhead

3 participants