| 308 | } // namespace |
| 309 | |
| 310 | void init_utils(py::module m) { |
| 311 | auto atexit = py::module::import("atexit"); |
| 312 | atexit.attr("register")(py::cpp_function([]() { g_global_finalized = true; })); |
| 313 | |
| 314 | py::class_<std::atomic<uint64_t>>(m, "AtomicUint64") |
| 315 | .def(py::init<>()) |
| 316 | .def(py::init<uint64_t>()) |
| 317 | .def("load", [](const std::atomic<uint64_t>& self) { return self.load(); }) |
| 318 | .def("store", [](std::atomic<uint64_t>& self, |
| 319 | uint64_t value) { return self.store(value); }) |
| 320 | .def("fetch_add", [](std::atomic<uint64_t>& self, |
| 321 | uint64_t value) { return self.fetch_add(value); }) |
| 322 | .def("fetch_sub", [](std::atomic<uint64_t>& self, |
| 323 | uint64_t value) { return self.fetch_sub(value); }) |
| 324 | .def(py::self += uint64_t()) |
| 325 | .def(py::self -= uint64_t()); |
| 326 | |
| 327 | // FIXME!!! Should add a submodule instead of using a class for logger |
| 328 | py::class_<LoggerWrapper> logger(m, "Logger"); |
| 329 | logger.def(py::init<>()) |
| 330 | .def_static("set_log_level", &LoggerWrapper::set_log_level) |
| 331 | .def_static("set_log_handler", &LoggerWrapper::set_log_handler); |
| 332 | |
| 333 | py::enum_<LoggerWrapper::LogLevel>(logger, "LogLevel") |
| 334 | .value("Debug", LoggerWrapper::LogLevel::DEBUG) |
| 335 | .value("Info", LoggerWrapper::LogLevel::INFO) |
| 336 | .value("Warn", LoggerWrapper::LogLevel::WARN) |
| 337 | .value("Error", LoggerWrapper::LogLevel::ERROR); |
| 338 | |
| 339 | m.def("_get_dtype_num", &_get_dtype_num, "Convert numpy dtype to internal dtype"); |
| 340 | |
| 341 | m.def("_get_serialized_dtype", &_get_serialized_dtype, |
| 342 | "Convert numpy dtype to internal dtype for serialization"); |
| 343 | |
| 344 | m.def("_get_device_count", &mgb::CompNode::get_device_count, |
| 345 | "Get total number of specific devices on this system"); |
| 346 | |
| 347 | m.def("_try_coalesce_all_free_memory", &mgb::CompNode::try_coalesce_all_free_memory, |
| 348 | "This function will try it best to free all consecutive free chunks back to " |
| 349 | "operating system"); |
| 350 | |
| 351 | using mgb::imperative::TensorSanityCheck; |
| 352 | py::class_<TensorSanityCheck>(m, "TensorSanityCheckImpl") |
| 353 | .def(py::init<>()) |
| 354 | .def("enable", |
| 355 | [](TensorSanityCheck& checker) -> TensorSanityCheck& { |
| 356 | checker.enable(); |
| 357 | return checker; |
| 358 | }) |
| 359 | .def("disable", [](TensorSanityCheck& checker) { checker.disable(); }); |
| 360 | |
| 361 | #if MGB_ENABLE_OPR_MM |
| 362 | m.def("create_mm_server", &mgb::opr::create_zmqrpc_server, py::arg("addr"), |
| 363 | py::arg("port") = 0); |
| 364 | #else |
| 365 | m.def("create_mm_server", []() {}); |
| 366 | #endif |
| 367 |
no test coverage detected