// Provider structs must be stable #![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
use icu_provider::prelude::*; use zerovec::{ule::UnvalidatedStr, 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),
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, UnvalidatedStr, 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>,
}
#[cfg(feature = "datagen")] impl databake::Bake for LstmDataFloat32<'_> { fn bake(&self, env: &databake::CrateEnv) -> databake::TokenStream { let model = self.model.bake(env); let dic = self.dic.bake(env); let embedding = self.embedding.bake(env); let fw_w = self.fw_w.bake(env); let fw_u = self.fw_u.bake(env); let fw_b = self.fw_b.bake(env); let bw_w = self.bw_w.bake(env); let bw_u = self.bw_u.bake(env); let bw_b = self.bw_b.bake(env); let time_w = self.time_w.bake(env); let time_b = self.time_b.bake(env);
databake::quote! {
icu_segmenter::provider::LstmDataFloat32::from_parts_unchecked( #model, #dic, #embedding, #fw_w, #fw_u, #fw_b, #bw_w, #bw_u, #bw_b, #time_w, #time_b,
)
}
}
}
/// 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> #[icu_provider::data_struct(LstmForWordLineAutoV1Marker = "segmenter/lstm/wl_auto@1")] #[derive(Debug, PartialEq, Clone)] #[cfg_attr(
feature = "datagen",
derive(serde::Serialize, databake::Bake),
databake(path = icu_segmenter::provider),
)] #[cfg_attr(feature = "serde", derive(serde::Deserialize))] #[yoke(prove_covariance_manually)] #[non_exhaustive] pubenum LstmDataV1<'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
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.14 Sekunden
(vorverarbeitet am 2026-06-23)
¤
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.