| 56 | } |
| 57 | |
| 58 | static StorageView create_view_from_array(py::object array) { |
| 59 | auto device = Device::CPU; |
| 60 | |
| 61 | py::object interface_obj = py::getattr(array, "__array_interface__", py::none()); |
| 62 | if (interface_obj.is_none()) { |
| 63 | interface_obj = py::getattr(array, "__cuda_array_interface__", py::none()); |
| 64 | if (interface_obj.is_none()) |
| 65 | throw std::invalid_argument("Object does not implement the array interface"); |
| 66 | device = Device::CUDA; |
| 67 | } |
| 68 | |
| 69 | py::dict interface = interface_obj.cast<py::dict>(); |
| 70 | if (interface_obj.contains("strides") && !interface_obj["strides"].is_none()) |
| 71 | throw std::invalid_argument("StorageView does not support arrays with non contiguous memory"); |
| 72 | |
| 73 | auto shape = interface["shape"].cast<Shape>(); |
| 74 | auto dtype = typestr_to_dtype(interface["typestr"].cast<std::string>()); |
| 75 | auto data = interface["data"].cast<py::tuple>(); |
| 76 | auto ptr = data[0].cast<uintptr_t>(); |
| 77 | auto read_only = data[1].cast<bool>(); |
| 78 | |
| 79 | if (read_only) |
| 80 | throw std::invalid_argument("StorageView does not support read-only arrays"); |
| 81 | |
| 82 | StorageView view(dtype, device); |
| 83 | view.view((void*)ptr, std::move(shape)); |
| 84 | return view; |
| 85 | } |
| 86 | |
| 87 | static py::dict get_array_interface(const StorageView& view) { |
| 88 | py::tuple shape(view.rank()); |
nothing calls this directly
no test coverage detected