MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

深度技术分析:v923z/micropython-ulab

项目背景与痛点

在嵌入式计算的世界里,资源的有限性一直是开发者面临的一大挑战。尽管 MicroPython 作为资源受限的设备上的轻量级 Python 解释器,提供了强大的编程能力,但对于那些处理大数据集和矩阵运算的任务来说,它还是显得有些力不从心。在这种情况下,micropython-ulab 的诞生正是为了填补这一空白,为微控制器提供了一个类似于 NumPy 的快速矢量模块。

micropython-ulab 适用于 MicroPython 和 CircuitPython 以及它们的衍生产品。它的设计目标之一就是优化在资源受限的设备上进行高效的数值计算,尤其是在进行科学和工程应用时需要进行大量的矩阵运算和信号处理。

核心技术揭秘

micropython-ulab 在底层代码中大量采用了优化的函数和算法,比如高效的数值计算库,确保在低内存和计算能力有限的设备上能够快速运行。此外,它还采用了微调的编译器和解释器,以提高执行效率。

以下是 ulab 的技术架构图:

graph TD A[输入数据] --> B[数据预处理] B --> C("内存管理") C --> D{"算法选择"} D -->|"矩阵运算"| E[线性代数模块] D -->|"信号处理"| F[FFT 模块] E --> G[结果输出] F --> G

在架构层面,ulab 的设计考虑了模块化和灵活性。它提供了多种算法,让开发者可以根据具体需求选择适当的算法来处理数据。例如,线性代数模块处理矩阵运算,而 FFT 模块则用于信号处理。这种设计极大地提高了代码的复用性和兼容性。

功能亮点与差异

除了提供快速的矩阵运算和信号处理功能之外,micropython-ulab 最大的特点在于其内存管理机制。在资源受限的环境里,如何高效地使用内存是一个至关重要的问题。ulab 通过优化的数据结构和算法,能够在尽量少的内存占用下完成复杂的数值计算任务。

相比于同类竞品,ulab 能够在保证性能的同时提供更多的 API 支持,比如在 FFT 模块中,它提供了多种窗口函数的支持,以满足不同的信号处理需求。

应用场景与落地建议

micropython-ulab 在生产环境中,能够广泛应用于各种嵌入式系统。例如,在物联网设备中,处理传感器数据是常见的任务,通过对采集数据进行实时处理,可以避免大量数据传输到云端,从而节省带宽、降低能耗。

在落地过程中,开发者需要注意几点:

  1. 内存占用:资源受限的设备对内存使用尤为敏感,开发者应细致地管理内存分配,确保 ulab 完成功能的同时不至于导致系统资源耗尽。
  2. 性能优化:针对具体的应用任务,可能需要对默认算法进行微调或选择更优的算法,以尽量提高计算效率。
  3. 调试与验证:由于嵌入式环境的特殊性,开发过程中需格外注意调试与验证环节,确保算法的正确性和资源优化。

综合评价

micropython-ulab 在嵌入式数值计算领域中提供了强大的支持,弥补了 MicroPython 在面对大量计算任务时的不足。它提供了诸多优秀的特性,不仅在计算效率上表现出色,而且在内存管理和算法设计上也相当具有竞争力。

当然,任何工具都有局限性。对于资源极其受限的设备,开发者仍需谨慎选择使用哪些功能和算法,避免资源浪费。总体而言,micropython-ulab 是一个能够显著提升嵌入式系统数值计算能力的优秀选择。

关于

GitHub 原始描述(英文)

a numpy-like fast vector module for micropython, circuitpython, and their derivatives

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
783
价值分
341
活跃状态
活跃

语言占比

C
CMake
Makefile
Python
Shell

Release

README

ulab

Documentation Status

ulab is a numpy-like array manipulation library for micropython and CircuitPython. The module is written in C, defines compact containers (ndarrays) for numerical data of one to four dimensions, and is fast. The library is a software-only standard micropython user module, i.e., it has no hardware dependencies, and can be compiled for any platform. 8-, and 16-bit signed and unsigned integer dtypes, as well as float, and, optionally, complex are supported. The float implementation of micropython (32-bit float, or 64-bit double) is automatically detected and handled.

  1. Supported functions and methods
    1. ndarray methods
    2. numpy and scipy functions
    3. ulab utilities
    4. user module
  2. Usage
  3. Finding help
  4. Benchmarks
  5. Firmware
    1. Customising the firmware
    2. Platforms including ulab
    3. Compiling
      1. UNIX
      2. STM-based boards
      3. ESP32-based boards
      4. RP2-based boards
      5. Compiling for CircuitPython
  6. Issues, contributing, and testing
    1. Testing

Supported functions and methods

ndarray methods

ulab implements numpy’s ndarray with the ==, !=, <, <=, >, >=, +, -, /, *, **, %, +=, -=, *=, /=, **=, %= binary operators, and the len, ~, -, +, abs unary operators that operate element-wise. Type-aware ndarrays can be initialised from any micropython iterable, lists of iterables via the array constructor, or by means of the arange, concatenate, diag, eye, frombuffer, full, linspace, logspace, ones, or zeros functions.

ndarrays can be sliced, and iterated on, and have a number of their own methods, and properties, such as flatten(), itemsize, reshape(), shape, size, strides, tobytes(), tolist(), and transpose() and T. If the firmware is compiled with complex support, the imag, and real properties are automatically included.

numpy and scipy functions

In addition, ulab includes universal functions, many numpy functions, and functions from the numpy.fft, numpy.linalg, numpy.random, scipy.linalg, scipy.optimize, scipy.signal, and scipy.special modules. A complete list of available routines can be found under micropython-ulab.

ulab utilities

The utils module contains functions for interfacing with peripheral devices supporting the buffer protocol. These functions do not have an obvious numpy equivalent, but share a similar programming interface, and allow direct data input-output between numerical arrays and hardware components.

user module

User-defined functions operating on numerical data can easily be added via the user module. This allows for transparent extensions, without having to change anything in the core. Hints as to how to work with ndarrays at the C level can be found in the programming manual.

Usage

ulab sports a numpy/scipy-compatible interface, which makes porting of CPython code straightforward. The following snippet should run equally well in micropython, or on a PC.

try:
    from ulab import numpy
    from ulab import scipy
except ImportError:
    import numpy
    import scipy.special

x = numpy.array([1, 2, 3])
scipy.special.erf(x)

Finding help

Documentation can be found on readthedocs under micropython-ulab, as well as at circuitpython-ulab. A number of practical examples are listed in Jeff Epler’s excellent circuitpython-ulab overview. The tricks chapter of the user manual discusses methods by which RAM and speed can be leveraged in particular numerical problems.

Benchmarks

Representative numbers on performance can be found under ulab samples.

Firmware

Pre-built, and up-to-date firmware files for select platforms can be downloaded from micropython-builder.

Customising the firmware

If flash space is a concern, unnecessary functions can be excluded from the compiled firmware with pre-processor switches. In addition, ulab also has options for trading execution speed for firmware size. A thorough discussion on how the firmware can be customised can be found in the corresponding section of the user manual.

Platforms including ulab

ulab is also included in the following compiled micropython variants and derivatives:

  1. CircuitPython for SAMD51 and nRF microcontrollers https://github.com/adafruit/circuitpython
  2. MicroPython for K210 https://github.com/loboris/MicroPython_K210_LoBo
  3. MaixPy https://github.com/sipeed/MaixPy
  4. OpenMV https://github.com/openmv/openmv
  5. pimoroni-pico https://github.com/pimoroni/pimoroni-pico
  6. Tulip Creative Computer https://github.com/shorepine/tulipcc

Compiling

If you want to try the latest version of ulab on micropython or one of its forks, the firmware can be compiled from the source by following these steps:

UNIX port

Simply clone the ulab repository with

git clone https://github.com/v923z/micropython-ulab.git ulab

and then run

./build.sh [matrix.dims] # Dimensions is 2 by default

This command will clone micropython, and build the unix port automatically, as well as run the test scripts. If you want an interactive unix session, you can launch it in

ulab/micropython/ports/unix

STM-based boards

First, you have to clone the micropython repository by running

git clone https://github.com/micropython/micropython.git

on the command line. This will create a new repository with the name micropython. Staying there, clone the ulab repository with

git clone https://github.com/v923z/micropython-ulab.git ulab

If you don’t have the cross-compiler installed, your might want to do that now, for instance on Linux by executing

sudo apt-get install gcc-arm-none-eabi

If this step was successful, you can try to run the make command in the port’s directory as

make BOARD=PYBV11 USER_C_MODULES=../../../ulab all

which will prepare the firmware for pyboard.v.11. Similarly,

make BOARD=PYBD_SF6 USER_C_MODULES=../../../ulab all

will compile for the SF6 member of the PYBD series. If your target is unix, you don’t need to specify the BOARD parameter.

Provided that you managed to compile the firmware, you would upload that by running either

dfu-util --alt 0 -D firmware.dfu

or

python pydfu.py -u firmware.dfu

In case you got stuck somewhere in the process, a bit more detailed instructions can be found under https://github.com/micropython/micropython/wiki/Getting-Started, and https://github.com/micropython/micropython/wiki/Pyboard-Firmware-Update.

ESP32-based boards

