transforms_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 TestTemperatureAndSoftmax(t *testing.T) {
  32. input := []float32{1, 4, -2, 0}
  33. got := temperature(toTokens(input), 0.5)
  34. // Check probabilities sum to 1
  35. var sum float32
  36. for _, token := range got {
  37. sum += token.value
  38. }
  39. if math.Abs(float64(sum-1.0)) > 1e-6 {
  40. t.Errorf("probabilities don't sum to 1: got %f", sum)
  41. }
  42. got = temperature(toTokens(input), 1)
  43. // Check probabilities sum to 1
  44. sum = 0.0
  45. for _, token := range got {
  46. sum += token.value
  47. }
  48. if math.Abs(float64(sum-1.0)) > 1e-6 {
  49. t.Errorf("probabilities don't sum to 1: got %f", sum)
  50. }
  51. }
  52. func TestTopK(t *testing.T) {
  53. 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}
  54. // Test k=5
  55. got := topK(toTokens(input), 5)
  56. if len(got) != 5 {
  57. t.Errorf("topK(5): wrong length: want 5, got %d", len(got))
  58. }
  59. // Should keep highest 3 values in descending order
  60. want := []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154}
  61. compareLogits(t, "topK(3)", want, got)
  62. got = topK(toTokens(input), 20)
  63. if len(got) != len(input) {
  64. t.Errorf("topK(20): wrong length: want %d, got %d", len(input), len(got))
  65. }
  66. // Test k=-1
  67. 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}
  68. 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}
  69. got = topK(toTokens(input), -1)
  70. if len(got) != len(input) {
  71. t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(got))
  72. }
  73. compareLogits(t, "topK(-1)", want, got)
  74. // Test k=0
  75. 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}
  76. 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}
  77. got = topK(toTokens(input), 0)
  78. if len(got) != len(input) {
  79. t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(got))
  80. }
  81. compareLogits(t, "topK(-1)", want, got)
  82. }
  83. func TestTopP(t *testing.T) {
  84. input := []float32{-3, -2, -1, 0, 1, 2, 4}
  85. tokens := toTokens(input)
  86. // First apply temperature and softmax to get probabilities
  87. tokens = temperature(tokens, 1)
  88. tokens = topK(tokens, 20)
  89. // Then apply topP
  90. got := topP(tokens, 0.95)
  91. // Should keep tokens until cumsum > 0.95
  92. if len(got) > 3 {
  93. t.Errorf("topP(0.95): kept too many tokens: got %d", len(got))
  94. t.Logf("got: %v", got)
  95. }
  96. }
  97. func TestMinP(t *testing.T) {
  98. input := []float32{-3, -2, -1, 0, 1, 2, 4, 3}
  99. tokens := toTokens(input)
  100. // First apply temperature and softmax
  101. tokens = temperature(tokens, 1)
  102. // Then apply minP
  103. got := minP(tokens, 0.2)
  104. // Should keep tokens with prob >= 0.2 * max_prob
  105. if len(got) > 3 {
  106. t.Errorf("minP(0.2): kept too many tokens: got %d", len(got))
  107. }
  108. }
  109. func TestSortLogits(t *testing.T) {
  110. 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}
  111. tokens := toTokens(input)
  112. tokens = topK(tokens, 20)
  113. for i := 1; i < len(tokens); i++ {
  114. if tokens[i].value > tokens[i-1].value {
  115. t.Errorf("sortLogits: tokens not sorted in descending order at index %d: %f > %f",
  116. i, tokens[i].value, tokens[i-1].value)
  117. }
  118. }
  119. 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}
  120. compareLogits(t, "sortLogits", want, tokens)
  121. }
  122. func BenchmarkTransforms(b *testing.B) {
  123. // Generate random logits
  124. tokens := make([]token, 1<<16)
  125. for i := range tokens {
  126. tokens[i] = token{
  127. id: int32(i),
  128. value: rand.Float32(),
  129. }
  130. }
  131. tokensCopy := make([]token, len(tokens))
  132. b.Run("Temperature", func(b *testing.B) {
  133. b.ResetTimer()
  134. for b.Loop() {
  135. copy(tokensCopy, tokens)
  136. temperature(tokensCopy, 0.5)
  137. }
  138. })
  139. b.Run("TopK", func(b *testing.B) {
  140. b.ResetTimer()
  141. for b.Loop() {
  142. copy(tokensCopy, tokens)
  143. topK(tokensCopy, 10)
  144. }
  145. })
  146. b.Run("TopP", func(b *testing.B) {
  147. b.ResetTimer()
  148. for b.Loop() {
  149. copy(tokensCopy, tokens)
  150. topP(tokensCopy, 0.9)
  151. }
  152. })
  153. b.Run("MinP", func(b *testing.B) {
  154. b.ResetTimer()
  155. for b.Loop() {
  156. copy(tokensCopy, tokens)
  157. minP(tokensCopy, 0.2)
  158. }
  159. })
  160. b.Run("SortTokens", func(b *testing.B) {
  161. b.ResetTimer()
  162. for b.Loop() {
  163. copy(tokensCopy, tokens)
  164. topK(tokensCopy, 200000)
  165. }
  166. })
  167. }