Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assigning parameter to tensor doesn't pass gradient info #77565

Closed
InfProbSciX opened this issue May 16, 2022 · 4 comments
Closed

Assigning parameter to tensor doesn't pass gradient info #77565

InfProbSciX opened this issue May 16, 2022 · 4 comments
Assignees
Labels
module: autograd Related to torch.autograd, and the autograd engine in general triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@InfProbSciX
Copy link

InfProbSciX commented May 16, 2022

Issue description

In the example below, assigning a parameter to tensor doesn't seem to pass gradient information, unless a list is used for indexing.

Code example

import torch
torch.__version__  # '1.11.0'
z = torch.zeros(3, 2)

z[:, 0] = torch.nn.Parameter(torch.tensor(1.0))
z

# tensor([[1., 0.],
#         [1., 0.],
#         [1., 0.]])  # <------------

z[:, [0]] = torch.nn.Parameter(torch.tensor(1.0))
z

# tensor([[1., 0.],
#         [1., 0.],
#         [1., 0.]], grad_fn=<CopySlices>)  # <------------

... is this behaviour expected?

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

@mikaylagawarecki mikaylagawarecki added module: autograd Related to torch.autograd, and the autograd engine in general triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels May 17, 2022
@ezyang
Copy link
Contributor

ezyang commented May 17, 2022

No, non-advanced indexing should propagate gradients, looks like a bug.

@albanD
Copy link
Collaborator

albanD commented May 17, 2022

Note that it doesn't require to use Parameter, the same thing happens with plain Tensors that require gradients.
Also this only happens if we use a scalar Tensor, so I guess some convertion to plain python Number in the advanced indexing code?

Note that as a workaround, you can use torch.nn.Parameter(torch.tensor((1.0,))) that is a 1D Tensor with a single value.

@yuguo68 yuguo68 self-assigned this May 25, 2022
@yuguo68
Copy link
Contributor

yuguo68 commented May 25, 2022

Thank you @InfProbSciX for reporting the bug and the reproducing code. It seems the root cause is here

} else if (src.sizes().size() == 0 && src.device().type() == at::kCPU) {
dst.fill_(src.item());
return;
, indeed it is "some convertion to plain python Number" suggested by @albanD. Also, if the scalar tensor is on GPU, we have the gradient info. I will have a pr to fix.

@albanD
Copy link
Collaborator

albanD commented Jun 1, 2022

Fixed in master now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: autograd Related to torch.autograd, and the autograd engine in general triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

5 participants