Skip to content

Turn on aggressive fusion heuristics by default. - #11980

Closed
MaheshRavishankar wants to merge 3 commits into
iree-org:mainfrom
MaheshRavishankar:turnon_aggressive_fusion
Closed

MaheshRavishankar wants to merge 3 commits into
iree-org:mainfrom
MaheshRavishankar:turnon_aggressive_fusion

Conversation

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

No description provided.

MaheshRavishankar pushed a commit to MaheshRavishankar/iree that referenced this pull request Feb 1, 2023
The tile and fuse transformation upstream has been split into its
individual steps allowing caller more control in terms of which
producers to fuse and which values to yield from within the tiled loop
nest. This moves IREEs tile+fuse to use these components, allowing for
handling of multi-result dispatches.

Currently multi-result dispatches do seem to cause bufferization
issues. IREE currently doesnt form such dispatches by default, but
will do so with PR iree-org#11980. The bufferization issue is tracked in

Also fixes iree-org#11505.
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Current blocker is #12019

MaheshRavishankar added a commit that referenced this pull request Feb 2, 2023
The tile and fuse transformation upstream has been split into its individual steps allowing caller more control in terms of which producers to fuse and which values to yield from within the tiled loop nest. This moves IREEs tile+fuse to use these components, allowing for handling of multi-result dispatches.

Currently multi-result dispatches do seem to cause bufferization issues. IREE currently doesnt form such dispatches by default, but will do so with PR #11980. The bufferization issue is tracked in #12019

Also fixes #11505.
nithinsubbiah pushed a commit to nod-ai/SRT that referenced this pull request Feb 3, 2023
The tile and fuse transformation upstream has been split into its individual steps allowing caller more control in terms of which producers to fuse and which values to yield from within the tiled loop nest. This moves IREEs tile+fuse to use these components, allowing for handling of multi-result dispatches.

Currently multi-result dispatches do seem to cause bufferization issues. IREE currently doesnt form such dispatches by default, but will do so with PR iree-org#11980. The bufferization issue is tracked in iree-org#12019

Also fixes iree-org#11505.
@MaheshRavishankar
MaheshRavishankar marked this pull request as ready for review February 12, 2023 17:27
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

All Vulkan and CPU tests pass for me locally now. Running CI.

@ThomasRaoux

Copy link
Copy Markdown
Contributor

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Going to look into the failures, but as it stands these are the dispatches on the benchmarks

Before

Model Fills Copies Dispatches Executables Generic Before Generic After DispatchWorkgroups
bert_encoderdefault_off 11 4 514 41 1198 534 514
collatzdefault_off 2 1 0 41 12 0 0
deeplabdefault_off 18 1 78 41 105 57 78
edge_detectiondefault_off 1 0 3 41 4 1 3
fragmentdefault_off 3 1 1 41 10 1 1
fullyconnecteddefault_off 0 0 3 41 8 3 3
minilmdefault_off 0 2 223 41 1137 249 223
mnistdefault_off 4 0 6 41 10 6 6
mobilebert-fp32default_off 25 4 752 41 1741 652 752
mobilenetv2default_off 18 1 74 41 103 56 74
mobilessddefault_off 30 1 121 41 157 88 121
persondetect-int8default_off 14 1 75 41 95 45 75
posenetdefault_off 14 1 48 41 59 32 48
resnet50default_off 338 0 78 41 711 55 78
unidirectional_lstmdefault_off 5 5 10 41 43 7 10

After

Model Fills Copies Dispatches Executables Generic Before Generic After DispatchWorkgroups
bert_encoderdefault_on 11 4 489 41 1198 533 489
collatzdefault_on 2 1 0 41 12 0 0
deeplabdefault_on 18 1 78 41 105 57 78
edge_detectiondefault_on 1 0 3 41 4 1 3
fragmentdefault_on 3 1 1 41 10 1 1
fullyconnecteddefault_on 0 0 3 41 8 3 3
minilmdefault_on 0 2 184 41 1137 248 184
mnistdefault_on 4 0 4 41 10 6 4
mobilebert-fp32default_on 1 4 727 41 1741 651 727
mobilenetv2default_on 18 1 73 41 103 56 73
mobilessddefault_on 30 1 121 41 157 88 121
persondetect-int8default_on 14 1 58 41 95 45 58
posenetdefault_on 14 1 48 41 59 32 48
resnet50default_on 338 0 76 41 711 55 76
unidirectional_lstmdefault_on 5 8 8 41 43 5 8

