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

Method Reduce

src/opr/impl/basic_arith.cpp:1405–1473  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1403
1404MGB_DYN_TYPE_OBJ_FINAL_IMPL(Reduce);
1405Reduce::Reduce(
1406 VarNode* inp, VarNode* target_shape, const Param& param,
1407 const OperatorNodeConfig& config)
1408 : Super{inp->owner_graph(),
1409 config,
1410 ssprintf("reduce%d", static_cast<int>(param.mode)),
1411 {inp}},
1412 m_param{param},
1413 m_kern_scheduler{std::make_unique<KernScheduler>()} {
1414 add_input({inp});
1415
1416 if (inp->dtype().enumv() == DTypeEnum::Quantized8Asymm &&
1417 inp->dtype().category() == DTypeCategory::QUANTIZED) {
1418 mgb_assert(
1419 param.mode != Param::Mode::PRODUCT,
1420 "Reduce does not support PRODUCT mode on quantized input");
1421 mgb_assert(
1422 param.mode != Param::Mode::SUM_SQR,
1423 "Reduce does not support SUM_SQR mode on quantized input");
1424 mgb_assert(
1425 param.mode != Param::Mode::SUM,
1426 "Reduce does not support SUM mode on quantized input");
1427 }
1428
1429 DType out_dtype;
1430 switch (param.data_type) {
1431 case Param::DataType::DEFAULT:
1432 out_dtype = inp->dtype();
1433 break;
1434#if !MEGDNN_DISABLE_FLOAT16
1435 case Param::DataType::FLOAT_O16xC32:
1436 out_dtype = dtype::Float16();
1437 break;
1438 case Param::DataType::FLOAT_IO16xC32:
1439 mgb_assert(false);
1440#endif
1441 case Param::DataType::FLOAT_O32xC32:
1442 out_dtype = dtype::Float32();
1443 break;
1444 case Param::DataType::QUINT_I8xO32:
1445 out_dtype = dtype::QuantizedS32(
1446 inp->dtype().param<dtype::Quantized8Asymm>().scale);
1447 break;
1448 case Param::DataType::QINT_I8xO32:
1449 out_dtype =
1450 dtype::QuantizedS32(inp->dtype().param<dtype::QuantizedS8>().scale);
1451 break;
1452 default:
1453 mgb_throw(GraphError, "invalid param data_type: %d", int(param.data_type));
1454 }
1455 add_output(None)->add_flag(VarNode::Flag::ALLOW_EMPTY_SHAPE).dtype(out_dtype);
1456 cg::add_workspace_output(this);
1457
1458 add_equivalence_component<PODHash<Param>>(&m_param);
1459
1460 if (param.axis >= -MEGDNN_MAX_NDIM && param.axis < MEGDNN_MAX_NDIM) {
1461 mgb_throw_if(
1462 target_shape, GraphError,

Callers 3

fFunction · 0.80
reduce_sumFunction · 0.80
test_builtin_reduceFunction · 0.80

Calls 4

categoryMethod · 0.80
owner_graphMethod · 0.45
enumvMethod · 0.45
dtypeMethod · 0.45

Tested by 1

test_builtin_reduceFunction · 0.64