| 1 | /* Tencent is pleased to support the open source community by making ncnn available. |
| 2 | * |
| 3 | * Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | * |
| 5 | * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | * in compliance with the License. You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://opensource.org/licenses/BSD-3-Clause |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software distributed |
| 11 | * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | * specific language governing permissions and limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <pybind11/pybind11.h> |
| 17 | #include <pybind11/stl.h> |
| 18 | #include <pybind11/numpy.h> |
| 19 | #include <pybind11/functional.h> |
| 20 | |
| 21 | #include <cpu.h> |
| 22 | #include <gpu.h> |
| 23 | #include <net.h> |
| 24 | #include <option.h> |
| 25 | #include <blob.h> |
| 26 | #include <paramdict.h> |
| 27 | |
| 28 | #include "pybind11_mat.h" |
| 29 | #include "pybind11_datareader.h" |
| 30 | #include "pybind11_allocator.h" |
| 31 | #include "pybind11_modelbin.h" |
| 32 | #include "pybind11_layer.h" |
| 33 | using namespace ncnn; |
| 34 | |
| 35 | namespace py = pybind11; |
| 36 | |
| 37 | class DataReaderFromMemoryCopy : public DataReaderFromMemory |
| 38 | { |
| 39 | public: |
| 40 | explicit DataReaderFromMemoryCopy(const unsigned char*& mem) |
| 41 | : DataReaderFromMemory(mem) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | virtual size_t reference(size_t size, const void** buf) const |
| 46 | { |
| 47 | return 0; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | struct LayerFactory |
| 52 | { |
| 53 | std::string name; |
| 54 | int index; |
| 55 | std::function<Layer*()> creator; |
| 56 | std::function<void(Layer*)> destroyer; |
| 57 | layer_creator_func creator_func; |
| 58 | layer_destroyer_func destroyer_func; |
| 59 | }; |
| 60 | |
| 61 | #define LayerFactoryDeclear(n) \ |
| 62 | static ncnn::Layer* LayerCreator##n(void*); \ |
| 63 | static void LayerDestroyer##n(ncnn::Layer*, void*); |
| 64 | |
| 65 | LayerFactoryDeclear(0); |
| 66 | LayerFactoryDeclear(1); |
| 67 | LayerFactoryDeclear(2); |
| 68 | LayerFactoryDeclear(3); |
| 69 | LayerFactoryDeclear(4); |
| 70 | LayerFactoryDeclear(5); |
| 71 | LayerFactoryDeclear(6); |
| 72 | LayerFactoryDeclear(7); |
| 73 | LayerFactoryDeclear(8); |
| 74 | LayerFactoryDeclear(9); |
| 75 | |
| 76 | std::vector<LayerFactory> g_layer_factroys = { |
| 77 | {"", -1, nullptr, nullptr, LayerCreator0, LayerDestroyer0}, |
| 78 | {"", -1, nullptr, nullptr, LayerCreator1, LayerDestroyer1}, |
| 79 | {"", -1, nullptr, nullptr, LayerCreator2, LayerDestroyer2}, |
| 80 | {"", -1, nullptr, nullptr, LayerCreator3, LayerDestroyer3}, |
| 81 | {"", -1, nullptr, nullptr, LayerCreator4, LayerDestroyer4}, |
| 82 | {"", -1, nullptr, nullptr, LayerCreator5, LayerDestroyer5}, |
| 83 | {"", -1, nullptr, nullptr, LayerCreator6, LayerDestroyer6}, |
| 84 | {"", -1, nullptr, nullptr, LayerCreator7, LayerDestroyer7}, |
| 85 | {"", -1, nullptr, nullptr, LayerCreator8, LayerDestroyer8}, |
| 86 | {"", -1, nullptr, nullptr, LayerCreator9, LayerDestroyer9}, |
| 87 | }; |
| 88 | int g_layer_factroy_index = 0; |
| 89 | |
| 90 | #define LayerFactoryDefine(n) \ |
| 91 | static ncnn::Layer* LayerCreator##n(void* p) \ |
| 92 | { \ |
| 93 | if (g_layer_factroys[n].creator != nullptr) \ |
| 94 | { \ |
| 95 | return g_layer_factroys[n].creator(); \ |
| 96 | } \ |
| 97 | return nullptr; \ |
| 98 | } \ |
| 99 | static void LayerDestroyer##n(ncnn::Layer* layer, void* p) \ |
| 100 | { \ |
| 101 | if (g_layer_factroys[n].destroyer) \ |
| 102 | { \ |
| 103 | g_layer_factroys[n].destroyer(layer); \ |
| 104 | } \ |
| 105 | } |
| 106 | |
| 107 | LayerFactoryDefine(0); |
| 108 | LayerFactoryDefine(1); |
| 109 | LayerFactoryDefine(2); |
| 110 | LayerFactoryDefine(3); |
| 111 | LayerFactoryDefine(4); |
| 112 | LayerFactoryDefine(5); |
| 113 | LayerFactoryDefine(6); |
| 114 | LayerFactoryDefine(7); |
| 115 | LayerFactoryDefine(8); |
| 116 | LayerFactoryDefine(9); |
| 117 | |
| 118 | PYBIND11_MODULE(ncnn, m) |
| 119 | { |
| 120 | auto atexit = py::module_::import("atexit"); |
| 121 | atexit.attr("register")(py::cpp_function([]() { |
| 122 | for (int i = 0; i < g_layer_factroys.size(); i++) |
| 123 | { |
| 124 | g_layer_factroys[i].creator = nullptr; |
| 125 | g_layer_factroys[i].destroyer = nullptr; |
| 126 | } |
| 127 | })); |
| 128 | |
| 129 | py::class_<Allocator, PyAllocator<> >(m, "Allocator"); |
| 130 | py::class_<PoolAllocator, Allocator, PyAllocatorOther<PoolAllocator> >(m, "PoolAllocator") |
| 131 | .def(py::init<>()) |
| 132 | .def("set_size_compare_ratio", &PoolAllocator::set_size_compare_ratio, py::arg("src")) |
| 133 | .def("clear", &PoolAllocator::clear) |
| 134 | .def("fastMalloc", &PoolAllocator::fastMalloc, py::arg("size")) |
| 135 | .def("fastFree", &PoolAllocator::fastFree, py::arg("ptr")); |
| 136 | py::class_<UnlockedPoolAllocator, Allocator, PyAllocatorOther<UnlockedPoolAllocator> >(m, "UnlockedPoolAllocator") |
| 137 | .def(py::init<>()) |
| 138 | .def("set_size_compare_ratio", &UnlockedPoolAllocator::set_size_compare_ratio, py::arg("src")) |
| 139 | .def("clear", &UnlockedPoolAllocator::clear) |
| 140 | .def("fastMalloc", &UnlockedPoolAllocator::fastMalloc, py::arg("size")) |
| 141 | .def("fastFree", &UnlockedPoolAllocator::fastFree, py::arg("ptr")); |
| 142 | |
| 143 | py::class_<DataReader, PyDataReader<> >(m, "DataReader") |
| 144 | .def(py::init<>()) |
| 145 | #if NCNN_STRING |
| 146 | .def("scan", &DataReader::scan, py::arg("format"), py::arg("p")) |
| 147 | #endif // NCNN_STRING |
| 148 | .def("read", &DataReader::read, py::arg("buf"), py::arg("size")); |
| 149 | py::class_<DataReaderFromEmpty, DataReader, PyDataReaderOther<DataReaderFromEmpty> >(m, "DataReaderFromEmpty") |
| 150 | .def(py::init<>()) |
| 151 | #if NCNN_STRING |
| 152 | .def("scan", &DataReaderFromEmpty::scan, py::arg("format"), py::arg("p")) |
| 153 | #endif // NCNN_STRING |
| 154 | .def("read", &DataReaderFromEmpty::read, py::arg("buf"), py::arg("size")); |
| 155 | |
| 156 | py::class_<Blob>(m, "Blob") |
| 157 | .def(py::init<>()) |
| 158 | #if NCNN_STRING |
| 159 | .def_readwrite("name", &Blob::name) |
| 160 | #endif // NCNN_STRING |
| 161 | .def_readwrite("producer", &Blob::producer) |
| 162 | .def_readwrite("consumer", &Blob::consumer) |
| 163 | .def_readwrite("shape", &Blob::shape); |
| 164 | |
| 165 | py::class_<ModelBin, PyModelBin<> >(m, "ModelBin") |
| 166 | .def(py::init<>()) |
| 167 | .def("load", (Mat(ModelBin::*)(int, int) const) & ModelBin::load, py::arg("w"), py::arg("type")) |
| 168 | .def("load", (Mat(ModelBin::*)(int, int, int) const) & ModelBin::load, py::arg("w"), py::arg("h"), py::arg("type")) |
| 169 | .def("load", (Mat(ModelBin::*)(int, int, int, int) const) & ModelBin::load, py::arg("w"), py::arg("h"), py::arg("c"), py::arg("type")) |
| 170 | .def("load", (Mat(ModelBin::*)(int, int, int, int, int) const) & ModelBin::load, py::arg("w"), py::arg("h"), py::arg("d"), py::arg("c"), py::arg("type")); |
| 171 | py::class_<ModelBinFromDataReader, ModelBin, PyModelBinOther<ModelBinFromDataReader> >(m, "ModelBinFromDataReader") |
| 172 | .def(py::init<const DataReader&>(), py::arg("dr")) |
| 173 | .def("load", &ModelBinFromDataReader::load, py::arg("w"), py::arg("type")); |
| 174 | py::class_<ModelBinFromMatArray, ModelBin, PyModelBinOther<ModelBinFromMatArray> >(m, "ModelBinFromMatArray") |
| 175 | .def(py::init<const Mat*>(), py::arg("weights")) |
| 176 | .def("load", &ModelBinFromMatArray::load, py::arg("w"), py::arg("type")); |
| 177 | |
| 178 | py::class_<ParamDict>(m, "ParamDict") |
| 179 | .def(py::init<>()) |
| 180 | .def("type", &ParamDict::type, py::arg("id")) |
| 181 | .def("get", (int (ParamDict::*)(int, int) const) & ParamDict::get, py::arg("id"), py::arg("def")) |
| 182 | .def("get", (float (ParamDict::*)(int, float) const) & ParamDict::get, py::arg("id"), py::arg("def")) |
| 183 | .def("get", (Mat(ParamDict::*)(int, const Mat&) const) & ParamDict::get, py::arg("id"), py::arg("def")) |
| 184 | .def("set", (void (ParamDict::*)(int, int)) & ParamDict::set, py::arg("id"), py::arg("i")) |
| 185 | .def("set", (void (ParamDict::*)(int, float)) & ParamDict::set, py::arg("id"), py::arg("f")) |
| 186 | .def("set", (void (ParamDict::*)(int, const Mat&)) & ParamDict::set, py::arg("id"), py::arg("v")); |
| 187 | |
| 188 | py::class_<Option>(m, "Option") |
| 189 | .def(py::init<>()) |
| 190 | .def_readwrite("lightmode", &Option::lightmode) |
| 191 | .def_readwrite("num_threads", &Option::num_threads) |
| 192 | .def_readwrite("blob_allocator", &Option::blob_allocator) |
| 193 | .def_readwrite("workspace_allocator", &Option::workspace_allocator) |
| 194 | #if NCNN_VULKAN |
| 195 | .def_readwrite("blob_vkallocator", &Option::blob_vkallocator) |
| 196 | .def_readwrite("workspace_vkallocator", &Option::workspace_vkallocator) |
| 197 | .def_readwrite("staging_vkallocator", &Option::staging_vkallocator) |
| 198 | //.def_readwrite("pipeline_cache", &Option::pipeline_cache) |
| 199 | #endif // NCNN_VULKAN |
| 200 | .def_readwrite("openmp_blocktime", &Option::openmp_blocktime) |
| 201 | .def_readwrite("use_winograd_convolution", &Option::use_winograd_convolution) |
| 202 | .def_readwrite("use_winograd23_convolution", &Option::use_winograd23_convolution) |
| 203 | .def_readwrite("use_winograd43_convolution", &Option::use_winograd43_convolution) |
| 204 | .def_readwrite("use_winograd63_convolution", &Option::use_winograd63_convolution) |
| 205 | .def_readwrite("use_sgemm_convolution", &Option::use_sgemm_convolution) |
| 206 | .def_readwrite("use_int8_inference", &Option::use_int8_inference) |
| 207 | .def_readwrite("use_vulkan_compute", &Option::use_vulkan_compute) |
| 208 | .def_readwrite("use_bf16_storage", &Option::use_bf16_storage) |
| 209 | .def_readwrite("use_fp16_packed", &Option::use_fp16_packed) |
| 210 | .def_readwrite("use_fp16_storage", &Option::use_fp16_storage) |
| 211 | .def_readwrite("use_fp16_arithmetic", &Option::use_fp16_arithmetic) |
| 212 | .def_readwrite("use_int8_packed", &Option::use_int8_packed) |
| 213 | .def_readwrite("use_int8_storage", &Option::use_int8_storage) |
| 214 | .def_readwrite("use_int8_arithmetic", &Option::use_int8_arithmetic) |
| 215 | .def_readwrite("use_packing_layout", &Option::use_packing_layout) |
| 216 | .def_readwrite("use_shader_pack8", &Option::use_shader_pack8) |
| 217 | .def_readwrite("use_subgroup_basic", &Option::use_subgroup_basic) |
| 218 | .def_readwrite("use_subgroup_vote", &Option::use_subgroup_vote) |
| 219 | .def_readwrite("use_subgroup_ballot", &Option::use_subgroup_ballot) |
| 220 | .def_readwrite("use_subgroup_shuffle", &Option::use_subgroup_shuffle) |
| 221 | .def_readwrite("use_image_storage", &Option::use_image_storage) |
| 222 | .def_readwrite("use_tensor_storage", &Option::use_tensor_storage); |
| 223 | |
| 224 | py::class_<Mat> mat(m, "Mat", py::buffer_protocol()); |
| 225 | mat.def(py::init<>()) |
| 226 | .def(py::init( |
| 227 | [](py::tuple shape, size_t elemsize, int elempack, Allocator* allocator) { |
| 228 | Mat* mat = nullptr; |
| 229 | switch (shape.size()) |
| 230 | { |
| 231 | case 1: |
| 232 | mat = new Mat(shape[0].cast<int>(), elemsize, elempack, allocator); |
| 233 | break; |
| 234 | case 2: |
| 235 | mat = new Mat(shape[0].cast<int>(), shape[1].cast<int>(), elemsize, elempack, allocator); |
| 236 | break; |
| 237 | case 3: |
| 238 | mat = new Mat(shape[0].cast<int>(), shape[1].cast<int>(), shape[2].cast<int>(), elemsize, elempack, allocator); |
| 239 | break; |
| 240 | case 4: |
| 241 | mat = new Mat(shape[0].cast<int>(), shape[1].cast<int>(), shape[2].cast<int>(), shape[3].cast<int>(), elemsize, elempack, allocator); |
| 242 | break; |
| 243 | default: |
| 244 | std::stringstream ss; |
| 245 | ss << "shape must be 1, 2, 3 or 4 dims, not " << shape.size(); |
| 246 | pybind11::pybind11_fail(ss.str()); |
| 247 | } |
| 248 | return mat; |
| 249 | }), |
| 250 | py::arg("shape"), py::kw_only(), |
| 251 | py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 252 | .def(py::init<int, size_t, int, Allocator*>(), |
| 253 | py::arg("w"), py::kw_only(), |
| 254 | py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 255 | .def(py::init<int, int, size_t, int, Allocator*>(), |
| 256 | py::arg("w"), py::arg("h"), py::kw_only(), |
| 257 | py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 258 | .def(py::init<int, int, int, size_t, int, Allocator*>(), |
| 259 | py::arg("w"), py::arg("h"), py::arg("c"), py::kw_only(), |
| 260 | py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 261 | .def(py::init<int, int, int, int, size_t, int, Allocator*>(), |
| 262 | py::arg("w"), py::arg("h"), py::arg("d"), py::arg("c"), py::kw_only(), |
| 263 | py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 264 | |
| 265 | .def(py::init<const Mat&>(), py::arg("m")) |
| 266 | |
| 267 | .def(py::init([](py::buffer const b) { |
| 268 | py::buffer_info info = b.request(); |
| 269 | if (info.ndim > 4) |
| 270 | { |
| 271 | std::stringstream ss; |
| 272 | ss << "convert numpy.ndarray to ncnn.Mat only dims <=4 support now, but given " << info.ndim; |
| 273 | pybind11::pybind11_fail(ss.str()); |
| 274 | } |
| 275 | |
| 276 | size_t elemsize = info.itemsize; |
| 277 | |
| 278 | Mat* v = nullptr; |
| 279 | if (info.ndim == 1) |
| 280 | { |
| 281 | v = new Mat((int)info.shape[0], info.ptr, elemsize); |
| 282 | } |
| 283 | else if (info.ndim == 2) |
| 284 | { |
| 285 | v = new Mat((int)info.shape[1], (int)info.shape[0], info.ptr, elemsize); |
| 286 | } |
| 287 | else if (info.ndim == 3) |
| 288 | { |
| 289 | v = new Mat((int)info.shape[2], (int)info.shape[1], (int)info.shape[0], info.ptr, elemsize); |
| 290 | |
| 291 | // in ncnn, buffer to construct ncnn::Mat need align to ncnn::alignSize |
| 292 | // with (w * h * elemsize, 16) / elemsize, but the buffer from numpy not |
| 293 | // so we set the cstep as numpy's cstep |
| 294 | v->cstep = (int)info.shape[2] * (int)info.shape[1]; |
| 295 | } |
| 296 | else if (info.ndim == 4) |
| 297 | { |
| 298 | v = new Mat((int)info.shape[3], (int)info.shape[2], (int)info.shape[1], (int)info.shape[0], info.ptr, elemsize); |
| 299 | |
| 300 | // in ncnn, buffer to construct ncnn::Mat need align to ncnn::alignSize |
| 301 | // with (w * h * d elemsize, 16) / elemsize, but the buffer from numpy not |
| 302 | // so we set the cstep as numpy's cstep |
| 303 | v->cstep = (int)info.shape[3] * (int)info.shape[2] * (int)info.shape[1]; |
| 304 | } |
| 305 | return std::unique_ptr<Mat>(v); |
| 306 | }), |
| 307 | py::arg("array")) |
| 308 | .def_buffer([](Mat& m) -> py::buffer_info { |
| 309 | return to_buffer_info(m); |
| 310 | }) |
| 311 | .def( |
| 312 | "numpy", [](py::object obj, const std::string& format = "") -> py::array { |
| 313 | auto* m = obj.cast<Mat*>(); |
| 314 | return py::array(to_buffer_info(*m, format), obj); |
| 315 | }, |
| 316 | py::arg("format") = "", "i for int32, f for float32, d for double") |
| 317 | //.def("fill", (void (Mat::*)(int))(&Mat::fill), py::arg("v")) |
| 318 | .def("fill", (void (Mat::*)(float))(&Mat::fill), py::arg("v")) |
| 319 | .def("clone", &Mat::clone, py::arg("allocator") = nullptr) |
| 320 | .def("clone_from", &Mat::clone_from, py::arg("mat"), py::arg("allocator") = nullptr) |
| 321 | .def( |
| 322 | "reshape", [](Mat& mat, py::tuple shape, Allocator* allocator) { |
| 323 | switch (shape.size()) |
| 324 | { |
| 325 | case 1: |
| 326 | return mat.reshape(shape[0].cast<int>(), allocator); |
| 327 | case 2: |
| 328 | return mat.reshape(shape[0].cast<int>(), shape[1].cast<int>(), allocator); |
| 329 | case 3: |
| 330 | return mat.reshape(shape[0].cast<int>(), shape[1].cast<int>(), shape[2].cast<int>(), allocator); |
| 331 | case 4: |
| 332 | return mat.reshape(shape[0].cast<int>(), shape[1].cast<int>(), shape[2].cast<int>(), shape[3].cast<int>(), allocator); |
| 333 | default: |
| 334 | std::stringstream ss; |
| 335 | ss << "shape must be 1, 2, 3 or 4 dims, not " << shape.size(); |
| 336 | pybind11::pybind11_fail(ss.str()); |
| 337 | } |
| 338 | return Mat(); |
| 339 | }, |
| 340 | py::arg("shape") = py::tuple(1), py::arg("allocator") = nullptr) |
| 341 | .def("reshape", (Mat(Mat::*)(int, Allocator*) const) & Mat::reshape, py::arg("w"), py::kw_only(), py::arg("allocator") = nullptr) |
| 342 | .def("reshape", (Mat(Mat::*)(int, int, Allocator*) const) & Mat::reshape, py::arg("w"), py::arg("h"), py::kw_only(), py::arg("allocator") = nullptr) |
| 343 | .def("reshape", (Mat(Mat::*)(int, int, int, Allocator*) const) & Mat::reshape, py::arg("w"), py::arg("h"), py::arg("c"), py::kw_only(), py::arg("allocator") = nullptr) |
| 344 | .def("reshape", (Mat(Mat::*)(int, int, int, int, Allocator*) const) & Mat::reshape, py::arg("w"), py::arg("h"), py::arg("d"), py::arg("c"), py::kw_only(), py::arg("allocator") = nullptr) |
| 345 | |
| 346 | .def( |
| 347 | "create", [](Mat& mat, py::tuple shape, size_t elemsize, int elempack, Allocator* allocator) { |
| 348 | switch (shape.size()) |
| 349 | { |
| 350 | case 1: |
| 351 | return mat.create(shape[0].cast<int>(), elemsize, elempack, allocator); |
| 352 | case 2: |
| 353 | return mat.create(shape[0].cast<int>(), shape[1].cast<int>(), elemsize, elempack, allocator); |
| 354 | case 3: |
| 355 | return mat.create(shape[0].cast<int>(), shape[1].cast<int>(), shape[2].cast<int>(), elemsize, elempack, allocator); |
| 356 | case 4: |
| 357 | return mat.create(shape[0].cast<int>(), shape[1].cast<int>(), shape[2].cast<int>(), shape[3].cast<int>(), elemsize, elempack, allocator); |
| 358 | default: |
| 359 | std::stringstream ss; |
| 360 | ss << "shape must be 1, 2, 3 or 4 dims, not " << shape.size(); |
| 361 | pybind11::pybind11_fail(ss.str()); |
| 362 | } |
| 363 | return; |
| 364 | }, |
| 365 | py::arg("shape"), py::kw_only(), py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 366 | .def("create", (void (Mat::*)(int, size_t, int, Allocator*)) & Mat::create, py::arg("w"), py::kw_only(), py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 367 | .def("create", (void (Mat::*)(int, int, size_t, int, Allocator*)) & Mat::create, py::arg("w"), py::arg("h"), py::kw_only(), py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 368 | .def("create", (void (Mat::*)(int, int, int, size_t, int, Allocator*)) & Mat::create, py::arg("w"), py::arg("h"), py::arg("c"), py::kw_only(), py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 369 | .def("create", (void (Mat::*)(int, int, int, int, size_t, int, Allocator*)) & Mat::create, py::arg("w"), py::arg("h"), py::arg("d"), py::arg("c"), py::kw_only(), py::arg("elemsize") = 4, py::arg("elempack") = 1, py::arg("allocator") = nullptr) |
| 370 | .def("create_like", (void (Mat::*)(const Mat&, Allocator*)) & Mat::create_like, py::arg("m"), py::arg("allocator") = nullptr) |
| 371 | .def("addref", &Mat::addref) |
| 372 | .def("release", &Mat::release) |
| 373 | .def("empty", &Mat::empty) |
| 374 | .def("total", &Mat::total) |
| 375 | .def("elembits", &Mat::elembits) |
| 376 | .def("shape", &Mat::shape) |
| 377 | .def("channel", (Mat(Mat::*)(int)) & Mat::channel, py::arg("c")) |
| 378 | //.def("channel", (const Mat (Mat::*)(int) const) & Mat::channel, py::arg("c")) |
| 379 | .def("depth", (Mat(Mat::*)(int)) & Mat::depth, py::arg("z")) |
| 380 | //.def("depth", (const Mat (Mat::*)(int) const) & Mat::depth, py::arg("z")) |
| 381 | .def( |
| 382 | "row", [](Mat& m, int y) { |
| 383 | if (m.elempack != 1) |
| 384 | { |
| 385 | std::stringstream ss; |
| 386 | ss << "get ncnn.Mat row only elempack 1 support now, but given " << m.elempack; |
| 387 | pybind11::pybind11_fail(ss.str()); |
| 388 | } |
| 389 | |
| 390 | switch (m.elemsize) |
| 391 | { |
| 392 | case 1: |
| 393 | return py::memoryview::from_buffer(m.row<int8_t>(y), {m.w}, {sizeof(int8_t)}); |
| 394 | //case 2: |
| 395 | // return py::memoryview::from_buffer(m.row<short>(y), {m.w}, {sizeof(short)}); |
| 396 | case 4: |
| 397 | return py::memoryview::from_buffer(m.row<float>(y), {m.w}, {sizeof(float)}); |
| 398 | default: |
| 399 | std::stringstream ss; |
| 400 | ss << "ncnn.Mat row elemsize " << m.elemsize << "not support now"; |
| 401 | pybind11::pybind11_fail(ss.str()); |
| 402 | } |
| 403 | return py::memoryview::from_buffer(m.row<float>(y), {m.w}, {sizeof(float)}); |
| 404 | }, |
| 405 | py::arg("y")) |
| 406 | .def("channel_range", (Mat(Mat::*)(int, int)) & Mat::channel_range, py::arg("c"), py::arg("channels")) |
| 407 | //.def("channel_range", (const Mat (Mat::*)(int, int) const) & Mat::channel_range, py::arg("c"), py::arg("channels")) |
| 408 | .def("depth_range", (Mat(Mat::*)(int, int)) & Mat::depth_range, py::arg("z"), py::arg("depths")) |
| 409 | //.def("depth_range", (const Mat (Mat::*)(int, int) const) & Mat::depth_range, py::arg("z"), py::arg("depths")) |
| 410 | .def("row_range", (Mat(Mat::*)(int, int)) & Mat::row_range, py::arg("y"), py::arg("rows")) |
| 411 | //.def("row_range", (const Mat (Mat::*)(int, int) const) & Mat::row_range, py::arg("y"), py::arg("rows")) |
| 412 | .def("range", (Mat(Mat::*)(int, int)) & Mat::range, py::arg("x"), py::arg("n")) |
| 413 | //.def("range", (const Mat (Mat::*)(int, int) const) & Mat::range, py::arg("x"), py::arg("n")) |
| 414 | .def( |
| 415 | "__getitem__", [](const Mat& m, size_t i) { |
| 416 | return m[i]; |
| 417 | }, |
| 418 | py::arg("i")) |
| 419 | .def( |
| 420 | "__setitem__", [](Mat& m, size_t i, float v) { |
| 421 | m[i] = v; |
| 422 | }, |
| 423 | py::arg("i"), py::arg("v")) |
| 424 | .def("__len__", [](Mat& m) { |
| 425 | return m.w; |
| 426 | }) |
| 427 | |
| 428 | //convenient construct from pixel data |
| 429 | .def_static( |
| 430 | "from_pixels", [](py::buffer const b, int type, int w, int h, Allocator* allocator) { |
| 431 | return Mat::from_pixels((const unsigned char*)b.request().ptr, type, w, h, allocator); |
| 432 | }, |
| 433 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("allocator") = nullptr) |
| 434 | .def_static( |
| 435 | "from_pixels", [](py::buffer const b, int type, int w, int h, int stride, Allocator* allocator) { |
| 436 | return Mat::from_pixels((const unsigned char*)b.request().ptr, type, w, h, stride, allocator); |
| 437 | }, |
| 438 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("stride"), py::arg("allocator") = nullptr) |
| 439 | .def_static( |
| 440 | "from_pixels_resize", [](py::buffer const b, int type, int w, int h, int target_width, int target_height, Allocator* allocator) { |
| 441 | return Mat::from_pixels_resize((const unsigned char*)b.request().ptr, |
| 442 | type, w, h, target_width, target_height, allocator); |
| 443 | }, |
| 444 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("target_width"), py::arg("target_height"), py::arg("allocator") = nullptr) |
| 445 | .def_static( |
| 446 | "from_pixels_resize", [](py::buffer const b, int type, int w, int h, int stride, int target_width, int target_height, Allocator* allocator) { |
| 447 | return Mat::from_pixels_resize((const unsigned char*)b.request().ptr, |
| 448 | type, w, h, stride, target_width, target_height, allocator); |
| 449 | }, |
| 450 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("stride"), py::arg("target_width"), py::arg("target_height"), py::arg("allocator") = nullptr) |
| 451 | .def_static( |
| 452 | "from_pixels_roi", [](py::buffer const b, int type, int w, int h, int roix, int roiy, int roiw, int roih, Allocator* allocator) { |
| 453 | return Mat::from_pixels_roi((const unsigned char*)b.request().ptr, |
| 454 | type, w, h, roix, roiy, roiw, roih, allocator); |
| 455 | }, |
| 456 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("roix"), py::arg("roiy"), py::arg("roiw"), py::arg("roih"), py::arg("allocator") = nullptr) |
| 457 | .def_static( |
| 458 | "from_pixels_roi", [](py::buffer const b, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, Allocator* allocator) { |
| 459 | return Mat::from_pixels_roi((const unsigned char*)b.request().ptr, |
| 460 | type, w, h, stride, roix, roiy, roiw, roih, allocator); |
| 461 | }, |
| 462 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("stride"), py::arg("roix"), py::arg("roiy"), py::arg("roiw"), py::arg("roih"), py::arg("allocator") = nullptr) |
| 463 | .def_static( |
| 464 | "from_pixels_roi_resize", [](py::buffer const b, int type, int w, int h, int roix, int roiy, int roiw, int roih, int target_width, int target_height, Allocator* allocator) { |
| 465 | return Mat::from_pixels_roi_resize((const unsigned char*)b.request().ptr, |
| 466 | type, w, h, roix, roiy, roiw, roih, target_width, target_height, allocator); |
| 467 | }, |
| 468 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("roix"), py::arg("roiy"), py::arg("roiw"), py::arg("roih"), py::arg("target_width"), py::arg("target_height"), py::arg("allocator") = nullptr) |
| 469 | .def_static( |
| 470 | "from_pixels_roi_resize", [](py::buffer const b, int type, int w, int h, int stride, int roix, int roiy, int roiw, int roih, int target_width, int target_height, Allocator* allocator) { |
| 471 | return Mat::from_pixels_roi_resize((const unsigned char*)b.request().ptr, |
| 472 | type, w, h, stride, roix, roiy, roiw, roih, target_width, target_height, allocator); |
| 473 | }, |
| 474 | py::arg("array"), py::arg("type"), py::arg("w"), py::arg("h"), py::arg("stride"), py::arg("roix"), py::arg("roiy"), py::arg("roiw"), py::arg("roih"), py::arg("target_width"), py::arg("target_height"), py::arg("allocator") = nullptr) |
| 475 | .def( |
| 476 | "substract_mean_normalize", [](Mat& mat, std::vector<float>& mean, std::vector<float>& norm) { |
| 477 | return mat.substract_mean_normalize(mean.size() > 0 ? &mean[0] : 0, norm.size() > 0 ? &norm[0] : 0); |
| 478 | }, |
| 479 | py::arg("mean"), py::arg("norm")) |
| 480 | .def_readwrite("refcount", &Mat::refcount) |
| 481 | .def_readwrite("elemsize", &Mat::elemsize) |
| 482 | .def_readwrite("elempack", &Mat::elempack) |
| 483 | .def_readwrite("allocator", &Mat::allocator) |
| 484 | .def_readwrite("dims", &Mat::dims) |
| 485 | .def_readwrite("w", &Mat::w) |
| 486 | .def_readwrite("h", &Mat::h) |
| 487 | .def_readwrite("d", &Mat::d) |
| 488 | .def_readwrite("c", &Mat::c) |
| 489 | .def_readwrite("cstep", &Mat::cstep) |
| 490 | .def("__repr__", [](const Mat& m) { |
| 491 | std::stringstream ss; |
| 492 | ss << "<ncnn.Mat w=" << m.w << " h=" << m.h << " d=" << m.d << " c=" << m.c << " dims=" << m.dims |
| 493 | << " cstep=" << m.cstep << " elemsize=" << m.elemsize << " elempack=" << m.elempack << "\n\t" |
| 494 | << "refcount=" << (m.refcount ? *m.refcount : 0) << " data=0x" << static_cast<const void*>(m.data) |
| 495 | << " allocator=0x" << static_cast<const void*>(m.allocator) << ">\n"; |
| 496 | |
| 497 | const int max_count = m.dims == 1 ? 10 : 6; |
| 498 | if (m.dims == 1) |
| 499 | { |
| 500 | ss << "["; |
| 501 | bool dot_printed_w = false; |
| 502 | |
| 503 | if (m.elemsize == 1) |
| 504 | { |
| 505 | const int8_t* row = m.row<int8_t>(0); |
| 506 | for (int i = 0; i < m.w; i++) |
| 507 | { |
| 508 | if (i < max_count / 2 || i >= m.w - max_count / 2) |
| 509 | { |
| 510 | if (i > 0) |
| 511 | { |
| 512 | ss << ", "; |
| 513 | } |
| 514 | ss << static_cast<int>(row[i]); |
| 515 | } |
| 516 | else if (!dot_printed_w) |
| 517 | { |
| 518 | dot_printed_w = true; |
| 519 | ss << ", ..."; |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | if (m.elemsize == 4) |
| 524 | { |
| 525 | const float* row = m.row<float>(0); |
| 526 | for (int i = 0; i < m.w; i++) |
| 527 | { |
| 528 | if (i < max_count / 2 || i >= m.w - max_count / 2) |
| 529 | { |
| 530 | if (i > 0) |
| 531 | { |
| 532 | ss << ", "; |
| 533 | } |
| 534 | ss << row[i]; |
| 535 | } |
| 536 | else if (!dot_printed_w) |
| 537 | { |
| 538 | dot_printed_w = true; |
| 539 | ss << ", ..."; |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | ss << "]"; |
| 544 | } |
| 545 | else if (m.dims == 2) |
| 546 | { |
| 547 | bool dot_printed_h = false; |
| 548 | ss << "["; |
| 549 | for (int j = 0; j < m.h; j++) |
| 550 | { |
| 551 | bool dot_printed_w = false; |
| 552 | if (j < max_count / 2 || j >= m.h - max_count / 2) |
| 553 | { |
| 554 | ss << "["; |
| 555 | if (m.elemsize == 1) |
| 556 | { |
| 557 | const int8_t* row = m.row<int8_t>(j); |
| 558 | for (int i = 0; i < m.w; i++) |
| 559 | { |
| 560 | if (i < max_count / 2 || i >= m.w - max_count / 2) |
| 561 | { |
| 562 | if (i > 0) |
| 563 | { |
| 564 | ss << ", "; |
| 565 | } |
| 566 | ss << static_cast<int>(row[i]); |
| 567 | } |
| 568 | else if (!dot_printed_w) |
| 569 | { |
| 570 | dot_printed_w = true; |
| 571 | ss << ", ..."; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | if (m.elemsize == 4) |
| 576 | { |
| 577 | const float* row = m.row<float>(j); |
| 578 | for (int i = 0; i < m.w; i++) |
| 579 | { |
| 580 | if (i < max_count / 2 || i >= m.w - max_count / 2) |
| 581 | { |
| 582 | if (i > 0) |
| 583 | { |
| 584 | ss << ", "; |
| 585 | } |
| 586 | ss << row[i]; |
| 587 | } |
| 588 | else if (!dot_printed_w) |
| 589 | { |
| 590 | dot_printed_w = true; |
| 591 | ss << ", ..."; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | ss << "]"; |
| 596 | if (j < m.h - 1) |
| 597 | { |
| 598 | ss << "\n"; |
| 599 | } |
| 600 | } |
| 601 | else if (!dot_printed_h) |
| 602 | { |
| 603 | dot_printed_h = true; |
| 604 | ss << "...\n"; |
| 605 | } |
| 606 | } |
| 607 | ss << "]\n"; |
| 608 | } |
| 609 | else if (m.dims == 3) |
| 610 | { |
| 611 | bool dot_printed_c = false; |
| 612 | ss << "["; |
| 613 | for (int k = 0; k < m.c; k++) |
| 614 | { |
| 615 | bool dot_printed_h = false; |
| 616 | if (k < max_count / 2 || k >= m.c - max_count / 2) |
| 617 | { |
| 618 | Mat channel = m.channel(k); |
| 619 | if (k > 0) |
| 620 | { |
| 621 | ss << " "; |
| 622 | } |
| 623 | ss << "["; |
| 624 | for (int j = 0; j < channel.h; j++) |
| 625 | { |
| 626 | bool dot_printed_w = false; |
| 627 | if (j < max_count / 2 || j >= channel.h - max_count / 2) |
| 628 | { |
| 629 | if (j > 0) |
| 630 | { |
| 631 | ss << " "; |
| 632 | } |
| 633 | ss << "["; |
| 634 | if (m.elemsize == 1) |
| 635 | { |
| 636 | const int8_t* row = channel.row<int8_t>(j); |
| 637 | for (int i = 0; i < channel.w; i++) |
| 638 | { |
| 639 | if (i < max_count / 2 || i >= channel.w - max_count / 2) |
| 640 | { |
| 641 | if (i > 0) |
| 642 | { |
| 643 | ss << ", "; |
| 644 | } |
| 645 | ss << static_cast<int>(row[i]); |
| 646 | } |
| 647 | else if (!dot_printed_w) |
| 648 | { |
| 649 | dot_printed_w = true; |
| 650 | ss << ", ..."; |
| 651 | } |
| 652 | } |
| 653 | } |
| 654 | if (m.elemsize == 4) |
| 655 | { |
| 656 | const float* row = channel.row<float>(j); |
| 657 | for (int i = 0; i < m.w; i++) |
| 658 | { |
| 659 | if (i < max_count / 2 || i >= m.w - max_count / 2) |
| 660 | { |
| 661 | if (i > 0) |
| 662 | { |
| 663 | ss << ", "; |
| 664 | } |
| 665 | ss << row[i]; |
| 666 | } |
| 667 | else if (!dot_printed_w) |
| 668 | { |
| 669 | dot_printed_w = true; |
| 670 | ss << ", ..."; |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | ss << "]"; |
| 675 | if (j < channel.h - 1) |
| 676 | { |
| 677 | ss << "\n"; |
| 678 | } |
| 679 | } |
| 680 | else if (!dot_printed_h) |
| 681 | { |
| 682 | dot_printed_h = true; |
| 683 | ss << " ...\n"; |
| 684 | } |
| 685 | } // for j |
| 686 | ss << "]"; |
| 687 | if (k < m.c - 1) |
| 688 | { |
| 689 | ss << "\n\n"; |
| 690 | } |
| 691 | } |
| 692 | else if (!dot_printed_c) |
| 693 | { |
| 694 | dot_printed_c = true; |
| 695 | ss << " ...\n"; |
| 696 | } |
| 697 | } // for k |
| 698 | ss << "]\n"; |
| 699 | } |
| 700 | else if (m.dims == 4) |
| 701 | { |
| 702 | bool dot_printed_c = false; |
| 703 | ss << "["; |
| 704 | for (int k = 0; k < m.c; k++) |
| 705 | { |
| 706 | bool dot_printed_d = false; |
| 707 | if (k < max_count / 2 || k >= m.c - max_count / 2) |
| 708 | { |
| 709 | Mat channel = m.channel(k); |
| 710 | if (k > 0) |
| 711 | { |
| 712 | ss << " "; |
| 713 | } |
| 714 | ss << "["; |
| 715 | for (int z = 0; z < channel.d; z++) |
| 716 | { |
| 717 | bool dot_printed_h = false; |
| 718 | if (z < max_count / 2 || z >= channel.d - max_count / 2) |
| 719 | { |
| 720 | if (z > 0) |
| 721 | { |
| 722 | ss << " "; |
| 723 | } |
| 724 | ss << "["; |
| 725 | for (int j = 0; j < channel.h; j++) |
| 726 | { |
| 727 | bool dot_printed_w = false; |
| 728 | if (j < max_count / 2 || j >= channel.h - max_count / 2) |
| 729 | { |
| 730 | if (j > 0) |
| 731 | { |
| 732 | ss << " "; |
| 733 | } |
| 734 | ss << "["; |
| 735 | if (m.elemsize == 1) |
| 736 | { |
| 737 | const int8_t* row = channel.depth(z).row<int8_t>(j); |
| 738 | for (int i = 0; i < channel.w; i++) |
| 739 | { |
| 740 | if (i < max_count / 2 || i >= channel.w - max_count / 2) |
| 741 | { |
| 742 | if (i > 0) |
| 743 | { |
| 744 | ss << ", "; |
| 745 | } |
| 746 | ss << static_cast<int>(row[i]); |
| 747 | } |
| 748 | else if (!dot_printed_w) |
| 749 | { |
| 750 | dot_printed_w = true; |
| 751 | ss << ", ..."; |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | if (m.elemsize == 4) |
| 756 | { |
| 757 | const float* row = channel.depth(z).row<float>(j); |
| 758 | for (int i = 0; i < m.w; i++) |
| 759 | { |
| 760 | if (i < max_count / 2 || i >= m.w - max_count / 2) |
| 761 | { |
| 762 | if (i > 0) |
| 763 | { |
| 764 | ss << ", "; |
| 765 | } |
| 766 | ss << row[i]; |
| 767 | } |
| 768 | else if (!dot_printed_w) |
| 769 | { |
| 770 | dot_printed_w = true; |
| 771 | ss << ", ..."; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | ss << "]"; |
| 776 | if (j < channel.h - 1) |
| 777 | { |
| 778 | ss << "\n"; |
| 779 | } |
| 780 | } |
| 781 | else if (!dot_printed_h) |
| 782 | { |
| 783 | dot_printed_h = true; |
| 784 | ss << " ...\n"; |
| 785 | } |
| 786 | } // for j |
| 787 | ss << "]"; |
| 788 | if (z < channel.d - 1) |
| 789 | { |
| 790 | ss << "\n"; |
| 791 | } |
| 792 | } |
| 793 | else if (!dot_printed_d) |
| 794 | { |
| 795 | dot_printed_d = true; |
| 796 | ss << " ...\n"; |
| 797 | } |
| 798 | } // for z |
| 799 | ss << "]"; |
| 800 | if (k < m.c - 1) |
| 801 | { |
| 802 | ss << "\n\n"; |
| 803 | } |
| 804 | } |
| 805 | else if (!dot_printed_c) |
| 806 | { |
| 807 | dot_printed_c = true; |
| 808 | ss << " ...\n"; |
| 809 | } |
| 810 | } // for k |
| 811 | ss << "]\n"; |
| 812 | } |
| 813 | return ss.str(); |
| 814 | }); |
| 815 | |
| 816 | py::enum_<ncnn::Mat::PixelType>(mat, "PixelType") |
| 817 | .value("PIXEL_CONVERT_SHIFT", ncnn::Mat::PixelType::PIXEL_CONVERT_SHIFT) |
| 818 | .value("PIXEL_FORMAT_MASK", ncnn::Mat::PixelType::PIXEL_FORMAT_MASK) |
| 819 | .value("PIXEL_CONVERT_MASK", ncnn::Mat::PixelType::PIXEL_CONVERT_MASK) |
| 820 | |
| 821 | .value("PIXEL_RGB", ncnn::Mat::PixelType::PIXEL_RGB) |
| 822 | .value("PIXEL_BGR", ncnn::Mat::PixelType::PIXEL_BGR) |
| 823 | .value("PIXEL_GRAY", ncnn::Mat::PixelType::PIXEL_GRAY) |
| 824 | .value("PIXEL_RGBA", ncnn::Mat::PixelType::PIXEL_RGBA) |
| 825 | .value("PIXEL_BGRA", ncnn::Mat::PixelType::PIXEL_BGRA) |
| 826 | |
| 827 | .value("PIXEL_RGB2BGR", ncnn::Mat::PixelType::PIXEL_RGB2BGR) |
| 828 | .value("PIXEL_RGB2GRAY", ncnn::Mat::PixelType::PIXEL_RGB2GRAY) |
| 829 | .value("PIXEL_RGB2RGBA", ncnn::Mat::PixelType::PIXEL_RGB2RGBA) |
| 830 | .value("PIXEL_RGB2BGRA", ncnn::Mat::PixelType::PIXEL_RGB2BGRA) |
| 831 | |
| 832 | .value("PIXEL_BGR2RGB", ncnn::Mat::PixelType::PIXEL_BGR2RGB) |
| 833 | .value("PIXEL_BGR2GRAY", ncnn::Mat::PixelType::PIXEL_BGR2GRAY) |
| 834 | .value("PIXEL_BGR2RGBA", ncnn::Mat::PixelType::PIXEL_BGR2RGBA) |
| 835 | .value("PIXEL_BGR2BGRA", ncnn::Mat::PixelType::PIXEL_BGR2BGRA) |
| 836 | |
| 837 | .value("PIXEL_GRAY2RGB", ncnn::Mat::PixelType::PIXEL_GRAY2RGB) |
| 838 | .value("PIXEL_GRAY2BGR", ncnn::Mat::PixelType::PIXEL_GRAY2BGR) |
| 839 | .value("PIXEL_GRAY2RGBA", ncnn::Mat::PixelType::PIXEL_GRAY2RGBA) |
| 840 | .value("PIXEL_GRAY2BGRA", ncnn::Mat::PixelType::PIXEL_GRAY2BGRA) |
| 841 | |
| 842 | .value("PIXEL_RGBA2RGB", ncnn::Mat::PixelType::PIXEL_RGBA2RGB) |
| 843 | .value("PIXEL_RGBA2BGR", ncnn::Mat::PixelType::PIXEL_RGBA2BGR) |
| 844 | .value("PIXEL_RGBA2GRAY", ncnn::Mat::PixelType::PIXEL_RGBA2GRAY) |
| 845 | .value("PIXEL_RGBA2BGRA", ncnn::Mat::PixelType::PIXEL_RGBA2BGRA) |
| 846 | |
| 847 | .value("PIXEL_BGRA2RGB", ncnn::Mat::PixelType::PIXEL_BGRA2RGB) |
| 848 | .value("PIXEL_BGRA2BGR", ncnn::Mat::PixelType::PIXEL_BGRA2BGR) |
| 849 | .value("PIXEL_BGRA2GRAY", ncnn::Mat::PixelType::PIXEL_BGRA2GRAY) |
| 850 | .value("PIXEL_BGRA2RGBA", ncnn::Mat::PixelType::PIXEL_BGRA2RGBA); |
| 851 | |
| 852 | py::class_<Extractor>(m, "Extractor") |
| 853 | .def("__enter__", [](Extractor& ex) -> Extractor& { return ex; }) |
| 854 | .def("__exit__", [](Extractor& ex, pybind11::args) { |
| 855 | ex.clear(); |
| 856 | }) |
| 857 | .def("clear", &Extractor::clear) |
| 858 | .def("set_light_mode", &Extractor::set_light_mode, py::arg("enable")) |
| 859 | .def("set_num_threads", &Extractor::set_num_threads, py::arg("num_threads")) |
| 860 | .def("set_blob_allocator", &Extractor::set_blob_allocator, py::arg("allocator")) |
| 861 | .def("set_workspace_allocator", &Extractor::set_workspace_allocator, py::arg("allocator")) |
| 862 | #if NCNN_STRING |
| 863 | .def("input", (int (Extractor::*)(const char*, const Mat&)) & Extractor::input, py::arg("blob_name"), py::arg("in")) |
| 864 | .def("extract", (int (Extractor::*)(const char*, Mat&, int)) & Extractor::extract, py::arg("blob_name"), py::arg("feat"), py::arg("type") = 0) |
| 865 | .def( |
| 866 | "extract", [](Extractor& ex, const char* blob_name, int type) { |
| 867 | ncnn::Mat feat; |
| 868 | int ret = ex.extract(blob_name, feat, type); |
| 869 | return py::make_tuple(ret, feat.clone()); |
| 870 | }, |
| 871 | py::arg("blob_name"), py::arg("type") = 0) |
| 872 | #endif |
| 873 | .def("input", (int (Extractor::*)(int, const Mat&)) & Extractor::input) |
| 874 | .def("extract", (int (Extractor::*)(int, Mat&, int)) & Extractor::extract, py::arg("blob_index"), py::arg("feat"), py::arg("type") = 0) |
| 875 | .def( |
| 876 | "extract", [](Extractor& ex, int blob_index, int type) { |
| 877 | ncnn::Mat feat; |
| 878 | int ret = ex.extract(blob_index, feat, type); |
| 879 | return py::make_tuple(ret, feat.clone()); |
| 880 | }, |
| 881 | py::arg("blob_index"), py::arg("type") = 0); |
| 882 | |
| 883 | py::class_<Layer, PyLayer>(m, "Layer") |
| 884 | .def(py::init<>()) |
| 885 | .def("load_param", &Layer::load_param, py::arg("pd")) |
| 886 | .def("load_model", &Layer::load_model, py::arg("mb")) |
| 887 | .def("create_pipeline", &Layer::create_pipeline, py::arg("opt")) |
| 888 | .def("destroy_pipeline", &Layer::destroy_pipeline, py::arg("opt")) |
| 889 | .def_readwrite("one_blob_only", &Layer::one_blob_only) |
| 890 | .def_readwrite("support_inplace", &Layer::support_inplace) |
| 891 | .def_readwrite("support_vulkan", &Layer::support_vulkan) |
| 892 | .def_readwrite("support_packing", &Layer::support_packing) |
| 893 | .def_readwrite("support_bf16_storage", &Layer::support_bf16_storage) |
| 894 | .def_readwrite("support_fp16_storage", &Layer::support_fp16_storage) |
| 895 | .def_readwrite("support_image_storage", &Layer::support_image_storage) |
| 896 | .def("forward", (int (Layer::*)(const std::vector<Mat>&, std::vector<Mat>&, const Option&) const) & Layer::forward, |
| 897 | py::arg("bottom_blobs"), py::arg("top_blobs"), py::arg("opt")) |
| 898 | .def("forward", (int (Layer::*)(const Mat&, Mat&, const Option&) const) & Layer::forward, |
| 899 | py::arg("bottom_blob"), py::arg("top_blob"), py::arg("opt")) |
| 900 | .def("forward_inplace", (int (Layer::*)(std::vector<Mat>&, const Option&) const) & Layer::forward_inplace, |
| 901 | py::arg("bottom_top_blobs"), py::arg("opt")) |
| 902 | .def("forward_inplace", (int (Layer::*)(Mat&, const Option&) const) & Layer::forward_inplace, |
| 903 | py::arg("bottom_top_blob"), py::arg("opt")) |
| 904 | .def_readwrite("typeindex", &Layer::typeindex) |
| 905 | #if NCNN_STRING |
| 906 | .def_readwrite("type", &Layer::type) |
| 907 | .def_readwrite("name", &Layer::name) |
| 908 | #endif // NCNN_STRING |
| 909 | .def_readwrite("bottoms", &Layer::bottoms) |
| 910 | .def_readwrite("tops", &Layer::tops) |
| 911 | .def_readwrite("bottom_shapes", &Layer::bottom_shapes) |
| 912 | .def_readwrite("top_shapes", &Layer::top_shapes); |
| 913 | |
| 914 | py::class_<Net>(m, "Net") |
| 915 | .def(py::init<>()) |
| 916 | .def_readwrite("opt", &Net::opt) |
| 917 | .def("__enter__", [](Net& net) -> Net& { return net; }) |
| 918 | .def("__exit__", [](Net& net, pybind11::args) { |
| 919 | net.clear(); |
| 920 | }) |
| 921 | |
| 922 | #if NCNN_VULKAN |
| 923 | .def("set_vulkan_device", (void (Net::*)(int)) & Net::set_vulkan_device, py::arg("device_index")) |
| 924 | .def("set_vulkan_device", (void (Net::*)(const VulkanDevice*)) & Net::set_vulkan_device, py::arg("vkdev")) |
| 925 | .def("vulkan_device", &Net::vulkan_device, py::return_value_policy::reference_internal) |
| 926 | #endif // NCNN_VULKAN |
| 927 | |
| 928 | #if NCNN_STRING |
| 929 | .def( |
| 930 | "register_custom_layer", [](Net& net, const char* type, const std::function<ncnn::Layer*()>& creator, const std::function<void(ncnn::Layer*)>& destroyer) { |
| 931 | if (g_layer_factroy_index == g_layer_factroys.size()) |
| 932 | { |
| 933 | std::stringstream ss; |
| 934 | ss << "python version only support " << g_layer_factroys.size() << " custom layers now"; |
| 935 | pybind11::pybind11_fail(ss.str()); |
| 936 | } |
| 937 | LayerFactory& lf = g_layer_factroys[g_layer_factroy_index++]; |
| 938 | lf.name = type; |
| 939 | lf.creator = creator; |
| 940 | lf.destroyer = destroyer; |
| 941 | return net.register_custom_layer(lf.name.c_str(), lf.creator_func, lf.destroyer_func); |
| 942 | }, |
| 943 | py::arg("type"), py::arg("creator"), py::arg("destroyer")) |
| 944 | #endif //NCNN_STRING |
| 945 | .def( |
| 946 | "register_custom_layer", [](Net& net, int index, const std::function<ncnn::Layer*()>& creator, const std::function<void(ncnn::Layer*)>& destroyer) { |
| 947 | if (g_layer_factroy_index == g_layer_factroys.size()) |
| 948 | { |
| 949 | std::stringstream ss; |
| 950 | ss << "python version only support " << g_layer_factroys.size() << " custom layers now"; |
| 951 | pybind11::pybind11_fail(ss.str()); |
| 952 | } |
| 953 | LayerFactory& lf = g_layer_factroys[g_layer_factroy_index++]; |
| 954 | lf.index = index; |
| 955 | lf.creator = creator; |
| 956 | lf.destroyer = destroyer; |
| 957 | return net.register_custom_layer(index, lf.creator_func, lf.destroyer_func); |
| 958 | }, |
| 959 | py::arg("index"), py::arg("creator"), py::arg("destroyer")) |
| 960 | #if NCNN_STRING |
| 961 | .def("load_param", (int (Net::*)(const DataReader&)) & Net::load_param, py::arg("dr")) |
| 962 | #endif // NCNN_STRING |
| 963 | .def("load_param_bin", (int (Net::*)(const DataReader&)) & Net::load_param_bin, py::arg("dr")) |
| 964 | .def("load_model", (int (Net::*)(const DataReader&)) & Net::load_model, py::arg("dr")) |
| 965 | |
| 966 | #if NCNN_STDIO |
| 967 | #if NCNN_STRING |
| 968 | .def("load_param", (int (Net::*)(const char*)) & Net::load_param, py::arg("protopath")) |
| 969 | .def("load_param_mem", (int (Net::*)(const char*)) & Net::load_param_mem, py::arg("mem")) |
| 970 | #endif // NCNN_STRING |
| 971 | .def("load_param_bin", (int (Net::*)(const char*)) & Net::load_param_bin, py::arg("protopath")) |
| 972 | .def("load_model", (int (Net::*)(const char*)) & Net::load_model, py::arg("modelpath")) |
| 973 | .def( |
| 974 | "load_model_mem", [](Net& net, const char* mem) { |
| 975 | const unsigned char* _mem = (const unsigned char*)mem; |
| 976 | DataReaderFromMemoryCopy dr(_mem); |
| 977 | net.load_model(dr); |
| 978 | }, |
| 979 | py::arg("mem")) |
| 980 | #endif // NCNN_STDIO |
| 981 | |
| 982 | .def("clear", &Net::clear) |
| 983 | .def("create_extractor", &Net::create_extractor, py::keep_alive<0, 1>()) //net should be kept alive until retuned ex is freed by gc |
| 984 | |
| 985 | .def("input_indexes", &Net::input_indexes, py::return_value_policy::reference) |
| 986 | .def("output_indexes", &Net::output_indexes, py::return_value_policy::reference) |
| 987 | #if NCNN_STRING |
| 988 | .def("input_names", &Net::input_names, py::return_value_policy::reference) |
| 989 | .def("output_names", &Net::output_names, py::return_value_policy::reference) |
| 990 | #endif // NCNN_STRING |
| 991 | |
| 992 | .def("blobs", &Net::blobs, py::return_value_policy::reference_internal) |
| 993 | .def("layers", &Net::layers, py::return_value_policy::reference_internal); |
| 994 | |
| 995 | py::enum_<ncnn::BorderType>(m, "BorderType") |
| 996 | .value("BORDER_CONSTANT", ncnn::BorderType::BORDER_CONSTANT) |
| 997 | .value("BORDER_REPLICATE", ncnn::BorderType::BORDER_REPLICATE); |
| 998 | |
| 999 | m.def("cpu_support_arm_neon", &cpu_support_arm_neon); |
| 1000 | m.def("cpu_support_arm_vfpv4", &cpu_support_arm_vfpv4); |
| 1001 | m.def("cpu_support_arm_asimdhp", &cpu_support_arm_asimdhp); |
| 1002 | m.def("cpu_support_x86_avx2", &cpu_support_x86_avx2); |
| 1003 | m.def("cpu_support_x86_avx", &cpu_support_x86_avx); |
| 1004 | m.def("get_cpu_count", &get_cpu_count); |
| 1005 | m.def("get_little_cpu_count", &get_little_cpu_count); |
| 1006 | m.def("get_big_cpu_count", &get_big_cpu_count); |
| 1007 | m.def("get_physical_cpu_count", &get_physical_cpu_count); |
| 1008 | m.def("get_physical_little_cpu_count", &get_physical_little_cpu_count); |
| 1009 | m.def("get_physical_big_cpu_count", &get_physical_big_cpu_count); |
| 1010 | m.def("get_cpu_powersave", &get_cpu_powersave); |
| 1011 | m.def("set_cpu_powersave", &set_cpu_powersave, py::arg("powersave")); |
| 1012 | m.def("get_omp_num_threads", &get_omp_num_threads); |
| 1013 | m.def("set_omp_num_threads", &set_omp_num_threads, py::arg("num_threads")); |
| 1014 | m.def("get_omp_dynamic", &get_omp_dynamic); |
| 1015 | m.def("set_omp_dynamic", &set_omp_dynamic, py::arg("dynamic")); |
| 1016 | m.def("get_omp_thread_num", &get_omp_thread_num); |
| 1017 | m.def("get_kmp_blocktime", &get_kmp_blocktime); |
| 1018 | m.def("set_kmp_blocktime", &set_kmp_blocktime, py::arg("time_ms")); |
| 1019 | |
| 1020 | m.def("copy_make_border", ©_make_border, |
| 1021 | py::arg("src"), py::arg("dst"), |
| 1022 | py::arg("top"), py::arg("bottom"), py::arg("left"), py::arg("right"), |
| 1023 | py::arg("type"), py::arg("v"), py::arg("opt") = Option()); |
| 1024 | m.def( |
| 1025 | "copy_make_border", |
| 1026 | [](const Mat& src, int top, int bottom, int left, int right, int type, float v, const Option& opt) { |
| 1027 | Mat dst; |
| 1028 | copy_make_border(src, dst, top, bottom, left, right, type, v, opt); |
| 1029 | return dst; |
| 1030 | }, |
| 1031 | py::arg("src"), |
| 1032 | py::arg("top"), py::arg("bottom"), py::arg("left"), py::arg("right"), |
| 1033 | py::arg("type"), py::arg("v"), py::arg("opt") = Option()); |
| 1034 | |
| 1035 | m.def("copy_make_border_3d", ©_make_border_3d, |
| 1036 | py::arg("src"), py::arg("dst"), |
| 1037 | py::arg("top"), py::arg("bottom"), py::arg("left"), py::arg("right"), py::arg("front"), py::arg("behind"), |
| 1038 | py::arg("type"), py::arg("v"), py::arg("opt") = Option()); |
| 1039 | m.def( |
| 1040 | "copy_make_border_3d", |
| 1041 | [](const Mat& src, int top, int bottom, int left, int right, int front, int behind, int type, float v, const Option& opt) { |
| 1042 | Mat dst; |
| 1043 | copy_make_border_3d(src, dst, top, bottom, left, right, front, behind, type, v, opt); |
| 1044 | return dst; |
| 1045 | }, |
| 1046 | py::arg("src"), |
| 1047 | py::arg("top"), py::arg("bottom"), py::arg("left"), py::arg("right"), py::arg("front"), py::arg("behind"), |
| 1048 | py::arg("type"), py::arg("v"), py::arg("opt") = Option()); |
| 1049 | |
| 1050 | m.def("copy_cut_border", ©_cut_border, |
| 1051 | py::arg("src"), py::arg("dst"), |
| 1052 | py::arg("top"), py::arg("bottom"), py::arg("left"), py::arg("right"), |
| 1053 | py::arg("opt") = Option()); |
| 1054 | m.def( |
| 1055 | "copy_cut_border", |
| 1056 | [](const Mat& src, int top, int bottom, int left, int right, const Option& opt) { |
| 1057 | Mat dst; |
| 1058 | copy_cut_border(src, dst, top, bottom, left, right, opt); |
| 1059 | return dst; |
| 1060 | }, |
| 1061 | py::arg("src"), |
| 1062 | py::arg("top"), py::arg("bottom"), py::arg("left"), py::arg("right"), |
| 1063 | py::arg("opt") = Option()); |
| 1064 | |
| 1065 | m.def("resize_nearest", &resize_nearest, |
| 1066 | py::arg("src"), py::arg("dst"), |
| 1067 | py::arg("w"), py::arg("h"), |
| 1068 | py::arg("opt") = Option()); |
| 1069 | m.def( |
| 1070 | "resize_nearest", |
| 1071 | [](const Mat& src, int w, int h, const Option& opt) { |
| 1072 | Mat dst; |
| 1073 | resize_nearest(src, dst, w, h); |
| 1074 | return dst; |
| 1075 | }, |
| 1076 | py::arg("src"), |
| 1077 | py::arg("w"), py::arg("h"), |
| 1078 | py::arg("opt") = Option()); |
| 1079 | |
| 1080 | m.def("resize_bilinear", &resize_bilinear, |
| 1081 | py::arg("src"), py::arg("dst"), |
| 1082 | py::arg("w"), py::arg("h"), |
| 1083 | py::arg("opt") = Option()); |
| 1084 | m.def( |
| 1085 | "resize_bilinear", |
| 1086 | [](const Mat& src, int w, int h, const Option& opt) { |
| 1087 | Mat dst; |
| 1088 | resize_bilinear(src, dst, w, h, opt); |
| 1089 | return dst; |
| 1090 | }, |
| 1091 | py::arg("src"), |
| 1092 | py::arg("w"), py::arg("h"), |
| 1093 | py::arg("opt") = Option()); |
| 1094 | |
| 1095 | m.def("resize_bicubic", &resize_bicubic, |
| 1096 | py::arg("src"), py::arg("dst"), |
| 1097 | py::arg("w"), py::arg("h"), |
| 1098 | py::arg("opt") = Option()); |
| 1099 | m.def( |
| 1100 | "resize_bicubic", |
| 1101 | [](const Mat& src, int w, int h, const Option& opt) { |
| 1102 | Mat dst; |
| 1103 | resize_bicubic(src, dst, w, h, opt); |
| 1104 | return dst; |
| 1105 | }, |
| 1106 | py::arg("src"), |
| 1107 | py::arg("w"), py::arg("h"), |
| 1108 | py::arg("opt") = Option()); |
| 1109 | |
| 1110 | m.def("convert_packing", &convert_packing, |
| 1111 | py::arg("src"), py::arg("dst"), |
| 1112 | py::arg("elempack"), |
| 1113 | py::arg("opt") = Option()); |
| 1114 | m.def( |
| 1115 | "convert_packing", |
| 1116 | [](const Mat& src, int elempack, const Option& opt) { |
| 1117 | Mat dst; |
| 1118 | convert_packing(src, dst, elempack, opt); |
| 1119 | return dst; |
| 1120 | }, |
| 1121 | py::arg("src"), |
| 1122 | py::arg("elempack"), |
| 1123 | py::arg("opt") = Option()); |
| 1124 | |
| 1125 | m.def("flatten", &flatten, |
| 1126 | py::arg("src"), py::arg("dst"), |
| 1127 | py::arg("opt") = Option()); |
| 1128 | m.def( |
| 1129 | "flatten", |
| 1130 | [](const Mat& src, const Option& opt) { |
| 1131 | Mat dst; |
| 1132 | flatten(src, dst, opt); |
| 1133 | return dst; |
| 1134 | }, |
| 1135 | py::arg("src"), |
| 1136 | py::arg("opt") = Option()); |
| 1137 | |
| 1138 | m.def("cast_float32_to_float16", &cast_float32_to_float16, |
| 1139 | py::arg("src"), py::arg("dst"), |
| 1140 | py::arg("opt") = Option()); |
| 1141 | m.def( |
| 1142 | "cast_float32_to_float16", |
| 1143 | [](const Mat& src, const Option& opt) { |
| 1144 | Mat dst; |
| 1145 | cast_float32_to_float16(src, dst, opt); |
| 1146 | return dst; |
| 1147 | }, |
| 1148 | py::arg("src"), |
| 1149 | py::arg("opt") = Option()); |
| 1150 | |
| 1151 | m.def("cast_float16_to_float32", &cast_float16_to_float32, |
| 1152 | py::arg("src"), py::arg("dst"), |
| 1153 | py::arg("opt") = Option()); |
| 1154 | m.def( |
| 1155 | "cast_float16_to_float32", |
| 1156 | [](const Mat& src, const Option& opt) { |
| 1157 | Mat dst; |
| 1158 | cast_float16_to_float32(src, dst, opt); |
| 1159 | return dst; |
| 1160 | }, |
| 1161 | py::arg("src"), |
| 1162 | py::arg("opt") = Option()); |
| 1163 | |
| 1164 | m.def("cast_int8_to_float32", &cast_int8_to_float32, |
| 1165 | py::arg("src"), py::arg("dst"), |
| 1166 | py::arg("opt") = Option()); |
| 1167 | m.def( |
| 1168 | "cast_int8_to_float32", |
| 1169 | [](const Mat& src, const Option& opt) { |
| 1170 | Mat dst; |
| 1171 | cast_int8_to_float32(src, dst, opt); |
| 1172 | return dst; |
| 1173 | }, |
| 1174 | py::arg("src"), |
| 1175 | py::arg("opt") = Option()); |
| 1176 | |
| 1177 | m.def("cast_float32_to_bfloat16", &cast_float32_to_bfloat16, |
| 1178 | py::arg("src"), py::arg("dst"), |
| 1179 | py::arg("opt") = Option()); |
| 1180 | m.def( |
| 1181 | "cast_float32_to_bfloat16", |
| 1182 | [](const Mat& src, const Option& opt) { |
| 1183 | Mat dst; |
| 1184 | cast_float32_to_bfloat16(src, dst, opt); |
| 1185 | return dst; |
| 1186 | }, |
| 1187 | py::arg("src"), |
| 1188 | py::arg("opt") = Option()); |
| 1189 | |
| 1190 | m.def("cast_bfloat16_to_float32", &cast_bfloat16_to_float32, |
| 1191 | py::arg("src"), py::arg("dst"), |
| 1192 | py::arg("opt") = Option()); |
| 1193 | m.def( |
| 1194 | "cast_bfloat16_to_float32", |
| 1195 | [](const Mat& src, const Option& opt) { |
| 1196 | Mat dst; |
| 1197 | cast_bfloat16_to_float32(src, dst, opt); |
| 1198 | return dst; |
| 1199 | }, |
| 1200 | py::arg("src"), |
| 1201 | py::arg("opt") = Option()); |
| 1202 | |
| 1203 | m.def("quantize_to_int8", &quantize_to_int8, |
| 1204 | py::arg("src"), py::arg("dst"), |
| 1205 | py::arg("scale_data"), |
| 1206 | py::arg("opt") = Option()); |
| 1207 | m.def( |
| 1208 | "quantize_to_int8", |
| 1209 | [](const Mat& src, const Mat& scale_data, const Option& opt) { |
| 1210 | Mat dst; |
| 1211 | quantize_to_int8(src, dst, scale_data, opt); |
| 1212 | return dst; |
| 1213 | }, |
| 1214 | py::arg("src"), |
| 1215 | py::arg("scale_data"), |
| 1216 | py::arg("opt") = Option()); |
| 1217 | |
| 1218 | #if NCNN_STRING |
| 1219 | m.def("layer_to_index", &layer_to_index, py::arg("type")); |
| 1220 | m.def( |
| 1221 | "create_layer", |
| 1222 | [](const char* type) { |
| 1223 | return static_cast<Layer*>(create_layer(type)); |
| 1224 | }, |
| 1225 | py::arg("type")); |
| 1226 | m.def( |
| 1227 | "create_layer", |
| 1228 | [](int index) { |
| 1229 | return static_cast<Layer*>(create_layer(index)); |
| 1230 | }, |
| 1231 | py::arg("index")); |
| 1232 | #endif //NCNN_STRING |
| 1233 | |
| 1234 | #if NCNN_VULKAN |
| 1235 | m.def("create_gpu_instance", &create_gpu_instance, py::arg("driver_path") = ((const char*)0)); |
| 1236 | m.def("destroy_gpu_instance", &destroy_gpu_instance); |
| 1237 | m.def("get_gpu_count", &get_gpu_count); |
| 1238 | m.def("get_default_gpu_index", &get_default_gpu_index); |
| 1239 | m.def("get_gpu_info", &get_gpu_info, py::arg("device_index") = 0, py::return_value_policy::reference); |
| 1240 | m.def("get_gpu_device", &get_gpu_device, py::arg("device_index") = 0, py::return_value_policy::reference); |
| 1241 | |
| 1242 | py::class_<VkBufferMemory>(m, "VkBufferMemory") |
| 1243 | .def_readwrite("offset", &VkBufferMemory::offset) |
| 1244 | .def_readwrite("capacity", &VkBufferMemory::capacity) |
| 1245 | .def_readwrite("refcount", &VkBufferMemory::refcount); |
| 1246 | |
| 1247 | py::class_<VkImageMemory>(m, "VkImageMemory") |
| 1248 | .def_readwrite("width", &VkImageMemory::width) |
| 1249 | .def_readwrite("height", &VkImageMemory::height) |
| 1250 | .def_readwrite("depth", &VkImageMemory::depth) |
| 1251 | .def_readwrite("refcount", &VkImageMemory::refcount); |
| 1252 | |
| 1253 | py::class_<VkAllocator, PyVkAllocator<> >(m, "VkAllocator") |
| 1254 | .def_readonly("vkdev", &VkAllocator::vkdev) |
| 1255 | .def_readwrite("buffer_memory_type_index", &VkAllocator::buffer_memory_type_index) |
| 1256 | .def_readwrite("image_memory_type_index", &VkAllocator::image_memory_type_index) |
| 1257 | .def_readwrite("mappable", &VkAllocator::mappable) |
| 1258 | .def_readwrite("coherent", &VkAllocator::coherent); |
| 1259 | |
| 1260 | py::class_<VkBlobAllocator, VkAllocator, PyVkAllocatorOther<VkBlobAllocator> >(m, "VkBlobAllocator") |
| 1261 | .def(py::init<const VulkanDevice*>()) |
| 1262 | .def("clear", &VkBlobAllocator::clear) |
| 1263 | .def("fastMalloc", (VkBufferMemory * (VkBlobAllocator::*)(size_t size)) & VkBlobAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1264 | .def("fastFree", (void (VkBlobAllocator::*)(VkBufferMemory * ptr)) & VkBlobAllocator::fastFree) |
| 1265 | .def("fastMalloc", (VkImageMemory * (VkBlobAllocator::*)(int, int, int, size_t, int)) & VkBlobAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1266 | .def("fastFree", (void (VkBlobAllocator::*)(VkImageMemory * ptr)) & VkBlobAllocator::fastFree); |
| 1267 | |
| 1268 | py::class_<VkWeightAllocator, VkAllocator, PyVkAllocatorOther<VkWeightAllocator> >(m, "VkWeightAllocator") |
| 1269 | .def(py::init<const VulkanDevice*>()) |
| 1270 | .def("clear", &VkWeightAllocator::clear) |
| 1271 | .def("fastMalloc", (VkBufferMemory * (VkWeightAllocator::*)(size_t size)) & VkWeightAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1272 | .def("fastFree", (void (VkWeightAllocator::*)(VkBufferMemory * ptr)) & VkWeightAllocator::fastFree) |
| 1273 | .def("fastMalloc", (VkImageMemory * (VkWeightAllocator::*)(int, int, int, size_t, int)) & VkWeightAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1274 | .def("fastFree", (void (VkWeightAllocator::*)(VkImageMemory * ptr)) & VkWeightAllocator::fastFree); |
| 1275 | |
| 1276 | py::class_<VkStagingAllocator, VkAllocator, PyVkAllocatorOther<VkStagingAllocator> >(m, "VkStagingAllocator") |
| 1277 | .def(py::init<const VulkanDevice*>()) |
| 1278 | .def("set_size_compare_ratio", &VkStagingAllocator::set_size_compare_ratio) |
| 1279 | .def("clear", &VkStagingAllocator::clear) |
| 1280 | .def("fastMalloc", (VkBufferMemory * (VkStagingAllocator::*)(size_t size)) & VkStagingAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1281 | .def("fastFree", (void (VkStagingAllocator::*)(VkBufferMemory * ptr)) & VkStagingAllocator::fastFree) |
| 1282 | .def("fastMalloc", (VkImageMemory * (VkStagingAllocator::*)(int, int, int, size_t, int)) & VkStagingAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1283 | .def("fastFree", (void (VkStagingAllocator::*)(VkImageMemory * ptr)) & VkStagingAllocator::fastFree); |
| 1284 | |
| 1285 | py::class_<VkWeightStagingAllocator, VkAllocator, PyVkAllocatorOther<VkWeightStagingAllocator> >(m, "VkWeightStagingAllocator") |
| 1286 | .def(py::init<const VulkanDevice*>()) |
| 1287 | .def("fastMalloc", (VkBufferMemory * (VkWeightStagingAllocator::*)(size_t size)) & VkWeightStagingAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1288 | .def("fastFree", (void (VkWeightStagingAllocator::*)(VkBufferMemory * ptr)) & VkWeightStagingAllocator::fastFree) |
| 1289 | .def("fastMalloc", (VkImageMemory * (VkWeightStagingAllocator::*)(int, int, int, size_t, int)) & VkWeightStagingAllocator::fastMalloc, py::return_value_policy::reference_internal) |
| 1290 | .def("fastFree", (void (VkWeightStagingAllocator::*)(VkImageMemory * ptr)) & VkWeightStagingAllocator::fastFree); |
| 1291 | |
| 1292 | py::class_<GpuInfo>(m, "GpuInfo") |
| 1293 | .def(py::init<>()) |
| 1294 | .def("api_version", &GpuInfo::api_version) |
| 1295 | .def("driver_version", &GpuInfo::driver_version) |
| 1296 | .def("vendor_id", &GpuInfo::vendor_id) |
| 1297 | .def("device_id", &GpuInfo::device_id) |
| 1298 | .def("pipeline_cache_uuid", [](GpuInfo& gpuinfo) { |
| 1299 | return py::memoryview::from_buffer(gpuinfo.pipeline_cache_uuid(), {VK_UUID_SIZE}, {sizeof(uint8_t) * VK_UUID_SIZE}); |
| 1300 | }) |
| 1301 | .def("type", &GpuInfo::type) |
| 1302 | .def("device_name", &GpuInfo::device_name); |
| 1303 | |
| 1304 | py::class_<VulkanDevice>(m, "VulkanDevice") |
| 1305 | .def(py::init<int>(), py::arg("device_index") = 0) |
| 1306 | .def( |
| 1307 | "info", [](VulkanDevice& dev) { |
| 1308 | return &dev.info; |
| 1309 | }, |
| 1310 | py::return_value_policy::reference_internal) |
| 1311 | .def("acquire_blob_allocator", &VulkanDevice::acquire_blob_allocator) |
| 1312 | .def("reclaim_blob_allocator", &VulkanDevice::reclaim_blob_allocator, py::arg("vkallocator")) |
| 1313 | .def("acquire_staging_allocator", &VulkanDevice::acquire_staging_allocator) |
| 1314 | .def("reclaim_staging_allocator", &VulkanDevice::reclaim_staging_allocator, py::arg("vkallocator")) |
| 1315 | .def("get_heap_budget", &VulkanDevice::get_heap_budget); |
| 1316 | #endif // NCNN_VULKAN |
| 1317 | |
| 1318 | m.doc() = R"pbdoc( |
| 1319 | ncnn python wrapper |
| 1320 | ----------------------- |
| 1321 | .. currentmodule:: pyncnn |
| 1322 | .. autosummary:: |
| 1323 | :toctree: _generate |
| 1324 | )pbdoc"; |
| 1325 | |
| 1326 | #ifdef VERSION_INFO |
| 1327 | m.attr("__version__") = VERSION_INFO; |
| 1328 | #else |
| 1329 | m.attr("__version__") = "dev"; |
| 1330 | #endif |
| 1331 | } |