transforms.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 and softmax to the logits
  23. func temperature(ts []token, temp float32) []token {
  24. // Find max logit for numerical stability
  25. maxLogit := float32(math.Inf(-1))
  26. for _, t := range ts {
  27. if t.value > maxLogit {
  28. maxLogit = t.value
  29. }
  30. }
  31. // Apply temperature and compute exp(x - max)
  32. temp = max(temp, 1e-7)
  33. var sum float32
  34. for i, v := range ts {
  35. ts[i].value = float32(math.Exp(float64((v.value - maxLogit) / temp)))
  36. sum += ts[i].value
  37. }
  38. // Normalize
  39. for i := range ts {
  40. ts[i].value /= sum
  41. }
  42. return ts
  43. }
  44. // topK limits the number of tokens considered to the k highest logits
  45. func topK(ts []token, k int) []token {
  46. if k >= len(ts) {
  47. sortLogits(ts)
  48. return ts
  49. }
  50. // Initialize min-heap with first k elements
  51. h := make(tokenHeap, k)
  52. copy(h, ts[:k])
  53. heap.Init(&h)
  54. // Process remaining elements
  55. for i := k; i < len(ts); i++ {
  56. if ts[i].value > h[0].value {
  57. heap.Pop(&h)
  58. heap.Push(&h, ts[i])
  59. }
  60. }
  61. // Convert heap to sorted slice in descending order
  62. result := make([]token, len(h))
  63. for i := k - 1; i >= 0; i-- {
  64. result[i] = heap.Pop(&h).(token)
  65. }
  66. return result
  67. }
  68. // topP limits tokens to those with cumulative probability p
  69. func topP(ts []token, p float32) []token {
  70. if p == 1.0 {
  71. return ts
  72. }
  73. // Find cutoff index where cumulative sum exceeds p
  74. var sum float32
  75. for i, t := range ts {
  76. sum += t.value
  77. if sum > float32(p) {
  78. ts = ts[:i+1]
  79. return ts
  80. }
  81. }
  82. return ts
  83. }
  84. // minP limits tokens to those with cumulative probability p
  85. func minP(ts []token, p float32) []token {
  86. if p == 1.0 {
  87. return ts
  88. }
  89. maxProb := float32(math.Inf(-1))
  90. for _, token := range ts {
  91. if token.value > maxProb {
  92. maxProb = token.value
  93. }
  94. }
  95. threshold := maxProb * float32(p)
  96. // Filter tokens in-place
  97. validTokens := ts[:0]
  98. for i, token := range ts {
  99. if token.value >= threshold {
  100. validTokens = append(validTokens, ts[i])
  101. }
  102. }
  103. ts = validTokens
  104. return ts
  105. }
  106. // sortLogits sorts the tokens in descending order of logits
  107. func sortLogits(ts []token) {
  108. slices.SortFunc(ts, func(a, b token) int {
  109. switch {
  110. case a.value < b.value:
  111. return 1
  112. case a.value > b.value:
  113. return -1
  114. default:
  115. return 0
  116. }
  117. })
  118. }