"llama/git@developer.sourcefind.cn:orangecat/ollama.git" did not exist on "79a999e95d1e4b618a15590068190bf9ca6865aa"
migraphx_py.cpp 10.8 KB
Newer Older
Paul's avatar
Paul committed
1
2
3

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Shucai Xiao's avatar
Shucai Xiao committed
4
#include <pybind11/numpy.h>
Paul's avatar
Paul committed
5
#include <migraphx/program.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
6
#include <migraphx/quantization.hpp>
Paul's avatar
Paul committed
7
8
#include <migraphx/generate.hpp>
#include <migraphx/cpu/target.hpp>
Paul's avatar
Paul committed
9
#include <migraphx/stringutils.hpp>
10
11
#include <migraphx/tf.hpp>
#include <migraphx/onnx.hpp>
12
#include <migraphx/type_name.hpp>
13
14
#include <migraphx/load_save.hpp>
#include <migraphx/register_target.hpp>
15

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

Shucai Xiao's avatar
Shucai Xiao committed
20
using half   = half_float::half;
Paul's avatar
Paul committed
21
22
namespace py = pybind11;

Shucai Xiao's avatar
Shucai Xiao committed
23
24
namespace pybind11 {
namespace detail {
Paul's avatar
Paul committed
25

Shucai Xiao's avatar
Shucai Xiao committed
26
27
template <>
struct npy_format_descriptor<half>
Paul's avatar
Paul committed
28
{
Shucai Xiao's avatar
Shucai Xiao committed
29
    static std::string format()
Paul's avatar
Paul committed
30
    {
Shucai Xiao's avatar
Shucai Xiao committed
31
32
        // following: https://docs.python.org/3/library/struct.html#format-characters
        return "e";
Paul's avatar
Paul committed
33
    }
Shucai Xiao's avatar
Shucai Xiao committed
34
    static constexpr auto name() { return _("half"); }
Paul's avatar
Paul committed
35
36
};

Shucai Xiao's avatar
Shucai Xiao committed
37
38
39
} // namespace detail
} // namespace pybind11

Paul's avatar
Paul committed
40
template <class F>
Paul's avatar
Paul committed
41
42
void visit_type(const migraphx::shape& s, F f)
{
Shucai Xiao's avatar
Shucai Xiao committed
43
    s.visit_type(f);
Paul's avatar
Paul committed
44
45
}

Paul's avatar
Paul committed
46
47
48
template <class T, class F>
void visit(const migraphx::raw_data<T>& x, F f)
{
Shucai Xiao's avatar
Shucai Xiao committed
49
    x.visit(f);
Paul's avatar
Paul committed
50
51
}

Paul's avatar
Paul committed
52
53
54
template <class F>
void visit_types(F f)
{
Shucai Xiao's avatar
Shucai Xiao committed
55
    migraphx::shape::visit_types(f);
Paul's avatar
Paul committed
56
57
}

Paul's avatar
Paul committed
58
template <class T>
Paul's avatar
Paul committed
59
60
61
py::buffer_info to_buffer_info(T& x)
{
    migraphx::shape s = x.get_shape();
Paul's avatar
Paul committed
62
63
64
    auto strides      = s.strides();
    std::transform(
        strides.begin(), strides.end(), strides.begin(), [&](auto i) { return i * s.type_size(); });
Paul's avatar
Paul committed
65
66
    py::buffer_info b;
    visit_type(s, [&](auto as) {
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
        // migraphx use int8_t data to store bool type, we need to
        // explicitly specify the data type as bool for python
        if(s.type() == migraphx::shape::bool_type)
        {
            b = py::buffer_info(x.data(),
                                as.size(),
                                py::format_descriptor<bool>::format(),
                                s.lens().size(),
                                s.lens(),
                                strides);
        }
        else
        {
            b = py::buffer_info(x.data(),
                                as.size(),
                                py::format_descriptor<decltype(as())>::format(),
                                s.lens().size(),
                                s.lens(),
                                strides);
        }
Paul's avatar
Paul committed
87
88
89
90
    });
    return b;
}

