transforms.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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) || k <= 0 {
  47. slices.SortFunc(ts, func(a, b token) int {
  48. switch {
  49. case a.value < b.value:
  50. return 1
  51. case a.value > b.value:
  52. return -1
  53. default:
  54. return 0
  55. }
  56. })
  57. return ts
  58. }
  59. // Initialize min-heap with first k elements
  60. h := make(tokenHeap, k)
  61. copy(h, ts[:k])
  62. heap.Init(&h)
  63. // Process remaining elements
  64. for i := k; i < len(ts); i++ {
  65. if ts[i].value > h[0].value {
  66. heap.Pop(&h)
  67. heap.Push(&h, ts[i])
  68. }
  69. }
  70. // Convert heap to sorted slice in descending order
  71. result := make([]token, len(h))
  72. for i := k - 1; i >= 0; i-- {
  73. result[i] = heap.Pop(&h).(token)
  74. }
  75. return result
  76. }
  77. // topP limits tokens to those with cumulative probability p
  78. func topP(ts []token, p float32) []token {
  79. if p == 1.0 {
  80. return ts
  81. }
  82. // Find cutoff index where cumulative sum exceeds p
  83. var sum float32
  84. for i, t := range ts {
  85. sum += t.value
  86. if sum > float32(p) {
  87. ts = ts[:i+1]
  88. return ts
  89. }
  90. }
  91. return ts
  92. }
  93. // minP limits tokens to those with cumulative probability p
  94. func minP(ts []token, p float32) []token {
  95. if p == 1.0 {
  96. return ts
  97. }
  98. maxProb := float32(math.Inf(-1))
  99. for _, token := range ts {
  100. if token.value > maxProb {
  101. maxProb = token.value
  102. }
  103. }
  104. threshold := maxProb * float32(p)
  105. // Filter tokens in-place
  106. validTokens := ts[:0]
  107. for i, token := range ts {
  108. if token.value >= threshold {
  109. validTokens = append(validTokens, ts[i])
  110. }
  111. }
  112. ts = validTokens
  113. return ts
  114. }