transforms_test.go 4.0 KB

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