Skip to content

fix: keep torch_scalar_tensor() a leaf when requires_grad = TRUE - #1484

Open
taekop wants to merge 1 commit into
mlverse:mainfrom
taekop:fix/scalar-tensor-requires-grad
Open

taekop wants to merge 1 commit into
mlverse:mainfrom
taekop:fix/scalar-tensor-requires-grad

Conversation

@taekop

@taekop taekop commented Sep 29, 2026

Copy link
Copy Markdown

torch_scalar_tensor() created the tensor with requires_grad = TRUE and then called $squeeze() on it. Since $squeeze() is a tracked autograd op, the returned tensor was no longer a leaf, so .grad was never populated after backward() (as sebffischer diagnosed in the issue thread).

This fix squeezes first and only enables requires_grad afterwards via $requires_grad_(), for the non-tensor-input branch (the tensor-input branch already called $requires_grad_() last and was unaffected). dtype/device handling is unchanged.

Added a regression test covering both branches (plain value and tensor input) that checks is_leaf and the gradient value after backward().

Fixes #1368.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug when using requires_grad = TRUE with torch_scalar_tensor

1 participant