migraphx_py.cpp 2.58 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <migraphx/program.hpp>
Paul's avatar
Paul committed
5
6
#include <migraphx/generate.hpp>
#include <migraphx/cpu/target.hpp>
Paul's avatar
Paul committed
7
8
9
10
#include <migraphx/onnx.hpp>

namespace py = pybind11;

Paul's avatar
Paul committed
11
template <class F>
Paul's avatar
Paul committed
12
13
14
15
struct skip_half
{
    F f;

Paul's avatar
Paul committed
16
    template <class A>
Paul's avatar
Paul committed
17
18
19
20
21
22
23
24
    void operator()(A a) const
    {
        f(a);
    }

    void operator()(migraphx::shape::as<migraphx::half>) const
    {
        throw std::runtime_error("Half not supported in python yet.");
Paul's avatar
Paul committed
25
    }
Paul's avatar
Paul committed
26
27
};

Paul's avatar
Paul committed
28
template <class F>
Paul's avatar
Paul committed
29
30
31
32
33
void visit_type(const migraphx::shape& s, F f)
{
    s.visit_type(skip_half<F>{f});
}

Paul's avatar
Paul committed
34
template <class T>
Paul's avatar
Paul committed
35
36
37
38
39
py::buffer_info to_buffer_info(T& x)
{
    migraphx::shape s = x.get_shape();
    py::buffer_info b;
    visit_type(s, [&](auto as) {
Paul's avatar
Paul committed
40
41
42
43
44
45
        b = py::buffer_info(x.data(),
                            as.size(),
                            py::format_descriptor<decltype(as())>::format(),
                            s.lens().size(),
                            s.lens(),
                            s.strides());
Paul's avatar
Paul committed
46
47
48
49
    });
    return b;
}

Paul's avatar
Paul committed
50
51
PYBIND11_MODULE(migraphx, m)
{
Paul's avatar
Paul committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
    py::class_<migraphx::shape>(m, "shape")
        .def(py::init<>())
        .def("type", &migraphx::shape::type)
        .def("lens", &migraphx::shape::lens)
        .def("strides", &migraphx::shape::strides)
        .def("elements", &migraphx::shape::elements)
        .def("bytes", &migraphx::shape::bytes)
        .def("type_size", &migraphx::shape::type_size)
        .def("packed", &migraphx::shape::packed)
        .def("transposed", &migraphx::shape::transposed)
        .def("broadcasted", &migraphx::shape::broadcasted)
        .def("standard", &migraphx::shape::standard)
        .def("scalar", &migraphx::shape::scalar);

    py::class_<migraphx::argument>(m, "argument", py::buffer_protocol())
Paul's avatar
Paul committed
67
        .def_buffer([](migraphx::argument& x) -> py::buffer_info { return to_buffer_info(x); });
Paul's avatar
Paul committed
68
69
70

    py::class_<migraphx::program>(m, "program")
        .def("get_parameter_shapes", &migraphx::program::get_parameter_shapes)
Paul's avatar
Paul committed
71
        .def("compile", [](migraphx::program& p, const migraphx::target& t) { p.compile(t); })
Paul's avatar
Paul committed
72
73
74
75
        .def("eval", &migraphx::program::eval);

    m.def("parse_onnx", &migraphx::parse_onnx);

Paul's avatar
Paul committed
76
77
78
79
80
81
82
83
    m.def("target", [](const std::string& name) -> migraphx::target {
        if (name == "cpu")
            return migraphx::cpu::target{};
        throw std::runtime_error("Target not found: " + name);
    });

    m.def("generate_argument", &migraphx::generate_argument, py::arg("s"), py::arg("seed") = 0);

Paul's avatar
Paul committed
84
85
86
87
88
89
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}