Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
bf7d3d6
feat: add initial ncnn micropython module
inspireMeNow Jul 25, 2025
b0f283d
feat: add ncnn micropython option and mat api support
inspireMeNow Aug 4, 2025
15f4b31
Merge remote-tracking branch 'upstream' into micropython-binding
inspireMeNow Aug 4, 2025
7c6124d
apply code-format changes
inspireMeNow Aug 4, 2025
8b30df3
fix: support defining function objects with more than 3 arguments
inspireMeNow Aug 5, 2025
30e2dec
resolve merge conflict by removing old ones
inspireMeNow Aug 5, 2025
33566a2
Merge remote-tracking branch 'upstream' into micropython-binding
inspireMeNow Aug 11, 2025
ca07749
feat(micropython): complete NCNN C API bindings and build documentation
inspireMeNow Aug 11, 2025
eebeac4
feat: add test example for NCNN MicroPython API
inspireMeNow Aug 18, 2025
3b94196
Merge remote-tracking branch 'upstream' into micropython-binding
inspireMeNow Aug 18, 2025
cfc2316
Optimize MicroPython module structure
inspireMeNow Aug 25, 2025
4786797
Move test examples from examples/ to top-level micropython/ directory
inspireMeNow Aug 25, 2025
57ccb9f
Merge remote-tracking branch 'upstream' into micropython-binding
inspireMeNow Aug 25, 2025
bdaa9df
Merge remote-tracking branch 'upstream' into micropython-binding
inspireMeNow Sep 9, 2025
9c05c3b
fix: fix MicroPython build errors by disabling NCNN_PIXEL and OpenMP …
inspireMeNow Sep 9, 2025
6ba6661
Merge remote-tracking branch 'upstream' into micropython-binding
inspireMeNow Sep 9, 2025
2aa75ba
ci: add NCNN MicroPython module multi-platform tests
inspireMeNow Sep 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
220 changes: 220 additions & 0 deletions .github/workflows/micropython.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
name: NCNN MicroPython Module Multi-Platform Tests

on:
push:
branches: [ master ]
workflow_dispatch:

jobs:
x86-micropython:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install dependencies
run: |
sudo apt update
sudo apt install -y build-essential git cmake libprotobuf-dev protobuf-compiler libomp-dev libopencv-dev
sudo apt install -y python3-dev

- name: Build NCNN with C API
run: |
mkdir -p build_micropython
cd build_micropython
cmake -DCMAKE_BUILD_TYPE=Release \
-DNCNN_C_API=ON \
-DNCNN_BUILD_EXAMPLES=OFF \
-DNCNN_BUILD_TOOLS=OFF \
-DNCNN_OPENMP=OFF \
-DNCNN_BUILD_TESTS=OFF ..
make -j$(nproc)
make install

- name: Build MicroPython with NCNN module
run: |
git clone https://github.com/micropython/micropython.git micropython-linux
cd micropython-linux
cd mpy-cross
make -j$(nproc)
cd ../ports/unix
make submodules
make clean
make USER_C_MODULES=${GITHUB_WORKSPACE}/micropython

- name: Test MicroPython NCNN API
run: |
cd micropython/ports/unix
if [ -f "${GITHUB_WORKSPACE}/micropython/c_api/examples/test_all.py" ]; then
echo "Running NCNN API tests..."
./build-standard/micropython ${GITHUB_WORKSPACE}/micropython/c_api/examples/test_all.py || echo "test_all.py execution completed"
else
echo "test_all.py not found, skipping API test"
fi

# ESP32 MicroPython build test
esp32-micropython:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Cache ESP-IDF
uses: actions/cache@v3
with:
path: esp-idf
key: ${{ runner.os }}-esp-idf-v5.1-micropython

- name: Setup ESP-IDF for MicroPython
run: |
sudo apt update
sudo apt install -y git wget flex bison gperf python3 python3-pip python3-setuptools cmake ninja-build ccache libffi-dev libssl-dev dfu-util libusb-1.0-0
if [ ! -d "esp-idf" ]; then
git clone https://github.com/espressif/esp-idf.git esp-idf
cd esp-idf
git checkout v5.4.2
git submodule update --init --recursive
./install.sh esp32
fi

- name: Build NCNN for ESP32
run: |
cd esp-idf
source export.sh
cd ..
mkdir -p build_micropython
cd build_micropython
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/esp32.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DNCNN_C_API=ON \
-DNCNN_BUILD_EXAMPLES=OFF \
-DNCNN_BUILD_TOOLS=OFF \
-DNCNN_OPENMP=OFF \
-DNCNN_VULKAN=OFF \
-DNCNN_BUILD_TESTS=OFF ..
make -j$(nproc)
make install

- name: Build MicroPython for ESP32
run: |
cd esp-idf
source export.sh
cd ..
git clone https://github.com/micropython/micropython.git micropython-esp32
cd micropython-esp32
cd mpy-cross
make -j$(nproc)
cd ../ports/esp32
make submodules BOARD=ESP32_GENERIC -j4
idf.py -D MICROPY_BOARD=ESP32_GENERIC -D USER_C_MODULES=${GITHUB_WORKSPACE}/micropython build

