Elffuss BraidHub

kikocis/ncnn public

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