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

Method replace_const_var

src/core/impl/graph/graph_opt.cpp:135–165  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

133}
134
135OperatorNodeBase* GraphOptimizer::replace_const_var(VarNode* var) {
136 if (!is_const_var_value(var))
137 return nullptr;
138
139 {
140 auto type = var->owner_opr()->dyn_typeinfo();
141 if (type == opr::ImmutableTensor::typeinfo())
142 return nullptr;
143 }
144
145 auto&& mgr = var->owner_graph()->static_infer_manager();
146 auto&& shp = mgr.infer_shape(var);
147 if (shp.total_nr_elems() >= MAX_CONST_FOLDING_SIZE)
148 return nullptr;
149
150 auto&& infer_val = mgr.infer_value(var);
151 if (!infer_val.layout().is_contiguous()) {
152 return nullptr;
153 }
154
155 HostTensorND val;
156 val.copy_from(infer_val);
157 auto imm = opr::ImmutableTensor::make(
158 *var->owner_graph(), val,
159 OperatorNodeConfig{}.comp_node(var->comp_node()))
160 .node()
161 ->owner_opr();
162 m_const_map[var] = imm;
163 mgb_assert(imm->output(0)->dtype() == var->dtype());
164 return imm;
165}
166
167// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

Callers

nothing calls this directly

Calls 14

is_const_var_valueFunction · 0.85
infer_valueMethod · 0.80
makeFunction · 0.70
owner_oprMethod · 0.45
owner_graphMethod · 0.45
infer_shapeMethod · 0.45
total_nr_elemsMethod · 0.45
is_contiguousMethod · 0.45
layoutMethod · 0.45
copy_fromMethod · 0.45
nodeMethod · 0.45
comp_nodeMethod · 0.45

Tested by

no test coverage detected