MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / ggml_opt_alloc

Function ggml_opt_alloc

subprojects/llama.cpp/ggml/src/ggml-opt.cpp:724–779  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

722}
723
724void ggml_opt_alloc(ggml_opt_context_t opt_ctx, bool backward) {
725 GGML_ASSERT(!opt_ctx->eval_ready);
726 if (opt_ctx->build_type == GGML_OPT_BUILD_TYPE_OPT && opt_ctx->opt_period > 1 && opt_ctx->opt_i == 0) {
727 ggml_graph_reset(opt_ctx->gb_grad);
728 }
729 if (backward) {
730 const int32_t opt_i_next = (opt_ctx->opt_i + 1) % opt_ctx->opt_period;
731 opt_ctx->build_type = opt_i_next == 0 ? GGML_OPT_BUILD_TYPE_OPT : GGML_OPT_BUILD_TYPE_GRAD;
732 } else {
733 opt_ctx->build_type = GGML_OPT_BUILD_TYPE_FORWARD;
734 }
735
736 if (!opt_ctx->static_graphs) {
737 ggml_opt_build(opt_ctx);
738 }
739
740 struct ggml_cgraph * graph = nullptr;
741 switch (opt_ctx->build_type) {
742 case GGML_OPT_BUILD_TYPE_FORWARD: {
743 graph = opt_ctx->gf;
744 } break;
745 case GGML_OPT_BUILD_TYPE_GRAD: {
746 graph = opt_ctx->gb_grad;
747 } break;
748 case GGML_OPT_BUILD_TYPE_OPT: {
749 graph = opt_ctx->gb_opt;
750 } break;
751 }
752 GGML_ASSERT(graph);
753
754 if (opt_ctx->allocated_graph == graph) {
755 opt_ctx->eval_ready = true;
756 return;
757 }
758
759 ggml_backend_sched_reset(opt_ctx->backend_sched); // clear allocation of previous graph
760
761 if (opt_ctx->static_graphs) {
762 ggml_init_params params = {
763 /*.mem_size =*/ graph->size*ggml_tensor_overhead() + ggml_graph_overhead_custom(graph->size, graph->grads),
764 /*.mem_buffer =*/ nullptr,
765 /*.no_alloc =*/ true,
766 };
767 ggml_free(opt_ctx->ctx_copy);
768 opt_ctx->ctx_copy = ggml_init(params);
769
770 opt_ctx->allocated_graph_copy = dup_graph(opt_ctx->ctx_copy, graph);
771 } else {
772 opt_ctx->allocated_graph_copy = graph;
773 }
774
775 ggml_backend_sched_alloc_graph(opt_ctx->backend_sched, opt_ctx->allocated_graph_copy);
776 opt_ctx->allocated_graph = graph;
777
778 opt_ctx->eval_ready = true;
779}
780
781void ggml_opt_eval(ggml_opt_context_t opt_ctx, ggml_opt_result_t result) {

Callers 6

opt_epoch_iterMethod · 0.85
test_gradFunction · 0.85
test_forward_backwardFunction · 0.85
test_idata_splitFunction · 0.85
ggml_opt_epochFunction · 0.85

Calls 9

ggml_graph_resetFunction · 0.85
ggml_opt_buildFunction · 0.85
ggml_backend_sched_resetFunction · 0.85
ggml_tensor_overheadFunction · 0.85
ggml_freeFunction · 0.85
ggml_initFunction · 0.85
dup_graphFunction · 0.85

Tested by 4

test_gradFunction · 0.68
test_forward_backwardFunction · 0.68
test_idata_splitFunction · 0.68