backend.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. MaxTensors() int
  43. Close()
  44. }
  45. type Tensor interface {
  46. Dim(n int) int
  47. Stride(n int) int
  48. Shape() []int
  49. DType() DType
  50. Bytes() []byte
  51. Floats() []float32
  52. Add(ctx Context, t2 Tensor) Tensor
  53. Mul(ctx Context, t2 Tensor) Tensor
  54. Mulmat(ctx Context, t2 Tensor) Tensor
  55. MulmatFullPrec(ctx Context, t2 Tensor) Tensor
  56. Softmax(ctx Context) Tensor
  57. LayerNorm(ctx Context, weight, bias Tensor, eps float32) Tensor
  58. RMSNorm(ctx Context, weight Tensor, eps float32) Tensor
  59. Scale(ctx Context, s float64) Tensor
  60. Conv2D(ctx Context, weight Tensor, s0, s1, p0, p1, d0, d1 int) Tensor
  61. RoPE(ctx Context, positionIDs, ropeFactors Tensor, dim uint32, base, scale float32) Tensor
  62. Tanh(ctx Context) Tensor
  63. GELU(ctx Context) Tensor
  64. SILU(ctx Context) Tensor
  65. Reshape(ctx Context, shape ...int) Tensor
  66. View(ctx Context, offset int, shape ...int) Tensor
  67. Permute(ctx Context, shape ...int) Tensor
  68. Contiguous(ctx Context) Tensor
  69. Pad(ctx Context, shape ...int) Tensor
  70. Unpad(ctx Context, shape ...int) Tensor
  71. Stack(ctx Context, dim int, s ...Tensor) Tensor
  72. Concat(ctx Context, t2 Tensor, dim int) Tensor
  73. Rows(ctx Context, t2 Tensor) Tensor
  74. Copy(ctx Context, t2 Tensor) Tensor
  75. }
  76. type number interface {
  77. ~int | ~int8 | ~int16 | ~int32 | ~int64 |
  78. ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
  79. ~float32 | ~float64 |
  80. ~complex64 | ~complex128
  81. }
  82. func mul[T number](s ...T) T {
  83. p := T(1)
  84. for _, v := range s {
  85. p *= v
  86. }
  87. return p
  88. }
  89. type DumpOptions struct {
  90. // Items is the number of elements to print at the beginning and end of each dimension.
  91. Items int
  92. // Precision is the number of decimal places to print. Applies to float32 and float64.
  93. Precision int
  94. }
  95. func Dump(ctx Context, t Tensor, opts ...DumpOptions) string {
  96. if len(opts) < 1 {
  97. opts = append(opts, DumpOptions{
  98. Items: 3,
  99. Precision: 4,
  100. })
  101. }
  102. switch t.DType() {
  103. case DTypeF32:
  104. return dump[[]float32](ctx, t, opts[0].Items, func(f float32) string {
  105. return strconv.FormatFloat(float64(f), 'f', opts[0].Precision, 32)
  106. })
  107. case DTypeF16:
  108. f32 := ctx.Zeros(DTypeF32, t.Shape()...)
  109. f32 = t.Copy(ctx, f32)
  110. return dump[[]float32](ctx, f32, opts[0].Items, func(f float32) string {
  111. return strconv.FormatFloat(float64(f), 'f', opts[0].Precision, 32)
  112. })
  113. case DTypeI32:
  114. return dump[[]int32](ctx, t, opts[0].Items, func(i int32) string {
  115. return strconv.FormatInt(int64(i), 10)
  116. })
  117. default:
  118. return "<unsupported>"
  119. }
  120. }
  121. func dump[S ~[]E, E number](ctx Context, t Tensor, items int, fn func(E) string) string {
  122. if t.Bytes() == nil {
  123. ctx.Forward(t)
  124. ctx.Compute(t)
  125. }
  126. s := make(S, mul(t.Shape()...))
  127. if err := binary.Read(bytes.NewBuffer(t.Bytes()), binary.LittleEndian, &s); err != nil {
  128. panic(err)
  129. }
  130. shape := t.Shape()
  131. var sb strings.Builder
  132. var f func([]int, int)
  133. f = func(dims []int, stride int) {
  134. prefix := strings.Repeat(" ", len(shape)-len(dims)+1)
  135. fmt.Fprint(&sb, "[")
  136. defer func() { fmt.Fprint(&sb, "]") }()
  137. for i := 0; i < dims[0]; i++ {
  138. if i >= items && i < dims[0]-items {
  139. fmt.Fprint(&sb, "..., ")
  140. // skip to next printable element
  141. skip := dims[0] - 2*items
  142. if len(dims) > 1 {
  143. stride += mul(append(dims[1:], skip)...)
  144. fmt.Fprint(&sb, strings.Repeat("\n", len(dims)-1), prefix)
  145. }
  146. i += skip - 1
  147. } else if len(dims) > 1 {
  148. f(dims[1:], stride)
  149. stride += mul(dims[1:]...)
  150. if i < dims[0]-1 {
  151. fmt.Fprint(&sb, ",", strings.Repeat("\n", len(dims)-1), prefix)
  152. }
  153. } else {
  154. fmt.Fprint(&sb, fn(s[stride+i]))
  155. if i < dims[0]-1 {
  156. fmt.Fprint(&sb, ", ")
  157. }
  158. }
  159. }
  160. }
  161. f(shape, 0)
  162. return sb.String()
  163. }
  164. type DType int
  165. const (
  166. DTypeOther DType = iota
  167. DTypeF32
  168. DTypeF16
  169. DTypeI32
  170. )