// Provider structs must be stable #![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
use icu_provider::prelude::*; use potential_utf::PotentialUtf8; use zerovec::{ZeroMap, ZeroVec};
// We do this instead of const generics because ZeroFrom and Yokeable derives, as well as serde // don't support them
macro_rules! lstm_matrix {
($name:ident, $generic:literal) => { /// The struct that stores a LSTM's matrix. /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[derive(PartialEq, Debug, Clone, zerofrom::ZeroFrom, yoke::Yokeable)] #[cfg_attr(feature = "datagen", derive(serde::Serialize))] pubstruct $name<'data> { // Invariant: dims.product() == data.len() #[allow(missing_docs)] pub(crate) dims: [u16; $generic], #[allow(missing_docs)] pub(crate) data: ZeroVec<'data, f32>,
}
impl<'data> $name<'data> { #[cfg(any(feature = "serde", feature = "datagen"))] /// Creates a LstmMatrix with the given dimensions. Fails if the dimensions don't match the data. pubfn from_parts(
dims: [u16; $generic],
data: ZeroVec<'data, f32>,
) -> Result<Self, DataError> { if dims.iter().map(|&i| i as usize).product::<usize>() != data.len() {
Err(DataError::custom("Dimension mismatch"))
} else {
Ok(Self { dims, data })
}
}
#[derive(PartialEq, Debug, Clone, Copy)] #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] #[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] /// The type of LSTM model /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> pubenum ModelType { /// A model working on code points
Codepoints, /// A model working on grapheme clusters
GraphemeClusters,
}
/// The struct that stores a LSTM model. /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[derive(PartialEq, Debug, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] #[cfg_attr(feature = "datagen", derive(serde::Serialize))] #[yoke(prove_covariance_manually)] pubstruct LstmDataFloat32<'data> { /// Type of the model pub(crate) model: ModelType, /// The grapheme cluster dictionary used to train the model pub(crate) dic: ZeroMap<'data, PotentialUtf8, u16>, /// The embedding layer. Shape (dic.len + 1, e) pub(crate) embedding: LstmMatrix2<'data>, /// The forward layer's first matrix. Shape (h, 4, e) pub(crate) fw_w: LstmMatrix3<'data>, /// The forward layer's second matrix. Shape (h, 4, h) pub(crate) fw_u: LstmMatrix3<'data>, /// The forward layer's bias. Shape (h, 4) pub(crate) fw_b: LstmMatrix2<'data>, /// The backward layer's first matrix. Shape (h, 4, e) pub(crate) bw_w: LstmMatrix3<'data>, /// The backward layer's second matrix. Shape (h, 4, h) pub(crate) bw_u: LstmMatrix3<'data>, /// The backward layer's bias. Shape (h, 4) pub(crate) bw_b: LstmMatrix2<'data>, /// The output layer's weights. Shape (2, 4, h) pub(crate) time_w: LstmMatrix3<'data>, /// The output layer's bias. Shape (4) pub(crate) time_b: LstmMatrix1<'data>,
}
/// The data to power the LSTM segmentation model. /// /// This data enum is extensible: more backends may be added in the future. /// Old data can be used with newer code but not vice versa. /// /// Examples of possible future extensions: /// /// 1. Variant to store data in 16 instead of 32 bits /// 2. Minor changes to the LSTM model, such as different forward/backward matrix sizes /// /// <div class="stab unstable"> /// This code is considered unstable; it may change at any time, in breaking or non-breaking ways, /// including in SemVer minor releases. While the serde representation of data structs is guaranteed /// to be stable, their Rust representation might not be. Use with caution. /// </div> #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] #[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))] #[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] #[yoke(prove_covariance_manually)] #[non_exhaustive] pubenum LstmData<'data> { /// The data as matrices of zerovec f32 values.
Float32(#[cfg_attr(feature = "serde", serde(borrow))] LstmDataFloat32<'data>), // new variants should go BELOW existing ones // Serde serializes based on variant name and index in the enum // https://docs.rs/serde/latest/serde/trait.Serializer.html#tymethod.serialize_unit_variant
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.