| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #ifndef CANN_COMMON_H |
| #define CANN_COMMON_H |
|
|
| #include "../ggml-impl.h" |
| #include "../include/ggml-cann.h" |
| #include "../include/ggml.h" |
|
|
| #include <acl/acl.h> |
| #include <unistd.h> |
|
|
| #include <atomic> |
| #include <condition_variable> |
| #include <cstdio> |
| #include <functional> |
| #include <iostream> |
| #include <list> |
| #include <map> |
| #include <memory> |
| #include <mutex> |
| #include <optional> |
| #include <string> |
| #include <thread> |
| #include <vector> |
|
|
| #define MATRIX_ROW_PADDING 512 |
| #define GGML_CANN_MAX_STREAMS 8 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [[noreturn]] void ggml_cann_error(const char * stmt, const char * func, const char * file, int line, const char * msg); |
|
|
| |
| |
| |
| |
| |
| |
| |
| #define ACL_CHECK_GEN(stmt, success, error_fn) \ |
| do { \ |
| int err_code = (stmt); \ |
| if (err_code != (success)) { \ |
| ggml_cann_error(#stmt, __func__, __FILE__, __LINE__, error_fn()); \ |
| } \ |
| } while (0); |
|
|
| #define ACL_CHECK(stmt) ACL_CHECK_GEN(stmt, 0, aclGetRecentErrMsg) |
|
|
| |
| |
| |
| struct ggml_cann_device_info { |
| |
| |
| |
| int32_t device_count; |
|
|
| |
| |
| |
| struct cann_device_info { |
| int cc; |
| size_t smpb; |
| bool vmm; |
| size_t vmm_granularity; |
| size_t total_vram; |
| }; |
|
|
| cann_device_info devices[GGML_CANN_MAX_DEVICES] = {}; |
| }; |
|
|
| const ggml_cann_device_info & ggml_cann_info(); |
|
|
| void ggml_cann_set_device(int32_t device); |
|
|
| std::optional<std::string> get_env_as_lowercase(const std::string & name); |
| bool parse_bool(const std::string & value); |
| int parse_integer(const std::string & value); |
|
|
| |
| |
| |
| struct ggml_cann_pool { |
| |
| |
| |
| virtual ~ggml_cann_pool() = default; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| virtual void * alloc(size_t size, size_t * actual_size) = 0; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| virtual void free(void * ptr, size_t size) = 0; |
| }; |
|
|
| |
| |
| |
| struct ggml_cann_pool_alloc { |
| ggml_cann_pool * pool = nullptr; |
| void * ptr = nullptr; |
| size_t actual_size = 0; |
|
|
| |
| |
| |
| ggml_cann_pool_alloc() = default; |
|
|
| |
| |
| |
| |
| explicit ggml_cann_pool_alloc(ggml_cann_pool & pool) : pool(&pool) {} |
|
|
| |
| |
| |
| |
| |
| ggml_cann_pool_alloc(ggml_cann_pool & pool, size_t size) : pool(&pool) { alloc(size); } |
|
|
| |
| |
| |
| ~ggml_cann_pool_alloc() { |
| if (ptr != nullptr) { |
| pool->free(ptr, actual_size); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| void * alloc(size_t size) { |
| GGML_ASSERT(pool != nullptr); |
| GGML_ASSERT(ptr == nullptr); |
| ptr = pool->alloc(size, &this->actual_size); |
| return ptr; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| void * alloc(ggml_cann_pool & pool, size_t size) { |
| this->pool = &pool; |
| return alloc(size); |
| } |
|
|
| |
| |
| |
| |
| void * get() { return ptr; } |
|
|
| |
| ggml_cann_pool_alloc(const ggml_cann_pool_alloc &) = delete; |
|
|
| |
| ggml_cann_pool_alloc(ggml_cann_pool_alloc &&) = delete; |
|
|
| |
| ggml_cann_pool_alloc & operator=(const ggml_cann_pool_alloc &) = delete; |
|
|
| |
| ggml_cann_pool_alloc & operator=(ggml_cann_pool_alloc &&) = delete; |
| }; |
|
|
| #ifdef USE_ACL_GRAPH |
| struct ggml_graph_node_properties { |
| |
| void * node_address; |
| int64_t ne[GGML_MAX_DIMS]; |
| size_t nb[GGML_MAX_DIMS]; |
|
|
| |
| void * src_address[GGML_MAX_SRC]; |
| int64_t src_ne[GGML_MAX_SRC][GGML_MAX_DIMS]; |
| size_t src_nb[GGML_MAX_SRC][GGML_MAX_DIMS]; |
|
|
| |
| ggml_op node_op; |
| int32_t op_params[GGML_MAX_OP_PARAMS / sizeof(int32_t)]; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| bool has_matching_properties(ggml_tensor * node) { |
| if (node->data != this->node_address && node->op != GGML_OP_VIEW) { |
| return false; |
| } |
|
|
| if (node->op != this->node_op) { |
| return false; |
| } |
|
|
| for (int i = 0; i < GGML_MAX_DIMS; i++) { |
| if (node->ne[i] != this->ne[i]) { |
| return false; |
| } |
| if (node->nb[i] != this->nb[i]) { |
| return false; |
| } |
| } |
|
|
| for (int i = 0; i < GGML_MAX_SRC; i++) { |
| if (node->src[i]) { |
| if (node->src[i]->data != this->src_address[i] && node->op != GGML_OP_VIEW) { |
| return false; |
| } |
|
|
| for (int d = 0; d < GGML_MAX_DIMS; d++) { |
| if (node->src[i]->ne[d] != this->src_ne[i][d]) { |
| return false; |
| } |
| if (node->src[i]->nb[d] != this->src_nb[i][d]) { |
| return false; |
| } |
| } |
| } else { |
| if (this->src_address[i] != nullptr) { |
| return false; |
| } |
| } |
| } |
|
|
| if (node->op == GGML_OP_SCALE || node->op == GGML_OP_UNARY || node->op == GGML_OP_GLU) { |
| return memcmp(this->op_params, node->op_params, GGML_MAX_OP_PARAMS) == 0; |
| } |
| return true; |
| } |
| }; |
|
|
| struct ggml_cann_graph { |
| ~ggml_cann_graph() { |
| if (graph != nullptr) { |
| ACL_CHECK(aclmdlRIDestroy(graph)); |
| } |
| } |
|
|
| aclmdlRI graph = nullptr; |
|
|
| std::vector<ggml_graph_node_properties> ggml_graph_properties; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| static ggml_cann_graph * create_from_cgraph(ggml_cgraph * cgraph) { |
| ggml_cann_graph * new_graph = new ggml_cann_graph(); |
| new_graph->ggml_graph_properties.resize(cgraph->n_nodes); |
|
|
| for (int node_idx = 0; node_idx < cgraph->n_nodes; ++node_idx) { |
| ggml_tensor * node = cgraph->nodes[node_idx]; |
| auto & prop = new_graph->ggml_graph_properties[node_idx]; |
|
|
| prop.node_address = node->data; |
| prop.node_op = node->op; |
|
|
| std::copy_n(node->ne, GGML_MAX_DIMS, prop.ne); |
| std::copy_n(node->nb, GGML_MAX_DIMS, prop.nb); |
|
|
| for (int src = 0; src < GGML_MAX_SRC; ++src) { |
| if (node->src[src]) { |
| prop.src_address[src] = node->src[src]->data; |
| std::copy_n(node->src[src]->ne, GGML_MAX_DIMS, prop.src_ne[src]); |
| std::copy_n(node->src[src]->nb, GGML_MAX_DIMS, prop.src_nb[src]); |
| } else { |
| prop.src_address[src] = nullptr; |
| std::fill_n(prop.src_ne[src], GGML_MAX_DIMS, 0); |
| std::fill_n(prop.src_nb[src], GGML_MAX_DIMS, 0); |
| } |
| } |
|
|
| memcpy(prop.op_params, node->op_params, GGML_MAX_OP_PARAMS); |
| } |
|
|
| return new_graph; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| bool matches_cgraph(ggml_cgraph * cgraph) { |
| if (this->ggml_graph_properties.size() != static_cast<size_t>(cgraph->n_nodes)) { |
| return false; |
| } |
|
|
| for (int i = 0; i < cgraph->n_nodes; ++i) { |
| if (!this->ggml_graph_properties[i].has_matching_properties(cgraph->nodes[i])) { |
| return false; |
| } |
| } |
|
|
| return true; |
| } |
| }; |
|
|
| |
| |
| |
| |
| |
| |
| |
| struct ggml_cann_graph_lru_cache { |
| size_t capacity; |
|
|
| std::list<ggml_cann_graph *> cache_list; |
|
|
| ggml_cann_graph_lru_cache() { capacity = parse_integer(get_env_as_lowercase("GGML_CANN_GRAPH_CACHE_CAPACITY").value_or("12")); } |
|
|
| |
| |
| |
| |
| |
| |
| void push(ggml_cann_graph * new_node) { |
| if (cache_list.size() >= capacity) { |
| ggml_cann_graph * old = cache_list.back(); |
| cache_list.pop_back(); |
| delete old; |
| } |
| cache_list.push_front(new_node); |
| } |
|
|
| |
| |
| |
| void clear() { |
| for (auto ptr : cache_list) { |
| delete ptr; |
| } |
| cache_list.clear(); |
| } |
|
|
| |
| |
| |
| ~ggml_cann_graph_lru_cache() { clear(); } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| bool find_and_move_to_front(ggml_cgraph * cgraph) { |
| for (auto & graph_ptr : this->cache_list) { |
| if (graph_ptr->matches_cgraph(cgraph)) { |
| cache_list.remove(graph_ptr); |
| cache_list.push_front(graph_ptr); |
| return true; |
| } |
| } |
| return false; |
| } |
| }; |
| #endif |
|
|
| struct ggml_cann_rope_cache { |
| ~ggml_cann_rope_cache() { |
| if (theta_scale_cache) { |
| ACL_CHECK(aclrtFree(theta_scale_cache)); |
| } |
| if (sin_cache) { |
| ACL_CHECK(aclrtFree(sin_cache)); |
| } |
| if (cos_cache) { |
| ACL_CHECK(aclrtFree(cos_cache)); |
| } |
| if (position_select_index) { |
| ACL_CHECK(aclrtFree(position_select_index)); |
| } |
| if (theta_scale_exp_host) { |
| free(theta_scale_exp_host); |
| } |
| if (position_select_index_host) { |
| free(position_select_index_host); |
| } |
| if (yarn_ramp_cache) { |
| ACL_CHECK(aclrtFree(yarn_ramp_cache)); |
| } |
| } |
|
|
| bool equal(int64_t theta_scale_length, |
| int64_t position_length, |
| float ext_factor, |
| float theta_scale, |
| float freq_scale, |
| float attn_factor, |
| bool is_neox, |
| bool indep_sects, |
| bool mrope_used, |
| bool is_imrope, |
| int sections[4]) { |
| return this->theta_scale_length == theta_scale_length && this->position_length == position_length && |
| this->ext_factor == ext_factor && this->theta_scale == theta_scale && this->freq_scale == freq_scale && |
| this->attn_factor == attn_factor && this->is_neox == is_neox && this->indep_sects == indep_sects && |
| this->mrope_used == mrope_used && this->is_imrope == is_imrope && this->sections[0] == sections[0] && |
| this->sections[1] == sections[1] && this->sections[2] == sections[2] && this->sections[3] == sections[3]; |
| } |
|
|
| void set(int64_t theta_scale_length, |
| int64_t position_length, |
| float ext_factor, |
| float theta_scale, |
| float freq_scale, |
| float attn_factor, |
| bool is_neox, |
| bool indep_sects, |
| bool mrope_used, |
| bool is_imrope, |
| int sections[4]) { |
| this->theta_scale_length = theta_scale_length; |
| this->position_length = position_length; |
| this->ext_factor = ext_factor; |
| this->theta_scale = theta_scale; |
| this->freq_scale = freq_scale; |
| this->attn_factor = attn_factor; |
| this->is_neox = is_neox; |
| this->indep_sects = indep_sects; |
| this->mrope_used = mrope_used; |
| this->is_imrope = is_imrope; |
| this->sections[0] = sections[0]; |
| this->sections[1] = sections[1]; |
| this->sections[2] = sections[2]; |
| this->sections[3] = sections[3]; |
| } |
|
|
| |
| void * theta_scale_cache = nullptr; |
| float * theta_scale_exp_host = nullptr; |
| int * position_select_index_host = nullptr; |
| void * position_select_index = nullptr; |
| void * yarn_ramp_cache = nullptr; |
| |
| void * sin_cache = nullptr; |
| void * cos_cache = nullptr; |
| |
| int64_t theta_scale_length = 0; |
| int64_t position_length = 0; |
| bool cached = false; |
| float ext_factor = 0.0f; |
| float theta_scale = 0.0f; |
| float freq_scale = 0.0f; |
| float attn_factor = 0.0f; |
| bool is_neox = false; |
| bool indep_sects = false; |
| bool mrope_used = false; |
| int sections[4] = { 0, 0, 0, 0 }; |
| bool is_imrope = false; |
| }; |
|
|
| struct ggml_cann_tensor_cache { |
| ~ggml_cann_tensor_cache() { |
| if (cache != nullptr) { |
| ACL_CHECK(aclrtFree(cache)); |
| } |
| } |
|
|
| void * cache = nullptr; |
| int64_t size = 0; |
| }; |
|
|
| |
| |
| |
| struct ggml_backend_cann_context { |
| int32_t device; |
| std::string name; |
| std::string description; |
| aclrtEvent copy_event = nullptr; |
| #ifdef USE_ACL_GRAPH |
| |
| ggml_cann_graph_lru_cache graph_lru_cache; |
| bool acl_graph_mode = true; |
| #endif |
| bool async_mode; |
| |
| ggml_cann_rope_cache rope_cache; |
| |
| ggml_cann_tensor_cache rms_norm_one_tensor_cache; |
| ggml_cann_tensor_cache rms_norm_zero_tensor_cache; |
|
|
| aclrtStream streams[GGML_CANN_MAX_STREAMS] = { nullptr }; |
|
|
| |
| |
| |
| |
| explicit ggml_backend_cann_context(int device) : device(device), name("CANN" + std::to_string(device)) { |
| ggml_cann_set_device(device); |
| description = aclrtGetSocName(); |
|
|
| #ifdef USE_ACL_GRAPH |
| acl_graph_mode = parse_bool(get_env_as_lowercase("GGML_CANN_ACL_GRAPH").value_or("on")); |
| GGML_LOG_INFO("%s: device %d execution mode is %s (%s)\n", __func__, device, acl_graph_mode ? "GRAPH" : "EAGER", |
| acl_graph_mode ? "acl graph enabled" : "acl graph disabled"); |
| #endif |
| } |
|
|
| |
| |
| |
| ~ggml_backend_cann_context() { |
| ggml_cann_set_device(device); |
| if (copy_event != nullptr) { |
| ACL_CHECK(aclrtDestroyEvent(copy_event)); |
| } |
| for (int i = 0; i < GGML_CANN_MAX_STREAMS; ++i) { |
| if (streams[i] != nullptr) { |
| ACL_CHECK(aclrtDestroyStream(streams[i])); |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| aclrtStream stream(int stream) { |
| if (streams[stream] == nullptr) { |
| |
| |
| |
| ACL_CHECK(aclrtSetDevice(device)); |
| ACL_CHECK(aclrtCreateStream(&streams[stream])); |
| } |
| return streams[stream]; |
| } |
|
|
| |
| |
| |
| |
| aclrtStream stream() { return stream(0); } |
|
|
| |
| std::unique_ptr<ggml_cann_pool> mem_pool; |
|
|
| |
| |
| |
| |
| |
| static std::unique_ptr<ggml_cann_pool> new_pool_for_device(int device); |
|
|
| |
| |
| |
| |
| ggml_cann_pool & pool() { |
| if (mem_pool == nullptr) { |
| mem_pool = new_pool_for_device(device); |
| } |
| return *mem_pool; |
| } |
| }; |
|
|
| #endif |
|
|