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

Function _adaptive_pool2d_cpp

imperative/python/src/tensor_utils.cpp:1083–1143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1081}
1082
1083py::object _adaptive_pool2d_cpp(
1084 py::handle inp_hdl, py::handle shape_val_hdl, py::handle pool_mode_hdl) {
1085 py::object shape_hdl = py::reinterpret_borrow<py::object>(shape_val_hdl);
1086 py::list shps(0);
1087 auto mode_string = pool_mode_hdl.cast<std::string>();
1088 ::megdnn::param::AdaptivePooling::Mode pool_mode =
1089 ::megdnn::param::AdaptivePooling::Mode::MAX;
1090 if (mode_string.compare(std::string("AVERAGE")) == 0) {
1091 pool_mode = ::megdnn::param::AdaptivePooling::Mode::AVERAGE;
1092 }
1093 std::shared_ptr<OpDef> op;
1094 std::vector<PyObject*> p;
1095 auto pool_format = ::megdnn::param::AdaptivePooling::Format::NCHW;
1096 auto inp_format = getattr(inp_hdl, "format").cast<std::string>();
1097 if (inp_format == "nhwc") {
1098 pool_format = ::megdnn::param::AdaptivePooling::Format::NHWC;
1099 }
1100 if (TensorWrapper::try_cast(shape_val_hdl.ptr())) {
1101 std::vector<int32_t> shp;
1102 op = AdaptivePooling::make(pool_mode, pool_format, shp);
1103 py::object Op = py::cast(op);
1104 p.resize(3);
1105 p[0] = Op.ptr();
1106 p[1] = inp_hdl.ptr();
1107 p[2] = shape_val_hdl.ptr();
1108 py::tuple ret =
1109 py::reinterpret_steal<py::object>(py_apply(NULL, p.data(), p.size()));
1110 return ret[0];
1111 } else if (!PyTuple_Check(shape_val_hdl.ptr())) {
1112 shps.append(PyLong_AsLong(shape_val_hdl.ptr()));
1113 shps.append(PyLong_AsLong(shape_val_hdl.ptr()));
1114
1115 shape_hdl = py::reinterpret_borrow<py::object>(shps);
1116 }
1117 py::object shape_tuple;
1118 try {
1119 shape_tuple = _make_shape_tuple(shape_hdl);
1120 } catch (py::error_already_set& err) {
1121 shape_tuple = py::reinterpret_borrow<py::object>(shape_hdl);
1122 }
1123
1124 auto [shape, fastpath] = tuple2vector(shape_tuple);
1125 fastpath &= enable_fastpath(inp_hdl);
1126 py::object shape_tensor;
1127 op = AdaptivePooling::make(pool_mode, pool_format, shape);
1128 if (fastpath) {
1129 p.resize(2);
1130 } else {
1131 p.resize(3);
1132 shape_tensor = _astensor1d_cpp(
1133 shape_hdl, py::cast((mgb::DType)dtype::Int32()),
1134 getattr(inp_hdl, "device"), inp_hdl);
1135 p[2] = shape_tensor.ptr();
1136 }
1137 py::object Op = py::cast(op);
1138 p[0] = Op.ptr();
1139 p[1] = inp_hdl.ptr();
1140 py::tuple ret =

Callers 1

tensor_utils.cppFile · 0.85

Calls 13

try_castFunction · 0.85
castFunction · 0.85
py_applyFunction · 0.85
_make_shape_tupleFunction · 0.85
tuple2vectorFunction · 0.85
enable_fastpathFunction · 0.85
_astensor1d_cppFunction · 0.85
resizeMethod · 0.80
makeFunction · 0.50
ptrMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected