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

TensorType detail: grad_enabled #21

Open
SimpleConjugate opened this issue May 27, 2021 · 1 comment
Open

TensorType detail: grad_enabled #21

SimpleConjugate opened this issue May 27, 2021 · 1 comment

Comments

@SimpleConjugate
Copy link

Is it possible to perform type checking for tensors with grad enabled? I myself am not sure of all the cases necessary to test against to confirm this as I don't fully understand how runtime type checking operates.

class _AutoGradTensorDetail(TensorDetail):
    def check(self, tensor: torch.Tensor)  -> bool:
        return tensor.requires_grad()
@patrick-kidger
Copy link
Owner

Ah, that's a nice idea for a tensor detail.

Yes, that should be completely possible. Quick mock-up (untested):

class _RequiresGradDetail(TensorDetail):
    def check(self, tensor: Tensor) -> bool:
        return tensor.requires_grad

    def __repr__(self) -> str:
        return "requires_grad"

    @classmethod
    def tensor_repr(cls, tensor: Tensor) -> str:
        if tensor.requires_grad:
            return "requires_grad"
       	else:
            return ""

requires_grad = _RequiresGradDetail()

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

No branches or pull requests

2 participants