backend.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package ml
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "strings"
  9. )
  10. type Config interface {
  11. Architecture() string
  12. String(string, ...string) string
  13. Uint(string, ...uint32) uint32
  14. Float(string, ...float32) float32
  15. Strings(string, ...[]string) []string
  16. Uints(string, ...[]uint32) []uint32
  17. }
  18. type Backend interface {
  19. Config() Config
  20. Get(name string) Tensor
  21. NewContext() Context
  22. }
  23. var backends = make(map[string]func(*os.File) (Backend, error))
  24. func RegisterBackend(name string, f func(*os.File) (Backend, error)) {
  25. if _, ok := backends[name]; ok {
  26. panic("backend: backend already registered")
  27. }
  28. backends[name] = f
  29. }
  30. func NewBackend(f *os.File) (Backend, error) {
  31. if backend, ok := backends["ggml"]; ok {
  32. return backend(f)
  33. }
  34. return nil, fmt.Errorf("unsupported backend")
  35. }
  36. type Context interface {
  37. Zeros(dtype DType, shape ...int) Tensor
  38. FromFloatSlice(s []float32, shape ...int) (Tensor, error)
  39. FromIntSlice(s []int32, shape ...int) (Tensor, error)
  40. Forward(Tensor)
  41. Compute(...Tensor)
  42. Close()
  43. }
  44. type Tensor interface {
  45. Dim(n int) int
  46. Stride(n int) int
  47. Shape() []int
  48. DType() DType
  49. Bytes() []byte
  50. Floats() []float32
  51. Add(ctx Context, t2 Tensor) Tensor
  52. Mul(ctx Context, t2 Tensor) Tensor
  53. Mulmat(ctx Context, t2 Tensor) Tensor
  54. MulmatFullPrec(ctx Context, t2 Tensor) Tensor
  55. Softmax(ctx Context) Tensor
  56. LayerNorm(ctx Context, weight, bias Tensor, eps float32) Tensor
  57. RMSNorm(ctx Context, weight Tensor, eps float32) Tensor
  58. Scale(ctx Context, s float64) Tensor
  59. Conv2D(ctx Context, weight Tensor, s0, s1, p0, p1, d0, d1 int) Tensor
  60. RoPE(ctx Context, positionIDs, ropeFactors Tensor, dim uint32, base, scale float32) Tensor
  61. Tanh(ctx Context) Tensor
  62. GELU(ctx Context) Tensor
  63. SILU(ctx Context) Tensor
  64. Reshape(ctx Context, shape ...int) Tensor
  65. View(ctx Context, offset int, shape ...int) Tensor
  66. Permute(ctx Context, shape ...int) Tensor
  67. Contiguous(ctx Context) Tensor
  68. Pad(ctx Context, shape ...int) Tensor
  69. Unpad(ctx Context, shape ...int) Tensor
  70. Stack(ctx Context, dim int, s ...Tensor) Tensor
  71. Concat(ctx Context, t2 Tensor, dim int) Tensor
  72. Rows(ctx Context, t2 Tensor) Tensor
  73. Copy(ctx Context, t2 Tensor) Tensor
  74. }
  75. type number interface {
  76. ~int | ~int8 | ~int16 | ~int32 | ~int64 |
  77. ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
  78. ~float32 | ~float64 |
  79. ~complex64 | ~complex128
  80. }
  81. func mul[T number](s ...T) T {
  82. p := T(1)
  83. for _, v := range s {
  84. p *= v
  85. }
  86. return p
  87. }
  88. type DumpOptions struct {
  89. // Items is the number of elements to print at the beginning and end of each dimension.
  90. Items int
  91. // Precision is the number of decimal places to print. Applies to float32 and float64.
  92. Precision int
  93. }
  94. func Dump(t Tensor, opts ...DumpOptions) string {
  95. if len(opts) < 1 {
  96. opts = append(opts, DumpOptions{
  97. Items: 3,
  98. Precision: 4,
  99. })
  100. }
  101. switch t.DType() {
  102. case DTypeF32:
  103. return dump[[]float32](t, opts[0].Items, func(f float32) string {
  104. return strconv.FormatFloat(float64(f), 'f', opts[0].Precision, 32)
  105. })
  106. case DTypeI32:
  107. return dump[[]int32](t, opts[0].Items, func(i int32) string {
  108. return strconv.FormatInt(int64(i), 10)
  109. })
  110. default:
  111. return "<unsupported>"
  112. }
  113. }
  114. func dump[S ~[]E, E number](t Tensor, items int, fn func(E) string) string {
  115. bts := t.Bytes()
  116. if bts == nil {
  117. return "<nil>"
  118. }
  119. s := make(S, mul(t.Shape()...))
  120. if err := binary.Read(bytes.NewBuffer(t.Bytes()), binary.LittleEndian, &s); err != nil {
  121. panic(err)
  122. }
  123. shape := t.Shape()
  124. var sb strings.Builder
  125. var f func([]int, int)
  126. f = func(dims []int, stride int) {
  127. prefix := strings.Repeat(" ", len(shape)-len(dims)+1)
  128. fmt.Fprint(&sb, "[")
  129. defer func() { fmt.Fprint(&sb, "]") }()
  130. for i := 0; i < dims[0]; i++ {
  131. if i >= items && i < dims[0]-items {
  132. fmt.Fprint(&sb, "..., ")
  133. // skip to next printable element
  134. skip := dims[0] - 2*items
  135. if len(dims) > 1 {
  136. stride += mul(append(dims[1:], skip)...)
  137. fmt.Fprint(&sb, strings.Repeat("\n", len(dims)-1), prefix)
  138. }
  139. i += skip - 1
  140. } else if len(dims) > 1 {
  141. f(dims[1:], stride)
  142. stride += mul(dims[1:]...)
  143. if i < dims[0]-1 {
  144. fmt.Fprint(&sb, ",", strings.Repeat("\n", len(dims)-1), prefix)
  145. }
  146. } else {
  147. fmt.Fprint(&sb, fn(s[stride+i]))
  148. if i < dims[0]-1 {
  149. fmt.Fprint(&sb, ", ")
  150. }
  151. }
  152. }
  153. }
  154. f(shape, 0)
  155. return sb.String()
  156. }
  157. type DType int
  158. const (
  159. DTypeF32 DType = iota
  160. DTypeI32
  161. DTypeOther
  162. )