1
0

format.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Package loader handles MAK file format reading and writing
  2. package loader
  3. // MAK Format Constants
  4. const (
  5. Magic = "MAKA"
  6. Version = 1
  7. Alignment = 32 // Default data alignment
  8. )
  9. // Header is the fixed-size header at the start of MAK files (32 bytes)
  10. type Header struct {
  11. Magic [4]byte // "MAK2"
  12. Version uint16 // Format version (2)
  13. Flags uint16 // Bit flags
  14. SectionCount uint32 // Number of sections
  15. Alignment uint32 // Data alignment
  16. Reserved [16]byte // Future use
  17. }
  18. // Header flags
  19. const (
  20. FlagLittleEndian uint16 = 0x0001
  21. FlagBigEndian uint16 = 0x0002
  22. )
  23. // SectionType identifies different sections in the file
  24. type SectionType uint32
  25. const (
  26. SectionMetadata SectionType = 1 // Model config JSON
  27. SectionTensorIndex SectionType = 2 // Tensor registry
  28. SectionTensorData SectionType = 3 // Raw tensor bytes
  29. SectionVisionEnc SectionType = 10 // Vision encoder weights
  30. SectionAudioEnc SectionType = 11 // Audio encoder (future)
  31. SectionTokenizer SectionType = 20 // Embedded tokenizer
  32. SectionLoRA SectionType = 30 // LoRA adapter weights
  33. )
  34. // SectionEntry describes a section in the section table
  35. type SectionEntry struct {
  36. Type SectionType // Section type
  37. Offset uint64 // Offset from file start
  38. Size uint64 // Section size in bytes
  39. Flags uint32 // Section-specific flags
  40. }
  41. // DType represents tensor data types including quantization
  42. type DType uint16
  43. const (
  44. // Float types
  45. F32 DType = 0
  46. F16 DType = 1
  47. BF16 DType = 2
  48. F64 DType = 3
  49. // Integer types
  50. I8 DType = 5
  51. I16 DType = 6
  52. I32 DType = 7
  53. I64 DType = 8
  54. // K-quants (llama.cpp style)
  55. Q4_K DType = 22
  56. Q3_K DType = 23
  57. Q5_K DType = 24
  58. Q6_K DType = 26
  59. Q8_K DType = 27
  60. Q2_K DType = 28
  61. )
  62. // DTypeInfo provides metadata about each data type
  63. type DTypeInfo struct {
  64. Name string
  65. BytesPerEl float32 // May be fractional for sub-byte quants
  66. BlockSize int // Elements per quant block (0 = no blocking)
  67. }
  68. var dtypeInfos = map[DType]DTypeInfo{
  69. F32: {"F32", 4, 0},
  70. F16: {"F16", 2, 0},
  71. BF16: {"BF16", 2, 0},
  72. F64: {"F64", 8, 0},
  73. I8: {"I8", 1, 0},
  74. I16: {"I16", 2, 0},
  75. I32: {"I32", 4, 0},
  76. I64: {"I64", 8, 0},
  77. Q4_K: {"Q4_K", 0.5625, 256}, // 144 bytes per 256 elements
  78. Q3_K: {"Q3_K", 0.4296875, 256}, // 110 bytes per 256 elements
  79. Q5_K: {"Q5_K", 0.6875, 256}, // 176 bytes per 256 elements
  80. Q6_K: {"Q6_K", 0.8203125, 256}, // 210 bytes per 256 elements
  81. Q8_K: {"Q8_K", 1.140625, 256}, // 292 bytes per 256 elements
  82. Q2_K: {"Q2_K", 0.328125, 256}, // 84 bytes per 256 elements
  83. }
  84. // Info returns metadata about a DType
  85. func (d DType) Info() DTypeInfo {
  86. if info, ok := dtypeInfos[d]; ok {
  87. return info
  88. }
  89. return DTypeInfo{Name: "unknown", BytesPerEl: 0}
  90. }
  91. // String returns the name of the DType
  92. func (d DType) String() string {
  93. return d.Info().Name
  94. }
  95. // TensorEntry describes a tensor in the tensor registry
  96. type TensorEntry struct {
  97. Name string
  98. DType DType
  99. Shape []uint64
  100. Offset uint64 // Offset within tensor data section
  101. Size uint64 // Size in bytes
  102. QuantMeta []byte // Optional quantization metadata
  103. }
  104. // Metadata contains model configuration as JSON
  105. type Metadata struct {
  106. ModelConfig ModelConfig `json:"model_config"`
  107. Tensors map[string]TensorEntry `json:"tensors,omitempty"` // Legacy compatibility
  108. }
  109. // ModelConfig holds model architecture and parameters
  110. type ModelConfig struct {
  111. Architecture string `json:"architecture"`
  112. Params map[string]interface{} `json:"params"`
  113. }