123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- package sample
- import (
- "math"
- "math/rand/v2"
- "testing"
- )
- // Helper to convert float32 slice to logit slice
- func toTokens(values []float32) []token {
- tokens := make([]token, len(values))
- for i, v := range values {
- tokens[i] = token{
- id: int32(i),
- value: v,
- }
- }
- return tokens
- }
- // Helper to compare logit slices
- func compareLogits(t *testing.T, name string, want []float32, got []token) {
- t.Helper()
- if len(want) != len(got) {
- t.Errorf("%s: length mismatch: want %d, got %d", name, len(want), len(got))
- return
- }
- for i := range want {
- if math.Abs(float64(got[i].value-want[i])) > 1e-6 {
- t.Errorf("%s: index %d: want %f, got %f", name, i, want[i], got[i].value)
- }
- }
- }
- func TestTemperature(t *testing.T) {
- input := []float32{1.0, 4.0, -2.0, 0.0}
- tokens := toTokens(input)
- temperature(tokens, 0.5)
- want := []float32{2.0, 8.0, -4.0, 0.0}
- compareLogits(t, "temperature(0.5)", want, tokens)
- input = []float32{1.0, 4.0, -2.0, 0.0}
- tokens = toTokens(input)
- temperature(tokens, 1.0)
- want = []float32{1.0, 4.0, -2.0, 0.0}
- compareLogits(t, "temperature(1)", want, tokens)
- input = []float32{1.0, 4.0, -2.0, 0.0}
- tokens = toTokens(input)
- temperature(tokens, 0.0)
- want = []float32{1e7, 4e7, -2e7, 0.0}
- compareLogits(t, "temperature(0)", want, tokens)
- }
- func TestSoftmax(t *testing.T) {
- tests := []struct {
- name string
- input []float32
- expected []float32
- }{
- {
- name: "correctness softmax",
- input: []float32{1, -2, 3, 0},
- expected: []float32{0.113550, 0.005653, 0.839024, 0.041773},
- },
- {
- name: "normal distribution",
- 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},
- },
- {
- name: "single value",
- input: []float32{1.0},
- },
- {
- name: "identical values",
- input: []float32{0.9, 0.9, 0.9},
- },
- {
- name: "large values",
- input: []float32{1000.0, 2000.0, 3000.0},
- },
- {
- name: "small values",
- input: []float32{1e-6, 2e-6, 3e-6},
- },
- {
- name: "negative values",
- input: []float32{-1.0, -2.0, -3.0},
- },
- {
- name: "mixed values",
- input: []float32{-100.0, 0.0, 100.0},
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- tokens := toTokens(tt.input)
- softmax(tokens)
- if tt.expected != nil {
- compareLogits(t, tt.name, tt.expected, tokens)
- return
- }
- // Check probabilities sum to 1
- var sum float32
- for _, token := range tokens {
- sum += token.value
- if token.value < 0 || token.value > 1 {
- t.Errorf("probability out of range [0,1]: got %f", token.value)
- }
- }
- if math.Abs(float64(sum-1.0)) > 1e-6 {
- t.Errorf("probabilities don't sum to 1: got %f", sum)
- }
- })
- }
- }
- func TestTopK(t *testing.T) {
- 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}
- tokens := toTokens(input)
- tokens = topK(tokens, 5)
- if len(tokens) != 5 {
- t.Errorf("topK(5): wrong length: want 5, got %d", len(tokens))
- }
- want := []float32{0.27755088, 0.20409796, 0.15720603, 0.08582123, 0.045046154}
- compareLogits(t, "topK(3)", want, tokens)
- tokens = toTokens(input)
- tokens = topK(tokens, 20)
- if len(tokens) != len(input) {
- t.Errorf("topK(20): wrong length: want %d, got %d", len(input), len(tokens))
- }
- 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}
- 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}
- tokens = toTokens(input)
- tokens = topK(tokens, -1)
- if len(tokens) != len(input) {
- t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(tokens))
- }
- compareLogits(t, "topK(-1)", want, tokens)
- 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}
- 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}
- tokens = toTokens(input)
- tokens = topK(tokens, 0)
- if len(tokens) != len(input) {
- t.Errorf("topK(-1): wrong length: want %d, got %d", len(input), len(tokens))
- }
- compareLogits(t, "topK(-1)", want, tokens)
- input = []float32{-1e7, -2e7, -3e7, -4e7}
- tokens = toTokens(input)
- tokens = topK(tokens, 1)
- if len(tokens) < 1 {
- t.Error("topK should keep at least one token")
- }
- }
- func TestTopP(t *testing.T) {
- input := []float32{-3, -2, -1, 0, 1, 2, 4}
- tokens := toTokens(input)
- // First apply temperature and softmax to get probabilities
- softmax(tokens)
- tokens = topK(tokens, 20)
- // Then apply topP
- tokens = topP(tokens, 0.95)
- // Should keep tokens until cumsum > 0.95
- if len(tokens) > 3 {
- t.Errorf("topP(0.95): kept too many tokens: got %d", len(tokens))
- t.Logf("got: %v", tokens)
- }
- // Test edge case - ensure at least one token remains
- input = []float32{-1e6, -1e6, -1e6} // One dominant token
- tokens = toTokens(input)
- softmax(tokens)
- tokens = topP(tokens, 0.0) // Very small p
- if len(tokens) < 1 {
- t.Error("topP should keep at least one token")
- }
- }
- func TestMinP(t *testing.T) {
- input := []float32{-3, -2, -1, 0, 1, 2, 4, 3}
- tokens := toTokens(input)
- // First apply temperature and softmax
- tokens = topK(tokens, 20)
- softmax(tokens)
- tokens = minP(tokens, 1.0)
- if len(tokens) != 1 {
- t.Errorf("minP(1.0): should keep all tokens, got %d, want %d", len(tokens), len(tokens))
- }
- // Test with normal p value
- tokens = toTokens(input) // Reset tokens
- tokens = topK(tokens, 20)
- softmax(tokens)
- tokens = minP(tokens, 0.2)
- // Should keep tokens with prob >= 0.2 * max_prob
- if len(tokens) > 3 {
- t.Errorf("minP(0.2): kept too many tokens: got %d", len(tokens))
- t.Logf("got: %v", tokens)
- }
- // Test with zero p value
- tokens = toTokens(input) // Reset tokens
- tokens = topK(tokens, 20)
- softmax(tokens)
- tokens = minP(tokens, 0.0)
- // Should keep only the highest probability token
- if len(tokens) != len(input) {
- t.Errorf("minP(0.0): should keep only one token, got %d", len(tokens))
- t.Logf("got: %v", tokens)
- }
- input = []float32{1e-10, 1e-10, 1e-10}
- tokens = toTokens(input)
- softmax(tokens)
- tokens = minP(tokens, 1.0)
- if len(tokens) < 1 {
- t.Error("minP should keep at least one token even with extreme probabilities")
- }
- }
- func TestSortLogits(t *testing.T) {
- 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}
- tokens := toTokens(input)
- tokens = topK(tokens, 20)
- for i := 1; i < len(tokens); i++ {
- if tokens[i].value > tokens[i-1].value {
- t.Errorf("sortLogits: tokens not sorted in descending order at index %d: %f > %f",
- i, tokens[i].value, tokens[i-1].value)
- }
- }
- 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}
- compareLogits(t, "sortLogits", want, tokens)
- }
- func BenchmarkTransforms(b *testing.B) {
- // Generate random logits
- tokens := make([]token, 1<<16)
- for i := range tokens {
- tokens[i] = token{
- id: int32(i),
- value: rand.Float32(),
- }
- }
- tokensCopy := make([]token, len(tokens))
- b.Run("Temperature", func(b *testing.B) {
- b.ResetTimer()
- for b.Loop() {
- copy(tokensCopy, tokens)
- temperature(tokensCopy, 0.5)
- }
- })
- b.Run("Softmax", func(b *testing.B) {
- b.ResetTimer()
- for b.Loop() {
- copy(tokensCopy, tokens)
- softmax(tokensCopy)
- }
- })
- b.Run("TopK", func(b *testing.B) {
- b.ResetTimer()
- for b.Loop() {
- copy(tokensCopy, tokens)
- tokens = topK(tokensCopy, 10)
- }
- })
- b.Run("TopP", func(b *testing.B) {
- b.ResetTimer()
- for b.Loop() {
- copy(tokensCopy, tokens)
- tokens = topP(tokensCopy, 0.9)
- }
- })
- b.Run("MinP", func(b *testing.B) {
- b.ResetTimer()
- for b.Loop() {
- copy(tokensCopy, tokens)
- tokens = minP(tokensCopy, 0.2)
- }
- })
- b.Run("SortTokens", func(b *testing.B) {
- b.ResetTimer()
- for b.Loop() {
- copy(tokensCopy, tokens)
- tokens = topK(tokensCopy, 200000)
- }
- })
- }
|