@ThomasRaoux

Copy link
Copy Markdown
Contributor

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

Right, the idea is to make the softmax decomposition do the rematerialization when decomposing and then remove the general pass. That means if a code sequence like softmax (but not using the softmax op) is fused we will have large allocations again. It sounds like that’s what could happen with aggressive fusion.

@MaheshRavishankar

MaheshRavishankar commented Feb 12, 2023 •

Copy link
Copy Markdown
Collaborator Author

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

Right, the idea is to make the softmax decomposition do the rematerialization when decomposing and then remove the general pass. That means if a code sequence like softmax (but not using the softmax op) is fused we will have large allocations again. It sounds like that’s what could happen with aggressive fusion.

yup, understood... today rematerialization works, cause really the only case that get fused that way is softmax. Once the decomposition does rematerialzation, I can remove the use of that pass in the backends and make sure we dont fuse in a way that creates allocations. I think those could be decoupled. In any case, large stacks are a hard-error today... so we will know if there are issues.

@ThomasRaoux

Copy link
Copy Markdown
Contributor

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

Right, the idea is to make the softmax decomposition do the rematerialization when decomposing and then remove the general pass. That means if a code sequence like softmax (but not using the softmax op) is fused we will have large allocations again. It sounds like that’s what could happen with aggressive fusion.

yup, understood... today rematerialization works, cause really the only case that get fused that way is softmax. Once the decomposition does rematerialzation, I can remove the use of that pass in the backends and make sure we dont fuse in a way that creates allocations. I think those could be decoupled. In any case, large stacks are a hard-error today... so we will know if there are issues.

Got it, so you are going to remove the cases that would create large allocs from the fusion? That would work. This is not done yet right?

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

Right, the idea is to make the softmax decomposition do the rematerialization when decomposing and then remove the general pass. That means if a code sequence like softmax (but not using the softmax op) is fused we will have large allocations again. It sounds like that’s what could happen with aggressive fusion.

yup, understood... today rematerialization works, cause really the only case that get fused that way is softmax. Once the decomposition does rematerialzation, I can remove the use of that pass in the backends and make sure we dont fuse in a way that creates allocations. I think those could be decoupled. In any case, large stacks are a hard-error today... so we will know if there are issues.

Got it, so you are going to remove the cases that would create large allocs from the fusion? That would work. This is not done yet right?

Trying to guage the order of things. Ill probably defer that to later stage. So keep the rematerialization for now (its used on all backends), and then evaluate after softmax doesnt need it.

@ThomasRaoux

Copy link
Copy Markdown
Contributor

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

Right, the idea is to make the softmax decomposition do the rematerialization when decomposing and then remove the general pass. That means if a code sequence like softmax (but not using the softmax op) is fused we will have large allocations again. It sounds like that’s what could happen with aggressive fusion.

yup, understood... today rematerialization works, cause really the only case that get fused that way is softmax. Once the decomposition does rematerialzation, I can remove the use of that pass in the backends and make sure we dont fuse in a way that creates allocations. I think those could be decoupled. In any case, large stacks are a hard-error today... so we will know if there are issues.

Got it, so you are going to remove the cases that would create large allocs from the fusion? That would work. This is not done yet right?

Trying to guage the order of things. Ill probably defer that to later stage. So keep the rematerialization for now (its used on all backends), and then evaluate after softmax doesnt need it.

Sounds good to me. I was going to remove it because you were concerned about it but I can’t really think about case where doing the rematerialization is worse and where backend cse wouldn’t revert it in case it is worth.
I guess we could have saved the work in matching softmax if we are going to enable aggressive fusion anyway

@MaheshRavishankar

MaheshRavishankar commented Feb 13, 2023 •

Copy link
Copy Markdown
Collaborator Author

I thought you didn’t want to turn it on? This will break once we remove rematerialization pass.

Didnt you plan to move all of that into the softmax decomposition pass?

Right, the idea is to make the softmax decomposition do the rematerialization when decomposing and then remove the general pass. That means if a code sequence like softmax (but not using the softmax op) is fused we will have large allocations again. It sounds like that’s what could happen with aggressive fusion.

yup, understood... today rematerialization works, cause really the only case that get fused that way is softmax. Once the decomposition does rematerialzation, I can remove the use of that pass in the backends and make sure we dont fuse in a way that creates allocations. I think those could be decoupled. In any case, large stacks are a hard-error today... so we will know if there are issues.

Got it, so you are going to remove the cases that would create large allocs from the fusion? That would work. This is not done yet right?

Trying to guage the order of things. Ill probably defer that to later stage. So keep the rematerialization for now (its used on all backends), and then evaluate after softmax doesnt need it.

Sounds good to me. I was going to remove it because you were concerned about it but I can’t really think about case where doing the rematerialization is worse and where backend cse wouldn’t revert it in case it is worth. I guess we could have saved the work in matching softmax if we are going to enable aggressive fusion anyway

I dont think it is a waste. Rematerialization and CSE are not the inverse of each other (they only become inverse of each other if you remove all the loops and thats a very special case....). The softmax matcher is needed anyway for flash attention... I am just trying to land it without rocking the boat too much...

@ThomasRaoux

Copy link
Copy Markdown
Contributor

I dont think it is a waste. Rematerialization and CSE are not the inverse of each other (they only become inverse of each other if you remove all the loops and thats a very special case....).

Right, what I’m saying is that when there are loops we will always end up with stack allocations which is In general not what we want. But let’s if we find counter examples in the future.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

I dont think it is a waste. Rematerialization and CSE are not the inverse of each other (they only become inverse of each other if you remove all the loops and thats a very special case....).

Right, what I’m saying is that when there are loops we will always end up with stack allocations which is In general not what we want. But let’s if we find counter examples in the future.

Yeah, but we can also not do such fusions. We can say we use rematerialization to avoid stack allocation but that is coming dangerously close to needing a cost model to decide fusion efficacy. That's not the intent right now, and I would like to not rely on it in a way that we cannot turn off materialization if we need based on other examples. Definitely need to look at examples, and today we don't have any. So I am OK with keeping it for now if it isn't used in a load bearing way like it is for softmax.

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

LG!

}

// Finally only fuse if the `ins` operand can be properly bufferized.
// TODO(#10498): Handle the multi-result case.

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.

What's the status of #10498?

I think we're able to handle the case for a while. Can you verify it and close the issue?

@ThomasRaoux

Copy link
Copy Markdown
Contributor

Going to look into the failures, but as it stands these are the dispatches on the benchmarks

Before

Model Fills Copies Dispatches Executables Generic Before Generic After DispatchWorkgroups
bert_encoderdefault_off 11 4 514 41 1198 534 514
collatzdefault_off 2 1 0 41 12 0 0
deeplabdefault_off 18 1 78 41 105 57 78
edge_detectiondefault_off 1 0 3 41 4 1 3
fragmentdefault_off 3 1 1 41 10 1 1
fullyconnecteddefault_off 0 0 3 41 8 3 3
minilmdefault_off 0 2 223 41 1137 249 223
mnistdefault_off 4 0 6 41 10 6 6
mobilebert-fp32default_off 25 4 752 41 1741 652 752
mobilenetv2default_off 18 1 74 41 103 56 74
mobilessddefault_off 30 1 121 41 157 88 121
persondetect-int8default_off 14 1 75 41 95 45 75
posenetdefault_off 14 1 48 41 59 32 48
resnet50default_off 338 0 78 41 711 55 78
unidirectional_lstmdefault_off 5 5 10 41 43 7 10
After

Model Fills Copies Dispatches Executables Generic Before Generic After DispatchWorkgroups
bert_encoderdefault_on 11 4 489 41 1198 533 489
collatzdefault_on 2 1 0 41 12 0 0
deeplabdefault_on 18 1 78 41 105 57 78
edge_detectiondefault_on 1 0 3 41 4 1 3
fragmentdefault_on 3 1 1 41 10 1 1
fullyconnecteddefault_on 0 0 3 41 8 3 3
minilmdefault_on 0 2 184 41 1137 248 184
mnistdefault_on 4 0 4 41 10 6 4
mobilebert-fp32default_on 1 4 727 41 1741 651 727
mobilenetv2default_on 18 1 73 41 103 56 73
mobilessddefault_on 30 1 121 41 157 88 121
persondetect-int8default_on 14 1 58 41 95 45 58
posenetdefault_on 14 1 48 41 59 32 48
resnet50default_on 338 0 76 41 711 55 76
unidirectional_lstmdefault_on 5 8 8 41 43 5 8

I wonder what those numbers look like after the merge of softmax patterns.

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Going to look into the failures, but as it stands these are the dispatches on the benchmarks
Before
Model Fills Copies Dispatches Executables Generic Before Generic After DispatchWorkgroups
bert_encoderdefault_off 11 4 514 41 1198 534 514
collatzdefault_off 2 1 0 41 12 0 0
deeplabdefault_off 18 1 78 41 105 57 78
edge_detectiondefault_off 1 0 3 41 4 1 3
fragmentdefault_off 3 1 1 41 10 1 1
fullyconnecteddefault_off 0 0 3 41 8 3 3
minilmdefault_off 0 2 223 41 1137 249 223
mnistdefault_off 4 0 6 41 10 6 6
mobilebert-fp32default_off 25 4 752 41 1741 652 752
mobilenetv2default_off 18 1 74 41 103 56 74
mobilessddefault_off 30 1 121 41 157 88 121
persondetect-int8default_off 14 1 75 41 95 45 75
posenetdefault_off 14 1 48 41 59 32 48
resnet50default_off 338 0 78 41 711 55 78
unidirectional_lstmdefault_off 5 5 10 41 43 7 10
After
Model Fills Copies Dispatches Executables Generic Before Generic After DispatchWorkgroups
bert_encoderdefault_on 11 4 489 41 1198 533 489
collatzdefault_on 2 1 0 41 12 0 0
deeplabdefault_on 18 1 78 41 105 57 78
edge_detectiondefault_on 1 0 3 41 4 1 3
fragmentdefault_on 3 1 1 41 10 1 1
fullyconnecteddefault_on 0 0 3 41 8 3 3
minilmdefault_on 0 2 184 41 1137 248 184
mnistdefault_on 4 0 4 41 10 6 4
mobilebert-fp32default_on 1 4 727 41 1741 651 727
mobilenetv2default_on 18 1 73 41 103 56 73
mobilessddefault_on 30 1 121 41 157 88 121
persondetect-int8default_on 14 1 58 41 95 45 58
posenetdefault_on 14 1 48 41 59 32 48
resnet50default_on 338 0 76 41 711 55 76
unidirectional_lstmdefault_on 5 8 8 41 43 5 8

I wonder what those numbers look like after the merge of softmax patterns.

Dont know. Will rebase and rerun scripts.

// CHECK: flow.dispatch.workgroups

// -----

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.

Do we have tests to cover cases that are now fused and were not before?

Mahesh Ravishankar added 3 commits February 18, 2023 09:19
…ons.

Rework the logic that prevents fusions that result in stack
allocation. This is mostly a WAR since these dispatches should be
lowered without any stack allocation. The root cause is that these
dispatches are not vectorized. When vectorized there wont be a stack
allocation.
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

Moved the first commit out into its own PR. (#12277 )

@pzread

pzread commented Feb 20, 2023

Copy link
Copy Markdown
Contributor

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator Author

This has landed in main already. Closing

qedawkins pushed a commit to qedawkins/iree that referenced this pull request Apr 2, 2023
The tile and fuse transformation upstream has been split into its individual steps allowing caller more control in terms of which producers to fuse and which values to yield from within the tiled loop nest. This moves IREEs tile+fuse to use these components, allowing for handling of multi-result dispatches.

Currently multi-result dispatches do seem to cause bufferization issues. IREE currently doesnt form such dispatches by default, but will do so with PR iree-org#11980. The bufferization issue is tracked in iree-org#12019

Also fixes iree-org#11505.
jpienaar pushed a commit that referenced this pull request May 1, 2023
The tile and fuse transformation upstream has been split into its individual steps allowing caller more control in terms of which producers to fuse and which values to yield from within the tiled loop nest. This moves IREEs tile+fuse to use these components, allowing for handling of multi-result dispatches.

Currently multi-result dispatches do seem to cause bufferization issues. IREE currently doesnt form such dispatches by default, but will do so with PR #11980. The bufferization issue is tracked in #12019

Also fixes #11505.
rengolin pushed a commit to plaidml/iree that referenced this pull request May 2, 2023
The tile and fuse transformation upstream has been split into its individual steps allowing caller more control in terms of which producers to fuse and which values to yield from within the tiled loop nest. This moves IREEs tile+fuse to use these components, allowing for handling of multi-result dispatches.

Currently multi-result dispatches do seem to cause bufferization issues. IREE currently doesnt form such dispatches by default, but will do so with PR iree-org#11980. The bufferization issue is tracked in iree-org#12019

Also fixes iree-org#11505.
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.

5 participants