".github/git@developer.sourcefind.cn:zhaoyu6/sglang.git" did not exist on "cd85b78f94e1f273a7041f66023f6715d0f2ef00"
migraphx_py.cpp 3.37 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/onnx.hpp>
Paul's avatar
Paul committed
8
#include <migraphx/stringutils.hpp>
Paul's avatar
Paul committed
9
10
11
12
#ifdef HAVE_GPU
#include <migraphx/gpu/target.hpp>
#include <migraphx/gpu/hip.hpp>
#endif
Paul's avatar
Paul committed
13
14
15

namespace py = pybind11;

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

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

Paul's avatar
Paul committed
33
template <class F>
Paul's avatar
Paul committed
34
35
36
37
38
void visit_type(const migraphx::shape& s, F f)
{
    s.visit_type(skip_half<F>{f});
}

Paul's avatar
Paul committed
39
template <class T>
Paul's avatar
Paul committed
40
41
42
43
44
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
45
46
47
48
49
50
        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
51
52
53
54
    });
    return b;
}

Paul's avatar
Paul committed
55
56
PYBIND11_MODULE(migraphx, m)
{
Paul's avatar
Paul committed
57
58
59
60
61
62
63
64
65
66
67
68
    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
69
        .def("scalar", &migraphx::shape::scalar)
Paul's avatar
Paul committed
70
        .def("__repr__", [](const migraphx::shape& s) { return migraphx::to_string(s); });
Paul's avatar
Paul committed
71
72

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

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

Paul's avatar
Paul committed
77
78
    py::class_<migraphx::program>(m, "program")
        .def("get_parameter_shapes", &migraphx::program::get_parameter_shapes)
Paul's avatar
Paul committed
79
        .def("compile", [](migraphx::program& p, const migraphx::target& t) { p.compile(t); })
Paul's avatar
Paul committed
80
        .def("run", &migraphx::program::eval)
Paul's avatar
Paul committed
81
        .def("__repr__", [](const migraphx::program& p) { return migraphx::to_string(p); });
Paul's avatar
Paul committed
82
83
84

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

Paul's avatar
Paul committed
85
    m.def("get_target", [](const std::string& name) -> migraphx::target {
Paul's avatar
Paul committed
86
        if(name == "cpu")
Paul's avatar
Paul committed
87
            return migraphx::cpu::target{};
Paul's avatar
Paul committed
88
89
90
91
#ifdef HAVE_GPU
        if(name == "gpu")
            return migraphx::gpu::target{};
#endif
Paul's avatar
Paul committed
92
93
94
95
96
        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
97
98
99
100
101
102
103
104
#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
105
106
107
108
109
110
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}