ggla.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package llm
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "io"
  6. "slices"
  7. )
  8. type containerGGLA struct {
  9. version uint32
  10. }
  11. func (c *containerGGLA) Name() string {
  12. return "ggla"
  13. }
  14. func (c *containerGGLA) Decode(rs io.ReadSeeker) (model, error) {
  15. if err := binary.Read(rs, binary.LittleEndian, &c.version); err != nil {
  16. return nil, err
  17. }
  18. switch c.version {
  19. case 1:
  20. default:
  21. return nil, errors.New("invalid version")
  22. }
  23. model := newGGLA(c)
  24. err := model.decode(rs)
  25. return model, err
  26. }
  27. type ggla struct {
  28. *containerGGLA
  29. kv KV
  30. tensors []*Tensor
  31. }
  32. func newGGLA(container *containerGGLA) *ggla {
  33. return &ggla{
  34. containerGGLA: container,
  35. kv: make(KV),
  36. }
  37. }
  38. func (llm *ggla) KV() KV {
  39. return llm.kv
  40. }
  41. func (llm *ggla) Tensors() Tensors {
  42. return llm.tensors
  43. }
  44. func (llm *ggla) decode(rs io.ReadSeeker) (retErr error) {
  45. var r uint32
  46. if err := binary.Read(rs, binary.LittleEndian, &r); err != nil {
  47. return err
  48. }
  49. llm.kv["r"] = r
  50. var alpha uint32
  51. if err := binary.Read(rs, binary.LittleEndian, &alpha); err != nil {
  52. return err
  53. }
  54. llm.kv["alpha"] = alpha
  55. for {
  56. var dims uint32
  57. if err := binary.Read(rs, binary.LittleEndian, &dims); err != nil {
  58. if errors.Is(err, io.EOF) {
  59. return nil
  60. }
  61. return err
  62. }
  63. defer func() {
  64. if errors.Is(retErr, io.EOF) {
  65. retErr = io.ErrUnexpectedEOF
  66. }
  67. }()
  68. var namesize uint32
  69. if err := binary.Read(rs, binary.LittleEndian, &namesize); err != nil {
  70. return err
  71. }
  72. var t Tensor
  73. if err := binary.Read(rs, binary.LittleEndian, &t.Kind); err != nil {
  74. return err
  75. }
  76. t.Shape = make([]uint64, dims)
  77. for i := 0; uint32(i) < dims; i++ {
  78. var shape32 uint32
  79. if err := binary.Read(rs, binary.LittleEndian, &shape32); err != nil {
  80. return err
  81. }
  82. t.Shape[i] = uint64(shape32)
  83. }
  84. // ggla tensor shape is reversed
  85. // ref: https://github.com/ggerganov/llama.cpp/blob/29ae62d2ae163e2b68aa0ad3bf2ab4636de0c957/convert-lora-to-ggml.py#L44
  86. slices.Reverse(t.Shape)
  87. name := make([]byte, namesize)
  88. if err := binary.Read(rs, binary.LittleEndian, &name); err != nil {
  89. return err
  90. }
  91. t.Name = string(name)
  92. offset, err := rs.Seek(0, io.SeekCurrent)
  93. if err != nil {
  94. return err
  95. }
  96. if _, err := rs.Seek((offset+31)&-32-offset, io.SeekCurrent); err != nil {
  97. return err
  98. }
  99. offset, err = rs.Seek(0, io.SeekCurrent)
  100. if err != nil {
  101. return err
  102. }
  103. t.Offset = uint64(offset)
  104. if _, err := rs.Seek(int64(t.Size()), io.SeekCurrent); err != nil {
  105. return err
  106. }
  107. llm.tensors = append(llm.tensors, &t)
  108. }
  109. }