test_util.cpp 1.19 KB
Newer Older
Gustavo Valiente's avatar
Gustavo Valiente committed
1 2 3 4 5
#include "test_util.h"

#include <chrono>
#include <iostream>
#include "pt_model.h"
6
#include "pt_dispatcher.h"
Gustavo Valiente's avatar
Gustavo Valiente committed
7 8 9 10 11 12 13 14 15 16 17 18

void testModel(pt::Tensor& in, const pt::Tensor& expected, const char* modelFileName, float eps)
{
    std::cout << std::fixed;

    REQUIRE(in.isValid());
    REQUIRE(modelFileName);

    auto model = pt::Model::create(std::string(PT_TEST_MODELS_FOLDER) + '/' + modelFileName + ".model");
    REQUIRE(model);

    pt::Tensor out;
19
    pt::Dispatcher dispatcher;
Gustavo Valiente's avatar
Gustavo Valiente committed
20
    auto startTime = std::chrono::high_resolution_clock::now();
21
    bool success = model->predict(dispatcher, in, out);
Gustavo Valiente's avatar
Gustavo Valiente committed
22 23 24 25 26 27 28 29
    auto elapsedTime = std::chrono::high_resolution_clock::now() - startTime;
    REQUIRE(success);
    REQUIRE(out.isValid());

    for(std::size_t i = 0, l = out.getDims()[0]; i != l; ++i)
    {
        if(std::fabs(out(i) - expected(i)) >= pt::FloatType(eps))
        {
30
	    std::cout << "Diff: " << std::fabs(out(i) - expected(i)) << std::endl;
Gustavo Valiente's avatar
Gustavo Valiente committed
31 32 33 34 35 36 37
            REQUIRE(std::fabs(out(i) - expected(i)) < pt::FloatType(eps));
        }
    }

    auto elapsedMcs = std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime).count();
    std::cout << modelFileName << " elapsed mcs: " << elapsedMcs << std::endl;
}