| 335 | } |
| 336 | |
| 337 | static std::pair<int, int> test_forward_backward( |
| 338 | enum ggml_opt_optimizer_type optim, |
| 339 | ggml_backend_sched_t backend_sched, ggml_backend_t backend, const bool high_level, const bool shuffle) { |
| 340 | int ntest = 0; |
| 341 | int npass = 0; |
| 342 | |
| 343 | struct helper_ctx_data cd = helper_get_ctx_data(optim, backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false); |
| 344 | struct ggml_tensor * loss = ggml_opt_loss(cd.opt_ctx); |
| 345 | |
| 346 | std::vector<float> loss_history(ndata); |
| 347 | for (int64_t idata = 0; idata < ndata; ++idata) { |
| 348 | loss_history[idata] = NAN; |
| 349 | } |
| 350 | |
| 351 | { |
| 352 | int64_t ndata; |
| 353 | ggml_opt_result_ndata(cd.result, &ndata); |
| 354 | double loss; |
| 355 | double loss_unc; |
| 356 | ggml_opt_result_loss(cd.result, &loss, &loss_unc); |
| 357 | double accuracy; |
| 358 | double accuracy_unc; |
| 359 | ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc); |
| 360 | const bool subtest_ok = ndata == 0 && almost_equal(loss, 0.0, 1e-6) && std::isnan(loss_unc) && std::isnan(accuracy) && std::isnan(accuracy_unc); |
| 361 | helper_after_test_forward_backward(optim, __func__, high_level, shuffle, "results_initial", subtest_ok, ntest, npass); |
| 362 | } |
| 363 | |
| 364 | if (high_level) { |
| 365 | ggml_opt_dataset_t dataset = cd.dataset_unsupervised; |
| 366 | if (shuffle) { |
| 367 | ggml_opt_dataset_shuffle(cd.opt_ctx, dataset, -1); |
| 368 | } |
| 369 | ggml_opt_epoch(cd.opt_ctx, dataset, nullptr, cd.result, 0, nullptr, nullptr); |
| 370 | } else { |
| 371 | for (int idata = 0; idata < ndata; ++idata) { |
| 372 | const float idataf = idata; |
| 373 | ggml_opt_alloc(cd.opt_ctx, /*backward =*/ false); |
| 374 | ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs)); |
| 375 | ggml_opt_eval(cd.opt_ctx, cd.result); |
| 376 | ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float)); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | { |
| 381 | float weights; |
| 382 | ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float)); |
| 383 | const bool subtest_ok = almost_equal(weights, ndata/2, 1e-10); |
| 384 | helper_after_test_forward_backward(optim, __func__, high_level, shuffle, "weights_after_forward", subtest_ok, ntest, npass); |
| 385 | } |
| 386 | { |
| 387 | constexpr double atol = 1e-10; |
| 388 | |
| 389 | int64_t ndata; |
| 390 | ggml_opt_result_ndata(cd.result, &ndata); |
| 391 | bool subtest_ok = ndata == 6; |
| 392 | |
| 393 | double loss; |
| 394 | double loss_unc; |
no test coverage detected