transforms_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. package sample
  2. import (
  3. "math"
  4. "math/rand/v2"
  5. "testing"
  6. )
  7. // Helper to convert float32 slice to logit slice
  8. func toTokens(values []float32) []token {
  9. tokens := make([]token, len(values))
  10. for i, v := range values {
  11. tokens[i] = token{
  12. id: int32(i),
  13. value: v,
  14. }
  15. }
  16. return tokens
  17. }
  18. // Helper to compare logit slices
  19. func compareLogits(t *testing.T, name string, want []float32, got []token) {
  20. t.Helper()
  21. if len(want) != len(got) {
  22. t.Errorf("%s: length mismatch: want %d, got %d", name, len(want), len(got))
  23. return
  24. }
  25. for i := range want {
  26. if math.Abs(float64(got[i].value-want[i])) > 1e-6 {
  27. t.Errorf("%s: index %d: want %f, got %f", name, i, want[i], got[i].value)
  28. }
  29. }
  30. }
  31. func TestTemperature(t *testing.T) {
  32. input := []float32{1.0, 4.0, -2.0, 0.0}
  33. got := temperature(toTokens(input), 0.5)
  34. want := []float32{2.0, 8.0, -4.0, 0.0}
  35. compareLogits(t, "temperature(0.5)", want, got)
  36. got = temperature(toTokens(input), 1.0)
  37. want = []float32{1.0, 4.0, -2.0, 0.0}
  38. compareLogits(t, "temperature(1)", want, got)
  39. got = temperature(toTokens(input), 0.0)
  40. want = []float32{1e7, 4e7, -2e7, 0.0}
  41. compareLogits(t, "temperature(0)", want, got)
  42. }
  43. func TestSoftmax(t *testing.T) {
  44. tests := []struct {
  45. name string
  46. input []float32
  47. expected []float32
  48. }{
  49. {
  50. name: "correctness softmax",
  51. input: []float32{1, -2, 3, 0},
  52. expected: []float32{0.113550, 0.005653, 0.839024, 0.041773},
  53. },
  54. {
  55. name: "normal distribution",
  56. input: []float32{0.026986899, 0.043722924, 0.036774673, 0.27755088, 0.0046718004, 0.08582123, 0.20409796, 0.00412893, 0.15720603, 0.045046154, 0.0030491839, 0.01681367},
  57. },
  58. {
  59. name: "single value",
  60. input: []float32{1.0},
  61. },
  62. {
  63. name: "identical values",
  64. input: []float32{0.9, 0.9, 0.9},
  65. },
  66. {
  67. name: "large values",
  68. input: []float32{1000.0, 2000.0, 3000.0},
  69. },
  70. {
  71. name: "small values",
  72. input: []float32{1e-6, 2e-6, 3e-6},
  73. },
  74. {
  75. name: "negative values",
  76. input: []float32{-1.0, -2.0, -3.0},
  77. },
  78. {
  79. name: "mixed values",
  80. input: []float32{-100.0, 0.0, 100.0},
  81. },
  82. }
  83. for _, tt := range tests {
  84. t.Run(tt.name, func(t *testing.T) {
  85. got := softmax(toTokens(tt.input))
  86. if tt.expected != nil {
  87. compareLogits(t, tt.name, tt.expected, got)
  88. return
  89. }
  90. // Check probabilities sum to 1
  91. var sum float32
  92. for _, token := range got {
  93. sum += token.value
  94. if token.value < 0 || token.value > 1 {
  95. t.Errorf("probability out of range [0,1]: got %f", token.value)
  96. }
  97. }
  98. if math.Abs(float64(sum-1.0)) > 1e-6 {
  99. t.Errorf("probabilities don't sum to 1: got %f", sum)
  100. }
  101. })
  102. }
  103. }
  104. func TestTopK(t *testing.T) {
  105. input := []float32{0.026986899, 0.043722924, 0.036774673, 0.27755088, 0.0046718004, 0.08582123, 0.20409796, 0.00412893, 0.15720603, 0.045046154, 0.0030491839, 0.01681367}
  106. // Test k=5
  107. got := topK(toTokens(input), 5)
  108. if len(got) != 5 {
  109. t.Errorf("topK(5): wrong length: want 5, got %d", len(got))
  110. }
  111. // Should keep highest 3 values in descending order
  112. want := []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154}
  113. compareLogits(t, "topK(3)", want, got)
  114. got = topK(toTokens(input), 20)
  115. if len(got) != len(input) {
  116. t.Errorf("topK(20): wrong length: want %d, got %d", len(input), len(got))
  117. }
  118. // Test k=-1
  119. input = []float32{0.026986899, 0.043722924, 0.036774673, 0.27755088, 0.0046718004, 0.08582123, 0.20409796, 0.00412893, 0.15720603, 0.045046154, 0.0030491839, 0.01681367}
  120. want = []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154, 0.043722924, 0.036774673, 0.026986899, 0.01681367, 0.0046718004, 0.00412893, 0.0030491839}
  121. got = topK(toTokens(input), -1)
  122. if len(got) != len(input) {
  123. t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(got))
  124. }
  125. compareLogits(t, "topK(-1)", want, got)
  126. // Test k=0
  127. input = []float32{0.026986899, 0.043722924, 0.036774673, 0.27755088, 0.0046718004, 0.08582123, 0.20409796, 0.00412893, 0.15720603, 0.045046154, 0.0030491839, 0.01681367}
  128. want = []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154, 0.043722924, 0.036774673, 0.026986899, 0.01681367, 0.0046718004, 0.00412893, 0.0030491839}
  129. got = topK(toTokens(input), 0)
  130. if len(got) != len(input) {
  131. t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(got))
  132. }
  133. compareLogits(t, "topK(-1)", want, got)
  134. }
  135. func TestTopP(t *testing.T) {
  136. input := []float32{-3, -2, -1, 0, 1, 2, 4}
  137. tokens := toTokens(input)
  138. // First apply temperature and softmax to get probabilities
  139. tokens = softmax(tokens)
  140. tokens = topK(tokens, 20)
  141. // Then apply topP
  142. got := topP(tokens, 0.95)
  143. // Should keep tokens until cumsum > 0.95
  144. if len(got) > 3 {
  145. t.Errorf("topP(0.95): kept too many tokens: got %d", len(got))
  146. t.Logf("got: %v", got)
  147. }
  148. }
  149. func TestMinP(t *testing.T) {
  150. input := []float32{-3, -2, -1, 0, 1, 2, 4, 3}
  151. tokens := toTokens(input)
  152. // First apply temperature and softmax
  153. tokens = softmax(tokens)
  154. // Then apply minP
  155. got := minP(tokens, 0.2)
  156. // Should keep tokens with prob >= 0.2 * max_prob
  157. if len(got) > 3 {
  158. t.Errorf("minP(0.2): kept too many tokens: got %d", len(got))
  159. }
  160. }
  161. func TestSortLogits(t *testing.T) {
  162. input := []float32{0.026986899, 0.043722924, 0.036774673, 0.27755088, 0.0046718004, 0.08582123, 0.20409796, 0.00412893, 0.15720603, 0.045046154, 0.0030491839, 0.01681367}
  163. tokens := toTokens(input)
  164. tokens = topK(tokens, 20)
  165. for i := 1; i < len(tokens); i++ {
  166. if tokens[i].value > tokens[i-1].value {
  167. t.Errorf("sortLogits: tokens not sorted in descending order at index %d: %f > %f",
  168. i, tokens[i].value, tokens[i-1].value)
  169. }
  170. }
  171. want := []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154, 0.043722924, 0.036774673, 0.026986899, 0.01681367, 0.0046718004, 0.00412893, 0.0030491839}
  172. compareLogits(t, "sortLogits", want, tokens)
  173. }
  174. func BenchmarkTransforms(b *testing.B) {
  175. // Generate random logits
  176. tokens := make([]token, 1<<16)
  177. for i := range tokens {
  178. tokens[i] = token{
  179. id: int32(i),
  180. value: rand.Float32(),
  181. }
  182. }
  183. tokensCopy := make([]token, len(tokens))
  184. b.Run("Temperature", func(b *testing.B) {
  185. b.ResetTimer()
  186. for b.Loop() {
  187. copy(tokensCopy, tokens)
  188. temperature(tokensCopy, 0.5)
  189. }
  190. })
  191. b.Run("Softmax", func(b *testing.B) {
  192. b.ResetTimer()
  193. for b.Loop() {
  194. copy(tokensCopy, tokens)
  195. softmax(tokensCopy)
  196. }
  197. })
  198. b.Run("TopK", func(b *testing.B) {
  199. b.ResetTimer()
  200. for b.Loop() {
  201. copy(tokensCopy, tokens)
  202. topK(tokensCopy, 10)
  203. }
  204. })
  205. b.Run("TopP", func(b *testing.B) {
  206. b.ResetTimer()
  207. for b.Loop() {
  208. copy(tokensCopy, tokens)
  209. topP(tokensCopy, 0.9)
  210. }
  211. })
  212. b.Run("MinP", func(b *testing.B) {
  213. b.ResetTimer()
  214. for b.Loop() {
  215. copy(tokensCopy, tokens)
  216. minP(tokensCopy, 0.2)
  217. }
  218. })
  219. b.Run("SortTokens", func(b *testing.B) {
  220. b.ResetTimer()
  221. for b.Loop() {
  222. copy(tokensCopy, tokens)
  223. topK(tokensCopy, 200000)
  224. }
  225. })
  226. }