Commit Graph

729 Commits

Author SHA1 Message Date
PyTorch MergeBot cb242afe1b Revert "Make Generator an Opaque Object (#179661)"
This reverts commit 18357d9483.

Reverted https://github.com/pytorch/pytorch/pull/179661 on behalf of https://github.com/yushangdi due to breaks pr_time_benchmarks ([comment](https://github.com/pytorch/pytorch/pull/179661#issuecomment-4285042651))
2026-04-21 00:09:01 +00:00
Shangdi Yu 18357d9483 Make Generator an Opaque Object (#179661)
### Summary

Replaces the `OpaqueGenerator` Python wrapper from  #179658 by making
`torch._C.Generator` itself a proper opaque reference type. This eliminates the
wrapper class entirely.

C++ changes to `THPGenerator`:
- Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`)
- Add weakref support (`tp_weaklistoffset` + `weakreflist` field)
- Add `tp_richcompare` (identity-based `__eq__`/`__ne__`)
- Add `tp_hash` (identity-based)
- Add `__fx_repr__` method for FX codegen

Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of
`Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now
`OpaqueBaseMeta` rather than `type`.

With these changes, `torch._C.Generator` is registered directly via
`register_opaque_type(torch._C.Generator, typ="value")` and flows through
`make_fx`, FX codegen, and the repro infrastructure without any wrapper.

Authored with Claude.

### Test plan

- `python test/dynamo/test_after_aot.py` (all 6 tests)
- `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda`
- `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness)
- ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled)
- `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes`
- `python agent_space/repro.py` (end-to-end repro)
- `python test/test_fx.py -k test_impure_random` (Generator is a C type, it has __init__ without __code__)
- `python test/dynamo/test_misc.py -k test_torch_generator_initial_seed`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/179661
Approved by: https://github.com/angelayi
2026-04-20 19:55:21 +00:00
PyTorch MergeBot 5c61b13b23 Revert "Make Generator an Opaque Object (#179661)"
This reverts commit 3fa6c02521.

Reverted https://github.com/pytorch/pytorch/pull/179661 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](https://github.com/pytorch/pytorch/pull/179661#issuecomment-4283639775))
2026-04-20 19:22:36 +00:00
Shangdi Yu 3fa6c02521 Make Generator an Opaque Object (#179661)
### Summary

Replaces the `OpaqueGenerator` Python wrapper from  #179658 by making
`torch._C.Generator` itself a proper opaque reference type. This eliminates the
wrapper class entirely.

C++ changes to `THPGenerator`:
- Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`)
- Add weakref support (`tp_weaklistoffset` + `weakreflist` field)
- Add `tp_richcompare` (identity-based `__eq__`/`__ne__`)
- Add `tp_hash` (identity-based)
- Add `__fx_repr__` method for FX codegen

Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of
`Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now
`OpaqueBaseMeta` rather than `type`.

With these changes, `torch._C.Generator` is registered directly via
`register_opaque_type(torch._C.Generator, typ="value")` and flows through
`make_fx`, FX codegen, and the repro infrastructure without any wrapper.

Authored with Claude.

### Test plan

- `python test/dynamo/test_after_aot.py` (all 6 tests)
- `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda`
- `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness)
- ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled)
- `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes`
- `python agent_space/repro.py` (end-to-end repro)
- `python test/test_fx.py -k test_impure_random` (Generator is a C type, it has __init__ without __code__)
- `python test/dynamo/test_misc.py -k test_torch_generator_initial_seed`

Pull Request resolved: https://github.com/pytorch/pytorch/pull/179661
Approved by: https://github.com/angelayi
2026-04-20 18:52:45 +00:00
cyy cf14e0ab47 Use Py_IsNone/Py_IsTrue/Py_IsFalse for singleton identity checks (#180753)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/180753
Approved by: https://github.com/Skylion007
2026-04-19 05:54:28 +00:00
Grayson Derossi 1dba3349d7 Add cuDNN depthwise convolution backend flag (#176500)
This change was separated from #170609, and adds a context flag to select the backend kernel for depthwise convolution. This allows easy benchmarking of cuDNN performance relative to fallback performance for heuristic tuning (and could also be used for debugging), while maintaining default behavior for standard use.

There are three options: `["auto", "cudnn", "native"]`. The `"auto"` option chooses a kernel based on the existing heuristic function. The `"cudnn"` and `"native"` flags skip the heuristic, instead always dispatching to a cuDNN kernel or a fallback kernel, respectively.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/176500
Approved by: https://github.com/malfet, https://github.com/eqy
2026-04-17 08:41:04 +00:00
Grayson Derossi d61bcb2575 [CUDA][cuBLAS] Add get/set/reset functions for BLAS workspaces (#177912)
This adds Python functions to get, set, and reset cuBLAS or cuBLASLt workspace sizes. Workspaces are updated lazily the next time they are accessed if the new workspace size is greater than the current size.

@ngimel, is this what you were hoping for? I believe the current BLAS backend can already be queried with `torch.backends.cuda.preferred_blas_library`.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/177912
Approved by: https://github.com/ngimel, https://github.com/malfet, https://github.com/eqy
2026-04-16 00:11:46 +00:00
PyTorch MergeBot cc8d1bdb03 Revert "Make Generator an Opaque Object (#179661)"
This reverts commit 4510a721a2.

Reverted https://github.com/pytorch/pytorch/pull/179661 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](https://github.com/pytorch/pytorch/pull/179661#issuecomment-4217993074))
2026-04-09 22:18:36 +00:00
Shangdi Yu 4510a721a2 Make Generator an Opaque Object (#179661)
### Summary

Replaces the `OpaqueGenerator` Python wrapper from  #179658 by making
`torch._C.Generator` itself a proper opaque reference type. This eliminates the
wrapper class entirely.

C++ changes to `THPGenerator`:
- Set `OpaqueBaseMeta` as the metaclass (same pattern as `ProcessGroup`)
- Add weakref support (`tp_weaklistoffset` + `weakreflist` field)
- Add `tp_richcompare` (identity-based `__eq__`/`__ne__`)
- Add `tp_hash` (identity-based)
- Add `__fx_repr__` method for FX codegen

Also fixes `THPModule_addDocStr` to use `PyType_Check(obj)` instead of
`Py_TYPE(obj) == &PyType_Type`, since Generator's metaclass is now
`OpaqueBaseMeta` rather than `type`.

With these changes, `torch._C.Generator` is registered directly via
`register_opaque_type(torch._C.Generator, typ="value")` and flows through
`make_fx`, FX codegen, and the repro infrastructure without any wrapper.

Authored with Claude.

### Test plan

- `python test/dynamo/test_after_aot.py` (all 6 tests)
- `python test/inductor/test_torchinductor.py -k test_randn_generator_cuda`
- `python test/test_opaque_obj_v2.py TestOpaqueGenerator` (make_fx + correctness)
- ` python test/test_torch.py -k test_pickle_generator` (generator can be pickled)
- `python test/dynamo/test_dynamic_shapes.py -k test_user_getattr1_dynamic_shapes`
- `python agent_space/repro.py` (end-to-end repro)
- `python test/test_fx.py -k test_impure_random` (Generator is a C type, it has __init__ without __code__)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/179661
Approved by: https://github.com/angelayi
ghstack dependencies: #179657
2026-04-09 21:17:34 +00:00
Nikita Shulga 50d4815bd4 Remove TorchVitals (#178479)
TorchVitals was an undocumented feature gated behind the TORCH_VITAL
environment variable that allowed setting and reading key-value "vital signs" (e.g. whether CUDA or DataLoader was used). It was never documented and the test itself had a "FIXME: document or deprecate" comment.

Originally added in PR #51047 (Jan 2021), with a Python API in #53238 and a CUDA vital in #58059. There have been no feature changes since June 2021 — all subsequent commits were mechanical cleanups (clang-tidy,
thread-safe getenv, etc.).

Update: `torch.set_vital` was used from TorchData (see https://github.com/meta-pytorch/data/pull/1537 ) so will keep the dummy API around

Authored with Claude.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/178479
Approved by: https://github.com/msaroufim, https://github.com/albanD, https://github.com/mlazos
2026-04-03 06:19:18 +00:00
PyTorch MergeBot 3f18f3f5ba Revert "[ROCm] Add hipDNN backend support for convolution (#178515)"
This reverts commit 3a52b28751.

Reverted https://github.com/pytorch/pytorch/pull/178515 on behalf of https://github.com/izaitsevfb due to breaks internal builds ([comment](https://github.com/pytorch/pytorch/pull/178515#issuecomment-4180671650))
2026-04-02 21:50:15 +00:00
Rob Timpe f6ef243ef8 Implement thread-local config with contextvars (#173568)
This is an alternative approach to https://github.com/pytorch/pytorch/issues/118387 using context vars

Pull Request resolved: https://github.com/pytorch/pytorch/pull/173568
Approved by: https://github.com/williamwen42
ghstack dependencies: #178950
2026-04-01 22:20:03 +00:00
zjgarvey 3a52b28751 [ROCm] Add hipDNN backend support for convolution (#178515)
This PR adds [hipDNN](https://github.com/ROCm/rocm-libraries/tree/develop/projects/hipdnn) convolution support to PyTorch on ROCm, following the newer dispatch mechanism which no longer requires per-backend frontend op registration.

New ROCm kernel providers will be able to register plugins and ship faster kernels through hipDNN without touching PyTorch code. The graph-based API of hipDNN also supports fused kernels. The eventual goal is to replace all MIOpen-specific PyTorch code with hipDNN, wherein MIOpen will be just one of multiple first-class kernel providers.

# Note

This PR is being opened parallel to #177534 in anticipation that batch norm's dispatch migration issues might stall the review process for hipdnn as a whole.

# Summary

- Integrates the lightweight hipDNN frontend graph API as an alternative convolution backend to MIOpen on ROCm, covering forward, backward-data, and backward-weight for 2D/3D convolutions including transposed variants.

- Adds cmake `USE_HIPDNN` detection, `torch.backends.hipdnn` Python module with `enabled`/`flags()` context manager, and `compiledWithHipDNN()` runtime hook.

- Implements a generic thread-local `ParamsLRUCache` to amortize hipDNN `graph->build()` cost across repeated convolution calls with identical parameters.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/178515
Approved by: https://github.com/jeffdaily

Co-authored-by: Dmitry Nikolaev <dmitry.nikolaev@amd.com>
2026-04-01 20:30:55 +00:00
Haoyu Zhang 4f6f0da0a9 Reland D97799113: [CK SDPA] Add CK backend support for varlen attention (#178322) (#178322) (#178729)
Summary:
CONTEXT: D97799113 was reverted by D98681402. This relands the original change.

Port GitHub PR https://github.com/pytorch/pytorch/issues/172246 to internal. This adds composable_kernel (CK) backend
support for scaled-dot-product-attention varlen path on ROCm:

- Fix philox seed/offset access in varlen backward to guard on is_dropout
- Fix softmax_lse shape and stride for CK varlen forward path
- Add `is_ck_sdpa_available()` API to `torch.backends.cuda`
- Update varlen_attn fake to use correct logsumexp shape per backend
- Add CK backend parametrization to varlen attention tests

Co-authored-by: Chinmay Kuchinad ChinmayDattanand.Kuchinad@amd.com
Co-authored-by: Jeff Daily jeff.daily@amd.com

Test Plan: buck test on relevant test targets

Differential Revision: D98693429

Pulled By: haoyuz

Pull Request resolved: https://github.com/pytorch/pytorch/pull/178729
Approved by: https://github.com/echen4096
2026-04-01 15:16:53 +00:00
Sam Gross 3eeaf56b00 Make LogAPIUsageOnceFromPython thread-safe for free-threaded Python (#178867)
The old implementation used a static std::unordered_set protected only by the GIL. Under free-threaded Python (3.14t) this is a data race.

Replace it with a Python dict stored in per-module state. The hot path (already-seen event) calls PyDict_Contains which is lock-free in 3.14t. The cold path (first occurrence) uses PyDict_SetDefaultRef which uses CPython's per-dict fine-grained locking to atomically check-and-insert, avoiding TOCTOU races between threads.

Authored with Claude.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/178867
Approved by: https://github.com/Skylion007
2026-04-01 14:58:03 +00:00
PyTorch MergeBot 537b1955c1 Revert "Remove TorchVitals (#178479)"
This reverts commit 179e2d57a9.

Reverted https://github.com/pytorch/pytorch/pull/178479 on behalf of https://github.com/georgehong due to change is breaking internal tests: AttributeError: module 'torch' has no attribute 'set_vital' ([comment](https://github.com/pytorch/pytorch/pull/178479#issuecomment-4145885127))
2026-03-27 23:25:57 +00:00
Nikita Shulga 179e2d57a9 Remove TorchVitals (#178479)
TorchVitals was an undocumented feature gated behind the TORCH_VITAL
environment variable that allowed setting and reading key-value "vital
signs" (e.g. whether CUDA or DataLoader was used). It was never
documented and the test itself had a "FIXME: document or deprecate"
comment.

Originally added in PR #51047 (Jan 2021), with a Python API in #53238
and a CUDA vital in #58059. There have been no feature changes since
June 2021 — all subsequent commits were mechanical cleanups (clang-tidy,
thread-safe getenv, etc.).

A search on cs.github.com and general web search confirmed no external
projects depend on torch.set_vital, torch.read_vitals, or
torch.vitals_enabled — usage was limited to pytorch/pytorch itself.

Authored with Claude.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

Pull Request resolved: https://github.com/pytorch/pytorch/pull/178479
Approved by: https://github.com/msaroufim, https://github.com/albanD
2026-03-26 22:00:52 +00:00
PyTorch MergeBot 554573d861 Revert "Implement thread-local config with contextvars (#173568)"
This reverts commit c904bdcf76.

Reverted https://github.com/pytorch/pytorch/pull/173568 on behalf of https://github.com/georgehong due to tests failing with torch._dynamo hit config.recompile_limit (8) ([comment](https://github.com/pytorch/pytorch/pull/173568#issuecomment-4129118162))
2026-03-25 19:17:48 +00:00
rraminen 120a341a01 [ROCm] Enable hipSPARSELt UTs (#178285)
This PR:

- Enables torch.backends.cusparselt.is_available() on ROCm
- Enables hiSPARSELt UTs on ROCm
- Skip the following UTs on ROCm
```
test_mm_sparse_first_NN_backend_cusparselt_dense_input_shape0_cuda_int8
test_mm_sparse_first_NN_backend_cusparselt_dense_input_shape1_cuda_int8
test_mm_sparse_first_NN_backend_cusparselt_dense_input_shape2_cuda_int8
test_mm_sparse_first_NT_backend_cusparselt_dense_input_shape0_cuda_int8
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/178285
Approved by: https://github.com/jeffdaily
2026-03-24 22:47:05 +00:00
Rob Timpe c904bdcf76 Implement thread-local config with contextvars (#173568)
This is an alternative approach to https://github.com/pytorch/pytorch/issues/118387 using context vars

Pull Request resolved: https://github.com/pytorch/pytorch/pull/173568
Approved by: https://github.com/williamwen42
2026-03-24 17:09:50 +00:00
PyTorch MergeBot 939efd00d8 Revert "[ROCm][CK] Enable variable-length attention support for CK SDPA backend (#172246)"
This reverts commit e1b7388686.

Reverted https://github.com/pytorch/pytorch/pull/172246 on behalf of https://github.com/huydhn due to sorry, failing builds with older HIP versions, see below ([comment](https://github.com/pytorch/pytorch/pull/172246#issuecomment-4033763552))
2026-03-10 19:13:24 +00:00
PyTorch MergeBot 2ee3377bb7 Revert "[fx] Move _Namespace to C++ (#170962)"
This reverts commit 5c68844a2a.

Reverted https://github.com/pytorch/pytorch/pull/170962 on behalf of https://github.com/wdvr due to reverted in fbcode ([comment](https://github.com/pytorch/pytorch/pull/170962#issuecomment-4027702285))
2026-03-10 00:00:56 +00:00
Chinmay Kuchinad e1b7388686 [ROCm][CK] Enable variable-length attention support for CK SDPA backend (#172246)
## Summary

Enables variable-length (varlen) attention support for the Composable Kernel (CK) SDPA backend on ROCm.

## Changes

### Forward pass (`mha_varlen_fwd_ck.hip`)

- Fixed LSE tensor allocation: changed from 3D `{batch_size, num_heads, max_seqlen_q}` to 2D `{num_heads, total_q}` to match CK group mode expectation
- Fixed `nhead_stride_lse`: use `stride(0)` instead of `stride(1)` for 2D layout
- Fixed `batch_stride_lse`: set to `0` (no batch dimension in group mode LSE)
- Fixed `min_seqlen_q`: changed from `-1` to `1` (valid minimum for kernel dispatch)

### Backward pass (`mha_varlen_bwd_ck.hip`)

- Fixed philox seed/offset access: guarded with `if (is_dropout)` to avoid dtype mismatch when dropout is disabled

### Test infrastructure (`test/test_varlen_attention.py`)

- Added `sdpa_backend` parametrization to test both `aotriton` and `ck` backends on ROCm
- Backend selection: `["aotriton", "ck"]` when CK is available, `["aotriton"]` otherwise
- Uses `preferred_rocm_fa_library()` to switch backends per test

### Platform detection

- Added `PLATFORM_SUPPORTS_CK_SDPA` in `torch/testing/_internal/common_cuda.py`
- Added `_is_ck_sdpa_available()` in `torch/csrc/Module.cpp` for runtime CK availability check

### Fake tensor implementation (`torch/nn/attention/varlen.py`)

- Updated `_varlen_attn_fake` to use standard `[num_heads, total_q]` logsumexp format (matches aotriton/CUDA)

## Dependencies: CK submodule bump

- **CK PR**: (https://github.com/ROCm/rocm-libraries/pull/4292)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/172246
Approved by: https://github.com/jeffdaily
2026-03-04 00:28:02 +00:00
PyTorch MergeBot 0a06050b14 Revert "[fx] Move _FindNodesLookupTable and _node_list to C++ (#170973)"
This reverts commit 6a78d3dff7.

Reverted https://github.com/pytorch/pytorch/pull/170973 on behalf of https://github.com/facebook-github-bot due to Diff reverted internally ([comment](https://github.com/pytorch/pytorch/pull/170973#issuecomment-3980822092))
2026-03-01 19:20:43 +00:00
Jason Ansel 6a78d3dff7 [fx] Move _FindNodesLookupTable and _node_list to C++ (#170973)
Cumulative Changes (PR 170946 → PR 170973)

  | Benchmark                             | PR 170946 | PR 170973 | Delta |
  |---------------------------------------|-----------|-----------|-------|
  | add_loop_eager                        | 3,308M    | 3,206M    | -3.1% |
  | aotdispatcher_partitioner_cpu2        | 1,944M    | 1,865M    | -4.1% |
  | aotdispatcher_partitioner_cpu         | 8,340M    | 8,062M    | -3.3% |
  | sum_floordiv_regression               | 3,700M    | 3,591M    | -3.0% |
  | aotdispatcher_training_nosubclass_cpu | 3,242M    | 3,168M    | -2.3% |
  | update_hint_regression                | 1,674M    | 1,641M    | -2.0% |
  | symint_sum_loop                       | 4,682M    | 4,590M    | -2.0% |
  | add_loop_inductor_gpu                 | 26,418M   | 26,031M   | -1.5% |
  | basic_NestedModule_eager              | 6,171M    | 6,084M    | -1.4% |
  | add_loop_inductor                     | 29,736M   | 29,342M   | -1.3% |

Pull Request resolved: https://github.com/pytorch/pytorch/pull/170973
Approved by: https://github.com/oulgen
ghstack dependencies: #170946, #170962
2026-02-26 17:48:17 +00:00
Jason Ansel 5c68844a2a [fx] Move _Namespace to C++ (#170962)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/170962
Approved by: https://github.com/oulgen
ghstack dependencies: #170946
2026-02-26 17:48:17 +00:00
Kushagra Rastogi 4f3d429973 Raise Warning Once when invariants check is disabled (#171695)
Fixes #170626

Pull Request resolved: https://github.com/pytorch/pytorch/pull/171695
Approved by: https://github.com/pearu, https://github.com/isuruf
2026-02-11 16:21:39 +00:00
Chinmay Kuchinad 03b8651970 [ROCm] Enable StaticCudaLauncher for ROCm (#166492)
This PR enables ROCm/HIP support for PyTorch's StaticCudaLauncher, which provides static compilation and launching of Triton kernels. The implementation has been tested on AMD MI300 and MI200 hardware.

**Changes**

**Python (torch/_inductor/runtime/)**
- static_cuda_launcher.py: Added ROCm detection, .hsaco binary support, and ROCm-specific scratch parameter handling
- triton_heuristics.py: Updated device type checks to support both cuda and hip

**C++ (torch/csrc/)**
- Module.cpp: Enabled StaticCudaLauncher for ROCm builds
- inductor/static_cuda_launcher.cpp: Added HIP API equivalents for all CUDA driver calls
- inductor/static_cuda_launcher.h: Updated header guard

**Tests (test/inductor/)**
- test_static_cuda_launcher.py: Removed @skipIfRocm decorators and updated binary file handling

**Enabled Unit Tests**
All tests in test/inductor/test_static_cuda_launcher.py now pass on ROCm:
1. Fixes #168654.
2. Fixes #168655.
3. Fixes #168656.
4. Fixes #168657.
5. Fixes #168658.
6. Fixes #168659.
7. Fixes #168660.
8. Fixes #168661.
9. Fixes #168662.
10. Fixes #168663.
11. Fixes #168664.
12. Fixes #168666
13. Fixes #168667
14. Fixes #168668
15. Fixes #168669
16. Fixes #168670
17. Fixes #168671
18. Fixes #168575

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166492
Approved by: https://github.com/jeffdaily
2026-02-03 17:01:30 +00:00
Ma, Jing1 b71eaf9fb0 [1/2][XPU] Implement XPUGraph capture and replay (#166285)
XPUGraph PR submission plan, following this [RFC](https://github.com/pytorch/pytorch/issues/162143).

- [x] 1, enhance XPUGenerator to cover XPUGeneratorState and philoxState
- [x] 2, the implementation of MemPool in XPU device allocator
- [ ] ->3, the implementations of capture_begin/capture_end/instantiate for XPUGraph
- [ ] 4, the implementations of replay/debug_dump/reset for XPUGraph
- [ ] 5, XPUGraph python APIs: is_current_stream_capturing/graph_pool_handle/graph
- [ ] 6, XPUGraph python APIs: make_graphed_callables

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166285
Approved by: https://github.com/EikanWang, https://github.com/gujinghui, https://github.com/eellison, https://github.com/guangyey
2026-01-31 12:45:42 +00:00
xinan.lin 84b7563c37 [xpu][feature][static_launcher] 3/4: Add xpu static launcher cpp module. (#168952)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/168952
Approved by: https://github.com/EikanWang, https://github.com/desertfire
ghstack dependencies: #169121
2026-01-26 07:03:14 +00:00
Ma, Jing1 f9e35b5409 [3/3][XPU][feature] The frontend python APIs for XPU MemPool (#166843)
The implementation plan of MemPool for XPU, which is the dependance of [XPUGraph](https://github.com/pytorch/pytorch/pull/166285), following the [RFC](https://github.com/pytorch/pytorch/issues/162143).

- [x] #166831
- [x] #166833
- [ ] ->#166843

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166843
Approved by: https://github.com/EikanWang, https://github.com/gujinghui, https://github.com/eellison
2026-01-24 09:32:23 +00:00
xinan.lin 4ec170a846 [xpu][static_launcher] 2/4: Refactor static cuda launcher to static triton launcher. (#169121)
This PR refactor the cuda static launcher to triton static launcher to be able to be reused by XPU triton backend as follow:
- Rename class `StaticallyLaunchedCudaKernel` -> class `StaticallyLaunchedTritonKernel`, and then keep `StaticallyLaunchedCudaKernel` inherit the  `StaticallyLaunchedTritonKernel`. Then there will be `StaticallyLaunchedXpuKernel`.
- Rename static_cuda_launcher.py -> static_triton_launcher.py
- Rename torch/csrc/inductor/static_cuda_launcher.cpp -> torch/csrc/inductor/static_launcher/cuda.cpp
- Rename test/_inductor/test_static_cuda_launcher.py -> test/_inductor/test_static_triton_launcher.py

Pull Request resolved: https://github.com/pytorch/pytorch/pull/169121
Approved by: https://github.com/EikanWang, https://github.com/desertfire
2026-01-23 17:23:19 +00:00
Howard Zhang 6407e23a77 Add FA3 to SDPA (#172040)
### Summary:
Added support for flash attention v3 to SDPA
Only supports fp8 forward pass for now
Created new `_fa3.py` mirroring `_fa4.py` registration
modified `torch/nn/attention/__init__.py` and `_registry.py` to add FA3 support
Added C++ hook to expose FA3 activation (to allow for fp8 dtype)

### Design:
- The design follows the same basic structure as the [FA4 implementation](https://github.com/pytorch/pytorch/pull/167348).
- I added a `_fa3.py` which mirrors the same registration framework as `_fa4.py` (wiring up the new fa3 impl with aten ops `_flash_attention_forward`, `_flash_attention_backward`, `_scaled_dot_product_flash_attention`, `_scaled_dot_product_flash_attention_backward`)
- I additionally added a C++ hook to expose the FA3 activation to `sdp_utils.cpp`, and added a new function to check for low precision dtypes in flash attention (since before fp8 was not allowed)
- Note that only fp8 forward pass is supported for now. We can add fp16/bf16 support later, but fp16/bf16 performance with FA4 is better anyway. The backwards pass currently will throw an error: "FA3 does not support backward pass. Either: 1. Use torch.no_grad() for inference. 2. Unregister FA3 before training: `restore_flash_attention_impl`"

### Test Plan:
Install FA3 library: https://github.com/Dao-AILab/flash-attention/tree/main, follow directions for "FlashAttention-3 beta release", tldr; clone the repo, cd into hopper (where the FA3 implementation lives), python setup.py install

Following the same steps as FA4 to keep it consistent:
```python
activate_flash_attention_impl("FA3")
with sdpa_kernel(SDPBackend.FLASH_ATTENTION):
        _ = F.scaled_dot_product_attention(q, k, v)
```
It also works with torch.compile
```python
activate_flash_attention_impl("FA3")
def sdpa_fn(q, k, v):
      with sdpa_kernel(SDPBackend.FLASH_ATTENTION):
          return F.scaled_dot_product_attention(q, k, v)

compiled_fn = torch.compile(sdpa_fn, fullgraph=True)
_ = compiled_fn(q, k, v)
```

### Some quick runtime results:
I ran some very quick tests with the pytorch profiler to test the runtime of fp8 using FA3 versus bf16 using FA4. Mostly just to catch any easy mistakes slowing it down. These are the results when running on (8, 16, 1024, 128) shape tensors:
FA3 eager:
[sdpa_profiler_fa3_eager.txt](https://github.com/user-attachments/files/24511657/sdpa_profiler_fa3_eager.txt)
FA3 compile:
[sdpa_profiler_fa3_compile.txt](https://github.com/user-attachments/files/24511664/sdpa_profiler_fa3_compile.txt)
FA4 eager:
[sdpa_profiler_fa4_eager.txt](https://github.com/user-attachments/files/24511667/sdpa_profiler_fa4_eager.txt)
FA4 compile:
[sdpa_profiler_fa4_compile.txt](https://github.com/user-attachments/files/24511669/sdpa_profiler_fa4_compile.txt)

### Some quick accuracy results:
I ran a very quick SQNR test on (8, 16, 1024, 128) shape tensors 100 times. I got an average SQNR of 25.55.

This is still a draft, so I'll be doing a quick test with the memory next. Then, I'll do some tests on the diffusers library stable diffusion models.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/172040
Approved by: https://github.com/drisspg
2026-01-22 23:43:56 +00:00
PyTorch MergeBot a01bb47a0c Revert "[xpu][static_launcher] 2/4: Refactor static cuda launcher to static triton launcher. (#169121)"
This reverts commit 84646f716d.

Reverted https://github.com/pytorch/pytorch/pull/169121 on behalf of https://github.com/yangw-dev due to break internal system, and casued SEV, please reachout internal staff for more details ([comment](https://github.com/pytorch/pytorch/pull/169121#issuecomment-3760882742))
2026-01-16 16:41:03 +00:00
PyTorch MergeBot 681d49c234 Revert "[xpu][feature][static_launcher] 3/4: Add xpu static launcher cpp module. (#168952)"
This reverts commit 96c7aa5c43.

Reverted https://github.com/pytorch/pytorch/pull/168952 on behalf of https://github.com/yangw-dev due to break internal system, and casued SEV, please reachout internal staff for more details ([comment](https://github.com/pytorch/pytorch/pull/169121#issuecomment-3760882742))
2026-01-16 16:41:03 +00:00
PyTorch MergeBot ff8dd55e0d Revert "[ROCm] Enable StaticTritonLauncher for ROCm (#166492)"
This reverts commit d05e0f03df.

Reverted https://github.com/pytorch/pytorch/pull/166492 on behalf of https://github.com/yangw-dev due to sorry, a previous commit introduce the new test file breaks internal system, please rebase after https://github.com/pytorch/pytorch/pull/169121 's revert and reland ([comment](https://github.com/pytorch/pytorch/pull/166492#issuecomment-3760844167))
2026-01-16 16:30:59 +00:00
Chinmay Kuchinad d05e0f03df [ROCm] Enable StaticTritonLauncher for ROCm (#166492)
This PR enables ROCm/HIP support for PyTorch's StaticCudaLauncher, which provides static compilation and launching of Triton kernels. The implementation has been tested on AMD MI300 and MI200 hardware.

**Changes**

**Python (torch/_inductor/runtime/)**
- static_cuda_launcher.py: Added ROCm detection, .hsaco binary support, and ROCm-specific scratch parameter handling
- triton_heuristics.py: Updated device type checks to support both cuda and hip

**C++ (torch/csrc/)**
- Module.cpp: Enabled StaticCudaLauncher for ROCm builds
- inductor/static_cuda_launcher.cpp: Added HIP API equivalents for all CUDA driver calls
- inductor/static_cuda_launcher.h: Updated header guard

**Tests (test/inductor/)**
- test_static_cuda_launcher.py: Removed @skipIfRocm decorators and updated binary file handling

**Enabled Unit Tests**
All tests in test/inductor/test_static_cuda_launcher.py now pass on ROCm:
1. Fixes #168654.
2. Fixes #168655.
3. Fixes #168656.
4. Fixes #168657.
5. Fixes #168658.
6. Fixes #168659.
7. Fixes #168660.
8. Fixes #168661.
9. Fixes #168662.
10. Fixes #168663.
11. Fixes #168664.
12. Fixes #168666
13. Fixes #168667
14. Fixes #168668
15. Fixes #168669
16. Fixes #168670
17. Fixes #168671
18. Fixes #168575

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166492
Approved by: https://github.com/jeffdaily, https://github.com/huydhn, https://github.com/atalman
2026-01-13 21:41:24 +00:00
PyTorch MergeBot 1aa99d6064 Revert "[ROCm] SDPA fix mem fault when dropout is enabled (#154864)"
This reverts commit fd27eb7b00.

Reverted https://github.com/pytorch/pytorch/pull/154864 on behalf of https://github.com/yangw-dev due to already unland internally since it's broken internal tests, ticket: T251298521 ([comment](https://github.com/pytorch/pytorch/pull/154864#issuecomment-3745570638))
2026-01-13 17:36:20 +00:00
xinan.lin 96c7aa5c43 [xpu][feature][static_launcher] 3/4: Add xpu static launcher cpp module. (#168952)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/168952
Approved by: https://github.com/EikanWang, https://github.com/desertfire
ghstack dependencies: #169121
2026-01-12 01:43:01 +00:00
xinan.lin 84646f716d [xpu][static_launcher] 2/4: Refactor static cuda launcher to static triton launcher. (#169121)
This PR refactor the cuda static launcher to triton static launcher to be able to be reused by XPU triton backend as follow:
- Rename class `StaticallyLaunchedCudaKernel` -> class `StaticallyLaunchedTritonKernel`, and then keep `StaticallyLaunchedCudaKernel` inherit the  `StaticallyLaunchedTritonKernel`. Then there will be `StaticallyLaunchedXpuKernel`.
- Rename static_cuda_launcher.py -> static_triton_launcher.py
- Rename torch/csrc/inductor/static_cuda_launcher.cpp -> torch/csrc/inductor/static_launcher/cuda.cpp
- Rename test/_inductor/test_static_cuda_launcher.py -> test/_inductor/test_static_triton_launcher.py

Pull Request resolved: https://github.com/pytorch/pytorch/pull/169121
Approved by: https://github.com/EikanWang, https://github.com/desertfire
2026-01-12 01:42:55 +00:00
Andy Lugo fd27eb7b00 [ROCm] SDPA fix mem fault when dropout is enabled (#154864)
Fixes issue that exhibited a device side memory access fault due to incorrect tensor life management

Pull Request resolved: https://github.com/pytorch/pytorch/pull/154864
Approved by: https://github.com/atalman
2026-01-09 19:59:44 +00:00
PyTorch MergeBot e91b428f82 Revert "[fx] Move _Namespace to C++ (#170962)"
This reverts commit a50d65ad2f.

Reverted https://github.com/pytorch/pytorch/pull/170962 on behalf of https://github.com/clee2000 due to sorry but I think this broke a test on inductor test/inductor/test_max_autotune.py::TestMaxAutotuneAsyncPipelined::test_empty_conv_input_search_space_EXHAUSTIVE_kernel_size_1 [GH job link](https://github.com/pytorch/pytorch/actions/runs/20510247600/job/58931365661) [HUD commit link](https://hud.pytorch.org/pytorch/pytorch/commit/a50d65ad2f8c39a19be6ee3d7e0b37b0f3f47189)? it might be a landrace since I see that it did succeed on this PR. Also its failing some tests internally D89800849 ([comment](https://github.com/pytorch/pytorch/pull/170946#issuecomment-3693134351))
2025-12-26 17:21:57 +00:00
Jason Ansel a50d65ad2f [fx] Move _Namespace to C++ (#170962)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/170962
Approved by: https://github.com/oulgen
ghstack dependencies: #170945, #170946
2025-12-25 20:03:30 +00:00
Kathryn-cat 8da5d29de7 [DLPack] C Functions for DLPack Speed Exchange and Stream Handling (#165483)
## Addressed Issue

Issue #162845

## Summary of Changes

This PR introduces a unified `DLPackExchangeAPI` struct as described in proposal [175](https://github.com/dmlc/dlpack/issues/175). This new convention replaces the previous mechanism of separate function pointers, and aligns with the latest DLPack standard as shown in PR [174](https://github.com/dmlc/dlpack/pull/174).

Specifically, the new `DLPackExchangeAPI` struct is exposed as `torch.Tensor.__c_dlpack_exchange_api__`, which stores and exposes the following function pointers:

* `managed_tensor_allocator`
* `managed_tensor_from_py_object_no_sync`
* `managed_tensor_to_py_object_no_sync`
* `dltensor_from_py_object_no_sync`
* `current_work_stream`

Within the new `DLPackExchangeAPI` struct, the new `current_work_stream` function pointer allows more robust and integrated querying of the current device stream (e.g., CUDA stream) during DLPack tensor exchanges. All the conversion from/to DLPack has been updated to `_no_sync`, meaning you should use `current_work_stream` to explicitly handle stream synchronization. It also includes a non-owning DLTensor conversion `dltensor_from_py_object_no_sync` to avoid unnecessary reference counting.

Following this change, the `dlpack.h` has been updated to the latest DLPack.

Unit tests are added using `torch.utils.cpp_extension.load_inline` to avoid GIL release issues
when calling `THPVariable_Wrap`.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/165483
Approved by: https://github.com/tqchen, https://github.com/albanD
2025-12-04 22:52:41 +00:00
PyTorch MergeBot 6f4baa5abe Revert "[ROCm] Enable StaticCudaLauncher for ROCm (#166492)"
This reverts commit 18f3ca08f1.

Reverted https://github.com/pytorch/pytorch/pull/166492 on behalf of https://github.com/huydhn due to Sorry for reverthing this but the change has been reverted by an internal meta team ([comment](https://github.com/pytorch/pytorch/pull/166492#issuecomment-3614162658))
2025-12-04 20:17:50 +00:00
fengqing.lu bc6a4863c7 [xpu][feature][2/N]Enable SDPA XPU FlashAttention backend with SYCL-TLA implementation (#167057)
This is a PR to upstream [SYCL-TLA](https://github.com/intel/sycl-tla) version FlashAttention for Pytorch XPU.

This is the second PR to register SYCL-TLA version FlashAttention forward/backward xpu kernels into SDPA's FlashAttention XPU backend.

PR stacks:
- https://github.com/pytorch/pytorch/pull/169101
- https://github.com/pytorch/pytorch/pull/167057

Currently, we support Intel Ponte Vecchio and Battlemage on Linux. In terms of other platform support, we are WIP.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/167057
Approved by: https://github.com/EikanWang, https://github.com/drisspg

Co-authored-by: Eikan Wang <eikan.wang@intel.com>
2025-12-03 21:04:05 +00:00
Chinmay Kuchinad 18f3ca08f1 [ROCm] Enable StaticCudaLauncher for ROCm (#166492)
This PR enables ROCm/HIP support for PyTorch's StaticCudaLauncher, which provides static compilation and launching of Triton kernels. The implementation has been tested on AMD MI300 and MI200 hardware.

**Changes**

**Python (torch/_inductor/runtime/)**
- static_cuda_launcher.py: Added ROCm detection, .hsaco binary support, and ROCm-specific scratch parameter handling
- triton_heuristics.py: Updated device type checks to support both cuda and hip

**C++ (torch/csrc/)**
- Module.cpp: Enabled StaticCudaLauncher for ROCm builds
- inductor/static_cuda_launcher.cpp: Added HIP API equivalents for all CUDA driver calls
- inductor/static_cuda_launcher.h: Updated header guard

**Tests (test/inductor/)**
- test_static_cuda_launcher.py: Removed @skipIfRocm decorators and updated binary file handling

**Enabled Unit Tests**
All tests in test/inductor/test_static_cuda_launcher.py now pass on ROCm:
1. test_basic
2. test_unsigned_integers
3. test_signed_integers
4. test_basic_1arg
5. test_constexpr
6. test_implied_constant
7. test_kernel_no_args
8. test_high_shared_mem
9. test_too_high_shared_mem
10. test_kernel_empty_tensor
11. test_kernel_many_args
12. test_basic_compile
13. test_incompatible_code
14. test_static_launch_user_defined_triton_kernels
15. test_empty_tensor
16. test_any
17. test_disable_static_cuda_launcher

In addition to this, the following tests from test/inductor/test_codecache.py also pass:
1. test_remote_cache_load_function_device_cuda_float32_dynamic_False_bundle_triton_False_use_static_cuda_launcher_False
2. test_remote_cache_load_function_device_cuda_float32_dynamic_False_bundle_triton_True_use_static_cuda_launcher_False
3. test_remote_cache_load_function_device_cuda_float32_dynamic_False_bundle_triton_True_use_static_cuda_launcher_True
4. test_remote_cache_load_function_device_cuda_bfloat16_dynamic_False_bundle_triton_False_use_static_cuda_launcher_False
5. test_remote_cache_load_function_device_cuda_bfloat16_dynamic_False_bundle_triton_True_use_static_cuda_launcher_False
6. test_remote_cache_load_function_device_cuda_bfloat16_dynamic_False_bundle_triton_True_use_static_cuda_launcher_True

The following tests are skipped since triton bundling is necessary for StaticCudaLauncher:
1. test_remote_cache_load_function_device_cuda_float32_dynamic_False_bundle_triton_False_use_static_cuda_launcher_True
2. test_remote_cache_load_function_device_cuda_bfloat16_dynamic_False_bundle_triton_False_use_static_cuda_launcher_True

Pull Request resolved: https://github.com/pytorch/pytorch/pull/166492
Approved by: https://github.com/jeffdaily
2025-12-03 16:49:11 +00:00
Yuanyuan Chen 5605fce2c8 Improve char printing (#167899)
This PR outputs chars to stream without building temporary strings.
They were modified by (on fish)
```
sed  -i -e 's/<< "\([^\\\']\)"/<< \'\1\'/g' (grep '<< "."' -r torch c10 aten -l)
```
and revert some invalid changes.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/167899
Approved by: https://github.com/Skylion007
2025-11-18 14:31:49 +00:00
PyTorch MergeBot ae3ce54f27 Revert "[ROCm] Enable StaticCudaLauncher for ROCm (#166492)"
This reverts commit 99fdca8f4d.

Reverted https://github.com/pytorch/pytorch/pull/166492 on behalf of https://github.com/jeanschmidt due to Internally we still depends on the old logic, so we need to find a way to maintain backwards compatibility, for now ([comment](https://github.com/pytorch/pytorch/pull/166492#issuecomment-3543198811))
2025-11-17 17:59:42 +00:00
PyTorch MergeBot 22ccd44d73 Revert "Improve char printing (#167899)"
This reverts commit 2245d7d3b9.

Reverted https://github.com/pytorch/pytorch/pull/167899 on behalf of https://github.com/jeanschmidt due to need to revert in order to revert https://github.com/pytorch/pytorch/pull/167899 ([comment](https://github.com/pytorch/pytorch/pull/167899#issuecomment-3542869096))
2025-11-17 16:46:44 +00:00