Skip to content

Commit 65b88c5

Browse files
committed
fix divide by zero
1 parent a422ba3 commit 65b88c5

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

fs/ggml/ggml.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,10 @@ func (f GGML) GraphSize(context, batch uint64, kvCacheType string) (kv, partialO
579579
}
580580

581581
func (llm GGML) VisionGraphSize() (weights, graphSize uint64) {
582+
if llm.KV().Uint("vision.block_count") == 0 {
583+
return
584+
}
585+
582586
for name, layer := range llm.Tensors().GroupLayers() {
583587
if strings.HasPrefix(name, "v.") {
584588
for _, tensor := range layer {
@@ -589,30 +593,36 @@ func (llm GGML) VisionGraphSize() (weights, graphSize uint64) {
589593

590594
imageSize := uint64(llm.KV().Uint("vision.image_size"))
591595
patchSize := uint64(llm.KV().Uint("vision.patch_size"))
596+
if patchSize == 0 {
597+
slog.Warn("unknown patch size for vision model")
598+
return
599+
}
600+
601+
numChannels := uint64(llm.KV().Uint("vision.num_channels"))
592602

593603
numPatches := (imageSize / patchSize) * (imageSize / patchSize)
594604
if _, ok := llm.Tensors().GroupLayers()["v"]["class_embd"]; ok {
595605
numPatches++
596606
}
597607

598608
headCount := uint64(llm.KV().Uint("vision.attention.head_count"))
609+
embeddingLength := uint64(llm.KV().Uint("vision.embedding_length"))
599610

600611
switch llm.KV().Architecture() {
601612
case "mllama":
602-
603613
numPaddedPatches := numPatches + 8 - (numPatches%8)%8
604614

605615
maxNumTiles := uint64(llm.KV().Uint("vision.max_num_tiles"))
606-
numChannels := uint64(llm.KV().Uint("vision.num_channels"))
607-
embeddingLength := uint64(llm.KV().Uint("vision.embedding_length"))
608616

609617
graphSize = 4 * (8 +
610618
imageSize*imageSize*numChannels*maxNumTiles +
611619
embeddingLength*numPatches*maxNumTiles +
612620
9*embeddingLength*numPaddedPatches*maxNumTiles +
613621
numPaddedPatches*maxNumTiles*numPaddedPatches*maxNumTiles*headCount)
614622
case "gemma3":
615-
graphSize = 4 * (numPatches * numPatches * headCount)
623+
graphSize = 4 * (imageSize*imageSize*numChannels +
624+
embeddingLength*patchSize +
625+
numPatches*numPatches*headCount)
616626
}
617627

618628
return weights, graphSize

0 commit comments

Comments
 (0)