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

[lang] Warn about non-contiguous gradient tensors #8450

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions python/taichi/lang/kernel_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,16 @@ def call_back():
if v.requires_grad and v.grad is None:
v.grad = torch.zeros_like(v)

if v.requires_grad:
if not isinstance(v.grad, torch.Tensor):
raise ValueError(
f"Expecting torch.Tensor for gradient tensor, but getting {v.grad.__class__.__name__} instead"
)
if not v.grad.is_contiguous():
raise ValueError(
"Non contiguous gradient tensors are not supported, please call tensor.grad.contiguous() before passing it into taichi kernel."
)

tmp = v
if (str(v.device) != "cpu") and not (
str(v.device).startswith("cuda") and taichi_arch == _ti_core.Arch.cuda
Expand Down
Loading