InfiniTensor/test/core/test_graph.cc

113 lines
4.3 KiB
C++
Raw Normal View History

#include "core/blob.h"
2022-07-31 21:43:26 +08:00
#include "core/graph.h"
#include "core/runtime.h"
2022-08-08 15:52:07 +08:00
#include "operators/matmul.h"
#include "operators/unary.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) {
Runtime runtime = CpuRuntimeObj::getInstance();
Graph g = make_ref<GraphObj>(runtime);
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);
g->dataMalloc();
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});
auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
g->print();
// check inputOf and outputsOf for tensor
Fix: unsigned compare in test (#50) fix: unsigned compare in test. Test project /home/mazx/git/InfiniTensor/build Start 1: test_graph 1/18 Test #1: test_graph ....................... Passed 0.03 sec Start 2: test_hash 2/18 Test #2: test_hash ........................ Passed 0.01 sec Start 3: test_tensor_save 3/18 Test #3: test_tensor_save ................. Passed 0.02 sec Start 4: test_verify 4/18 Test #4: test_verify ...................... Passed 0.01 sec Start 5: test_batch_norm 5/18 Test #5: test_batch_norm .................. Passed 0.01 sec Start 6: test_concat 6/18 Test #6: test_concat ...................... Passed 0.01 sec Start 7: test_conv 7/18 Test #7: test_conv ........................ Passed 0.24 sec Start 8: test_conv_transposed_2d 8/18 Test #8: test_conv_transposed_2d .......... Passed 0.01 sec Start 9: test_element_wise 9/18 Test #9: test_element_wise ................ Passed 0.01 sec Start 10: test_extend 10/18 Test #10: test_extend ...................... Passed 0.01 sec Start 11: test_gather 11/18 Test #11: test_gather ...................... Passed 0.01 sec Start 12: test_matmul 12/18 Test #12: test_matmul ...................... Passed 0.01 sec Start 13: test_pad 13/18 Test #13: test_pad ......................... Passed 0.01 sec Start 14: test_pooling 14/18 Test #14: test_pooling ..................... Passed 0.01 sec Start 15: test_reduce_mean 15/18 Test #15: test_reduce_mean ................. Passed 0.01 sec Start 16: test_reshape 16/18 Test #16: test_reshape ..................... Passed 0.01 sec Start 17: test_slice 17/18 Test #17: test_slice ....................... Passed 0.01 sec Start 18: test_split 18/18 Test #18: test_split ....................... Passed 0.02 sec 100% tests passed, 0 tests failed out of 18
2022-10-19 15:03:03 +08:00
EXPECT_EQ(i0->getInputOf().size(), 1u);
EXPECT_EQ(w0->getInputOf().size(), 1u);
EXPECT_EQ(o0->getInputOf().size(), 0u);
EXPECT_EQ(i0->getOutputOf(), nullptr);
EXPECT_EQ(w0->getOutputOf(), nullptr);
EXPECT_NE(o0->getOutputOf(), nullptr);
Fix: unsigned compare in test (#50) fix: unsigned compare in test. Test project /home/mazx/git/InfiniTensor/build Start 1: test_graph 1/18 Test #1: test_graph ....................... Passed 0.03 sec Start 2: test_hash 2/18 Test #2: test_hash ........................ Passed 0.01 sec Start 3: test_tensor_save 3/18 Test #3: test_tensor_save ................. Passed 0.02 sec Start 4: test_verify 4/18 Test #4: test_verify ...................... Passed 0.01 sec Start 5: test_batch_norm 5/18 Test #5: test_batch_norm .................. Passed 0.01 sec Start 6: test_concat 6/18 Test #6: test_concat ...................... Passed 0.01 sec Start 7: test_conv 7/18 Test #7: test_conv ........................ Passed 0.24 sec Start 8: test_conv_transposed_2d 8/18 Test #8: test_conv_transposed_2d .......... Passed 0.01 sec Start 9: test_element_wise 9/18 Test #9: test_element_wise ................ Passed 0.01 sec Start 10: test_extend 10/18 Test #10: test_extend ...................... Passed 0.01 sec Start 11: test_gather 11/18 Test #11: test_gather ...................... Passed 0.01 sec Start 12: test_matmul 12/18 Test #12: test_matmul ...................... Passed 0.01 sec Start 13: test_pad 13/18 Test #13: test_pad ......................... Passed 0.01 sec Start 14: test_pooling 14/18 Test #14: test_pooling ..................... Passed 0.01 sec Start 15: test_reduce_mean 15/18 Test #15: test_reduce_mean ................. Passed 0.01 sec Start 16: test_reshape 16/18 Test #16: test_reshape ..................... Passed 0.01 sec Start 17: test_slice 17/18 Test #17: test_slice ....................... Passed 0.01 sec Start 18: test_split 18/18 Test #18: test_split ....................... Passed 0.02 sec 100% tests passed, 0 tests failed out of 18
2022-10-19 15:03:03 +08:00
EXPECT_EQ(matmul->getPredecessors().size(), 0u);
EXPECT_EQ(matmul->getSuccessors().size(), 0u);
runtime->run(g);
// check execution results
auto ans = make_ref<TensorObj>(Shape{1, 2, 4}, DataType::UInt32, runtime);
ans->dataMalloc();
ans->copyData(vector<uint32_t>{38, 44, 50, 56, 83, 98, 113, 128});
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) {
Runtime runtime = CpuRuntimeObj::getInstance();
Graph g = make_ref<GraphObj>(runtime);
Tensor i0 = g->addTensor({1, 2, 3}, DataType::UInt32);
Tensor w0 = g->addTensor({1, 3, 4}, DataType::UInt32);
auto matmul = g->addOp<MatmulObj>(i0, w0, nullptr);
2022-08-07 21:12:17 +08:00
g->dataMalloc();
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
auto ans = make_ref<TensorObj>(Shape{1, 2, 4}, DataType::UInt32, runtime);
ans->dataMalloc();
ans->copyData(vector<uint32_t>{38, 44, 50, 56, 83, 98, 113, 128});
EXPECT_TRUE(matmul->getOutput()->equalData(ans));
2022-08-07 21:12:17 +08:00
}
TEST(Graph, test_tensor_id) {
Runtime runtime = CpuRuntimeObj::getInstance();
Graph g = make_ref<GraphObj>(runtime);
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);
g->dataMalloc();
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});
auto i1 = g->addTensor(i0->clone());
auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
g->print();
EXPECT_NE(i0->getGuid(), i1->getGuid());
EXPECT_EQ(i0->getFuid(), i1->getFuid());
EXPECT_NE(i0->getDataBlob(), nullptr);
EXPECT_EQ(i1->getDataBlob(), nullptr);
}
TEST(Graph, test_OpVec_ctor) {
Runtime runtime = CpuRuntimeObj::getInstance();
Graph g = make_ref<GraphObj>(runtime);
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);
g->dataMalloc();
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});
auto o1 = g->addTensor(o0->clone());
auto matmul = g->addOpWithOutputs<MatmulObj>(i0, w0, o0);
g->addOp<ReluObj>(o1, nullptr);
g->print();
puts("=========");
OpVec ops = g->getOperators();
Graph g2 = make_ref<GraphObj>(runtime, ops);
g2->print();
// Check if the two tensors with the same FUID (o0,o1) remain only one in g2
EXPECT_EQ(g2->getTensors().size(), 4u);
EXPECT_EQ(g2->getOperators().size(), 2u);
map<pair<int, int>, int> inputOutput2Cnt = {
{{1, 0}, 2}, {{1, 1}, 1}, {{0, 1}, 1}};
for (auto t : g2->getTensors()) {
pair<int, int> key = {t->getInputOf().size(),
t->getOutputOf() != nullptr};
EXPECT_GE(inputOutput2Cnt[key], 0);
inputOutput2Cnt[key]--;
}
for (auto [u, v] : inputOutput2Cnt) {
EXPECT_EQ(v, 0);
}
}
} // namespace infini