Skip to main content

Module bert

Module bert 

Source
Expand description

BERT Model V2 - Clean implementation using solid abstractions

BERT is an encoder-only model with key differences from decoder-only models:

  • Returns embeddings (ModelOutputs::Embeddings) instead of logits
  • Bidirectional attention (NO causal masking)
  • Uses position embeddings (learned) instead of RoPE
  • Uses token type embeddings for segment distinction
  • Uses LayerNorm (not RMSNorm)
  • Has a pooler for [CLS] token representation

Structsยง

BertAttention
BERT attention: self-attention + output projection with residual
BertConfig
BertEmbeddings
BERT embeddings: word + position + token_type embeddings, then LayerNorm
BertEncoder
BERT encoder: stack of BertLayer
BertIntermediate
BERT intermediate (first FFN layer with activation)
BertLayer
BERT transformer layer
BertModelV2
Main BERT model implementation
BertOutput
BERT output (second FFN layer with residual and LayerNorm)
BertPooler
BERT pooler: takes [CLS] token and projects through dense + tanh
BertSelfAttention
BERT self-attention (bidirectional, no causal mask)
BertSelfOutput
BERT self-attention output projection