| 822 | } |
| 823 | |
| 824 | static bool ggml_gallocr_reserve_n_impl( |
| 825 | ggml_gallocr_t galloc, struct ggml_cgraph * graph, const int * node_buffer_ids, const int * leaf_buffer_ids, bool no_alloc) { |
| 826 | size_t min_hash_size = graph->n_nodes + graph->n_leafs; |
| 827 | // add 25% margin to avoid hash collisions |
| 828 | min_hash_size += min_hash_size / 4; |
| 829 | |
| 830 | // initialize hash table |
| 831 | if (galloc->hash_set.size < min_hash_size) { |
| 832 | ggml_hash_set_free(&galloc->hash_set); |
| 833 | galloc->hash_set = ggml_hash_set_new(min_hash_size); |
| 834 | GGML_ASSERT(galloc->hash_set.keys != NULL); |
| 835 | |
| 836 | free(galloc->hash_values); |
| 837 | galloc->hash_values = malloc(sizeof(struct hash_node) * galloc->hash_set.size); |
| 838 | GGML_ASSERT(galloc->hash_values != NULL); |
| 839 | } |
| 840 | |
| 841 | // reset allocators |
| 842 | for (int i = 0; i < galloc->n_buffers; i++) { |
| 843 | ggml_dyn_tallocr_reset(galloc->buf_tallocs[i]); |
| 844 | } |
| 845 | |
| 846 | // allocate in hash table |
| 847 | ggml_gallocr_alloc_graph_impl(galloc, graph, node_buffer_ids, leaf_buffer_ids); |
| 848 | |
| 849 | // set the node_allocs from the hash table |
| 850 | if (galloc->n_nodes < graph->n_nodes) { |
| 851 | free(galloc->node_allocs); |
| 852 | galloc->node_allocs = calloc(graph->n_nodes, sizeof(struct node_alloc)); |
| 853 | GGML_ASSERT(galloc->node_allocs != NULL); |
| 854 | } |
| 855 | galloc->n_nodes = graph->n_nodes; |
| 856 | for (int i = 0; i < graph->n_nodes; i++) { |
| 857 | struct ggml_tensor * node = graph->nodes[i]; |
| 858 | struct node_alloc * node_alloc = &galloc->node_allocs[i]; |
| 859 | if (node->view_src || node->data) { |
| 860 | node_alloc->dst.buffer_id = -1; |
| 861 | node_alloc->dst.addr = GGML_BUFFER_ADDRESS_INVALID; |
| 862 | node_alloc->dst.size_max = 0; |
| 863 | } else { |
| 864 | struct hash_node * hn = ggml_gallocr_hash_get(galloc, node); |
| 865 | node_alloc->dst.buffer_id = hn->buffer_id; |
| 866 | node_alloc->dst.addr = hn->addr; |
| 867 | node_alloc->dst.size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], node); |
| 868 | } |
| 869 | for (int j = 0; j < GGML_MAX_SRC; j++) { |
| 870 | struct ggml_tensor * src = node->src[j]; |
| 871 | if (!src || src->view_src || src->data) { |
| 872 | node_alloc->src[j].buffer_id = -1; |
| 873 | node_alloc->src[j].addr = GGML_BUFFER_ADDRESS_INVALID; |
| 874 | node_alloc->src[j].size_max = 0; |
| 875 | } else { |
| 876 | struct hash_node * hn = ggml_gallocr_hash_get(galloc, src); |
| 877 | node_alloc->src[j].buffer_id = hn->buffer_id; |
| 878 | node_alloc->src[j].addr = hn->addr; |
| 879 | node_alloc->src[j].size_max = ggml_backend_buft_get_alloc_size(galloc->bufts[hn->buffer_id], src); |
| 880 | } |
| 881 | } |
no test coverage detected