Spaces:
Running
Running
ggml : add AMX backend (llama/8998)
Browse files- ggml/include/ggml-amx.h +25 -0
- ggml/src/ggml-amx.cpp +453 -0
- ggml/src/ggml-amx/common.h +93 -0
- ggml/src/ggml-amx/mmq.cpp +0 -0
- ggml/src/ggml-amx/mmq.h +17 -0
ggml/include/ggml-amx.h
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include "ggml.h"
|
| 4 |
+
#include "ggml-backend.h"
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
#ifdef __cplusplus
|
| 8 |
+
extern "C" {
|
| 9 |
+
#endif
|
| 10 |
+
|
| 11 |
+
// buffer_type API
|
| 12 |
+
GGML_API ggml_backend_buffer_type_t ggml_backend_amx_buffer_type(void);
|
| 13 |
+
|
| 14 |
+
GGML_API bool ggml_backend_is_amx(ggml_backend_t backend);
|
| 15 |
+
|
| 16 |
+
// backend API
|
| 17 |
+
GGML_API ggml_backend_t ggml_backend_amx_init(void);
|
| 18 |
+
|
| 19 |
+
GGML_API void ggml_backend_amx_set_n_threads(ggml_backend_t backend_amx, int n_threads);
|
| 20 |
+
|
| 21 |
+
GGML_API ggml_backend_reg_t ggml_backend_amx_reg(void);
|
| 22 |
+
|
| 23 |
+
#ifdef __cplusplus
|
| 24 |
+
}
|
| 25 |
+
#endif
|
ggml/src/ggml-amx.cpp
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#include "ggml-amx.h"
|
| 2 |
+
#include "ggml-amx/common.h"
|
| 3 |
+
#include "ggml-amx/mmq.h"
|
| 4 |
+
#include "ggml-backend-impl.h"
|
| 5 |
+
#include "ggml-impl.h"
|
| 6 |
+
|
| 7 |
+
#if defined(__gnu_linux__)
|
| 8 |
+
#include <sys/syscall.h>
|
| 9 |
+
#include <unistd.h>
|
| 10 |
+
#endif
|
| 11 |
+
|
| 12 |
+
#include <cstdlib>
|
| 13 |
+
#include <cstring>
|
| 14 |
+
#include <memory>
|
| 15 |
+
|
| 16 |
+
#if defined(__AMX_INT8__)
|
| 17 |
+
|
| 18 |
+
// AMX buffer interface
|
| 19 |
+
static const char * ggml_backend_amx_buffer_get_name(ggml_backend_buffer_t buffer) {
|
| 20 |
+
return "AMX";
|
| 21 |
+
|
| 22 |
+
GGML_UNUSED(buffer);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
static void ggml_backend_amx_buffer_free_buffer(ggml_backend_buffer_t buffer) {
|
| 26 |
+
free(buffer->context);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
static void * ggml_backend_amx_buffer_get_base(ggml_backend_buffer_t buffer) {
|
| 30 |
+
return (void *)(buffer->context);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
static void ggml_backend_amx_buffer_memset_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) {
|
| 34 |
+
memset((char *)tensor->data + offset, value, size);
|
| 35 |
+
|
| 36 |
+
GGML_UNUSED(buffer);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
static void ggml_backend_amx_buffer_set_tensor(ggml_backend_buffer_t buffer, struct ggml_tensor * tensor, const void * data, size_t offset, size_t size) {
|
| 40 |
+
if (qtype_has_amx_kernels(tensor->type)) {
|
| 41 |
+
ggml_backend_amx_convert_weight(tensor, data, offset, size);
|
| 42 |
+
} else {
|
| 43 |
+
memcpy((char *)tensor->data + offset, data, size);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
GGML_UNUSED(buffer);
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
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) {
|
| 50 |
+
GGML_ASSERT(!qtype_has_amx_kernels(tensor->type));
|
| 51 |
+
memcpy(data, (const char *)tensor->data + offset, size);
|
| 52 |
+
|
| 53 |
+
GGML_UNUSED(buffer);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
static bool ggml_backend_amx_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
|
| 57 |
+
if (ggml_backend_buffer_is_host(src->buffer)) {
|
| 58 |
+
if (qtype_has_amx_kernels(src->type)) {
|
| 59 |
+
ggml_backend_amx_convert_weight(dst, src->data, 0, ggml_backend_amx_get_alloc_size(dst));
|
| 60 |
+
} else {
|
| 61 |
+
memcpy(dst->data, src->data, ggml_nbytes(src));
|
| 62 |
+
}
|
| 63 |
+
return true;
|
| 64 |
+
}
|
| 65 |
+
return false;
|
| 66 |
+
|
| 67 |
+
GGML_UNUSED(buffer);
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
static void ggml_backend_amx_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
|
| 71 |
+
memset(buffer->context, value, buffer->size);
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
static ggml_backend_buffer_i ggml_backend_amx_buffer_interface = {
|
| 75 |
+
/* .get_name = */ ggml_backend_amx_buffer_get_name,
|
| 76 |
+
/* .free_buffer = */ ggml_backend_amx_buffer_free_buffer,
|
| 77 |
+
/* .get_base = */ ggml_backend_amx_buffer_get_base,
|
| 78 |
+
/* .init_tensor = */ NULL, // no initialization required
|
| 79 |
+
/* .memset_tensor = */ ggml_backend_amx_buffer_memset_tensor,
|
| 80 |
+
/* .set_tensor = */ ggml_backend_amx_buffer_set_tensor,
|
| 81 |
+
/* .get_tensor = */ ggml_backend_amx_buffer_get_tensor,
|
| 82 |
+
/* .cpy_tensor = */ ggml_backend_amx_buffer_cpy_tensor,
|
| 83 |
+
/* .clear = */ ggml_backend_amx_buffer_clear,
|
| 84 |
+
/* .reset = */ NULL,
|
| 85 |
+
};
|
| 86 |
+
|
| 87 |
+
static const char * ggml_backend_amx_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
|
| 88 |
+
return "AMX";
|
| 89 |
+
|
| 90 |
+
GGML_UNUSED(buft);
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
static ggml_backend_buffer_t ggml_backend_amx_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
|
| 94 |
+
void * data = aligned_alloc(TENSOR_ALIGNMENT, size);
|
| 95 |
+
if (data == NULL) {
|
| 96 |
+
fprintf(stderr, "%s: failed to allocate buffer of size %zu\n", __func__, size);
|
| 97 |
+
return NULL;
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
return ggml_backend_buffer_init(buft, ggml_backend_amx_buffer_interface, data, size);
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
static size_t ggml_backend_amx_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
|
| 104 |
+
return TENSOR_ALIGNMENT;
|
| 105 |
+
|
| 106 |
+
GGML_UNUSED(buft);
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
static size_t ggml_backend_amx_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor* tensor) {
|
| 110 |
+
return ggml_backend_amx_get_alloc_size(tensor);
|
| 111 |
+
|
| 112 |
+
GGML_UNUSED(buft);
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
static bool ggml_backend_amx_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
|
| 116 |
+
return false;
|
| 117 |
+
|
| 118 |
+
GGML_UNUSED(buft);
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
ggml_backend_buffer_type_t ggml_backend_amx_buffer_type() {
|
| 122 |
+
static struct ggml_backend_buffer_type ggml_backend_buffer_type_amx = {
|
| 123 |
+
/* .iface = */ {
|
| 124 |
+
/* .get_name = */ ggml_backend_amx_buffer_type_get_name,
|
| 125 |
+
/* .alloc_buffer = */ ggml_backend_amx_buffer_type_alloc_buffer,
|
| 126 |
+
/* .get_alignment = */ ggml_backend_amx_buffer_type_get_alignment,
|
| 127 |
+
/* .get_max_size = */ NULL, // defaults to SIZE_MAX
|
| 128 |
+
/* .get_alloc_size = */ ggml_backend_amx_buffer_type_get_alloc_size,
|
| 129 |
+
/* .is_host = */ ggml_backend_amx_buffer_type_is_host,
|
| 130 |
+
},
|
| 131 |
+
/* .device = */ NULL,
|
| 132 |
+
/* .context = */ NULL,
|
| 133 |
+
};
|
| 134 |
+
|
| 135 |
+
return &ggml_backend_buffer_type_amx;
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
// backend interface
|
| 139 |
+
|
| 140 |
+
static const char * ggml_backend_amx_name(ggml_backend_t backend) {
|
| 141 |
+
return "AMX";
|
| 142 |
+
|
| 143 |
+
GGML_UNUSED(backend);
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
static void ggml_backend_amx_free(ggml_backend_t backend) {
|
| 147 |
+
ggml_backend_amx_context * ctx = (ggml_backend_amx_context *)backend->context;
|
| 148 |
+
delete ctx;
|
| 149 |
+
delete backend;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
static ggml_backend_buffer_type_t ggml_backend_amx_get_default_buffer_type(ggml_backend_t backend) {
|
| 153 |
+
return ggml_backend_amx_buffer_type();
|
| 154 |
+
|
| 155 |
+
GGML_UNUSED(backend);
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
static enum ggml_status ggml_backend_amx_graph_compute(ggml_backend_t backend, struct ggml_cgraph * cgraph) {
|
| 159 |
+
ggml_backend_amx_context * ctx = (ggml_backend_amx_context *)backend->context;
|
| 160 |
+
|
| 161 |
+
for (int i = 0; i < cgraph->n_nodes; i++) {
|
| 162 |
+
struct ggml_tensor * node = cgraph->nodes[i];
|
| 163 |
+
|
| 164 |
+
switch (node->op) {
|
| 165 |
+
case GGML_OP_MUL_MAT:
|
| 166 |
+
ggml_backend_amx_mul_mat(ctx, node);
|
| 167 |
+
break;
|
| 168 |
+
|
| 169 |
+
case GGML_OP_NONE:
|
| 170 |
+
case GGML_OP_RESHAPE:
|
| 171 |
+
case GGML_OP_VIEW:
|
| 172 |
+
case GGML_OP_PERMUTE:
|
| 173 |
+
case GGML_OP_TRANSPOSE:
|
| 174 |
+
break;
|
| 175 |
+
|
| 176 |
+
default:
|
| 177 |
+
fprintf(stderr, "%s: unsupported op %s\n", __func__, ggml_op_desc(node));
|
| 178 |
+
GGML_ASSERT(false);
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
return GGML_STATUS_SUCCESS;
|
| 183 |
+
|
| 184 |
+
GGML_UNUSED(backend);
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
static struct ggml_backend_i ggml_backend_amx_i = {
|
| 188 |
+
/* .get_name = */ ggml_backend_amx_name,
|
| 189 |
+
/* .free = */ ggml_backend_amx_free,
|
| 190 |
+
/* .get_default_buffer_type = */ ggml_backend_amx_get_default_buffer_type,
|
| 191 |
+
/* .set_tensor_async = */ NULL,
|
| 192 |
+
/* .get_tensor_async = */ NULL,
|
| 193 |
+
/* .cpy_tensor_async = */ NULL,
|
| 194 |
+
/* .synchronize = */ NULL,
|
| 195 |
+
/* .graph_plan_create = */ NULL,
|
| 196 |
+
/* .graph_plan_free = */ NULL,
|
| 197 |
+
/* .graph_plan_update = */ NULL,
|
| 198 |
+
/* .graph_plan_compute = */ NULL,
|
| 199 |
+
/* .graph_compute = */ ggml_backend_amx_graph_compute,
|
| 200 |
+
/* .supports_op = */ NULL,
|
| 201 |
+
/* .supports_buft = */ NULL,
|
| 202 |
+
/* .offload_op = */ NULL,
|
| 203 |
+
/* .event_record = */ NULL,
|
| 204 |
+
/* .event_wait = */ NULL,
|
| 205 |
+
};
|
| 206 |
+
|
| 207 |
+
static ggml_guid_t ggml_backend_amx_guid() {
|
| 208 |
+
static ggml_guid guid = { 0x13, 0xb8, 0xa4, 0xc4, 0xba, 0xfe, 0x51, 0x67, 0x87, 0x44, 0x55, 0x15, 0xb2, 0x35, 0x62, 0x3e };
|
| 209 |
+
return &guid;
|
| 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(__gnu_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 |
+
#endif
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
ggml_backend_t ggml_backend_amx_init() {
|
| 230 |
+
|
| 231 |
+
// invoke a Linux system call to request access to AMX features
|
| 232 |
+
ggml_amx_init();
|
| 233 |
+
|
| 234 |
+
// backend context
|
| 235 |
+
ggml_backend_amx_context * ctx = new ggml_backend_amx_context;
|
| 236 |
+
|
| 237 |
+
// ggml amx backend
|
| 238 |
+
ggml_backend_t backend = new ggml_backend {
|
| 239 |
+
/* .guid = */ ggml_backend_amx_guid(),
|
| 240 |
+
/* .interface = */ ggml_backend_amx_i,
|
| 241 |
+
/* .device = */ ggml_backend_reg_dev_get(ggml_backend_amx_reg(), 0),
|
| 242 |
+
/* .context = */ ctx,
|
| 243 |
+
};
|
| 244 |
+
|
| 245 |
+
return backend;
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
bool ggml_backend_is_amx(ggml_backend_t backend) {
|
| 249 |
+
return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_amx_guid());
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
void ggml_backend_amx_set_n_threads(ggml_backend_t backend_amx, int n_threads) {
|
| 253 |
+
GGML_ASSERT(ggml_backend_is_amx(backend_amx));
|
| 254 |
+
|
| 255 |
+
ggml_backend_amx_context * ctx = (ggml_backend_amx_context *)backend_amx->context;
|
| 256 |
+
ctx->n_threads = n_threads;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
// device interface
|
| 260 |
+
|
| 261 |
+
static const char * ggml_backend_amx_device_get_name(ggml_backend_dev_t dev) {
|
| 262 |
+
return "AMX";
|
| 263 |
+
|
| 264 |
+
GGML_UNUSED(dev);
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
static const char * ggml_backend_amx_device_get_description(ggml_backend_dev_t dev) {
|
| 268 |
+
return "Intel Advanced Matrix Extensions";
|
| 269 |
+
|
| 270 |
+
GGML_UNUSED(dev);
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
static void ggml_backend_amx_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
|
| 274 |
+
// TODO
|
| 275 |
+
*free = 0;
|
| 276 |
+
*total = 0;
|
| 277 |
+
|
| 278 |
+
GGML_UNUSED(dev);
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
static enum ggml_backend_dev_type ggml_backend_amx_device_get_type(ggml_backend_dev_t dev) {
|
| 282 |
+
return GGML_BACKEND_DEVICE_TYPE_CPU;
|
| 283 |
+
|
| 284 |
+
GGML_UNUSED(dev);
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
static void ggml_backend_amx_device_get_props(ggml_backend_dev_t dev, struct ggml_backend_dev_props * props) {
|
| 288 |
+
props->name = ggml_backend_amx_device_get_name(dev);
|
| 289 |
+
props->description = ggml_backend_amx_device_get_description(dev);
|
| 290 |
+
props->type = ggml_backend_amx_device_get_type(dev);
|
| 291 |
+
ggml_backend_amx_device_get_memory(dev, &props->memory_free, &props->memory_total);
|
| 292 |
+
|
| 293 |
+
// `buffer_from_host_ptr` is intended to be used in mmap, when memory layout unchanged
|
| 294 |
+
props->caps = {
|
| 295 |
+
/* .async = */ false,
|
| 296 |
+
/* .host_buffer = */ false,
|
| 297 |
+
/* .buffer_from_host_ptr = */ false,
|
| 298 |
+
/* .events = */ false,
|
| 299 |
+
};
|
| 300 |
+
}
|
| 301 |
+
|
| 302 |
+
static ggml_backend_t ggml_backend_amx_device_init(ggml_backend_dev_t dev, const char * params) {
|
| 303 |
+
return ggml_backend_amx_init();
|
| 304 |
+
|
| 305 |
+
GGML_UNUSED(dev);
|
| 306 |
+
GGML_UNUSED(params);
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
static ggml_backend_buffer_type_t ggml_backend_amx_device_get_buffer_type(ggml_backend_dev_t dev) {
|
| 310 |
+
return ggml_backend_amx_buffer_type();
|
| 311 |
+
|
| 312 |
+
GGML_UNUSED(dev);
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
static bool ggml_backend_amx_device_supports_op(ggml_backend_dev_t dev, const struct ggml_tensor * op) {
|
| 316 |
+
|
| 317 |
+
// handle only 2d gemm for now
|
| 318 |
+
auto is_contiguous_2d = [](const struct ggml_tensor * t) {
|
| 319 |
+
return ggml_is_contiguous(t) && t->ne[3] == 1 && t->ne[2] == 1;
|
| 320 |
+
};
|
| 321 |
+
|
| 322 |
+
switch (op->op) {
|
| 323 |
+
case GGML_OP_NONE:
|
| 324 |
+
case GGML_OP_RESHAPE:
|
| 325 |
+
case GGML_OP_VIEW:
|
| 326 |
+
case GGML_OP_PERMUTE:
|
| 327 |
+
case GGML_OP_TRANSPOSE:
|
| 328 |
+
return true;
|
| 329 |
+
|
| 330 |
+
case GGML_OP_MUL_MAT: {
|
| 331 |
+
const struct ggml_tensor * src0 = op->src[0];
|
| 332 |
+
const struct ggml_tensor * src1 = op->src[1];
|
| 333 |
+
|
| 334 |
+
const enum ggml_type type = src0->type;
|
| 335 |
+
const int64_t ne0 = op->ne[0];
|
| 336 |
+
|
| 337 |
+
bool is_training = src0->grad || src1->grad;
|
| 338 |
+
|
| 339 |
+
// amx kernels enables for Q4_0, Q4_1, Q8_0, F16
|
| 340 |
+
// Q4_K, Q5_K, Q6_K, IQ4_XS enabled for QK_K = 256
|
| 341 |
+
bool has_amx_kernels = qtype_has_amx_kernels(type) || (type == GGML_TYPE_F16);
|
| 342 |
+
|
| 343 |
+
bool can_use_amx =
|
| 344 |
+
is_contiguous_2d(src0) && // src0 must be contiguous
|
| 345 |
+
is_contiguous_2d(src1) && // src1 must be contiguous
|
| 346 |
+
!is_training && // inference only
|
| 347 |
+
src1->type == GGML_TYPE_F32 && // src1 must be float32
|
| 348 |
+
has_amx_kernels && // with amx kernel impls
|
| 349 |
+
ne0 % (TILE_N * 2) == 0; // out_features is 32x
|
| 350 |
+
|
| 351 |
+
return can_use_amx;
|
| 352 |
+
}
|
| 353 |
+
default:
|
| 354 |
+
return false;
|
| 355 |
+
}
|
| 356 |
+
|
| 357 |
+
GGML_UNUSED(dev);
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
static bool ggml_backend_amx_device_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) {
|
| 361 |
+
return buft->iface.get_name == ggml_backend_amx_buffer_type_get_name;
|
| 362 |
+
|
| 363 |
+
GGML_UNUSED(dev);
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
static const struct ggml_backend_device_i ggml_backend_amx_device_i = {
|
| 367 |
+
/* .get_name = */ ggml_backend_amx_device_get_name,
|
| 368 |
+
/* .get_description = */ ggml_backend_amx_device_get_description,
|
| 369 |
+
/* .get_memory = */ ggml_backend_amx_device_get_memory,
|
| 370 |
+
/* .get_type = */ ggml_backend_amx_device_get_type,
|
| 371 |
+
/* .get_props = */ ggml_backend_amx_device_get_props,
|
| 372 |
+
/* .init_backend = */ ggml_backend_amx_device_init,
|
| 373 |
+
/* .get_buffer_type = */ ggml_backend_amx_device_get_buffer_type,
|
| 374 |
+
/* .get_host_buffer_type = */ NULL,
|
| 375 |
+
/* .buffer_from_host_ptr = */ NULL,
|
| 376 |
+
/* .supports_op = */ ggml_backend_amx_device_supports_op,
|
| 377 |
+
/* .supports_buft = */ ggml_backend_amx_device_supports_buft,
|
| 378 |
+
/* .offload_op = */ NULL,
|
| 379 |
+
/* .event_new = */ NULL,
|
| 380 |
+
/* .event_free = */ NULL,
|
| 381 |
+
/* .event_synchronize = */ NULL,
|
| 382 |
+
};
|
| 383 |
+
|
| 384 |
+
// backend reg interface
|
| 385 |
+
|
| 386 |
+
static const char * ggml_backend_amx_reg_get_name(ggml_backend_reg_t reg) {
|
| 387 |
+
return "AMX";
|
| 388 |
+
|
| 389 |
+
GGML_UNUSED(reg);
|
| 390 |
+
}
|
| 391 |
+
|
| 392 |
+
static size_t ggml_backend_amx_reg_get_device_count(ggml_backend_reg_t reg) {
|
| 393 |
+
return 1;
|
| 394 |
+
|
| 395 |
+
GGML_UNUSED(reg);
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
static ggml_backend_dev_t ggml_backend_amx_reg_get_device(ggml_backend_reg_t reg, size_t index) {
|
| 399 |
+
GGML_ASSERT(index == 0);
|
| 400 |
+
|
| 401 |
+
static ggml_backend_device ggml_backend_amx_device = {
|
| 402 |
+
/* .iface = */ ggml_backend_amx_device_i,
|
| 403 |
+
/* .reg = */ reg,
|
| 404 |
+
/* .context = */ nullptr,
|
| 405 |
+
};
|
| 406 |
+
|
| 407 |
+
return &ggml_backend_amx_device;
|
| 408 |
+
|
| 409 |
+
GGML_UNUSED(reg);
|
| 410 |
+
GGML_UNUSED(index);
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
static void * ggml_backend_amx_get_proc_address(ggml_backend_reg_t reg, const char * name) {
|
| 414 |
+
if (std::strcmp(name, "ggml_backend_set_n_threads") == 0) {
|
| 415 |
+
return (void *)ggml_backend_amx_set_n_threads;
|
| 416 |
+
}
|
| 417 |
+
return NULL;
|
| 418 |
+
|
| 419 |
+
GGML_UNUSED(reg);
|
| 420 |
+
GGML_UNUSED(name);
|
| 421 |
+
}
|
| 422 |
+
|
| 423 |
+
static const struct ggml_backend_reg_i ggml_backend_amx_reg_i = {
|
| 424 |
+
/* .get_name = */ ggml_backend_amx_reg_get_name,
|
| 425 |
+
/* .get_device_count = */ ggml_backend_amx_reg_get_device_count,
|
| 426 |
+
/* .get_device = */ ggml_backend_amx_reg_get_device,
|
| 427 |
+
/* .get_proc_address = */ ggml_backend_amx_get_proc_address,
|
| 428 |
+
};
|
| 429 |
+
|
| 430 |
+
ggml_backend_reg_t ggml_backend_amx_reg(void) {
|
| 431 |
+
static struct ggml_backend_reg ggml_backend_amx_reg = {
|
| 432 |
+
/* .iface = */ ggml_backend_amx_reg_i,
|
| 433 |
+
/* .context = */ NULL,
|
| 434 |
+
};
|
| 435 |
+
|
| 436 |
+
return &ggml_backend_amx_reg;
|
| 437 |
+
}
|
| 438 |
+
|
| 439 |
+
#else // if defined(__AMX_INT8__)
|
| 440 |
+
|
| 441 |
+
ggml_backend_t ggml_backend_amx_init(void) {
|
| 442 |
+
fprintf(stderr, "GGML is not compiled with AMX support!\n");
|
| 443 |
+
return ggml_backend_t{};
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
void ggml_backend_amx_set_n_threads(ggml_backend_t backend_amx, int n_threads) {
|
| 447 |
+
fprintf(stderr, "GGML is not compiled with AMX support!\n");
|
| 448 |
+
|
| 449 |
+
GGML_UNUSED(backend_amx);
|
| 450 |
+
GGML_UNUSED(n_threads);
|
| 451 |
+
}
|
| 452 |
+
|
| 453 |
+
#endif
|
ggml/src/ggml-amx/common.h
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
|
| 3 |
+
#include "ggml.h"
|
| 4 |
+
#include "ggml-cpu-impl.h" // <immintrin.h>
|
| 5 |
+
|
| 6 |
+
#include <algorithm>
|
| 7 |
+
#include <memory>
|
| 8 |
+
#include <type_traits>
|
| 9 |
+
|
| 10 |
+
#if defined(_OPENMP)
|
| 11 |
+
#include <omp.h>
|
| 12 |
+
#endif
|
| 13 |
+
|
| 14 |
+
#define TILE_M 16
|
| 15 |
+
#define TILE_N 16
|
| 16 |
+
#define TILE_K 32
|
| 17 |
+
#define VNNI_BLK 4
|
| 18 |
+
|
| 19 |
+
#define AMX_BLK_SIZE 32
|
| 20 |
+
|
| 21 |
+
#define TMM0 0
|
| 22 |
+
#define TMM1 1
|
| 23 |
+
#define TMM2 2
|
| 24 |
+
#define TMM3 3
|
| 25 |
+
#define TMM4 4
|
| 26 |
+
#define TMM5 5
|
| 27 |
+
#define TMM6 6
|
| 28 |
+
#define TMM7 7
|
| 29 |
+
|
| 30 |
+
// parallel routines
|
| 31 |
+
template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
|
| 32 |
+
inline T div_up(T x, T y) { return (x + y - 1) / y; }
|
| 33 |
+
|
| 34 |
+
template <typename T>
|
| 35 |
+
inline void balance211(T n, T nth, T ith, T& n_start, T& n_end) {
|
| 36 |
+
#if 0
|
| 37 |
+
// onednn partition pattern
|
| 38 |
+
T& n_my = n_end;
|
| 39 |
+
if (nth <= 1 || n == 0) {
|
| 40 |
+
n_start = 0;
|
| 41 |
+
n_my = n;
|
| 42 |
+
} else {
|
| 43 |
+
T n1 = div_up(n, nth);
|
| 44 |
+
T n2 = n1 - 1;
|
| 45 |
+
T T1 = n - n2 * nth;
|
| 46 |
+
n_my = ith < T1 ? n1 : n2;
|
| 47 |
+
n_start = ith <= T1 ? ith*n1 : T1 * n1 + (ith - T1) * n2;
|
| 48 |
+
}
|
| 49 |
+
n_end += n_start;
|
| 50 |
+
#else
|
| 51 |
+
// pytorch aten partition pattern
|
| 52 |
+
T n_my = div_up(n, nth);
|
| 53 |
+
n_start = ith * n_my;
|
| 54 |
+
n_end = std::min(n_start + n_my, n);
|
| 55 |
+
#endif
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
template <typename func_t>
|
| 59 |
+
inline void parallel_for(int nth, int n, const func_t& f) {
|
| 60 |
+
#if defined(_OPENMP)
|
| 61 |
+
#pragma omp parallel num_threads(nth)
|
| 62 |
+
{
|
| 63 |
+
//int nth = omp_get_num_threads();
|
| 64 |
+
int ith = omp_get_thread_num();
|
| 65 |
+
int tbegin, tend;
|
| 66 |
+
balance211(n, nth, ith, tbegin, tend);
|
| 67 |
+
f(tbegin, tend);
|
| 68 |
+
}
|
| 69 |
+
#else
|
| 70 |
+
f(0, n);
|
| 71 |
+
|
| 72 |
+
GGML_UNUSED(nth);
|
| 73 |
+
#endif
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
// quantized types that have AMX support
|
| 77 |
+
inline bool qtype_has_amx_kernels(const enum ggml_type type) {
|
| 78 |
+
// TODO: fix padding for vnni format
|
| 79 |
+
return (type == GGML_TYPE_Q4_0) ||
|
| 80 |
+
(type == GGML_TYPE_Q4_1);
|
| 81 |
+
//(type == GGML_TYPE_Q8_0) ||
|
| 82 |
+
//(type == GGML_TYPE_Q4_K) ||
|
| 83 |
+
//(type == GGML_TYPE_Q5_K) ||
|
| 84 |
+
//(type == GGML_TYPE_Q6_K) ||
|
| 85 |
+
//(type == GGML_TYPE_IQ4_XS);
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// ggml backend context
|
| 89 |
+
struct ggml_backend_amx_context {
|
| 90 |
+
int n_threads = GGML_DEFAULT_N_THREADS;
|
| 91 |
+
std::unique_ptr<char[]> work_data;
|
| 92 |
+
size_t work_size = 0;
|
| 93 |
+
};
|
ggml/src/ggml-amx/mmq.cpp
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
ggml/src/ggml-amx/mmq.h
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#pragma once
|
| 2 |
+
#include "common.h"
|
| 3 |
+
#include <stdint.h>
|
| 4 |
+
|
| 5 |
+
#ifdef __cplusplus
|
| 6 |
+
extern "C" {
|
| 7 |
+
#endif
|
| 8 |
+
|
| 9 |
+
size_t ggml_backend_amx_get_alloc_size(const struct ggml_tensor * tensor);
|
| 10 |
+
|
| 11 |
+
void ggml_backend_amx_convert_weight(struct ggml_tensor * tensor, const void * data, size_t offset, size_t size);
|
| 12 |
+
|
| 13 |
+
void ggml_backend_amx_mul_mat(ggml_backend_amx_context * ctx, struct ggml_tensor * dst);
|
| 14 |
+
|
| 15 |
+
#ifdef __cplusplus
|
| 16 |
+
}
|
| 17 |
+
#endif
|