migraphx_py.cpp 5.87 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>
7
8
// #include <migraphx/onnx.hpp>
#include <migraphx/tf.hpp>
Paul's avatar
Paul committed
9
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
10
11
12
13
#ifdef HAVE_GPU
#include <migraphx/gpu/target.hpp>
#include <migraphx/gpu/hip.hpp>
#endif
Paul's avatar
Paul committed
14
15
16

namespace py = pybind11;

Paul's avatar
Paul committed
17
template <class F>
Paul's avatar
Paul committed
18
struct throw_half
Paul's avatar
Paul committed
19
20
21
{
    F f;

Paul's avatar
Paul committed
22
    template <class A>
Paul's avatar
Paul committed
23
24
25
26
27
28
29
30
    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
31
    }
Paul's avatar
Paul committed
32
33
34
35
36

    void operator()(migraphx::tensor_view<migraphx::half>) const
    {
        throw std::runtime_error("Half not supported in python yet.");
    }
Paul's avatar
Paul committed
37
38
};

Paul's avatar
Paul committed
39
40
41
42
43
44
45
46
47
48
49
template <class F>
struct skip_half
{
    F f;

    template <class A>
    void operator()(A a) const
    {
        f(a);
    }

Paul's avatar
Paul committed
50
    void operator()(migraphx::shape::as<migraphx::half>) const {}
Paul's avatar
Paul committed
51

Paul's avatar
Paul committed
52
    void operator()(migraphx::tensor_view<migraphx::half>) const {}
Paul's avatar
Paul committed
53
54
};

Paul's avatar
Paul committed
55
template <class F>
Paul's avatar
Paul committed
56
57
void visit_type(const migraphx::shape& s, F f)
{
Paul's avatar
Paul committed
58
59
60
    s.visit_type(throw_half<F>{f});
}

Paul's avatar
Paul committed
61
62
63
64
65
66
template <class T, class F>
void visit(const migraphx::raw_data<T>& x, F f)
{
    x.visit(throw_half<F>{f});
}

Paul's avatar
Paul committed
67
68
69
70
template <class F>
void visit_types(F f)
{
    migraphx::shape::visit_types(skip_half<F>{f});
Paul's avatar
Paul committed
71
72
}

Paul's avatar
Paul committed
73
template <class T>
Paul's avatar
Paul committed
74
75
76
py::buffer_info to_buffer_info(T& x)
{
    migraphx::shape s = x.get_shape();
Paul's avatar
Paul committed
77
78
79
    auto strides      = s.strides();
    std::transform(
        strides.begin(), strides.end(), strides.begin(), [&](auto i) { return i * s.type_size(); });
Paul's avatar
Paul committed
80
81
    py::buffer_info b;
    visit_type(s, [&](auto as) {
Paul's avatar
Paul committed
82
83
84
85
86
        b = py::buffer_info(x.data(),
                            as.size(),
                            py::format_descriptor<decltype(as())>::format(),
                            s.lens().size(),
                            s.lens(),
87
                            strides);
Paul's avatar
Paul committed
88
89
90
91
    });
    return b;
}

Paul's avatar
Paul committed
92
93
94
migraphx::shape to_shape(const py::buffer_info& info)
{
    migraphx::shape::type_t t;
95
    std::size_t n = 0;
Paul's avatar
Paul committed
96
    visit_types([&](auto as) {
Paul's avatar
Paul committed
97
98
        if(info.format == py::format_descriptor<decltype(as())>::format())
        {
Paul's avatar
Paul committed
99
            t = as.type_enum();
100
101
102
103
104
105
            n = sizeof(as());
        }

    });
    auto strides = info.strides;
    std::transform(strides.begin(), strides.end(), strides.begin(), [&](auto i) -> std::size_t {
Paul's avatar
Paul committed
106
        return n > 0 ? i / n : 0;
Paul's avatar
Paul committed
107
    });
108
    return migraphx::shape{t, info.shape, strides};
Paul's avatar
Paul committed
109
110
}

Paul's avatar
Paul committed
111
112
PYBIND11_MODULE(migraphx, m)
{
Paul's avatar
Paul committed
113
114
115
116
117
118
119
120
121
122
123
124
    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)
