| 36 | using namespace mlx::data; |
| 37 | |
| 38 | void init_mlx_data_buffer(py::module& m) { |
| 39 | py::class_<PyBufferIterator>(m, "BufferIterator") |
| 40 | .def( |
| 41 | "__iter__", |
| 42 | [](PyBufferIterator& it) -> PyBufferIterator& { return it; }) |
| 43 | .def("__next__", &PyBufferIterator::next); |
| 44 | |
| 45 | auto buffer_class = |
| 46 | py::class_< |
| 47 | Buffer, |
| 48 | std::unique_ptr<Buffer, mlx::pybind::NogilDeleter<Buffer>>>( |
| 49 | m, "Buffer") |
| 50 | .def("__iter__", [](Buffer& buf) { return PyBufferIterator(buf); }) |
| 51 | .def("size", &Buffer::size, py::call_guard<py::gil_scoped_release>()) |
| 52 | .def( |
| 53 | "__len__", |
| 54 | &Buffer::size, |
| 55 | py::call_guard<py::gil_scoped_release>()) |
| 56 | .def( |
| 57 | "__getitem__", |
| 58 | [](const Buffer& b, int64_t idx) { |
| 59 | Sample sample; |
| 60 | { |
| 61 | py::gil_scoped_release release; |
| 62 | idx = (idx < 0) ? idx + b.size() : idx; |
| 63 | sample = b.get(idx); |
| 64 | } |
| 65 | py::dict pysample; |
| 66 | for (auto& item : sample) { |
| 67 | pysample[py::str(item.first)] = |
| 68 | mlx::pybind::to_py_array(item.second); |
| 69 | } |
| 70 | return pysample; |
| 71 | }, |
| 72 | py::arg("idx")) |
| 73 | .def( |
| 74 | "__repr__", |
| 75 | [](const Buffer& b) { |
| 76 | std::stringstream msg; |
| 77 | msg << "Buffer(size=" << b.size() << ", keys={"; |
| 78 | if (b.size() > 0) { |
| 79 | Sample first = b.get(0); |
| 80 | int cnt = 0; |
| 81 | for (auto& item : first) { |
| 82 | if (cnt++ > 0) { |
| 83 | msg << ", "; |
| 84 | } |
| 85 | msg << "'" << item.first << "'"; |
| 86 | } |
| 87 | } |
| 88 | msg << "})"; |
| 89 | |
| 90 | return msg.str(); |
| 91 | }) |
| 92 | .def( |
| 93 | "batch", |
| 94 | [](const Buffer& b, |
| 95 | const std::variant<int64_t, std::vector<int64_t>>& batch_size, |
no test coverage detected