bunsen 0.38.0

bunsen is a batteries included common library for burn
Documentation
//! Load pretrained models.
use std::path::Path;

use burn::{
    store::{
        BurnpackStore,
        KeyRemapper,
        ModuleSnapshot,
        burn_pack::Error as BurnpackError,
    },
    tensor::Device,
};

use crate::{
    burner::module::ModuleInit,
    errors::{
        BunsenError,
        BunsenErrorKind,
        BunsenResult,
        ResultContext,
        WithOkOrPanic,
        sys_at,
    },
    kits::speech::silero_vad::{
        SileroVad,
        SileroVadCollection,
        SileroVadSignalConfig,
    },
};

#[cfg(feature = "silero-weights")]
mod with_weights {
    use bunsen_bundled_silero::burnpack_as_burn_bytes;

    use super::*;

    impl SileroVad {
        /// Load the pretrained 16khz model.
        pub fn load_16khz_pretrained(device: &Device) -> BunsenResult<Self> {
            Self::load_16khz_from_burnpack_bytes(burnpack_as_burn_bytes(), device)
        }

        /// Load the pretrained 8khz model.
        pub fn load_8khz_pretrained(device: &Device) -> BunsenResult<Self> {
            Self::load_8khz_from_burnpack_bytes(burnpack_as_burn_bytes(), device)
        }
    }

    impl SileroVadCollection {
        /// Load the standard 16khz/8khz pretrained models.
        pub fn load_pretrained(device: &Device) -> BunsenResult<Self> {
            Ok(Self::new_common_collection(
                SileroVad::load_16khz_pretrained(device)?,
                SileroVad::load_8khz_pretrained(device)?,
            ))
        }
    }
}

impl SileroVad {
    /// Load the 16khz model from pretrained burnpack bytes.
    /// Uses the upstream `silero_vad` keying.
    pub fn load_16khz_from_burnpack_bytes(
        bytes: burn::tensor::Bytes,
        device: &Device,
    ) -> BunsenResult<Self> {
        Self::load_from_burnpack(
            BurnpackStore::from_bytes(Some(bytes)),
            SileroVadSignalConfig::standard_16khz(),
            Self::pretrained_16khz_remapper(),
            device,
        )
    }

    /// Load the 16khz model from a burnpack file.
    /// Uses the upstream `silero_vad` keying.
    ///
    /// # Errors
    /// [`Lookup`](crate::errors::BunsenErrorKind::Lookup) when `path` is
    /// missing or forbidden; otherwise as
    /// [`load_from_burnpack`](Self::load_from_burnpack), under a frame
    /// naming the file.
    pub fn load_16khz_from_burnpack_file(
        path: impl AsRef<Path>,
        device: &Device,
    ) -> BunsenResult<Self> {
        Self::load_from_burnpack_path(
            path,
            SileroVadSignalConfig::standard_16khz(),
            Self::pretrained_16khz_remapper(),
            device,
        )
    }

    /// Load the 8khz model from pretrained burnpack bytes.
    /// Uses the upstream `silero_vad` keying.
    pub fn load_8khz_from_burnpack_bytes(
        bytes: burn::tensor::Bytes,
        device: &Device,
    ) -> BunsenResult<Self> {
        Self::load_from_burnpack(
            BurnpackStore::from_bytes(Some(bytes)),
            SileroVadSignalConfig::standard_8khz(),
            Self::pretrained_8khz_remapper(),
            device,
        )
    }

    /// Load the 8khz model from a burnpack file.
    /// Uses the upstream `silero_vad` keying.
    ///
    /// # Errors
    /// [`Lookup`](crate::errors::BunsenErrorKind::Lookup) when `path` is
    /// missing or forbidden; otherwise as
    /// [`load_from_burnpack`](Self::load_from_burnpack), under a frame
    /// naming the file.
    pub fn load_8khz_from_burnpack_file(
        path: impl AsRef<Path>,
        device: &Device,
    ) -> BunsenResult<Self> {
        Self::load_from_burnpack_path(
            path,
            SileroVadSignalConfig::standard_8khz(),
            Self::pretrained_8khz_remapper(),
            device,
        )
    }

    /// The key remapping for the 16khz model.
    pub fn pretrained_16khz_remapper() -> KeyRemapper {
        KeyRemapper::from_patterns(vec![
            ("conv1d37", "stft"),
            ("conv1d38", "encoder.blocks.0.conv"),
            ("conv1d39", "encoder.blocks.1.conv"),
            ("conv1d40", "encoder.blocks.2.conv"),
            ("conv1d41", "encoder.blocks.3.conv"),
            ("linear13", "lstm.hidden"),
            ("linear14", "lstm.features"),
            ("conv1d42", "decoder"),
        ])
        .ok_or_panic()
    }

    /// The key remapping for the 8khz model.
    pub fn pretrained_8khz_remapper() -> KeyRemapper {
        KeyRemapper::from_patterns(vec![
            ("conv1d43", "stft"),
            ("conv1d44", "encoder.blocks.0.conv"),
            ("conv1d45", "encoder.blocks.1.conv"),
            ("conv1d46", "encoder.blocks.2.conv"),
            ("conv1d47", "encoder.blocks.3.conv"),
            ("linear15", "lstm.hidden"),
            ("linear16", "lstm.features"),
            ("conv1d48", "decoder"),
        ])
        .ok_or_panic()
    }

    /// Load from a burnpack file, under a frame naming it.
    fn load_from_burnpack_path<C>(
        path: impl AsRef<Path>,
        cfg: C,
        remapper: KeyRemapper,
        device: &Device,
    ) -> BunsenResult<Self>
    where
        C: ModuleInit<Self>,
    {
        let path = path.as_ref();
        std::fs::metadata(path).map_err(sys_at("open", path))?;
        Self::load_from_burnpack(BurnpackStore::from_file(path), cfg, remapper, device)
            .with_context(|| format!("loading {}", path.display()))
    }

    /// Load from a burnpack store.
    ///
    /// # Errors
    /// - as `cfg`'s [`try_init`](ModuleInit::try_init);
    /// - [`InvalidResource`](crate::errors::BunsenErrorKind::InvalidResource),
    ///   with the [`BurnpackError`] as its cause, when the store is not a
    ///   burnpack or lacks a tensor the model needs;
    ///   [`Unsupported`](crate::errors::BunsenErrorKind::Unsupported) for a
    ///   burnpack version this build does not read;
    ///   [`Sys`](crate::errors::BunsenErrorKind::Sys) when reading it fails.
    pub fn load_from_burnpack<C>(
        store: BurnpackStore,
        cfg: C,
        remapper: KeyRemapper,
        device: &Device,
    ) -> BunsenResult<Self>
    where
        C: ModuleInit<Self>,
    {
        let mut store = store.remap(remapper);
        let mut module = cfg.try_init(device)?;
        module.load_from(&mut store).map_err(burnpack_load_error)?;

        Ok(module)
    }
}

/// Sorts a [`BurnpackError`] from loading a module by what it means.
fn burnpack_load_error(error: BurnpackError) -> BunsenError {
    let kind = match &error {
        BurnpackError::IoError(_) => BunsenErrorKind::Sys,
        BurnpackError::InvalidVersion => BunsenErrorKind::Unsupported,
        _ => BunsenErrorKind::InvalidResource,
    };
    BunsenError::from_cause(kind, error)
}

impl SileroVadCollection {
    pub(super) fn new_common_collection(
        vad_16: SileroVad,
        vad_8: SileroVad,
    ) -> Self {
        Self {
            branches: vec![(16000, vad_16), (8000, vad_8)],
        }
    }

    /// Load the standard 16khz/8khz pretrained models from burnpack bytes.
    /// Uses the upstream `silero_vad` keying.
    pub fn load_from_burnpack_bytes(
        bytes: burn::tensor::Bytes,
        device: &Device,
    ) -> BunsenResult<Self> {
        Ok(Self::new_common_collection(
            SileroVad::load_16khz_from_burnpack_bytes(bytes.clone(), device)?,
            SileroVad::load_8khz_from_burnpack_bytes(bytes, device)?,
        ))
    }

    /// Load the standard 16khz/8khz pretrained models from a burnpack file.
    /// Uses the upstream `silero_vad` keying.
    pub fn load_from_burnpack_file<P: AsRef<Path>>(
        path: P,
        device: &Device,
    ) -> BunsenResult<Self> {
        let path = path.as_ref();
        Ok(Self::new_common_collection(
            SileroVad::load_16khz_from_burnpack_file(path, device)?,
            SileroVad::load_8khz_from_burnpack_file(path, device)?,
        ))
    }
}