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

[BUG]: Cannot resolve numpy array as tensor input if flag write=False is set #42

Closed
chrisrichardson opened this issue Jun 9, 2022 · 5 comments

Comments

@chrisrichardson
Copy link

Problem description

If we have a function which takes a const reference, it should be possible to do something like this:

m.def("get_dim", [](const nb::tensor<const double>& x){ return x.ndim(); });

and send in an unwriteable numpy array:

    w = np.array([[2.0,2.0]])
    w.setflags(write=False)
    assert (module.get_dim(w) == 2)

But this doesn't seem to work at the moment:

E       TypeError: get_dim(): incompatible function arguments. The following argument types are supported:
E           1. get_dim(arg: tensor[dtype=float64], /) -> int
E       
E       Invoked with types: ndarray

Removing the write=False, it works fine, but sometimes we have an unwriteable array from somewhere else...

Reproducible example code

No response

@brettc
Copy link

brettc commented Jun 30, 2022

I think I have the same problem. I'm converting code from pybind11, and I pass in a numpy array which is constructed from a buffer:

buf = fstream.read(RAW_CHUNK_SIZE)
arr = np.frombuffer(buf, dtype=np.int8)

Passing arr into a function with just nb::tensor<> as the parameter gives me the same error as above, I'm guessing because frombuffer makes it not writeable.

@brettc
Copy link

brettc commented Jun 30, 2022

I spent a bit of time futzing around trying to debug this. If I change this line:

if (PyObject_GetBuffer(o, view.get(), PyBUF_RECORDS)) {

to this:

  if (PyObject_GetBuffer(o, view.get(), PyBUF_RECORDS_RO)) {

Then my code works. But I have no idea about the further implications of this. There is a lot to take in!

@chrisrichardson Does this work for you?

@brettc
Copy link

brettc commented Jul 4, 2022

It appears the readonly problem is not just with nanobind.

import numpy as np
w = np.array([[2.0,2.0]])
w.setflags(write=False)
w.__dlpack__()

gives

TypeError: NumPy currently only supports dlpack for writeable arrays

Some discussion here: numpy/numpy#20742

So is the issue that dlpack doesn't support readonly buffers?

@whateverforever
Copy link

Relevant when working with mikedh/trimesh, as the vertex and normal arrays have write=False by default

@wjakob
Copy link
Owner

wjakob commented Feb 27, 2023

I will close this issue, the current master version of nanobind addresses it to my knowledge. If not, feel free to reopen.

@wjakob wjakob closed this as completed Feb 27, 2023
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

4 participants