Skip to content

Add pass to expand all vectors in vecmat/matvec operations into matrices to enable tiling - #15273

Merged
NatashaKnk merged 4 commits into
iree-org:mainfrom
NatashaKnk:vectors_to_matmul
Oct 25, 2023
Merged

NatashaKnk merged 4 commits into
iree-org:mainfrom
NatashaKnk:vectors_to_matmul

Conversation

@NatashaKnk

Copy link
Copy Markdown
Contributor

This PR fails until the newest llvm integration as it depends on 68945.

As discussed in issue#15053 and PR#15257, expanding the vectors in vecmat/matvec operations and avoiding them in the encoding step entirely seems like the best approach.

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

Looking great, just a couple of nits!

Comment on lines +33 to +36
if (op.isRowMajorMatmul() || op.isColumnMajorMatmul() ||
op.isRowMajorBatchMatmul()) {
return rewriter.notifyMatchFailure(linalgOp, "op is already a matmul");
}

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.

Is checking this needed? Below at line 46 we are returning failure when not one of the matrix-vector ops that this pattern would rewrite. That line 46 return is necessary and ... sufficient?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks!

.addPass(IREE::Flow::createFoldUnitExtentDimsPass)
.addPass(IREE::Flow::createFuseDequantizationMatmulPass)
// Expand all vectors in vecmat/matvec ops into matrices for tiling.
.addPass(createExpandVectorsPass)

@pzread pzread Oct 24, 2023 •

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.

Should we only add this pass when transformOptions.options.dataTiling == true? It seems like the current main purpose of this pass is for data-tiling.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. This should be done using .addPredicatedPass similarly to SetEncoding, right? (Couldn't find any explicit documentation that that's what it does so just want to make sure)

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 think so. But let me know if there is a better approach : )

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.

Yes, we should only add it when data-tiling is enabled for now. If we want to make it on by default, we should collect some data point.

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

Awesome!

@pzread pzread added benchmarks:x86_64 Run default x86_64 benchmarks benchmarks:android-cpu Run default Android CPU benchmarks labels Oct 24, 2023

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

LGTM, just few nits. Thanks!

@bjacob

bjacob commented Oct 25, 2023 •

Copy link
Copy Markdown
Collaborator

Since this is nearly ready, I'm rebasing my own work #15158 to depend on this (I am adding that missing linalg.batch_vecmat ops to linag and will update this ExpandVectors pass to support it).

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

LGTM if the comments are addressd.

@github-actions

Copy link
Copy Markdown

Abbreviated Benchmark Summary

@ commit 81db8c88bff0bdde5c16037aa507e3b3f23da0f7 (vs. base 466e812764beb3eeafebba5e5a45324cd40b5341)

Regressed Latencies 🚩

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
BertLargeTF(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][default-flags] local\_task(embedded\_elf)[8-thread,full-inference,default-flags] with zeros @ c2-standard-16[cpu] 1634.888 (vs. 1334.916, 22.47%↑) 1632.015 14.953
GPT2\_117M\_TF\_1X4XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][default-flags] local\_task(embedded\_elf)[1-thread,full-inference,system-scheduling] with zeros @ pixel-6-pro[big-core] 301.243 (vs. 246.687, 22.12%↑) 301.262 1.227
MobileBertSquad\_fp32(tflite) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel] local\_task(embedded\_elf)[8-thread,full-inference,default-flags] with zeros @ c2-standard-16[cpu] 83.673 (vs. 73.652, 13.61%↑) 85.378 3.865

[Top 3 out of 6 results showed]

Improved Latencies 🎉

Benchmark Name Average Latency (ms) Median Latency (ms) Latency Standard Deviation (ms)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,data-tiling,ukernel] local\_task(embedded\_elf)[1-thread,full-inference,system-scheduling] with zeros @ pixel-4[big-core] 35.551 (vs. 410.690, 91.34%↓) 35.506 0.129
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,data-tiling,ukernel] local\_task(embedded\_elf)[4-thread,full-inference,system-scheduling] with zeros @ pixel-4[big-core] 29.191 (vs. 308.051, 90.52%↓) 29.185 0.056
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,data-tiling,ukernel] local\_task(embedded\_elf)[1-thread,full-inference,system-scheduling] with zeros @ pixel-6-pro[big-core] 22.386 (vs. 199.375, 88.77%↓) 22.384 0.069

[Top 3 out of 7 results showed]

Regressed Total Dispatch Sizes 🚩

