Skip to content

Commit 779d7b5

Browse files
authored
special case NULL tensor as DATAPTR is not allowed on NULL objects. (#1319)
1 parent 1b0433f commit 779d7b5

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/tensor.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ torch::Tensor create_tensor_from_atomic(SEXP x, torch::Dtype cdtype) {
9595
: std::vector<int64_t>(1, LENGTH(x));
9696
auto strides = stride_from_dim(dim);
9797

98-
torch::Tensor tensor =
99-
lantern_from_blob(DATAPTR(x), &dim[0], dim.size(), &strides[0],
100-
strides.size(), options.get());
98+
torch::Tensor tensor = [&]() {
99+
if (TYPEOF(x) == NILSXP) {
100+
return lantern_from_blob(nullptr, &dim[0], dim.size(), &strides[0],
101+
strides.size(), options.get());
102+
}
103+
104+
return lantern_from_blob(DATAPTR(x), &dim[0], dim.size(), &strides[0],
105+
strides.size(), options.get());
106+
}();
107+
101108
if (dim.size() == 1) {
102109
// if we have a 1-dim vector contigous doesn't trigger a copy, and
103110
// would be unexpected.

0 commit comments

Comments
 (0)