| 482 | } |
| 483 | |
| 484 | static std::pair<int, int> test_idata_split(ggml_backend_sched_t backend_sched, ggml_backend_t backend, const bool high_level) { |
| 485 | int ntest = 0; |
| 486 | int npass = 0; |
| 487 | |
| 488 | struct helper_ctx_data cd = helper_get_ctx_data(backend_sched, backend, /*init_opt_ctx =*/ true, /*optimizer_defaults =*/ false); |
| 489 | struct ggml_tensor * loss = ggml_opt_loss(cd.opt_ctx); |
| 490 | const int idata_split = ndata * 2/3; |
| 491 | |
| 492 | std::vector<float> loss_history(ndata); |
| 493 | for (int64_t idata = 0; idata < ndata; ++idata) { |
| 494 | loss_history[idata] = NAN; |
| 495 | } |
| 496 | |
| 497 | for (int epoch = 1; epoch <= 4; ++epoch) { |
| 498 | if (high_level) { |
| 499 | ggml_opt_epoch(cd.opt_ctx, cd.dataset_unsupervised, cd.result, cd.result2, idata_split, nullptr, nullptr); |
| 500 | } else { |
| 501 | int idata = 0; |
| 502 | for (; idata < idata_split; ++idata) { |
| 503 | const float idataf = idata; |
| 504 | ggml_opt_alloc(cd.opt_ctx, /*backward =*/ true); |
| 505 | ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs)); |
| 506 | ggml_opt_eval(cd.opt_ctx, cd.result); |
| 507 | ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float)); |
| 508 | } |
| 509 | for (; idata < ndata; ++idata) { |
| 510 | const float idataf = idata; |
| 511 | ggml_opt_alloc(cd.opt_ctx, /*backward =*/ false); |
| 512 | ggml_backend_tensor_set(cd.inputs, &idataf, 0, ggml_nbytes(cd.inputs)); |
| 513 | ggml_opt_eval(cd.opt_ctx, cd.result2); |
| 514 | ggml_backend_tensor_get(loss, loss_history.data() + idata, 0, sizeof(float)); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | { |
| 519 | float weights; |
| 520 | ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float)); |
| 521 | const bool subtest_ok = weights == ndata/2 - epoch*idata_split; |
| 522 | helper_after_test_idata_split(__func__, high_level, epoch, "weights", subtest_ok, ntest, npass); |
| 523 | } |
| 524 | { |
| 525 | int64_t ndata_result; |
| 526 | ggml_opt_result_ndata(cd.result, &ndata_result); |
| 527 | bool subtest_ok = ndata_result == idata_split; |
| 528 | |
| 529 | double loss; |
| 530 | double loss_unc; |
| 531 | ggml_opt_result_loss(cd.result, &loss, &loss_unc); |
| 532 | subtest_ok = subtest_ok && loss == 28.0 - epoch*16.0 && loss_unc == 0.0; |
| 533 | |
| 534 | double accuracy; |
| 535 | double accuracy_unc; |
| 536 | ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc); |
| 537 | subtest_ok = subtest_ok && std::isnan(accuracy) && std::isnan(accuracy_unc); |
| 538 | |
| 539 | helper_after_test_idata_split(__func__, high_level, epoch, "results_backward", subtest_ok, ntest, npass); |
| 540 | } |
| 541 | { |
no test coverage detected