transforms_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. tokens := toTokens(input)
  34. temperature(tokens, 0.5)
  35. want := []float32{2.0, 8.0, -4.0, 0.0}
  36. compareLogits(t, "temperature(0.5)", want, tokens)
  37. input = []float32{1.0, 4.0, -2.0, 0.0}
  38. tokens = toTokens(input)
  39. temperature(tokens, 1.0)
  40. want = []float32{1.0, 4.0, -2.0, 0.0}
  41. compareLogits(t, "temperature(1)", want, tokens)
  42. input = []float32{1.0, 4.0, -2.0, 0.0}
  43. tokens = toTokens(input)
  44. temperature(tokens, 0.0)
  45. want = []float32{1e7, 4e7, -2e7, 0.0}
  46. compareLogits(t, "temperature(0)", want, tokens)
  47. }
  48. func TestSoftmax(t *testing.T) {
  49. tests := []struct {
  50. name string
  51. input []float32
  52. expected []float32
  53. }{
  54. {
  55. name: "correctness softmax",
  56. input: []float32{1, -2, 3, 0},
  57. expected: []float32{0.113550, 0.005653, 0.839024, 0.041773},
  58. },
  59. {
  60. name: "normal distribution",
  61. 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},
  62. },
  63. {
  64. name: "single value",
  65. input: []float32{1.0},
  66. },
  67. {
  68. name: "identical values",
  69. input: []float32{0.9, 0.9, 0.9},
  70. },
  71. {
  72. name: "large values",
  73. input: []float32{1000.0, 2000.0, 3000.0},
  74. },
  75. {
  76. name: "small values",
  77. input: []float32{1e-6, 2e-6, 3e-6},
  78. },
  79. {
  80. name: "negative values",
  81. input: []float32{-1.0, -2.0, -3.0},
  82. },
  83. {
  84. name: "mixed values",
  85. input: []float32{-100.0, 0.0, 100.0},
  86. },
  87. }
  88. for _, tt := range tests {
  89. t.Run(tt.name, func(t *testing.T) {
  90. tokens := toTokens(tt.input)
  91. softmax(tokens)
  92. if tt.expected != nil {
  93. compareLogits(t, tt.name, tt.expected, tokens)
  94. return
  95. }
  96. // Check probabilities sum to 1
  97. var sum float32
  98. for _, token := range tokens {
  99. sum += token.value
  100. if token.value < 0 || token.value > 1 {
  101. t.Errorf("probability out of range [0,1]: got %f", token.value)
  102. }
  103. }
  104. if math.Abs(float64(sum-1.0)) > 1e-6 {
  105. t.Errorf("probabilities don't sum to 1: got %f", sum)
  106. }
  107. })
  108. }
  109. }
  110. func TestTopK(t *testing.T) {
  111. 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}
  112. tokens := toTokens(input)
  113. tokens = topK(tokens, 5)
  114. if len(tokens) != 5 {
  115. t.Errorf("topK(5): wrong length: want 5, got %d", len(tokens))
  116. }
  117. want := []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154}
  118. compareLogits(t, "topK(3)", want, tokens)
  119. tokens = toTokens(input)
  120. tokens = topK(tokens, 20)
  121. if len(tokens) != len(input) {
  122. t.Errorf("topK(20): wrong length: want %d, got %d", len(input), len(tokens))
  123. }
  124. 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}
  125. 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}
  126. tokens = toTokens(input)
  127. tokens = topK(tokens, -1)
  128. if len(tokens) != len(input) {
  129. t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(tokens))
  130. }
  131. compareLogits(t, "topK(-1)", want, tokens)
  132. 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}
  133. 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}
  134. tokens = toTokens(input)
  135. tokens = topK(tokens, 0)
  136. if len(tokens) != len(input) {
  137. t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(tokens))
  138. }
  139. compareLogits(t, "topK(-1)", want, tokens)
  140. input = []float32{-1e7, -2e7, -3e7, -4e7}
  141. tokens = toTokens(input)
  142. tokens = topK(tokens, 1)
  143. if len(tokens) < 1 {
  144. t.Error("topK should keep at least one token")
  145. }
  146. }
  147. func TestTopP(t *testing.T) {
  148. input := []float32{-3, -2, -1, 0, 1, 2, 4}
  149. tokens := toTokens(input)
  150. // First apply temperature and softmax to get probabilities
  151. softmax(tokens)
  152. tokens = topK(tokens, 20)
  153. // Then apply topP
  154. tokens = topP(tokens, 0.95)
  155. // Should keep tokens until cumsum > 0.95
  156. if len(tokens) > 3 {
  157. t.Errorf("topP(0.95): kept too many tokens: got %d", len(tokens))
  158. t.Logf("got: %v", tokens)
  159. }
  160. // Test edge case - ensure at least one token remains
  161. input = []float32{-1e6, -1e6, -1e6} // One dominant token
  162. tokens = toTokens(input)
  163. softmax(tokens)
  164. tokens = topP(tokens, 0.0) // Very small p
  165. if len(tokens) < 1 {
  166. t.Error("topP should keep at least one token")
  167. }
  168. }
  169. func TestMinP(t *testing.T) {
  170. input := []float32{-3, -2, -1, 0, 1, 2, 4, 3}
  171. tokens := toTokens(input)
  172. // First apply temperature and softmax
  173. tokens = topK(tokens, 20)
  174. softmax(tokens)
  175. tokens = minP(tokens, 1.0)
  176. if len(tokens) != 1 {
  177. t.Errorf("minP(1.0): should keep all tokens, got %d, want %d", len(tokens), len(tokens))
  178. }
  179. // Test with normal p value
  180. tokens = toTokens(input) // Reset tokens
  181. tokens = topK(tokens, 20)
  182. softmax(tokens)
  183. tokens = minP(tokens, 0.2)
  184. // Should keep tokens with prob >= 0.2 * max_prob
  185. if len(tokens) > 3 {
  186. t.Errorf("minP(0.2): kept too many tokens: got %d", len(tokens))
  187. t.Logf("got: %v", tokens)
  188. }
  189. // Test with zero p value
  190. tokens = toTokens(input) // Reset tokens
  191. tokens = topK(tokens, 20)
  192. softmax(tokens)
  193. tokens = minP(tokens, 0.0)
  194. // Should keep only the highest probability token
  195. if len(tokens) != len(input) {
  196. t.Errorf("minP(0.0): should keep only one token, got %d", len(tokens))
  197. t.Logf("got: %v", tokens)
  198. }
  199. input = []float32{1e-10, 1e-10, 1e-10}
  200. tokens = toTokens(input)
  201. softmax(tokens)
  202. tokens = minP(tokens, 1.0)
  203. if len(tokens) < 1 {
  204. t.Error("minP should keep at least one token even with extreme probabilities")
  205. }
  206. }
  207. func TestSortLogits(t *testing.T) {
  208. 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}
  209. tokens := toTokens(input)
  210. tokens = topK(tokens, 20)
  211. for i := 1; i < len(tokens); i++ {
  212. if tokens[i].value > tokens[i-1].value {
  213. t.Errorf("sortLogits: tokens not sorted in descending order at index %d: %f > %f",
  214. i, tokens[i].value, tokens[i-1].value)
  215. }
  216. }
  217. 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}
  218. compareLogits(t, "sortLogits", want, tokens)
  219. }
  220. func BenchmarkTransforms(b *testing.B) {
  221. // Generate random logits
  222. tokens := make([]token, 1<<16)
  223. for i := range tokens {
  224. tokens[i] = token{
  225. id: int32(i),
  226. value: rand.Float32(),
  227. }
  228. }
  229. tokensCopy := make([]token, len(tokens))
  230. b.Run("Temperature", func(b *testing.B) {
  231. b.ResetTimer()
  232. for b.Loop() {
  233. copy(tokensCopy, tokens)
  234. temperature(tokensCopy, 0.5)
  235. }
  236. })
  237. b.Run("Softmax", func(b *testing.B) {
  238. b.ResetTimer()
  239. for b.Loop() {
  240. copy(tokensCopy, tokens)
  241. softmax(tokensCopy)
  242. }
  243. })
  244. b.Run("TopK", func(b *testing.B) {
  245. b.ResetTimer()
  246. for b.Loop() {
  247. copy(tokensCopy, tokens)
  248. tokens = topK(tokensCopy, 10)
  249. }
  250. })
  251. b.Run("TopP", func(b *testing.B) {
  252. b.ResetTimer()
  253. for b.Loop() {
  254. copy(tokensCopy, tokens)
  255. tokens = topP(tokensCopy, 0.9)
  256. }
  257. })
  258. b.Run("MinP", func(b *testing.B) {
  259. b.ResetTimer()
  260. for b.Loop() {
  261. copy(tokensCopy, tokens)
  262. tokens = minP(tokensCopy, 0.2)
  263. }
  264. })
  265. b.Run("SortTokens", func(b *testing.B) {
  266. b.ResetTimer()
  267. for b.Loop() {
  268. copy(tokensCopy, tokens)
  269. tokens = topK(tokensCopy, 200000)
  270. }
  271. })
  272. }