Paul's avatar
Paul committed
125
        .def("scalar", &migraphx::shape::scalar)
Paul's avatar
Paul committed
126
127
        .def("__eq__", std::equal_to<migraphx::shape>{})
        .def("__ne__", std::not_equal_to<migraphx::shape>{})
Paul's avatar
Paul committed
128
        .def("__repr__", [](const migraphx::shape& s) { return migraphx::to_string(s); });
Paul's avatar
Paul committed
129
130

    py::class_<migraphx::argument>(m, "argument", py::buffer_protocol())
Paul's avatar
Paul committed
131
        .def_buffer([](migraphx::argument& x) -> py::buffer_info { return to_buffer_info(x); })
Paul's avatar
Paul committed
132
133
134
135
136
        .def("__init__",
             [](migraphx::argument& x, py::buffer b) {
                 py::buffer_info info = b.request();
                 new(&x) migraphx::argument(to_shape(info), info.ptr);
             })
Paul's avatar
Paul committed
137
        .def("get_shape", &migraphx::argument::get_shape)
Paul's avatar
Paul committed
138
139
140
141
142
143
        .def("tolist",
             [](migraphx::argument& x) {
                 py::list l{x.get_shape().elements()};
                 visit(x, [&](auto data) { l = py::cast(data.to_vector()); });
                 return l;
             })
Paul's avatar
Paul committed
144
145
146
        .def("__eq__", std::equal_to<migraphx::argument>{})
        .def("__ne__", std::not_equal_to<migraphx::argument>{})
        .def("__repr__", [](const migraphx::argument& x) { return migraphx::to_string(x); });
Paul's avatar
Paul committed
147

Paul's avatar
Paul committed
148
149
    py::class_<migraphx::target>(m, "target");

Paul's avatar
Paul committed
150
151
    py::class_<migraphx::program>(m, "program")
        .def("get_parameter_shapes", &migraphx::program::get_parameter_shapes)
Paul's avatar
Paul committed
152
        .def("get_shape", &migraphx::program::get_shape)
Paul's avatar
Paul committed
153
        .def("compile", [](migraphx::program& p, const migraphx::target& t) { p.compile(t); })
Paul's avatar
Paul committed
154
        .def("run", &migraphx::program::eval)
Paul's avatar
Paul committed
155
156
        .def("__eq__", std::equal_to<migraphx::program>{})
        .def("__ne__", std::not_equal_to<migraphx::program>{})
Paul's avatar
Paul committed
157
        .def("__repr__", [](const migraphx::program& p) { return migraphx::to_string(p); });
Paul's avatar
Paul committed
158

Khalique's avatar
Khalique committed
159
    //     m.def("parse_onnx", &migraphx::parse_onnx);
160

Khalique's avatar
Khalique committed
161
162
163
164
165
    m.def("parse_tf",
          &migraphx::parse_tf,
          "Parse tf protobuf (default format is nhwc)",
          py::arg("filename"),
          py::arg("is_nhwc") = true);
Paul's avatar
Paul committed
166

Paul's avatar
Paul committed
167
    m.def("get_target", [](const std::string& name) -> migraphx::target {
Paul's avatar
Paul committed
168
        if(name == "cpu")
Paul's avatar
Paul committed
169
            return migraphx::cpu::target{};
Paul's avatar
Paul committed
170
171
172
173
#ifdef HAVE_GPU
        if(name == "gpu")
            return migraphx::gpu::target{};
#endif
Paul's avatar
Paul committed
174
175
176
177
178
        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
179
180
181
182
183
184
185
186
#ifdef HAVE_GPU
    m.def("allocate_gpu", &migraphx::gpu::allocate_gpu, py::arg("s"), py::arg("host") = false);
    m.def("to_gpu", &migraphx::gpu::to_gpu, py::arg("arg"), py::arg("host") = false);
    m.def("from_gpu", &migraphx::gpu::from_gpu);
    m.def("gpu_sync", &migraphx::gpu::gpu_sync);
    m.def("copy_to_gpu", &migraphx::gpu::copy_to_gpu);
#endif

Paul's avatar
Paul committed
187
188
189
190
191
192
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}