Skip to content

kernel_runtime

Shared operator-kernel runtime helpers.

This module is the general-purpose sibling of :mod:helia_aot.aot.operators.activation_runtime. Where the activation runtime collapses repeated activation bodies, this one collapses repeated operator call sites (e.g. elementwise ADD / MUL or FULLY_CONNECTED) into a single shared runtime function per distinct kernel signature.

An operator opts in by returning descriptors from :meth:helia_aot.aot.operators.operator.AotOperator.shared_kernel_helpers. The operator then emits a compact per-node descriptor (rodata) plus a thin _run wrapper that forwards to the shared helper, instead of an inlined per-node call with a dozen marshalled arguments. The dominant per-op code (the CMSIS call site) is compiled exactly once.

Helpers are keyed by signature (key), so distinct operator variants map to distinct shared helpers and never collide. Adding a future variant (e.g. a second fully-connected kernel) simply introduces a new key and a new helper; existing kernels are untouched.

Classes

KernelHelper dataclass

KernelHelper(key: str, struct_kind: str, helper_name: str, ctype: str, cmsis: str)

Descriptor for one distinct shared operator-kernel signature.

A descriptor is the single source of truth tying together the Python operator, the shared runtime declaration/definition ({prefix}_kernels.h/.c) and the per-node _run wrapper. Because it is a frozen dataclass rather than a bare dict, field typos surface at construction time (and via the type checker) instead of as a Jinja render error or a C compile error in generated code. Instances are hashable, so distinct signatures de-duplicate cleanly by value.

Attributes:

  • key (str) –

    Signature identity used to de-duplicate helpers across operators.

  • struct_kind (str) –

    Selects the descriptor struct layout / dispatch branch in the kernel templates.

  • helper_name (str) –

    Bare name of the shared runtime function (prefixed at render time).

  • ctype (str) –

    C element type the kernel operates on (e.g. int8_t).

  • cmsis (str) –

    CMSIS-NN entry point the shared runtime forwards to.

Functions

elementwise_kernel_helper

elementwise_kernel_helper(family: str, dtype: dtype) -> KernelHelper | None

Return the shared kernel descriptor for an elementwise family.

Parameters:

  • family

    (str) –

    The elementwise family ("add" or "mul").

  • dtype

    (dtype) –

    The numpy dtype of the operator's output elements.

Returns:

  • A ( KernelHelper | None ) –

    class:KernelHelper, or None if family/dtype has no

  • KernelHelper | None

    shared kernel.

fully_connected_kernel_helper

fully_connected_kernel_helper() -> KernelHelper

Return the shared descriptor for the per-channel int8 fully-connected kernel.

Returns:

  • The ( KernelHelper ) –

    class:KernelHelper for arm_fully_connected_per_channel_s8.

collect_kernel_helpers

collect_kernel_helpers(operators: list) -> list[KernelHelper]

Collect the distinct shared kernel helpers required by operators.

Parameters:

  • operators

    (list) –

    The resolved :class:AotOperator instances for the model.

Returns:

  • list[KernelHelper]

    class:KernelHelper descriptors (one per distinct key) sorted by key,

  • list[KernelHelper]

    suitable for rendering the shared kernels runtime file. Empty when no

  • list[KernelHelper]

    operator uses a shared kernel helper.

Raises:

  • ValueError

    If two operators declare the same key with differing descriptors (a signature collision that would otherwise resolve silently to whichever operator was visited last).