MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / TEST

Function TEST

src/gopt/test/gopt_old_model.cpp:70–130  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

68} // namespace
69
70TEST(TestGoptOldModel, FoldingGlobalPooling) {
71 HostTensorGenerator<> gen;
72 auto cn = CompNode::load("cpu0");
73 auto graph = ComputingGraph::make();
74 graph->options().graph_opt_level = 0;
75 auto mkcvar = [&](const char* name, const TensorShape& shp) {
76 return opr::SharedDeviceTensor::make(*graph, *gen(shp, cn)).rename(name);
77 };
78
79 auto host_x = gen({2, 3, 16, 16}, cn);
80 auto x = opr::Host2DeviceCopy::make(*graph, host_x);
81
82 opr::Convolution::Param param_conv;
83 param_conv.stride_h = param_conv.stride_w = 1;
84 param_conv.pad_h = param_conv.pad_w = 1;
85 auto w1 = mkcvar("w1", {8, 3, 3, 3});
86 auto conv1 =
87 opr::Convolution::make(x, w1, param_conv, {}, OperatorNodeConfig("conv1"));
88 auto conv_n = opr::GetVarShape::make(conv1, 0);
89 auto conv_c = opr::GetVarShape::make(conv1, 1);
90 auto conv_h = opr::GetVarShape::make(conv1, 2);
91 auto conv_w = opr::GetVarShape::make(conv1, 3);
92 auto hxw = conv_h * conv_w;
93 auto reshape_shape = opr::Concat::make({conv_n, conv_c, hxw}, 0);
94
95 auto reshape1 = opr::Reshape::make(conv1, reshape_shape);
96
97 opr::Reduce::Param param_reduce;
98 param_reduce.axis = 2;
99 param_reduce.mode = opr::Reduce::Mode::SUM;
100 auto reduce = opr::Reduce::make(reshape1, param_reduce);
101 auto reduce_remove_axis = opr::AxisAddRemove::make(
102 reduce, {opr::AxisAddRemove::AxisDesc::make_remove(2)});
103 auto hw_count = opr::GetVarShape::make(reshape1, 2);
104
105 auto fp32_hw_count = opr::TypeCvt::make(hw_count, dtype::Float32());
106 auto true_div = reduce_remove_axis / fp32_hw_count;
107 auto y = opr::AxisAddRemove::make(
108 true_div, {opr::AxisAddRemove::AxisDesc::make_add(2),
109 opr::AxisAddRemove::AxisDesc::make_add(3)});
110
111 SymbolVar y_opt = y;
112 {
113 auto options = gopt::OptimizeForInferenceOptions{};
114 options.fuse_grain = true;
115 unpack_vector(gopt::optimize_for_inference({y}, options), y_opt);
116 }
117 ASSERT_EQ(
118 opr::AdaptivePooling::Param::Mode::AVERAGE,
119 find_opr<opr::AdaptivePooling>(y_opt).param().mode);
120
121 graph->compile({{y_opt, {}}})
122 ->to_json()
123 ->writeto_fpath(output_file("TestGoptOldModel.FoldingGlobalPooling.json"));
124
125 HostTensorND host_y_opt, host_y;
126 auto func = graph->compile(
127 {make_callback_copy(y, host_y), make_callback_copy(y_opt, host_y_opt)});

Callers

nothing calls this directly

Calls 14

OperatorNodeConfigClass · 0.85
unpack_vectorFunction · 0.85
output_fileFunction · 0.85
make_callback_copyFunction · 0.85
renameMethod · 0.80
loadFunction · 0.50
makeFunction · 0.50
genFunction · 0.50
optimize_for_inferenceFunction · 0.50
paramMethod · 0.45
writeto_fpathMethod · 0.45
to_jsonMethod · 0.45

Tested by

no test coverage detected