Skip to content

[Inductor max autotune] Multithreaded Precompilation - #119003

Merged
kadeng merged 6 commits into
gh/kadeng/18/basefrom
gh/kadeng/18/head
Feb 7, 2024
Merged

kadeng merged 6 commits into
gh/kadeng/18/basefrom
gh/kadeng/18/head

Conversation

@kadeng

@kadeng kadeng commented Feb 2, 2024 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

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

When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Feb 2, 2024 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 907de97 with merge base 520771d (image):
💚 Looks good so far! There are no failures yet. 💚

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

When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

[ghstack-poisoned]
@kadeng kadeng self-assigned this Feb 2, 2024
@kadeng
kadeng marked this pull request as ready for review February 2, 2024 19:56
@aakhundov aakhundov added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Feb 3, 2024

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

Add tests

and check_cache(self.get_global_cache(), callback=log_stats)
):
try:
# re-benchmark everything to try to get consistent numbers from the same machine

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.

This comment seems important. I think we do want consistent numbers from the same machine. If you mix old numbers with new numbers it may not be apples-to-apples.

Typically, compiling is much more expensive than running. So the cost of re-running (with a cached compile) should be very low.

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 will change that back if you like. It will literally mean that we run the same code again on the same machine, just at a different point in time. So I can imagine there is a difference if GPU / CPU power settings or background load have changed in the meantime, but not really much else.

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.

done

Comment thread torch/_inductor/config.py Outdated
int(
os.environ.get(
"TORCHINDUCTOR_MAX_AUTOTUNE_PRECOMPILATION_WORKERS",
str(torch.get_num_threads() - 8),

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.

Why -8? This could be negative on some machines.

@kadeng kadeng Feb 5, 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.

Yes, negative values are allowed, in that case we simply don't have parallel precompilation. The idea is to leave at least 8 CPUs unused by parallel precompilation by default. But we could change that of course. If you have a clear preference what to put here instead just tell me.

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.

Why not use just use torch.get_num_threads()?

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.

The idea is to leave 8 CPUs unused under the assumption that these are required for whatever else is happening on the system. But I will remove the -8.

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.

done

len(choices),
num_workers,
)
with ThreadPoolExecutor(max_workers=num_workers) as executor:

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.

Why not reuse the same threadpool and infra for parallel compiles? This seems like it is doing the same thing as async compile...

@kadeng kadeng Feb 5, 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.

Several reasons:

1.) Threads are relatively lightweight to construct but (a bit) costly to keep alive, so there's no real benefit but a downside to maintain a persistent threadpool.

2.) If I would reuse the AsyncCompile threadpool, I could run into deadlocks or have strange performance issues, depending on what other code does. I could of course ensure this is bug free right now, but that's hard to keep ensured as the code evolves. Unit tests would be insufficient to guard against sporadic deadlocks through certain race conditions. Encapsulating this is more robust in the long run.

3.) This code works well, is compact and doesn't couple otherwise unrelated code sections design-wise via global state or code. The DRY principle and avoiding unneccessary coupling as design principles are in conflict here. I would prefer to avoid unneccessary coupling, as it's much more important IMO.

UPDATE:

Removing the "-8" in the number of precompilation workers makes it unneccessary to discern between the two config settings. I will reuse the existing config.

@kadeng

kadeng commented Feb 5, 2024 •

Copy link
Copy Markdown
Contributor Author

Add tests

Add tests

This is actually on the default codepath for all unit tests that involve max autotune. So while there were no dedicated unit tests of the precompilation functionality per-se, a lot of tests would immediately fail if this would not work. Performance benefits in practice are proven through manual tests. But I will add a test where I force the config setting.

UPDATE:

I have added two additional unit tests. One acts as an integration test, forcing parallel precompilation, the other is a whitebox unit test which ensures that the precompile method on the ChoiceCallers is being called properly.

When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

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

[ghstack-poisoned]
When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

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

[ghstack-poisoned]
When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

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

[ghstack-poisoned]
When using the Cutlass backend, the compilation
of CUDA source files can totally dominate the runtime required for the benchmarking done
as part of Autotuning.

This change adds a multithreaded precompilation phase, which serves to pre-populate the compilation cache ( both in-memory, and a
possible on-disk sccache ).

Also it ensures that no unneccessary compilation
and benchmarking steps are performed, which was peviously the case.

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

[ghstack-poisoned]
@kadeng
kadeng requested a review from jansel February 6, 2024 14:50
@kadeng
kadeng merged commit 2ad56f2 into gh/kadeng/18/base Feb 7, 2024
kadeng added a commit that referenced this pull request Feb 7, 2024
kadeng added a commit that referenced this pull request Feb 7, 2024
@kadeng

kadeng commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Can't merge closed PR #119003

@kadeng

kadeng commented Feb 7, 2024

Copy link
Copy Markdown
Contributor Author

Accidentally clicked merge, which doesn't work with ghstack. I can't reopen it, so I had to recreate the PR here: #119386

@github-actions
github-actions Bot deleted the gh/kadeng/18/head branch March 9, 2024 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/trunk Trigger trunk jobs on your pull request module: inductor topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants