Expand description
ModernBERT encoder and Laya decision heads, in candle.
Architecture follows upstream Laya and Hugging Face ModernBERT. Four details are easy to get wrong and are called out where they occur:
- The encoder and scoring head use exact (erf) GELU, so
gelu_erf— candle’sgeluis the tanh approximation. - The decision-head feed-forward uses ReLU, because upstream builds it
from
torch.nn.TransformerEncoderLayer, whose default activation is ReLU even though everything around it is GELU. - Padded queries are allowed to attend to valid keys. Without that a fully-masked softmax row produces NaN. Padded positions are never used as keys or pooled outputs, so valid-token results are unchanged.
- RoPE tables are built in f32 and cast afterwards; building them directly in f16 loses precision at long positions.
Structs§
- Attention
Masks - Additive attention masks:
0where attention is allowed,-infelsewhere. - Decision
Model - The full Laya decision model: encoder, decision head, scorer and action head.
- Model
Output - One forward pass: masked scoring logits and action probabilities’ logits.
- Modern
Bert - Rotary
- Precomputed RoPE tables for one theta.