Skip to content

[inductor] Force contiguous inputs for custom ops in FallbackKernel - #179422

Closed
abaybektursun wants to merge 1 commit into
pytorch:mainfrom
abaybektursun:fix/inductor-custom-op-reinterpret-tensor
Closed

abaybektursun wants to merge 1 commit into
pytorch:mainfrom
abaybektursun:fix/inductor-custom-op-reinterpret-tensor

Conversation

@abaybektursun

@abaybektursun abaybektursun commented Apr 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Custom C++ ops registered via torch.library that call .data_ptr() on their inputs crash when torch.compile is used, because Inductor passes reinterpret_tensor views that lack accessible storage.

Works on PyTorch 2.9.1, crashes on 2.11.0.

Error

RuntimeError: Cannot access data pointer of Tensor that doesn't have storage
  Exception raised from throw_data_ptr_access_error at c10/core/TensorImpl.cpp:307

Root Cause

FallbackKernel.process_kernel() calls realize_input() which may return ReinterpretView nodes. Internal ops (aten, prims, etc.) handle views natively, but custom C++ ops typically call .data_ptr() directly, which fails on storage-less views.

Between PyTorch 2.9.1 and 2.11.0, Inductor's codegen changed to create these storage-less views where it previously didn't.

Fix

For non-internal ops, if realize_input() produces a ReinterpretView, fall back to copy_input() which creates a contiguous tensor with its own storage. Internal ops are identified by namespace (aten, prims, inductor, _c10d_functional, quantized, quantized_decomposed) and are unaffected. The copy only occurs for ReinterpretView inputs to custom ops.

Test Plan

Added test/inductor/test_custom_op_reinterpret_tensor.py:

  • Registers a custom op that calls .data_ptr()
  • Uses it inside a torch.autograd.Function within a multi-layer compiled model
  • Compares compiled output against eager execution (forward correctness)
  • Compares compiled gradients against eager gradients (backward correctness)
  • Skipped on CPU-only workers (@unittest.skipIf(not HAS_CUDA, ...))

Related

Impact

Blocks all custom CUDA C++ ops that call .data_ptr() from working inside torch.compile on PyTorch 2.11+. Real-world example: fused CUTLASS EVT matmul+activation backward kernel providing 3.7% training speedup on H100.

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 Apr 5, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@linux-foundation-easycla

linux-foundation-easycla Bot commented Apr 5, 2026 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: abaybektursun / name: Abay Bektursun (8cbbcc0)

@abaybektursun
abaybektursun force-pushed the fix/inductor-custom-op-reinterpret-tensor branch 4 times, most recently from bbafe45 to ca40916 Compare April 5, 2026 19:25
@abaybektursun

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: composability"

@pytorch-bot pytorch-bot Bot added the release notes: composability release notes category label Apr 5, 2026
Custom C++ ops registered via torch.library that call .data_ptr() crash
when Inductor passes reinterpret_tensor views without accessible storage.

FallbackKernel.process_kernel() calls realize_input() which may return
ReinterpretView nodes. Internal ops (aten, prims, etc.) handle views
natively, but custom C++ ops typically call .data_ptr() directly, which
fails on storage-less views.

Fix: for non-internal ops, if realize_input() produces a ReinterpretView,
fall back to copy_input() which creates a contiguous tensor with its own
storage. Internal op performance is unaffected. The copy only occurs for
ReinterpretView inputs to custom ops, which is the minimum necessary to
ensure .data_ptr() is callable.

Error before fix:
  RuntimeError: Cannot access data pointer of Tensor that doesn't have storage
  Exception raised from throw_data_ptr_access_error at c10/core/TensorImpl.cpp:307

Regression: works on PyTorch 2.9.1, crashes on 2.11.0.
@abaybektursun
abaybektursun force-pushed the fix/inductor-custom-op-reinterpret-tensor branch from ca40916 to 8cbbcc0 Compare April 5, 2026 19:33
@soulitzer
soulitzer requested review from eellison and jansel April 15, 2026 16:19
@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Apr 15, 2026

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

this isn't correct, and reinterpret view do have data pointers.

@jansel
jansel requested a review from zou3519 May 21, 2026 02:54

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

+1, we shouldn't force contiguous inputs

@github-actions

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been updated in a while so we're going to go ahead and mark this as Stale.
Feel free to remove the Stale label if you feel this was a mistake.
If you are unable to remove the Stale label please contact a maintainer in order to do so.
If you want the bot to never mark this PR stale again, add the no-stale label.
Stale pull requests will automatically be closed after 30 days of inactivity.

@github-actions github-actions Bot added the Stale label Aug 16, 2026
@github-actions github-actions Bot closed this Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module: inductor open source release notes: composability release notes category Stale triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants