Skip to content

utils

AOT Handler Utilities API

The utils module provides utility functions for AOT handlers.

Copyright 2025 Ambiq. All Rights Reserved.

Classes

Functions

generate_mermaid

generate_mermaid(model: AirModel, direction: str = 'TB') -> str

Generate a Mermaid flowchart showing operator connectivity.

Parameters:

  • model

    (AirModel) –

    The model to generate the flowchart for.

  • direction

    (str, default: 'TB' ) –

    Mermaid graph direction (“TB”, “LR”, etc.)

Returns:

  • str ( str ) –

    Mermaid code as a string.

sanitize_for_c_comment

sanitize_for_c_comment(text: str | bytes | bytearray) -> str

Sanitize a string (or bytes) so it can safely live inside a C block comment (/ ... /).

Transforms: - If bytes/bytearray, decode as UTF-8 (replacing errors). - Replaces '/' with '/ ' and '/' with ' /' - Strips out non-printable/control chars except tab/newline/carriage-return.

Parameters:

Returns:

  • str ( str ) –

    Sanitized text.

derive_stimulus_seed

derive_stimulus_seed(tensor_id: str, salt: str = '') -> int

Derive the stimulus seed for a tensor.

The seed is a pure function of salt and tensor_id so stimulus is reproducible across processes and independent of scheduling order. Only hashlib is involved, so the derivation is stable across interpreter and NumPy versions.

tensor_id alone is not unique across models — the LiteRT parser numbers tensors by their position within a subgraph, so tensor "0" exists in every model. salt supplies the case-level component (the model name) that keeps otherwise-identical tensors from sharing a stream.

Parameters:

  • tensor_id

    (str) –

    Identifier of the tensor within its model.

  • salt

    (str, default: '' ) –

    Case-level qualifier, typically the model name. Defaults to an empty string.

Returns:

  • int ( int ) –

    64-bit seed for the tensor's stimulus generator.

generate_stimulus_data

generate_stimulus_data(tensor: AirTensor, salt: str = '') -> np.ndarray

Generate deterministic pseudo-random stimulus data for a tensor.

Parameters:

  • tensor

    (AirTensor) –

    Target tensor.

  • salt

    (str, default: '' ) –

    Case-level qualifier mixed into the seed, typically the model name. Tensor ids are only unique within a model, so without a salt two models sharing a tensor index, dtype and shape receive identical stimulus. Defaults to an empty string.

Returns:

  • ndarray

    np.ndarray: Generated stimulus data, seeded from salt and the tensor identifier.