Skip to content

Commit

Permalink
Add a currently unexported torch_tensor_free that deletes tensors w…
Browse files Browse the repository at this point in the history
…ithout requiring us to wait for GC. (#1194)
  • Loading branch information
dfalbel authored Sep 20, 2024
1 parent 7142cf7 commit 030cda0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,9 @@ torch::Tensor nnf_pad_circular(torch::Tensor input,
// [[Rcpp::export]]
bool cpp_method_Tensor_is_sparse (torch::Tensor x) {
return lantern_Tensor_is_sparse(x.get());
}

// [[Rcpp::export]]
void torch_tensor_free(Rcpp::XPtr<torch::Tensor> x) {
x.release();
}
9 changes: 7 additions & 2 deletions src/torch_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

static inline void tensor_finalizer(SEXP ptr) {
auto xptr = Rcpp::as<Rcpp::XPtr<XPtrTorchTensor>>(ptr);
lantern_tensor_set_pyobj(xptr->get(), nullptr);
// When the the xptr is released, we might call this function with an invalid ptr.
if (xptr.get()) {
lantern_tensor_set_pyobj(xptr->get(), nullptr);
}
}

static inline bool rlang_is_named(SEXP x) {
Expand Down Expand Up @@ -140,7 +143,9 @@ XPtrTorchTensor from_sexp_tensor(SEXP x) {
Rcpp::stop("Expected a torch_tensor.");
}

void delete_tensor(void* x) { lantern_Tensor_delete(x); }
void delete_tensor(void* x) {
lantern_Tensor_delete(x);
}

// optional_torch_tensor

Expand Down

0 comments on commit 030cda0

Please sign in to comment.