#include #include #include #include #include #include #include #ifdef ENABLE_TF #include #else #include #endif #ifdef HAVE_GPU #include #include #endif namespace py = pybind11; template struct throw_half { F f; template void operator()(A a) const { f(a); } void operator()(migraphx::shape::as) const { throw std::runtime_error("Half not supported in python yet."); } void operator()(migraphx::tensor_view) const { throw std::runtime_error("Half not supported in python yet."); } }; template struct skip_half { F f; template void operator()(A a) const { f(a); } void operator()(migraphx::shape::as) const {} void operator()(migraphx::tensor_view) const {} }; template void visit_type(const migraphx::shape& s, F f) { s.visit_type(throw_half{f}); } template void visit(const migraphx::raw_data& x, F f) { x.visit(throw_half{f}); } template void visit_types(F f) { migraphx::shape::visit_types(skip_half{f}); } template py::buffer_info to_buffer_info(T& x) { migraphx::shape s = x.get_shape(); auto strides = s.strides(); std::transform( strides.begin(), strides.end(), strides.begin(), [&](auto i) { return i * s.type_size(); }); py::buffer_info b; visit_type(s, [&](auto as) { b = py::buffer_info(x.data(), as.size(), py::format_descriptor::format(), s.lens().size(), s.lens(), strides); }); return b; } migraphx::shape to_shape(const py::buffer_info& info) { migraphx::shape::type_t t; std::size_t n = 0; visit_types([&](auto as) { if(info.format == py::format_descriptor::format()) { t = as.type_enum(); n = sizeof(as()); } }); auto strides = info.strides; std::transform(strides.begin(), strides.end(), strides.begin(), [&](auto i) -> std::size_t { return n > 0 ? i / n : 0; }); return migraphx::shape{t, info.shape, strides}; } PYBIND11_MODULE(migraphx, m) { py::class_(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) .def("__eq__", std::equal_to{}) .def("__ne__", std::not_equal_to{}) .def("__repr__", [](const migraphx::shape& s) { return migraphx::to_string(s); }); py::class_(m, "argument", py::buffer_protocol()) .def_buffer([](migraphx::argument& x) -> py::buffer_info { return to_buffer_info(x); }) .def("__init__", [](migraphx::argument& x, py::buffer b) { py::buffer_info info = b.request(); new(&x) migraphx::argument(to_shape(info), info.ptr); }) .def("get_shape", &migraphx::argument::get_shape) .def("tolist", [](migraphx::argument& x) { py::list l{x.get_shape().elements()}; visit(x, [&](auto data) { l = py::cast(data.to_vector()); }); return l; }) .def("__eq__", std::equal_to{}) .def("__ne__", std::not_equal_to{}) .def("__repr__", [](const migraphx::argument& x) { return migraphx::to_string(x); }); py::class_(m, "target"); py::class_(m, "program") .def("get_parameter_shapes", &migraphx::program::get_parameter_shapes) .def("get_shape", &migraphx::program::get_shape) .def("compile", [](migraphx::program& p, const migraphx::target& t) { p.compile(t); }) .def("run", &migraphx::program::eval) .def("__eq__", std::equal_to{}) .def("__ne__", std::not_equal_to{}) .def("__repr__", [](const migraphx::program& p) { return migraphx::to_string(p); }); #ifdef ENABLE_TF m.def("parse_tf", &migraphx::parse_tf, "Parse tf protobuf (default format is nhwc)", py::arg("filename"), py::arg("is_nhwc") = true); #else m.def("parse_onnx", &migraphx::parse_onnx); #endif m.def("get_target", [](const std::string& name) -> migraphx::target { if(name == "cpu") return migraphx::cpu::target{}; #ifdef HAVE_GPU if(name == "gpu") return migraphx::gpu::target{}; #endif throw std::runtime_error("Target not found: " + name); }); m.def("generate_argument", &migraphx::generate_argument, py::arg("s"), py::arg("seed") = 0); m.def("quantize", &migraphx::quantize); #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 #ifdef VERSION_INFO m.attr("__version__") = VERSION_INFO; #else m.attr("__version__") = "dev"; #endif }