| 6964 | } |
| 6965 | |
| 6966 | struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads) { |
| 6967 | const size_t obj_size = ggml_graph_nbytes(size, grads); |
| 6968 | struct ggml_object * obj = ggml_new_object(ctx, GGML_OBJECT_TYPE_GRAPH, obj_size); |
| 6969 | struct ggml_cgraph * cgraph = (struct ggml_cgraph *) ((char *) ctx->mem_buffer + obj->offs); |
| 6970 | |
| 6971 | // the size of the hash table is doubled since it needs to hold both nodes and leafs |
| 6972 | size_t hash_size = ggml_hash_size(size * 2); |
| 6973 | |
| 6974 | void * p = cgraph + 1; |
| 6975 | |
| 6976 | struct ggml_tensor ** nodes_ptr = incr_ptr_aligned(&p, size * sizeof(struct ggml_tensor *), sizeof(struct ggml_tensor *)); |
| 6977 | struct ggml_tensor ** leafs_ptr = incr_ptr_aligned(&p, size * sizeof(struct ggml_tensor *), sizeof(struct ggml_tensor *)); |
| 6978 | int32_t * use_counts_ptr = incr_ptr_aligned(&p, hash_size * sizeof(int32_t), sizeof(int32_t)); |
| 6979 | struct ggml_tensor ** hash_keys_ptr = incr_ptr_aligned(&p, hash_size * sizeof(struct ggml_tensor *), sizeof(struct ggml_tensor *)); |
| 6980 | struct ggml_tensor ** grads_ptr = grads ? incr_ptr_aligned(&p, hash_size * sizeof(struct ggml_tensor *), sizeof(struct ggml_tensor *)) : NULL; |
| 6981 | struct ggml_tensor ** grad_accs_ptr = grads ? incr_ptr_aligned(&p, hash_size * sizeof(struct ggml_tensor *), sizeof(struct ggml_tensor *)) : NULL; |
| 6982 | |
| 6983 | ggml_bitset_t * hash_used = incr_ptr_aligned(&p, ggml_bitset_size(hash_size) * sizeof(ggml_bitset_t), sizeof(ggml_bitset_t)); |
| 6984 | |
| 6985 | // check that we allocated the correct amount of memory |
| 6986 | assert(obj_size == (size_t)((char *)p - (char *)cgraph)); |
| 6987 | |
| 6988 | *cgraph = (struct ggml_cgraph) { |
| 6989 | /*.size =*/ size, |
| 6990 | /*.n_nodes =*/ 0, |
| 6991 | /*.n_leafs =*/ 0, |
| 6992 | /*.nodes =*/ nodes_ptr, |
| 6993 | /*.grads =*/ grads_ptr, |
| 6994 | /*.grad_accs =*/ grad_accs_ptr, |
| 6995 | /*.leafs =*/ leafs_ptr, |
| 6996 | /*.use_counts =*/ use_counts_ptr, |
| 6997 | /*.hash_table =*/ { hash_size, hash_used, hash_keys_ptr }, |
| 6998 | /*.order =*/ GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT, |
| 6999 | }; |
| 7000 | |
| 7001 | ggml_hash_set_reset(&cgraph->visited_hash_set); |
| 7002 | if (grads) { |
| 7003 | memset(cgraph->grads, 0, hash_size*sizeof(struct ggml_tensor *)); |
| 7004 | memset(cgraph->grad_accs, 0, hash_size*sizeof(struct ggml_tensor *)); |
| 7005 | } |
| 7006 | |
| 7007 | return cgraph; |
| 7008 | } |
| 7009 | |
| 7010 | struct ggml_cgraph * ggml_new_graph(struct ggml_context * ctx) { |
| 7011 | return ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, false); |