| 16343 | } |
| 16344 | |
| 16345 | struct ggml_cgraph * ggml_new_graph_custom(struct ggml_context * ctx, size_t size, bool grads) { |
| 16346 | const size_t obj_size = ggml_graph_nbytes(size, grads); |
| 16347 | struct ggml_object * obj = ggml_new_object(ctx, GGML_OBJECT_GRAPH, obj_size); |
| 16348 | struct ggml_cgraph * cgraph = (struct ggml_cgraph *) ((char *) ctx->mem_buffer + obj->offs); |
| 16349 | |
| 16350 | struct ggml_tensor ** data_start = (struct ggml_tensor **) (cgraph + 1); |
| 16351 | |
| 16352 | size_t hash_size = ggml_hash_size(size * 2); |
| 16353 | struct ggml_tensor ** nodes_ptr = data_start; |
| 16354 | struct ggml_tensor ** leafs_ptr = nodes_ptr + size; |
| 16355 | struct ggml_tensor ** hash_keys_ptr = leafs_ptr + size; |
| 16356 | struct ggml_tensor ** grads_ptr = grads ? hash_keys_ptr + hash_size : NULL; |
| 16357 | |
| 16358 | // check that we allocated the correct amount of memory |
| 16359 | assert(obj_size == (size_t) ( |
| 16360 | (grads ? (char *)(grads_ptr + size) : (char *)(hash_keys_ptr + hash_size)) - (char *)cgraph)); |
| 16361 | |
| 16362 | memset(hash_keys_ptr, 0, hash_size * sizeof(struct ggml_tensor *)); |
| 16363 | |
| 16364 | *cgraph = (struct ggml_cgraph) { |
| 16365 | /*.size =*/ size, |
| 16366 | /*.n_nodes =*/ 0, |
| 16367 | /*.n_leafs =*/ 0, |
| 16368 | /*.nodes =*/ nodes_ptr, |
| 16369 | /*.grads =*/ grads_ptr, |
| 16370 | /*.leafs =*/ leafs_ptr, |
| 16371 | /*.hash_table =*/ { hash_size, hash_keys_ptr }, |
| 16372 | /*.order =*/ GGML_CGRAPH_EVAL_ORDER_LEFT_TO_RIGHT, |
| 16373 | /*.perf_runs =*/ 0, |
| 16374 | /*.perf_cycles =*/ 0, |
| 16375 | /*.perf_time_us =*/ 0, |
| 16376 | }; |
| 16377 | |
| 16378 | return cgraph; |
| 16379 | } |
| 16380 | |
| 16381 | struct ggml_cgraph * ggml_new_graph(struct ggml_context * ctx) { |
| 16382 | return ggml_new_graph_custom(ctx, GGML_DEFAULT_GRAPH_SIZE, false); |