Skip to content

[user-streams][aoti] Support explicit user streams in cpp_wrapper (#182971) - #182971

Closed
bbus wants to merge 1 commit into
pytorch:mainfrom
bbus:export-D104403189
Closed

bbus wants to merge 1 commit into
pytorch:mainfrom
bbus:export-D104403189

Conversation

@bbus

@bbus bbus commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

NOTE: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: arc lint -a on all touched caffe2 files -> No lint issues. OSS lintrunner (noclang linter set) run locally -> clean.

Exercised end-to-end on an A100, rebased on master.

Full user-streams suite (JIT torch.compile + AOTI paths), including the new TestAOTIUserStreams (inline cudaEventRecord / cudaStreamWaitEvent, per-thread AOTIPerThreadStreamCache / AOTIPerThreadEventCache, loud rejection of synchronize_stream) and the JIT cross-stream strided-copy regression:

buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Result: Tests finished: Pass 71. Fail 0. Skip 7.

AOTI pinned non-blocking copy, which lowers to torch.ops.streams.synchronize_event (this diff emits it inline as cudaEventSynchronize):

buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:test_aot_inductor -- test_copy_non_blocking_is_pinned_cuda

Result: Tests finished: Pass 3. Fail 0.

Reviewed By: mlazos, desertfire

Differential Revision: D104403189

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

@pytorch-bot

pytorch-bot Bot commented May 8, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit dccf8bb with merge base c0b0064 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

@meta-codesync

meta-codesync Bot commented May 8, 2026

Copy link
Copy Markdown

@bbus has exported this pull request. If you are a Meta employee, you can view the originating Diff in D104403189.

@aorenste
aorenste removed their request for review May 8, 2026 17:57
@bbus
bbus requested review from desertfire and mlazos May 8, 2026 19:53
Comment thread torch/_inductor/lowering.py Outdated
# Dynamo's stream-tracing module.
from torch.fx.node import has_side_effect as _has_side_effect

_has_side_effect(torch.ops.streams.record_event.default)

@mlazos mlazos May 12, 2026 •

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 we marked these already in torch/_dynamo/variables/streams.py

# Don't generate ctx switching. Memory planning code (e.g., delete buffers) on current
# node goes to previous stream ctx.
return
elif self.current_stream_idx is None and stream is not None:

@mlazos mlazos May 12, 2026 •

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.

hmm not sure if this is needed? elif has the same effect here?

Comment thread torch/_inductor/scheduler.py Outdated
the previous node's stream. NopKernelSchedulerNodes have stream=None and inherit the
enclosing stream context (or do nothing if no context is active yet).

Defaults missing nodes to the default stream: post-fusion reorder

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.

We should probably fix this, I'm worried other data might be getting lost, we want to preserve metadata

Comment thread test/inductor/test_user_streams.py Outdated
result = loaded(*inputs)
return result, code

def test_multistream_with_cudagraphs_raises_at_compile_time(self):

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.

There isn't a fundamental blocker to this right? just not supported right now. Streams should be able to work with cudagraphs

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, there is no fundamental limitation. Changed the error to not implemented.



@unittest.skipIf(not TEST_CUDA, "requires CUDA")
class TestAOTIUserStreams(InductorTestCase):

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.

Higher level note, could we just run all of the stream tests that exist in AOTI and parametrize it so we can run with both AOTI or normal Inductor?

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.

Updated the user_stream tests to be parametrized

@meta-codesync meta-codesync Bot changed the title [user-streams][aoti] Support explicit user streams in cpp_wrapper [user-streams][aoti] Support explicit user streams in cpp_wrapper (#182971) May 15, 2026
@bbus
bbus force-pushed the export-D104403189 branch from fa63401 to 573d47f Compare May 15, 2026 16:14
bbus added a commit to bbus/pytorch that referenced this pull request May 15, 2026
…torch#182971)

Summary:
Pull Request resolved: pytorch#182971

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 573d47f to a3ed86c Compare May 15, 2026 16:21
bbus added a commit to bbus/pytorch that referenced this pull request May 15, 2026
…torch#182971)

Summary:
Pull Request resolved: pytorch#182971

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from a3ed86c to 75b7e27 Compare May 17, 2026 14:00
bbus added a commit to bbus/pytorch that referenced this pull request May 17, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 75b7e27 to 693aa13 Compare May 18, 2026 14:45
bbus added a commit to bbus/pytorch that referenced this pull request May 18, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Differential Revision: D104403189
bbus added a commit to bbus/pytorch that referenced this pull request May 18, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 693aa13 to cb40450 Compare May 18, 2026 14:49
"""
if num_streams <= 1 or not V.graph.aot_mode:
# JIT cpp_wrapper + multi-stream is a separate, currently
# unsupported path. Skipping preserves the pre-existing behavior

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.

It's better to error out loud if this path is not supported yet.

self._multi_stream_helpers_emitted = True
self.header.splice(
maybe_hipify_code_wrapper(
"""

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.

Let's make this as a header file under the aoti_runtime directory. Easier for other people to follow.

// since destructors must not throw and CUDA may already
// be tearing down.
for (auto event : events) {
if (event != nullptr) (void)cudaEventDestroy(event);

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.

Maybe write to std::cerr? similar to

std::cerr << "Failed to destroy CUDA event in AOTInductor model: "


~AOTIPerThreadStreamCache() {
for (auto stream : streams) {
if (stream != nullptr) (void)cudaStreamDestroy(stream);

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.

similar here

};

static thread_local AOTIPerThreadEventCache _aoti_event_cache;
static thread_local AOTIPerThreadStreamCache _aoti_aux_stream_cache;

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.

These two can be left here instead of going to the new .h file.

@bbus
bbus force-pushed the export-D104403189 branch from a9bf6e1 to 1df2d98 Compare June 7, 2026 12:45
bbus added a commit to bbus/pytorch that referenced this pull request Jun 7, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Reviewed By: desertfire

Differential Revision: D104403189
bbus added a commit to bbus/pytorch that referenced this pull request Jun 7, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 1df2d98 to 8912f8f Compare June 7, 2026 13:46
bbus added a commit to bbus/pytorch that referenced this pull request Jun 7, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 8912f8f to 9153a60 Compare June 7, 2026 14:46
bbus added a commit to bbus/pytorch that referenced this pull request Jun 7, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 9153a60 to 683dc13 Compare June 7, 2026 15:04
bbus added a commit to bbus/pytorch that referenced this pull request Jun 7, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 683dc13 to e39d9ba Compare June 7, 2026 18:20
bbus added a commit to bbus/pytorch that referenced this pull request Jun 7, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan: buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=h100a -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from e39d9ba to acf8392 Compare June 7, 2026 18:22
@bbus
bbus force-pushed the export-D104403189 branch from acf8392 to 5d71f5c Compare July 1, 2026 13:20
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues.

Exercised end-to-end on an A100, rebased on `master`. Ran the full user-streams suite (both the JIT `torch.compile` path and the new AOTI path). The new `TestAOTIUserStreams` cases compile via `aoti_compile_and_package`, load, run, and compare to eager -- covering inline `cudaEventRecord` / `cudaStreamWaitEvent`, the per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, and loud compile-time rejection of `synchronize_stream`. The suite also covers the JIT cross-stream strided-copy regression.

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```

Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

Reviewed By: desertfire

Differential Revision: D104403189
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:
Pull Request resolved: pytorch#182971

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues.

Exercised end-to-end on an A100, rebased on `master`. Ran the full user-streams suite (both the JIT `torch.compile` path and the new AOTI path). The new `TestAOTIUserStreams` cases compile via `aoti_compile_and_package`, load, run, and compare to eager -- covering inline `cudaEventRecord` / `cudaStreamWaitEvent`, the per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, and loud compile-time rejection of `synchronize_stream`. The suite also covers the JIT cross-stream strided-copy regression.

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```

Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 5d71f5c to 9d5390f Compare July 1, 2026 13:26
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues.

Exercised end-to-end on an A100, rebased on `master`. Ran the full user-streams suite (both the JIT `torch.compile` path and the new AOTI path). The new `TestAOTIUserStreams` cases compile via `aoti_compile_and_package`, load, run, and compare to eager -- covering inline `cudaEventRecord` / `cudaStreamWaitEvent`, the per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, and loud compile-time rejection of `synchronize_stream`. The suite also covers the JIT cross-stream strided-copy regression.

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```

Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from 9d5390f to d2011b2 Compare July 1, 2026 14:09
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:
Pull Request resolved: pytorch#182971

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues.

Exercised end-to-end on an A100, rebased on `master`. Ran the full user-streams suite (both the JIT `torch.compile` path and the new AOTI path). The new `TestAOTIUserStreams` cases compile via `aoti_compile_and_package`, load, run, and compare to eager -- covering inline `cudaEventRecord` / `cudaStreamWaitEvent`, the per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, and loud compile-time rejection of `synchronize_stream`. The suite also covers the JIT cross-stream strided-copy regression.

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```

Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch from d2011b2 to 94d81b9 Compare July 1, 2026 14:13
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues.

Exercised end-to-end on an A100, rebased on `master`. Ran the full user-streams suite (both the JIT `torch.compile` path and the new AOTI path). The new `TestAOTIUserStreams` cases compile via `aoti_compile_and_package`, load, run, and compare to eager -- covering inline `cudaEventRecord` / `cudaStreamWaitEvent`, the per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, and loud compile-time rejection of `synchronize_stream`. The suite also covers the JIT cross-stream strided-copy regression.

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```

Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

Reviewed By: desertfire

Differential Revision: D104403189
@bbus
bbus force-pushed the export-D104403189 branch 3 times, most recently from f7416d9 to c0a5f8d Compare July 1, 2026 18:43
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues. OSS `lintrunner` (noclang linter set) run locally -> clean.

Exercised end-to-end on an A100, rebased on `master`.

Full user-streams suite (JIT `torch.compile` + AOTI paths), including the new `TestAOTIUserStreams` (inline `cudaEventRecord` / `cudaStreamWaitEvent`, per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, loud rejection of `synchronize_stream`) and the JIT cross-stream strided-copy regression:

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```
Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

AOTI pinned non-blocking copy, which lowers to `torch.ops.streams.synchronize_event` (this diff emits it inline as `cudaEventSynchronize`):

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:test_aot_inductor -- test_copy_non_blocking_is_pinned_cuda
```
Result: `Tests finished: Pass 3. Fail 0.`

Reviewed By: desertfire

Differential Revision: D104403189
bbus added a commit to bbus/pytorch that referenced this pull request Jul 1, 2026
…torch#182971)

Summary:
Pull Request resolved: pytorch#182971

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues. OSS `lintrunner` (noclang linter set) run locally -> clean.

Exercised end-to-end on an A100, rebased on `master`.

Full user-streams suite (JIT `torch.compile` + AOTI paths), including the new `TestAOTIUserStreams` (inline `cudaEventRecord` / `cudaStreamWaitEvent`, per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, loud rejection of `synchronize_stream`) and the JIT cross-stream strided-copy regression:

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```
Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

AOTI pinned non-blocking copy, which lowers to `torch.ops.streams.synchronize_event` (this diff emits it inline as `cudaEventSynchronize`):

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:test_aot_inductor -- test_copy_non_blocking_is_pinned_cuda
```
Result: `Tests finished: Pass 3. Fail 0.`

Reviewed By: desertfire

Differential Revision: D104403189
…torch#182971)

Summary:
Pull Request resolved: pytorch#182971

Extending user-streams to AOT inductor. This mechanism will also be later leveraged for compiler created multi-streams (profile guided parallelism).

Leverages thread local storage without freeing for stream and event creation. Later PRs will include mechanisms to change the stream priority at creation.

**NOTE**: Internally at Meta we tightly control the host threads that run AOTI models making it favorable to have a TLS mechanism.

Authored with Claude.

Test Plan:
Lint/format: `arc lint -a` on all touched `caffe2` files -> No lint issues. OSS `lintrunner` (noclang linter set) run locally -> clean.

Exercised end-to-end on an A100, rebased on `master`.

Full user-streams suite (JIT `torch.compile` + AOTI paths), including the new `TestAOTIUserStreams` (inline `cudaEventRecord` / `cudaStreamWaitEvent`, per-thread `AOTIPerThreadStreamCache` / `AOTIPerThreadEventCache`, loud rejection of `synchronize_stream`) and the JIT cross-stream strided-copy regression:

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:user_streams
```
Result: `Tests finished: Pass 71. Fail 0. Skip 7.`

AOTI pinned non-blocking copy, which lowers to `torch.ops.streams.synchronize_event` (this diff emits it inline as `cudaEventSynchronize`):

```
buck2 test mode/opt-split-dwarf -c fbcode.use_link_groups=True -c fbcode.fbgemm_codegen_inference_mode=true -c fbcode.dwp=true -c fbcode.enable_gpu_sections=true -c fbcode.nvcc_arch=a100 -c fbcode.platform=platform010 -c fbcode.split-dwarf=true fbcode//caffe2/test/inductor:test_aot_inductor -- test_copy_non_blocking_is_pinned_cuda
```
Result: `Tests finished: Pass 3. Fail 0.`

Reviewed By: mlazos, desertfire

Differential Revision: D104403189
@meta-codesync

meta-codesync Bot commented Jul 8, 2026

Copy link
Copy Markdown

@pytorchbot merge

(Initiating merge automatically since Phabricator Diff has merged)

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants