Skip to content

Commit e92734d

Browse files
test-opt: allow slight inprecision (ggml-org#15503)
1 parent 4536363 commit e92734d

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

tests/test-opt.cpp

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ static std::pair<int, int> test_forward_backward(
358358
double accuracy;
359359
double accuracy_unc;
360360
ggml_opt_result_accuracy(cd.result, &accuracy, &accuracy_unc);
361-
const bool subtest_ok = ndata == 0 && loss == 0.0 && std::isnan(loss_unc) && std::isnan(accuracy) && std::isnan(accuracy_unc);
361+
const bool subtest_ok = ndata == 0 && almost_equal(loss, 0.0, 1e-6) && std::isnan(loss_unc) && std::isnan(accuracy) && std::isnan(accuracy_unc);
362362
helper_after_test_forward_backward(optim, __func__, high_level, shuffle, "results_initial", subtest_ok, ntest, npass);
363363
}
364364

@@ -381,18 +381,20 @@ static std::pair<int, int> test_forward_backward(
381381
{
382382
float weights;
383383
ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
384-
const bool subtest_ok = weights == ndata/2;
384+
const bool subtest_ok = almost_equal(weights, ndata/2, 1e-10);
385385
helper_after_test_forward_backward(optim, __func__, high_level, shuffle, "weights_after_forward", subtest_ok, ntest, npass);
386386
}
387387
{
388+
constexpr double atol = 1e-10;
389+
388390
int64_t ndata;
389391
ggml_opt_result_ndata(cd.result, &ndata);
390392
bool subtest_ok = ndata == 6;
391393

392394
double loss;
393395
double loss_unc;
394396
ggml_opt_result_loss(cd.result, &loss, &loss_unc);
395-
subtest_ok = subtest_ok && loss == 33.0 && almost_equal(loss_unc, sqrt(3.5), 1e-10);
397+
subtest_ok = subtest_ok && almost_equal(loss, 33.0, atol) && almost_equal(loss_unc, sqrt(3.5), atol);
396398

397399
double accuracy;
398400
double accuracy_unc;
@@ -437,7 +439,7 @@ static std::pair<int, int> test_forward_backward(
437439
{
438440
float weights;
439441
ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
440-
const bool subtest_ok = weights == -ndata * .5;
442+
const bool subtest_ok = almost_equal(weights, -ndata * 0.5, 1e-10);
441443
helper_after_test_forward_backward(optim, __func__, high_level, shuffle, "weights_after_forward_backward", subtest_ok, ntest, npass);
442444
}
443445
{
@@ -448,7 +450,7 @@ static std::pair<int, int> test_forward_backward(
448450
double loss;
449451
double loss_unc;
450452
ggml_opt_result_loss(cd.result, &loss, &loss_unc);
451-
subtest_ok = subtest_ok && loss == 18.0 && (shuffle || loss_unc == 0.0);
453+
subtest_ok = subtest_ok && almost_equal(loss, 18.0, 1e-10) && (shuffle || loss_unc == 0.0);
452454

453455
double accuracy;
454456
double accuracy_unc;
@@ -550,18 +552,20 @@ static std::pair<int, int> test_idata_split(
550552
if (adamw) {
551553
float weights;
552554
ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
553-
const bool subtest_ok = weights == ndata/2 - epoch*idata_split;
555+
const bool subtest_ok = almost_equal(weights, ndata/2 - epoch*idata_split, 1e-10);
554556
helper_after_test_idata_split(optim, __func__, high_level, epoch, "weights", subtest_ok, ntest, npass);
555557
}
556558
if (adamw) {
559+
constexpr double atol = 1e-10;
560+
557561
int64_t ndata_result;
558562
ggml_opt_result_ndata(cd.result, &ndata_result);
559563
bool subtest_ok = ndata_result == idata_split;
560564

561565
double loss;
562566
double loss_unc;
563567
ggml_opt_result_loss(cd.result, &loss, &loss_unc);
564-
subtest_ok = subtest_ok && loss == 28.0 - epoch*16.0 && loss_unc == 0.0;
568+
subtest_ok = subtest_ok && almost_equal(loss, 28.0 - epoch*16.0, atol) && almost_equal(loss_unc, 0.0, atol);
565569

566570
double accuracy;
567571
double accuracy_unc;
@@ -571,14 +575,16 @@ static std::pair<int, int> test_idata_split(
571575
helper_after_test_idata_split(optim, __func__, high_level, epoch, "results_backward", subtest_ok, ntest, npass);
572576
}
573577
if (adamw) {
578+
constexpr double atol = 1e-10;
579+
574580
int64_t ndata_result;
575581
ggml_opt_result_ndata(cd.result2, &ndata_result);
576582
bool subtest_ok = ndata_result == ndata - idata_split;
577583

578584
double loss;
579585
double loss_unc;
580586
ggml_opt_result_loss(cd.result2, &loss, &loss_unc);
581-
subtest_ok = subtest_ok && loss == 15.0 - epoch*8 && almost_equal(loss_unc, sqrt(0.5), 1e-10);
587+
subtest_ok = subtest_ok && almost_equal(loss, 15.0 - epoch*8, atol) && almost_equal(loss_unc, sqrt(0.5), atol);
582588

583589
double accuracy;
584590
double accuracy_unc;
@@ -687,22 +693,24 @@ static std::pair<int, int> test_gradient_accumulation(
687693
}
688694
bool const adamw = optim == GGML_OPT_OPTIMIZER_TYPE_ADAMW;
689695
if (adamw) {
696+
constexpr double atol = 1e-6;
690697
float weights;
691698
ggml_backend_tensor_get(cd.weights, &weights, 0, sizeof(float));
692-
const bool subtest_ok = weights == (ndata/2) - epoch;
699+
const bool subtest_ok = almost_equal(weights, (ndata/2) - epoch, atol);
693700
helper_after_test_gradient_accumulation(optim, __func__, nbatch_physical, loss_type, epoch, "weights", subtest_ok, ntest, npass);
694701
}
695702
{
703+
constexpr double atol = 1e-6;
696704
int64_t ndata_result;
697705
ggml_opt_result_ndata(cd.result, &ndata_result);
698-
bool subtest_ok = ndata_result == ndata/nbatch_physical;
706+
bool subtest_ok = almost_equal(ndata_result, ndata/nbatch_physical, atol);
699707

700708
double loss;
701709
ggml_opt_result_loss(cd.result, &loss, /*loss_unc =*/ nullptr);
702710
if (loss_type == GGML_OPT_LOSS_TYPE_SUM) {
703-
subtest_ok = subtest_ok && loss == (39.0 - epoch*6.0);
711+
subtest_ok = subtest_ok && almost_equal(loss, (39.0 - epoch*6.0), atol);
704712
} else if (loss_type == GGML_OPT_LOSS_TYPE_MEAN) {
705-
subtest_ok = subtest_ok && almost_equal(loss, (39.0 - epoch*6.0) / ndata, 1e-6);
713+
subtest_ok = subtest_ok && almost_equal(loss, (39.0 - epoch*6.0) / ndata, atol);
706714
} else {
707715
GGML_ASSERT(false);
708716
}

0 commit comments

Comments
 (0)