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

Function ggml_graph_reset

subprojects/llama.cpp/ggml/src/ggml.c:7097–7131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7095}
7096
7097void ggml_graph_reset(struct ggml_cgraph * cgraph) {
7098 if (!cgraph) {
7099 return;
7100 }
7101 GGML_ASSERT(cgraph->grads != NULL);
7102
7103 for (int i = 0; i < cgraph->n_nodes; i++) {
7104 struct ggml_tensor * node = cgraph->nodes[i];
7105 struct ggml_tensor * grad_acc = ggml_graph_get_grad_acc(cgraph, node);
7106
7107 if (node->op == GGML_OP_OPT_STEP_ADAMW) {
7108 // clear momenta
7109 ggml_set_zero(node->src[2]);
7110 ggml_set_zero(node->src[3]);
7111 }
7112
7113 // initial gradients of loss should be 1, 0 otherwise
7114 if (grad_acc) {
7115 if (node->flags & GGML_TENSOR_FLAG_LOSS) {
7116 GGML_ASSERT(grad_acc->type == GGML_TYPE_F32);
7117 GGML_ASSERT(ggml_is_scalar(grad_acc));
7118
7119 const float onef = 1.0f;
7120 if (grad_acc->buffer) {
7121 ggml_backend_tensor_set(grad_acc, &onef, 0, sizeof(float));
7122 } else {
7123 GGML_ASSERT(grad_acc->data);
7124 *((float *) grad_acc->data) = onef;
7125 }
7126 } else {
7127 ggml_set_zero(grad_acc);
7128 }
7129 }
7130 }
7131}
7132
7133void ggml_graph_clear(struct ggml_cgraph * cgraph) {
7134 cgraph->n_leafs = 0;

Callers 4

eval_gradMethod · 0.85
ggml_opt_buildFunction · 0.85
ggml_opt_resetFunction · 0.85
ggml_opt_allocFunction · 0.85

Calls 4

ggml_graph_get_grad_accFunction · 0.85
ggml_set_zeroFunction · 0.85
ggml_is_scalarFunction · 0.85
ggml_backend_tensor_setFunction · 0.85

Tested by 1

eval_gradMethod · 0.68