Skip to content

Conversation

bohnstingl
Copy link

@bohnstingl bohnstingl commented Oct 6, 2025

Description

This PR aims to add a tutorial of the recent control flow operators.

Checklist

  • The issue that is being fixed is referred in the description (see above "Fixes #ISSUE_NUMBER")
  • Only one issue is addressed in this pull request
  • Labels from the issue that this PR is fixing are added to this pull request
  • No unnecessary issues are included into this pull request.

cc @ydwu4

Copy link

pytorch-bot bot commented Oct 6, 2025

🔗 Helpful Links

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

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

❗ 2 Active SEVs

There are 2 currently active SEVs. If your PR is affected, please view them below:

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

Copy link

meta-cla bot commented Oct 6, 2025

Hi @bohnstingl!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!


The tutorial is split into three parts:

Part 1: Basic Inference Examples
Copy link

Choose a reason for hiding this comment

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

Thanks for writing this! For part 1 and part 2, I feel this tutorial does not add much value compared to what we have in the doc string. For part 3, users probably know how to do autograd in PyTorch and it feels to me just a demo that we could do autograd.

Instead, maybe we should just focus on Part 2, explore the killer examples in depth and show the benefits of using control flow operators in that particular setting e.g. using scan for chunked loss, rnn and associative scan for SSMs and try to get convincing e2e benchmarking results.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you @ydwu4 for looking at it.

About part 1, to me it still feels instructive and my impression is that not too many people are aware of the control flow operators, hence showing some basic usecases may make sense?

I guess we can remove part 3 entirely and then expand more thoroughly in part 2. The RNN and SSM examples are already there, but I can definitely expand more on them. I will also add the chunked loss computation that you mentioned.

What I thought would be interesting to show also are the cases where the control flow ops are actually needed in order to have a compilable model, e.g., show that without them there would be graph breaks. Early stopping based on data-dependent conditions would be another example, aka while_loop. WDYT?

Copy link

Choose a reason for hiding this comment

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

Yeah, sounds good. For chunked loss, I'm doing some benchmarks against liger so you can focus on rnn/SSMs. Graph breaks are not the fundamental reasons why we need control flow operators unless you cannot have graph breaks e.g. in export. It doesn't necessarily have worse performance when the model has graph breaks (at least we need to prove it makes perf worse in important use cases). IMO it's important to show control flow operators' values from perf/usability side compared with eager, torch.compile with native control and with hand-tuned kernels.

Copy link
Author

Choose a reason for hiding this comment

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

But what about early stopping? Wouldn't you need data-dependent operations that get compiled somehow?

Copy link

@ydwu4 ydwu4 Oct 7, 2025

Choose a reason for hiding this comment

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

can compile the body and have multiple torch.compiles in your program where you want to accelerate.

@bohnstingl
Copy link
Author

@ydwu4, I've removed the part 3 and enhanced the scan example in part 2. However, the performance is not as expected.
In particular, one problem arises when comparing scan to the for-loop approach. Although the compile time of scan is clearly much faster, the runtime is actually slower.

I would be more than happy if you can help me investigate that. This is also linked to the dedicated benchmark scripts that I have started here.

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.

2 participants