2022-08-22 15:01:03 +08:00
|
|
|
#include "core/blob.h"
|
2022-07-31 21:43:26 +08:00
|
|
|
#include "core/graph.h"
|
2022-08-22 15:01:03 +08:00
|
|
|
#include "core/runtime.h"
|
2022-08-08 15:52:07 +08:00
|
|
|
#include "operators/matmul.h"
|
2022-07-31 21:43:26 +08:00
|
|
|
#include "test.h"
|
|
|
|
|
2022-08-07 21:12:17 +08:00
|
|
|
namespace infini {
|
2022-07-31 21:43:26 +08:00
|
|
|
|
2022-08-07 21:12:17 +08:00
|
|
|
TEST(Graph, build_and_run) {
|
2022-08-23 16:55:59 +08:00
|
|
|
Runtime runtime = CpuRuntimeObj::getInstance();
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 2, 3}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({1, 3, 4}, DataType::UInt32);
|
|
|
|
Tensor o0 = g->addTensor({1, 2, 4}, DataType::UInt32);
|
2022-08-06 15:58:40 +08:00
|
|
|
g->dataMalloc();
|
2022-08-22 15:01:03 +08:00
|
|
|
i0->copyData(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
|
|
|
|
w0->copyData(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
|
2022-08-15 15:08:56 +08:00
|
|
|
g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
|
2022-08-22 15:01:03 +08:00
|
|
|
runtime->run(g);
|
2022-08-06 15:58:40 +08:00
|
|
|
// check answer
|
2022-08-23 16:55:59 +08:00
|
|
|
auto ans = make_ref<TensorObj>(Shape{1, 2, 4}, DataType::UInt32, runtime);
|
2022-08-22 15:01:03 +08:00
|
|
|
ans->dataMalloc(runtime);
|
|
|
|
ans->copyData(vector<uint32_t>{38, 44, 50, 56, 83, 98, 113, 128});
|
2022-08-06 15:58:40 +08:00
|
|
|
EXPECT_TRUE(o0->equalData(ans));
|
2022-07-31 21:43:26 +08:00
|
|
|
}
|
|
|
|
|
2022-08-07 21:12:17 +08:00
|
|
|
TEST(Graph, perf_engine) {
|
2022-08-23 16:55:59 +08:00
|
|
|
Runtime runtime = CpuRuntimeObj::getInstance();
|
2022-08-22 15:01:03 +08:00
|
|
|
Graph g = make_ref<GraphObj>(runtime);
|
2022-08-17 14:16:01 +08:00
|
|
|
Tensor i0 = g->addTensor({1, 2, 3}, DataType::UInt32);
|
|
|
|
Tensor w0 = g->addTensor({1, 3, 4}, DataType::UInt32);
|
2022-08-15 15:08:56 +08:00
|
|
|
auto matmul = g->addOp<MatmulObj>(i0, w0, nullptr);
|
|
|
|
|
2022-08-07 21:12:17 +08:00
|
|
|
g->dataMalloc();
|
2022-08-22 15:01:03 +08:00
|
|
|
i0->copyData(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
|
|
|
|
w0->copyData(vector<uint32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12});
|
|
|
|
runtime->run(g, true, true);
|
|
|
|
double perfTime = runtime->getPerfTime(g);
|
2022-08-07 21:12:17 +08:00
|
|
|
// The example matmul takes 0.0036ms with one core
|
|
|
|
EXPECT_GT(perfTime, 0);
|
|
|
|
EXPECT_LT(perfTime, 0.01);
|
|
|
|
// check answer
|
2022-08-23 16:55:59 +08:00
|
|
|
auto ans = make_ref<TensorObj>(Shape{1, 2, 4}, DataType::UInt32, runtime);
|
2022-08-22 15:01:03 +08:00
|
|
|
ans->dataMalloc(runtime);
|
|
|
|
ans->copyData(vector<uint32_t>{38, 44, 50, 56, 83, 98, 113, 128});
|
2022-08-15 15:08:56 +08:00
|
|
|
EXPECT_TRUE(matmul->getOutput()->equalData(ans));
|
2022-08-07 21:12:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace infini
|