transforms_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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=3
  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. }
  67. func TestTopP(t *testing.T) {
  68. input := []float32{-3, -2, -1, 0, 1, 2, 4}
  69. tokens := toTokens(input)
  70. // First apply temperature and softmax to get probabilities
  71. tokens = temperature(tokens, 1)
  72. sortLogits(tokens)
  73. // Then apply topP
  74. got := topP(tokens, 0.95)
  75. // Should keep tokens until cumsum > 0.95
  76. if len(got) > 3 {
  77. t.Errorf("topP(0.95): kept too many tokens: got %d", len(got))
  78. t.Logf("got: %v", got)
  79. }
  80. }
  81. func TestMinP(t *testing.T) {
  82. input := []float32{-3, -2, -1, 0, 1, 2, 4, 3}
  83. tokens := toTokens(input)
  84. // First apply temperature and softmax
  85. tokens = temperature(tokens, 1)
  86. // Then apply minP
  87. got := minP(tokens, 0.2)
  88. // Should keep tokens with prob >= 0.2 * max_prob
  89. if len(got) > 3 {
  90. t.Errorf("minP(0.2): kept too many tokens: got %d", len(got))
  91. }
  92. }
  93. func TestSortLogits(t *testing.T) {
  94. 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}
  95. tokens := toTokens(input)
  96. sortLogits(tokens)
  97. for i := 1; i < len(tokens); i++ {
  98. if tokens[i].value > tokens[i-1].value {
  99. t.Errorf("sortLogits: tokens not sorted in descending order at index %d: %f > %f",
  100. i, tokens[i].value, tokens[i-1].value)
  101. }
  102. }
  103. 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}
  104. compareLogits(t, "sortLogits", want, tokens)
  105. }
  106. func BenchmarkTransforms(b *testing.B) {
  107. // Generate random logits
  108. tokens := make([]token, 1<<16)
  109. for i := range tokens {
  110. tokens[i] = token{
  111. id: int32(i),
  112. value: rand.Float32(),
  113. }
  114. }
  115. tokensCopy := make([]token, len(tokens))
  116. b.Run("Temperature", func(b *testing.B) {
  117. b.ResetTimer()
  118. for b.Loop() {
  119. copy(tokensCopy, tokens)
  120. temperature(tokensCopy, 0.5)
  121. }
  122. })
  123. b.Run("TopK", func(b *testing.B) {
  124. b.ResetTimer()
  125. for b.Loop() {
  126. copy(tokensCopy, tokens)
  127. topK(tokensCopy, 10)
  128. }
  129. })
  130. b.Run("TopP", func(b *testing.B) {
  131. b.ResetTimer()
  132. for b.Loop() {
  133. copy(tokensCopy, tokens)
  134. topP(tokensCopy, 0.9)
  135. }
  136. })
  137. b.Run("MinP", func(b *testing.B) {
  138. b.ResetTimer()
  139. for b.Loop() {
  140. copy(tokensCopy, tokens)
  141. minP(tokensCopy, 0.2)
  142. }
  143. })
  144. b.Run("SortTokens", func(b *testing.B) {
  145. b.ResetTimer()
  146. for b.Loop() {
  147. copy(tokensCopy, tokens)
  148. sortLogits(tokensCopy)
  149. }
  150. })
  151. }