ncnn_mp — ncnn C API bindings for MicroPython #6292
Willaaaaaaa
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Repository: ncnn_mp
Brief Introduction
What is ncnn_mp
ncnn_mpis an external C module developed for the MicroPython environment, designed to port Tencent's high-performance neural network inference framework, ncnn, to resource-constrained microcontrollers (MCUs).Why ncnn_mp
Although there exist Python bindings for
ncnn,ncnn_mpspecifically targets MicroPython and MCUs.By encapsulating the
ncnnC API in an object-oriented way,ncnn_mpenables developers to usencnnfeatures with a natural and Pythonic API on embedded platforms.How it works
Select the target platform (Unix, ESP32 ...)
->
micropython.cmakeormicropython.mk-> Compile the
ncnn_mp.csource code (external C module)->
ncnn_mp.ccalls the ncnn C apis throughc_api.h-> A MicroPython firmware containing the
ncnn_mpmodule is generatedncnn_mp.c:Allocatoras an ExampleDifficulties
Overview
.pyistub file, and CI workflows.Practice
1. Converting from C-style handles to Python objects
Allocator,Option,Mat,Blob,ParamDict,DataReader,ModelBin,Layer,Net, andExtractor. Each C struct inncnn_mp.ccontains a pointer to the corresponding ncnn C object. This object-oriented approach automates resource management by MicroPython, which automatically calls the__del__method to safely release the underlying ncnn resources.2. Resolving the Unexpected MicroPython Instance Attribute Lookup Routine
Mat.wandMat.h) together with regular methods (for example,Mat.fill()) led to unexpectedAttributeErrorwhen accessing methods from instances. The behaviour was inconsistent: direct function-style calls (Mat.fill(a, ...)) worked, whilea.fill()raised an error.attrfunction. Ifattrreturns a value or indicates the attribute is handled, lookup stops. Ifattrdoes not find the attribute, it must explicitly signal that lookup should continue to the instance's locals dict. The fix was to modify theattrfunction so that when a requested attribute is not one of the dynamic attributes it recognizes, it setsdest[0] = MP_OBJ_NULLto allow normal lookup in thelocals_dict. After this change, both dynamic attributes and normal method lookups behave correctly.3. Improving the Python Development Experience
.pyistub file was written manually. This file provides complete type definitions and docstrings for all classes and methods, enabling full support for static analysis, IntelliSense, and parameter information in modern editors like VS Code.4. Bridging Python binary data and ncnn Mat structures
ncnnexpects tensor/matrix data in native memory formats, whereas Python commonly usesbytesandbytearrayfor binary data. There was no native C API to directly construct a Mat from a continuous Python buffer.Mat.from_bytes()to populate an existing an ncnnMatfrom a bytes-like object. The implementation validates buffer size againstw * h * c * sizeof(element)and copies data into Mat’s memory. On mismatch, aValueErroris raised.Mat.to_bytes()to return abyteobject containing data. The method allocates a Python-managed buffer, copies data into it, and returns it to the caller. Memory ownership and lifetime are handled by MicroPython’s GC to avoid leaks.5. Other problems&solutions
Innovations
mnistin./example).pyifile and docstrings)Further work
Update on 9.4: All is done now.
See
Beta Was this translation helpful? Give feedback.
All reactions