| 523 | } |
| 524 | |
| 525 | ggml_opt_context_t ggml_opt_init(struct ggml_opt_params params) { |
| 526 | ggml_opt_context_t result = new struct ggml_opt_context; |
| 527 | result->backend_sched = params.backend_sched; |
| 528 | result->ctx_compute = params.ctx_compute; |
| 529 | result->loss_type = params.loss_type; |
| 530 | result->build_type = params.build_type; |
| 531 | result->build_type_alloc = params.build_type; |
| 532 | result->inputs = params.inputs; |
| 533 | result->outputs = params.outputs; |
| 534 | result->opt_period = params.opt_period; |
| 535 | result->get_opt_pars = params.get_opt_pars; |
| 536 | result->get_opt_pars_ud = params.get_opt_pars_ud; |
| 537 | |
| 538 | GGML_ASSERT(result->opt_period >= 1); |
| 539 | |
| 540 | result->static_graphs = result->ctx_compute; |
| 541 | |
| 542 | if (!result->static_graphs) { |
| 543 | GGML_ASSERT(!result->inputs); |
| 544 | GGML_ASSERT(!result->outputs); |
| 545 | return result; |
| 546 | } |
| 547 | |
| 548 | GGML_ASSERT(result->inputs); |
| 549 | GGML_ASSERT(result->outputs); |
| 550 | |
| 551 | result->gf = ggml_new_graph_custom(result->ctx_compute, GGML_DEFAULT_GRAPH_SIZE, /*grads =*/ true); // Forward pass. |
| 552 | ggml_build_forward_expand(result->gf, result->outputs); |
| 553 | |
| 554 | ggml_opt_build(result); |
| 555 | |
| 556 | return result; |
| 557 | } |
| 558 | |
| 559 | void ggml_opt_free(ggml_opt_context_t opt_ctx) { |
| 560 | if (opt_ctx == nullptr) { |