| 66 | }; |
| 67 | |
| 68 | void init_mlx_data_stream(py::module& m) { |
| 69 | auto py_stream_next = [](Stream& stream) { |
| 70 | Sample res; |
| 71 | { |
| 72 | py::gil_scoped_release release; |
| 73 | res = stream.next(); |
| 74 | } |
| 75 | auto pyres = mlx::pybind::to_py_sample(res); |
| 76 | return pyres; |
| 77 | }; |
| 78 | |
| 79 | auto stream_class = |
| 80 | py::class_< |
| 81 | Stream, |
| 82 | std::unique_ptr<Stream, mlx::pybind::NogilDeleter<Stream>>>( |
| 83 | m, "Stream") |
| 84 | .def("__iter__", [](Stream& stream) { return stream; }) |
| 85 | .def( |
| 86 | "__next__", |
| 87 | [](Stream& stream) { |
| 88 | Sample res; |
| 89 | { |
| 90 | py::gil_scoped_release release; |
| 91 | res = stream.next(); |
| 92 | } |
| 93 | if (res.empty()) { |
| 94 | throw py::stop_iteration(); |
| 95 | } else { |
| 96 | py::dict pyres; |
| 97 | std::for_each( |
| 98 | res.begin(), |
| 99 | res.end(), |
| 100 | [&pyres](std::pair< |
| 101 | const std::string, |
| 102 | std::shared_ptr<mlx::data::Array>>& a) { |
| 103 | if (a.second == nullptr) { |
| 104 | throw std::runtime_error( |
| 105 | "Stream: empty value for " + a.first + "key"); |
| 106 | } |
| 107 | pyres[py::str(a.first)] = |
| 108 | mlx::pybind::to_py_array(a.second); |
| 109 | }); |
| 110 | return pyres; |
| 111 | } |
| 112 | }) |
| 113 | .def("__call__", py_stream_next) |
| 114 | .def("__repr__", [](const Stream& s) { return "Stream()"; }) |
| 115 | .def("next", py_stream_next) |
| 116 | .def( |
| 117 | "reset", |
| 118 | &Stream::reset, |
| 119 | py::call_guard<py::gil_scoped_release>(), |
| 120 | R"pbcopy( |
| 121 | Reset the stream so that it can be iterated upon again. |
| 122 | )pbcopy") |
| 123 | .def( |
| 124 | "batch", |
| 125 | &Stream::batch, |
no test coverage detected