Skip to content

Adding -iree-hal-materialize-interfaces2 to finally use hal.interface (mostly) as it was intended - #5328

Merged
benvanik merged 3 commits into
mainfrom
benvanik-constant-io
Apr 8, 2021
Merged

benvanik merged 3 commits into
mainfrom
benvanik-constant-io

Conversation

@benvanik

@benvanik benvanik commented Apr 7, 2021 •

Copy link
Copy Markdown
Collaborator

hal.interface was intended to allow us to combine dispatch I/O into a smaller set of bindings but due to limitations of things like iree.placeholder on the old linalg-on-buffers path the @legacy_io style was used that kept the bindings 1:1 with the I/O. With the linalg-on-tensors path using the bindings properly it's now possible to start breaking that restriction. This change - the first of a chain of many - finds usage of constant buffers as inputs and tries to combine them into single bindings. Future changes as part of buffer allocation will do the same for transient buffers.

There's currently an entire missing layer of IR ops in the HAL that allow this to be done in phases. Ideally we should be performing an analysis of dispatch usage, the materialization of the interfaces based on that usage, and the application of those interfaces to the use-sites independently. Until the ops are added, though, this cuts through that by doing the analysis at the same time as the interfaces are built and tags the use-sites with the mappings required during subsequent conversion. As buffer allocation gets added I'll start peeling those out and rearranging the passes so that we have a much clearer order:

  1. identify/group constants into pools
  2. convert flow streams to (tbd) hal streams
  3. perform hal stream dispatch analysis
  4. perform buffer reservation
  5. run analysis on dispatch usage with complete constant/transient allocation information
  6. materialize structural interfaces
  7. canonicalize interfaces and assign binding/ordinals
  8. convert hal streams to command buffers

Only step 1 and 6 exist today, which is why there are so many layering skips here :)

An example with debug printing can be seen here:
https://gist.githubusercontent.com/benvanik/41407e8436b3e3fed3fd336b08d169fa/raw/881b7a66f9cbf43fff7e38c5c716aeae9b90322a/bert_materialize_interfaces_2.mlir
This demonstrates the analysis performed, the combining of constants/detection of strides, creation of the newly reduced binding sets on interfaces, and attribution of bindings to the use-sites. dispatch_45 is a good example showing a heavily-reused dispatch from 20 something use-sites coalescing a 20 something unique-per-site constant reads down to a single binding:
https://gist.github.com/benvanik/41407e8436b3e3fed3fd336b08d169fa#file-bert_materialize_interfaces_2-mlir-L7684-L8489
Once transient buffers are included here it'll drop from 24 total bindings down to 2 (constant in, transient in/out). https://gist.github.com/benvanik/41407e8436b3e3fed3fd336b08d169fa#file-bert_materialize_interfaces_2-mlir-L15551-L16109 shows 104 bindings down to 28 (of which can also go to 2 when transient buffers are used).

@benvanik benvanik added the compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) label Apr 7, 2021
@google-cla google-cla Bot added the cla: yes label Apr 7, 2021
@benvanik
benvanik force-pushed the benvanik-constant-io branch from 2c2c43a to ae5a9ea Compare April 7, 2021 16:55
@benvanik
benvanik force-pushed the benvanik-constant-io branch from ae5a9ea to 4ad4e09 Compare April 7, 2021 20:12
@benvanik

benvanik commented Apr 7, 2021

Copy link
Copy Markdown
Collaborator Author

@MaheshRavishankar / @ScottTodd: I split out refactoring into the first two commits if you want to focus on the last one that adds all the logic. All of this is only enabled for the linalg-on-tensors path (when flow.dispatch.workgroups is used) and any case of constant usage outside the one handled here will fall back to the existing behavior of getting its own binding, so this should (hopefully) only be an optimistic optimization. The examples linked in the description will show what it's all doing, which is largely a simple scan + bucketing and a whole ton of goo for adding hal.binding attrs that then are picked up for when flow.dispatch is turned into hal.command_buffer.dispatch. That'll get better when there's a new hal.stream.dispatch that carries this information instead but that's still a few weeks away.

@benvanik
benvanik marked this pull request as ready for review April 7, 2021 20:34
SymbolRefAttr constantBuffer;
SmallVector<Attribute> constantRanges;

void dump() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This debug output is great for inspecting this pass, but it's also very verbose (judging by your examples for complex models). Is there guidance from LLVM/MLIR on how to use llvm:dbgs() like this? Should this be further flag guarded, or is the idea that people turning on debug logging would be targeted with which passes they are running?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I debated about that with myself as well. I settled on leaving them in as when we started using linalg our default -debug output became useless as it spams a tremendous amount and -debug-only= is the only real way to get usable results - in which case, the logs in this file only show with -debug-only=iree-hal-materialize-interfaces. There's not a way (that I could find) to only enable these if and only if -debug-only= is set - LLVM_DEBUG will either always log if -debug is passed or only log if -debug-only= is passed with the right name.

Since this is one of those areas that I expect compilation errors to happen in I'm going to leave them in for now so that it's easy to get reports from people (no need to go change code, just pass -debug-only=iree-hal-materialize-interfaces and pipe to a file).

benvanik added 2 commits April 8, 2021 10:08
This dramatically reduces the stream captures (from 1000 to 3 in BERT)
and enables subsequent passes to know when a dispatch is using a constant.
This will insulate the old code paths from coming interface changes that
are not compatible with them.
@benvanik
benvanik force-pushed the benvanik-constant-io branch from 4ad4e09 to b98dca8 Compare April 8, 2021 17:26
@benvanik
benvanik force-pushed the benvanik-constant-io branch from b98dca8 to 4f86152 Compare April 8, 2021 17:38
@benvanik
benvanik requested a review from ScottTodd April 8, 2021 17:41
@MaheshRavishankar
MaheshRavishankar removed their request for review April 8, 2021 17:43
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Thanks @benvanik . I dont have enough context here to review this, but I am waiting for this to land. I think this is one of the last blockers remaining on the linalg on tensors path.

@benvanik

benvanik commented Apr 8, 2021

Copy link
Copy Markdown
Collaborator Author

Thanks @benvanik . I dont have enough context here to review this, but I am waiting for this to land. I think this is one of the last blockers remaining on the linalg on tensors path.

Hopefully I can land soon - I haven't exhaustively tested anything not currently enabled already beyond BERT, so if you have some other things you know that are blocked behind it you could give them a shot and see if any follow-up tuning is required (there's a few additional cases I could handle in here but was deferring them until we had usage).

@ScottTodd

Copy link
Copy Markdown
Member

jury-duty-cat

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Thanks @benvanik . I dont have enough context here to review this, but I am waiting for this to land. I think this is one of the last blockers remaining on the linalg on tensors path.

Hopefully I can land soon - I haven't exhaustively tested anything not currently enabled already beyond BERT, so if you have some other things you know that are blocked behind it you could give them a shot and see if any follow-up tuning is required (there's a few additional cases I could handle in here but was deferring them until we had usage).

I think in OSS, Bert, Resnet and MobileNet are blocked by this. If those pass, then we might have more examples from intergration tests internally.

@benvanik
benvanik merged commit 56307e1 into main Apr 8, 2021
@benvanik
benvanik deleted the benvanik-constant-io branch April 8, 2021 18:44
@NatashaKnk NatashaKnk mentioned this pull request Apr 9, 2021
@rsuderman rsuderman mentioned this pull request Apr 12, 2021
copybara-service Bot pushed a commit that referenced this pull request Apr 13, 2021
* 0132538 Merge google -> main #5404
* 53e7279 Limit fusion of linalg.generic/indexed_generic ops to avoid redundant computat..
* 516d1ac Merge branch 'main' into google-to-main
* 9630450 Make PrePartitioningConversionPass not recurse into Linalg (#5358)
* 29610ce Merge google -> main (#5388)
* 692c586 Merge branch 'main' into google-to-main
* 9b159c1 [CUDA Codegen] Add support for promotion to workgroup memory. (#5386)
* 11b3bfe MaterializeCPULaunch configurations before bufferization (#5362)
* ee0ddf4 Legalize ui64 to i32 in Flow type converter (#5384)
* 87346a2 Handle tensor.cast bufferization in LinalgBufferizePass (#5369)
* 2922e11 Removes tied transient buffer assignment propagation. (#5365)
* 5b952e4 Making flow.tensor.reshape a tied-op. (#5368)
* 7048463 Fixing ASAN-instrumented dylib library loading. (#5382)
* 2f3ad70 Adding some basic flow.tensor.reshape folders. (#5366)
* 357fa58 Switch simple_embeddding to test on DyLib + Vulkan (#5374)
* cb7d144 Turn on operand fusion flag for benchmarks of Linalg on tensors (#5380)
* f2d74fd Disable fusion of named ops with elementwise operations by default. (#5363)
* 3fc84c3 Add support for returning Python tuples (#5347)
* 8fb65cb Remove handling of shape dialect in flow->hal stream conversion. (#5360)
* 68c27a0 Add logging for Android (#5232)
* 56307e1 Merge pull request #5328 from google/benvanik-constant-io
* d3eb8e4 Merge pull request #5357 from google/benvanik-host-visible
* 4f86152 Materialize interfaces based on usage for flow.dispatch.workgroups.
* f41b25d Splitting -iree-hal-materialize-interfaces into old/new paths. This will insul..
* 62a3dfd Move constant pool ops/hal.constant.subspan into streams. This dramatically re..
* 79dc138 [spirv] NFC: use upstream push constant utility functions (#5352)
* 38daefb Revert "Disable running BERT on Vulkan (see Issue #5268) (#5269)"
* 0995956 Revert "[vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332)"
* 0eeb3f8 Removing hack that forced all Vulkan buffers to be host-visible. After removin..
* 8abc33b Tracy fixes for GPU name allocation and allocation callstack mode. (#5356)
* 55f76d3 Fix submodule check failure (#5355)
* 830b547 [CodeGen] Copy constant at the proper place during bufferization (#5343)
* 59ad168 Drop absl flat_hash_set and some unused includes. (#5346)
* de2aabc Control TF MLIR print options via flags (#5336)
* 2e2dfdd Merge google -> main (#5345)
* ef9b787 Merge branch 'main' into google-to-main
* 8b70733 Remove extra comma from context_wrapper.cc
* 8cc6256 Fix linalg-on-tensors smoketests (#5341)
* a578d6a Add TosaToStandard to cpu/gpu target pipeline from ApplyScale lowering (#5288)
* 0c92b88 Add mixed static-dynamic arguments interface for flow.dispatch.tensor.{load|st..
* 9890722 Update Java Bindings To Build In Experimental (#5226)
* 734ec24 Add smoketest_linalg_on_tensors to LLVM and Vulkan-SPIRV targets. (#5324)
* d8bcb78 Drop absl logging and flat_hash_map. (#5334)
* 5c63668 [vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332)
* 55d3715 Fix vulkan_device struct manipulation issue with std::vector. (#5333)
* 47d00c6 Fix benchmarking MobileNetV2 on S20 GPU in Linalg on tensor path. (#5331)
* ccae793 Fix bugs in ExtractReduceWindowOpPaddingAttributes. (#5329)
* 652e328 Don't inline block arguments that can be written (#5247)
* 86e7d29 [CodeGen] Enable fusion with consumer elementwise ops (#5282)
* 44ad774 Integrate MLIR-EmitC at iml130/mlir-emitc@3c265bf (#5326)
* 0141857 Added pad test, update LegalizeInputTypes to allow linalg.yield (#5305)
* 5a3653f Add mobile-bert f16 version to Mako benchmark. (#5204)
* f596cdb Add a target_env_conversion test for Linalg on tensors. (#5309)
* f1aa6f4 Print out the status when iree-check-module fails (#5323)
* 40d7589 Check for VMLA and Vulkan components for simple_embedding_test. (#5318)

COPYBARA_INTEGRATE_REVIEW=#5407 from rsuderman:main-to-google c9c3ea7
PiperOrigin-RevId: 368121520
iree-github-actions-bot pushed a commit that referenced this pull request Apr 13, 2021
* 0132538 Merge google -> main #5404
* 53e7279 Limit fusion of linalg.generic/indexed_generic ops to avoid redundant computat..
* 516d1ac Merge branch 'main' into google-to-main
* 9630450 Make PrePartitioningConversionPass not recurse into Linalg (#5358)
* 29610ce Merge google -> main (#5388)
* 692c586 Merge branch 'main' into google-to-main
* 9b159c1 [CUDA Codegen] Add support for promotion to workgroup memory. (#5386)
* 11b3bfe MaterializeCPULaunch configurations before bufferization (#5362)
* ee0ddf4 Legalize ui64 to i32 in Flow type converter (#5384)
* 87346a2 Handle tensor.cast bufferization in LinalgBufferizePass (#5369)
* 2922e11 Removes tied transient buffer assignment propagation. (#5365)
* 5b952e4 Making flow.tensor.reshape a tied-op. (#5368)
* 7048463 Fixing ASAN-instrumented dylib library loading. (#5382)
* 2f3ad70 Adding some basic flow.tensor.reshape folders. (#5366)
* 357fa58 Switch simple_embeddding to test on DyLib + Vulkan (#5374)
* cb7d144 Turn on operand fusion flag for benchmarks of Linalg on tensors (#5380)
* f2d74fd Disable fusion of named ops with elementwise operations by default. (#5363)
* 3fc84c3 Add support for returning Python tuples (#5347)
* 8fb65cb Remove handling of shape dialect in flow->hal stream conversion. (#5360)
* 68c27a0 Add logging for Android (#5232)
* 56307e1 Merge pull request #5328 from google/benvanik-constant-io
* d3eb8e4 Merge pull request #5357 from google/benvanik-host-visible
* 4f86152 Materialize interfaces based on usage for flow.dispatch.workgroups.
* f41b25d Splitting -iree-hal-materialize-interfaces into old/new paths. This will insul..
* 62a3dfd Move constant pool ops/hal.constant.subspan into streams. This dramatically re..
* 79dc138 [spirv] NFC: use upstream push constant utility functions (#5352)
* 38daefb Revert "Disable running BERT on Vulkan (see Issue #5268) (#5269)"
* 0995956 Revert "[vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332)"
* 0eeb3f8 Removing hack that forced all Vulkan buffers to be host-visible. After removin..
* 8abc33b Tracy fixes for GPU name allocation and allocation callstack mode. (#5356)
* 55f76d3 Fix submodule check failure (#5355)
* 830b547 [CodeGen] Copy constant at the proper place during bufferization (#5343)
* 59ad168 Drop absl flat_hash_set and some unused includes. (#5346)
* de2aabc Control TF MLIR print options via flags (#5336)
* 2e2dfdd Merge google -> main (#5345)
* ef9b787 Merge branch 'main' into google-to-main
* 8b70733 Remove extra comma from context_wrapper.cc
* 8cc6256 Fix linalg-on-tensors smoketests (#5341)
* a578d6a Add TosaToStandard to cpu/gpu target pipeline from ApplyScale lowering (#5288)
* 0c92b88 Add mixed static-dynamic arguments interface for flow.dispatch.tensor.{load|st..
* 9890722 Update Java Bindings To Build In Experimental (#5226)
* 734ec24 Add smoketest_linalg_on_tensors to LLVM and Vulkan-SPIRV targets. (#5324)
* d8bcb78 Drop absl logging and flat_hash_map. (#5334)
* 5c63668 [vulkan] Disable ResNet50 test failing on NVIDIA GPUs (#5332)
* 55d3715 Fix vulkan_device struct manipulation issue with std::vector. (#5333)
* 47d00c6 Fix benchmarking MobileNetV2 on S20 GPU in Linalg on tensor path. (#5331)
* ccae793 Fix bugs in ExtractReduceWindowOpPaddingAttributes. (#5329)
* 652e328 Don't inline block arguments that can be written (#5247)
* 86e7d29 [CodeGen] Enable fusion with consumer elementwise ops (#5282)
* 44ad774 Integrate MLIR-EmitC at iml130/mlir-emitc@3c265bf (#5326)
* 0141857 Added pad test, update LegalizeInputTypes to allow linalg.yield (#5305)
* 5a3653f Add mobile-bert f16 version to Mako benchmark. (#5204)
* f596cdb Add a target_env_conversion test for Linalg on tensors. (#5309)
* f1aa6f4 Print out the status when iree-check-module fails (#5323)
* 40d7589 Check for VMLA and Vulkan components for simple_embedding_test. (#5318)

PiperOrigin-RevId: 368121520
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants