Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 9 additions & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ jobs:
with:
submodules: true
- name: Install OS dependencies
run: sudo add-apt-repository universe
run: |
sudo add-apt-repository universe
sudo apt-get install qemu-system-arm
- uses: actions/setup-python@v5
with:
python-version: '3.10'
Expand All @@ -71,6 +73,12 @@ jobs:
run: make dist ARCH=armv7m V=1
- name: Build module armv7emsp
run: make dist ARCH=armv7emsp V=1
- name: Build module armv7emdp
run: make dist ARCH=armv7emdp V=1
- name: Build QEMU MicroPython image
run: make qemu_build V=1
- name: Run tests under QEMU
run: make check_qemu V=1
- name: Archive dist artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
45 changes: 37 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,35 @@ $(MODULES_PATH)/emlearn_cnn_int8.mpy: $(MODULES_PATH)/emlearn_cnn_fp32.mpy
# Generate list of .mpy files
MODULE_MPYS = $(addprefix $(MODULES_PATH)/,$(addsuffix .mpy,$(MODULES)))

# Build dynamic native module (without forced clean)
$(MODULES_PATH)/%.mpy:
# Build dynamic native module
$(MODULES_PATH)/%.mpy: $(ARCH_SENTINEL)
$(MAKE) -C $(or $($(*)_SRC),src/$*) \
ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) CFLAGS_EXTRA=$(CFLAGS_EXTRA) \
V=1 $($(*)_CONFIG) dist

# A file to track ARCH changes etc, to nuke old artifact
ARCH_SENTINEL := .sentinel_arch_$(ARCH)
$(ARCH_SENTINEL):
@echo "ARCH changed or not set — cleaning old build artifacts"
@rm -f .sentinel_arch_*
find src -name ".mpy_ld_cache" -type d -exec rm -rf {} + ; \
find src -name "build" -type d -exec rm -rf {} + ; \
find src -name "*.mpy" -delete ; \
find src -name "*.o" -delete
@mkdir -p $(MODULES_PATH)
@touch $@

# CNN modules need clean build due to shared build directory
# They must also build sequentially (fp32 first, then int8)
$(MODULES_PATH)/emlearn_cnn_fp32.mpy:
$(MODULES_PATH)/emlearn_cnn_fp32.mpy: $(ARCH_SENTINEL)
$(MAKE) -C src/tinymaix_cnn \
ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) CFLAGS_EXTRA=$(CFLAGS_EXTRA) \
V=1 CONFIG=fp32 clean
$(MAKE) -C src/tinymaix_cnn \
ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) CFLAGS_EXTRA=$(CFLAGS_EXTRA) \
V=1 CONFIG=fp32 dist

$(MODULES_PATH)/emlearn_cnn_int8.mpy: $(MODULES_PATH)/emlearn_cnn_fp32.mpy
$(MODULES_PATH)/emlearn_cnn_int8.mpy: $(MODULES_PATH)/emlearn_cnn_fp32.mpy $(ARCH_SENTINEL)
$(MAKE) -C src/tinymaix_cnn \
ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) CFLAGS_EXTRA=$(CFLAGS_EXTRA) \
V=1 CONFIG=int8 clean
Expand Down Expand Up @@ -159,10 +171,7 @@ codesize:
python3 tools/code_size.py $(MPY_DIR_ABS)/ports/unix/build-standard

clean:
make -C src/emlearn_trees/ ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) V=1 clean
make -C src/emlearn_neighbors/ ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) V=1 clean
make -C src/emlearn_iir/ ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) V=1 clean
make -C src/emlearn_logreg/ ARCH=$(ARCH) MPY_DIR=$(MPY_DIR_ABS) V=1 clean
git clean -dfx src/
rm -rf ./dist

RELEASE_NAME = emlearn-micropython-$(VERSION)
Expand All @@ -177,4 +186,24 @@ check: check_unix_natmod

dist: $(MODULE_MPYS)

# =============================================================================
# QEMU MicroPython targets
# =============================================================================

QEMU_PORT_DIR = $(MPY_DIR)/ports/qemu
QEMU_BOARD ?= MPS2_AN500
QEMU_ARCH ?= armv7emdp
QEMU_FIRMWARE = $(QEMU_PORT_DIR)/build-$(QEMU_BOARD)/firmware.elf

# Build firmware for QEMU
.PHONY: qemu_build
qemu_build:
$(MAKE) -C $(QEMU_PORT_DIR) BOARD=$(QEMU_BOARD) MICROPY_HEAP_SIZE=1024000

# Run tests/test_all.py on QEMU using mpremote mount
# Usage: make check_qemu QEMU_BOARD=MPS2_AN500 QEMU_ARCH=armv7emdp
.PHONY: check_qemu
check_qemu: $(QEMU_FIRMWARE)
python3 $(abspath tools/run_qemu_tests.py) --board $(QEMU_BOARD) --arch $(QEMU_ARCH) --abi-version $(MPY_ABI_VERSION) --mount $(abspath .)


133 changes: 0 additions & 133 deletions spectrofood_download.py

This file was deleted.

