|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2024 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 | +#pragma once |
| 24 | + |
| 25 | +#include "ggml-backend.h" |
| 26 | +#include "ggml.h" |
| 27 | + |
| 28 | +#ifdef __cplusplus |
| 29 | +extern "C" { |
| 30 | +#endif |
| 31 | + |
| 32 | +/** |
| 33 | + * @def GGML_CANN_NAME |
| 34 | + * @brief Define for the name of the CANN backend. |
| 35 | + */ |
| 36 | +#define GGML_CANN_NAME "CANN" |
| 37 | + |
| 38 | +/** |
| 39 | + * @brief Maximum number of CANN devices supported. |
| 40 | + */ |
| 41 | +#define GGML_CANN_MAX_DEVICES 16 |
| 42 | + |
| 43 | +/** |
| 44 | + * @brief Structure for QK4_0 data format. |
| 45 | + */ |
| 46 | +#define QK4_0 32 |
| 47 | +typedef struct { |
| 48 | + uint16_t d; /**< Delta */ |
| 49 | + uint8_t qs[QK4_0 / 2]; /**< Nibbles / quants */ |
| 50 | +} block_q4_0; |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief Structure for QK8_0 data format. |
| 54 | + */ |
| 55 | +#define QK8_0 32 |
| 56 | +typedef struct { |
| 57 | + uint16_t d; /**< Delta */ |
| 58 | + int8_t qs[QK8_0]; /**< Quants */ |
| 59 | +} block_q8_0; |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Initializes the CANN backend for a specified device. |
| 63 | + * |
| 64 | + * This function initializes the CANN backend for the given device. |
| 65 | + * It verifies the device index, allocates a context, and creates a backend |
| 66 | + * instance. |
| 67 | + * |
| 68 | + * @param device The index of the device to initialize. |
| 69 | + * @return A pointer to the initialized backend instance, or nullptr on failure. |
| 70 | + */ |
| 71 | +GGML_API GGML_CALL ggml_backend_t ggml_backend_cann_init(int32_t device); |
| 72 | + |
| 73 | +/** |
| 74 | + * @brief Checks if a given backend is a CANN backend. |
| 75 | + * |
| 76 | + * This function verifies if the provided backend is a CANN backend by comparing |
| 77 | + * its GUID with the CANN backend's GUID. |
| 78 | + * |
| 79 | + * @param backend The backend instance to check. |
| 80 | + * @return True if the backend is a CANN backend, false otherwise. |
| 81 | + */ |
| 82 | +GGML_API GGML_CALL bool ggml_backend_is_cann(ggml_backend_t backend); |
| 83 | + |
| 84 | +/** |
| 85 | + * @brief Retrieves the CANN buffer type for a specified device. |
| 86 | + * |
| 87 | + * This function initializes and returns the buffer type interface associated |
| 88 | + * with the given device. It ensures thread-safe access using a mutex. |
| 89 | + * |
| 90 | + * @param device The device index for which to retrieve the buffer type. |
| 91 | + * @return A pointer to the buffer type interface for the specified device, or |
| 92 | + * nullptr if the device index is out of range. |
| 93 | + */ |
| 94 | +GGML_API GGML_CALL ggml_backend_buffer_type_t |
| 95 | +ggml_backend_cann_buffer_type(int32_t device); |
| 96 | + |
| 97 | +/** |
| 98 | + * @brief Retrieves the number of CANN devices available. |
| 99 | + * |
| 100 | + * This function returns the number of CANN devices available based on |
| 101 | + * information obtained from `ggml_cann_info()`. |
| 102 | + * |
| 103 | + * @return The number of CANN devices available. |
| 104 | + */ |
| 105 | +GGML_API GGML_CALL int32_t ggml_backend_cann_get_device_count(void); |
| 106 | + |
| 107 | +/** |
| 108 | + * @brief Retrieves the description of a specific CANN device. |
| 109 | + * |
| 110 | + * This function sets the specified device, retrieves the SoC name, |
| 111 | + * and writes it into the provided description buffer. |
| 112 | + * |
| 113 | + * @param device The device index to retrieve the description for. |
| 114 | + * @param description Pointer to a buffer where the description will be written. |
| 115 | + * @param description_size Size of the description buffer. |
| 116 | + */ |
| 117 | +GGML_API GGML_CALL void ggml_backend_cann_get_device_description( |
| 118 | + int32_t device, char* description, size_t description_size); |
| 119 | + |
| 120 | +/** |
| 121 | + * @brief Retrieves the memory information of a specific CANN device. |
| 122 | + * |
| 123 | + * This function sets the specified device, retrieves the free and total |
| 124 | + * memory information of the specified type (ACL_HBM_MEM), and stores them |
| 125 | + * in the provided pointers. |
| 126 | + * |
| 127 | + * @param device The device index to retrieve memory information for. |
| 128 | + * @param free Pointer to a variable where the free memory size will be stored. |
| 129 | + * @param total Pointer to a variable where the total memory size will be |
| 130 | + * stored. |
| 131 | + */ |
| 132 | +GGML_API GGML_CALL void ggml_backend_cann_get_device_memory(int32_t device, |
| 133 | + size_t* free, |
| 134 | + size_t* total); |
| 135 | + |
| 136 | +/** |
| 137 | + * @brief Initializes resources required by the CANN backend. |
| 138 | + */ |
| 139 | +void ggml_cann_backend_init(void); |
| 140 | + |
| 141 | +/** |
| 142 | + * @brief Frees resources used by the CANN backend. |
| 143 | + */ |
| 144 | +void ggml_cann_backend_free(void); |
| 145 | + |
| 146 | +#ifdef __cplusplus |
| 147 | +} |
| 148 | +#endif |
0 commit comments