mirror of
https://github.com/ollama/ollama.git
synced 2026-12-04 08:32:11 +00:00
* inkling2: faster Metal decode, prefill and draft rounds Add Inkling-only Metal kernels, each with a stock-graph fallback, gated by GPU generation and size class: short BF16 products, deduplicated NVFP4/MXFP8 expert gathers with fused SwiGLU, fused loop controllers and residual mixing, relative-position split attention, fused short convolutions, faster global prefill, an ordered window cache that appends in place, and an 8-bit draft with a 4-bit vocabulary head (proposals only; outputs stay exact).
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package cache
|
|
|
|
import "math"
|
|
|
|
// EstimateMemory budgets one request's live cache, copies and checkpoints using
|
|
// tensor shapes from a fresh cache's first forward. It does not evaluate arrays
|
|
// or allocate storage.
|
|
// Unknown or uninitialized caches cannot safely increase automatic parallelism.
|
|
func EstimateMemory(c Cache, tokens, checkpoints int) (int64, bool) {
|
|
if c == nil {
|
|
return 0, true // Some layers share another layer's cache.
|
|
}
|
|
if tokens <= 0 || checkpoints < 0 {
|
|
return 0, false
|
|
}
|
|
var bytes int64
|
|
for _, a := range c.State() {
|
|
if a != nil {
|
|
bytes += int64(a.NumBytes())
|
|
}
|
|
}
|
|
if bytes <= 0 || c.Offset() <= 0 {
|
|
return 0, false
|
|
}
|
|
var count int64
|
|
switch c := c.(type) {
|
|
case *KVCache:
|
|
bytes /= int64(c.Offset())
|
|
// Full-attention snapshots share the live prefix until it is copied.
|
|
count = 2 * ((int64(tokens) + int64(c.step) - 1) / int64(c.step) * int64(c.step))
|
|
case *RotatingKVCache:
|
|
bytes /= int64(min(c.Offset(), c.maxSize))
|
|
count = int64(min(tokens, c.maxSize)) * (2 + int64(checkpoints))
|
|
if c.ordered {
|
|
// The live buffer and its compaction copy each keep spare rows.
|
|
count += 2 * int64(c.step)
|
|
}
|
|
case *RecurrentCache:
|
|
// Bounded state is retained at each prompt checkpoint, plus live state
|
|
// and a copy when the next request resumes or rewinds it.
|
|
count = 2 + int64(checkpoints)
|
|
default:
|
|
return 0, false
|
|
}
|
|
if count <= 0 || bytes > math.MaxInt64/count {
|
|
return 0, false
|
|
}
|
|
return bytes * count, true
|
|
}
|