circuits-llm
Safe HaskellNone
LanguageGHC2024

Circuit.LLM.Backprop

Description

Full backward pass through one transformer block. This is the core of the training backprop chain.

Synopsis

Full backward pass

bertBackward :: GptConfig -> Gpt -> [Int] -> [Int] -> [Bool] -> (Double, GptGrads) Source #

BERT-style masked-language-model backward pass.

  • inputIds are the (possibly masked) token IDs fed to the model.
  • targetIds are the original token IDs to reconstruct.
  • mask indicates which positions are supervised.

The model uses bidirectional attention, so every position can attend to every other position.

Gradient types

Primitives (exported for testing)

maskedCrossEntropyBwd :: Matrix Double -> [Int] -> [Bool] -> (Double, Matrix Double) Source #

Masked cross-entropy for BERT-style training. The mask indicates which positions are supervised; loss and gradients are averaged only over those positions.