amx.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * llama.cpp - commit 40c6d79fb52f995f47507fedfeaae2ac05d9b35c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #include "amx.h"
  27. #include "common.h"
  28. #include "mmq.h"
  29. #include "ggml-backend-impl.h"
  30. #include "ggml-backend.h"
  31. #include "ggml-impl.h"
  32. #include "ggml-cpu.h"
  33. #if defined(__gnu_linux__)
  34. #include <sys/syscall.h>
  35. #include <unistd.h>
  36. #endif
  37. #include <cstdlib>
  38. #include <cstring>
  39. #include <memory>
  40. #if defined(__AMX_INT8__) && defined(__AVX512VNNI__)
  41. // AMX buffer interface
  42. static void ggml_backend_amx_buffer_free_buffer(ggml_backend_buffer_t buffer) {
  43. free(buffer->context);
  44. }
  45. static void * ggml_backend_amx_buffer_get_base(ggml_backend_buffer_t buffer) {
  46. return (void *)(buffer->context);
  47. }
  48. 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) {
  49. memset((char *)tensor->data + offset, value, size);
  50. GGML_UNUSED(buffer);
  51. }
  52. 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) {
  53. if (qtype_has_amx_kernels(tensor->type)) {
  54. ggml_backend_amx_convert_weight(tensor, data, offset, size);
  55. } else {
  56. memcpy((char *)tensor->data + offset, data, size);
  57. }
  58. GGML_UNUSED(buffer);
  59. }
  60. 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) {
  61. GGML_ASSERT(!qtype_has_amx_kernels(tensor->type));
  62. memcpy(data, (const char *)tensor->data + offset, size);
  63. GGML_UNUSED(buffer);
  64. }
  65. static bool ggml_backend_amx_buffer_cpy_tensor(ggml_backend_buffer_t buffer, const struct ggml_tensor * src, struct ggml_tensor * dst) {
  66. if (ggml_backend_buffer_is_host(src->buffer)) {
  67. if (qtype_has_amx_kernels(src->type)) {
  68. ggml_backend_amx_convert_weight(dst, src->data, 0, ggml_nbytes(dst));
  69. } else {
  70. memcpy(dst->data, src->data, ggml_nbytes(src));
  71. }
  72. return true;
  73. }
  74. return false;
  75. GGML_UNUSED(buffer);
  76. }
  77. static void ggml_backend_amx_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) {
  78. memset(buffer->context, value, buffer->size);
  79. }
  80. static ggml_backend_buffer_i ggml_backend_amx_buffer_interface = {
  81. /* .free_buffer = */ ggml_backend_amx_buffer_free_buffer,
  82. /* .get_base = */ ggml_backend_amx_buffer_get_base,
  83. /* .init_tensor = */ NULL, // no initialization required
  84. /* .memset_tensor = */ ggml_backend_amx_buffer_memset_tensor,
  85. /* .set_tensor = */ ggml_backend_amx_buffer_set_tensor,
  86. /* .get_tensor = */ ggml_backend_amx_buffer_get_tensor,
  87. /* .cpy_tensor = */ ggml_backend_amx_buffer_cpy_tensor,
  88. /* .clear = */ ggml_backend_amx_buffer_clear,
  89. /* .reset = */ NULL,
  90. };
  91. static const char * ggml_backend_amx_buffer_type_get_name(ggml_backend_buffer_type_t buft) {
  92. return "AMX";
  93. GGML_UNUSED(buft);
  94. }
  95. static ggml_backend_buffer_t ggml_backend_amx_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
  96. void * data = aligned_alloc(TENSOR_ALIGNMENT, size);
  97. if (data == NULL) {
  98. fprintf(stderr, "%s: failed to allocate buffer of size %zu\n", __func__, size);
  99. return NULL;
  100. }
  101. return ggml_backend_buffer_init(buft, ggml_backend_amx_buffer_interface, data, size);
  102. }
  103. static size_t ggml_backend_amx_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) {
  104. return TENSOR_ALIGNMENT;
  105. GGML_UNUSED(buft);
  106. }
  107. static size_t ggml_backend_amx_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, const ggml_tensor* tensor) {
  108. return ggml_backend_amx_get_alloc_size(tensor);
  109. GGML_UNUSED(buft);
  110. }
  111. static bool ggml_backend_amx_buffer_type_is_host(ggml_backend_buffer_type_t buft) {
  112. return false;
  113. GGML_UNUSED(buft);
  114. }
  115. #define ARCH_GET_XCOMP_PERM 0x1022
  116. #define ARCH_REQ_XCOMP_PERM 0x1023
  117. #define XFEATURE_XTILECFG 17
  118. #define XFEATURE_XTILEDATA 18
  119. static bool ggml_amx_init() {
  120. #if defined(__gnu_linux__)
  121. if (syscall(SYS_arch_prctl, ARCH_REQ_XCOMP_PERM, XFEATURE_XTILEDATA)) {
  122. fprintf(stderr, "AMX is not ready to be used!\n");
  123. return false;
  124. }
  125. return true;
  126. #elif defined(_WIN32)
  127. return true;
  128. #endif
  129. }
  130. ggml_backend_buffer_type_t ggml_backend_amx_buffer_type() {
  131. static struct ggml_backend_buffer_type ggml_backend_buffer_type_amx = {
  132. /* .iface = */ {
  133. /* .get_name = */ ggml_backend_amx_buffer_type_get_name,
  134. /* .alloc_buffer = */ ggml_backend_amx_buffer_type_alloc_buffer,
  135. /* .get_alignment = */ ggml_backend_amx_buffer_type_get_alignment,
  136. /* .get_max_size = */ NULL, // defaults to SIZE_MAX
  137. /* .get_alloc_size = */ ggml_backend_amx_buffer_type_get_alloc_size,
  138. /* .is_host = */ ggml_backend_amx_buffer_type_is_host,
  139. },
  140. /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cpu_reg(), 0),
  141. /* .context = */ NULL,
  142. };
  143. if (!ggml_amx_init()) {
  144. return NULL;
  145. }
  146. return &ggml_backend_buffer_type_amx;
  147. }
  148. bool ggml_backend_amx_buft_is_amx(ggml_backend_buffer_type_t buft) {
  149. return buft->iface.get_name == ggml_backend_amx_buffer_type_get_name;
  150. }
  151. bool ggml_backend_amx_device_supports_op(const struct ggml_tensor * op) {
  152. // handle only 2d gemm for now
  153. auto is_contiguous_2d = [](const struct ggml_tensor * t) {
  154. return ggml_is_contiguous(t) && t->ne[3] == 1 && t->ne[2] == 1;
  155. };
  156. switch (op->op) {
  157. case GGML_OP_NONE:
  158. case GGML_OP_RESHAPE:
  159. case GGML_OP_VIEW:
  160. case GGML_OP_PERMUTE:
  161. case GGML_OP_TRANSPOSE:
  162. return true;
  163. case GGML_OP_MUL_MAT: {
  164. const struct ggml_tensor * src0 = op->src[0];
  165. const struct ggml_tensor * src1 = op->src[1];
  166. const enum ggml_type type = src0->type;
  167. const int64_t ne0 = op->ne[0];
  168. // amx kernels enables for Q4_0, Q4_1, Q8_0, F16
  169. // Q4_K, Q5_K, Q6_K, IQ4_XS enabled for QK_K = 256
  170. bool has_amx_kernels = qtype_has_amx_kernels(type) || (type == GGML_TYPE_F16);
  171. bool can_use_amx =
  172. is_contiguous_2d(src0) && // src0 must be contiguous
  173. is_contiguous_2d(src1) && // src1 must be contiguous
  174. src1->type == GGML_TYPE_F32 && // src1 must be float32
  175. has_amx_kernels && // with amx kernel impls
  176. ne0 % (TILE_N * 2) == 0; // out_features is 32x
  177. return can_use_amx;
  178. }
  179. default:
  180. return false;
  181. }
  182. }
  183. #endif // defined(__AMX_INT8__) && defined(__AVX512VNNI__)