ulab can be tested on the ESP32 in wokwi’s micropython emulator without having to compile the C code. This utility also offers the possibility to save and share your micropython code.

Firmware for Espressif hardware can be built in two different ways, which are discussed in the next two paragraphs. A solution for issues with the firmware size is outlined in the last paragraph of this section.

Compiling with cmake

Beginning with version 1.15, micropython switched to cmake on the ESP32 port. If your operating system supports CMake > 3.12, you can either simply download, and run the single build script, or follow the step in this section. Otherwise, you should skip to the next one, where the old, make-based approach is discussed.

In case you encounter difficulties during the build process, you can consult the (general instructions for the ESP32)[https://github.com/micropython/micropython/tree/master/ports/esp32#micropython-port-to-the-esp32].

First, clone the ulab, the micropython, as well as the espressif repositories:

export BUILD_DIR=$(pwd)

git clone https://github.com/v923z/micropython-ulab.git ulab
git clone https://github.com/micropython/micropython.git

cd $BUILD_DIR/micropython/

git clone -b v4.0.2 --recursive https://github.com/espressif/esp-idf.git

Also later releases of esp-idf are possible (e.g. v4.2.1).

Then install the ESP-IDF tools:

cd esp-idf
./install.sh
. ./export.sh

Next, build the micropython cross-compiler, and the ESP sub-modules:

cd $BUILD_DIR/micropython/mpy-cross
make
cd $BUILD_DIR/micropython/ports/esp32
make submodules

At this point, all requirements are installed and built. We can now compile the firmware with ulab. In $BUILD_DIR/micropython/ports/esp32 create a makefile with the following content:

BOARD = GENERIC
USER_C_MODULES = $(BUILD_DIR)/ulab/code/micropython.cmake

include Makefile

You specify with the BOARD variable, what you want to compile for, a generic board, or TINYPICO (for micropython version >1.1.5, use UM_TINYPICO), etc. Still in $BUILD_DIR/micropython/ports/esp32, you can now run make.

Compiling with make

If your operating system does not support a recent enough version of CMake, you have to stay with micropython version 1.14. The firmware can be compiled either by downloading and running the build script, or following the steps below:

First, clone ulab with

git clone https://github.com/v923z/micropython-ulab.git ulab

and then, in the same directory, micropython

git clone https://github.com/micropython/micropython.git

At this point, you should have ulab, and micropython side by side.

With version 1.14, micropython switched to cmake on the ESP32 port, thus breaking compatibility with user modules. ulab can, however, still be compiled with version 1.14. You can check out a particular version by pinning the release tag as


cd ./micropython/
git checkout tags/v1.14

Next, update the submodules,

git submodule update --init
cd ./mpy-cross && make # build cross-compiler (required)

and find the ESP commit hash

cd ./micropython/ports/esp32
make ESPIDF= # will display supported ESP-IDF commit hashes
# output should look like: """
# ...
# Supported git hash (v3.3): 9e70825d1e1cbf7988cf36981774300066580ea7
# Supported git hash (v4.0) (experimental): 4c81978a3e2220674a432a588292a4c860eef27b

Choose an ESPIDF version from one of the options printed by the previous command:

ESPIDF_VER=9e70825d1e1cbf7988cf36981774300066580ea7

In the micropython directory, create a new directory with

mkdir esp32

Your micropython directory should now look like

ls
ACKNOWLEDGEMENTS    CONTRIBUTING.md  esp32     lib      mpy-cross  README.md
CODECONVENTIONS.md  docs             examples  LICENSE  ports      tests
CODEOFCONDUCT.md    drivers          extmod    logo     py         tools

In ./micropython/esp32, download the software development kit with

git clone https://github.com/espressif/esp-idf.git esp-idf
cd ./esp-idf
git checkout $ESPIDF_VER
git submodule update --init --recursive # get idf submodules
pip install -r ./requirements.txt # install python reqs

Next, still staying in ./micropython/eps32/esd-idf/, install the ESP32 compiler. If using an ESP-IDF version >= 4.x (chosen by $ESPIDF_VER above), this can be done by running . $BUILD_DIR/esp-idf/install.sh. Otherwise, for version 3.x, run the following commands in in .micropython/esp32/esp-idf:

# for 64 bit linux
curl https://dl.espressif.com/dl/xtensa-esp32-elf-linux64-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar xvz

# for 32 bit
# curl https://dl.espressif.com/dl/xtensa-esp32-elf-linux32-1.22.0-80-g6c4433a-5.2.0.tar.gz | tar xvz

# don't worry about adding to path; we'll specify that later

# also, see https://docs.espressif.com/projects/esp-idf/en/v3.3.2/get-started for more info

Finally, build the firmware:

cd ./micropython/ports/esp32
# temporarily add esp32 compiler to path
export PATH=../../esp32/esp-idf/xtensa-esp32-elf/bin:$PATH
export ESPIDF=../../esp32/esp-idf # req'd by Makefile
export BOARD=GENERIC # options are dirs in ./boards
export USER_C_MODULES=../../../ulab # include ulab in firmware

make submodules & make all

If it compiles without error, you can plug in your ESP32 via USB and then flash it with:

make erase && make deploy
What to do, if the firmware is too large?

When selecting BOARD=TINYPICO, the firmware is built but fails to deploy, because it is too large for the standard partitions. We can rectify the problem by creating a new partition table. In order to do so, in $BUILD_DIR/micropython/ports/esp32/, copy the following 8 lines to a file named partitions_ulab.cvs:

# Notes: the offset of the partition table itself is set in
# $ESPIDF/components/partition_table/Kconfig.projbuild and the
# offset of the factory/ota_0 partition is set in makeimg.py
# Name,   Type, SubType, Offset,  Size, Flags
nvs,      data, nvs,     0x9000,  0x6000,
phy_init, data, phy,     0xf000,  0x1000,
factory,  app,  factory, 0x10000, 0x200000,
vfs,      data, fat,     0x220000, 0x180000,

This expands the factory partition by 128 kB, and reduces the size of vfs by the same amount. Having defined the new partition table, we should extend sdkconfig.board by adding the following two lines:

CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_ulab.csv"

This file can be found in $BUILD_DIR/micropython/ports/esp32/boards/TINYPICO/. Finally, run make clean, and make. The new firmware contains the modified partition table, and should fit on the microcontroller.

RP2-based boards

RP2 firmware can be compiled either by downloading and running the single build script/build script for Pico W, or executing the commands below.

First, clone micropython:

git clone https://github.com/micropython/micropython.git

Then, setup the required submodules:

cd micropython
git submodule update --init lib/tinyusb
git submodule update --init lib/pico-sdk
cd lib/pico-sdk
git submodule update --init lib/tinyusb

You’ll also need to compile mpy-cross:

cd ../../mpy-cross
make

That’s all you need to do for the micropython repository. Now, let us clone ulab (in a directory outside the micropython repository):

cd ../../
git clone https://github.com/v923z/micropython-ulab ulab

With this setup, we can now build the firmware. Back in the micropython repository, use these commands:

cd ports/rp2
make USER_C_MODULES=/path/to/ulab/code/micropython.cmake

If micropython and ulab were in the same folder on the computer, you can set USER_C_MODULES=../../../ulab/code/micropython.cmake. The compiled firmware will be placed in micropython/ports/rp2/build.

Compiling for CircuitPython

Adafruit Industries always include a relatively recent version of ulab in their nightly builds. However, if you really need the bleeding edge, you can easily compile the firmware from the source. Simply clone circuitpython, and move the commit pointer to the latest version of ulab (ulab will automatically be cloned with circuitpython):

git clone https://github.com/adafruit/circuitpython.git

cd circuitpyton/extmod/ulab

# update ulab here
git checkout master
git pull

You might have to check, whether the CIRCUITPY_ULAB variable is set to 1 for the port that you want to compile for. You find this piece of information in the make fragment:

circuitpython/ports/port_of_your_choice/mpconfigport.mk

After this, you would run make with the single BOARD argument, e.g.:

make BOARD=mini_sam_m4

Issues, contributing, and testing

If you find a problem with the code, please, raise an issue! An issue should address a single problem, and should contain a minimal code snippet that demonstrates the difference from the expected behaviour. Reducing a problem to the bare minimum significantly increases the chances of a quick fix.

Feature requests (porting a particular function from numpy or scipy) should also be posted at ulab issue.

Contributions of any kind are always welcome. If you feel like adding to the code, you can simply issue a pull request. If you do so, please, try to adhere to micropython’s coding conventions.

However, you can also contribute to the documentation (preferably via the jupyter notebooks, or improve the tests.

Testing

If you decide to lend a hand with testing, here are the steps:

  1. Write a test script that checks a particular function, or a set of related functions!
  2. Drop this script in one of the folders in ulab tests!
  3. Run the ./build.sh script in the root directory of ulab! This will clone the latest micropython, compile the firmware for unix, execute all scripts in the ulab/tests, and compare the results to those in the expected results files, which are also in ulab/tests, and have an extension .exp. In case you have a new snippet, i.e., you have no expected results file, or if the results differ from those in the expected file, a new expected file will be generated in the root directory. You should inspect the contents of this file, and if they are satisfactory, then the file can be moved to the ulab/tests folder, alongside your snippet.

评论

暂无评论