Paul's avatar
Paul committed
91
92
93
migraphx::shape to_shape(const py::buffer_info& info)
{
    migraphx::shape::type_t t;
94
    std::size_t n = 0;
Paul's avatar
Paul committed
95
    visit_types([&](auto as) {
Shucai Xiao's avatar
Shucai Xiao committed
96
97
98
        if(info.format == py::format_descriptor<decltype(as())>::format() or
           (info.format == "l" and py::format_descriptor<decltype(as())>::format() == "q") or
           (info.format == "L" and py::format_descriptor<decltype(as())>::format() == "Q"))
Paul's avatar
Paul committed
99
        {
Paul's avatar
Paul committed
100
            t = as.type_enum();
101
102
            n = sizeof(as());
        }
Shucai Xiao's avatar
Shucai Xiao committed
103
104
105
106
107
        else if(info.format == "?" and py::format_descriptor<decltype(as())>::format() == "b")
        {
            t = migraphx::shape::bool_type;
            n = sizeof(bool);
        }
108
    });
109

Shucai Xiao's avatar
Shucai Xiao committed
110
    if(n == 0)
111
    {
Shucai Xiao's avatar
Shucai Xiao committed
112
        MIGRAPHX_THROW("MIGRAPHX PYTHON: Unsupported data type " + info.format);
113
114
    }

115
116
    auto strides = info.strides;
    std::transform(strides.begin(), strides.end(), strides.begin(), [&](auto i) -> std::size_t {
Paul's avatar
Paul committed
117
        return n > 0 ? i / n : 0;
Paul's avatar
Paul committed
118
    });
119
120
121
122
123
124
125
126
127
128

    // scalar support
    if(info.shape.empty())
    {
        return migraphx::shape{t};
    }
    else
    {
        return migraphx::shape{t, info.shape, strides};
    }
Paul's avatar
Paul committed
129
130
}

Paul's avatar
Paul committed
131
132
PYBIND11_MODULE(migraphx, m)
{
Paul's avatar
Paul committed
133
134
135
136
137
138
139
140
141
142
143
144
    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
145
        .def("scalar", &migraphx::shape::scalar)
Paul's avatar
Paul committed
146
147
        .def("__eq__", std::equal_to<migraphx::shape>{})
        .def("__ne__", std::not_equal_to<migraphx::shape>{})
Paul's avatar
Paul committed
148
        .def("__repr__", [](const migraphx::shape& s) { return migraphx::to_string(s); });
Paul's avatar
Paul committed
149
150

    py::class_<migraphx::argument>(m, "argument", py::buffer_protocol())
Paul's avatar
Paul committed
151
        .def_buffer([](migraphx::argument& x) -> py::buffer_info { return to_buffer_info(x); })
Paul's avatar
Paul committed
152
153
154
155
156
        .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
157
        .def("get_shape", &migraphx::argument::get_shape)
Paul's avatar
Paul committed
158
159
160
161
162
163
        .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
164
165
166
        .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
167

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

Paul's avatar
Paul committed
170
    py::class_<migraphx::program>(m, "program")
171
        .def("clone", [](migraphx::program& p) { return *(new migraphx::program(p)); })
172
        .def("get_parameter_names", &migraphx::program::get_parameter_names)
Paul's avatar
Paul committed
173
        .def("get_parameter_shapes", &migraphx::program::get_parameter_shapes)
174
        .def("get_output_shapes", &migraphx::program::get_output_shapes)
kahmed10's avatar
kahmed10 committed
175
176
177
178
179
180
181
182
183
184
185
        .def(
            "compile",
            [](migraphx::program& p, const migraphx::target& t, bool offload_copy, bool fast_math) {
                migraphx::compile_options options;
                options.offload_copy = offload_copy;
                options.fast_math    = fast_math;
                p.compile(t, options);
            },
            py::arg("t"),
            py::arg("offload_copy") = true,
            py::arg("fast_math")    = true)
186
187
188
189
190
191
192
193
194
195
196
197
        .def("run",
             [](migraphx::program& p, py::dict params) {
                 migraphx::program::parameter_map pm;
                 for(auto x : params)
                 {
                     std::string key      = x.first.cast<std::string>();
                     py::buffer b         = x.second.cast<py::buffer>();
                     py::buffer_info info = b.request();
                     pm[key]              = migraphx::argument(to_shape(info), info.ptr);
                 }
                 return p.eval(pm);
             })
198
        .def("sort", &migraphx::program::sort)
Paul's avatar
Paul committed
199
200
        .def("__eq__", std::equal_to<migraphx::program>{})
        .def("__ne__", std::not_equal_to<migraphx::program>{})
Paul's avatar
Paul committed
201
        .def("__repr__", [](const migraphx::program& p) { return migraphx::to_string(p); });
Paul's avatar
Paul committed
202

Khalique's avatar
Khalique committed
203
    m.def("parse_tf",
204
205
206
          [](const std::string& filename, bool is_nhwc, unsigned int batch_size) {
              return migraphx::parse_tf(filename, migraphx::tf_options{is_nhwc, batch_size});
          },
Khalique's avatar
Khalique committed
207
208
          "Parse tf protobuf (default format is nhwc)",
          py::arg("filename"),
209
210
          py::arg("is_nhwc")    = true,
          py::arg("batch_size") = 1);
211

212
    m.def("parse_onnx",
213
          [](const std::string& filename,
214
             unsigned int default_dim_value,
215
             std::unordered_map<std::string, std::vector<std::size_t>> map_input_dims,
216
217
             bool skip_unknown_operators,
             bool print_program_on_error) {
218
              migraphx::onnx_options options;
219
220
221
222
              options.default_dim_value      = default_dim_value;
              options.map_input_dims         = map_input_dims;
              options.skip_unknown_operators = skip_unknown_operators;
              options.print_program_on_error = print_program_on_error;
223
              return migraphx::parse_onnx(filename, options);
224
225
226
          },
          "Parse onnx file",
          py::arg("filename"),
227
228
229
230
          py::arg("default_dim_value") = 1,
          py::arg("map_input_dims") = std::unordered_map<std::string, std::vector<std::size_t>>(),
          py::arg("skip_unknown_operators") = false,
          py::arg("print_program_on_error") = false);
Paul's avatar
Paul committed
231

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
    m.def("parse_onnx_buffer",
          [](const std::string& onnx_buffer,
             unsigned int default_dim_value,
             std::unordered_map<std::string, std::vector<std::size_t>> map_input_dims,
             bool skip_unknown_operators,
             bool print_program_on_error) {
              migraphx::onnx_options options;
              options.default_dim_value      = default_dim_value;
              options.map_input_dims         = map_input_dims;
              options.skip_unknown_operators = skip_unknown_operators;
              options.print_program_on_error = print_program_on_error;
              return migraphx::parse_onnx_buffer(onnx_buffer, options);
          },
          "Parse onnx file",
          py::arg("filename"),
          py::arg("default_dim_value") = 1,
          py::arg("map_input_dims") = std::unordered_map<std::string, std::vector<std::size_t>>(),
          py::arg("skip_unknown_operators") = false,
          py::arg("print_program_on_error") = false);

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
    m.def("load",
          [](const std::string& name, const std::string& format) {
              migraphx::file_options options;
              options.format = format;
              return migraphx::load(name, options);
          },
          "Load MIGraphX program",
          py::arg("filename"),
          py::arg("format") = "msgpack");

    m.def("save",
          [](const migraphx::program& p, const std::string& name, const std::string& format) {
              migraphx::file_options options;
              options.format = format;
              return migraphx::save(p, name, options);
          },
          "Save MIGraphX program",
          py::arg("p"),
          py::arg("filename"),
          py::arg("format") = "msgpack");
Paul's avatar
Paul committed
272

273
    m.def("get_target", &migraphx::make_target);
Paul's avatar
Paul committed
274
    m.def("generate_argument", &migraphx::generate_argument, py::arg("s"), py::arg("seed") = 0);
Shucai Xiao's avatar
Shucai Xiao committed
275
276
277
278
279
280
281
282
283
284
    m.def("quantize_fp16",
          &migraphx::quantize_fp16,
          py::arg("prog"),
          py::arg("ins_names") = std::vector<std::string>{"all"});
    m.def("quantize_int8",
          &migraphx::quantize_int8,
          py::arg("prog"),
          py::arg("t"),
          py::arg("calibration") = std::vector<migraphx::program::parameter_map>{},
          py::arg("ins_names")   = std::vector<std::string>{"dot", "convolution"});
Shucai Xiao's avatar
Shucai Xiao committed
285

Paul's avatar
Paul committed
286
287
288
289
290
291
292
#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);
#endif

Paul's avatar
Paul committed
293
294
295
296
297
298
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}