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

[BE] More informative error messages in THPVariable_set_grad #100174

Closed
awgu opened this issue Apr 27, 2023 · 4 comments
Closed

[BE] More informative error messages in THPVariable_set_grad #100174

awgu opened this issue Apr 27, 2023 · 4 comments
Labels
actionable better-engineering Relatively self-contained tasks for better engineering contributors module: autograd Related to torch.autograd, and the autograd engine in general module: error checking Bugs related to incorrect/lacking error checking triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@awgu
Copy link
Contributor

awgu commented Apr 27, 2023

int THPVariable_set_grad(THPVariable* self, PyObject* py_grad, void* unused) {

When the gradient metadata does not match the corresponding tensor's metadata, THPVariable_set_grad() raises an error, e.g.:

RuntimeError: assigned grad has data of a different type

Including what leads to the mismatch would be very helpful. For example, for the above message, if we could see something like:

RuntimeError: assigned grad has data of type torch.float16 that differs from the required torch.float32

A similar idea applies for mismatched devices and sizes.

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

@awgu awgu added the better-engineering Relatively self-contained tasks for better engineering contributors label Apr 27, 2023
@awgu
Copy link
Contributor Author

awgu commented Apr 27, 2023

Following up on this, consider the following two asserts:

THPUtils_assertRet(
-1,
grad.options().type_equal(var.options()) || gradIsSparse,
"assigned grad has data of a different type");

if (var.is_cuda()) {
THPUtils_assertRet(
-1,
grad.get_device() == var.get_device(),
"assigned grad has data located on a different device");
}

Can the second one (which is checked after the first) ever be triggered?

If I look at options(), it includes the device:

TensorOptions options() const {
return TensorOptions().dtype(dtype())
.device(device())
.layout(layout());
}

>>> t = torch.randn((3, 3), device="cuda")
>>> g = torch.randn((3, 3), device="cpu")
>>> t.grad = g
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: assigned grad has data of a different type

@zou3519 zou3519 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 Apr 28, 2023
@albanD
Copy link
Collaborator

albanD commented Apr 28, 2023

I would expect type_equal() to only check the dtype? But maybe that's not true.

We would definitely be happy with a PR improving these errors!

@albanD albanD added module: error checking Bugs related to incorrect/lacking error checking actionable labels Apr 28, 2023
@awgu
Copy link
Contributor Author

awgu commented Apr 28, 2023

bool type_equal(const TensorOptions& other) const {
return computeDispatchKey() == other.computeDispatchKey() &&
typeMetaToScalarType(dtype_) == typeMetaToScalarType(other.dtype());
}

Maybe the first clause on the dispatch key equality is causing the non-intuitive behavior since it seems to be a function of the device?
DispatchKey computeDispatchKey() const {
return c10::computeDispatchKey(
optTypeMetaToScalarType(dtype_opt()), layout_opt(), device_opt());
}

@bdhirsh
Copy link
Contributor

bdhirsh commented Apr 28, 2023

   THPUtils_assertRet( 
       -1, 
       grad.get_device() == var.get_device(), 
       "assigned grad has data located on a different device"); 
 } 

It looks like get_device() checks that the device index matches, e.g. cuda:0 vs cuda: (code)

For the first check involving type_equal() - dispatch keys take into account the device type, but not the device index. So that first check will only error if e.g. the two tensors are on cpu vs cuda, but not if the two tensors have different indices, like cuda:0 vs cuda:1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
actionable better-engineering Relatively self-contained tasks for better engineering contributors module: autograd Related to torch.autograd, and the autograd engine in general module: error checking Bugs related to incorrect/lacking error checking triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants