migraphx_py.cpp 5.92 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
#include <migraphx/stringutils.hpp>
8
9
10
11
12
13
#ifdef ENABLE_TF
#include <migraphx/tf.hpp>
#else
#include <migraphx/onnx.hpp>
#endif

Paul's avatar
Paul committed
14
15
16
17
#ifdef HAVE_GPU
#include <migraphx/gpu/target.hpp>
#include <migraphx/gpu/hip.hpp>
#endif
Paul's avatar
Paul committed
18
19
20

namespace py = pybind11;

Paul's avatar
Paul committed
21
template <class F>
Paul's avatar
Paul committed
22
struct throw_half
Paul's avatar
Paul committed
23
24
25
{
    F f;

Paul's avatar
Paul committed
26
    template <class A>
Paul's avatar
Paul committed
27
28
29
30
31
32
33
34
    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
35
    }
Paul's avatar
Paul committed
36
37
38
39
40

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

Paul's avatar
Paul committed
43
44
45
46
47
48
49
50
51
52
53
template <class F>
struct skip_half
{
    F f;

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

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

Paul's avatar
Paul committed
56
    void operator()(migraphx::tensor_view<migraphx::half>) const {}
Paul's avatar
Paul committed
57
58
};

Paul's avatar
Paul committed
59
template <class F>
Paul's avatar
Paul committed
60
61
void visit_type(const migraphx::shape& s, F f)
{
Paul's avatar
Paul committed
62
63
64
    s.visit_type(throw_half<F>{f});
}

Paul's avatar
Paul committed
65
66
67
68
69
70
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
71
72
73
74
template <class F>
void visit_types(F f)
{
    migraphx::shape::visit_types(skip_half<F>{f});
Paul's avatar
Paul committed
75
76
}

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

Paul's avatar
Paul committed
96
97
98
migraphx::shape to_shape(const py::buffer_info& info)
{
    migraphx::shape::type_t t;
99
    std::size_t n = 0;
Paul's avatar
Paul committed
100
    visit_types([&](auto as) {
Paul's avatar
Paul committed
101
102
        if(info.format == py::format_descriptor<decltype(as())>::format())
        {
Paul's avatar
Paul committed
103
            t = as.type_enum();
104
105
106
107
108
109
            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
110
        return n > 0 ? i / n : 0;
Paul's avatar
Paul committed
111
    });
112
    return migraphx::shape{t, info.shape, strides};
Paul's avatar
Paul committed
113
114
}

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

    py::class_<migraphx::argument>(m, "argument", py::buffer_protocol())
Paul's avatar
Paul committed
135
        .def_buffer([](migraphx::argument& x) -> py::buffer_info { return to_buffer_info(x); })
Paul's avatar
Paul committed
136
137
138
139
140
        .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
141
        .def("get_shape", &migraphx::argument::get_shape)
Paul's avatar
Paul committed
142
143
144
145
146
147
        .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
148
149
150
        .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
151

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

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

163
#ifdef ENABLE_TF
Khalique's avatar
Khalique committed
164
165
166
167
168
    m.def("parse_tf",
          &migraphx::parse_tf,
          "Parse tf protobuf (default format is nhwc)",
          py::arg("filename"),
          py::arg("is_nhwc") = true);
169
170
#else
    m.def("parse_onnx", &migraphx::parse_onnx);
Paul's avatar
Paul committed
171

172
#endif
Paul's avatar
Paul committed
173
    m.def("get_target", [](const std::string& name) -> migraphx::target {
Paul's avatar
Paul committed
174
        if(name == "cpu")
Paul's avatar
Paul committed
175
            return migraphx::cpu::target{};
Paul's avatar
Paul committed
176
177
178
179
#ifdef HAVE_GPU
        if(name == "gpu")
            return migraphx::gpu::target{};
#endif
Paul's avatar
Paul committed
180
181
182
183
184
        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
185
186
187
188
189
190
191
192
#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
193
194
195
196
197
198
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}