pub struct BertConfig {
pub hidden_dim: usize,
pub num_layers: usize,
pub num_heads: usize,
pub intermediate_dim: usize,
pub vocab_size: usize,
pub max_position_embeddings: usize,
pub type_vocab_size: usize,
pub layer_norm_eps: f32,
pub pad_token_id: u32,
}Expand description
BERT model configuration.
Maps to the relevant fields of HuggingFace BertConfig.
Fields§
Hidden dimension of each token (e.g. 768 for base, 384 for MiniLM-L-6).
num_layers: usizeNumber of encoder layers (e.g. 12 for base, 6 for MiniLM-L-6).
num_heads: usizeNumber of attention heads. hidden_dim must divide evenly by this.
intermediate_dim: usizeFFN intermediate dimension (typically 4 × hidden_dim).
vocab_size: usizeToken vocabulary size (e.g. 30522 for bert-base-uncased).
max_position_embeddings: usizeMaximum sequence length supported by position embeddings.
type_vocab_size: usizeToken-type vocabulary size (typically 2 for [A] / [B] segments).
layer_norm_eps: f32LayerNorm epsilon (HuggingFace default 1e-12).
pad_token_id: u32Pad token id (typically 0). Used for attention masking.
Implementations§
Source§impl BertConfig
impl BertConfig
Sourcepub const fn head_dim(&self) -> usize
pub const fn head_dim(&self) -> usize
Compute the per-head dimension. hidden_dim must be divisible by num_heads.
Sourcepub fn minilm_l6() -> BertConfig
pub fn minilm_l6() -> BertConfig
MiniLM-L-6 preset (22M params, 384 hidden, 12 heads, 6 layers).
Sourcepub fn validate(&self) -> Result<(), BertConfigError>
pub fn validate(&self) -> Result<(), BertConfigError>
Check the structural invariants that model construction asserts.
MultiHeadAttention::new (reached via BertEncoder::new /
CrossEncoder::new) asserts that hidden_dim is a multiple of
num_heads, and head_dim() divides by num_heads. Callers that
build a config from untrusted input (CLI overrides, config files) must
call this first and surface the error, instead of letting the assert
abort the process.
§Errors
Returns BertConfigError naming the offending field when
hidden_dim or num_heads is zero, or when hidden_dim is not
divisible by num_heads.
Sourcepub fn validate_ids(
&self,
input_ids: &[u32],
token_type_ids: &[u32],
) -> Result<(), BertConfigError>
pub fn validate_ids( &self, input_ids: &[u32], token_type_ids: &[u32], ) -> Result<(), BertConfigError>
Check a token-id batch against this config before a forward pass.
BertEmbeddings::forward indexes the word/position/token-type tables
with the raw ids and slices them unchecked, so an id at or above the
corresponding table size aborts the process with a slice-range panic.
This is the fallible pre-check for any caller feeding user-supplied
ids.
§Errors
Returns BertConfigError for a length mismatch, a sequence longer
than max_position_embeddings, an input id >= vocab_size, or a
token-type id >= type_vocab_size.
Trait Implementations§
Source§impl Clone for BertConfig
impl Clone for BertConfig
Source§fn clone(&self) -> BertConfig
fn clone(&self) -> BertConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BertConfig
impl Debug for BertConfig
Source§impl Default for BertConfig
impl Default for BertConfig
Source§fn default() -> BertConfig
fn default() -> BertConfig
bert-base-uncased defaults.
Source§impl PartialEq for BertConfig
impl PartialEq for BertConfig
impl StructuralPartialEq for BertConfig
Auto Trait Implementations§
impl Freeze for BertConfig
impl RefUnwindSafe for BertConfig
impl Send for BertConfig
impl Sync for BertConfig
impl Unpin for BertConfig
impl UnsafeUnpin for BertConfig
impl UnwindSafe for BertConfig
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more