|
| 1 | +// Copyright © 2023-2024 Apple Inc. |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <cstring> |
| 5 | + |
| 6 | +#include <mlx/io/gguf.h> |
| 7 | + |
| 8 | +namespace mlx::core { |
| 9 | + |
| 10 | +void unpack_32_4(uint8_t* data, int8_t* dst) { |
| 11 | + for (int64_t j = 0; j < 16; ++j) { |
| 12 | + uint8_t x = (data[j + 2] & 0x0F); // j+2 to skip scale bytes. |
| 13 | + if (j % 2 != 0) { |
| 14 | + x <<= 4; |
| 15 | + } |
| 16 | + dst[j / 2] += x; |
| 17 | + } |
| 18 | + // Last 16 weights are in the higher bits |
| 19 | + for (int64_t j = 0; j < 16; ++j) { |
| 20 | + uint8_t x = (data[j + 2] >> 4); |
| 21 | + if (j % 2 != 0) { |
| 22 | + x <<= 4; |
| 23 | + } |
| 24 | + dst[8 + j / 2] += x; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Extracts (weight, scales, biases) from Q4_0 tensors. |
| 29 | +// Data layout is: |16 bit scale|32 x 4bit weights|. |
| 30 | +void extract_q4_0_data( |
| 31 | + const gguf_tensor& tensor, |
| 32 | + array& weights_arr, |
| 33 | + array& scales_arr, |
| 34 | + array& biases_arr) { |
| 35 | + const uint64_t bytes_per_block = 18; // 2 bytes scale, 32x0.5 byte weights |
| 36 | + auto data = static_cast<uint8_t*>(tensor.weights_data); |
| 37 | + auto weights = weights_arr.data<int8_t>(); |
| 38 | + auto scales = scales_arr.data<float16_t>(); |
| 39 | + auto biases = biases_arr.data<float16_t>(); |
| 40 | + for (int64_t i = 0; i < scales_arr.size(); i++) { |
| 41 | + scales[i] = *((float16_t*)data); |
| 42 | + biases[i] = -8 * scales[i]; |
| 43 | + unpack_32_4(data, weights); |
| 44 | + weights += 16; |
| 45 | + data += bytes_per_block; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// Extracts (weight, scales, biases) from Q4_1 tensors. |
| 50 | +// Data layout is: |16 bit scale|16 bit bias|32 x 4bit weights|. |
| 51 | +void extract_q4_1_data( |
| 52 | + const gguf_tensor& tensor, |
| 53 | + array& weights_arr, |
| 54 | + array& scales_arr, |
| 55 | + array& biases_arr) { |
| 56 | + const uint64_t bytes_per_block = |
| 57 | + 20; // 2 bytes scale, 2 bytes bias, 32x0.5 byte weights |
| 58 | + auto data = static_cast<uint8_t*>(tensor.weights_data); |
| 59 | + auto weights = weights_arr.data<int8_t>(); |
| 60 | + auto scales = scales_arr.data<float16_t>(); |
| 61 | + auto biases = biases_arr.data<float16_t>(); |
| 62 | + for (int64_t i = 0; i < scales_arr.size(); i++) { |
| 63 | + scales[i] = *((float16_t*)data); |
| 64 | + biases[i] = *((float16_t*)(data) + 1); |
| 65 | + unpack_32_4(data, weights); |
| 66 | + weights += 16; |
| 67 | + data += bytes_per_block; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +// Extracts (weight, scales, biases) from Q8_0 tensors. |
| 72 | +// Data layout is: |16 bit scale|32 x 8bit weights|. |
| 73 | +void extract_q8_0_data( |
| 74 | + const gguf_tensor& tensor, |
| 75 | + array& weights_arr, |
| 76 | + array& scales_arr, |
| 77 | + array& biases_arr) { |
| 78 | + const uint64_t weights_per_block = 32; |
| 79 | + const uint64_t bytes_per_block = 34; // 2 bytes scale, 32x1 byte weights |
| 80 | + auto data = static_cast<uint8_t*>(tensor.weights_data); |
| 81 | + auto weights = weights_arr.data<int8_t>(); |
| 82 | + auto scales = scales_arr.data<float16_t>(); |
| 83 | + auto biases = biases_arr.data<float16_t>(); |
| 84 | + for (int64_t i = 0; i < scales_arr.size(); i++) { |
| 85 | + uint8_t* block_data = data + i * bytes_per_block; |
| 86 | + scales[i] = *((float16_t*)block_data); |
| 87 | + biases[i] = -128 * scales[i]; |
| 88 | + for (int64_t j = 0; j < weights_per_block; ++j) { |
| 89 | + uint8_t x = block_data[j + 2]; // j+2 to skip the scale bytes. |
| 90 | + // Original data is in int8_t, so we add a bias of -128 and invert the |
| 91 | + // first bit. |
| 92 | + x ^= 1 << 7; |
| 93 | + weights[i * weights_per_block + j] = x; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void gguf_load_quantized( |
| 99 | + std::unordered_map<std::string, array>& a, |
| 100 | + const gguf_tensor& tensor) { |
| 101 | + uint64_t weights_per_byte; |
| 102 | + if (tensor.type == GGUF_TYPE_Q4_0 || tensor.type == GGUF_TYPE_Q4_1) { |
| 103 | + weights_per_byte = 2; |
| 104 | + } else { // tensor.type == GGUF_TYPE_Q8_0 |
| 105 | + weights_per_byte = 1; |
| 106 | + } |
| 107 | + |
| 108 | + std::string name = std::string(tensor.name, tensor.namelen); |
| 109 | + std::vector<int> shape = get_shape(tensor); |
| 110 | + const uint64_t weights_per_block = 32; |
| 111 | + if (shape[shape.size() - 1] % weights_per_block != 0) { |
| 112 | + std::ostringstream msg; |
| 113 | + msg << "[load_gguf] tensor " << name |
| 114 | + << "has incompatible last dim shape: " << shape[shape.size() - 1]; |
| 115 | + throw std::runtime_error(msg.str()); |
| 116 | + } |
| 117 | + const uint64_t num_blocks = tensor.num_weights / weights_per_block; |
| 118 | + |
| 119 | + std::vector<int> weights_shape = shape; |
| 120 | + weights_shape.back() /= (weights_per_byte * 4); |
| 121 | + |
| 122 | + array weights(std::move(weights_shape), uint32, nullptr, {}); |
| 123 | + weights.set_data(allocator::malloc(weights.nbytes())); |
| 124 | + |
| 125 | + // For scales and bias |
| 126 | + shape[shape.size() - 1] = shape[shape.size() - 1] / weights_per_block; |
| 127 | + array scales(shape, float16, nullptr, {}); |
| 128 | + array biases(std::move(shape), float16, nullptr, {}); |
| 129 | + scales.set_data(allocator::malloc(scales.nbytes())); |
| 130 | + biases.set_data(allocator::malloc(biases.nbytes())); |
| 131 | + |
| 132 | + if (tensor.type == GGUF_TYPE_Q4_0) { |
| 133 | + extract_q4_0_data(tensor, weights, scales, biases); |
| 134 | + } else if (tensor.type == GGUF_TYPE_Q4_1) { |
| 135 | + extract_q4_1_data(tensor, weights, scales, biases); |
| 136 | + } else if (tensor.type == GGUF_TYPE_Q8_0) { |
| 137 | + extract_q8_0_data(tensor, weights, scales, biases); |
| 138 | + } |
| 139 | + |
| 140 | + a.insert({name, weights}); |
| 141 | + |
| 142 | + auto check_insert = [](auto inserted) { |
| 143 | + if (!inserted.second) { |
| 144 | + std::ostringstream msg; |
| 145 | + msg << "[load_gguf] Duplicate parameter name " << inserted.first->second |
| 146 | + << " this can happend when loading quantized tensors."; |
| 147 | + throw std::runtime_error(msg.str()); |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + const std::string weight_suffix = ".weight"; |
| 152 | + const std::string name_prefix = |
| 153 | + name.substr(0, name.length() - weight_suffix.length()); |
| 154 | + check_insert(a.insert({name_prefix + ".scales", scales})); |
| 155 | + check_insert(a.insert({name_prefix + ".biases", biases})); |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace mlx::core |
0 commit comments