sample.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package sample
  2. import (
  3. "errors"
  4. "math"
  5. "sort"
  6. "gonum.org/v1/gonum/floats"
  7. "gonum.org/v1/gonum/stat/sampleuv"
  8. )
  9. type Sampler interface {
  10. Sample([]float64) ([]float64, error)
  11. }
  12. type Temperature float64
  13. func (s Temperature) Sample(logits []float64) ([]float64, error) {
  14. if s < 0 || s > 1 {
  15. return nil, errors.New("temperature must be between 0 and 1")
  16. }
  17. copiedLogits := append([]float64(nil), logits...)
  18. // Greedy sampling
  19. if s == 0 {
  20. return []float64{floats.Max(copiedLogits)}, nil
  21. }
  22. floats.Scale(1.0/float64(s), copiedLogits)
  23. return copiedLogits, nil
  24. }
  25. type softmax struct{}
  26. func Softmax() Sampler {
  27. return softmax{}
  28. }
  29. func (softmax) Sample(logits []float64) ([]float64, error) {
  30. return computeSoftmax(logits)
  31. }
  32. func computeSoftmax(logits []float64) ([]float64, error) {
  33. copiedLogits := make([]float64, len(logits))
  34. copy(copiedLogits, logits)
  35. for i := range copiedLogits {
  36. copiedLogits[i] = math.Exp(copiedLogits[i])
  37. }
  38. floatSum := floats.Sum(copiedLogits)
  39. if floatSum == 0 {
  40. return nil, errors.New("no valid tokens found")
  41. }
  42. floats.Scale(1.0/floatSum, copiedLogits)
  43. return copiedLogits, nil
  44. }
  45. type TopK int
  46. func (k TopK) Sample(logits []float64) ([]float64, error) {
  47. if k <= 0 {
  48. return nil, errors.New("k must be positive")
  49. }
  50. if int(k) >= len(logits) {
  51. return logits, nil
  52. }
  53. indices := make([]int, len(logits))
  54. for i := range indices {
  55. indices[i] = i
  56. }
  57. sort.Slice(indices, func(i, j int) bool {
  58. return logits[indices[i]] > logits[indices[j]]
  59. })
  60. for _, idx := range indices[k:] {
  61. logits[idx] = math.NaN()
  62. }
  63. return logits, nil
  64. }
  65. type TopP float32
  66. func (p TopP) Sample(logits []float64) ([]float64, error) {
  67. if p <= 0 || p >= 1 {
  68. return nil, errors.New("p must be between 0 and 1")
  69. }
  70. probs, err := computeSoftmax(logits)
  71. if err != nil {
  72. return nil, err
  73. }
  74. indices := make([]int, len(probs))
  75. for i := range indices {
  76. indices[i] = i
  77. }
  78. sort.Slice(indices, func(i, j int) bool {
  79. return probs[indices[i]] > probs[indices[j]]
  80. })
  81. cumSum := 0.0
  82. for i, idx := range indices {
  83. cumSum += probs[idx]
  84. if cumSum > float64(p) {
  85. for _, idx := range indices[i+1:] {
  86. logits[idx] = math.NaN()
  87. }
  88. break
  89. }
  90. }
  91. return logits, nil
  92. }
  93. type MinP float32
  94. func (p MinP) Sample(logits []float64) ([]float64, error) {
  95. if p <= 0 || p >= 1 {
  96. return nil, errors.New("p must be between 0 and 1")
  97. }
  98. probs, err := computeSoftmax(logits)
  99. if err != nil {
  100. return nil, err
  101. }
  102. copiedProbs := make([]float64, len(probs))
  103. copy(copiedProbs, probs)
  104. sort.Slice(copiedProbs, func(i, j int) bool { return copiedProbs[i] > copiedProbs[j] })
  105. maxProb := floats.Max(probs)
  106. probThreshold := float64(p) * maxProb
  107. for i := range probs {
  108. if probs[i] < probThreshold {
  109. logits[i] = math.NaN()
  110. }
  111. }
  112. return logits, nil
  113. }
  114. type weighed struct{}
  115. func Weighed() Sampler {
  116. return weighed{}
  117. }
  118. func (s weighed) Sample(logits []float64) ([]float64, error) {
  119. logitsCopy := make([]float64, 0, len(logits))
  120. indices := make([]int, 0, len(logits))
  121. // the uv sampler does not support NaN values
  122. for i, logit := range logits {
  123. if !math.IsNaN(logit) {
  124. logitsCopy = append(logitsCopy, logit)
  125. indices = append(indices, i)
  126. }
  127. }
  128. if len(logitsCopy) == 0 {
  129. return nil, errors.New("no valid tokens found")
  130. }
  131. w := sampleuv.NewWeighted(logitsCopy, nil)
  132. if v, ok := w.Take(); ok {
  133. return []float64{float64(indices[v])}, nil
  134. }
  135. return nil, errors.New("weighed sampler failed")
  136. }
  137. func Sample(tokenID []float64, samplers ...Sampler) ([]float64, error) {
  138. var err error
  139. for _, sampler := range samplers {
  140. tokenID, err = sampler.Sample(tokenID)
  141. if err != nil {
  142. return nil, err
  143. }
  144. }
  145. return tokenID, nil
  146. }