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

Preserve autograd history in torch backend #79

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 3 additions & 5 deletions scoringrules/backend/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ def __getitem__(self, __key: str) -> ArrayBackend:
"""Get a backend from the registry."""
try:
return super().__getitem__(__key)
except KeyError as err:
raise BackendNotRegistered(
f"The backend '{__key}' is not registered. "
f"You can register it with scoringrules.register_backend('{__key}')"
) from err
except KeyError:
self.register_backend(__key)
return super().__getitem__(__key)

def set_active(self, backend: str):
self._active = backend
Expand Down
3 changes: 2 additions & 1 deletion scoringrules/backend/torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def asarray(
*,
dtype: Dtype | None = None,
) -> "Tensor":
return torch.asarray(obj, dtype=dtype)
# torch.asarray(obj) would cancel gradients!
return torch.as_tensor(obj, dtype=dtype)

def broadcast_arrays(self, *arrays: "Tensor") -> tuple["Tensor", ...]:
return torch.broadcast_tensors(*arrays)
Expand Down
Loading