Skip to content

Split iree-dispatch-linalg-on-tensors-pass into two - #11313

Merged
grypp merged 5 commits into
iree-org:mainfrom
grypp:split_dispatch_workgroup
Dec 7, 2022
Merged

grypp merged 5 commits into
iree-org:mainfrom
grypp:split_dispatch_workgroup

Conversation

@grypp

@grypp grypp commented Nov 28, 2022 •

Copy link
Copy Markdown
Contributor

iree-flow-dispatch-linalg-on-tensors-pass has become a large Pass: 1) generates flow.dispatch.region, 2) forms flow.dispatch.workgroups from these generated ones, 3) transforms Linalg Ops into flow Ops. This PR splits this pass into first step and further steps. It introduces two Passes to do this.

iree-flow-form-dispatch-regions-pass : Runs heuristic to fuse Linalg Ops using tensors and generates flow.dispatch.region from them. It also generates workload information for each dispatch.region.

iree-flow-form-workgroups-regions-pass : It traverses over flow.dispatch.region and generates flow.dispatch.workgroups. It also performs the remaining steps of iree-flow-dispatch-linalg-on-tensors-pass.

@grypp

grypp commented Nov 28, 2022

Copy link
Copy Markdown
Contributor Author

In the 2nd phase (iree-flow-form-workgroups-regions-pass), workload generation was a bit tricky. Right now, I first generates workload inside the flow.dispatch.region Op. After that, I move all of the Ops outside of this region. If we don't like this solution, let's discuss alternatives.

One alternative could be to generate workloads in the first pass (iree-flow-form-dispatch-regions-pass). In this case, I should use these variables, otherwise, the optimizer removes them. So I can extend dispatch.region op with workload information. I am not sure whether this is any better.

@benvanik

Copy link
Copy Markdown
Collaborator

I think you're discovering why flow.dispatch ops require workloads: it's too brittle to try to split that up when forming them :)
I'd recommend adding the (optional) workload information to the region ops but leave the decision on when that gets populated to @MaheshRavishankar as he's likely to be the one who has to maintain the code and triage potential issues (classic ones we've seen with dominance/data dependencies that prevent hoisting, scoping preventing things like calling functions to compute a value that themselves may have side-effecting ops, etc etc). Basically, the workload was added to avoid that class of bugs and I'd hate to see us bring those bugs back.

@benvanik

Copy link
Copy Markdown
Collaborator

(it's also important that (nearly) any IR is able to be round-tripped and that programs can come in with flow IR that earlier stages may have produced - so anything that requires certain passes run in certain orders or that things like the canonicalizer aren't allowed on certain stages of IR is a no go)

@grypp

grypp commented Nov 28, 2022

Copy link
Copy Markdown
Contributor Author

Mahesh and I discussed about splitting the pass into two, this PR is my first attempt. Great feedback, thanks, I think we can discuss from here how we want to pursue.

I can add optional workload to 'dispatch.region'. I can find a reason why we need workload and 'dispatch.workgroups' in general. We run the workload region on host and we might run the other region on different device like gpu. But I don't understand why we need 'dispatch.region' 🤔

@benvanik

Copy link
Copy Markdown
Collaborator

The region op was added as an un-distributed form of the workgroups op - the idea being that going from flow.dispatch.region -> flow.dispatch.workgroups could be done by a pass that focused only on distribution. If that's not useful, though, then flow.dispatch.workgroups can be formed directly without ever using flow.dispatch.region. flow.dispatch.region is easier to work with for non-distributed stuff - for example if we just wanted to move some scalar computations on to the device (and we do: #6948 (comment)) then throwing them in a region is more concise than adding all the slicing stuff for workgroups even if they become a 1x1x1 workgroup later on.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

I have been thinking about how to reduce the handshake between the workgroup count region created at Flow and its lowering in codegen. Right now it is workable, but a bit wonky... Without going into details (cause doing this correctly will require moving the first level tile and distribute in Codegen to use scf.foreach_thread, which I plan to take up in January), at that time the region in flow.dispatch.group will probably not be needed. But for now, adding a region in flow.dispatch.region makes sense.. When I get to fixing this up, Ill address this as needed.

@grypp
grypp force-pushed the split_dispatch_workgroup branch from a1ca0aa to 78fe041 Compare December 2, 2022 08:33
@grypp

grypp commented Dec 2, 2022

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback to everyone. I addressed your comments. Here are the changes:

  • Extended flow.dispatch.region with workload argument and workload_count region.
  • The 1st pass (iree-flow-form-dispatch-regions-pass) calculates workloads and put it into workload_count region.
  • The 2nd pass (iree-flow-form-workgroups-regions-pass) receives flow.dispatch.region Ops whose workload is already calculated. It uses rewriteFlowDispatchRegionToFlowDispatchWorkgroups to lower flow.dispatch.region into flow.dispatch.workgroups.

Let's take this example:

%zero = arith.constant 0.0 : f32
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%M = tensor.dim %A, %c0 : tensor<?x?xf32>
%N = tensor.dim %B, %c1 : tensor<?x?xf32>
%0 = tensor.empty(%M, %N) : tensor<?x?xf32>
%1 = linalg.fill ins(%zero : f32) outs(%0 : tensor<?x?xf32>) -> tensor<?x?xf32>
%2 = linalg.matmul ins(%A, %B : tensor<?x?xf32>, tensor<?x?xf32>) outs(%1 : tensor<?x?xf32>) -> tensor<?x?xf32>

This is after 1st pass:

%c1 = arith.constant 1 : index
%cst = arith.constant 0.000000e+00 : f32
%c0 = arith.constant 0 : index
%dim = tensor.dim %arg1, %c1 : tensor<?x?xf32>
%dim_0 = tensor.dim %arg0, %c0 : tensor<?x?xf32>
%0 = tensor.empty(%dim_0, %dim) : tensor<?x?xf32>
%1 = flow.dispatch.region {operand_segment_sizes = array<i32: 2, 3>}[%dim_0, %dim, %c1] -> (tensor<?x?xf32>{%dim_0, %dim}) {
  %2 = linalg.fill ins(%cst : f32) outs(%0 : tensor<?x?xf32>) -> tensor<?x?xf32>
  %3 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%2 : tensor<?x?xf32>) -> tensor<?x?xf32>
  flow.return %3 : tensor<?x?xf32>
} count(%arg2: index, %arg3: index, %arg4: index) -> (index, index, index) {
  %x, %y, %z = flow.dispatch.workgroup_count_from_dag_root %arg2, %arg3, %arg4
  flow.return %x, %y, %z : index, index, index
}

This is after 2nd pass:

%c1 = arith.constant 1 : index
%c0 = arith.constant 0 : index
%dim = tensor.dim %arg1, %c1 : tensor<?x?xf32>
%dim_0 = tensor.dim %arg0, %c0 : tensor<?x?xf32>
%dim_1 = tensor.dim %arg0, %c1 : tensor<?x?xf32>
%dim_2 = tensor.dim %arg1, %c0 : tensor<?x?xf32>
%0 = flow.dispatch.workgroups[%dim_0, %dim, %c1](%arg0, %arg1, %dim_0, %dim_1, %dim_2, %dim) : (tensor<?x?xf32>{%dim_0, %dim_1}, tensor<?x?xf32>{%dim_2, %dim}, index, index, index, index) -> tensor<?x?xf32>{%dim_0, %dim} =
    (%arg2: !flow.dispatch.tensor<readonly:tensor<?x?xf32>>, %arg3: !flow.dispatch.tensor<readonly:tensor<?x?xf32>>, %arg4: index, %arg5: index, %arg6: index, %arg7: index, %arg8: !flow.dispatch.tensor<writeonly:tensor<?x?xf32>>) {
  %cst = arith.constant 0.000000e+00 : f32
  %1 = flow.dispatch.tensor.load %arg2, offsets = [0, 0], sizes = [%arg4, %arg5], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<?x?xf32>>{%arg4, %arg5} -> tensor<?x?xf32>
  %2 = flow.dispatch.tensor.load %arg3, offsets = [0, 0], sizes = [%arg6, %arg7], strides = [1, 1] : !flow.dispatch.tensor<readonly:tensor<?x?xf32>>{%arg6, %arg7} -> tensor<?x?xf32>
  %3 = tensor.empty(%arg4, %arg7) : tensor<?x?xf32>
  %4 = linalg.fill ins(%cst : f32) outs(%3 : tensor<?x?xf32>) -> tensor<?x?xf32>
  %5 = linalg.matmul ins(%1, %2 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%4 : tensor<?x?xf32>) -> tensor<?x?xf32>
  flow.dispatch.tensor.store %5, %arg8, offsets = [0, 0], sizes = [%arg4, %arg7], strides = [1, 1] : tensor<?x?xf32> -> !flow.dispatch.tensor<writeonly:tensor<?x?xf32>>{%arg4, %arg7}
  flow.return
} count(%arg2: index, %arg3: index, %arg4: index) -> (index, index, index) {
  %x, %y, %z = flow.dispatch.workgroup_count_from_dag_root %arg2, %arg3, %arg4
  flow.return %x, %y, %z : index, index, index
}

@grypp
grypp force-pushed the split_dispatch_workgroup branch from 78fe041 to 58ff262 Compare December 2, 2022 08:49
@grypp grypp closed this Dec 2, 2022
@grypp
grypp force-pushed the split_dispatch_workgroup branch from 58ff262 to a32ed6a Compare December 2, 2022 09:07
@grypp grypp reopened this Dec 2, 2022

@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! Mostly looks ready to go to me. Have a few follow up comments. Thanks for taking this on!

@grypp
grypp force-pushed the split_dispatch_workgroup branch from a0f04ac to 73de5e0 Compare December 5, 2022 10:51
@grypp grypp mentioned this pull request Dec 5, 2022

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

Very nice! Thanks a lot. Regarding clone/move I left a pointer if that helps.

Thanks once again for doing this!

Also now that these are different passes, maybe we can actually split the file as well and move relevant pieces to the right files. Totally for bonus points! :)

@grypp
grypp force-pushed the split_dispatch_workgroup branch from 73de5e0 to 4b2403b Compare December 6, 2022 14:08
@grypp
grypp requested a review from hanhanW as a code owner December 6, 2022 14:08
@grypp

grypp commented Dec 6, 2022

Copy link
Copy Markdown
Contributor Author

I split files too, let me know if you like the names of the passes and files -- bonus episodes are always fun :)

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

Looks great to me and a really nice cleanup/reorganization! Thanks for doing this!

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

Looks good! Thanks!

namespace mlir {
class Operation;
/// A rewriter that keeps track of all tensor::DimOps.
class TensorDimTrackingRewriter : public IRRewriter {

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.

Not needed to do with this PR, but you can land this in llvm-external-projects and use it from there. You are probably interested in upstreaming this?

@hanhanW hanhanW left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I skimmed through the PR and left some style nits. Overall it looks good!

for (Region &r : op->getRegions())
for (Block &b : r.getBlocks())
for (Operation &op : b)
if (isa<OpTys...>(&op)) rootOps.push_back(&op);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does Region::getOps() work for this case? That could help less nested loops. E.g.,

for (Region &r : op->getRegions()) {
  for (auto &op: r->getOps()) {
    if (isa<OpTys..
  }
}

also, we have to wrap braces for this case.

grypp added 2 commits December 7, 2022 08:56
`iree-flow-dispatch-linalg-on-tensors-pass` has become a large Pass: 1) generates `flow.dispatch.region`, 2) forms `flow.dispatch.workgroups` from these generated ones, 3) transforms Linalg Ops into flow Ops. This PR splits this pass into first step and further steps. It introduces two Passes to do this.

**iree-flow-form-dispatch-regions-pass** : Runs heuristic to fuse Linalg Ops using tensors and generates `flow.dispatch.region` from them. It also generates workload information for each `dispatch.region`.

**iree-flow-form-workgroups-regions-pass** : It traverses over `flow.dispatch.region` and generates `flow.dispatch.workgroups`. It also performs the remaining steps of `iree-flow-dispatch-linalg-on-tensors-pass`.
@grypp
grypp force-pushed the split_dispatch_workgroup branch from 2b7e086 to 17203ff Compare December 7, 2022 08:20
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

@grypp feel free to land this and rebase your other PR on top of this. All reviewers have stamped it.

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.

4 participants