Improve custom ops aliasing error message (#134688)

Fixes https://github.com/pytorch/pytorch/issues/134278

Test Plan:
- tested locally
Pull Request resolved: https://github.com/pytorch/pytorch/pull/134688
Approved by: https://github.com/yushangdi
ghstack dependencies: #134466, #134490, #134491, #134690, #134692
This commit is contained in:
rzou
2024-08-28 10:17:01 -07:00
committed by PyTorch MergeBot
parent dd443f418a
commit a7933acd5a

View File

@ -332,12 +332,16 @@ class CustomOpDef:
fn = self._backend_fns[device_type]
module = inspect.getmodule(fn)
raise RuntimeError(
f"Tensors returned from custom ops (1) must not "
f"be inputs to the custom op and (2) may not alias "
f"any inputs or other returns. Please clone the "
f"the offending output tensors (e.g. output.clone()) "
f"or refactor your code. "
f"Offending op: {self._name} (with implementation in {module})"
f"{self._name} (with implementation in {module}): "
f"The output of this custom operator (1) must not "
f"also be an input to this custom operator and "
f"(2) may not alias any inputs to this custom operator "
f"or other returns. "
f"The most common way to trigger this error is if "
f"we have y = custom_op(x) and y and x are the same Tensor. "
f"Please instead return a clone of the offending output "
f"tensor(s) (e.g. return x.clone()) or refactor the custom "
f"operator to not return y."
)
storages.add(key)
return result