Skip to content

Tie operands and results when creating DispatchWorkgroupsOp - #5102

Merged
antiagainst merged 4 commits into
iree-org:mainfrom
antiagainst:tile-operands-result
Mar 15, 2021
Merged

antiagainst merged 4 commits into
iree-org:mainfrom
antiagainst:tile-operands-result

Conversation

@antiagainst

Copy link
Copy Markdown
Member

For each resultant tensor, DispatchWorkgroupsOp now can indicate
to which operand it ties to, in order to share the underlying
buffer storage. This can be used for subtensor_insert ops, where
we insert into the target tensor and also return the target
tensor as the result, which can happen as in-place update on
the buffer down the layers.

Addresses #5042

For each resultant tensor, DispatchWorkgroupsOp now can indicate
to which operand it ties to, in order to share the underlying
buffer storage. This can be used for subtensor_insert ops, where
we insert into the target tensor and also return the target
tensor as the result, which can happen as in-place update on
the buffer down the layers.

@MaheshRavishankar MaheshRavishankar 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.

Wow! just changing that worked and took care of bufferization too? Could you upload the dump of ir after all for this. I am surprised that it worked out of the box.

Otherwise this LGTM!

@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.

awesome - lgtm besides tile->tie. thanks so much for working through this and sorry there were some rough edges!

func @subtensor_insert(%arg0: tensor<1x224x224x3xf32>) -> tensor<1x225x225x3xf32> {
%cst = constant 0.000000e+00 : f32
%0 = linalg.init_tensor [1, 225, 225, 3] : tensor<1x225x225x3xf32>
%1 = linalg.fill(%0, %cst) : tensor<1x225x225x3xf32>, f32 -> tensor<1x225x225x3xf32>

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.

this made me think of something Mahesh and I were chatting about a bit the other day: we should have some identification of DMA operations like fills and copies, as turning those into the corresponding flow ops will be significantly better (especially on GPUs). Maybe it'd be enough to have a canonicalizer on flow.dispatch.workgroups: if the body is linalg.fill -> tensor.store then turn that into a flow.tensor.splat, or if tensor.load -> tensor.store then turn into a flow.tensor.clone/etc.

Do either you or @MaheshRavishankar know if there's an issue tracking something like that, or should I file one?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

+1 on having such patterns to turn standalone fill/copy into corresponding DMA operations. Fill ops are used for initializing the output buffer and normally they will be fused together with the real producer ops and after a bunch of transformations those fill ops will be killed with vector store to load forwarding. Here it's a special case: the padding happened after has a different workload, so we cannot go down that route and have the fill "for free". Having DMA operations is better for such cases!

I don't recall we have such an issue to track; so please feel free to create one! (And I might get to it soon). :)

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.

Filed #5105 to discuss/share examples/track!

@antiagainst

Copy link
Copy Markdown
Member Author

Could you upload the dump of ir after all for this. I am surprised that it worked out of the box.

For the pad_test in iree/test/e2e/xla_ops/pad.mlir, here is the IR dump before this patch, and here is the IR dump after this patch. I think the interesting part is we are seeing the following before bufferization, with this patch:

  func @pad_test_dispatch_1() {
    %c0 = constant 0 : index
    %0 = hal.interface.binding.subspan @legacy_io::@ro0[%c0] : !flow.dispatch.tensor<readonly:2x3xi32>
    %1 = hal.interface.binding.subspan @legacy_io::@rw1[%c0] : !flow.dispatch.tensor<readwrite:3x9xi32>
    %2 = flow.dispatch.tensor.load %0 : !flow.dispatch.tensor<readonly:2x3xi32> -> tensor<2x3xi32>
    %3 = flow.dispatch.tensor.load %1 : !flow.dispatch.tensor<readwrite:3x9xi32> -> tensor<3x9xi32>
    %4 = subtensor_insert %2 into %3[0, 1] [2, 3] [1, 1] : tensor<2x3xi32> into tensor<3x9xi32>
    flow.dispatch.tensor.store %4, %1 : tensor<3x9xi32> -> !flow.dispatch.tensor<readwrite:3x9xi32>
    return
  }

Given that %3 and %4 are mapped to the same buffer, we won't need a new allocation or copy when handling subtensor_insert's result tensor.

@antiagainst antiagainst changed the title Tile operands and results when creating DispatchWorkgroupsOp Tie operands and results when creating DispatchWorkgroupsOp Mar 14, 2021

@MaheshRavishankar MaheshRavishankar 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.

Awesome! works as expected. Still not sure how bufferization picked that up, but it seems to be doing the right thing here.

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.

3 participants