Skip to content

Re-land moving tensor->flow passes. - #6651

Merged
ScottTodd merged 4 commits into
mainfrom
scotttodd-tensortoflow
Aug 5, 2021
Merged

ScottTodd merged 4 commits into
mainfrom
scotttodd-tensortoflow

Conversation

@ScottTodd

@ScottTodd ScottTodd commented Aug 4, 2021 •

Copy link
Copy Markdown
Member

Reverts #6648, rolling-forward #6586.

First commit is the pure revert, second is a set of proposed fixes for the performance regression. The single pass (with a true/false option) is now split further:

  • createConvertTensorOpsPass
  • createConvertLinalgTensorOpsPass(true)
  • createConvertLinalgTensorOpsPass(false)

This gets the original change closer to an NFC (but it is still not an NFC).

Before this change:

stage patterns
buildCommonInputConversionPassPipeline tensor::CastOp, tensor::FromElementsOp
Flow, before DispatchRegionFormation tensor::InsertSliceOp, tensor::ExtractSliceOp,
linalg::TensorCollapseShapeOp, linalg::TensorExpandShapeOp
Flow, after DispatchRegionFormation linalg::FillOp

After this change:

stage patterns
buildCommonInputConversionPassPipeline (deleted)
Flow, before DispatchRegionFormation tensor::CastOp, tensor::FromElementsOp,
tensor::InsertSliceOp, tensor::ExtractSliceOp,
linalg::TensorCollapseShapeOp, linalg::TensorExpandShapeOp
Flow, after DispatchRegionFormation linalg::FillOp

Passes on linalg ops are the same, but two tensor ops are now converted after linalg fusion, rather than at input conversion time. The first version of this change had those linalg ops converted during the "common input conversion pipeline".

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator
buildCommonInputConversionPassPipeline tensor::CastOp, tensor::FromElementsOp,tensor::InsertSliceOp, tensor::ExtractSliceOp

Of the top of my head I dont know if the move of tensor.insert_slice and tensor.extract_slice early on is going to have an impact. If this is being done from detensoring, you could just use tensor::CastOp and tensor::FromElementsOp in the buildCommonInputConversionPassPipeline and use the ConvertToFlowTensorOpsPass(/*before =*/true) before you run the detonsorization passes?

@ScottTodd

Copy link
Copy Markdown
Member Author

buildCommonInputConversionPassPipeline
tensor::CastOp, tensor::FromElementsOp,tensor::InsertSliceOp, tensor::ExtractSliceOp

Of the top of my head I dont know if the move of tensor.insert_slice and tensor.extract_slice early on is going to have an impact. If this is being done from detensoring, you could just use tensor::CastOp and tensor::FromElementsOp in the buildCommonInputConversionPassPipeline and use the ConvertToFlowTensorOpsPass(/*before =*/true) before you run the detonsorization passes?

The specific need I have for detensoring is for the tensor::FromElementsOp pattern after detensoring, before dispatch region formation. Beyond that, I'd like to keep the patterns/passes organized and grouped together where possible. Requiring certain patterns run (or not) at specific times for performance (and not correctness) is tricky to maintain without first converting that tribal knowledge into documentation or tests...

@iree-github-actions-bot

iree-github-actions-bot commented Aug 5, 2021 •

Copy link
Copy Markdown
Contributor

Abbreviated Benchmark Summary

@ commit 21ecd0ce52fcf928a541e6e096adbfdbb7e882a1 (vs. base cd0d8bf37104354ede6c8e8021c9fc7c90abff2f)

Raw Benchmarks

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
MobileNetV2 [fp32,imagenet] (TensorFlow) kernel-execution with IREE-Vulkan @ SM-G980F (GPU-Mali-G77) 14 14 0
MobileNetV2 [fp32,imagenet] (TensorFlow) 3-thread,little-core,full-inference with IREE-Dylib @ SM-G980F (CPU-ARMv8.2-A) 387 387 10
MobileNetV2 [fp32,imagenet] (TensorFlow) 1-thread,little-core,full-inference with IREE-Dylib @ SM-G980F (CPU-ARMv8.2-A) 815 825 26

[Top 3 out of 51 benchmark results showed]

For more information:

@ScottTodd

Copy link
Copy Markdown
Member Author

Benchmark results look inline with prior commits to me - no regression from this :)

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

buildCommonInputConversionPassPipeline
tensor::CastOp, tensor::FromElementsOp,tensor::InsertSliceOp, tensor::ExtractSliceOp

Of the top of my head I dont know if the move of tensor.insert_slice and tensor.extract_slice early on is going to have an impact. If this is being done from detensoring, you could just use tensor::CastOp and tensor::FromElementsOp in the buildCommonInputConversionPassPipeline and use the ConvertToFlowTensorOpsPass(/*before =*/true) before you run the detonsorization passes?

The specific need I have for detensoring is for the tensor::FromElementsOp pattern after detensoring, before dispatch region formation. Beyond that, I'd like to keep the patterns/passes organized and grouped together where possible. Requiring certain patterns run (or not) at specific times for performance (and not correctness) is tricky to maintain without first converting that tribal knowledge into documentation or tests...

Agreed it is tricky to maintain, but is also tricky to write tests. Only realistic way to test these is to use the number of dispatches created as a proxy for this. I cant think of good lit tests you can write for this. These are whole program optimizations.
Documentation is something we can do, but those can get stale quickly. These arent correctness issues. These are performance issues. Tracking via perf regressions is the best test IMO.
In any case, thats orthogonal to what you are trying to do right. I am not against the refactoring here, but putting all patterns in one place is not always beneficial. You want to be deliberate in which patterns apply where. Each of those passes and patterns are for a specific purpose.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Benchmark results look inline with prior commits to me - no regression from this :)

Still would be better to not do tensor.*_slice -> flow.tensor.* that early. In general lowering directly to Flow is problematic. I dont think long term that is going to be the case. So why put more of such conversions on that path.

@ScottTodd

Copy link
Copy Markdown
Member Author

Benchmark results look inline with prior commits to me - no regression from this :)

Still would be better to not do tensor.*_slice -> flow.tensor.* that early. In general lowering directly to Flow is problematic. I dont think long term that is going to be the case. So why put more of such conversions on that path.

Okay. I'm not totally following the rationale here and in #6368, but could we just drop the 2-4 patterns from buildCommonInputConversionPassPipeline and handle all 4 entirely during the flow transformation pipeline? Or is there some factoring that you'd propose for pass naming / options?

@ScottTodd ScottTodd added compiler/dialects Relating to the IREE compiler dialects (flow, hal, vm) and removed buildkite:benchmark labels Aug 5, 2021
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Okay. I'm not totally following the rationale here and in #6368, but could we just drop the 2-4 patterns from buildCommonInputConversionPassPipeline and handle all 4 entirely during the flow transformation pipeline? Or is there some factoring that you'd propose for pass naming / options?

I think that would be ideal, but its going to be non-trivial. If I understand correctly you can move the tensor.cast to flow.tensor.cast conversion after dispatch region formation (you just need to make sure that the tensor.cast is not within a dispatch region.

The tensor.from_elements is tricky. That, if memory serves me right, is related to how mhlo rerpesents operations related to control flow.

@stellaraccident might help more on that.

I am just saying in the table above make the before and after this change the same. The demarcation of what "tensor" ops are and what "linalg on tensors" ops are is blur. So they dont bucket neatly by dialect.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

Looked through this again. I am more convinced that the tensor.insert_slice and tensor.extract_slice conversion to flow.tensor.* ops. needs to happen after all the Linalg fusion passes. One of the things that happens in Linalg fusion is that unit-extent dimensions are dropped. One way that is done is by using the rank-reducing versions of these ops. Converting them to flow. means that this is not going to happen.
The impact might not show up on our benchmarks, but its one of the pieces of the puzzle that helps elementwise fusion.
Ideally no op should be converted to flow ops before linalg fusion, giving it full opportunity to fuse. tensor.from_elements is different in the sense that there is nothing much you can do in terms of fusion there. So if possible all the conversion to flow should be done after linalg fusion.

@ScottTodd

Copy link
Copy Markdown
Member Author

I removed the common input conversion pass entirely, moving those tensor conversion patterns to after linalg fusion. I also added a test for tensor.cast e2e to verify that #6368 is still fixed (though without a more complex test model I'm not sure if this overly simplifies the problem).

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

Ok, this looks good to me. Hopefully doesnt cause any other issues. If there is, I think we should try to preserve this flow anyway.

@ScottTodd
ScottTodd merged commit af998b2 into main Aug 5, 2021
@ScottTodd
ScottTodd deleted the scotttodd-tensortoflow branch August 5, 2021 19:59
@NatashaKnk NatashaKnk mentioned this pull request Aug 6, 2021
@ThomasRaoux ThomasRaoux mentioned this pull request Aug 9, 2021
ScottTodd added a commit that referenced this pull request Aug 25, 2021
…6850)

This refactoring prepares for a similar before/after split within `populateTensorToFlowPatterns` so I can move the `tensor.extract` to `flow.tensor.load` pattern from `PromoteTensorLoads` into that common location.

This also follows work in #6651 to organize conversions from `tensor` and `linalg` to `flow`.

<sub>[Related Discord discussion here](https://discord.com/channels/689900678990135345/689906000043573354/879860687747289099)</sub>
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.

3 participants