Benchmark Name Total Dispatch Size (bytes)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 40192 (vs. 24384, 64.83%↑)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 18888 (vs. 16392, 15.23%↑)

Regressed Total Artifact Sizes 🚩

Benchmark Name Total Artifact Size (bytes)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 992369428 (vs. 652738900, 52.03%↑)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 992415252 (vs. 652771412, 52.03%↑)

Improved Total Artifact Sizes 🎉

Benchmark Name Total Artifact Size (bytes)
matmul\_128x256x8192\_f16t\_tile\_config\_default(linalg) [cuda-sm\_80-linux\_gnu-cuda][ukernel,matmul,splitk,compile-stats] 14827 (vs. 25843, 42.63%↓)
matmul\_128x256x8192\_f32t\_tile\_config\_default(linalg) [cuda-sm\_80-linux\_gnu-cuda][ukernel,matmul,splitk,compile-stats] 15835 (vs. 26847, 41.02%↓)
matmul\_2560x2560x2560\_f16t\_tile\_config\_default(linalg) [cuda-sm\_80-linux\_gnu-cuda][ukernel,matmul,compile-stats] 33593 (vs. 39097, 14.08%↓)

[Top 3 out of 11 results showed]

Regressed Stream IR Dispatch Count (# of cmd.dispatch ops) 🚩

Benchmark Name Stream IR Dispatch Count (# of cmd.dispatch ops)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 440 (vs. 320, 37.50%↑)
GPT2\_117M\_TF\_1X1XI32(stablehlo) [armv8.2-a-generic-linux\_android29-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 440 (vs. 320, 37.50%↑)
MiniLML12H384Uncased(stablehlo) [x86\_64-cascadelake-linux\_gnu-llvm\_cpu][experimental-flags,data-tiling,ukernel,compile-stats] 355 (vs. 353, 0.57%↑)

[Top 3 out of 4 results showed]

For more information:

Source Workflow Run

@pzread

pzread commented Oct 25, 2023

Copy link
Copy Markdown
Contributor

Impressive improvements on GPT2 benchmarks 🎉

@hanhanW

hanhanW commented Oct 25, 2023

Copy link
Copy Markdown
Contributor

@MaheshRavishankar FYI there is a boost for GPT2 models from matvec data-tiling

@NatashaKnk

Copy link
Copy Markdown
Contributor Author

Any reason for concern on the regressions? Otherwise I'll merge it. Thanks everyone for the help :)

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

That's fantastic. I was hoping that was the case with the gpt numbers . Cc @mariecwhite

@benvanik

Copy link
Copy Markdown
Collaborator

The regressions look big on dispatch count/size/etc - do we know why?

@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

If i have to guess its because we have some pack operations that may not be getting fused. There are reshapes introduced that are probably blocking the fusion. That would be good to look into, but for me it is not a blocker. It'd be great if we can file an issue with some snippets of what exactly is happening with this model.

@benvanik

Copy link
Copy Markdown
Collaborator

Yeah not a blocker but would be good to capture here when we see it as it's probably easy to spot with an IR diff and will be good to fix - will be trickier to find in the future :)

@mariecwhite

Copy link
Copy Markdown
Contributor

Wowee! Nice work!

@NatashaKnk
NatashaKnk merged commit 117e82d into iree-org:main Oct 25, 2023
@MaheshRavishankar

Copy link
Copy Markdown
Collaborator

@NatashaKnk would be really useful to do some triage of the increase in dispatch count if you have all the pieces

@hanhanW

hanhanW commented Oct 25, 2023

Copy link
Copy Markdown
Contributor

If i have to guess its because we have some pack operations that may not be getting fused. There are reshapes introduced that are probably blocking the fusion. That would be good to look into, but for me it is not a blocker. It'd be great if we can file an issue with some snippets of what exactly is happening with this model.

Yes, that's likely the case. The regression only happens on data-tiling path.

@dcaballe

Copy link
Copy Markdown
Contributor

Awesome!

Probably similar issue: #15242

ramiro050 pushed a commit to ramiro050/iree that referenced this pull request Dec 19, 2023
…ces to enable tiling (iree-org#15273)

As discussed in
[issue#15053](iree-org#15053) and
[PR#15257](iree-org#15257), expanding the
vectors in vecmat/matvec operations and avoiding them in the encoding
step entirely seems like the best approach.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmarks:android-cpu Run default Android CPU benchmarks benchmarks:x86_64 Run default x86_64 benchmarks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable data tiling + ukernels for linalg.vecmat and linalg.matvec

8 participants