Class LlamaModel
java.lang.Object
smile.deep.layer.LayerBlock
smile.llm.model.llama.LlamaModel
Llama dense decoder: token embeddings, stacked
LlamaBlocks,
final RMSNorm, and the output projection.
Construct on CPU, load weights, then call to(Device) to place
parameters and the RoPE frequency table. Install the KV pool afterward
via setKvCachePool(KvCachePool).
-
Field Summary
Fields inherited from class LayerBlock
device, dtype, module -
Constructor Summary
ConstructorsConstructorDescriptionLlamaModel(int dim, int numLayers, int numHeads, int numKvHeads, int vocabSize, Integer intermediateSize, int multipleOf, Double ffnDimMultiplier, double normEps, double ropeTheta, boolean scaledRope, int maxSeqLen) Constructs the module graph on CPU with a host-side RoPE table. -
Method Summary
Modifier and TypeMethodDescriptionForward propagation (or forward pass) through the layer.Forward pass through the model.Decode forward with per-row absolute positions (seqLenmust be 1).Returns the shared KV cache pool.layers()Returns the decoder blocks.intReturns the number of transformer blocks.voidsetKvCachePool(KvCachePool pool) Replaces the shared KV cache pool on every attention layer.voidsetKvCachePool(KvCachePool pool, boolean closePrevious) Replaces the shared KV cache pool on every attention layer.Moves parameters and the RoPE frequency table todevice.to(Device device, ScalarType dtype) Moves parameters and the RoPE frequency table todevice/dtype.Methods inherited from class LayerBlock
add, add, device, dtype, eval, isTraining, load, loadStateDict, module, save, setRequiresGrad, toString, train
-
Constructor Details
-
LlamaModel
public LlamaModel(int dim, int numLayers, int numHeads, int numKvHeads, int vocabSize, Integer intermediateSize, int multipleOf, Double ffnDimMultiplier, double normEps, double ropeTheta, boolean scaledRope, int maxSeqLen) Constructs the module graph on CPU with a host-side RoPE table. Callto(Device)after weight load; thensetKvCachePool(KvCachePool).- Parameters:
dim- token embedding dimension.numLayers- number of transformer blocks.numHeads- number of query heads.numKvHeads- number of key/value heads.vocabSize- vocabulary size.intermediateSize- explicit FFN hidden size, ornull.multipleOf- FFN rounding multiple when deriving size.ffnDimMultiplier- optional FFN dim multiplier.normEps- RMSNorm epsilon.ropeTheta- RoPE theta.scaledRope- whether to use Llama-3 scaled RoPE.maxSeqLen- maximum sequence length (RoPE table uses2 * maxSeqLen).
-
-
Method Details
-
to
Moves parameters and the RoPE frequency table todevice.- Specified by:
toin interfaceLayer- Overrides:
toin classLayerBlock- Parameters:
device- the compute device.- Returns:
- this layer.
-
to
Moves parameters and the RoPE frequency table todevice/dtype. RoPEcisstays complex64 (device move only); casting it to a real dtype would drop the imaginary part and break rotary embeddings.- Specified by:
toin interfaceLayer- Overrides:
toin classLayerBlock- Parameters:
device- the compute device.dtype- the data type.- Returns:
- this layer.
-
kvCachePool
-
numLayers
public int numLayers()Returns the number of transformer blocks.- Returns:
- number of transformer blocks.
-
layers
Returns the decoder blocks.- Returns:
- decoder blocks (mutable list owned by this model).
-
setKvCachePool
Replaces the shared KV cache pool on every attention layer. Closes the previous pool. Intended to be called once after weight loading so the pool can be sized from residual device memory.- Parameters:
pool- the new pool (must not benull).
-
setKvCachePool
Replaces the shared KV cache pool on every attention layer.- Parameters:
pool- the new pool (must not benull).closePrevious- whentrue, closes the previously installed pool.
-
forward
-
forward
-
forward
-