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

Function _getitem_cpp

imperative/python/src/tensor_utils.cpp:1276–1319  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1274}
1275
1276py::object _getitem_cpp(py::handle inp_hdl, py::handle idx_hdl) {
1277 py::tuple try_res = _try_cond_take(inp_hdl, idx_hdl);
1278 if (try_res.size() == 2) {
1279 return try_res[0];
1280 }
1281 py::tuple tuple_val;
1282 if (py::isinstance<py::tuple>(idx_hdl)) {
1283 tuple_val = py::reinterpret_borrow<py::tuple>(idx_hdl);
1284 } else {
1285 tuple_val = _get_tuple_idx(idx_hdl);
1286 }
1287 if (subtensor_fastpath(inp_hdl, tuple_val)) {
1288 return _fastpath_getitem_cpp(inp_hdl, tuple_val);
1289 }
1290 py::tuple up = _unpack_indexes(inp_hdl, tuple_val);
1291 py::object tensor = py::reinterpret_borrow<py::object>(up[0]);
1292 py::list tensors = py::reinterpret_borrow<py::list>(up[1]);
1293 py::list py_items = py::reinterpret_borrow<py::list>(up[2]);
1294 std::vector<std::tuple<int8_t, bool, bool, bool, bool>> cpp_items;
1295 std::vector<std::tuple<int32_t, int32_t, int32_t, int32_t>> slice_items;
1296 for (size_t i = 0; i < py_items.size(); ++i) {
1297 py::list item = py::reinterpret_borrow<py::list>(py_items[i]);
1298 cpp_items.push_back(
1299 {item[0].cast<int8_t>(), item[1].cast<bool>(), item[2].cast<bool>(),
1300 item[3].cast<bool>(), item[4].cast<bool>()});
1301 }
1302 std::shared_ptr<OpDef> op;
1303 if (up[3].cast<bool>()) {
1304 op = Subtensor::make(cpp_items, slice_items);
1305 } else {
1306 op = IndexingMultiAxisVec::make(cpp_items);
1307 }
1308 std::vector<PyObject*> p;
1309 p.resize(tensors.size() + 2);
1310 py::object Op = py::cast(op);
1311 p[0] = Op.ptr();
1312 p[1] = tensor.ptr();
1313 for (size_t i = 0; i < tensors.size(); ++i) {
1314 p[i + 2] = tensors[i].ptr();
1315 }
1316 py::tuple ret =
1317 py::reinterpret_steal<py::object>(py_apply(NULL, p.data(), p.size()));
1318 return ret[0];
1319}
1320
1321py::object _setitem_cpp(py::handle inp_hdl, py::handle idx_hdl, py::handle val_hdl) {
1322 py::object org_shape = getattr(inp_hdl, "shape");

Callers 1

tensor_utils.cppFile · 0.85

Calls 13

_try_cond_takeFunction · 0.85
_get_tuple_idxFunction · 0.85
subtensor_fastpathFunction · 0.85
_fastpath_getitem_cppFunction · 0.85
_unpack_indexesFunction · 0.85
castFunction · 0.85
py_applyFunction · 0.85
resizeMethod · 0.80
makeFunction · 0.50
sizeMethod · 0.45
push_backMethod · 0.45
ptrMethod · 0.45

Tested by

no test coverage detected