| 174 | } |
| 175 | |
| 176 | void InternalGraphGenerator::add_opr(cg::OperatorNodeBase* opr) { |
| 177 | if (m_opr_set.count(opr)) { |
| 178 | // ignore duplicated oprs (which occur in tests) |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (opr->input().empty()) { |
| 183 | mgb_assert( |
| 184 | opr->same_type<opr::ImmutableTensor>(), |
| 185 | "should not add net source opr %s{%s}", opr->cname(), |
| 186 | opr->dyn_typeinfo()->name); |
| 187 | } |
| 188 | |
| 189 | // currently only single-output opr is supported; ensure it here |
| 190 | for (size_t i = 1; i < opr->output().size(); ++i) { |
| 191 | mgb_assert(opr->output()[i]->contain_flag(VarNode::Flag::VOLATILE_CONTENT)); |
| 192 | } |
| 193 | |
| 194 | if (!m_opr_set.empty()) { |
| 195 | auto nr_remove = m_graph_input_set.erase(opr->output(0)); |
| 196 | mgb_assert(nr_remove == 1, "opr output not added"); |
| 197 | } else { |
| 198 | // opr_set is empty, so this is the endpoint opr |
| 199 | m_var_dep_type[opr->output(0)] = DepType::DEV_VALUE; |
| 200 | } |
| 201 | |
| 202 | m_opr_set.insert(opr); |
| 203 | for (auto inp : opr->input()) { |
| 204 | m_graph_input_set.insert(inp); |
| 205 | } |
| 206 | |
| 207 | for (auto&& i : opr->node_prop().dep_map()) { |
| 208 | DepType dt = i.second & ~DepType::VALUE_ALLOW_EMPTY; |
| 209 | mgb_assert( |
| 210 | dt == DepType::DEV_VALUE || dt == DepType::HOST_VALUE, |
| 211 | "unsupported dep type: opr %s{%s} on input %s dt=%d", opr->cname(), |
| 212 | opr->dyn_typeinfo()->name, i.first->cname(), static_cast<int>(dt)); |
| 213 | m_var_dep_type[i.first] |= i.second; |
| 214 | } |
| 215 | |
| 216 | if (opr->same_type<opr::Reduce>()) { |
| 217 | if (!has_reduce()) { |
| 218 | m_before_reduce_shape = opr->input(0)->shape(); |
| 219 | m_feature_bits |= JITFeatureBits::REDUCE; |
| 220 | } |
| 221 | mgb_assert(opr->input(0)->shape().eq_shape(m_before_reduce_shape)); |
| 222 | find_reduce_opr_deps(opr); |
| 223 | } |
| 224 | if (opr->same_type<opr::Dimshuffle>()) { |
| 225 | m_feature_bits |= JITFeatureBits::DIMSHUFFLE; |
| 226 | find_oprs_depended_by_dimshuffle(opr); |
| 227 | } |
| 228 | if (opr->same_type<mgb::jit::JITExecutor>()) { |
| 229 | auto jit = &opr->cast_final<mgb::jit::JITExecutor>(); |
| 230 | if (jit->has_reduce()) { |
| 231 | if (!has_reduce()) { |
| 232 | m_before_reduce_shape = jit->broadcasted_input_shape(); |
| 233 | m_feature_bits |= JITFeatureBits::REDUCE; |