transforms_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. // Test with very high p value
  154. got := topP(tokens, 1.0)
  155. // Should keep all tokens since p is 1
  156. if len(got) != len(input) {
  157. t.Errorf("topP(1.0): should keep all tokens, got %d, want %d", len(got), len(input))
  158. }
  159. // Test with normal p value
  160. got = topP(tokens, 0.95)
  161. if len(got) > 3 {
  162. t.Errorf("topP(0.95): kept too many tokens: got %d", len(tokens))
  163. t.Logf("got: %v", got)
  164. }
  165. // Test edge case - ensure at least one token remains
  166. input = []float32{-1e6, -1e6, -1e7}
  167. tokens = toTokens(input)
  168. tokens = topK(tokens, 20)
  169. softmax(tokens)
  170. got = topP(tokens, 0.0)
  171. if len(got) < 1 {
  172. t.Error("topP should keep at least one token")
  173. }
  174. // Test with zero p value
  175. got = topP(tokens, 0.0)
  176. // Should keep only the highest probability token
  177. if len(got) != 1 {
  178. t.Errorf("topP(0.0): should keep only one token, got %d", len(got))
  179. t.Logf("got: %v", got)
  180. }
  181. tokens = toTokens(input)
  182. tokens = topK(tokens, 20)
  183. softmax(tokens)
  184. got = topP(tokens, 1e-10)
  185. if len(got) == 0 {
  186. t.Errorf("topP(1e-10): should keep at least one token, got %d", len(got))
  187. t.Logf("got: %v", got)
  188. }
  189. }
  190. func TestMinP(t *testing.T) {
  191. input := []float32{-2, 0, -1, -3, 2, 1, 4, 3}
  192. tokens := toTokens(input)
  193. // First apply temperature and softmax
  194. tokens = topK(tokens, 20)
  195. softmax(tokens)
  196. tokens = minP(tokens, 1.0)
  197. if len(tokens) != 1 {
  198. t.Errorf("minP(1.0): should keep all tokens, got %d, want %d", len(tokens), len(tokens))
  199. }
  200. // Test with normal p value
  201. tokens = toTokens(input) // Reset tokens
  202. tokens = topK(tokens, 20)
  203. softmax(tokens)
  204. tokens = minP(tokens, 0.2)
  205. // Should keep tokens with prob >= 0.2 * max_prob
  206. if len(tokens) > 3 {
  207. t.Errorf("minP(0.2): kept too many tokens: got %d", len(tokens))
  208. t.Logf("got: %v", tokens)
  209. }
  210. // Test with zero p value
  211. tokens = toTokens(input) // Reset tokens
  212. tokens = topK(tokens, 20)
  213. softmax(tokens)
  214. tokens = minP(tokens, 0.0)
  215. // Should keep only the highest probability token
  216. if len(tokens) != len(input) {
  217. t.Errorf("minP(0.0): should keep only one token, got %d", len(tokens))
  218. t.Logf("got: %v", tokens)
  219. }
  220. // Test with single token
  221. tokens = toTokens(input[:1])
  222. tokens = topK(tokens, 20)
  223. softmax(tokens)
  224. tokens = minP(tokens, 0.1)
  225. // Should keep only the highest probability token
  226. if len(tokens) != 1 {
  227. t.Errorf("minP(0.1): should return single token, got %d", len(tokens))
  228. t.Logf("got: %v", tokens)
  229. }
  230. input = []float32{1e-10, 1e-10, 1e-10}
  231. tokens = toTokens(input)
  232. softmax(tokens)
  233. tokens = minP(tokens, 1.0)
  234. if len(tokens) < 1 {
  235. t.Error("minP should keep at least one token even with extreme probabilities")
  236. got := minP(tokens, 1.0)
  237. if len(got) != 1 {
  238. t.Errorf("minP(1.0): should keep all tokens, got %d, want %d", len(got), len(tokens))
  239. }
  240. // Test with normal p value
  241. got = minP(tokens, 0.2)
  242. // Should keep tokens with prob >= 0.2 * max_prob
  243. if len(got) > 3 {
  244. t.Errorf("minP(0.2): kept too many tokens: got %d", len(got))
  245. t.Logf("got: %v", got)
  246. }
  247. // Test with zero p value
  248. got = minP(tokens, 0.0)
  249. // Should keep only the highest probability token
  250. if len(got) != len(tokens) {
  251. t.Errorf("minP(0.0): should keep only one token, got %d", len(got))
  252. t.Logf("got: %v", got)
  253. }
  254. }
  255. }
  256. func BenchmarkTransforms(b *testing.B) {
  257. // Generate random logits
  258. tokens := make([]token, 1<<16)
  259. for i := range tokens {
  260. tokens[i] = token{
  261. id: int32(i),
  262. value: rand.Float32(),
  263. }
  264. }
  265. tokensCopy := make([]token, len(tokens))
  266. b.Run("Temperature", func(b *testing.B) {
  267. b.ResetTimer()
  268. for b.Loop() {
  269. copy(tokensCopy, tokens)
  270. temperature(tokensCopy, 0.5)
  271. }
  272. })
  273. b.Run("Softmax", func(b *testing.B) {
  274. b.ResetTimer()
  275. for b.Loop() {
  276. copy(tokensCopy, tokens)
  277. softmax(tokensCopy)
  278. }
  279. })
  280. b.Run("TopK", func(b *testing.B) {
  281. b.ResetTimer()
  282. for b.Loop() {
  283. copy(tokensCopy, tokens)
  284. tokens = topK(tokensCopy, 10)
  285. }
  286. })
  287. b.Run("TopP", func(b *testing.B) {
  288. b.ResetTimer()
  289. for b.Loop() {
  290. copy(tokensCopy, tokens)
  291. tokens = topP(tokensCopy, 0.9)
  292. }
  293. })
  294. b.Run("MinP", func(b *testing.B) {
  295. b.ResetTimer()
  296. for b.Loop() {
  297. copy(tokensCopy, tokens)
  298. tokens = minP(tokensCopy, 0.2)
  299. }
  300. })
  301. b.Run("SortTokens", func(b *testing.B) {
  302. b.ResetTimer()
  303. for b.Loop() {
  304. copy(tokensCopy, tokens)
  305. tokens = topK(tokensCopy, 200000)
  306. }
  307. })
  308. }