You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 54 kB

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