Skip to content

[QUESTION]: returning nb::ndarray #1085

@senyai

Description

@senyai

Hello! Just started using nanobind.

  • Is there a way to allocate nb::ndarray using nanobind?
  • Is it possible to return the same type of nb::ndarray as was passed in, i.e. functionality similar to np.empty_like with exact library (numpy, torch, etc.)? An example in documentation would be nice.
  • I'm getting this error with the following code RuntimeError: nanobind::detail::ndarray_export(): copy failed: AttributeError: 'PyCapsule' object has no attribute 'copy'. (linux, python 3.13). Is the problem on my side?
#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>

namespace nb = nanobind;

using Image2D = nb::ndarray<float, nb::shape<-1, -1>, nb::device::cpu>;

Image2D test(Image2D &image) {
  Image2D output(nullptr, {10, 30});
  return output;
}

NB_MODULE(libadrt, m) { m.def("test", &test); }

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions