KEMBAR78
`print` statement causes inplace error · Issue #99968 · pytorch/pytorch · GitHub
Skip to content

print statement causes inplace error #99968

@ptrblck

Description

@ptrblck

🐛 Describe the bug

Reported in: https://discuss.pytorch.org/t/error-with-view-no-grad-and-inplace-modify/173082
but I couldn't find the created GitHub issue and the author didn't follow up.

Code to reproduce the issue:

net = nn.Sequential(
    nn.Linear(10, 10),
    nn.ReLU(),
    nn.Linear(10, 10),
    nn.ReLU(),
    nn.Linear(10, 10),
    nn.ReLU(),
)

with torch.no_grad():
  for param in net.parameters():
    for j in param.flatten():
        #print("current j", j)
        j += 1

Comment the print statement in and the code will fail with:

RuntimeError: A view was created in no_grad mode and its base or another view of its base has been modified inplace with grad mode enabled. Given that this use case is ambiguous and error-prone, it is forbidden. You can clarify your code by moving both the view and the inplace either both inside the no_grad block (if you don't want the inplace to be tracked) or both outside (if you want the inplace to be tracked).

I would assume the inplace operation is allowed as it's in a no_grad block and no computation graph was ever created.

Also, maybe related to: https://discuss.pytorch.org/t/old-problem-but-strange-things-trying-to-backward-through-the-graph-a-second-time/178369

but no executable code snippet was posted yet.

Versions

Reproduced in a nightly build: 2.1.0.dev20230407+cu118.

CC @albanD as we talked about this issue before.

cc @ezyang @gchanan @zou3519 @kadeng @albanD @gqchen @pearu @nikitaved @soulitzer @lezcano @Varal7

Metadata

Metadata

Assignees

Labels

actionablehigh prioritymodule: autogradRelated to torch.autograd, and the autograd engine in generalneeds designWe want to add this feature but we need to figure out how firsttriage reviewtriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions