Skip to content

[Inductor Cutlass backend] Tolerate dynamic shapes - #121497

Closed
kadeng wants to merge 21 commits into
gh/kadeng/43/basefrom
gh/kadeng/43/head
Closed

kadeng wants to merge 21 commits into
gh/kadeng/43/basefrom
gh/kadeng/43/head

Conversation

@kadeng

@kadeng kadeng commented Mar 8, 2024 •

Copy link
Copy Markdown
Contributor

Previously, when the Cutlass backend was
enabled, using dynamic shapes could lead
to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends,
then an ATen Kernel is used as fallback,
even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Mar 8, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/121497

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 New Failures

As of commit 7a78dc8 with merge base 365e89a (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

kadeng added 2 commits March 11, 2024 14:50
Previously, when the Cutlass backend was
enabled, using dynamic shapes could lead
to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends,
then an ATen Kernel is used as fallback,
even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Previously, when the Cutlass backend was
enabled, using dynamic shapes could lead
to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends,
then an ATen Kernel is used as fallback,
even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Previously, when the Cutlass backend was
enabled, using dynamic shapes could lead
to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends,
then an ATen Kernel is used as fallback,
even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Comment on lines +177 to +179
# checks whether all input tensors and the output layout
# have a static shape by attempting to convert the dimensions
# to int

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.

You probably want to use something like the is_statically_known_list_of_ints implemented in the wrapper.py, which uses the guards information that we already have to simplify the expression.

@kadeng kadeng Mar 13, 2024 •

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.

Thanks for the code pointer. I looked it up, and now I'm not sure if that's what I want to use here. I see that the implementation underlying is_statically_known_list_of_ints is this function:

def statically_known_int_or_none(x):
try:
val = V.graph._shape_env._maybe_evaluate_static(x)
return int(x)
except Exception:
return None
which differs from my implementation (just attempt casting to int), by first calling V.graph._shape_env._maybe_evaluate_static(x). What's surprising to me is that the result of that _maybe_evaluate_static call is just being discarded. I'm not too familiar with the dynamic shape inference system: Does _maybe_evaluate_static have a side effect that would be important here?

@lezcano lezcano Mar 13, 2024 •

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.

It doesn't have any side-effect. What it tries to do is use the knowledge from the guards / asserts that the user may have set, etc to evaluate the expression. Sometimes things that look dynamic, may be possibly static after some asserts in the code, some constant propagation, value range analysis, etc.

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.

Yes, that's also how I understood it. But it's return value is not used, see the code section I quoted ( val is assigned to but discarded). Maybe this is not intentional?

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.

Ah, yes, you found a bug there lol

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.

I started doubting I understood how this is working ;) I'll prepapre a small fix then...

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.

Created #121808

Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
kadeng added 6 commits March 19, 2024 22:36
Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
…ckend] Tolerate dynamic shapes"


Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx peterbell10 ipiszy yf225 chenyang78 muchulee8 aakhundov ColinPeppler amjames desertfire chauhang

[ghstack-poisoned]
[ghstack-poisoned]
@kadeng

kadeng commented Mar 22, 2024

Copy link
Copy Markdown
Contributor Author

Just FYI: I will be on PTO soon ( back after easter Monday ), I won't necessarily be able to react to comments until I return.

kadeng added 2 commits April 4, 2024 20:17
[ghstack-poisoned]
[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Apr 22, 2024
…ing as class annotation (#123930)

I previously added @skipIfRocm as a class annotation within test/inductor/test_cutlass_backend.py - turns out this annotation always skips if applied at class level, so I need to skip Cutlass tests on ROCm differently..

Pull Request resolved: #123930
Approved by: https://github.com/jansel
ghstack dependencies: #121497
pytorchmergebot pushed a commit that referenced this pull request Apr 23, 2024
…els (#123932)

If a Kernel does not return in a reasonable amount of time during autotuning, it can delay inductor compilation a lot. This change introduces soft / hard kill timeouts and a mechanism to kill Kernels being profiled in subprocesses if they take too long.

Correspondingly, a few new config options are introduced within _inductor/config.py - all of them with inline docs.

Test Plan:
Existing tests within test_max_autotune.py and test_cutlass_backend.py ) cover the new codepaths.

Pull Request resolved: #123932
Approved by: https://github.com/jansel
ghstack dependencies: #121497, #123930
pytorchmergebot pushed a commit that referenced this pull request Apr 24, 2024
Add support for bmm ( batch matrix multiply ) op through the Cutlass backend.

Test Plan:
 * CI
 * Added test in test_cutlass_backend.py

Pull Request resolved: #121734
Approved by: https://github.com/eellison
ghstack dependencies: #121497, #123930, #123932
pytorchmergebot pushed a commit that referenced this pull request Apr 24, 2024
This diff disables Cutlass backend EVT epilogue fusions. It does not yet contain the removal of most of the underlying implementation.

Pull Request resolved: #124107
Approved by: https://github.com/jansel
ghstack dependencies: #121497, #123930, #123932, #121734
pytorchmergebot pushed a commit that referenced this pull request Apr 24, 2024
…est setup (#124574)

The diff #122661 introduces a new automatic cache refresh mechanism during all inductor-derived test cases.

But this refresh mechanism seems not to work properly across process boundaries, specifically when using  autotune_in_subproc, which many tests in test_cutlass_backend.py rely on.

Solution: Set the env var INDUCTOR_TEST_DISABLE_FRESH_CACHE=1
early during test setup within test_cutlass_backend.py

Test Plan:
This is a change to unit tests only.

Pull Request resolved: #124574
Approved by: https://github.com/aakhundov
ghstack dependencies: #121497, #123930, #123932, #121734, #124107
pytorchmergebot pushed a commit that referenced this pull request Apr 24, 2024
…emm_choices (#124575)

Clean up CUTLASSGemmTemplate.add_cutlass_gemm_choices, removing code that became unneccessary by removing EVT-based epilogue fusion.

Test Plan:
Already covered by test_cutlass_backend.py

Pull Request resolved: #124575
Approved by: https://github.com/jansel
ghstack dependencies: #121497, #123930, #123932, #121734, #124107, #124574
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
Previously, when the Cutlass backend was enabled, using dynamic shapes could lead to exceptions during JIT.

With this change, there are guards in place to just disable the Cutlass backend if dynamic dimensions are involved.

In addition, if no choices for a GEMM are available using the selected backends, then an ATen Kernel is used as fallback, even if the ATen backend is not enabled.

Test:
CI
Additional unit test in test_cutlass_backend.py

Pull Request resolved: #121497
Approved by: https://github.com/jansel
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
…ing as class annotation (#123930)

I previously added @skipIfRocm as a class annotation within test/inductor/test_cutlass_backend.py - turns out this annotation always skips if applied at class level, so I need to skip Cutlass tests on ROCm differently..

Pull Request resolved: #123930
Approved by: https://github.com/jansel
ghstack dependencies: #121497
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
…els (#123932)

If a Kernel does not return in a reasonable amount of time during autotuning, it can delay inductor compilation a lot. This change introduces soft / hard kill timeouts and a mechanism to kill Kernels being profiled in subprocesses if they take too long.

Correspondingly, a few new config options are introduced within _inductor/config.py - all of them with inline docs.

Test Plan:
Existing tests within test_max_autotune.py and test_cutlass_backend.py ) cover the new codepaths.

Pull Request resolved: #123932
Approved by: https://github.com/jansel
ghstack dependencies: #121497, #123930
petrex pushed a commit to petrex/pytorch that referenced this pull request May 3, 2024
Add support for bmm ( batch matrix multiply ) op through the Cutlass backend.

Test Plan:
 * CI
 * Added test in test_cutlass_backend.py

Pull Request resolved: pytorch#121734
Approved by: https://github.com/eellison
ghstack dependencies: pytorch#121497, pytorch#123930, pytorch#123932
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
This diff disables Cutlass backend EVT epilogue fusions. It does not yet contain the removal of most of the underlying implementation.

Pull Request resolved: #124107
Approved by: https://github.com/jansel
ghstack dependencies: #121497, #123930, #123932, #121734
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
…est setup (#124574)

The diff #122661 introduces a new automatic cache refresh mechanism during all inductor-derived test cases.

But this refresh mechanism seems not to work properly across process boundaries, specifically when using  autotune_in_subproc, which many tests in test_cutlass_backend.py rely on.

Solution: Set the env var INDUCTOR_TEST_DISABLE_FRESH_CACHE=1
early during test setup within test_cutlass_backend.py

Test Plan:
This is a change to unit tests only.

Pull Request resolved: #124574
Approved by: https://github.com/aakhundov
ghstack dependencies: #121497, #123930, #123932, #121734, #124107
pytorch-bot Bot pushed a commit that referenced this pull request May 3, 2024
…emm_choices (#124575)

Clean up CUTLASSGemmTemplate.add_cutlass_gemm_choices, removing code that became unneccessary by removing EVT-based epilogue fusion.

Test Plan:
Already covered by test_cutlass_backend.py

Pull Request resolved: #124575
Approved by: https://github.com/jansel
ghstack dependencies: #121497, #123930, #123932, #121734, #124107, #124574
int3 added a commit to int3/pytorch that referenced this pull request May 8, 2024
)

Summary:

pytorch#121497 made "no choices found" into a soft error, falling back to the ATen kernel in those cases. However, when running benchmarks, we want to be certain that we are actually invoking a particular backend; if we can't, we should hard error. As such, I have added an `autotune_fallback_to_aten` flag to control this behavior.

That PR also introduces a `NoValidChoicesError` type, and has catch blocks for it, but it seems that the code doesn't actually throw this exception. I have added the appropriate throws, as well as tests.

Test Plan: python test/inductor/test_max_autotune.py

Differential Revision: D56764094
pytorch-bot Bot pushed a commit that referenced this pull request May 10, 2024
Summary:

#121497 made "no choices found" into a soft error, falling back to the ATen kernel in those cases. However, when running benchmarks, we want to be certain that we are actually invoking a particular backend; if we can't, we should hard error. As such, I have added an `autotune_fallback_to_aten` flag to control this behavior.

That PR also introduces a `NoValidChoicesError` type, and has catch blocks for it, but it seems that the code doesn't actually throw this exception. I have added the appropriate throws, as well as tests.

Test Plan: python test/inductor/test_max_autotune.py

Differential Revision: D56764094
@github-actions
github-actions Bot deleted the gh/kadeng/43/head branch June 2, 2024 01:59
@cpuhrsch

Copy link
Copy Markdown
Contributor

Just fyi @kadeng this broke some CUTLASS extensions for quantization, but somehow was still able to land. So we were either missing tests or this stack deleted the tests. This might already be resolved, I'm only adding this comment for reference. cc @HDCharles @alexsamardzic

@alexsamardzic

Copy link
Copy Markdown
Collaborator

Just posted a fix for CUTLASS-based mixed dtypes GEMM auto-tuning for Ampere: PR 129058 - let's continue the discussion on the comments section of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants