pytorch - 💡(How to fix) Fix [Inductor] AssertionError: wrong number of dimensions1 in backward() with dynamic shape involving nonzero() and item() [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#177252Fetched 2026-04-08 00:42:44
View on GitHub
Comments
0
Participants
1
Timeline
122
Reactions
0
Author
Participants
Assignees
Timeline (top)
mentioned ×54subscribed ×54labeled ×7referenced ×3

Error Message

import torch torch._dynamo.config.capture_scalar_outputs = True torch._dynamo.config.capture_dynamic_output_shape_ops = True

def f(x): num = x.nonzero().numel() num = x.new_tensor([num]).item() y = w * x return y.sum() / num

x = torch.randn(4) w = torch.tensor(0.0, requires_grad=True)

loss1 = torch.compile(f, dynamic=True, backend="aot_eager")(x) loss1.backward()

loss2 = torch.compile(f, dynamic=True, backend="inductor")(x) loss2.backward() # AssertionError: wrong number of dimensions1

Code Example

import torch
torch._dynamo.config.capture_scalar_outputs = True
torch._dynamo.config.capture_dynamic_output_shape_ops = True

def f(x):
    num = x.nonzero().numel()
    num = x.new_tensor([num]).item()
    y = w * x
    return y.sum() / num

x = torch.randn(4)
w = torch.tensor(0.0, requires_grad=True)

loss1 = torch.compile(f, dynamic=True, backend="aot_eager")(x)
loss1.backward()

loss2 = torch.compile(f, dynamic=True, backend="inductor")(x)
loss2.backward() # AssertionError: wrong number of dimensions1

---

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    loss2.backward() # AssertionError: wrong number of dimensions1
  File "pytorch/torch/_tensor.py", line 631, in backward
    torch.autograd.backward(
  File "pytorch/torch/autograd/__init__.py", line 379, in backward
    _engine_run_backward(
  File "pytorch/torch/autograd/graph.py", line 877, in _engine_run_backward
    return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
  File "pytorch/torch/autograd/function.py", line 317, in apply
    return user_fn(self, *args)
  File "pytorch/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 2852, in backward
    return impl_fn()
  File "pytorch/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 2838, in impl_fn
    out = CompiledFunction._backward_impl(ctx, all_args)
  File "pytorch/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 3022, in _backward_impl
    out = call_func_at_runtime_with_args(
  File "pytorch/torch/_functorch/_aot_autograd/utils.py", line 138, in call_func_at_runtime_with_args
    out = normalize_as_list(f(args))
  File "pytorch/torch/_dynamo/eval_frame.py", line 1266, in _fn
    return fn(*args, **kwargs)
  File "pytorch/torch/_inductor/output_code.py", line 673, in __call__
    return self.current_callable(inputs)
  File "/tmp/tq/ctqfsv2jfmundvfjsq3g4eleq7qvmnat4xymgfnce3djdlgvmyvr.py", line 101, in call
    assert_size_stride(tangents_1, (), ())
AssertionError: wrong number of dimensions1
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

Reproduce the failure with following case (BTW: not only CPU, GPU also fails):

import torch
torch._dynamo.config.capture_scalar_outputs = True
torch._dynamo.config.capture_dynamic_output_shape_ops = True

def f(x):
    num = x.nonzero().numel()
    num = x.new_tensor([num]).item()
    y = w * x
    return y.sum() / num

x = torch.randn(4)
w = torch.tensor(0.0, requires_grad=True)

loss1 = torch.compile(f, dynamic=True, backend="aot_eager")(x)
loss1.backward()

loss2 = torch.compile(f, dynamic=True, backend="inductor")(x)
loss2.backward() # AssertionError: wrong number of dimensions1

The error message:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    loss2.backward() # AssertionError: wrong number of dimensions1
  File "pytorch/torch/_tensor.py", line 631, in backward
    torch.autograd.backward(
  File "pytorch/torch/autograd/__init__.py", line 379, in backward
    _engine_run_backward(
  File "pytorch/torch/autograd/graph.py", line 877, in _engine_run_backward
    return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
  File "pytorch/torch/autograd/function.py", line 317, in apply
    return user_fn(self, *args)
  File "pytorch/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 2852, in backward
    return impl_fn()
  File "pytorch/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 2838, in impl_fn
    out = CompiledFunction._backward_impl(ctx, all_args)
  File "pytorch/torch/_functorch/_aot_autograd/runtime_wrappers.py", line 3022, in _backward_impl
    out = call_func_at_runtime_with_args(
  File "pytorch/torch/_functorch/_aot_autograd/utils.py", line 138, in call_func_at_runtime_with_args
    out = normalize_as_list(f(args))
  File "pytorch/torch/_dynamo/eval_frame.py", line 1266, in _fn
    return fn(*args, **kwargs)
  File "pytorch/torch/_inductor/output_code.py", line 673, in __call__
    return self.current_callable(inputs)
  File "/tmp/tq/ctqfsv2jfmundvfjsq3g4eleq7qvmnat4xymgfnce3djdlgvmyvr.py", line 101, in call
    assert_size_stride(tangents_1, (), ())
AssertionError: wrong number of dimensions1

Versions.log

It seems:

  • FX graph for backward() should be correct (as backend="aot_eager" could pass)
  • inductor generates wrong code code for backward() (while forward() is correct)

Do you know why or how to fix this problem? Thanks in advance!

cc @ezyang @albanD @gqchen @nikitaved @soulitzer @Varal7 @bobrenjc93 @chauhang @penguinwu @aditvenk @laithsakka @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo

extent analysis

Fix Plan

The issue arises from the incorrect handling of tensor dimensions in the backward() pass when using the inductor backend. To fix this, we need to ensure that the tensor dimensions are correctly propagated during the backward pass.

Here are the steps to fix the issue:

  • Modify the f function to handle the tensor dimensions correctly:
def f(x):
    num = x.nonzero().numel()
    num = x.new_tensor([num]).item()
    y = w * x
    return y.sum() / num

becomes

def f(x):
    num = x.nonzero().numel()
    num = x.new_tensor([num]).item()
    y = w * x
    y = y.sum().unsqueeze(0)  # Add a dimension to the output
    return y / num
  • Update the loss2 calculation to use the modified f function:
loss2 = torch.compile(f, dynamic=True, backend="inductor")(x)
loss2.backward()

Verification

To verify that the fix worked, run the modified code and check that the backward() pass completes without errors.

Extra Tips

  • When using the inductor backend, ensure that tensor dimensions are correctly handled during the backward pass.
  • Use unsqueeze(0) to add a dimension to the output tensor if necessary.
  • Verify that the fix works by running the modified code and checking for errors.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING