| 227 | |
| 228 | |
| 229 | PYBIND11_MODULE(python_function_plugin, m, py::mod_gil_not_used()) { |
| 230 | m.def("current_dali_stream", []() { return reinterpret_cast<uint64_t>(GetCurrentStream()); }); |
| 231 | |
| 232 | m.def("DLTensorToArray", [](py::capsule dl_capsule) { |
| 233 | auto dlm_tensor_ptr = DLMTensorPtrFromCapsule(dl_capsule); |
| 234 | const auto &dl_tensor = dlm_tensor_ptr->dl_tensor; |
| 235 | auto dali_type = ToDALIType(dl_tensor.dtype); |
| 236 | py::dtype dtype(FormatStrFromType(dali_type)); |
| 237 | auto shape = make_span(dl_tensor.shape, dl_tensor.ndim); |
| 238 | py::array array; |
| 239 | if (dl_tensor.strides) { |
| 240 | TensorShape<> strides; |
| 241 | strides.resize(dl_tensor.ndim); |
| 242 | for (int i = 0; i < dl_tensor.ndim; ++i) { |
| 243 | strides[i] = dl_tensor.strides[i] * dtype.itemsize(); |
| 244 | } |
| 245 | array = py::array(dtype, shape, strides, dl_tensor.data, py::array()); |
| 246 | } else { |
| 247 | array = py::array(dtype, shape, dl_tensor.data, py::array()); |
| 248 | } |
| 249 | return array; |
| 250 | }); |
| 251 | |
| 252 | m.def("ArrayToDLTensor", [](py::array array) { |
| 253 | auto rsrc = GetDLTensorResource(array); |
| 254 | return DLTensorToCapsule(ToDLMTensor(std::move(rsrc))); |
| 255 | }); |
| 256 | |
| 257 | // For the _a suffix |
| 258 | using namespace pybind11::literals; // NOLINT |
| 259 | |
| 260 | py::class_<DLTensorObj>(m, "DLTensorObj") |
| 261 | .def("__dlpack_device__", &DLTensorObj::dlpack_device) |
| 262 | .def( |
| 263 | "__dlpack__", |
| 264 | [](DLTensorObj &self, std::optional<int64_t> stream) { |
| 265 | std::optional<cudaStream_t> cuda_stream{}; |
| 266 | if (stream.has_value()) { |
| 267 | cuda_stream = reinterpret_cast<cudaStream_t>(*stream); |
| 268 | } else { |
| 269 | if (std::get<0>(self.dlpack_device()) == kDLCUDA) |
| 270 | cuda_stream = cudaStream_t(cudaStreamDefault); |
| 271 | } |
| 272 | DLManagedTensor *data_ptr = self.dlpack(cuda_stream); |
| 273 | return py::capsule(data_ptr, DLTENSOR_NAME, &DLTensorCapsuleDestructor); |
| 274 | }, |
| 275 | "stream"_a); |
| 276 | } |
| 277 | |
| 278 | } // namespace dali |
nothing calls this directly
no test coverage detected