| 306 | } |
| 307 | |
| 308 | static std::pair<int, int> test_forward_backward( |
| 309 | ggml_backend_sched_t backend_sched, ggml_backend_t backend, const bool high_level, const bool shuffle) { |
| 310 | int ntest = 0; |
| 311 | int npass = 0; |
| 312 | |
| 313 | struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false); |
| 314 | struct ggml_tensor * loss = ggml_opt_loss(cd.opt_ctx); |
| 315 | |
| 316 | std::vector<float> loss_history(ndata); |
| 317 | for (int64_t idata = 0; idata < ndata; ++idata) { |
| 318 | loss_history[idata] = NAN; |
| 319 | } |
| 320 | |
| 321 | { |
| 322 | int64_t ndata; |
| 323 | ggml_opt_result_ndata(cd.result, &ndata); |
| 324 | double loss; |
| 325 | double loss_unc; |
| 326 | ggml_opt_result_loss(cd.result, &loss, &loss_unc); |
| 327 | double accuracy; |
| 328 | double accuracy_unc; |
| 329 | ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc); |
| 330 | const bool subtest_ok = ndata == 0 && loss == 0.0 && std::isnan(loss_unc) && std::isnan(accuracy) && std::isnan(accuracy_unc); |
| 331 | helper_after_test_forward_backward(__func__, high_level, shuffle, "results_initial", subtest_ok, ntest, npass); |
| 332 | } |
| 333 | |
| 334 | if (high_level) { |
| 335 | ggml_opt_dataset_t dataset = cd.dataset_unsupervised; |
| 336 | if (shuffle) { |
| 337 | ggml_opt_dataset_shuffle(cd.opt_ctx, dataset, -1); |
| 338 | } |
| 339 | ggml_opt_epoch(cd.opt_ctx, dataset, nullptr, cd.result, 0, nullptr, nullptr); |
| 340 | } else { |
| 341 | for (int idata = 0; idata < ndata; ++idata) { |
| 342 | const float idataf = idata; |
| 343 | ggml_opt_alloc(cd.opt_ctx, /*backward =*/ false); |
| 344 | ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs)); |
| 345 | ggml_opt_eval(cd.opt_ctx, cd.result); |
| 346 | ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float)); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | { |
| 351 | float weights; |
| 352 | ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float)); |
| 353 | const bool subtest_ok = weights == ndata/2; |
| 354 | helper_after_test_forward_backward(__func__, high_level, shuffle, "weights_after_forward", subtest_ok, ntest, npass); |
| 355 | } |
| 356 | { |
| 357 | int64_t ndata; |
| 358 | ggml_opt_result_ndata(cd.result, &ndata); |
| 359 | bool subtest_ok = ndata == 6; |
| 360 | |
| 361 | double loss; |
| 362 | double loss_unc; |
| 363 | ggml_opt_result_loss(cd.result, &loss, &loss_unc); |
| 364 | subtest_ok = subtest_ok && loss == 33.0 && almost_equal(loss_unc, sqrt(3.5), 1e-10); |
| 365 |
no test coverage detected