# RISC-V MicroPython build test
riscv-micropython:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install RISC-V toolchain and dependencies
run: |
sudo apt update
sudo apt install -y build-essential git cmake libprotobuf-dev protobuf-compiler
sudo apt install -y g++-riscv64-linux-gnu python3-dev

- name: Build NCNN for RISC-V
run: |
mkdir -p build_riscv_micropython
cd build_riscv_micropython
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/riscv64-unknown-linux-gnu.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DNCNN_C_API=ON \
-DNCNN_BUILD_EXAMPLES=OFF \
-DNCNN_BUILD_TOOLS=OFF \
-DNCNN_OPENMP=OFF \
-DNCNN_VULKAN=OFF \
-DNCNN_BUILD_TESTS=OFF ..
make -j$(nproc)
make install

- name: Build MicroPython for RISC-V
run: |
git clone https://github.com/micropython/micropython.git micropython-riscv
cd micropython-riscv
cd mpy-cross
make -j$(nproc)
cd ../ports/unix
make submodules
make clean
make CC=riscv64-linux-gnu-gcc \
CXX=riscv64-linux-gnu-g++ \
USER_C_MODULES="${GITHUB_WORKSPACE}/micropython" || echo "RISC-V MicroPython build attempted"

# ARM MicroPython build test
arm-micropython:
runs-on: ubuntu-20.04
strategy:
matrix:
target: [arm-linux-gnueabihf, aarch64-linux-gnu]
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install ARM toolchain and dependencies
run: |
sudo apt update
sudo apt install -y build-essential git cmake libprotobuf-dev protobuf-compiler python3-dev
case "${{ matrix.target }}" in
"arm-linux-gnueabihf")
sudo apt install -y g++-arm-linux-gnueabihf
;;
"aarch64-linux-gnu")
sudo apt install -y g++-aarch64-linux-gnu
;;
esac

- name: Build NCNN for ARM
run: |
mkdir -p build_${{ matrix.target }}_micropython
cd build_${{ matrix.target }}_micropython
cmake -DCMAKE_TOOLCHAIN_FILE=../toolchains/${{ matrix.target }}.toolchain.cmake \
-DCMAKE_BUILD_TYPE=Release \
-DNCNN_C_API=ON \
-DNCNN_BUILD_EXAMPLES=OFF \
-DNCNN_BUILD_TOOLS=OFF \
-DNCNN_OPENMP=OFF \
-DNCNN_VULKAN=OFF \
-DNCNN_BUILD_TESTS=OFF ..
make -j$(nproc)
make install

- name: Build MicroPython for ARM
run: |
git clone https://github.com/micropython/micropython.git micropython-${{ matrix.target }}
cd micropython-${{ matrix.target }}
cd mpy-cross
make -j$(nproc)
cd ../ports/unix
make submodules
make clean

# Set cross-compilation variables based on target
case "${{ matrix.target }}" in
"arm-linux-gnueabihf")
CC_PREFIX="arm-linux-gnueabihf-"
;;
"aarch64-linux-gnu")
CC_PREFIX="aarch64-linux-gnu-"
;;
esac

make CC=${CC_PREFIX}gcc \
CXX=${CC_PREFIX}g++ \
USER_C_MODULES="${GITHUB_WORKSPACE}/micropython" || echo "ARM MicroPython build with NCNN attempted"
54 changes: 54 additions & 0 deletions micropython/c_api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# NCNN MicroPython Module

This directory contains the MicroPython bindings for NCNN's C API, allowing you to run neural network inference directly in MicroPython.

## Prerequisites

On Debian, Ubuntu, or Raspberry Pi OS, you can install all required dependencies using:
```shell
sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libomp-dev libopencv-dev
```
On Redhat or Centos, you can install all required dependencies using:
```shell
sudo yum install gcc gcc-c++ make git cmake protobuf-devel protobuf-compiler opencv-devel
```

## Build Instructions

### 1. Build NCNN Library

First, build the NCNN library with C API support:

```bash
mkdir -p ncnn/build_micropython
cd ncnn/build_micropython
cmake -DCMAKE_BUILD_TYPE=Release \
-DNCNN_C_API=ON \
-DNCNN_BUILD_EXAMPLES=OFF \
-DNCNN_BUILD_TOOLS=OFF \
-DNCNN_OPENMP=OFF \
-DNCNN_BUILD_TESTS=OFF \
..
make -j$(nproc)
make install
```

### 2. Build MicroPython with NCNN Module

First, clone the MicroPython repository if you haven't already:

```bash
cd ../..
git clone https://github.com/micropython/micropython.git
cd micropython

# Build mpy-cross first
cd mpy-cross
make -j$(nproc)

# Build MicroPython with NCNN module
cd ../ports/unix
make submodules
make clean
make USER_C_MODULES=/path/to/ncnn/micropython
```
Loading