16 changes: 7 additions & 9 deletions tests/npyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,23 +271,21 @@ def write_values(self, arr, typecode=None):
#print('write', self.written_bytes)


def load(filelike) -> tuple[tuple, array.array]:
def load(filelike, chunk_size=64) -> tuple[tuple, array.array]:
"""
Load array from .npy file

Convenience function for doing it in one shot.
For streaming, use npyfile.Reader instead
"""

chunks = []
with Reader(filelike) as reader:
# Just read everything in one chunk
total_items = compute_items(reader.shape)
for c in reader.read_data_chunks(total_items):
chunks.append(c)
data = array.array(reader.typecode, (0 for _ in range(total_items)))
idx = 0
for chunk in reader.read_data_chunks(chunk_size):
data[idx:idx + len(chunk)] = chunk
idx += len(chunk)
return reader.shape, data

assert len(chunks) == 1
return reader.shape, chunks[0]

def save(filelike, arr : array.array, shape=None, typecode=None):
"""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import sys
import gc

# Find the module path (architecture+version specific)
sys_mpy = sys.implementation._mpy
Expand Down Expand Up @@ -63,6 +64,16 @@ def main():
passed = 0
failed = 0

# Test markers for external test runners (mpremote, etc.)
# These allow the runner to know when tests are complete without relying on timeouts
print('\n=== TEST START ===')

free = gc.mem_free()
alloc = gc.mem_alloc()
print(f"RAM free={free} used={alloc} total={free+alloc}")

print('sys.path', sys.path)

for module_name in modules:
mod = None
print(f'{module_name}:')
Expand All @@ -75,6 +86,9 @@ def main():
failed += 1
continue

# Try to free space
gc.collect()

module_attributes = dir(mod)
tests = [ o for o in module_attributes if o.startswith('test_') ]
for test_name in tests:
Expand All @@ -92,8 +106,12 @@ def main():
print(f'\t PASS')
passed += 1

# Try to free space
gc.collect()

print(f'Passed: {passed}')
print(f'Failed: {failed}')
print('\n=== TEST END ===')

# Let status code reflect number of failures
return failed
Expand Down
3 changes: 0 additions & 3 deletions tests/test_extratrees_cancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,11 @@ def load_npy_labels_int16(filename):
return array.array('h', labels)

def test_real_dataset():
print("=== REAL DATASET TEST ===")

X_train_flat = load_npy_features_int16(DATA_FILES['X_train'])
y_train = load_npy_labels_int16(DATA_FILES['y_train'])
X_test_flat = load_npy_features_int16(DATA_FILES['X_test'])
y_test = load_npy_labels_int16(DATA_FILES['y_test'])


n_features = 30
n_train = len(y_train)
n_test = len(y_test)
Expand Down
25 changes: 10 additions & 15 deletions tests/test_extratrees_wine.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,22 @@ def load_npy_features_int16(filename):
"""Load .npy file and convert to int16 array (scaled from float32)"""
shape, data = npyfile.load(filename)
# Scale float32 data to int16 range (multiply by 1000 and convert)
scaled = [int(v * 1000) for v in data]
return array.array('h', scaled)
return array.array('h', (int(v * 1000) for v in data))

def load_npy_labels_int16(filename):
"""Load .npy file and convert to int16 array (labels, no scaling)"""
shape, data = npyfile.load(filename)
# Labels are already integers (0.0, 1.0, 2.0), just convert directly
labels = [int(v) for v in data]
return array.array('h', labels)
return array.array('h', (int(v) for v in data))

def test_wine():
print("=== WINE DATASET TEST (3-class) ===")

# Load preprocessed data
try:
X_train_flat = load_npy_features_int16(DATA_FILES['X_train'])
y_train = load_npy_labels_int16(DATA_FILES['y_train'])
X_test_flat = load_npy_features_int16(DATA_FILES['X_test'])
y_test = load_npy_labels_int16(DATA_FILES['y_test'])
except:
print("Error: Run wine/prepare.py first")
return
X_train_flat = load_npy_features_int16(DATA_FILES['X_train'])
y_train = load_npy_labels_int16(DATA_FILES['y_train'])
X_test_flat = load_npy_features_int16(DATA_FILES['X_test'])
y_test = load_npy_labels_int16(DATA_FILES['y_test'])

n_features = 13 # 13 wine features (alcohol, malic_acid, ash, etc.)
n_train = len(y_train)
Expand All @@ -54,12 +48,13 @@ def test_wine():
print(f"Classes: {n_classes} (wine cultivars 0, 1, 2)")
print("Task: Classify wine cultivar")

max_samples = n_train
# Create model
model = emlearn_extratrees.new(
n_features, n_classes,
n_trees=20, max_depth=12, min_samples_leaf=2,
n_thresholds=15, subsample_ratio=0.8, feature_subsample_ratio=1.0,
max_nodes=3000, max_samples=500, rng_seed=42
n_trees=5, max_depth=5, min_samples_leaf=2,
n_thresholds=8, subsample_ratio=0.8, feature_subsample_ratio=1.0,
max_nodes=1000, max_samples=max_samples, rng_seed=42
)

train_start = time.ticks_ms()
Expand Down
Loading
Loading