-
I am wondering if it possible to expose C++ buffers as a writable tensor in python. E.g. if I have the struct Foo {
std::vector<int> tags = {1, 2, 3};
}; and the using IntArray = nb::tensor<nb::numpy, int, nb::shape<nb::any>>;
nb::class_<Foo>(m, "Foo")
.def_property("tags",
[](Foo& foo) {
size_t shape[] = {foo.tags.size()};
return IntArray(reinterpret_cast<void*>(foo.tags.data()), 1, shape);
}, [](Foo& foo, IntArray tensor) {
...
}); In python, the numpy array corresponding to a = Foo()
print(a.tags.flags)
# C_CONTIGUOUS : True
# F_CONTIGUOUS : True
# OWNDATA : False
# WRITEABLE : False
# ALIGNED : True
# WRITEBACKIFCOPY : False Is there a way of creating writable tensor such that in place update like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
As @qnzhou said in #53, this looks like a limitation of using So working through dlpack currently puts some constraints on passing tensors.
I see that the reimplementation of the numpy interface is "open for discussion". @wjakob, is it worthwhile submitting a PR that ports the numpy code from pybind11? I could give this a go, though it might be a challenge given my limited understanding of the internals. |
Beta Was this translation helpful? Give feedback.
As @qnzhou said in #53, this looks like a limitation of using
from_dlpack()
and is by design. A related issue is the one mentioned in #42, where you cannot send a read-only numpy array to c++, though it looks like this might be worked around in nanobind.So working through dlpack currently puts some constraints on passing tensors.
I see that the reimplementation of the numpy interface is "open for discussion". @wjakob, is it worthwhile submitting a PR that ports the numpy code from pybind11? I could give this a go, though it might be a challenge given my limited understanding of the…