* readme: add Ellama to list of community integrations (#9800) * readme: add screenpipe to community integrations (#9786) * Add support for ROCm gfx1151 (#9773) * conditionally enable parallel pipelines * sample: make mutations in transforms explicit (#9743) * updated minP to use early exit making use of sorted tokens * ml/backend/ggml: allocate memory with malloc when loading model (#9822) * runner: remove cache prompt flag from ollama runner (#9826) We do not need to bypass the prompt caching in the ollama runner yet, as only embedding models needed to bypass the prompt caching. When embedding models are implemented they can skip initializing this cache completely. * ollamarunner: Check for minBatch of context space when shifting Models can specify that a group of inputs need to be handled a single batch. However, context shifting didn't respect this and could trigger a break anyways. In this case, we should instead trigger a context shift earlier so that it occurs before the grouped batch. Note that there still some corner cases: - A long prompt that exceeds the context window can get truncated in the middle of an image. With the current models, this will result in the model not recognizing the image at all, which is pretty much the expected result with truncation. - The context window is set less than the minimum batch size. The only solution to this is to refuse to load the model with these settings. However, this can never occur with current models and default settings. Since users are unlikely to run into these scenarios, fixing them is left as a follow up. * Applied latest patches from McBane87 See this for details: https://github.com/whyvl/ollama-vulkan/issues/7#issuecomment-2708820861 Signed-off-by: Vadim Grinco <vadim@grinco.eu> * Add ability to enable flash attention on vulkan (#4) * discover: add flash attention handling for vulkan * envconfig: fix typo in config.go As part of the process some code was refactored and I added a new field FlashAttention to GpuInfo since the previous solution didn't allow for a granular check via vulkan extensions. As a side effect, this now allows for granular per-device FA support checking in other places --------- Signed-off-by: Vadim Grinco <vadim@grinco.eu> Co-authored-by: zeo <108888572+zeozeozeo@users.noreply.github.com> Co-authored-by: Louis Beaumont <louis.beaumont@gmail.com> Co-authored-by: Daniel Hiltgen <dhiltgen@users.noreply.github.com> Co-authored-by: Michael Yang <mxyng@pm.me> Co-authored-by: Parth Sareen <parth.sareen@ollama.com> Co-authored-by: Jeffrey Morgan <jmorganca@gmail.com> Co-authored-by: Bruce MacDonald <brucewmacdonald@gmail.com> Co-authored-by: Jesse Gross <jesse@ollama.com> Co-authored-by: Nikita <50599445+nasrally@users.noreply.github.com>
131 lines
2.6 KiB
Go
131 lines
2.6 KiB
Go
package sample
|
|
|
|
import (
|
|
"container/heap"
|
|
"math"
|
|
"slices"
|
|
)
|
|
|
|
// tokenHeap implements heap.Interface and holds tokens as a min-heap to track k largest elements
|
|
type tokenHeap []token
|
|
|
|
func (h tokenHeap) Len() int { return len(h) }
|
|
func (h tokenHeap) Less(i, j int) bool { return h[i].value < h[j].value }
|
|
func (h tokenHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
|
|
|
|
func (h *tokenHeap) Push(x any) {
|
|
*h = append(*h, x.(token))
|
|
}
|
|
|
|
func (h *tokenHeap) Pop() any {
|
|
old := *h
|
|
n := len(old)
|
|
x := old[n-1]
|
|
*h = old[0 : n-1]
|
|
return x
|
|
}
|
|
|
|
// temperature applies scaling to the logits
|
|
func temperature(ts []token, temp float32) {
|
|
// Ensure temperature clipping near 0 to avoid numerical instability
|
|
temp = max(temp, 1e-7)
|
|
for i := range ts {
|
|
ts[i].value = ts[i].value / temp
|
|
}
|
|
}
|
|
|
|
// softmax applies normalization to the logits
|
|
func softmax(ts []token) {
|
|
// Find max logit for numerical stability
|
|
maxLogit := float32(math.Inf(-1))
|
|
for _, t := range ts {
|
|
if t.value > maxLogit {
|
|
maxLogit = t.value
|
|
}
|
|
}
|
|
|
|
// Compute exp(x - max)
|
|
var sum float32
|
|
for i, v := range ts {
|
|
ts[i].value = float32(math.Exp(float64(v.value - maxLogit)))
|
|
sum += ts[i].value
|
|
}
|
|
|
|
// exp(x - max) / sum(exp(x - max))
|
|
for i := range ts {
|
|
ts[i].value /= sum
|
|
}
|
|
}
|
|
|
|
// topK limits the number of tokens considered to the k highest logits
|
|
func topK(ts []token, k int) []token {
|
|
if k >= len(ts) || k <= 0 {
|
|
slices.SortFunc(ts, func(a, b token) int {
|
|
switch {
|
|
case a.value < b.value:
|
|
return 1
|
|
case a.value > b.value:
|
|
return -1
|
|
default:
|
|
return 0
|
|
}
|
|
})
|
|
return ts
|
|
}
|
|
|
|
// Initialize min-heap with first k elements
|
|
h := make(tokenHeap, k)
|
|
copy(h, ts[:k])
|
|
heap.Init(&h)
|
|
|
|
// Process remaining elements
|
|
for i := k; i < len(ts); i++ {
|
|
if ts[i].value > h[0].value {
|
|
heap.Pop(&h)
|
|
heap.Push(&h, ts[i])
|
|
}
|
|
}
|
|
|
|
// Convert heap to sorted slice in descending order
|
|
result := make([]token, len(h))
|
|
for i := k - 1; i >= 0; i-- {
|
|
result[i] = heap.Pop(&h).(token)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
// topP limits tokens to those with cumulative probability p
|
|
// requires ts to be sorted in descending order of probabilities
|
|
func topP(ts []token, p float32) []token {
|
|
if p == 1.0 {
|
|
return ts
|
|
}
|
|
|
|
// Find cutoff index where cumulative sum exceeds p
|
|
var sum float32
|
|
for i, t := range ts {
|
|
sum += t.value
|
|
if sum > float32(p) {
|
|
return ts[:i+1]
|
|
}
|
|
}
|
|
|
|
return ts
|
|
}
|
|
|
|
// minP filters tokens with probabilities >= p * max_prob
|
|
// requires ts to be sorted in descending order of probabilities
|
|
func minP(ts []token, p float32) []token {
|
|
maxProb := ts[0].value
|
|
|
|
threshold := maxProb * p
|
|
|
|
for i, t := range ts {
|
|
if t.value < threshold {
|
|
return ts[:i]
|
|
}
|
|
}
|
|
return ts
|
|
}
|