| 10 | using namespace gopt; |
| 11 | |
| 12 | TEST(TestReformatManager, Feature) { |
| 13 | constexpr size_t N = 16, C = 128, H = 7, W = 7; |
| 14 | HostTensorGenerator<> gen; |
| 15 | using ReformatKey = ReformatManager::ReformatKey; |
| 16 | auto src_format = TensorFormats::NHWC, dst_format = TensorFormats::NCHWc64; |
| 17 | ReformatKey key{src_format, dst_format}; |
| 18 | auto reformat = ReformatManager::instance().get(key); |
| 19 | |
| 20 | auto graph = ComputingGraph::make(); |
| 21 | graph->options().graph_opt_level = 0; |
| 22 | |
| 23 | auto r = [](VarNode* inp) { |
| 24 | auto x = SymbolVar(inp); |
| 25 | auto xshp = opr::GetVarShape::make(x); |
| 26 | auto cv = [&x](int v) { return x.make_scalar(v); }; |
| 27 | auto sub = [&xshp, &cv](int idx) { |
| 28 | return opr::IndexAt::make(xshp, {{0, cv(idx)}}); |
| 29 | }; |
| 30 | auto tshp0 = |
| 31 | opr::Concat::make({sub(0), sub(1), sub(2), sub(3) / 64, cv(64)}, 0); |
| 32 | auto y0 = opr::Reshape::make(x, tshp0); |
| 33 | auto y1 = opr::Dimshuffle::make(y0, {0, 3, 1, 2, 4}); |
| 34 | return y1; |
| 35 | }; |
| 36 | |
| 37 | auto mkvar = [&](const char* name, const TensorShape& shp) { |
| 38 | return opr::Host2DeviceCopy::make(*graph, gen(shp)).rename(name); |
| 39 | }; |
| 40 | auto x = mkvar("x", {N, H, W, C}); |
| 41 | auto y1 = SymbolVar(reformat({x.node()})); |
| 42 | auto y2 = r(x.node()); |
| 43 | size_t nr_shapeof = 0; |
| 44 | size_t nr_reshape = 0; |
| 45 | cg::DepOprIter{[&nr_shapeof, &nr_reshape](cg::OperatorNodeBase* o) { |
| 46 | if (o->same_type<opr::GetVarShape>()) |
| 47 | nr_shapeof++; |
| 48 | if (o->same_type<opr::Reshape>()) |
| 49 | nr_reshape++; |
| 50 | }}.add(y1.node()->owner_opr()); |
| 51 | ASSERT_EQ(nr_shapeof, 1); |
| 52 | ASSERT_EQ(nr_reshape, 1); |
| 53 | HostTensorND t1, t2; |
| 54 | auto func1 = graph->compile({make_callback_copy(y1, t1)}); |
| 55 | func1->execute(); |
| 56 | auto func2 = graph->compile({make_callback_copy(y2, t2)}); |
| 57 | func2->execute(); |
| 58 | MGB_ASSERT_TENSOR_EQ(t1, t2); |
| 59 | } |
| 60 | |
| 61 | TEST(TestReformatManager, Weight) { |
| 62 | constexpr size_t G = 8, K = 128, C = 128, R = 3, S = 3; |
nothing calls this directly
no test coverage detected