transforms.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package sample
  2. import (
  3. "container/heap"
  4. "math"
  5. "slices"
  6. )
  7. // tokenHeap implements heap.Interface and holds tokens as a min-heap to track k largest elements
  8. type tokenHeap []token
  9. func (h tokenHeap) Len() int { return len(h) }
  10. func (h tokenHeap) Less(i, j int) bool { return h[i].value < h[j].value }
  11. func (h tokenHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  12. func (h *tokenHeap) Push(x any) {
  13. *h = append(*h, x.(token))
  14. }
  15. func (h *tokenHeap) Pop() any {
  16. old := *h
  17. n := len(old)
  18. x := old[n-1]
  19. *h = old[0 : n-1]
  20. return x
  21. }
  22. // temperature applies scaling to the logits
  23. func temperature(ts []token, temp float32) {
  24. // Ensure temperature clipping near 0 to avoid numerical instability
  25. temp = max(temp, 1e-7)
  26. for i := range ts {
  27. ts[i].value = ts[i].value / temp
  28. }
  29. }
  30. // softmax applies normalization to the logits
  31. func softmax(ts []token) {
  32. // Find max logit for numerical stability
  33. maxLogit := float32(math.Inf(-1))
  34. for _, t := range ts {
  35. if t.value > maxLogit {
  36. maxLogit = t.value
  37. }
  38. }
  39. // Compute exp(x - max)
  40. var sum float32
  41. for i, v := range ts {
  42. ts[i].value = float32(math.Exp(float64(v.value - maxLogit)))
  43. sum += ts[i].value
  44. }
  45. // exp(x - max) / sum(exp(x - max))
  46. for i := range ts {
  47. ts[i].value /= sum
  48. }
  49. }
  50. // topK limits the number of tokens considered to the k highest logits
  51. func topK(ts []token, k int) []token {
  52. if k >= len(ts) || k <= 0 {
  53. slices.SortFunc(ts, func(a, b token) int {
  54. switch {
  55. case a.value < b.value:
  56. return 1
  57. case a.value > b.value:
  58. return -1
  59. default:
  60. return 0
  61. }
  62. })
  63. return ts
  64. }
  65. // Initialize min-heap with first k elements
  66. h := make(tokenHeap, k)
  67. copy(h, ts[:k])
  68. heap.Init(&h)
  69. // Process remaining elements
  70. for i := k; i < len(ts); i++ {
  71. if ts[i].value > h[0].value {
  72. heap.Pop(&h)
  73. heap.Push(&h, ts[i])
  74. }
  75. }
  76. // Convert heap to sorted slice in descending order
  77. result := make([]token, len(h))
  78. for i := k - 1; i >= 0; i-- {
  79. result[i] = heap.Pop(&h).(token)
  80. }
  81. return result
  82. }
  83. // topP limits tokens to those with cumulative probability p
  84. // requires ts to be sorted in descending order of probabilities
  85. func topP(ts []token, p float32) []token {
  86. if p == 1.0 {
  87. return ts
  88. }
  89. // Find cutoff index where cumulative sum exceeds p
  90. var sum float32
  91. for i, t := range ts {
  92. sum += t.value
  93. if sum > float32(p) {
  94. return ts[:i+1]
  95. }
  96. }
  97. return ts
  98. }
  99. // minP filters tokens with probabilities >= p * max_prob
  100. // requires ts to be sorted in descending order of probabilities
  101. func minP(ts []token, p float32) []token {
  102. maxProb := ts[0].value
  103. threshold := maxProb * p
  104. for i, t := range ts {
  105. if t.value < threshold {
  106. return ts[:i]
  107. }
  108. }
  109. return ts
  110. }