Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/ggml/blob/master/src/ggml-cpu/amx/amx.cpp # ggml [Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `ggml / src / ggml-cpu / amx / amx.cpp` 254 lines ยท 8509 bytes Raw Blame History 1 #include "amx.h" 2 #include "common.h" 3 #include "mmq.h" 4 #include "ggml-backend-impl.h" 5 #include "ggml-backend.h" 6 #include "ggml-impl.h" 7 #include "ggml-cpu.h" 8 #include "traits.h" 9 10 #if defined(__linux__) 11 #include 12 #include 13 #endif 14 15 #include 16 #include 17 #include 18 19 #if defined(__AMX_INT8__) && defined(__AVX512VNNI__) 20 21 // AMX type_trais 22 namespace ggml::cpu::amx { 23 class tensor_traits : public ggml::cpu::tensor_traits { 24 bool work_size(int /* n_threads */, const struct ggml_tensor * op, size_t & size) override { 25 size = ggml_backend_amx_desired_wsize(op); 26 return true; 27 } 28 29 bool compute_forward(struct ggml_compute_params * params, struct ggml_tensor * op) override { 30 if (op->op == GGML_OP_MUL_MAT) { 31 ggml_backend_amx_mul_mat(params, op); 32 return true; 33 } 34 return false; 35 } 36 }; 37 38 static ggml::cpu::tensor_traits * get_tensor_traits(ggml_backend_buffer_t, struct ggml_tensor *) { 39 static tensor_traits traits; 40 return &traits; 41 } 42 } // namespace ggml::cpu::amx 43 44 // AMX buffer interface 45 static void ggml_backend_amx_buffer_free_buffer(ggml_backend_buffer_t buffer) { 46 free(buffer->context); 47 } 48 49 static void * ggml_backend_amx_buffer_get_base(ggml_backend_buffer_t buffer) { 50 return (void *) (buffer->context); 51 } 52 53 static enum ggml_status ggml_backend_amx_buffer_init_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor) { 54 tensor->extra = (void *) ggml::cpu::amx::get_tensor_traits(buffer, tensor); 55 56 GGML_UNUSED(buffer); 57 return GGML_STATUS_SUCCESS; 58 } 59 60 static void ggml_backend_amx_buffer_memset_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, 61 uint8_t value, size_t offset, size_t size) { 62 memset((char *) tensor->data + offset, value, size); 63 64 GGML_UNUSED(buffer); 65 } 66 67 static void ggml_backend_amx_buffer_set_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, 68 const void * data, size_t offset, size_t size) { 69 if (qtype_has_amx_kernels(tensor->type)) { 70 GGML_LOG_DEBUG("%s: amx repack tensor %s of type %s\n", __func__, tensor->name, ggml_type_name(tensor->type)); 71 ggml_backend_amx_convert_weight(tensor, data, offset, size); 72 } else { 73 memcpy((char *) tensor->data + offset, data, size); 74 } 75 76 GGML_UNUSED(buffer); 77 } 78 79 /* 80 // need to figure what we need to do with buffer->extra. 81 static void ggml_backend_amx_buffer_get_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * tensor, void * data, size_t offset, size_t size) { 82 GGML_ASSERT(!qtype_has_amx_kernels(tensor->type)); 83 memcpy(data, (const char *)tensor->data + offset, size); 84 85 GGML_UNUSED(buffer); 86 } 87 88 static bool ggml_backend_amx_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) { 89 if (ggml_backend_buffer_is_host(src->buffer)) { 90 if (qtype_has_amx_kernels(src->type)) { 91 ggml_backend_amx_convert_weight(dst, src->data, 0, ggml_nbytes(dst)); 92 } else { 93 memcpy(dst->data, src->data, ggml_nbytes(src)); 94 } 95 return true; 96 } 97 return false; 98 99 GGML_UNUSED(buffer); 100 } 101 */ 102 103 static void ggml_backend_amx_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) { 104 memset(buffer->context, value, buffer->size); 105 } 106 107 static ggml_backend_buffer_i ggml_backend_amx_buffer_interface = { 108 /* .free_buffer = */ ggml_backend_amx_buffer_free_buffer, 109 /* .get_base = */ ggml_backend_amx_buffer_get_base, 110 /* .init_tensor = */ ggml_backend_amx_buffer_init_tensor, 111 /* .memset_tensor = */ ggml_backend_amx_buffer_memset_tensor, 112 /* .set_tensor = */ ggml_backend_amx_buffer_set_tensor, 113 /* .get_tensor = */ nullptr, 114 /* .set_tensor_2d = */ nullptr, 115 /* .get_tensor_2d = */ nullptr, 116 /* .cpy_tensor = */ nullptr, 117 /* .clear = */ ggml_backend_amx_buffer_clear, 118 /* .reset = */ nullptr, 119 /* .set_tensor_n = */ NULL, 120 /* .get_tensor_n = */ NULL, 121 /* .cpy_tensor_n = */ NULL, 122 }; 123 124 static const char * ggml_backend_amx_buffer_type_get_name(ggml_backend_buffer_type_t buft) { 125 return "AMX"; 126 127 GGML_UNUSED(buft); 128 } 129 130 static ggml_backend_buffer_t ggml_backend_amx_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) { 131 void * data = ggml_aligned_malloc(size); 132 if (data == NULL) { 133 fprintf(stderr, "%s: failed to allocate buffer of size %zu\n", __func__, size); 134 return NULL; 135 } 136 137 return ggml_backend_buffer_init(buft, ggml_backend_amx_buffer_interface, data, size); 138 } 139 140 static size_t ggml_backend_amx_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) { 141 return TENSOR_ALIGNMENT; 142 143 GGML_UNUSED(buft); 144 } 145 146 namespace ggml::cpu::amx { 147 class extra_buffer_type : ggml::cpu::extra_buffer_type { 148 bool supports_op(ggml_backend_dev_t, const struct ggml_tensor * op) override { 149 if (op->op != GGML_OP_MUL_MAT) { 150 return false; 151 } 152 auto * src0 = op->src[0]; 153 auto * src1 = op->src[1]; 154 155 if (!ggml_is_contiguous(src0) || !ggml_is_contiguous(src1)) { 156 return false; 157 } 158 if (!src0->buffer || src0->buffer->buft != ggml_backend_amx_buffer_type()) { 159 return false; 160 } 161 if (src1->buffer && !ggml_backend_buft_is_host(src1->buffer->buft)) { 162 return false; 163 } 164 if (op->ne[0] % (TILE_N * 2)) { 165 return false; 166 } 167 int alignment; 168 switch (src0->type) { 169 case GGML_TYPE_Q4_0: 170 case GGML_TYPE_Q4_1: 171 case GGML_TYPE_Q8_0: 172 alignment = TILE_K; 173 break; 174 case GGML_TYPE_Q4_K: 175 case GGML_TYPE_Q5_K: 176 case GGML_TYPE_Q6_K: 177 case GGML_TYPE_IQ4_XS: 178 alignment = 256; // QK_K 179 break; 180 case GGML_TYPE_F16: 181 alignment = 16; 182 break; 183 default: 184 return false; 185 } 186 if (src0->ne[0] % alignment) { 187 return false; 188 } 189 if (src1->type != GGML_TYPE_F32) { 190 return false; 191 } 192 return true; 193 } 194 195 ggml::cpu::tensor_traits * get_tensor_traits(const struct ggml_tensor * op) override { 196 if (op->op == GGML_OP_MUL_MAT && op->src[0]->buffer && 197 op->src[0]->buffer->buft == ggml_backend_amx_buffer_type()) { 198 return (ggml::cpu::tensor_traits *) op->src[0]->extra; 199 } 200 201 return nullptr; 202 } 203 }; 204 } // namespace ggml::cpu::amx 205 206 static size_t ggml_backend_amx_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor * tensor) { 207 return ggml_backend_amx_get_alloc_size(tensor); 208 209 GGML_UNUSED(buft); 210 } 211 212 #define ARCH_GET_XCOMP_PERM 0x1022 213 #define ARCH_REQ_XCOMP_PERM 0x1023 214 #define XFEATURE_XTILECFG 17 215 #define XFEATURE_XTILEDATA 18 216 217 static bool ggml_amx_init() { 218 #if defined(__linux__) 219 if (syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA)) { 220 fprintf(stderr, "AMX is not ready to be used!\n"); 221 return false; 222 } 223 return true; 224 #elif defined(_WIN32) 225 return true; 226 #else 227 return false; 228 #endif 229 } 230 231 ggml_backend_buffer_type_t ggml_backend_amx_buffer_type() { 232 static struct ggml_backend_buffer_type ggml_backend_buffer_type_amx = { 233 /* .iface = */ { 234 /* .get_name = */ ggml_backend_amx_buffer_type_get_name, 235 /* .alloc_buffer = */ ggml_backend_amx_buffer_type_alloc_buffer, 236 /* .alloc_buffer_n = */ nullptr, 237 /* .get_alignment = */ ggml_backend_amx_buffer_type_get_alignment, 238 /* .get_max_size = */ nullptr, // defaults to SIZE_MAX 239 /* .get_alloc_size = */ ggml_backend_amx_buffer_type_get_alloc_size, 240 /* .get_alloc_size_n = */ NULL, 241 /* .is_host = */ nullptr, 242 }, 243 /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cpu_reg(), 0), 244 /* .context = */ new ggml::cpu::amx::extra_buffer_type(), 245 }; 246 247 if (!ggml_amx_init()) { 248 return nullptr; 249 } 250 251 return &ggml_backend_buffer_type_amx; 252 } 253 254 #endif // defined(__AMX_INT8__) && defined(__AVX512VNNI__)