#include #include #include #include #include #include #include #include #include #include #include namespace pybind11 { namespace detail { template <> struct type_caster { public: PYBIND11_TYPE_CASTER(torch::monitor::data_value_t, _("data_value_t")); // Python -> C++ bool load(handle src, bool) { PyObject* source = src.ptr(); if (THPUtils_checkLong(source)) { this->value = THPUtils_unpackLong(source); } else if (THPUtils_checkDouble(source)) { this->value = THPUtils_unpackDouble(source); } else if (THPUtils_checkString(source)) { this->value = THPUtils_unpackString(source); } else if (PyBool_Check(source)) { this->value = THPUtils_unpackBool(source); } else { return false; } return !PyErr_Occurred(); } // C++ -> Python static handle cast( torch::monitor::data_value_t src, return_value_policy /* policy */, handle /* parent */) { if (c10::holds_alternative(src)) { return PyFloat_FromDouble(c10::get(src)); } else if (c10::holds_alternative(src)) { return THPUtils_packInt64(c10::get(src)); } else if (c10::holds_alternative(src)) { if (c10::get(src)) { Py_RETURN_TRUE; } else { Py_RETURN_FALSE; } } else if (c10::holds_alternative(src)) { std::string str = c10::get(src); return THPUtils_packString(str); } throw std::runtime_error("unknown data_value_t type"); } }; } // namespace detail } // namespace pybind11 namespace torch { namespace monitor { namespace { class PythonEventHandler : public EventHandler { public: explicit PythonEventHandler(std::function handler) : handler_(std::move(handler)) {} void handle(const Event& e) override { handler_(e); } private: std::function handler_; }; } // namespace void initMonitorBindings(PyObject* module) { auto rootModule = py::handle(module).cast(); auto m = rootModule.def_submodule("_monitor"); py::enum_(m, "Aggregation") .value("VALUE", Aggregation::NONE) .value("MEAN", Aggregation::MEAN) .value("COUNT", Aggregation::COUNT) .value("SUM", Aggregation::SUM) .value("MAX", Aggregation::MAX) .value("MIN", Aggregation::MIN) .export_values(); py::class_>(m, "Stat") .def("add", &Stat::add) .def("get", &Stat::get) .def_property_readonly("name", &Stat::name) .def_property_readonly("count", &Stat::count); py::class_, Stat>(m, "IntervalStat") .def(py::init< std::string, std::vector, std::chrono::milliseconds>()); py::class_, Stat>(m, "FixedCountStat") .def(py::init, int64_t>()); py::class_(m, "Event") .def( py::init([](const std::string& name, std::chrono::system_clock::time_point timestamp, std::unordered_map data) { Event e; e.name = name; e.timestamp = timestamp; e.data = data; return e; }), py::arg("name"), py::arg("timestamp"), py::arg("data")) .def_readwrite("name", &Event::name) .def_readwrite("timestamp", &Event::timestamp) .def_readwrite("data", &Event::data); m.def("log_event", &logEvent); py::class_ dataClass(m, "data_value_t"); py::implicitly_convertible(); py::implicitly_convertible(); py::implicitly_convertible(); py::implicitly_convertible(); py::class_> eventHandlerClass(m, "PythonEventHandler"); m.def("register_event_handler", [](std::function f) { auto handler = std::make_shared(f); registerEventHandler(handler); return handler; }); m.def( "unregister_event_handler", [](std::shared_ptr handler) { unregisterEventHandler(handler); }); } } // namespace monitor } // namespace torch