backend.go 4.6 KB

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