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

Function test_grad

subprojects/llama.cpp/tests/test-opt.cpp:283–326  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

281}
282
283static std::pair<int, int> test_grad(
284 enum ggml_opt_optimizer_type optim,
285 ggml_backend_sched_t backend_sched, ggml_backend_t backend) {
286 int ntest = 0;
287 int npass = 0;
288
289 struct helper_ctx_data cd = helper_get_ctx_data(optim, backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false,
290 /*nbatch_logical =*/ 999999, /*nbatch_physical =*/ 1);
291
292 std::vector<float> grad_history(ndata);
293 for (int64_t idata = 0; idata < ndata; ++idata) {
294 grad_history[idata] = NAN;
295 }
296
297 for (int idata = 0; idata < ndata; ++idata) {
298 const float idataf = idata;
299 ggml_opt_alloc(cd.opt_ctx, /*backward =*/ true);
300 // leaked
301 ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs));
302 ggml_opt_eval(cd.opt_ctx, cd.result);
303 ggml_backend_tensor_get(ggml_opt_grad_acc(cd.opt_ctx, cd.weights), grad_history.data() + idata, 0, sizeof(float));
304 }
305
306 {
307 bool subtest_ok = true;
308 for (int idata = 0; idata < ndata; ++idata) {
309 if (grad_history[idata] != idata + 1) {
310 subtest_ok = false;
311 }
312 }
313 printf(" %s(): ", __func__);
314 if (subtest_ok) {
315 printf("\033[1;32mOK\033[0m\n");
316 npass++;
317 } else {
318 printf("\033[1;31mFAIL\033[0m\n");
319 }
320 ntest++;
321 }
322
323 helper_free_ctx_data(cd);
324
325 return std::make_pair(npass, ntest);
326}
327
328static void helper_after_test_forward_backward(
329 enum ggml_opt_optimizer_type optim,

Callers 1

test_backendFunction · 0.85

Calls 9

helper_get_ctx_dataFunction · 0.85
ggml_opt_allocFunction · 0.85
ggml_backend_tensor_setFunction · 0.85
ggml_nbytesFunction · 0.85
ggml_opt_evalFunction · 0.85
ggml_backend_tensor_getFunction · 0.85
ggml_opt_grad_accFunction · 0.85
helper_free_ctx_dataFunction · 0.85
dataMethod · 0.45

Tested by

no test coverage detected