Skip to content

Is there any way to directly pass numpy.float16 dtype to pybind? #3750

Answered by jiwaszki
FeixLiu asked this question in Q&A
Discussion options

You must be logged in to vote

Two ways of doing that:

#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

// ...

m.def("is_float16", [](py::dtype &type)  // use of dtype class from numpy.h
      {
        if (type.is(py::dtype("float16")))
        {
          py::print("First overload.");
        }
        else
        {
          py::print("Not float16!");
        }
      });

m.def("is_float16", [](py::object &type)  // pass python object directly
      {
        if (py::dtype::from_args(type).is(py::dtype("float16")))  // use helper function
        {
          py::print("Second overload.");
        }
        else
        {
          py::print("Not float16!");
        }
      });
In [1]: import numpy as np

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@FeixLiu
Comment options

Answer selected by FeixLiu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants