native_neural_network 0.3.1

Lib no_std Rust for native neural network (.rnn)
Documentation
pub(crate) const RNN0_MAGIC: &[u8; 4] = b"RNN\x00";
pub(crate) const RNN0_VERSION: u16 = 1;
pub(crate) const TLV_HEADER_NETWORK_SUMMARY: u8 = 0x05;
pub(crate) const TLV_HEADER_BENCHMARK: u8 = 0x06;
pub(crate) const TLV_HEADER_MODEL_NAME: u8 = 0x04;
pub(crate) const LAYER_META_SIZE: usize = 20;

pub(crate) const BLOB_LAYER_META: &str = "layer_meta";
pub(crate) const BLOB_WEIGHTS: &str = "weights";
pub(crate) const BLOB_BIASES: &str = "biases";
#[cfg_attr(not(feature = "publisher-trust-service"), allow(dead_code))]
pub(crate) const MODEL_NAME_BLOB_DATA: &str = "model.name";
pub(crate) const MODEL_PRECISION_BLOB_DATA: &str = "model.precision";
pub(crate) const DEFAULT_MODEL_NAME: &str = "sample";

pub(crate) const AUTH_HMAC_SHA256_BLOB_DATA: &str = "auth.hmac_sha256";
pub(crate) const AUTH_ED25519_SIG_BLOB_DATA: &str = "auth.ed25519_sig";
pub(crate) const AUTH_ED25519_PUBKEY_BLOB_DATA: &str = "auth.ed25519_pubkey";
pub(crate) const AUTH_DISTRIBUTION_POLICY_BLOB_DATA: &str = "auth.distribution_policy";

pub(crate) fn encoded_payload_size(
    dtype: u8,
    layer_count: usize,
    weights_len: usize,
    biases_len: usize,
) -> Option<usize> {
    let elem_size = match dtype {
        0 => 4usize,
        1 => 8usize,
        _ => return None,
    };

    let layers_bytes = layer_count.checked_mul(LAYER_META_SIZE)?;
    let weights_bytes = weights_len.checked_mul(elem_size)?;
    let biases_bytes = biases_len.checked_mul(elem_size)?;

    20usize
        .checked_add(layers_bytes)?
        .checked_add(weights_bytes)?
        .checked_add(biases_bytes)
}
use crate::layers::LayerSpec;

pub(crate) struct Buffers<'a> {
    pub(crate) layer_specs_scratch: &'a mut [LayerSpec],
    pub(crate) rmd1_scratch: &'a mut [u8],
    pub(crate) metadata_scratch: &'a mut [u8],
    pub(crate) out_bytes: &'a mut [u8],
}

impl<'a> From<crate::rnn_api::PackBuffers<'a>> for Buffers<'a> {
    fn from(p: crate::rnn_api::PackBuffers<'a>) -> Self {
        Buffers {
            layer_specs_scratch: p.layer_specs_scratch,
            rmd1_scratch: p.rmd1_scratch,
            metadata_scratch: p.metadata_scratch,
            out_bytes: p.out_bytes,
        }
    }
}

pub(crate) const DISTRIBUTION_POLICY_PUBLISHER_SHARED: u8 = 1;
pub(crate) const DISTRIBUTION_POLICY_USER_DEVICE_LOCKED: u8 = 2;