emlearn-micropython icon indicating copy to clipboard operation
emlearn-micropython copied to clipboard

Segfault when insufficient memory

Open jonnor opened this issue 10 months ago • 0 comments

The following code

import emlearn_trees

import array
import gc

def test_trees_alloc():
    n_trees = 100
    nodes_per_tree = 10000

    leaves = 127
    n_classes = leaves
    n_features = 127
    n_nodes = n_trees * nodes_per_tree

    print(n_nodes)
    model = emlearn_trees.new(n_trees, n_nodes, leaves)
    model.setdata(n_features, n_classes)

    for n in range(n_trees):
        model.addroot(n)

    for n in range(n_nodes):
        model.addnode(-1, -2, 3, 4)
        pass

    inp = array.array('h', range(n_features))
    out = array.array('f', range(n_classes))
    model.predict(inp, out)

    print('OK')

test_trees_alloc()

segfault with 1M heap

/usr/bin/time -v micropython -X heapsize=1M test_trees.py 

Works with 10 M heap

/usr/bin/time -v micropython -X heapsize=10M test_trees.py

This is probably due to m_malloc returning 0. I thought it would raise a MemoryError exception... This likely affects many/most of the other modules as well.

jonnor avatar Jan 30 '25 09:01 jonnor