ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / src / ggml-cann / ggml-cann.cpp
| 1 | /* |
| 2 | * Copyright (c) 2023-2026 The ggml authors |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to |
| 6 | * deal in the Software without restriction, including without limitation the |
| 7 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 | * sell copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 | * IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include "ggml-cann.h" |
| 24 | |
| 25 | #include "ggml-backend-impl.h" |
| 26 | #include "ggml-cann/aclnn_ops.h" |
| 27 | #include "ggml-cann/common.h" |
| 28 | #include "ggml-impl.h" |
| 29 | #include "ggml.h" |
| 30 | |
| 31 | #include <acl/acl.h> |
| 32 | #include <aclnnop/aclnn_trans_matmul_weight.h> |
| 33 | #include <stdarg.h> |
| 34 | |
| 35 | #include <chrono> |
| 36 | #include <cmath> |
| 37 | #include <cstdio> |
| 38 | #include <cstring> |
| 39 | #include <memory> |
| 40 | #include <mutex> |
| 41 | #include <optional> |
| 42 | #include <queue> |
| 43 | #include <unordered_map> |
| 44 | #include <unordered_set> |
| 45 | #include <vector> |
| 46 | |
| 47 | #define GGML_COMMON_DECL_C |
| 48 | |
| 49 | #include "ggml-common.h" |
| 50 | |
| 51 | #define GGML_CANN_NAME "CANN" |
| 52 | |
| 53 | /** |
| 54 | * @brief Handles CANN errors by printing an error message and aborting. |
| 55 | * |
| 56 | * @param stmt The statement that caused the error. |
| 57 | * @param func The function in which the error occurred. |
| 58 | * @param file The file in which the error occurred. |
| 59 | * @param line The line number where the error occurred. |
| 60 | * @param msg The error message. |
| 61 | */ |
| 62 | [[noreturn]] void ggml_cann_error(const char * stmt, const char * func, const char * file, int line, const char * msg) { |
| 63 | int32_t id = -1; |
| 64 | aclrtGetDevice(&id); |
| 65 | |
| 66 | GGML_LOG_ERROR("CANN error: %s\n", msg); |
| 67 | GGML_LOG_ERROR(" current device: %d, in function %s at %s:%d\n", id, func, file, line); |
| 68 | GGML_LOG_ERROR(" %s\n", stmt); |
| 69 | // abort with GGML_ASSERT to get a stack trace |
| 70 | GGML_ABORT("CANN error"); |
| 71 | } |
| 72 | |
| 73 | // Thread-local variable to record the current device of this thread. |
| 74 | thread_local int g_current_cann_device = -1; |
| 75 | |
| 76 | /** |
| 77 | * @brief Set the CANN device to be used. |
| 78 | * |
| 79 | * @param device The target device ID to set. |
| 80 | */ |
| 81 | void ggml_cann_set_device(const int32_t device) { |
| 82 | // int current_device = -1; |
| 83 | // Note: In some CANN versions, if no device has been set yet, |
| 84 | // aclrtGetDevice(¤t_device) may return 0 by default. |
| 85 | // aclrtGetDevice(¤t_device); |
| 86 | |
| 87 | // If the current device is already the target one, no need to switch. |
| 88 | if (device == g_current_cann_device) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | // Switch to the new device. |
| 93 | ACL_CHECK(aclrtSetDevice(device)); |
| 94 | |
| 95 | // Update the global device record. |
| 96 | g_current_cann_device = device; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @brief Get the value of the specified environment variable (name) as lowercase. |
| 101 | * if not empty, return a std::string object |
| 102 | */ |
| 103 | std::optional<std::string> get_env_as_lowercase(const std::string & name) { |
| 104 | const char * val = std::getenv(name.c_str()); |
| 105 | if (!val) { |
| 106 | return std::nullopt; |
| 107 | } |
| 108 | std::string res = std::string(val); |
| 109 | std::transform(res.begin(), res.end(), res.begin(), ::tolower); |
| 110 | return res; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @brief Verify whether the environment variable is a valid value. |
| 115 | */ |
| 116 | bool parse_bool(const std::string & value) { |
| 117 | static const std::unordered_set<std::string> valid_values = { "on", "1", "yes", "y", "enable", "true" }; |
| 118 | return valid_values.find(value) != valid_values.end(); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @brief Parse a string as an integer, returning 0 if invalid. |
| 123 | * |
| 124 | * This function attempts to convert the input string `value` to an `int`. |
| 125 | * If the string is not a valid integer or is out of the `int` range, |
| 126 | * it returns 0. |
| 127 | * |
| 128 | * @param value The string to parse. |
| 129 | * @return The parsed integer, or 0 if conversion fails. |
| 130 | */ |
| 131 | int parse_integer(const std::string & value) { |
| 132 | try { |
| 133 | return std::stoi(value); |
| 134 | } catch (...) { |
| 135 | return 0; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @brief Initialize the CANN device information. |
| 141 | * |
| 142 | * This function initializes the CANN device information by obtaining the |
| 143 | * device count and setting the memory allocation granularity for each device. |
| 144 | * |
| 145 | * @return A structure containing the device information. |
| 146 | */ |
| 147 | static ggml_cann_device_info ggml_cann_init() { |
| 148 | ggml_cann_device_info info = {}; |
| 149 | |
| 150 | aclError err = aclrtGetDeviceCount((uint32_t *) &info.device_count); |
| 151 | |
| 152 | if (err != ACL_SUCCESS) { |
| 153 | GGML_LOG_ERROR("%s: failed to initialize CANN: %s\n", __func__, aclGetRecentErrMsg()); |
| 154 | return info; |
| 155 | } |
| 156 | |
| 157 | GGML_ASSERT(info.device_count <= GGML_CANN_MAX_DEVICES); |
| 158 | |
| 159 | for (int id = 0; id < info.device_count; ++id) { |
| 160 | aclrtPhysicalMemProp prop = {}; |
| 161 | prop.handleType = ACL_MEM_HANDLE_TYPE_NONE; |
| 162 | prop.allocationType = ACL_MEM_ALLOCATION_TYPE_PINNED; |
| 163 | prop.memAttr = ACL_HBM_MEM_HUGE; |
| 164 | prop.location.type = ACL_MEM_LOCATION_TYPE_DEVICE; |
| 165 | prop.location.id = id; |
| 166 | prop.reserve = 0; |
| 167 | err = aclrtMemGetAllocationGranularity(&prop, ACL_RT_MEM_ALLOC_GRANULARITY_RECOMMENDED, |
| 168 | &info.devices[id].vmm_granularity); |
| 169 | info.devices[id].vmm = err == ACL_SUCCESS; |
| 170 | |
| 171 | size_t free, total; |
| 172 | ggml_backend_cann_get_device_memory(id, &free, &total); |
| 173 | info.devices[id].total_vram = free; |
| 174 | } |
| 175 | |
| 176 | // TODO: add more device info later. |
| 177 | return info; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * @brief Retrieve the CANN device information. |
| 182 | * |
| 183 | * This function returns a reference to a structure containing the CANN device |
| 184 | * information. The device information is initialized once and reused on |
| 185 | * subsequent calls. |
| 186 | * |
| 187 | * @return A reference to the structure containing the device information. |
| 188 | */ |
| 189 | const ggml_cann_device_info & ggml_cann_info() { |
| 190 | static ggml_cann_device_info info = ggml_cann_init(); |
| 191 | return info; |
| 192 | } |
| 193 | |
| 194 | //#define DEBUG_CANN_MALLOC |
| 195 | /** |
| 196 | * @brief A pool of CANN buffers(priority segment buffer). |
| 197 | * |
| 198 | * This class manages a pool of CANN buffers for a specific device. |
| 199 | */ |
| 200 | struct ggml_cann_pool_buf_prio : public ggml_cann_pool { |
| 201 | /** |
| 202 | * @brief The maximum reuse margin for a buffer. |
| 203 | */ |
| 204 | static const size_t max_reuse_margin = 1ull << 22; // 4MB |
| 205 | |
| 206 | /** |
| 207 | * @brief The minimum free margin for a buffer. |
| 208 | */ |
| 209 | static const size_t min_free_margin = 1ull << 20; // 1MB |
| 210 | |
| 211 | /** |
| 212 | * @brief The alignment for buffer allocation. |
| 213 | */ |
| 214 | static const size_t alignment = 128; |
| 215 | |
| 216 | /** |
| 217 | * @brief The device ID associated with this buffer pool. |
| 218 | */ |
| 219 | int device; |
| 220 | |
| 221 | /** |
| 222 | * @brief Whether to disable clean during buffer allocation. |
| 223 | */ |
| 224 | bool disable_clean = false; |
| 225 | |
| 226 | /** |
| 227 | * @brief Structure representing a CANN buffer. |
| 228 | */ |
| 229 | struct ggml_cann_buffer { |
| 230 | void * ptr = nullptr; ///< Pointer to the buffer. |
| 231 | size_t size = 0; ///< Size of the buffer. |
| 232 | std::chrono::steady_clock::time_point last_used; ///< Last used time. |
| 233 | |
| 234 | bool operator>(const ggml_cann_buffer & other) const { return size > other.size; } |
| 235 | }; |
| 236 | |
| 237 | /** |
| 238 | * @brief Array of CANN buffers in the pool. |
| 239 | */ |
| 240 | std::unordered_map<void *, size_t> buffer_pool; |
| 241 | std::priority_queue<ggml_cann_buffer, std::vector<ggml_cann_buffer>, std::greater<>> free_buffers; |
| 242 | |
| 243 | /** |
| 244 | * @brief Total size of all buffers in the pool. |
| 245 | */ |
| 246 | size_t pool_size = 0; |
| 247 | |
| 248 | /** |
| 249 | * @brief Constructor to initialize the buffer pool for a specific device. |
| 250 | * |
| 251 | * @param device The device ID to associate with this buffer pool. |
| 252 | */ |
| 253 | explicit ggml_cann_pool_buf_prio(int device) : device(device) { |
| 254 | disable_clean = parse_bool(get_env_as_lowercase("GGML_CANN_DISABLE_BUF_POOL_CLEAN").value_or("")); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * @brief Destructor to free all buffers in the pool. |
| 259 | */ |
| 260 | ~ggml_cann_pool_buf_prio() { |
| 261 | ggml_cann_set_device(device); |
| 262 | for (auto & [b_ptr, b_size] : buffer_pool) { |
| 263 | aclrtFree(b_ptr); |
| 264 | pool_size -= b_size; |
| 265 | } |
| 266 | buffer_pool.clear(); |
| 267 | GGML_ASSERT(pool_size == 0); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * @brief Allocate a buffer of the given size. |
| 272 | * |
| 273 | * @param size The size of the buffer to allocate. |
| 274 | * @param actual_size A pointer to a variable to receive the actual size of |
| 275 | * the allocated buffer. |
| 276 | * @return A pointer to the allocated buffer. |
| 277 | */ |
| 278 | void * alloc(size_t size, size_t * actual_size) override { |
| 279 | size = GGML_PAD(size, alignment); |
| 280 | if (size == 0) { |
| 281 | size = alignment; |
| 282 | } |
| 283 | |
| 284 | void * ptr = nullptr; |
| 285 | auto now = std::chrono::steady_clock::now(); |
| 286 | |
| 287 | std::vector<ggml_cann_buffer> free_buffers_rest; |
| 288 | free_buffers_rest.reserve(free_buffers.size()); |
| 289 | while (!free_buffers.empty()) { |
| 290 | auto b = free_buffers.top(); |
| 291 | free_buffers.pop(); |
| 292 | |
| 293 | if (b.size >= size) { |
| 294 | // reuse the buffer if the size is enough |
| 295 | const size_t margin = b.size - size; |
| 296 | if (margin <= max_reuse_margin) { |
| 297 | *actual_size = b.size; |
| 298 | ptr = b.ptr; |
| 299 | #ifdef DEBUG_CANN_MALLOC |
| 300 | GGML_LOG_INFO( |
| 301 | "cann pool[%d]: reused %p, " |
| 302 | "pool_size = %5u MB, " |
| 303 | "size = %5u MB, " |
| 304 | "margin = %5u MB\n", |
| 305 | device, b.ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576), |
| 306 | (uint32_t) (GGML_PAD(size, 1048576) / 1048576), |
| 307 | (uint32_t) (GGML_PAD(margin, 1048576) / 1048576)); |
| 308 | #endif |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | bool should_clean = !disable_clean && b.size > min_free_margin && |
| 314 | std::chrono::duration_cast<std::chrono::milliseconds>(now - b.last_used).count() > 100; |
| 315 | if (should_clean) { |
| 316 | // free the buffer if the size is needed to be freed |
| 317 | ACL_CHECK(aclrtFree(b.ptr)); |
| 318 | pool_size -= b.size; |
| 319 | buffer_pool.erase(b.ptr); |
| 320 | #ifdef DEBUG_CANN_MALLOC |
| 321 | GGML_LOG_INFO( |
| 322 | "cann pool[%d]: clean %p, " |
| 323 | "pool_size = %5u MB, " |
| 324 | "size = %5u MB\n", |
| 325 | device, b.ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576), |
| 326 | (uint32_t) (GGML_PAD(b.size, 1048576) / 1048576)); |
| 327 | #endif |
| 328 | continue; |
| 329 | } |
| 330 | free_buffers_rest.push_back(b); |
| 331 | } |
| 332 | for (ggml_cann_buffer & b : free_buffers_rest) { |
| 333 | free_buffers.push(std::move(b)); |
| 334 | } |
| 335 | |
| 336 | #ifdef DEBUG_CANN_MALLOC |
| 337 | GGML_LOG_INFO("cann pool[%d] free pool_size = %5u MB\n\n", device, |
| 338 | (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576)); |
| 339 | #endif |
| 340 | if (ptr != nullptr) { |
| 341 | return ptr; |
| 342 | } |
| 343 | |
| 344 | // allocate a new buffer if no buffer can be reused |
| 345 | ggml_cann_set_device(device); |
| 346 | ACL_CHECK(aclrtMalloc(&ptr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 347 | *actual_size = size; |
| 348 | pool_size += size; |
| 349 | #ifdef DEBUG_CANN_MALLOC |
| 350 | GGML_LOG_INFO( |
| 351 | "cann pool[%d]: allocate %p, " |
| 352 | "pool_size = %5u MB, " |
| 353 | "size = %5u MB\n", |
| 354 | device, ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576), |
| 355 | (uint32_t) (GGML_PAD(size, 1048576) / 1048576)); |
| 356 | #endif |
| 357 | buffer_pool.emplace(ptr, size); |
| 358 | return ptr; |
| 359 | } |
| 360 | |
| 361 | /** |
| 362 | * @brief Free a buffer and return it to the pool. |
| 363 | * |
| 364 | * @param ptr Pointer to the buffer to free. |
| 365 | * @param size Size of the buffer to free. |
| 366 | */ |
| 367 | void free(void * ptr, size_t size) override { |
| 368 | GGML_UNUSED(size); |
| 369 | auto it = buffer_pool.find(ptr); |
| 370 | if (it == buffer_pool.end()) { |
| 371 | GGML_ABORT("cann pool[%d]: buffer %p not found in pool\n", device, ptr); |
| 372 | } |
| 373 | |
| 374 | auto now = std::chrono::steady_clock::now(); |
| 375 | free_buffers.emplace(ggml_cann_buffer{ ptr, it->second, now }); |
| 376 | #ifdef DEBUG_CANN_MALLOC |
| 377 | GGML_LOG_INFO( |
| 378 | "cann pool[%d]: return %p, " |
| 379 | "pool_size = %5u MB\n", |
| 380 | device, ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576)); |
| 381 | #endif |
| 382 | } |
| 383 | }; |
| 384 | |
| 385 | /** |
| 386 | * @brief A pool of CANN buffers(segment buffer). |
| 387 | * |
| 388 | * This class manages a pool of CANN buffers for a specific device. |
| 389 | */ |
| 390 | struct ggml_cann_pool_buf : public ggml_cann_pool { |
| 391 | /** |
| 392 | * @brief The maximum reuse margin for a buffer. |
| 393 | */ |
| 394 | static const size_t max_reuse_margin = 1ull << 22; // 4MB |
| 395 | |
| 396 | /** |
| 397 | * @brief The minimum free margin for a buffer. |
| 398 | */ |
| 399 | static const size_t min_free_margin = 1ull << 20; // 1MB |
| 400 | |
| 401 | /** |
| 402 | * @brief The alignment for buffer allocation. |
| 403 | */ |
| 404 | static const size_t alignment = 128; |
| 405 | |
| 406 | /** |
| 407 | * @brief The maximum number of buffers in the pool. |
| 408 | */ |
| 409 | static const int MAX_BUFFERS = 256; |
| 410 | |
| 411 | /** |
| 412 | * @brief The device ID associated with this buffer pool. |
| 413 | */ |
| 414 | int device; |
| 415 | |
| 416 | /** |
| 417 | * @brief Whether to disable clean during buffer allocation. |
| 418 | */ |
| 419 | bool disable_clean = false; |
| 420 | |
| 421 | /** |
| 422 | * @brief Structure representing a CANN buffer. |
| 423 | */ |
| 424 | struct ggml_cann_buffer { |
| 425 | void * ptr = nullptr; ///< Pointer to the buffer memory. |
| 426 | size_t size = 0; ///< Size of the buffer. |
| 427 | bool used = false; ///< Whether the buffer is currently in use. |
| 428 | std::chrono::steady_clock::time_point last_used; ///< Last used time. |
| 429 | }; |
| 430 | |
| 431 | /** |
| 432 | * @brief Array of CANN buffers in the pool. |
| 433 | */ |
| 434 | ggml_cann_buffer buffer_pool[MAX_BUFFERS] = {}; |
| 435 | |
| 436 | /** |
| 437 | * @brief Total size of all buffers in the pool. |
| 438 | */ |
| 439 | size_t pool_size = 0; |
| 440 | |
| 441 | /** |
| 442 | * @brief Constructor to initialize the buffer pool for a specific device. |
| 443 | * |
| 444 | * @param device The device ID to associate with this buffer pool. |
| 445 | */ |
| 446 | explicit ggml_cann_pool_buf(int device) : device(device) { |
| 447 | disable_clean = parse_bool(get_env_as_lowercase("GGML_CANN_DISABLE_BUF_POOL_CLEAN").value_or("")); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * @brief Destructor to free all buffers in the pool. |
| 452 | */ |
| 453 | ~ggml_cann_pool_buf() { |
| 454 | ggml_cann_set_device(device); |
| 455 | for (int i = 0; i < MAX_BUFFERS; ++i) { |
| 456 | ggml_cann_buffer & b = buffer_pool[i]; |
| 457 | if (b.ptr != nullptr) { |
| 458 | aclrtFree(b.ptr); |
| 459 | pool_size -= b.size; |
| 460 | } |
| 461 | } |
| 462 | GGML_ASSERT(pool_size == 0); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * @brief Allocate a buffer of the given size. |
| 467 | * |
| 468 | * @param size The size of the buffer to allocate. |
| 469 | * @param actual_size A pointer to a variable to receive the actual size of |
| 470 | * the allocated buffer. |
| 471 | * @return A pointer to the allocated buffer. |
| 472 | */ |
| 473 | void * alloc(size_t size, size_t * actual_size) override { |
| 474 | size = GGML_PAD(size, alignment); |
| 475 | if (size == 0) { |
| 476 | size = alignment; |
| 477 | } |
| 478 | |
| 479 | void * ptr = nullptr; |
| 480 | auto now = std::chrono::steady_clock::now(); |
| 481 | |
| 482 | int i = 0; |
| 483 | for (; i < MAX_BUFFERS; ++i) { |
| 484 | ggml_cann_buffer & b = buffer_pool[i]; |
| 485 | if (b.ptr == nullptr) { |
| 486 | break; |
| 487 | } |
| 488 | if (b.used) { |
| 489 | continue; |
| 490 | } |
| 491 | if (b.size >= size) { |
| 492 | // reuse the buffer if the size is enough |
| 493 | const size_t margin = b.size - size; |
| 494 | if (margin <= max_reuse_margin) { |
| 495 | *actual_size = b.size; |
| 496 | b.used = true; |
| 497 | ptr = b.ptr; |
| 498 | #ifdef DEBUG_CANN_MALLOC |
| 499 | GGML_LOG_INFO( |
| 500 | "cann pool[%d]: reused %p, " |
| 501 | "pool_size = %5u MB, " |
| 502 | "size = %5u MB, " |
| 503 | "margin = %5u MB\n", |
| 504 | device, b.ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576), |
| 505 | (uint32_t) (GGML_PAD(size, 1048576) / 1048576), |
| 506 | (uint32_t) (GGML_PAD(margin, 1048576) / 1048576)); |
| 507 | #endif |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | bool should_clean = !disable_clean && b.size > min_free_margin && |
| 513 | std::chrono::duration_cast<std::chrono::milliseconds>(now - b.last_used).count() > 100; |
| 514 | if (should_clean) { |
| 515 | // free the buffer if the size is needed to be freed |
| 516 | ACL_CHECK(aclrtFree(b.ptr)); |
| 517 | pool_size -= b.size; |
| 518 | #ifdef DEBUG_CANN_MALLOC |
| 519 | GGML_LOG_INFO( |
| 520 | "cann pool[%d]: clean %p, " |
| 521 | "pool_size = %5u MB, " |
| 522 | "size = %5u MB\n", |
| 523 | device, b.ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576), |
| 524 | (uint32_t) (GGML_PAD(b.size, 1048576) / 1048576)); |
| 525 | #endif |
| 526 | b.ptr = nullptr; |
| 527 | } |
| 528 | } |
| 529 | if (ptr != nullptr) { |
| 530 | return ptr; |
| 531 | } |
| 532 | |
| 533 | if (i < MAX_BUFFERS) { |
| 534 | // allocate a new buffer if no buffer can be reused |
| 535 | ggml_cann_buffer & b = buffer_pool[i]; |
| 536 | ggml_cann_set_device(device); |
| 537 | ACL_CHECK(aclrtMalloc(&b.ptr, size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 538 | pool_size += size; |
| 539 | *actual_size = size; |
| 540 | b.size = size; |
| 541 | b.used = true; |
| 542 | if (i >= MAX_BUFFERS - 8) { |
| 543 | GGML_LOG_WARN("cann pool[%d]: slots almost full\n", device); |
| 544 | } |
| 545 | #ifdef DEBUG_CANN_MALLOC |
| 546 | GGML_LOG_INFO( |
| 547 | "cann pool[%d]: allocate %p, " |
| 548 | "pool_size = %5u MB, " |
| 549 | "size = %5u MB\n", |
| 550 | device, b.ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576), |
| 551 | (uint32_t) (GGML_PAD(b.size, 1048576) / 1048576)); |
| 552 | #endif |
| 553 | return b.ptr; |
| 554 | } |
| 555 | |
| 556 | GGML_ABORT("cann pool[%d]: slots full\n", device); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * @brief Free a buffer and return it to the pool. |
| 561 | * |
| 562 | * @param ptr Pointer to the buffer to free. |
| 563 | * @param size Size of the buffer to free. |
| 564 | */ |
| 565 | void free(void * ptr, size_t size) override { |
| 566 | GGML_UNUSED(size); |
| 567 | for (int i = 0; i < MAX_BUFFERS; ++i) { |
| 568 | ggml_cann_buffer & b = buffer_pool[i]; |
| 569 | if (b.ptr != ptr) { |
| 570 | continue; |
| 571 | } |
| 572 | b.used = false; |
| 573 | b.last_used = std::chrono::steady_clock::now(); |
| 574 | #ifdef DEBUG_CANN_MALLOC |
| 575 | GGML_LOG_INFO( |
| 576 | "cann pool[%d]: return %p, " |
| 577 | "pool_size = %5u MB\n", |
| 578 | device, b.ptr, (uint32_t) (GGML_PAD(pool_size, 1048576) / 1048576)); |
| 579 | #endif |
| 580 | return; |
| 581 | } |
| 582 | GGML_ABORT("cann pool[%d]: slots full\n", device); |
| 583 | } |
| 584 | }; |
| 585 | |
| 586 | /** |
| 587 | * @brief A pool of CANN buffers with virtual memory. |
| 588 | * |
| 589 | * This class manages a pool of CANN buffers with virtual memory for a specific |
| 590 | * device. |
| 591 | */ |
| 592 | struct ggml_cann_pool_vmm : public ggml_cann_pool { |
| 593 | /** |
| 594 | * @brief The maximum size of the virtual memory pool (32 GB). |
| 595 | */ |
| 596 | size_t max_size; |
| 597 | |
| 598 | /** |
| 599 | * @brief The device ID associated with this buffer pool. |
| 600 | */ |
| 601 | int device; |
| 602 | |
| 603 | /** |
| 604 | * @brief Pointer to the start of the virtual memory pool. |
| 605 | */ |
| 606 | void * pool_addr = 0; |
| 607 | |
| 608 | /** |
| 609 | * @brief Amount of virtual memory used in the pool. |
| 610 | */ |
| 611 | size_t pool_used = 0; |
| 612 | |
| 613 | /** |
| 614 | * @brief Total size of the virtual memory pool. |
| 615 | */ |
| 616 | size_t pool_size = 0; |
| 617 | |
| 618 | /** |
| 619 | * @brief Allocation granularity for the virtual memory pool. |
| 620 | */ |
| 621 | size_t granularity; |
| 622 | |
| 623 | /** |
| 624 | * @brief Handles for the physical memory allocated. |
| 625 | */ |
| 626 | std::vector<aclrtDrvMemHandle> handles; |
| 627 | |
| 628 | /** |
| 629 | * @brief Offsets for the mapped memory regions. |
| 630 | */ |
| 631 | std::vector<void *> map_offsets; |
| 632 | |
| 633 | /** |
| 634 | * @brief Constructor to initialize the buffer pool with virtual memory for |
| 635 | * a specific device. |
| 636 | * |
| 637 | * @param device The device ID to associate with this buffer pool. |
| 638 | */ |
| 639 | explicit ggml_cann_pool_vmm(int device) : device(device) { |
| 640 | auto dev = ggml_cann_info().devices[device]; |
| 641 | granularity = dev.vmm_granularity; |
| 642 | max_size = dev.total_vram; |
| 643 | } |
| 644 | |
| 645 | /** |
| 646 | * @brief Destructor to free all buffers in the virtual memory pool. |
| 647 | */ |
| 648 | ~ggml_cann_pool_vmm() { |
| 649 | if (pool_addr != 0) { |
| 650 | for (auto & offset : map_offsets) { |
| 651 | ACL_CHECK(aclrtUnmapMem(offset)); |
| 652 | } |
| 653 | for (auto & handle : handles) { |
| 654 | ACL_CHECK(aclrtFreePhysical(handle)); |
| 655 | } |
| 656 | ACL_CHECK(aclrtReleaseMemAddress(pool_addr)); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * @brief Allocate a buffer of the given size in the virtual memory pool. |
| 662 | * |
| 663 | * @param size The size of the buffer to allocate. |
| 664 | * @param actual_size A pointer to a variable to receive the actual size of |
| 665 | * the allocated buffer. |
| 666 | * @return A pointer to the allocated buffer. |
| 667 | */ |
| 668 | void * alloc(size_t size, size_t * actual_size) override { |
| 669 | // round up the allocation size to the alignment to ensure that all |
| 670 | // allocations are aligned for all data types |
| 671 | const size_t alignment = 128; |
| 672 | size = GGML_PAD(size, alignment); |
| 673 | if (size == 0) { |
| 674 | size = alignment; |
| 675 | } |
| 676 | |
| 677 | size_t avail = pool_size - pool_used; |
| 678 | |
| 679 | if (size > avail) { |
| 680 | // round up to the next multiple of the granularity |
| 681 | size_t reserve_size = size - avail; |
| 682 | reserve_size = GGML_PAD(reserve_size, granularity); |
| 683 | |
| 684 | GGML_ASSERT(pool_size + reserve_size <= max_size); |
| 685 | |
| 686 | // allocate more physical memory |
| 687 | aclrtPhysicalMemProp prop = {}; |
| 688 | prop.handleType = ACL_MEM_HANDLE_TYPE_NONE; |
| 689 | prop.allocationType = ACL_MEM_ALLOCATION_TYPE_PINNED; |
| 690 | prop.memAttr = ACL_HBM_MEM_HUGE; |
| 691 | prop.location.type = ACL_MEM_LOCATION_TYPE_DEVICE; |
| 692 | prop.location.id = device; |
| 693 | prop.reserve = 0; |
| 694 | aclrtDrvMemHandle handle; |
| 695 | ACL_CHECK(aclrtMallocPhysical(&handle, reserve_size, &prop, 0)); |
| 696 | |
| 697 | // reserve virtual address space (if not already reserved) |
| 698 | if (pool_addr == 0) { |
| 699 | ACL_CHECK(aclrtReserveMemAddress(&pool_addr, max_size, 0, NULL, 1)); |
| 700 | } |
| 701 | |
| 702 | // map at the end of the pool |
| 703 | ACL_CHECK(aclrtMapMem((char *) pool_addr + pool_size, reserve_size, 0, handle, 0)); |
| 704 | |
| 705 | handles.push_back(handle); |
| 706 | map_offsets.push_back((char *) pool_addr + pool_size); |
| 707 | |
| 708 | // add to the pool |
| 709 | pool_size += reserve_size; |
| 710 | |
| 711 | #ifdef DEBUG_CANN_MALLOC |
| 712 | GGML_LOG_INFO("cann pool[%d]: size increased to %llu MB (reserved %llu MB)\n", device, |
| 713 | (unsigned long long) (pool_size / 1024 / 1024), |
| 714 | (unsigned long long) (reserve_size / 1024 / 1024)); |
| 715 | #endif |
| 716 | } |
| 717 | |
| 718 | GGML_ASSERT(pool_addr != 0); |
| 719 | |
| 720 | void * ptr = (void *) ((char *) pool_addr + pool_used); |
| 721 | *actual_size = size; |
| 722 | pool_used += size; |
| 723 | |
| 724 | #ifdef DEBUG_CANN_MALLOC |
| 725 | GGML_LOG_INFO("cann pool[%d]: allocated %llu bytes at %llx\n", device, (unsigned long long) size, |
| 726 | (unsigned long long) ptr); |
| 727 | #endif |
| 728 | return ptr; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * @brief Free a buffer and return it to the virtual memory pool. |
| 733 | * |
| 734 | * @param ptr Pointer to the buffer to free. |
| 735 | * @param size Size of the buffer to free. |
| 736 | */ |
| 737 | void free(void * ptr, size_t size) override { |
| 738 | #ifdef DEBUG_CANN_MALLOC |
| 739 | GGML_LOG_INFO("cann pool[%d]: freed %llu bytes at %llx\n", device, (unsigned long long) size, |
| 740 | (unsigned long long) ptr); |
| 741 | #endif |
| 742 | |
| 743 | pool_used -= size; |
| 744 | |
| 745 | // all deallocations must be in reverse order of the allocations |
| 746 | GGML_ASSERT(ptr == (void *) ((char *) pool_addr + pool_used)); |
| 747 | } |
| 748 | }; |
| 749 | |
| 750 | /** |
| 751 | * @brief Create a new CANN pool for a specific device. |
| 752 | * |
| 753 | * Factory method to create a new CANN pool object based on the device type. |
| 754 | * |
| 755 | * @param device The device ID for which to create the pool. |
| 756 | * @return A unique pointer to the created CANN pool. |
| 757 | */ |
| 758 | std::unique_ptr<ggml_cann_pool> ggml_backend_cann_context::new_pool_for_device(int device) { |
| 759 | std::string mem_pool_type = get_env_as_lowercase("GGML_CANN_MEM_POOL").value_or(""); |
| 760 | |
| 761 | if (mem_pool_type == "prio") { |
| 762 | GGML_LOG_INFO("%s: device %d use buffer pool with priority queue\n", __func__, device); |
| 763 | return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf_prio(device)); |
| 764 | } |
| 765 | |
| 766 | if (ggml_cann_info().devices[device].vmm && mem_pool_type != "leg") { |
| 767 | GGML_LOG_INFO("%s: device %d use vmm pool\n", __func__, device); |
| 768 | return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_vmm(device)); |
| 769 | } |
| 770 | |
| 771 | GGML_LOG_INFO("%s: device %d use buffer pool\n", __func__, device); |
| 772 | return std::unique_ptr<ggml_cann_pool>(new ggml_cann_pool_buf(device)); |
| 773 | } |
| 774 | |
| 775 | // cann buffer |
| 776 | |
| 777 | /** |
| 778 | * @brief Tracks multi-threaded write progress for a single tensor. |
| 779 | * |
| 780 | * When multiple threads call set_tensor on different chunks of the same tensor, |
| 781 | * this tracker accumulates progress and defers post-processing (quantized format |
| 782 | * transform or ND-to-NZ conversion) until all data has been written. |
| 783 | */ |
| 784 | struct TensorSetTracker { |
| 785 | std::mutex mtx; ///< Protects concurrent access to this tracker |
| 786 | size_t bytes_written = 0; ///< Accumulated bytes written so far |
| 787 | size_t total_bytes = 0; ///< Target size (full tensor) |
| 788 | std::vector<uint8_t> host_buffer; ///< Host staging buffer for quantized tensors |
| 789 | }; |
| 790 | |
| 791 | /** |
| 792 | * @brief Context for managing a CANN buffer associated with a specific device. |
| 793 | * |
| 794 | * This structure holds information about a CANN buffer, including the device |
| 795 | * ID, device pointer, and a name derived from GGML_CANN_NAME and the device ID. |
| 796 | */ |
| 797 | struct ggml_backend_cann_buffer_context { |
| 798 | int32_t device; ///< The device ID associated with this buffer context. |
| 799 | void * dev_ptr = nullptr; ///< Pointer to the device memory allocated for the buffer. |
| 800 | |
| 801 | std::mutex tracker_mutex; ///< Protects the trackers map |
| 802 | std::unordered_map<void *, std::unique_ptr<TensorSetTracker>> trackers; |
| 803 | |
| 804 | /** |
| 805 | * @brief Constructor to initialize the CANN buffer context. |
| 806 | * |
| 807 | * @param device The device ID associated with this buffer context. |
| 808 | * @param dev_ptr Pointer to the device memory allocated for the buffer. |
| 809 | */ |
| 810 | ggml_backend_cann_buffer_context(int32_t device, void * dev_ptr) : device(device), dev_ptr(dev_ptr) {} |
| 811 | |
| 812 | /** |
| 813 | * @brief Destructor to free the device memory allocated for the buffer. |
| 814 | */ |
| 815 | ~ggml_backend_cann_buffer_context() { ACL_CHECK(aclrtFree(dev_ptr)); } |
| 816 | |
| 817 | /** |
| 818 | * @brief Get or create a tracker for the given tensor. |
| 819 | */ |
| 820 | TensorSetTracker * get_or_create_tracker(ggml_tensor * tensor) { |
| 821 | std::lock_guard<std::mutex> lock(tracker_mutex); |
| 822 | auto key = tensor->data; |
| 823 | auto it = trackers.find(key); |
| 824 | if (it == trackers.end()) { |
| 825 | auto tracker = std::make_unique<TensorSetTracker>(); |
| 826 | tracker->total_bytes = ggml_nbytes(tensor); |
| 827 | auto * ptr = tracker.get(); |
| 828 | trackers[key] = std::move(tracker); |
| 829 | return ptr; |
| 830 | } |
| 831 | return it->second.get(); |
| 832 | } |
| 833 | |
| 834 | /** |
| 835 | * @brief Remove the tracker for the given tensor. |
| 836 | */ |
| 837 | void remove_tracker(ggml_tensor * tensor) { |
| 838 | std::lock_guard<std::mutex> lock(tracker_mutex); |
| 839 | trackers.erase(tensor->data); |
| 840 | } |
| 841 | }; |
| 842 | |
| 843 | // cann buffer type |
| 844 | /** |
| 845 | * @brief Structure representing context information for a specific backend |
| 846 | * buffer type. |
| 847 | */ |
| 848 | struct ggml_backend_cann_buffer_type_context { |
| 849 | int32_t device; /**< Device identifier associated with the buffer context. */ |
| 850 | std::string name; /**< Name associated with the buffer context. */ |
| 851 | }; |
| 852 | |
| 853 | /** |
| 854 | * @brief Retrieves the name associated with a CANN buffer type. |
| 855 | * |
| 856 | * This function returns the descriptive name associated with the specified |
| 857 | * CANN buffer type context. |
| 858 | * |
| 859 | * @param buft Pointer to the buffer type context. |
| 860 | * @return Const pointer to the C-style string containing the name. |
| 861 | */ |
| 862 | static const char * ggml_backend_cann_buffer_type_name(ggml_backend_buffer_type_t buft) { |
| 863 | ggml_backend_cann_buffer_type_context * buft_ctx = (ggml_backend_cann_buffer_type_context *) buft->context; |
| 864 | |
| 865 | return buft_ctx->name.c_str(); |
| 866 | } |
| 867 | |
| 868 | /** |
| 869 | * @brief Checks if the backend buffer type is associated with the CANN backend. |
| 870 | * |
| 871 | * This function checks whether the provided backend buffer type is associated |
| 872 | * with the CANN backend based on the comparison of its name retrieval function |
| 873 | * pointer. |
| 874 | * |
| 875 | * @param buft Pointer to the backend buffer type to check. |
| 876 | * @return bool Returns true if the buffer type is associated with the CANN |
| 877 | * backend, otherwise false. |
| 878 | */ |
| 879 | static bool ggml_backend_buft_is_cann(ggml_backend_buffer_type_t buft) { |
| 880 | return buft->iface.get_name == ggml_backend_cann_buffer_type_name; |
| 881 | } |
| 882 | |
| 883 | /** |
| 884 | * @brief Free resources associated with a CANN buffer. |
| 885 | * |
| 886 | * This function frees the resources associated with a CANN buffer, including |
| 887 | * its context. |
| 888 | * |
| 889 | * @param buffer The CANN buffer to free. |
| 890 | */ |
| 891 | static void ggml_backend_cann_buffer_free_buffer(ggml_backend_buffer_t buffer) { |
| 892 | ggml_backend_cann_buffer_context * ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 893 | delete ctx; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * @brief Retrieve the base pointer of a CANN buffer. |
| 898 | * |
| 899 | * This function returns the base pointer of a CANN buffer, which points to the |
| 900 | * device memory allocated for the buffer. |
| 901 | * |
| 902 | * @param buffer The CANN buffer whose base pointer is to be retrieved. |
| 903 | * @return A pointer to the base of the device memory allocated for the buffer. |
| 904 | */ |
| 905 | static void * ggml_backend_cann_buffer_get_base(ggml_backend_buffer_t buffer) { |
| 906 | ggml_backend_cann_buffer_context * ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 907 | return ctx->dev_ptr; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @brief Transform quantized Q4.0 tensor data into a format suitable for CANN |
| 912 | * processing. |
| 913 | * |
| 914 | * This function transforms quantized Q4.0 tensor data into a format suitable |
| 915 | * for CANN processing. It extracts quantization values and scales from the |
| 916 | * source data and prepares them in a format expected by CANN operations. |
| 917 | * |
| 918 | * @param tensor Pointer to the tensor information. |
| 919 | * @param src Pointer to the source data in Q4.0 format. |
| 920 | * @param dst Pointer to the destination buffer where transformed data will be |
| 921 | * stored. |
| 922 | */ |
| 923 | static void ggml_backend_cann_transform_q4_0(const ggml_tensor * tensor, const void * src, void * dst) { |
| 924 | int64_t n_elems = ggml_nelements(tensor); |
| 925 | int64_t groups = n_elems / QK4_0; |
| 926 | size_t quant_bytes = n_elems * sizeof(uint8_t) / 2; |
| 927 | |
| 928 | uint8_t * quant_offset = (uint8_t *) dst; |
| 929 | uint16_t * scale_offset = (uint16_t *) ((char *) dst + quant_bytes); |
| 930 | |
| 931 | for (int i = 0; i < groups; i++) { |
| 932 | const block_q4_0 * group = (const block_q4_0 *) ((const char *) src + i * sizeof(block_q4_0)); |
| 933 | *scale_offset = group->d; |
| 934 | scale_offset++; |
| 935 | |
| 936 | // 0-15 |
| 937 | for (int j = 0; j < QK4_0 / 2; j += 2) { |
| 938 | (*quant_offset) = (group->qs[j] & 0x0F); |
| 939 | (*quant_offset) |= ((group->qs[j + 1] << 4)); |
| 940 | quant_offset++; |
| 941 | } |
| 942 | |
| 943 | // 16-31 |
| 944 | for (int j = 0; j < QK4_0 / 2; j += 2) { |
| 945 | (*quant_offset) = (group->qs[j] >> 4); |
| 946 | (*quant_offset) |= (group->qs[j + 1] & 0xF0); |
| 947 | quant_offset++; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | // put (uint4b_t -8) into int4b_t |
| 952 | for (quant_offset = (uint8_t *) dst; quant_offset < (uint8_t *) dst + quant_bytes; quant_offset++) { |
| 953 | (*quant_offset) ^= 0x88; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * @brief Transform CANN processed data back into quantized Q4.0 format. |
| 959 | * |
| 960 | * This function transforms CANN processed data back into quantized Q4.0 format. |
| 961 | * It reverses the transformation performed by |
| 962 | * ggml_backend_cann_transform_q4_0(), converting the data back into its |
| 963 | * original quantized form. |
| 964 | * |
| 965 | * @param tensor Pointer to the tensor information. |
| 966 | * @param src Pointer to the source buffer containing transformed data. |
| 967 | * @param dst Pointer to the destination buffer where the Q4.0 formatted data |
| 968 | * will be stored. |
| 969 | */ |
| 970 | static void ggml_backend_cann_transform_back_q4_0(const ggml_tensor * tensor, void * src, void * dst) { |
| 971 | int64_t n_elems = ggml_nelements(tensor); |
| 972 | int64_t groups = n_elems / QK4_0; |
| 973 | size_t quant_bytes = n_elems * sizeof(uint8_t) / 2; |
| 974 | |
| 975 | uint8_t * quant_offset = (uint8_t *) src; |
| 976 | uint16_t * scale_offset = (uint16_t *) ((char *) src + quant_bytes); |
| 977 | |
| 978 | for (; quant_offset < (uint8_t *) src + quant_bytes; quant_offset++) { |
| 979 | (*quant_offset) ^= 0x88; |
| 980 | } |
| 981 | quant_offset = (uint8_t *) src; |
| 982 | |
| 983 | for (int i = 0; i < groups; i++) { |
| 984 | block_q4_0 * group = (block_q4_0 *) ((char *) dst + i * sizeof(block_q4_0)); |
| 985 | group->d = *scale_offset; |
| 986 | scale_offset++; |
| 987 | |
| 988 | // 0-15 |
| 989 | for (int j = 0; j < QK4_0 / 2; j += 2) { |
| 990 | group->qs[j] = ((*quant_offset) & 0x0F); |
| 991 | group->qs[j + 1] = ((*quant_offset) >> 4); |
| 992 | quant_offset++; |
| 993 | } |
| 994 | |
| 995 | // 16-31 |
| 996 | for (int j = 0; j < QK4_0 / 2; j += 2) { |
| 997 | group->qs[j] |= ((*quant_offset) << 4); |
| 998 | group->qs[j + 1] |= ((*quant_offset) & 0xF0); |
| 999 | quant_offset++; |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * @brief Transform quantized Q8.0 tensor data into a format suitable for CANN |
| 1006 | * processing. |
| 1007 | * |
| 1008 | * This function transforms quantized Q8.0 tensor data into a format suitable |
| 1009 | * for CANN processing. It extracts quantization values and scales from the |
| 1010 | * source data and prepares them in a format expected by CANN operations. |
| 1011 | * |
| 1012 | * @param tensor Pointer to the tensor information. |
| 1013 | * @param src Pointer to the source data in Q8.0 format. |
| 1014 | * @param dst Pointer to the destination buffer where transformed data will be |
| 1015 | * stored. |
| 1016 | */ |
| 1017 | static void ggml_backend_cann_transform_q8_0(const ggml_tensor * tensor, const void * src, void * dst) { |
| 1018 | int64_t n_elems = ggml_nelements(tensor); |
| 1019 | int64_t groups = n_elems / QK8_0; |
| 1020 | size_t quant_bytes = n_elems * sizeof(uint8_t); |
| 1021 | |
| 1022 | uint8_t * quant_offset = (uint8_t *) dst; |
| 1023 | uint16_t * scale_offset = (uint16_t *) ((char *) dst + quant_bytes); |
| 1024 | |
| 1025 | for (int i = 0; i < groups; i++) { |
| 1026 | const block_q8_0 * group = (const block_q8_0 *) ((const char *) src + i * sizeof(block_q8_0)); |
| 1027 | *scale_offset = group->d; |
| 1028 | scale_offset++; |
| 1029 | size_t group_quant_size = QK8_0 * sizeof(uint8_t); |
| 1030 | memcpy(quant_offset, group->qs, group_quant_size); |
| 1031 | quant_offset += group_quant_size; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * @brief Transform CANN processed data back into quantized Q8.0 format. |
| 1037 | * |
| 1038 | * This function transforms CANN processed data back into quantized Q8.0 format. |
| 1039 | * It reverses the transformation performed by |
| 1040 | * ggml_backend_cann_transform_q8_0(), converting the data back into its |
| 1041 | * original quantized form. |
| 1042 | * |
| 1043 | * @param tensor Pointer to the tensor information. |
| 1044 | * @param src Pointer to the source buffer containing transformed data. |
| 1045 | * @param dst Pointer to the destination buffer where the Q8.0 formatted data |
| 1046 | * will be stored. |
| 1047 | */ |
| 1048 | static void ggml_backend_cann_transform_back_q8_0(const ggml_tensor * tensor, const void * src, void * dst) { |
| 1049 | int64_t n_elems = ggml_nelements(tensor); |
| 1050 | int64_t groups = n_elems / QK8_0; |
| 1051 | size_t quant_bytes = n_elems * sizeof(uint8_t); |
| 1052 | |
| 1053 | const uint8_t * quant_offset = (const uint8_t *) src; |
| 1054 | const uint16_t * scale_offset = (const uint16_t *) ((const char *) src + quant_bytes); |
| 1055 | |
| 1056 | for (int i = 0; i < groups; i++) { |
| 1057 | block_q8_0 * group = (block_q8_0 *) ((char *) dst + i * sizeof(block_q8_0)); |
| 1058 | group->d = *scale_offset; |
| 1059 | scale_offset++; |
| 1060 | size_t group_quant_size = QK8_0 * sizeof(uint8_t); |
| 1061 | memcpy(group->qs, quant_offset, group_quant_size); |
| 1062 | quant_offset += group_quant_size; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * @brief Transform tensor data based on its type for CANN processing. |
| 1068 | * |
| 1069 | * This function transforms tensor data based on its quantization type for CANN |
| 1070 | * processing. It dispatches the transformation based on the tensor's type to |
| 1071 | * specialized functions handling Q4.0 and Q8.0 formats. |
| 1072 | * |
| 1073 | * @param tensor Pointer to the tensor information. |
| 1074 | * @param src Pointer to the source data to be transformed. |
| 1075 | * @param dst Pointer to the destination buffer where transformed data will be |
| 1076 | * stored. |
| 1077 | */ |
| 1078 | static void ggml_backend_cann_transform(const ggml_tensor * tensor, const void * src, void * dst) { |
| 1079 | switch (tensor->type) { |
| 1080 | case GGML_TYPE_Q4_0: |
| 1081 | ggml_backend_cann_transform_q4_0(tensor, src, dst); |
| 1082 | break; |
| 1083 | case GGML_TYPE_Q8_0: |
| 1084 | ggml_backend_cann_transform_q8_0(tensor, src, dst); |
| 1085 | break; |
| 1086 | default: |
| 1087 | break; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /** |
| 1092 | * @brief Transform CANN processed data back into tensor data based on its type. |
| 1093 | * |
| 1094 | * This function transforms CANN processed data back into tensor data based on |
| 1095 | * its quantization type for Q4.0 and Q8.0 formats. It dispatches the |
| 1096 | * transformation based on the tensor's type to specialized functions. |
| 1097 | * |
| 1098 | * @param tensor Pointer to the tensor information. |
| 1099 | * @param src Pointer to the source data containing CANN processed data. |
| 1100 | * @param dst Pointer to the destination buffer where transformed tensor data |
| 1101 | * will be stored. |
| 1102 | */ |
| 1103 | static void ggml_backend_cann_transform_back(const ggml_tensor * tensor, void * src, void * dst) { |
| 1104 | switch (tensor->type) { |
| 1105 | case GGML_TYPE_Q4_0: |
| 1106 | ggml_backend_cann_transform_back_q4_0(tensor, src, dst); |
| 1107 | break; |
| 1108 | case GGML_TYPE_Q8_0: |
| 1109 | ggml_backend_cann_transform_back_q8_0(tensor, src, dst); |
| 1110 | break; |
| 1111 | default: |
| 1112 | break; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * @brief Check if transformation is needed for a given tensor type. |
| 1118 | * |
| 1119 | * This function checks if transformation is needed for a given tensor type |
| 1120 | * to prepare data for CANN processing. |
| 1121 | * |
| 1122 | * @param type The tensor type to check. |
| 1123 | * @return true if transformation is needed, false otherwise. |
| 1124 | */ |
| 1125 | static bool need_transform(ggml_type type) { |
| 1126 | switch (type) { |
| 1127 | case GGML_TYPE_Q4_0: |
| 1128 | case GGML_TYPE_Q8_0: |
| 1129 | return true; |
| 1130 | default: |
| 1131 | return false; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * @brief Initialize a tensor using data from a CANN buffer. |
| 1137 | * |
| 1138 | * This function initializes a tensor using data from a CANN buffer. |
| 1139 | * It handles special cases such as views and quantization. |
| 1140 | * |
| 1141 | * @param buffer The CANN buffer from which to initialize the tensor. |
| 1142 | * @param tensor Pointer to the tensor to be initialized. |
| 1143 | */ |
| 1144 | static enum ggml_status ggml_backend_cann_buffer_init_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor) { |
| 1145 | if (tensor->view_src != NULL && tensor->view_offs == 0) { |
| 1146 | GGML_ASSERT(tensor->view_src->buffer->buft == buffer->buft); |
| 1147 | return GGML_STATUS_SUCCESS; |
| 1148 | } |
| 1149 | |
| 1150 | // TODO: cann backend doesn't support quantized yet. Just leave the code |
| 1151 | // here. |
| 1152 | if (ggml_is_quantized(tensor->type)) { |
| 1153 | // Initialize padding to 0 to avoid possible NaN values |
| 1154 | size_t original_size = ggml_nbytes(tensor); |
| 1155 | size_t padded_size = ggml_backend_buft_get_alloc_size(buffer->buft, tensor); |
| 1156 | |
| 1157 | if (padded_size > original_size && tensor->view_src == nullptr) { |
| 1158 | size_t memset_size = padded_size - original_size; |
| 1159 | ACL_CHECK(aclrtMemset((char *) tensor->data + original_size, memset_size, 0, memset_size)); |
| 1160 | } |
| 1161 | } |
| 1162 | return GGML_STATUS_SUCCESS; |
| 1163 | } |
| 1164 | |
| 1165 | /** |
| 1166 | * @brief Workspace for caching NZ buffers per device. |
| 1167 | * |
| 1168 | * This struct manages a device buffer used in NZ computations. It supports |
| 1169 | * allocation, reallocation, and clearing of cached memory. The struct is |
| 1170 | * designed to be used with a global array, one per device. |
| 1171 | */ |
| 1172 | struct ggml_cann_nz_workspace { |
| 1173 | std::mutex mtx; // Protects ptr/allocated from concurrent access |
| 1174 | void * ptr; // Pointer to allocated device buffer |
| 1175 | size_t allocated; // Size of currently allocated buffer in bytes |
| 1176 | |
| 1177 | /** |
| 1178 | * @brief Constructor. Initializes the workspace with no allocated memory. |
| 1179 | */ |
| 1180 | ggml_cann_nz_workspace() : ptr(nullptr), allocated(0) {} |
| 1181 | |
| 1182 | /** |
| 1183 | * @brief Free cached memory and reset the workspace. |
| 1184 | * |
| 1185 | * If a buffer has been allocated, this function releases it using |
| 1186 | * aclrtFree and resets internal state. |
| 1187 | */ |
| 1188 | void clear() { |
| 1189 | if (ptr) { |
| 1190 | ACL_CHECK(aclrtFree(ptr)); |
| 1191 | ptr = nullptr; |
| 1192 | allocated = 0; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | /** |
| 1197 | * @brief Allocate or reallocate the workspace buffer. |
| 1198 | * |
| 1199 | * If the requested size is larger than the currently allocated size, |
| 1200 | * the old buffer will be freed and a new buffer of the requested size |
| 1201 | * will be allocated on the device. |
| 1202 | * |
| 1203 | * @param new_size Size in bytes to allocate for the workspace. |
| 1204 | */ |
| 1205 | void realloc(size_t new_size) { |
| 1206 | if (new_size > allocated) { |
| 1207 | clear(); |
| 1208 | ACL_CHECK(aclrtMalloc(&ptr, new_size, ACL_MEM_MALLOC_HUGE_FIRST)); |
| 1209 | allocated = new_size; |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * @brief Get the device buffer pointer. |
| 1215 | * |
| 1216 | * @return Pointer to the allocated buffer, or nullptr if not allocated. |
| 1217 | */ |
| 1218 | void * get() const { return ptr; } |
| 1219 | }; |
| 1220 | |
| 1221 | /** |
| 1222 | * @brief Global array of NZ workspaces, one per device. |
| 1223 | */ |
| 1224 | static ggml_cann_nz_workspace g_nz_workspaces[GGML_CANN_MAX_DEVICES]; |
| 1225 | |
| 1226 | /** |
| 1227 | * @brief Convert tensor weights to NZ format using Ascend CANN API. |
| 1228 | * |
| 1229 | * This function creates a transposed tensor descriptor and performs the |
| 1230 | * TransMatmulWeight operation. Converting tensor formats can significantly |
| 1231 | * improve performance on certain hardware. |
| 1232 | * |
| 1233 | * @param tensor Pointer to the input ggml_tensor containing the weights. |
| 1234 | * @param offset Byte offset within the tensor data buffer where weights start. |
| 1235 | * @param device device id. |
| 1236 | * |
| 1237 | * @note The workspace buffer used in this function is managed globally and reused |
| 1238 | * across calls. This reduces overhead from repeated memory allocation and deallocation. |
| 1239 | */ |
| 1240 | static void weight_format_to_nz(ggml_tensor * tensor, int device) { |
| 1241 | acl_tensor_ptr weightTransposed = ggml_cann_create_tensor(tensor, tensor->ne, tensor->nb, 2, ACL_FORMAT_ND, 0); |
| 1242 | uint64_t workspaceSize = 0; |
| 1243 | aclOpExecutor * executor; |
| 1244 | |
| 1245 | // TransMatmulWeight |
| 1246 | ACL_CHECK(aclnnTransMatmulWeightGetWorkspaceSize(weightTransposed.get(), &workspaceSize, &executor)); |
| 1247 | |
| 1248 | std::lock_guard<std::mutex> lock(g_nz_workspaces[device].mtx); |
| 1249 | // Avoid frequent malloc/free of the workspace. |
| 1250 | g_nz_workspaces[device].realloc(workspaceSize); |
| 1251 | |
| 1252 | void * g_nz_workspace = g_nz_workspaces[device].get(); |
| 1253 | |
| 1254 | ACL_CHECK(aclnnTransMatmulWeight(g_nz_workspace, workspaceSize, executor, nullptr)); |
| 1255 | } |
| 1256 | |
| 1257 | // TODO: need handle tensor which has paddings. |
| 1258 | /** |
| 1259 | * @brief Set tensor data in a CANN buffer. |
| 1260 | * |
| 1261 | * This function sets tensor data in a CANN buffer, handling transformations |
| 1262 | * if needed based on the tensor's type. It supports multi-threaded calls |
| 1263 | * where different threads write different chunks of the same tensor. |
| 1264 | * |
| 1265 | * For quantized tensors (Q4_0/Q8_0), data is staged in a host buffer and |
| 1266 | * the format transform is deferred until all chunks are written. |
| 1267 | * For NZ weight tensors, chunks are uploaded directly but the ND-to-NZ |
| 1268 | * conversion is deferred until all chunks are written. |
| 1269 | * |
| 1270 | * @param buffer The CANN buffer where the tensor data will be set. |
| 1271 | * @param tensor Pointer to the tensor whose data will be set. |
| 1272 | * @param data Pointer to the source data to be copied into the tensor. |
| 1273 | * @param offset Offset in the source data from where to start copying. |
| 1274 | * @param size Size of the data to be copied, in bytes. |
| 1275 | */ |
| 1276 | static void ggml_backend_cann_buffer_set_tensor(ggml_backend_buffer_t buffer, |
| 1277 | ggml_tensor * tensor, |
| 1278 | const void * data, |
| 1279 | size_t offset, |
| 1280 | size_t size) { |
| 1281 | ggml_backend_cann_buffer_context * ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 1282 | |
| 1283 | ggml_cann_set_device(ctx->device); |
| 1284 | |
| 1285 | // Only check env once. |
| 1286 | static bool weight_to_nz = parse_bool(get_env_as_lowercase("GGML_CANN_WEIGHT_NZ").value_or("on")); |
| 1287 | |
| 1288 | bool is_quantized = need_transform(tensor->type); |
| 1289 | bool is_nz = !is_quantized && tensor->type != GGML_TYPE_BF16 && weight_to_nz && |
| 1290 | is_matmul_weight((const ggml_tensor *) tensor); |
| 1291 | |
| 1292 | // Plain tensor (not quantized, not NZ): direct copy, no tracking needed |
| 1293 | if (!is_quantized && !is_nz) { |
| 1294 | ACL_CHECK(aclrtMemcpy((char *) tensor->data + offset, size, data, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 1295 | return; |
| 1296 | } |
| 1297 | |
| 1298 | // Single-shot write (full tensor at once): handle directly without tracking overhead |
| 1299 | if (offset == 0 && size == ggml_nbytes(tensor)) { |
| 1300 | if (is_quantized) { |
| 1301 | void * transform_buffer = malloc(size); |
| 1302 | ggml_backend_cann_transform(tensor, data, transform_buffer); |
| 1303 | ACL_CHECK(aclrtMemcpy(tensor->data, size, transform_buffer, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 1304 | free(transform_buffer); |
| 1305 | } else { |
| 1306 | // NZ weight |
| 1307 | GGML_ASSERT(tensor->ne[2] == 1); |
| 1308 | GGML_ASSERT(tensor->ne[3] == 1); |
| 1309 | ACL_CHECK(aclrtMemcpy(tensor->data, size, data, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 1310 | weight_format_to_nz(tensor, ctx->device); |
| 1311 | } |
| 1312 | return; |
| 1313 | } |
| 1314 | |
| 1315 | // Chunked write: use tracker to accumulate progress and defer transform/conversion |
| 1316 | TensorSetTracker * tracker = ctx->get_or_create_tracker(tensor); |
| 1317 | std::unique_lock<std::mutex> lock(tracker->mtx); |
| 1318 | |
| 1319 | if (is_quantized) { |
| 1320 | // Stage data in host buffer; transform requires full tensor data |
| 1321 | if (tracker->host_buffer.empty()) { |
| 1322 | tracker->host_buffer.resize(tracker->total_bytes); |
| 1323 | } |
| 1324 | memcpy(tracker->host_buffer.data() + offset, data, size); |
| 1325 | } else { |
| 1326 | // NZ weight: upload chunk to device immediately, defer conversion |
| 1327 | ACL_CHECK(aclrtMemcpy((char *) tensor->data + offset, size, data, size, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 1328 | } |
| 1329 | |
| 1330 | tracker->bytes_written += size; |
| 1331 | |
| 1332 | // All chunks received: perform deferred transform/conversion |
| 1333 | if (tracker->bytes_written >= tracker->total_bytes) { |
| 1334 | if (is_quantized) { |
| 1335 | void * transform_buffer = malloc(tracker->total_bytes); |
| 1336 | ggml_backend_cann_transform(tensor, tracker->host_buffer.data(), transform_buffer); |
| 1337 | ACL_CHECK(aclrtMemcpy(tensor->data, tracker->total_bytes, transform_buffer, tracker->total_bytes, ACL_MEMCPY_HOST_TO_DEVICE)); |
| 1338 | free(transform_buffer); |
| 1339 | } |
| 1340 | |
| 1341 | if (is_nz) { |
| 1342 | GGML_ASSERT(tensor->ne[2] == 1); |
| 1343 | GGML_ASSERT(tensor->ne[3] == 1); |
| 1344 | weight_format_to_nz(tensor, ctx->device); |
| 1345 | } |
| 1346 | |
| 1347 | // Unlock before removing tracker, as remove_tracker destroys the mutex |
| 1348 | lock.unlock(); |
| 1349 | ctx->remove_tracker(tensor); |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * @brief Get tensor data from a CANN buffer. |
| 1355 | * |
| 1356 | * This function retrieves tensor data from a CANN buffer, handling |
| 1357 | * transformations if needed based on the tensor's type. |
| 1358 | * |
| 1359 | * @param buffer The CANN buffer from which to retrieve tensor data. |
| 1360 | * @param tensor Pointer to the tensor whose data will be retrieved. |
| 1361 | * @param data Pointer to the destination buffer where the tensor data will be |
| 1362 | * copied. |
| 1363 | * @param offset Offset in the destination buffer where to start copying. |
| 1364 | * @param size Size of the data to be copied, in bytes. |
| 1365 | */ |
| 1366 | static void ggml_backend_cann_buffer_get_tensor(ggml_backend_buffer_t buffer, |
| 1367 | const ggml_tensor * tensor, |
| 1368 | void * data, |
| 1369 | size_t offset, |
| 1370 | size_t size) { |
| 1371 | ggml_backend_cann_buffer_context * ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 1372 | |
| 1373 | ggml_cann_set_device(ctx->device); |
| 1374 | |
| 1375 | if (!need_transform(tensor->type)) { |
| 1376 | ACL_CHECK(aclrtMemcpy(data, size, (char *) tensor->data + offset, size, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 1377 | } else { |
| 1378 | void * transform_buffer = malloc(size); |
| 1379 | ACL_CHECK(aclrtMemcpy(transform_buffer, size, (char *) tensor->data + offset, size, ACL_MEMCPY_DEVICE_TO_HOST)); |
| 1380 | ggml_backend_cann_transform_back(tensor, transform_buffer, data); |
| 1381 | free(transform_buffer); |
| 1382 | } |
| 1383 | } |
| 1384 | |
| 1385 | /** |
| 1386 | * @brief Copy tensor data between CANN buffers if possible. |
| 1387 | * |
| 1388 | * This function copies tensor data between CANN buffers if the source and |
| 1389 | * destination buffers are CANN buffers and they meet the necessary conditions |
| 1390 | * (same device or devices can access each other). |
| 1391 | * |
| 1392 | * @param buffer The destination CANN buffer where the tensor data will be |
| 1393 | * copied. |
| 1394 | * @param src Pointer to the source tensor whose data will be copied. |
| 1395 | * @param dst Pointer to the destination tensor where the data will be copied. |
| 1396 | * @return true if the copy operation succeeded, false otherwise. |
| 1397 | */ |
| 1398 | static bool ggml_backend_cann_buffer_cpy_tensor(ggml_backend_buffer_t buffer, |
| 1399 | const ggml_tensor * src, |
| 1400 | ggml_tensor * dst) { |
| 1401 | if (ggml_backend_buft_is_cann(src->buffer->buft)) { |
| 1402 | ggml_backend_cann_buffer_context * src_ctx = (ggml_backend_cann_buffer_context *) src->buffer->context; |
| 1403 | ggml_backend_cann_buffer_context * dst_ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 1404 | |
| 1405 | size_t memcpy_size = ggml_nbytes(src); |
| 1406 | // Same device. |
| 1407 | if (src_ctx->device == dst_ctx->device) { |
| 1408 | ACL_CHECK(aclrtMemcpy((char *) dst->data, memcpy_size, (const char *) src->data, memcpy_size, |
| 1409 | ACL_MEMCPY_DEVICE_TO_DEVICE)); |
| 1410 | return true; |
| 1411 | } else { |
| 1412 | #ifdef ASCEND_310P |
| 1413 | // TODO: Support 310p P2P copy |
| 1414 | return false; |
| 1415 | #endif |
| 1416 | // Different device but can access by peer. |
| 1417 | int32_t canAccessPeer = 0; |
| 1418 | ACL_CHECK(aclrtDeviceCanAccessPeer(&canAccessPeer, src_ctx->device, dst_ctx->device)); |
| 1419 | if (canAccessPeer) { |
| 1420 | ggml_cann_set_device(src_ctx->device); |
| 1421 | ACL_CHECK(aclrtDeviceEnablePeerAccess(dst_ctx->device, 0)); |
| 1422 | ACL_CHECK(aclrtMemcpy((char *) dst->data, memcpy_size, (const char *) src->data, memcpy_size, |
| 1423 | ACL_MEMCPY_DEVICE_TO_DEVICE)); |
| 1424 | return true; |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | return false; |
| 1429 | } |
| 1430 | |
| 1431 | /** |
| 1432 | * @brief Set a region of a tensor's device memory to a specified value. |
| 1433 | * |
| 1434 | * @param buffer The CANN buffer containing the tensor. |
| 1435 | * @param tensor Pointer to the tensor whose memory will be set. |
| 1436 | * @param value The value to which each byte in the region will be set. |
| 1437 | * @param offset Byte offset within the tensor's data to start setting. |
| 1438 | * @param size Number of bytes to set. |
| 1439 | */ |
| 1440 | static void ggml_backend_cann_buffer_memset_tensor(ggml_backend_buffer_t buffer, ggml_tensor * tensor, uint8_t value, size_t offset, size_t size) { |
| 1441 | ggml_backend_cann_buffer_context * ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 1442 | |
| 1443 | ggml_cann_set_device(ctx->device); |
| 1444 | ACL_CHECK(aclrtMemset((char *) tensor->data + offset, size, value, size)); |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * @brief Clear a CANN buffer by setting all its memory to a specified value. |
| 1449 | * |
| 1450 | * This function clears a CANN buffer by setting all its memory to a specified |
| 1451 | * value. |
| 1452 | * |
| 1453 | * @param buffer The CANN buffer to be cleared. |
| 1454 | * @param value The value to which each byte in the buffer will be set. |
| 1455 | */ |
| 1456 | static void ggml_backend_cann_buffer_clear(ggml_backend_buffer_t buffer, uint8_t value) { |
| 1457 | ggml_backend_cann_buffer_context * ctx = (ggml_backend_cann_buffer_context *) buffer->context; |
| 1458 | |
| 1459 | ggml_cann_set_device(ctx->device); |
| 1460 | ACL_CHECK(aclrtMemset(ctx->dev_ptr, buffer->size, value, buffer->size)); |
| 1461 | } |
| 1462 | |
| 1463 | /** |
| 1464 | * @brief Interface for a CANN buffer in the backend. |
| 1465 | * |
| 1466 | * This structure defines function pointers to operations that can be performed |
| 1467 | * on a CANN buffer within the backend. |
| 1468 | */ |
| 1469 | static const ggml_backend_buffer_i ggml_backend_cann_buffer_interface = { |
| 1470 | /* .free_buffer = */ ggml_backend_cann_buffer_free_buffer, |
| 1471 | /* .get_base = */ ggml_backend_cann_buffer_get_base, |
| 1472 | /* .init_tensor = */ ggml_backend_cann_buffer_init_tensor, |
| 1473 | /* .memset_tensor = */ ggml_backend_cann_buffer_memset_tensor, |
| 1474 | /* .set_tensor = */ ggml_backend_cann_buffer_set_tensor, |
| 1475 | /* .get_tensor = */ ggml_backend_cann_buffer_get_tensor, |
| 1476 | /* .set_tensor_2d = */ NULL, |
| 1477 | /* .get_tensor_2d = */ NULL, |
| 1478 | /* .cpy_tensor = */ ggml_backend_cann_buffer_cpy_tensor, |
| 1479 | /* .clear = */ ggml_backend_cann_buffer_clear, |
| 1480 | /* .reset = */ NULL, |
| 1481 | /* .set_tensor_n = */ NULL, |
| 1482 | /* .get_tensor_n = */ NULL, |
| 1483 | /* .cpy_tensor_n = */ NULL, |
| 1484 | }; |
| 1485 | |
| 1486 | /** |
| 1487 | * @brief Allocates a new CANN buffer of the specified type and size. |
| 1488 | * |
| 1489 | * This function allocates a new CANN buffer on the specified device with the |
| 1490 | * given size. |
| 1491 | * |
| 1492 | * @param buft Pointer to the buffer type context. |
| 1493 | * @param size Size in bytes of the buffer to allocate. |
| 1494 | * @return Pointer to the allocated buffer, or nullptr if allocation fails. |
| 1495 | */ |
| 1496 | static ggml_backend_buffer_t ggml_backend_cann_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) { |
| 1497 | ggml_backend_cann_buffer_type_context * buft_ctx = (ggml_backend_cann_buffer_type_context *) buft->context; |
| 1498 | |
| 1499 | ggml_cann_set_device(buft_ctx->device); |
| 1500 | |
| 1501 | const size_t alignment = 128; |
| 1502 | size = GGML_PAD(size, alignment); |
| 1503 | if (size == 0) { |
| 1504 | size = alignment; |
| 1505 | } |
| 1506 | void * dev_ptr; |
| 1507 | aclError err = aclrtMalloc(&dev_ptr, size, ACL_MEM_MALLOC_HUGE_FIRST); |
| 1508 | if (err != ACL_SUCCESS) { |
| 1509 | GGML_LOG_ERROR("%s: allocating %.2f MiB on device %d: aclrtMalloc failed: %s\n", __func__, |
| 1510 | size / 1024.0 / 1024.0, buft_ctx->device, aclGetRecentErrMsg()); |
| 1511 | return nullptr; |
| 1512 | } |
| 1513 | |
| 1514 | ggml_backend_cann_buffer_context * ctx = new ggml_backend_cann_buffer_context(buft_ctx->device, dev_ptr); |
| 1515 | |
| 1516 | return ggml_backend_buffer_init(buft, ggml_backend_cann_buffer_interface, ctx, size); |
| 1517 | } |
| 1518 | |
| 1519 | /** |
| 1520 | * @brief Retrieves the memory alignment requirement for CANN buffers of this |
| 1521 | * type. |
| 1522 | * |
| 1523 | * This function returns the alignment requirement in bytes for memory allocated |
| 1524 | * by the CANN buffer type. |
| 1525 | * |
| 1526 | * @param buft Pointer to the buffer type context (unused in this |
| 1527 | * implementation). |
| 1528 | * @return The alignment requirement in bytes (fixed at 128 bytes for CANN |
| 1529 | * buffers). |
| 1530 | */ |
| 1531 | static size_t ggml_backend_cann_buffer_type_get_alignment(ggml_backend_buffer_type_t buft) { |
| 1532 | return 128; |
| 1533 | |
| 1534 | GGML_UNUSED(buft); |
| 1535 | } |
| 1536 | |
| 1537 | /** |
| 1538 | * @brief Calculates the allocation size required for a tensor in a CANN buffer. |
| 1539 | * |
| 1540 | * Computes the total allocation size needed for storing the tensor's data in a |
| 1541 | * CANN buffer, considering any necessary padding or adjustments for quantized |
| 1542 | * types. |
| 1543 | * |
| 1544 | * @param buft Pointer to the buffer type context (unused in this |
| 1545 | * implementation). |
| 1546 | * @param tensor Pointer to the tensor for which the allocation size is |
| 1547 | * calculated. |
| 1548 | * @return The total allocation size in bytes required for the tensor in the |
| 1549 | * CANN buffer. |
| 1550 | */ |
| 1551 | static size_t ggml_backend_cann_buffer_type_get_alloc_size(ggml_backend_buffer_type_t buft, |
| 1552 | const ggml_tensor * tensor) { |
| 1553 | size_t size = ggml_nbytes(tensor); |
| 1554 | int64_t ne0 = tensor->ne[0]; |
| 1555 | |
| 1556 | // Only check env once. |
| 1557 | static bool weight_to_nz = parse_bool(get_env_as_lowercase("GGML_CANN_WEIGHT_NZ").value_or("on")); |
| 1558 | |
| 1559 | // last line must bigger than 32, because every single op deal at |
| 1560 | // least 32 bytes. |
| 1561 | // TODO: quantized type? |
| 1562 | // int64_t line_size = ne0 * ggml_element_size(tensor); |
| 1563 | // int64_t line_size_align_32 = (line_size + 31) & ~31; |
| 1564 | // size += (line_size_align_32 - line_size); |
| 1565 | if (ggml_is_quantized(tensor->type)) { |
| 1566 | if (ne0 % MATRIX_ROW_PADDING != 0) { |
| 1567 | size += ggml_row_size(tensor->type, MATRIX_ROW_PADDING - ne0 % MATRIX_ROW_PADDING); |
| 1568 | } |
| 1569 | } else if (weight_to_nz && tensor->type != GGML_TYPE_BF16 |
| 1570 | && is_matmul_weight((const ggml_tensor *) tensor)) { |
| 1571 | // NZ format weight are not support quantized yet. |
| 1572 | // If ND tensor transform to NZ, size may changed. |
| 1573 | int64_t shape[] = { tensor->ne[1], tensor->ne[0] }; |
| 1574 | GGML_ASSERT(tensor->ne[2] == 1); |
| 1575 | GGML_ASSERT(tensor->ne[3] == 1); |
| 1576 | const aclIntArray * acl_shape = aclCreateIntArray(shape, 2); |
| 1577 | size_t new_size; |
| 1578 | ACL_CHECK(aclnnCalculateMatmulWeightSizeV2(acl_shape, ggml_cann_type_mapping(tensor->type), &new_size)); |
| 1579 | ACL_CHECK(aclDestroyIntArray(acl_shape)); |
| 1580 | size = std::max(size, new_size); |
| 1581 | } |
| 1582 | |
| 1583 | return size; |
| 1584 | |
| 1585 | GGML_UNUSED(buft); |
| 1586 | } |
| 1587 | |
| 1588 | static bool ggml_backend_cann_buffer_type_is_host(ggml_backend_buffer_type_t buft) { |
| 1589 | return false; |
| 1590 | |
| 1591 | GGML_UNUSED(buft); |
| 1592 | } |
| 1593 | |
| 1594 | /** |
| 1595 | * @brief Interface for managing CANN buffer types in the GGML backend. |
| 1596 | * |
| 1597 | * Provides function pointers for allocating, querying properties, and managing |
| 1598 | * memory for CANN buffer types in the GGML backend. |
| 1599 | */ |
| 1600 | static const ggml_backend_buffer_type_i ggml_backend_cann_buffer_type_interface = { |
| 1601 | /* .get_name = */ ggml_backend_cann_buffer_type_name, |
| 1602 | /* .alloc_buffer = */ ggml_backend_cann_buffer_type_alloc_buffer, |
| 1603 | /* .alloc_buffer_n = */ NULL, |
| 1604 | /* .get_alignment = */ ggml_backend_cann_buffer_type_get_alignment, |
| 1605 | /* .get_max_size = */ NULL, // defaults to SIZE_MAX |
| 1606 | /* .get_alloc_size = */ ggml_backend_cann_buffer_type_get_alloc_size, |
| 1607 | /* .get_alloc_size_n = */ NULL, |
| 1608 | /* .is_host = */ ggml_backend_cann_buffer_type_is_host, |
| 1609 | }; |
| 1610 | |
| 1611 | /** |
| 1612 | * @brief Retrieves the CANN buffer type for a specified device. |
| 1613 | * |
| 1614 | * This function initializes and returns the buffer type interface associated |
| 1615 | * with the given device. It ensures thread-safe access using a mutex. |
| 1616 | * |
| 1617 | * @param device The device index for which to retrieve the buffer type. |
| 1618 | * @return A pointer to the buffer type interface for the specified device, or |
| 1619 | * nullptr if the device index is out of range. |
| 1620 | */ |
| 1621 | ggml_backend_buffer_type_t ggml_backend_cann_buffer_type(int32_t device) { |
| 1622 | static std::mutex mutex; |
| 1623 | std::lock_guard<std::mutex> lock(mutex); |
| 1624 | |
| 1625 | if (device >= ggml_backend_cann_get_device_count()) { |
| 1626 | return nullptr; |
| 1627 | } |
| 1628 | |
| 1629 | static ggml_backend_buffer_type ggml_backend_cann_buffer_types[GGML_CANN_MAX_DEVICES]; |
| 1630 | |
| 1631 | static bool ggml_backend_cann_buffer_type_initialized = false; |
| 1632 | |
| 1633 | if (!ggml_backend_cann_buffer_type_initialized) { |
| 1634 | for (int32_t i = 0; i < ggml_cann_info().device_count; i++) { |
| 1635 | ggml_backend_cann_buffer_types[i] = { |
| 1636 | /* .iface = */ ggml_backend_cann_buffer_type_interface, |
| 1637 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cann_reg(), i), |
| 1638 | /* .context = */ |
| 1639 | new ggml_backend_cann_buffer_type_context{ i, "CANN" + std::to_string(i) }, |
| 1640 | }; |
| 1641 | } |
| 1642 | ggml_backend_cann_buffer_type_initialized = true; |
| 1643 | } |
| 1644 | |
| 1645 | return &ggml_backend_cann_buffer_types[device]; |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * @brief Retrieves the name associated with a CANN host buffer type. |
| 1650 | * |
| 1651 | * This function returns the descriptive name associated with the specified |
| 1652 | * CANN host buffer type context. |
| 1653 | * |
| 1654 | * @param buft Pointer to the host buffer type context. |
| 1655 | * @return Const pointer to the C-style string containing the name. |
| 1656 | */ |
| 1657 | static const char * ggml_backend_cann_host_buffer_type_name(ggml_backend_buffer_type_t buft) { |
| 1658 | return "CANN_Host"; |
| 1659 | |
| 1660 | GGML_UNUSED(buft); |
| 1661 | } |
| 1662 | |
| 1663 | /** |
| 1664 | * @brief Retrieves the name associated with a CANN host buffer. |
| 1665 | * |
| 1666 | * This function returns the descriptive name associated with the specified |
| 1667 | * CANN host buffer context. |
| 1668 | * |
| 1669 | * @param buft Pointer to the host buffer context. |
| 1670 | * @return Const pointer to the C-style string containing the name. |
| 1671 | */ |
| 1672 | static const char * ggml_backend_cann_host_buffer_name(ggml_backend_buffer_t buffer) { |
| 1673 | return "CANN_Host"; |
| 1674 | |
| 1675 | GGML_UNUSED(buffer); |
| 1676 | } |
| 1677 | |
| 1678 | /** |
| 1679 | * @brief Free resources associated with a CANN host buffer. |
| 1680 | * |
| 1681 | * This function frees the resources associated with a CANN host buffer, including |
| 1682 | * its context. |
| 1683 | * |
| 1684 | * @param buffer The CANN host buffer to free. |
| 1685 | */ |
| 1686 | static void ggml_backend_cann_host_buffer_free(ggml_backend_buffer_t buffer) { |
| 1687 | ACL_CHECK(aclrtFreeHost(buffer->context)); |
| 1688 | } |
| 1689 | |
| 1690 | /** |
| 1691 | * @brief Allocates a new CANN host buffer of the specified size. |
| 1692 | * |
| 1693 | * This function allocates a new CANN host buffer with the given size. |
| 1694 | * @param size Size in bytes of the host buffer to allocate. |
| 1695 | * @return Pointer to the allocated host buffer, or nullptr if allocation fails. |
| 1696 | */ |
| 1697 | static void * ggml_cann_host_malloc(size_t size) { |
| 1698 | if (getenv("GGML_CANN_NO_PINNED") != nullptr) { |
| 1699 | return nullptr; |
| 1700 | } |
| 1701 | |
| 1702 | const size_t alignment = 128; |
| 1703 | size = GGML_PAD(size, alignment); |
| 1704 | if (size == 0) { |
| 1705 | size = alignment; |
| 1706 | } |
| 1707 | |
| 1708 | void * hostPtr = nullptr; |
| 1709 | aclError err = aclrtMallocHost((void **) &hostPtr, size); |
| 1710 | if (err != ACL_SUCCESS) { |
| 1711 | GGML_LOG_WARN("%s: failed to allocate %.2f MiB of pinned memory: %s\n", __func__, size / 1024.0 / 1024.0, |
| 1712 | aclGetRecentErrMsg()); |
| 1713 | return nullptr; |
| 1714 | } |
| 1715 | return hostPtr; |
| 1716 | } |
| 1717 | |
| 1718 | /** |
| 1719 | * @brief Allocates a new CANN host buffer of the specified type and size. |
| 1720 | * |
| 1721 | * @param buft Pointer to the host buffer type context. |
| 1722 | * @param size Size in bytes of the host buffer to allocate. |
| 1723 | * @return Pointer to the allocated host buffer, or CPU buffer pointer if allocation fails. |
| 1724 | */ |
| 1725 | static ggml_backend_buffer_t ggml_backend_cann_host_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, |
| 1726 | size_t size) { |
| 1727 | void * hostPtr = ggml_cann_host_malloc(size); |
| 1728 | |
| 1729 | if (hostPtr == nullptr) { |
| 1730 | // fallback to cpu buffer |
| 1731 | return ggml_backend_buft_alloc_buffer(ggml_backend_cpu_buffer_type(), size); |
| 1732 | } |
| 1733 | |
| 1734 | ggml_backend_buffer_t buffer = ggml_backend_cpu_buffer_from_ptr(hostPtr, size); |
| 1735 | buffer->buft = buft; |
| 1736 | buffer->iface.free_buffer = ggml_backend_cann_host_buffer_free; |
| 1737 | |
| 1738 | return buffer; |
| 1739 | } |
| 1740 | |
| 1741 | /** |
| 1742 | * @brief Interface for managing CANN host buffer types in the GGML backend. |
| 1743 | * |
| 1744 | * Provides function pointers for allocating, querying properties, and managing |
| 1745 | * memory for CANN buffer types in the GGML backend. |
| 1746 | */ |
| 1747 | ggml_backend_buffer_type_t ggml_backend_cann_host_buffer_type() { |
| 1748 | static struct ggml_backend_buffer_type ggml_backend_cann_buffer_type_host = { |
| 1749 | /* .iface = */ { |
| 1750 | /* .get_name = */ ggml_backend_cann_host_buffer_type_name, |
| 1751 | /* .alloc_buffer = */ ggml_backend_cann_host_buffer_type_alloc_buffer, |
| 1752 | /* .alloc_buffer_n = */ NULL, |
| 1753 | /* .get_alignment = */ ggml_backend_cpu_buffer_type()->iface.get_alignment, |
| 1754 | /* .get_max_size = */ NULL, // defaults to SIZE_MAX |
| 1755 | /* .get_alloc_size = */ ggml_backend_cpu_buffer_type()->iface.get_alloc_size, |
| 1756 | /* .get_alloc_size_n = */ NULL, |
| 1757 | /* .is_host = */ ggml_backend_cpu_buffer_type()->iface.is_host, |
| 1758 | }, |
| 1759 | /* .device = */ |
| 1760 | ggml_backend_reg_dev_get(ggml_backend_cann_reg(), 0), |
| 1761 | /* .context = */ nullptr, |
| 1762 | }; |
| 1763 | |
| 1764 | return &ggml_backend_cann_buffer_type_host; |
| 1765 | } |
| 1766 | |
| 1767 | /** |
| 1768 | * @brief Computes the forward operation for a given tensor using CANN |
| 1769 | * operations. |
| 1770 | * |
| 1771 | * This function selects the appropriate CANN operation based on the type of |
| 1772 | * operation specified in the tensor and performs the computation. |
| 1773 | * |
| 1774 | * @param ctx The CANN context containing necessary resources and |
| 1775 | * configurations. |
| 1776 | * @param dst The destination tensor where the result of the computation will be |
| 1777 | * stored. |
| 1778 | * @return true if the computation was successful; false otherwise. |
| 1779 | */ |
| 1780 | static bool ggml_cann_compute_forward(ggml_backend_cann_context & ctx, struct ggml_tensor * dst) { |
| 1781 | switch (dst->op) { |
| 1782 | case GGML_OP_REPEAT: |
| 1783 | ggml_cann_repeat(ctx, dst); |
| 1784 | break; |
| 1785 | case GGML_OP_GET_ROWS: |
| 1786 | ggml_cann_get_rows(ctx, dst); |
| 1787 | break; |
| 1788 | case GGML_OP_SET_ROWS: |
| 1789 | ggml_cann_set_rows(ctx, dst); |
| 1790 | break; |
| 1791 | case GGML_OP_DUP: |
| 1792 | ggml_cann_dup(ctx, dst); |
| 1793 | break; |
| 1794 | case GGML_OP_ADD: |
| 1795 | case GGML_OP_ADD1: |
| 1796 | ggml_cann_binary_op<aclnn_add>(ctx, dst); |
| 1797 | break; |
| 1798 | case GGML_OP_SUB: |
| 1799 | ggml_cann_binary_op<aclnn_sub>(ctx, dst); |
| 1800 | break; |
| 1801 | case GGML_OP_ACC: |
| 1802 | ggml_cann_acc(ctx, dst); |
| 1803 | break; |
| 1804 | case GGML_OP_MUL: |
| 1805 | ggml_cann_binary_op<aclnn_mul>(ctx, dst); |
| 1806 | break; |
| 1807 | case GGML_OP_DIV: |
| 1808 | ggml_cann_binary_op<aclnn_div>(ctx, dst); |
| 1809 | break; |
| 1810 | case GGML_OP_UNARY: |
| 1811 | switch (ggml_get_unary_op(dst)) { |
| 1812 | case GGML_UNARY_OP_ABS: |
| 1813 | GGML_CANN_CALL_OP_UNARY(Abs); |
| 1814 | break; |
| 1815 | case GGML_UNARY_OP_NEG: |
| 1816 | GGML_CANN_CALL_OP_UNARY(Neg); |
| 1817 | break; |
| 1818 | case GGML_UNARY_OP_GELU: |
| 1819 | case GGML_UNARY_OP_GELU_ERF: |
| 1820 | // aclnnGelu internally uses the erf-based approximation. |
| 1821 | GGML_CANN_CALL_OP_UNARY(Gelu); |
| 1822 | break; |
| 1823 | case GGML_UNARY_OP_SILU: |
| 1824 | GGML_CANN_CALL_OP_UNARY(Silu); |
| 1825 | break; |
| 1826 | case GGML_UNARY_OP_GELU_QUICK: |
| 1827 | { |
| 1828 | auto lambda = [](ggml_backend_cann_context & ctx, aclTensor * acl_src, aclTensor * acl_dst) { |
| 1829 | GGML_CANN_CALL_ACLNN_OP(ctx, GeluV2, acl_src, 0, acl_dst); |
| 1830 | }; |
| 1831 | ggml_cann_op_unary(lambda, ctx, dst); |
| 1832 | } |
| 1833 | break; |
| 1834 | case GGML_UNARY_OP_TANH: |
| 1835 | GGML_CANN_CALL_OP_UNARY(Tanh); |
| 1836 | break; |
| 1837 | case GGML_UNARY_OP_RELU: |
| 1838 | GGML_CANN_CALL_OP_UNARY(Relu); |
| 1839 | break; |
| 1840 | case GGML_UNARY_OP_SIGMOID: |
| 1841 | GGML_CANN_CALL_OP_UNARY(Sigmoid); |
| 1842 | break; |
| 1843 | case GGML_UNARY_OP_HARDSIGMOID: |
| 1844 | GGML_CANN_CALL_OP_UNARY(Hardsigmoid); |
| 1845 | break; |
| 1846 | case GGML_UNARY_OP_HARDSWISH: |
| 1847 | GGML_CANN_CALL_OP_UNARY(Hardswish); |
| 1848 | break; |
| 1849 | case GGML_UNARY_OP_EXP: |
| 1850 | GGML_CANN_CALL_OP_UNARY(Exp); |
| 1851 | break; |
| 1852 | case GGML_UNARY_OP_ELU: |
| 1853 | ggml_cann_elu(ctx, dst); |
| 1854 | break; |
| 1855 | case GGML_UNARY_OP_SGN: |
| 1856 | GGML_CANN_CALL_OP_UNARY(Sign); |
| 1857 | break; |
| 1858 | case GGML_UNARY_OP_STEP: |
| 1859 | ggml_cann_step(ctx, dst); |
| 1860 | break; |
| 1861 | case GGML_UNARY_OP_SOFTPLUS: |
| 1862 | ggml_cann_softplus(ctx, dst); |
| 1863 | break; |
| 1864 | default: |
| 1865 | return false; |
| 1866 | } |
| 1867 | break; |
| 1868 | case GGML_OP_GLU: |
| 1869 | switch (ggml_get_glu_op(dst)) { |
| 1870 | case GGML_GLU_OP_REGLU: |
| 1871 | GGML_CANN_CALL_OP_UNARY_GATED(Relu); |
| 1872 | break; |
| 1873 | case GGML_GLU_OP_GEGLU: |
| 1874 | ggml_cann_geglu(ctx, dst, 0); // approximate=0 → tanh |
| 1875 | break; |
| 1876 | case GGML_GLU_OP_GEGLU_ERF: |
| 1877 | ggml_cann_geglu(ctx, dst, 1); // approximate=1 → erf |
| 1878 | break; |
| 1879 | case GGML_GLU_OP_SWIGLU: |
| 1880 | ggml_cann_swiglu(ctx, dst); |
| 1881 | break; |
| 1882 | case GGML_GLU_OP_SWIGLU_CLAMP: |
| 1883 | ggml_cann_swiglu_clamp(ctx, dst); |
| 1884 | break; |
| 1885 | case GGML_GLU_OP_GEGLU_QUICK: |
| 1886 | ggml_cann_geglu_quick(ctx, dst); |
| 1887 | break; |
| 1888 | default: |
| 1889 | return false; |
| 1890 | } |
| 1891 | break; |
| 1892 | case GGML_OP_NORM: |
| 1893 | ggml_cann_norm(ctx, dst); |
| 1894 | break; |
| 1895 | case GGML_OP_GROUP_NORM: |
| 1896 | ggml_cann_group_norm(ctx, dst); |
| 1897 | break; |
| 1898 | case GGML_OP_L2_NORM: |
| 1899 | ggml_cann_l2_norm(ctx, dst); |
| 1900 | break; |
| 1901 | case GGML_OP_CROSS_ENTROPY_LOSS: |
| 1902 | ggml_cann_cross_entropy_loss(ctx, dst); |
| 1903 | break; |
| 1904 | case GGML_OP_CONCAT: |
| 1905 | ggml_cann_concat(ctx, dst); |
| 1906 | break; |
| 1907 | case GGML_OP_UPSCALE: |
| 1908 | ggml_cann_upsample_nearest2d(ctx, dst); |
| 1909 | break; |
| 1910 | case GGML_OP_PAD: |
| 1911 | ggml_cann_pad(ctx, dst); |
| 1912 | break; |
| 1913 | case GGML_OP_ARANGE: |
| 1914 | ggml_cann_arange(ctx, dst); |
| 1915 | break; |
| 1916 | case GGML_OP_TIMESTEP_EMBEDDING: |
| 1917 | ggml_cann_timestep_embedding(ctx, dst); |
| 1918 | break; |
| 1919 | case GGML_OP_LEAKY_RELU: |
| 1920 | ggml_cann_leaky_relu(ctx, dst); |
| 1921 | break; |
| 1922 | case GGML_OP_RMS_NORM: |
| 1923 | ggml_cann_rms_norm(ctx, dst); |
| 1924 | break; |
| 1925 | case GGML_OP_MUL_MAT: |
| 1926 | ggml_cann_mul_mat(ctx, dst); |
| 1927 | break; |
| 1928 | case GGML_OP_MUL_MAT_ID: |
| 1929 | ggml_cann_mul_mat_id(ctx, dst); |
| 1930 | break; |
| 1931 | case GGML_OP_SCALE: |
| 1932 | ggml_cann_scale(ctx, dst); |
| 1933 | break; |
| 1934 | case GGML_OP_SQR: |
| 1935 | GGML_ASSERT(dst->src[1] == nullptr); |
| 1936 | dst->src[1] = dst->src[0]; |
| 1937 | ggml_cann_binary_op<aclnn_mul>(ctx, dst); |
| 1938 | break; |
| 1939 | case GGML_OP_SQRT: |
| 1940 | GGML_CANN_CALL_OP_UNARY(Sqrt); |
| 1941 | break; |
| 1942 | case GGML_OP_CLAMP: |
| 1943 | ggml_cann_clamp(ctx, dst); |
| 1944 | break; |
| 1945 | case GGML_OP_CPY: |
| 1946 | ggml_cann_cpy(ctx, dst); |
| 1947 | break; |
| 1948 | case GGML_OP_SET: |
| 1949 | ggml_cann_set(ctx, dst); |
| 1950 | break; |
| 1951 | case GGML_OP_CONT: |
| 1952 | ggml_cann_dup(ctx, dst); |
| 1953 | break; |
| 1954 | case GGML_OP_NONE: |
| 1955 | case GGML_OP_RESHAPE: |
| 1956 | case GGML_OP_VIEW: |
| 1957 | case GGML_OP_PERMUTE: |
| 1958 | case GGML_OP_TRANSPOSE: |
| 1959 | break; |
| 1960 | case GGML_OP_DIAG_MASK_INF: |
| 1961 | ggml_cann_diag_mask(ctx, dst, -INFINITY); |
| 1962 | break; |
| 1963 | case GGML_OP_SOFT_MAX: |
| 1964 | ggml_cann_softmax(ctx, dst); |
| 1965 | break; |
| 1966 | case GGML_OP_ROPE: |
| 1967 | ggml_cann_rope(ctx, dst); |
| 1968 | break; |
| 1969 | case GGML_OP_IM2COL: |
| 1970 | ggml_cann_im2col(ctx, dst); |
| 1971 | break; |
| 1972 | case GGML_OP_POOL_2D: |
| 1973 | ggml_cann_pool2d(ctx, dst); |
| 1974 | break; |
| 1975 | case GGML_OP_SUM: |
| 1976 | ggml_cann_sum(ctx, dst); |
| 1977 | break; |
| 1978 | case GGML_OP_SUM_ROWS: |
| 1979 | ggml_cann_sum_rows(ctx, dst); |
| 1980 | break; |
| 1981 | case GGML_OP_ARGSORT: |
| 1982 | ggml_cann_argsort(ctx, dst); |
| 1983 | break; |
| 1984 | case GGML_OP_ARGMAX: |
| 1985 | ggml_cann_argmax(ctx, dst); |
| 1986 | break; |
| 1987 | case GGML_OP_COS: |
| 1988 | ggml_cann_op_unary<aclnn_cos>(ctx, dst); |
| 1989 | break; |
| 1990 | case GGML_OP_SIN: |
| 1991 | ggml_cann_op_unary<aclnn_sin>(ctx, dst); |
| 1992 | break; |
| 1993 | case GGML_OP_CONV_TRANSPOSE_1D: |
| 1994 | ggml_cann_conv_transpose_1d(ctx, dst); |
| 1995 | break; |
| 1996 | case GGML_OP_LOG: |
| 1997 | GGML_CANN_CALL_OP_UNARY(Log); |
| 1998 | break; |
| 1999 | case GGML_OP_MEAN: |
| 2000 | ggml_cann_mean(ctx, dst); |
| 2001 | break; |
| 2002 | case GGML_OP_PAD_REFLECT_1D: |
| 2003 | ggml_cann_pad_reflect_1d(ctx, dst); |
| 2004 | break; |
| 2005 | case GGML_OP_COUNT_EQUAL: |
| 2006 | ggml_cann_count_equal(ctx, dst); |
| 2007 | break; |
| 2008 | case GGML_OP_FLASH_ATTN_EXT: |
| 2009 | ggml_cann_flash_attn_ext(ctx, dst); |
| 2010 | break; |
| 2011 | case GGML_OP_OUT_PROD: |
| 2012 | ggml_cann_out_prod(ctx, dst); |
| 2013 | break; |
| 2014 | case GGML_OP_GATED_LINEAR_ATTN: |
| 2015 | ggml_cann_gated_linear_attn(ctx, dst); |
| 2016 | break; |
| 2017 | case GGML_OP_SSM_CONV: |
| 2018 | ggml_cann_ssm_conv(ctx, dst); |
| 2019 | break; |
| 2020 | case GGML_OP_CUMSUM: |
| 2021 | ggml_cann_cumsum(ctx, dst); |
| 2022 | break; |
| 2023 | case GGML_OP_TRI: |
| 2024 | ggml_cann_tri(ctx, dst); |
| 2025 | break; |
| 2026 | case GGML_OP_FILL: |
| 2027 | ggml_cann_fill(ctx, dst); |
| 2028 | break; |
| 2029 | case GGML_OP_DIAG: |
| 2030 | ggml_cann_diag(ctx, dst); |
| 2031 | break; |
| 2032 | case GGML_OP_SOLVE_TRI: |
| 2033 | ggml_cann_solve_tri(ctx, dst); |
| 2034 | break; |
| 2035 | default: |
| 2036 | return false; |
| 2037 | } |
| 2038 | |
| 2039 | return true; |
| 2040 | } |
| 2041 | |
| 2042 | // backend |
| 2043 | /** |
| 2044 | * @brief Retrieves the name associated with the CANN backend. |
| 2045 | * |
| 2046 | * This function returns the name assigned to the CANN backend, which is stored |
| 2047 | * in the context of the provided backend structure. |
| 2048 | * |
| 2049 | * @param backend Pointer to the CANN backend structure. |
| 2050 | * @return A pointer to a constant string representing the backend name. |
| 2051 | */ |
| 2052 | static const char * ggml_backend_cann_name(ggml_backend_t backend) { |
| 2053 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2054 | |
| 2055 | return cann_ctx->name.c_str(); |
| 2056 | } |
| 2057 | |
| 2058 | /** |
| 2059 | * @brief Frees resources associated with the CANN backend. |
| 2060 | * |
| 2061 | * This function releases resources associated with the CANN backend context |
| 2062 | * and resets the device associated with the backend to its initial state. |
| 2063 | * |
| 2064 | * @param backend Pointer to the CANN backend structure to be freed. |
| 2065 | */ |
| 2066 | static void ggml_backend_cann_free(ggml_backend_t backend) { |
| 2067 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2068 | ACL_CHECK(aclrtSynchronizeDevice()); |
| 2069 | ACL_CHECK(aclrtResetDevice(cann_ctx->device)); |
| 2070 | |
| 2071 | delete cann_ctx; |
| 2072 | delete backend; |
| 2073 | } |
| 2074 | |
| 2075 | /** |
| 2076 | * @brief Sets tensor data asynchronously in the CANN backend. |
| 2077 | * |
| 2078 | * This function asynchronously sets tensor data in the CANN backend. |
| 2079 | * |
| 2080 | * @param backend Pointer to the CANN backend structure. |
| 2081 | * @param tensor Pointer to the tensor structure to set data for. |
| 2082 | * @param data Pointer to the host data to copy to the tensor. |
| 2083 | * @param offset Offset in bytes within the host data. |
| 2084 | * @param size Size of the data to copy in bytes. |
| 2085 | */ |
| 2086 | static void ggml_backend_cann_set_tensor_async(ggml_backend_t backend, |
| 2087 | ggml_tensor * tensor, |
| 2088 | const void * data, |
| 2089 | size_t offset, |
| 2090 | size_t size) { |
| 2091 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2092 | ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer; |
| 2093 | |
| 2094 | GGML_ASSERT(buf->buft == ggml_backend_cann_buffer_type(cann_ctx->device) && "unsupported buffer type"); |
| 2095 | GGML_ASSERT(!ggml_is_quantized(tensor->type)); |
| 2096 | |
| 2097 | ACL_CHECK(aclrtMemcpyAsync((char *) tensor->data + offset, size, data, size, ACL_MEMCPY_HOST_TO_DEVICE, |
| 2098 | cann_ctx->stream())); |
| 2099 | } |
| 2100 | |
| 2101 | /** |
| 2102 | * @brief Gets tensor data asynchronously in the CANN backend. |
| 2103 | * |
| 2104 | * This function asynchronously gets tensor data in the CANN backend. |
| 2105 | * |
| 2106 | * @param backend Pointer to the CANN backend structure. |
| 2107 | * @param tensor Pointer to the tensor structure to get data from. |
| 2108 | * @param data Pointer to the host data to copy from the tensor. |
| 2109 | * @param offset Offset in bytes within the host data. |
| 2110 | * @param size Size of the data to copy in bytes. |
| 2111 | */ |
| 2112 | static void ggml_backend_cann_get_tensor_async(ggml_backend_t backend, |
| 2113 | const ggml_tensor * tensor, |
| 2114 | void * data, |
| 2115 | size_t offset, |
| 2116 | size_t size) { |
| 2117 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2118 | ggml_backend_buffer_t buf = tensor->view_src ? tensor->view_src->buffer : tensor->buffer; |
| 2119 | |
| 2120 | GGML_ASSERT(buf->buft == ggml_backend_cann_buffer_type(cann_ctx->device) && "unsupported buffer type"); |
| 2121 | GGML_ASSERT(!ggml_is_quantized(tensor->type)); |
| 2122 | |
| 2123 | ACL_CHECK(aclrtMemcpyAsync(data, size, (char *) tensor->data + offset, size, ACL_MEMCPY_DEVICE_TO_HOST, |
| 2124 | cann_ctx->stream())); |
| 2125 | } |
| 2126 | |
| 2127 | /** |
| 2128 | * @brief Asynchronously copies tensor data between CANN backends. |
| 2129 | * |
| 2130 | * This function copies tensor data asynchronously between two CANN backends. It |
| 2131 | * checks if both tensors reside in CANN buffers and whether the devices support |
| 2132 | * peer-to-peer access for direct copying. If not, it returns false. |
| 2133 | * |
| 2134 | * @param backend_src Pointer to the source CANN backend structure. |
| 2135 | * @param backend_dst Pointer to the destination CANN backend structure. |
| 2136 | * @param src Pointer to the source tensor to copy data from. |
| 2137 | * @param dst Pointer to the destination tensor to copy data to. |
| 2138 | * @return true if the copy operation succeeds, false otherwise. |
| 2139 | */ |
| 2140 | static bool ggml_backend_cann_cpy_tensor_async(ggml_backend_t backend_src, |
| 2141 | ggml_backend_t backend_dst, |
| 2142 | const ggml_tensor * src, |
| 2143 | ggml_tensor * dst) { |
| 2144 | GGML_ASSERT(ggml_backend_is_cann(backend_src) || ggml_backend_is_cann(backend_dst)); |
| 2145 | |
| 2146 | GGML_ASSERT(!is_matmul_weight((const ggml_tensor *) src)); |
| 2147 | |
| 2148 | if (!ggml_backend_buft_is_cann(src->buffer->buft) || !ggml_backend_buft_is_cann(dst->buffer->buft)) { |
| 2149 | return false; |
| 2150 | } |
| 2151 | |
| 2152 | ggml_backend_buffer_t buf_src = src->view_src ? src->view_src->buffer : src->buffer; |
| 2153 | ggml_backend_buffer_t buf_dst = dst->view_src ? dst->view_src->buffer : dst->buffer; |
| 2154 | |
| 2155 | ggml_backend_cann_context * cann_ctx_src = (ggml_backend_cann_context *) backend_src->context; |
| 2156 | ggml_backend_cann_context * cann_ctx_dst = (ggml_backend_cann_context *) backend_dst->context; |
| 2157 | |
| 2158 | size_t copy_size = ggml_nbytes(dst); |
| 2159 | if (copy_size == 0) { |
| 2160 | return true; |
| 2161 | } |
| 2162 | if (backend_src != backend_dst) { |
| 2163 | #ifdef ASCEND_310P |
| 2164 | // TODO: Support 310p P2P copy |
| 2165 | return false; |
| 2166 | #endif |
| 2167 | ggml_backend_cann_buffer_context * buf_ctx_src = (ggml_backend_cann_buffer_context *) buf_src->context; |
| 2168 | ggml_backend_cann_buffer_context * buf_ctx_dst = (ggml_backend_cann_buffer_context *) buf_dst->context; |
| 2169 | |
| 2170 | GGML_ASSERT(cann_ctx_src->device == buf_ctx_src->device); |
| 2171 | GGML_ASSERT(cann_ctx_dst->device == buf_ctx_dst->device); |
| 2172 | |
| 2173 | int32_t canAccessPeer = 0; |
| 2174 | ACL_CHECK(aclrtDeviceCanAccessPeer(&canAccessPeer, cann_ctx_src->device, cann_ctx_dst->device)); |
| 2175 | if (!canAccessPeer) { |
| 2176 | return false; |
| 2177 | } |
| 2178 | |
| 2179 | // need open both directions for memcpyasync between devices. |
| 2180 | ACL_CHECK(aclrtDeviceEnablePeerAccess(cann_ctx_src->device, 0)); |
| 2181 | ggml_cann_set_device(cann_ctx_src->device); |
| 2182 | ACL_CHECK(aclrtDeviceEnablePeerAccess(cann_ctx_dst->device, 0)); |
| 2183 | |
| 2184 | // wait for task_queue empty to keep task order. |
| 2185 | ACL_CHECK(aclrtMemcpyAsync(dst->data, copy_size, src->data, copy_size, ACL_MEMCPY_DEVICE_TO_DEVICE, |
| 2186 | cann_ctx_src->stream())); |
| 2187 | // record event on src stream after the copy |
| 2188 | // TODO: this event is not effective with acl graph mode, change to use aclrtSynchronizeStream |
| 2189 | // if (!cann_ctx_src->copy_event) { |
| 2190 | // ACL_CHECK(aclrtCreateEventWithFlag(&cann_ctx_src->copy_event, ACL_EVENT_SYNC)); |
| 2191 | // } |
| 2192 | // ACL_CHECK(aclrtRecordEvent(cann_ctx_src->copy_event, cann_ctx_src->stream())); |
| 2193 | |
| 2194 | // // wait on dst stream for the copy to complete |
| 2195 | // ggml_cann_set_device(cann_ctx_dst->device); |
| 2196 | // ACL_CHECK(aclrtStreamWaitEvent(cann_ctx_dst->stream(), cann_ctx_src->copy_event)); |
| 2197 | ACL_CHECK(aclrtSynchronizeStream(cann_ctx_src->stream())); |
| 2198 | } else { |
| 2199 | // src and dst are on the same backend |
| 2200 | ACL_CHECK(aclrtMemcpyAsync(dst->data, copy_size, src->data, copy_size, ACL_MEMCPY_DEVICE_TO_DEVICE, |
| 2201 | cann_ctx_dst->stream())); |
| 2202 | } |
| 2203 | |
| 2204 | return true; |
| 2205 | } |
| 2206 | |
| 2207 | /** |
| 2208 | * @brief Synchronizes a CANN backend. |
| 2209 | * |
| 2210 | * This function synchronizes the specified CANN backend by waiting for all |
| 2211 | * operations in its associated stream to complete. |
| 2212 | * |
| 2213 | * @param backend Pointer to the CANN backend structure to synchronize. |
| 2214 | */ |
| 2215 | static void ggml_backend_cann_synchronize(ggml_backend_t backend) { |
| 2216 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2217 | ggml_cann_set_device(cann_ctx->device); |
| 2218 | ACL_CHECK(aclrtSynchronizeStream(cann_ctx->stream())); |
| 2219 | } |
| 2220 | |
| 2221 | /** |
| 2222 | * @brief Check if CANN backend can fuse the specified operation sequence |
| 2223 | * |
| 2224 | * This function determines whether an operation sequence starting from the specified node |
| 2225 | * can be fused into an optimized operation in the CANN backend. Operation fusion can reduce |
| 2226 | * memory access overhead and improve computational efficiency. |
| 2227 | * |
| 2228 | * @param cgraph Pointer to the computation graph |
| 2229 | * @param node_idx Index of the starting node in the computation graph |
| 2230 | * @param ops Sequence of operation types to check for fusion |
| 2231 | * @return true if the operations can be fused |
| 2232 | * @return false if the operations cannot be fused |
| 2233 | */ |
| 2234 | static bool ggml_cann_can_fuse(const struct ggml_cgraph * cgraph, |
| 2235 | int node_idx, |
| 2236 | std::initializer_list<enum ggml_op> ops) { |
| 2237 | if (!ggml_can_fuse(cgraph, node_idx, ops)) { |
| 2238 | return false; |
| 2239 | } |
| 2240 | |
| 2241 | // CANN backend supports fusing ADD + RMS_NORM operations |
| 2242 | if ((ops.size() == 2) && ops.begin()[0] == GGML_OP_ADD && ops.begin()[1] == GGML_OP_RMS_NORM) { |
| 2243 | ggml_tensor * add_node = cgraph->nodes[node_idx]; |
| 2244 | // TODO: support broadcast for ADD + RMS_NORM |
| 2245 | if (add_node->src[0]->ne[0] != add_node->src[1]->ne[0] || add_node->src[0]->ne[1] != add_node->src[1]->ne[1] || |
| 2246 | add_node->src[0]->ne[2] != add_node->src[1]->ne[2] || add_node->src[0]->ne[3] != add_node->src[1]->ne[3]) { |
| 2247 | return false; |
| 2248 | } |
| 2249 | return true; |
| 2250 | } |
| 2251 | |
| 2252 | return false; |
| 2253 | } |
| 2254 | |
| 2255 | /** |
| 2256 | * @brief Evaluate the computation graph and optionally capture or execute it using CANN graph API. |
| 2257 | * |
| 2258 | * If CANN graph execution is enabled and graph capture is required, this function begins |
| 2259 | * graph capture, runs the graph, ends capture, and stores the captured graph. |
| 2260 | * |
| 2261 | * Otherwise, it falls back to op-by-op execution using the CANN compute kernel dispatcher. |
| 2262 | * |
| 2263 | * @param cann_ctx The CANN backend context. |
| 2264 | * @param cgraph The ggml computation graph. |
| 2265 | * @param use_cann_graph Whether to use CANN graph execution. |
| 2266 | * @param cann_graph_capture_required Whether graph capture is needed due to graph changes. |
| 2267 | */ |
| 2268 | static void evaluate_and_capture_cann_graph(ggml_backend_cann_context * cann_ctx, |
| 2269 | ggml_cgraph * cgraph, |
| 2270 | bool use_cann_graph, |
| 2271 | bool cann_graph_capture_required) { |
| 2272 | #ifdef USE_ACL_GRAPH |
| 2273 | if (use_cann_graph && cann_graph_capture_required) { // Begin CANN graph capture |
| 2274 | ACL_CHECK(aclmdlRICaptureBegin(cann_ctx->stream(), ACL_MODEL_RI_CAPTURE_MODE_GLOBAL)); |
| 2275 | } |
| 2276 | #endif // USE_ACL_GRAPH |
| 2277 | // Only perform the graph execution if CANN graphs are not enabled, or we are capturing the graph. |
| 2278 | // With the use of CANN graphs, the execution will be performed by the graph launch. |
| 2279 | static bool opt_fusion = parse_bool(get_env_as_lowercase("GGML_CANN_OPERATOR_FUSION").value_or("")); |
| 2280 | |
| 2281 | if (!use_cann_graph || cann_graph_capture_required) { |
| 2282 | for (int i = 0; i < cgraph->n_nodes; i++) { |
| 2283 | ggml_tensor * node = cgraph->nodes[i]; |
| 2284 | if (opt_fusion) { |
| 2285 | if (ggml_cann_can_fuse(cgraph, i, { GGML_OP_ADD, GGML_OP_RMS_NORM })) { |
| 2286 | ggml_cann_op_add_rms_norm_fused(*cann_ctx, node, cgraph->nodes[i + 1]); |
| 2287 | i++; |
| 2288 | continue; |
| 2289 | } |
| 2290 | } |
| 2291 | |
| 2292 | if (ggml_is_empty(node) || node->op == GGML_OP_RESHAPE || node->op == GGML_OP_TRANSPOSE || |
| 2293 | node->op == GGML_OP_VIEW || node->op == GGML_OP_PERMUTE || node->op == GGML_OP_NONE) { |
| 2294 | continue; |
| 2295 | } |
| 2296 | |
| 2297 | if ((node->flags & GGML_TENSOR_FLAG_COMPUTE) == 0) { |
| 2298 | continue; |
| 2299 | } |
| 2300 | |
| 2301 | bool ok = ggml_cann_compute_forward(*cann_ctx, node); |
| 2302 | if (!ok) { |
| 2303 | GGML_LOG_ERROR("%s: op not supported %s (%s)\n", __func__, node->name, ggml_op_name(node->op)); |
| 2304 | } |
| 2305 | GGML_ASSERT(ok); |
| 2306 | } |
| 2307 | } |
| 2308 | |
| 2309 | #ifdef USE_ACL_GRAPH |
| 2310 | if (use_cann_graph) { |
| 2311 | GGML_ASSERT(!cann_ctx->graph_lru_cache.cache_list.empty()); |
| 2312 | ggml_cann_graph * matched_graph = cann_ctx->graph_lru_cache.cache_list.front(); |
| 2313 | |
| 2314 | if (cann_graph_capture_required) { // End CANN graph capture |
| 2315 | ACL_CHECK(aclmdlRICaptureEnd(cann_ctx->stream(), &matched_graph->graph)); |
| 2316 | } |
| 2317 | |
| 2318 | // Execute CANN graph |
| 2319 | ACL_CHECK(aclmdlRIExecuteAsync(matched_graph->graph, cann_ctx->stream())); |
| 2320 | } |
| 2321 | #endif // USE_ACL_GRAPH |
| 2322 | } |
| 2323 | |
| 2324 | /** |
| 2325 | * @brief Computes a computational graph using a CANN backend. |
| 2326 | * |
| 2327 | * This function computes the operations defined in the computational graph |
| 2328 | * using the specified CANN backend. |
| 2329 | * |
| 2330 | * @param backend Pointer to the CANN backend structure to use for computation. |
| 2331 | * @param cgraph Pointer to the computational graph structure containing nodes |
| 2332 | * representing operations to be computed. |
| 2333 | * @return enum ggml_status Returns GGML_STATUS_SUCCESS if computation |
| 2334 | * completes successfully, otherwise an appropriate error status. |
| 2335 | */ |
| 2336 | static enum ggml_status ggml_backend_cann_graph_compute(ggml_backend_t backend, ggml_cgraph * cgraph) { |
| 2337 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2338 | ggml_cann_set_device(cann_ctx->device); |
| 2339 | g_nz_workspaces[cann_ctx->device].clear(); |
| 2340 | |
| 2341 | // calculate rope cache for fist layer in current device. |
| 2342 | cann_ctx->rope_cache.cached = false; |
| 2343 | |
| 2344 | bool graph_capture_required = false; |
| 2345 | #ifdef USE_ACL_GRAPH |
| 2346 | bool use_cann_graph = true; |
| 2347 | |
| 2348 | static bool prefill_use_graph = parse_bool(get_env_as_lowercase("GGML_CANN_PREFILL_USE_GRAPH").value_or("")); |
| 2349 | if (!prefill_use_graph) { |
| 2350 | // Do not use acl_graph for prefill. |
| 2351 | for (int i = 0; i < cgraph->n_nodes; i++) { |
| 2352 | ggml_tensor * node = cgraph->nodes[i]; |
| 2353 | // TODO: Optimize here. Currently, we can only |
| 2354 | // get seq_len by FA's input. |
| 2355 | if (node->op == GGML_OP_FLASH_ATTN_EXT) { |
| 2356 | // Q -> src[0], shape: [B, S, N, D] |
| 2357 | use_cann_graph = (node->src[0]->ne[1] == 1); |
| 2358 | break; |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | if (!cann_ctx->acl_graph_mode) { |
| 2364 | use_cann_graph = false; |
| 2365 | } |
| 2366 | |
| 2367 | if (use_cann_graph) { |
| 2368 | // If no matching graph is found, the graph needs to be recaptured. |
| 2369 | graph_capture_required = !cann_ctx->graph_lru_cache.find_and_move_to_front(cgraph); |
| 2370 | |
| 2371 | if (graph_capture_required) { |
| 2372 | // If no matching graph is found, add a new ACL graph. |
| 2373 | ggml_cann_graph * new_graph = ggml_cann_graph::create_from_cgraph(cgraph); |
| 2374 | cann_ctx->graph_lru_cache.push(new_graph); |
| 2375 | |
| 2376 | // Pre-load rope cache before graph capture. During capture the |
| 2377 | // stream cannot perform host-to-device memcpy or device memory |
| 2378 | // malloc/free. Running the full cache init now populates the |
| 2379 | // cache metadata so these branches are skipped during capture, |
| 2380 | // while also warming up the memory pool. |
| 2381 | for (int i = 0; i < cgraph->n_nodes; i++) { |
| 2382 | ggml_tensor * node = cgraph->nodes[i]; |
| 2383 | if (node->op == GGML_OP_ROPE) { |
| 2384 | ggml_cann_rope_cache_preload(*cann_ctx, node); |
| 2385 | break; |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | } |
| 2390 | #else |
| 2391 | bool use_cann_graph = false; |
| 2392 | #endif // USE_ACL_GRAPH |
| 2393 | evaluate_and_capture_cann_graph(cann_ctx, cgraph, use_cann_graph, graph_capture_required); |
| 2394 | |
| 2395 | return GGML_STATUS_SUCCESS; |
| 2396 | } |
| 2397 | |
| 2398 | /** |
| 2399 | * @brief Checks if the CANN backend supports a specific operation. |
| 2400 | * |
| 2401 | * This function checks whether the specified operation is supported by the |
| 2402 | * CANN backend. |
| 2403 | * |
| 2404 | * @param backend Pointer to the CANN backend structure to check support for |
| 2405 | * the operation. |
| 2406 | * @param op Pointer to the tensor representing the operation to check. |
| 2407 | * @return bool Returns true if the operation is supported by the backend, |
| 2408 | * otherwise false. |
| 2409 | */ |
| 2410 | static bool ggml_backend_cann_supports_op(ggml_backend_dev_t dev, const ggml_tensor * op) { |
| 2411 | switch (op->op) { |
| 2412 | case GGML_OP_UNARY: |
| 2413 | switch (ggml_get_unary_op(op)) { |
| 2414 | case GGML_UNARY_OP_ABS: |
| 2415 | case GGML_UNARY_OP_NEG: |
| 2416 | case GGML_UNARY_OP_GELU: |
| 2417 | case GGML_UNARY_OP_SILU: |
| 2418 | case GGML_UNARY_OP_RELU: |
| 2419 | case GGML_UNARY_OP_SIGMOID: |
| 2420 | case GGML_UNARY_OP_HARDSIGMOID: |
| 2421 | case GGML_UNARY_OP_HARDSWISH: |
| 2422 | case GGML_UNARY_OP_GELU_QUICK: |
| 2423 | case GGML_UNARY_OP_TANH: |
| 2424 | case GGML_UNARY_OP_EXP: |
| 2425 | case GGML_UNARY_OP_ELU: |
| 2426 | case GGML_UNARY_OP_SGN: |
| 2427 | case GGML_UNARY_OP_STEP: |
| 2428 | case GGML_UNARY_OP_GELU_ERF: |
| 2429 | case GGML_UNARY_OP_SOFTPLUS: |
| 2430 | return true; |
| 2431 | default: |
| 2432 | return false; |
| 2433 | } |
| 2434 | case GGML_OP_GLU: |
| 2435 | switch (ggml_get_glu_op(op)) { |
| 2436 | case GGML_GLU_OP_REGLU: |
| 2437 | case GGML_GLU_OP_GEGLU: |
| 2438 | case GGML_GLU_OP_SWIGLU: |
| 2439 | case GGML_GLU_OP_GEGLU_ERF: |
| 2440 | case GGML_GLU_OP_GEGLU_QUICK: |
| 2441 | case GGML_GLU_OP_SWIGLU_CLAMP: |
| 2442 | return true; |
| 2443 | default: |
| 2444 | return false; |
| 2445 | } |
| 2446 | break; |
| 2447 | case GGML_OP_MUL_MAT: |
| 2448 | { |
| 2449 | switch (op->src[0]->type) { |
| 2450 | #ifndef ASCEND_310P |
| 2451 | case GGML_TYPE_BF16: |
| 2452 | #endif |
| 2453 | case GGML_TYPE_F16: |
| 2454 | case GGML_TYPE_F32: |
| 2455 | return true; |
| 2456 | case GGML_TYPE_Q8_0: |
| 2457 | case GGML_TYPE_Q4_0: |
| 2458 | #ifdef ASCEND_310P |
| 2459 | // Q4 && Q8 per group is not support on 310p device |
| 2460 | return false; |
| 2461 | #else |
| 2462 | // only support contiguous for quantized types. |
| 2463 | return ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]); |
| 2464 | #endif |
| 2465 | default: |
| 2466 | return false; |
| 2467 | } |
| 2468 | } |
| 2469 | case GGML_OP_MUL_MAT_ID: |
| 2470 | switch (op->src[0]->type) { |
| 2471 | case GGML_TYPE_F16: |
| 2472 | case GGML_TYPE_F32: |
| 2473 | return true; |
| 2474 | case GGML_TYPE_Q8_0: |
| 2475 | case GGML_TYPE_Q4_0: |
| 2476 | #ifdef ASCEND_310P |
| 2477 | // Q4 && Q8 per group is not support on 310p device |
| 2478 | return false; |
| 2479 | #else |
| 2480 | // only support contiguous for quantized types. |
| 2481 | return ggml_is_contiguous(op->src[0]) && ggml_is_contiguous(op->src[1]); |
| 2482 | #endif |
| 2483 | default: |
| 2484 | return false; |
| 2485 | } |
| 2486 | // embedding |
| 2487 | case GGML_OP_GET_ROWS: |
| 2488 | { |
| 2489 | switch (op->src[0]->type) { |
| 2490 | case GGML_TYPE_F32: |
| 2491 | case GGML_TYPE_F16: |
| 2492 | #ifndef ASCEND_310P |
| 2493 | case GGML_TYPE_BF16: |
| 2494 | #endif |
| 2495 | case GGML_TYPE_Q8_0: |
| 2496 | return true; |
| 2497 | default: |
| 2498 | return false; |
| 2499 | } |
| 2500 | } |
| 2501 | break; |
| 2502 | case GGML_OP_SET_ROWS: |
| 2503 | { |
| 2504 | switch (op->type) { |
| 2505 | case GGML_TYPE_F32: |
| 2506 | case GGML_TYPE_F16: |
| 2507 | #ifndef ASCEND_310P |
| 2508 | case GGML_TYPE_BF16: |
| 2509 | #endif |
| 2510 | return true; |
| 2511 | default: |
| 2512 | return false; |
| 2513 | } |
| 2514 | } |
| 2515 | break; |
| 2516 | case GGML_OP_CPY: |
| 2517 | { |
| 2518 | ggml_tensor * src = op->src[0]; |
| 2519 | #ifdef ASCEND_310P |
| 2520 | if ((op->type != GGML_TYPE_F32 && op->type != GGML_TYPE_F16) || |
| 2521 | (src->type != GGML_TYPE_F32 && src->type != GGML_TYPE_F16)) { |
| 2522 | // only support F32 and F16 on 310P. |
| 2523 | return false; |
| 2524 | } |
| 2525 | #else |
| 2526 | if ((op->type != GGML_TYPE_F32 && op->type != GGML_TYPE_F16 && op->type != GGML_TYPE_BF16) || |
| 2527 | (src->type != GGML_TYPE_F32 && src->type != GGML_TYPE_F16 && src->type != GGML_TYPE_BF16)) { |
| 2528 | // only support F32, F16 and BF16. |
| 2529 | return false; |
| 2530 | } |
| 2531 | #endif |
| 2532 | return true; |
| 2533 | } |
| 2534 | break; |
| 2535 | case GGML_OP_CONT: |
| 2536 | { |
| 2537 | switch (op->src[0]->type) { |
| 2538 | case GGML_TYPE_F32: |
| 2539 | case GGML_TYPE_F16: |
| 2540 | #ifndef ASCEND_310P |
| 2541 | case GGML_TYPE_BF16: |
| 2542 | #endif |
| 2543 | return true; |
| 2544 | default: |
| 2545 | return false; |
| 2546 | } |
| 2547 | } |
| 2548 | case GGML_OP_ROPE: |
| 2549 | { |
| 2550 | if (((const int32_t *) op->op_params)[15] != 0) { |
| 2551 | return false; // FIXME: support ggml_rope_set_offset |
| 2552 | } |
| 2553 | if (op->src[0]->ne[0] > 896) { |
| 2554 | return false; |
| 2555 | } |
| 2556 | #ifdef ASCEND_310P |
| 2557 | // TODO: Support rope_dim < ne00(dim) |
| 2558 | if (op->src[0]->ne[0] != op->op_params[1]) { |
| 2559 | return false; |
| 2560 | } |
| 2561 | if (!ggml_is_contiguous(op->src[0])) { |
| 2562 | return false; |
| 2563 | } |
| 2564 | #endif |
| 2565 | return true; |
| 2566 | } |
| 2567 | case GGML_OP_UPSCALE: |
| 2568 | { |
| 2569 | // aclnnUpsampleNearest2dGetWorkspaceSize not support |
| 2570 | // selfDimN[2]/outDimN[2] or selfDimC[3]/outDimC[3] not equal |
| 2571 | if (op->src[0]->ne[2] * op->ne[3] != op->src[0]->ne[3] * op->ne[2]) { |
| 2572 | return false; |
| 2573 | } |
| 2574 | if (op->op_params[0] != GGML_SCALE_MODE_NEAREST) { |
| 2575 | return false; |
| 2576 | } |
| 2577 | if (op->op_params[0] & GGML_SCALE_FLAG_ANTIALIAS) { |
| 2578 | return false; |
| 2579 | } |
| 2580 | return true; |
| 2581 | } |
| 2582 | case GGML_OP_POOL_2D: |
| 2583 | { |
| 2584 | const int32_t * opts = (const int32_t *) op->op_params; |
| 2585 | #ifdef ASCEND_310P |
| 2586 | enum ggml_op_pool opt = static_cast<ggml_op_pool>(opts[0]); |
| 2587 | if (opt == GGML_OP_POOL_MAX) { |
| 2588 | return false; |
| 2589 | } |
| 2590 | #endif |
| 2591 | const int k0 = opts[1]; |
| 2592 | const int k1 = opts[2]; |
| 2593 | const int p0 = opts[5]; |
| 2594 | const int p1 = opts[6]; |
| 2595 | // value of paddingH should be at most half of kernelH |
| 2596 | // value of paddingW should be at most half of kernelW |
| 2597 | return (p0 <= (k0 / 2)) && (p1 <= (k1 / 2)); |
| 2598 | } |
| 2599 | case GGML_OP_SUM: |
| 2600 | return ggml_is_contiguous_rows(op->src[0]); |
| 2601 | case GGML_OP_L2_NORM: |
| 2602 | case GGML_OP_CROSS_ENTROPY_LOSS: |
| 2603 | case GGML_OP_DUP: |
| 2604 | case GGML_OP_IM2COL: |
| 2605 | case GGML_OP_CONCAT: |
| 2606 | case GGML_OP_REPEAT: |
| 2607 | case GGML_OP_NONE: |
| 2608 | case GGML_OP_RESHAPE: |
| 2609 | case GGML_OP_VIEW: |
| 2610 | case GGML_OP_PERMUTE: |
| 2611 | case GGML_OP_TRANSPOSE: |
| 2612 | case GGML_OP_NORM: |
| 2613 | case GGML_OP_ADD: |
| 2614 | case GGML_OP_ADD1: |
| 2615 | case GGML_OP_SUB: |
| 2616 | case GGML_OP_MUL: |
| 2617 | case GGML_OP_DIV: |
| 2618 | case GGML_OP_RMS_NORM: |
| 2619 | case GGML_OP_SQR: |
| 2620 | case GGML_OP_SQRT: |
| 2621 | case GGML_OP_CLAMP: |
| 2622 | case GGML_OP_DIAG_MASK_INF: |
| 2623 | case GGML_OP_SUM_ROWS: |
| 2624 | case GGML_OP_ARGSORT: |
| 2625 | case GGML_OP_ACC: |
| 2626 | case GGML_OP_SET: |
| 2627 | case GGML_OP_GROUP_NORM: |
| 2628 | return true; |
| 2629 | case GGML_OP_PAD: |
| 2630 | // TODO: add circular padding support for cann, see https://github.com/ggml-org/llama.cpp/pull/16985 |
| 2631 | return ggml_get_op_params_i32(op, 8) == 0; |
| 2632 | case GGML_OP_ARANGE: |
| 2633 | case GGML_OP_TIMESTEP_EMBEDDING: |
| 2634 | case GGML_OP_LEAKY_RELU: |
| 2635 | case GGML_OP_ARGMAX: |
| 2636 | case GGML_OP_COS: |
| 2637 | case GGML_OP_SIN: |
| 2638 | case GGML_OP_LOG: |
| 2639 | case GGML_OP_MEAN: |
| 2640 | case GGML_OP_PAD_REFLECT_1D: |
| 2641 | case GGML_OP_COUNT_EQUAL: |
| 2642 | case GGML_OP_GATED_LINEAR_ATTN: |
| 2643 | return true; |
| 2644 | case GGML_OP_OUT_PROD: |
| 2645 | { |
| 2646 | #ifdef ASCEND_310P |
| 2647 | // Ger is not supported on 310p device |
| 2648 | return false; |
| 2649 | #else |
| 2650 | switch (op->src[0]->type) { |
| 2651 | case GGML_TYPE_F16: |
| 2652 | case GGML_TYPE_F32: |
| 2653 | return true; |
| 2654 | default: |
| 2655 | return false; |
| 2656 | } |
| 2657 | #endif |
| 2658 | } |
| 2659 | case GGML_OP_CONV_TRANSPOSE_1D: |
| 2660 | return true; |
| 2661 | case GGML_OP_SCALE: |
| 2662 | float bias; |
| 2663 | memcpy(&bias, (const float *) (op->op_params) + 1, sizeof(float)); |
| 2664 | return bias == 0.0f; // TODO: support bias != 0.0f |
| 2665 | case GGML_OP_SOFT_MAX: |
| 2666 | // TODO: support attention sinks [TAG_ATTN_SINKS] |
| 2667 | if (op->src[2]) { |
| 2668 | return false; |
| 2669 | } |
| 2670 | return true; |
| 2671 | case GGML_OP_FLASH_ATTN_EXT: |
| 2672 | { |
| 2673 | // K/V row lists (ggml_flash_attn_ext_rows) are not supported |
| 2674 | if (op->src[5] != nullptr) { |
| 2675 | return false; |
| 2676 | } |
| 2677 | #ifdef ASCEND_310P |
| 2678 | // FA not support on 310p device |
| 2679 | return false; |
| 2680 | #else |
| 2681 | // derived from [ggml-cuda.cu] |
| 2682 | if (op->src[1]->type != GGML_TYPE_F16 || op->src[2]->type != GGML_TYPE_F16) { |
| 2683 | return false; |
| 2684 | } |
| 2685 | if (op->src[1]->type != GGML_TYPE_F16 && op->src[1]->type != GGML_TYPE_F32 && |
| 2686 | op->src[1]->type != GGML_TYPE_BF16) { |
| 2687 | return false; |
| 2688 | } |
| 2689 | if (op->type != GGML_TYPE_F16 && op->type != GGML_TYPE_F32 && op->type != GGML_TYPE_BF16) { |
| 2690 | return false; |
| 2691 | } |
| 2692 | // TODO: support attention sinks [TAG_ATTN_SINKS] |
| 2693 | if (op->src[4]) { |
| 2694 | return false; |
| 2695 | } |
| 2696 | if (op->src[1]->ne[0] != op->src[2]->ne[0]) { |
| 2697 | // different head sizes of K and V are not supported yet |
| 2698 | return false; |
| 2699 | } |
| 2700 | float logitSoftcap = 0.0f; |
| 2701 | memcpy(&logitSoftcap, (const float *) (op->op_params) + 2, sizeof(float)); |
| 2702 | if (logitSoftcap != 0.0f) { |
| 2703 | return false; |
| 2704 | } |
| 2705 | return true; |
| 2706 | #endif |
| 2707 | } |
| 2708 | case GGML_OP_SSM_CONV: |
| 2709 | return true; |
| 2710 | case GGML_OP_CUMSUM: |
| 2711 | return op->src[0]->type == GGML_TYPE_F32; |
| 2712 | case GGML_OP_TRI: |
| 2713 | return op->src[0]->type == GGML_TYPE_F32; |
| 2714 | case GGML_OP_FILL: |
| 2715 | return op->src[0]->type == GGML_TYPE_F32; |
| 2716 | case GGML_OP_DIAG: |
| 2717 | return op->src[0]->type == GGML_TYPE_F32; |
| 2718 | case GGML_OP_SOLVE_TRI: |
| 2719 | return op->src[0]->type == GGML_TYPE_F32; |
| 2720 | default: |
| 2721 | return false; |
| 2722 | } |
| 2723 | |
| 2724 | GGML_UNUSED(dev); |
| 2725 | } |
| 2726 | |
| 2727 | /** |
| 2728 | * @brief Records an event on the CANN backend stream. |
| 2729 | * |
| 2730 | * This function records the given event on the ACL runtime stream associated |
| 2731 | * with the backend context. |
| 2732 | * |
| 2733 | * @param event Pointer to the event structure to be recorded. |
| 2734 | */ |
| 2735 | static void ggml_backend_cann_event_record(ggml_backend_t backend, ggml_backend_event_t event) { |
| 2736 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2737 | ACL_CHECK(aclrtRecordEvent((aclrtEvent) event->context, cann_ctx->stream())); |
| 2738 | } |
| 2739 | |
| 2740 | /** |
| 2741 | * @brief Waits for a recorded event to complete on the CANN backend stream. |
| 2742 | * |
| 2743 | * This function makes the given backend wait for the event to complete on its |
| 2744 | * ACL runtime stream. |
| 2745 | * |
| 2746 | * @param backend Pointer to the backend structure. |
| 2747 | * @param event Pointer to the event structure that the backend needs to wait |
| 2748 | * for. |
| 2749 | */ |
| 2750 | static void ggml_backend_cann_event_wait(ggml_backend_t backend, ggml_backend_event_t event) { |
| 2751 | ggml_backend_cann_context * cann_ctx = (ggml_backend_cann_context *) backend->context; |
| 2752 | if (ggml_backend_is_cann(backend)) { |
| 2753 | ACL_CHECK(aclrtStreamWaitEvent(cann_ctx->stream(), (aclrtEvent) event->context)); |
| 2754 | } else { |
| 2755 | GGML_ABORT("fatal error"); |
| 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | /** |
| 2760 | * @brief Structure defining the interface for the CANN backend. |
| 2761 | * |
| 2762 | * This structure contains function pointers for various operations |
| 2763 | * supported by the CANN backend, including name retrieval, memory |
| 2764 | * management, tensor operations, synchronization, and event handling. |
| 2765 | */ |
| 2766 | static const ggml_backend_i ggml_backend_cann_interface = { |
| 2767 | /* .get_name = */ ggml_backend_cann_name, |
| 2768 | /* .free = */ ggml_backend_cann_free, |
| 2769 | /* .set_tensor_async = */ ggml_backend_cann_set_tensor_async, |
| 2770 | /* .get_tensor_async = */ ggml_backend_cann_get_tensor_async, |
| 2771 | /* .set_tensor_2d_async = */ NULL, |
| 2772 | /* .get_tensor_2d_async = */ NULL, |
| 2773 | /* .cpy_tensor_async = */ ggml_backend_cann_cpy_tensor_async, |
| 2774 | /* .synchronize = */ ggml_backend_cann_synchronize, |
| 2775 | /* .graph_plan_create = */ NULL, |
| 2776 | /* .graph_plan_free = */ NULL, |
| 2777 | /* .graph_plan_update = */ NULL, |
| 2778 | /* .graph_plan_compute = */ NULL, |
| 2779 | /* .graph_compute = */ ggml_backend_cann_graph_compute, |
| 2780 | /* .event_record = */ ggml_backend_cann_event_record, |
| 2781 | /* .event_wait = */ ggml_backend_cann_event_wait, |
| 2782 | /* .graph_optimize = */ NULL, |
| 2783 | }; |
| 2784 | |
| 2785 | /** |
| 2786 | * @brief Return the hardcoded GUID for the CANN backend. |
| 2787 | * |
| 2788 | * This function returns a static GUID which uniquely identifies the CANN |
| 2789 | * backend. |
| 2790 | * |
| 2791 | * @return A pointer to the static GUID. |
| 2792 | */ |
| 2793 | static ggml_guid_t ggml_backend_cann_guid() { |
| 2794 | static ggml_guid guid = { 0xa1, 0x94, 0xaf, 0xac, 0xbd, 0x4f, 0x47, 0x34, |
| 2795 | 0xbe, 0x1a, 0x9e, 0x71, 0x1f, 0x9e, 0xed, 0x64 }; |
| 2796 | return &guid; |
| 2797 | } |
| 2798 | |
| 2799 | // backend device |
| 2800 | struct ggml_backend_cann_device_context { |
| 2801 | int device; |
| 2802 | std::string name; |
| 2803 | std::string description; |
| 2804 | int op_offload_min_batch_size; |
| 2805 | }; |
| 2806 | |
| 2807 | static const char * ggml_backend_cann_device_get_name(ggml_backend_dev_t dev) { |
| 2808 | ggml_backend_cann_device_context * ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2809 | return ctx->name.c_str(); |
| 2810 | } |
| 2811 | |
| 2812 | static const char * ggml_backend_cann_device_get_description(ggml_backend_dev_t dev) { |
| 2813 | ggml_backend_cann_device_context * ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2814 | return ctx->description.c_str(); |
| 2815 | } |
| 2816 | |
| 2817 | static void ggml_backend_cann_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) { |
| 2818 | ggml_backend_cann_device_context * ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2819 | ggml_backend_cann_get_device_memory(ctx->device, free, total); |
| 2820 | } |
| 2821 | |
| 2822 | static enum ggml_backend_dev_type ggml_backend_cann_device_get_type(ggml_backend_dev_t dev) { |
| 2823 | GGML_UNUSED(dev); |
| 2824 | return GGML_BACKEND_DEVICE_TYPE_GPU; |
| 2825 | } |
| 2826 | |
| 2827 | static void ggml_backend_cann_device_get_props(ggml_backend_dev_t dev, ggml_backend_dev_props * props) { |
| 2828 | props->name = ggml_backend_cann_device_get_name(dev); |
| 2829 | props->description = ggml_backend_cann_device_get_description(dev); |
| 2830 | props->type = ggml_backend_cann_device_get_type(dev); |
| 2831 | ggml_backend_cann_device_get_memory(dev, &props->memory_free, &props->memory_total); |
| 2832 | |
| 2833 | bool host_buffer = getenv("GGML_CANN_NO_PINNED") == nullptr; |
| 2834 | |
| 2835 | props->caps = { |
| 2836 | /* .async = */ false, |
| 2837 | /* .host_buffer = */ host_buffer, |
| 2838 | /* .buffer_from_host_ptr = */ false, |
| 2839 | /* .events = */ true, |
| 2840 | /* .mmap_support = */ true, |
| 2841 | }; |
| 2842 | } |
| 2843 | |
| 2844 | static ggml_backend_t ggml_backend_cann_device_init(ggml_backend_dev_t dev, const char * params) { |
| 2845 | GGML_UNUSED(params); |
| 2846 | ggml_backend_cann_device_context * ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2847 | return ggml_backend_cann_init(ctx->device); |
| 2848 | } |
| 2849 | |
| 2850 | /** |
| 2851 | * @brief Checks if the CANN backend supports a specific backend buffer type. |
| 2852 | * |
| 2853 | * This function determines whether the CANN backend supports the given backend |
| 2854 | * buffer type by comparing the device context of the backend and buffer type. |
| 2855 | * It returns true if the devices are same between the backend context and |
| 2856 | * buffer type context. |
| 2857 | * |
| 2858 | * @param backend Pointer to the CANN backend. |
| 2859 | * @param buft Pointer to the backend buffer type to check. |
| 2860 | * @return bool Returns true if the CANN backend supports the buffer type, |
| 2861 | * otherwise false. |
| 2862 | */ |
| 2863 | static bool ggml_backend_cann_supports_buft(ggml_backend_dev_t dev, ggml_backend_buffer_type_t buft) { |
| 2864 | if (ggml_backend_buft_is_cann(buft)) { |
| 2865 | ggml_backend_cann_device_context * dev_ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2866 | ggml_backend_cann_buffer_type_context * buft_ctx = (ggml_backend_cann_buffer_type_context *) buft->context; |
| 2867 | return buft_ctx->device == dev_ctx->device; |
| 2868 | } |
| 2869 | return false; |
| 2870 | } |
| 2871 | |
| 2872 | static ggml_backend_buffer_type_t ggml_backend_cann_device_get_buffer_type(ggml_backend_dev_t dev) { |
| 2873 | ggml_backend_cann_device_context * ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2874 | return ggml_backend_cann_buffer_type(ctx->device); |
| 2875 | } |
| 2876 | |
| 2877 | static ggml_backend_buffer_type_t ggml_backend_cann_device_get_host_buffer_type(ggml_backend_dev_t dev) { |
| 2878 | GGML_UNUSED(dev); |
| 2879 | return ggml_backend_cann_host_buffer_type(); |
| 2880 | } |
| 2881 | |
| 2882 | /** |
| 2883 | * @brief Determines if a tensor operation should be offloaded to the CANN |
| 2884 | * backend. |
| 2885 | * |
| 2886 | * This function checks if a given tensor operation should be offloaded to the |
| 2887 | * CANN backend based on the operation type and the size of the tensor. It |
| 2888 | * returns true if the second dimension (ne[1]) of the tensor is greater than or |
| 2889 | * equal to the minimum batch size and the operation is not GGML_OP_GET_ROWS. |
| 2890 | * |
| 2891 | * @param backend Pointer to the CANN backend. |
| 2892 | * @param op Pointer to the tensor operation to check. |
| 2893 | * @return bool Returns true if the operation should be offloaded, otherwise |
| 2894 | * false. |
| 2895 | */ |
| 2896 | static bool ggml_backend_cann_offload_op(ggml_backend_dev_t dev, const ggml_tensor * op) { |
| 2897 | ggml_backend_cann_device_context * dev_ctx = (ggml_backend_cann_device_context *)dev->context; |
| 2898 | |
| 2899 | return op->ne[1] >= dev_ctx->op_offload_min_batch_size && op->op != GGML_OP_GET_ROWS; |
| 2900 | } |
| 2901 | |
| 2902 | /** |
| 2903 | * @brief Creates a new event for the CANN backend device. |
| 2904 | * |
| 2905 | * This function initializes a new event for the CANN backend by setting the |
| 2906 | * device and creating an ACL runtime event. The created event is then wrapped |
| 2907 | * in a ggml_backend_event structure and returned. |
| 2908 | * |
| 2909 | * @param backend Pointer to the CANN backend. |
| 2910 | * @return ggml_backend_event_t Returns a pointer to the new event structure. |
| 2911 | */ |
| 2912 | static ggml_backend_event_t ggml_backend_cann_device_event_new(ggml_backend_dev_t dev) { |
| 2913 | ggml_backend_cann_device_context * dev_ctx = (ggml_backend_cann_device_context *) dev->context; |
| 2914 | |
| 2915 | ggml_cann_set_device(dev_ctx->device); |
| 2916 | |
| 2917 | aclrtEvent event; |
| 2918 | ACL_CHECK(aclrtCreateEvent(&event)); |
| 2919 | |
| 2920 | return new ggml_backend_event{ |
| 2921 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cann_reg(), dev_ctx->device), |
| 2922 | /* .context = */ event, |
| 2923 | }; |
| 2924 | } |
| 2925 | |
| 2926 | /** |
| 2927 | * @brief Frees a CANN backend event. |
| 2928 | * |
| 2929 | * This function destroys the ACL runtime event associated with the given CANN |
| 2930 | * backend event and then deletes the event structure itself. |
| 2931 | * |
| 2932 | * @param event Pointer to the event structure to be freed. |
| 2933 | */ |
| 2934 | static void ggml_backend_cann_device_event_free(ggml_backend_dev_t dev, ggml_backend_event_t event) { |
| 2935 | ACL_CHECK(aclrtDestroyEvent((aclrtEvent) event->context)); |
| 2936 | |
| 2937 | delete event; |
| 2938 | GGML_UNUSED(dev); |
| 2939 | } |
| 2940 | |
| 2941 | /** |
| 2942 | * @brief Synchronizes the given event on the CANN backend. |
| 2943 | * |
| 2944 | * This function waits for the specified event to complete on the ACL runtime. |
| 2945 | * |
| 2946 | * @param event Pointer to the event structure to be synchronized. |
| 2947 | */ |
| 2948 | static void ggml_backend_cann_device_event_synchronize(ggml_backend_dev_t dev, ggml_backend_event_t event) { |
| 2949 | ACL_CHECK(aclrtSynchronizeEvent((aclrtEvent) event->context)); |
| 2950 | |
| 2951 | GGML_UNUSED(dev); |
| 2952 | } |
| 2953 | |
| 2954 | static const ggml_backend_device_i ggml_backend_cann_device_interface = { |
| 2955 | /* .get_name = */ ggml_backend_cann_device_get_name, |
| 2956 | /* .get_description = */ ggml_backend_cann_device_get_description, |
| 2957 | /* .get_memory = */ ggml_backend_cann_device_get_memory, |
| 2958 | /* .get_type = */ ggml_backend_cann_device_get_type, |
| 2959 | /* .get_props = */ ggml_backend_cann_device_get_props, |
| 2960 | /* .init_backend = */ ggml_backend_cann_device_init, // called for every card |
| 2961 | /* .get_buffer_type = */ ggml_backend_cann_device_get_buffer_type, |
| 2962 | /* .get_host_buffer_type = */ ggml_backend_cann_device_get_host_buffer_type, |
| 2963 | /* .buffer_from_host_ptr = */ NULL, // not supported for CANN |
| 2964 | /* .supports_op = */ ggml_backend_cann_supports_op, |
| 2965 | /* .supports_buft = */ ggml_backend_cann_supports_buft, |
| 2966 | /* .offload_op = */ ggml_backend_cann_offload_op, |
| 2967 | /* .event_new = */ ggml_backend_cann_device_event_new, |
| 2968 | /* .event_free = */ ggml_backend_cann_device_event_free, |
| 2969 | /* .event_synchronize = */ ggml_backend_cann_device_event_synchronize, |
| 2970 | }; |
| 2971 | |
| 2972 | // backend reg |
| 2973 | struct ggml_backend_cann_reg_context { |
| 2974 | std::vector<ggml_backend_dev_t> devices; |
| 2975 | }; |
| 2976 | |
| 2977 | static const char * ggml_backend_cann_reg_get_name(ggml_backend_reg_t reg) { |
| 2978 | GGML_UNUSED(reg); |
| 2979 | return GGML_CANN_NAME; |
| 2980 | } |
| 2981 | |
| 2982 | static size_t ggml_backend_cann_reg_get_device_count(ggml_backend_reg_t reg) { |
| 2983 | ggml_backend_cann_reg_context * ctx = (ggml_backend_cann_reg_context *) reg->context; |
| 2984 | return ctx->devices.size(); |
| 2985 | } |
| 2986 | |
| 2987 | static ggml_backend_dev_t ggml_backend_cann_reg_get_device(ggml_backend_reg_t reg, size_t index) { |
| 2988 | ggml_backend_cann_reg_context * ctx = (ggml_backend_cann_reg_context *) reg->context; |
| 2989 | GGML_ASSERT(index < ctx->devices.size()); |
| 2990 | return ctx->devices[index]; |
| 2991 | } |
| 2992 | |
| 2993 | static void * ggml_backend_cann_reg_get_proc_address(ggml_backend_reg_t reg, const char * name) { |
| 2994 | GGML_UNUSED(reg); |
| 2995 | GGML_UNUSED(name); |
| 2996 | // reserved for future use |
| 2997 | return nullptr; |
| 2998 | } |
| 2999 | |
| 3000 | static const ggml_backend_reg_i ggml_backend_cann_reg_interface = { |
| 3001 | /* .get_name = */ ggml_backend_cann_reg_get_name, |
| 3002 | /* .get_device_count = */ ggml_backend_cann_reg_get_device_count, |
| 3003 | /* .get_device = */ ggml_backend_cann_reg_get_device, |
| 3004 | /* .get_proc_address = */ ggml_backend_cann_reg_get_proc_address, |
| 3005 | }; |
| 3006 | |
| 3007 | // backend registry, called only once for cann backend |
| 3008 | ggml_backend_reg_t ggml_backend_cann_reg() { |
| 3009 | static ggml_backend_reg reg; |
| 3010 | static bool initialized = false; |
| 3011 | |
| 3012 | { |
| 3013 | static std::mutex mutex; |
| 3014 | std::lock_guard<std::mutex> lock(mutex); |
| 3015 | if (!initialized) { |
| 3016 | aclInit(nullptr); |
| 3017 | ggml_backend_cann_reg_context * ctx = new ggml_backend_cann_reg_context; |
| 3018 | const int min_batch_size = getenv("GGML_OP_OFFLOAD_MIN_BATCH") ? atoi(getenv("GGML_OP_OFFLOAD_MIN_BATCH")) : 32; |
| 3019 | |
| 3020 | for (int i = 0; i < ggml_cann_info().device_count; i++) { |
| 3021 | ggml_backend_cann_device_context * dev_ctx = new ggml_backend_cann_device_context(); |
| 3022 | dev_ctx->description = aclrtGetSocName(); |
| 3023 | dev_ctx->device = i; |
| 3024 | dev_ctx->name = GGML_CANN_NAME + std::to_string(i); |
| 3025 | dev_ctx->op_offload_min_batch_size = min_batch_size; |
| 3026 | ggml_cann_set_device(i); |
| 3027 | ggml_backend_dev_t dev = new ggml_backend_device{ /* .iface = */ ggml_backend_cann_device_interface, |
| 3028 | /* .reg = */ ®, |
| 3029 | /* .context = */ dev_ctx }; |
| 3030 | ctx->devices.push_back(dev); |
| 3031 | } |
| 3032 | |
| 3033 | reg = ggml_backend_reg{ /* .api_version = */ GGML_BACKEND_API_VERSION, |
| 3034 | /* .iface = */ ggml_backend_cann_reg_interface, |
| 3035 | /* .context = */ ctx }; |
| 3036 | } |
| 3037 | |
| 3038 | initialized = true; |
| 3039 | } |
| 3040 | |
| 3041 | return ® |
| 3042 | } |
| 3043 | |
| 3044 | ggml_backend_t ggml_backend_cann_init(int32_t device) { |
| 3045 | aclInit(nullptr); |
| 3046 | if (device < 0 || device >= ggml_backend_cann_get_device_count()) { |
| 3047 | GGML_LOG_ERROR("%s: error: invalid device %d\n", __func__, device); |
| 3048 | return nullptr; |
| 3049 | } |
| 3050 | |
| 3051 | ggml_backend_cann_context * ctx = new ggml_backend_cann_context(device); |
| 3052 | if (ctx == nullptr) { |
| 3053 | GGML_LOG_ERROR("%s: error: failed to allocate context\n", __func__); |
| 3054 | return nullptr; |
| 3055 | } |
| 3056 | ggml_cann_set_device(ctx->device); |
| 3057 | ggml_backend_t cann_backend = |
| 3058 | new ggml_backend{ /* .guid = */ ggml_backend_cann_guid(), |
| 3059 | /* .interface = */ ggml_backend_cann_interface, |
| 3060 | /* .device = */ ggml_backend_reg_dev_get(ggml_backend_cann_reg(), device), |
| 3061 | /* .context = */ ctx }; |
| 3062 | |
| 3063 | return cann_backend; |
| 3064 | } |
| 3065 | |
| 3066 | bool ggml_backend_is_cann(ggml_backend_t backend) { |
| 3067 | return backend != NULL && ggml_guid_matches(backend->guid, ggml_backend_cann_guid()); |
| 3068 | } |
| 3069 | |
| 3070 | int32_t ggml_backend_cann_get_device_count() { |
| 3071 | return ggml_cann_info().device_count; |
| 3072 | } |
| 3073 | |
| 3074 | void ggml_backend_cann_get_device_description(int32_t device, char * description, size_t description_size) { |
| 3075 | ggml_cann_set_device(device); |
| 3076 | const char * soc_name = aclrtGetSocName(); |
| 3077 | snprintf(description, description_size, "%s", soc_name); |
| 3078 | } |
| 3079 | |
| 3080 | void ggml_backend_cann_get_device_memory(int32_t device, size_t * free, size_t * total) { |
| 3081 | ggml_cann_set_device(device); |
| 3082 | ACL_CHECK(aclrtGetMemInfo(ACL_HBM_MEM, free, total)); |
| 3083 | } |
| 3084 | |
| 3085 | GGML_BACKEND_DL_IMPL(ggml_backend_cann_reg) |