MCPcopy Create free account
hub / github.com/alibaba/MNN / PyMNNVar_subscript

Function PyMNNVar_subscript

pymnn/src/expr.h:531–583  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

529 return isbool;
530}
531static PyObject* PyMNNVar_subscript(PyObject* x, PyObject* slice) {
532 // gather: 1. 0-1 gather; 2. idx gather;
533 if (isIdx(slice)) {
534 auto val = toVar(x);
535 auto idx = toVar(slice);
536 if (nullptr == val->getInfo()) {
537 PyMNN_ERROR("Can't support subscript for tensor without shape\n");
538 Py_RETURN_NONE;
539 }
540 if (val->getInfo()->size > 1 && isBoolIdx(idx, val->getInfo()->size)) {
541 // 0-1 gather -> idx gather
542 idx = Express::_Where(idx);
543 val = Express::_GatherND(val, idx);
544 val = Express::_Reshape(val, {-1});
545 return toPyObj(val);
546 }
547 auto r = Express::_Gather(val, idx);
548 r->readMap<void>();
549 return toPyObj(r);
550 }
551
552 std::vector<int> begin, end, strides;
553 int new_axis_mask = 0, shrink_axis_mask = 0, begin_mask = 0, end_mask = 0, ellipsis_mask = 0;
554 dealSlice(slice, begin, end, strides, new_axis_mask, shrink_axis_mask, begin_mask, end_mask, ellipsis_mask);
555 int size_ = static_cast<int>(begin.size());
556 auto begin_ = Express::_Const(begin.data(), {size_}, NHWC, halide_type_of<int>());
557 auto end_ = Express::_Const(end.data(), {size_}, NHWC, halide_type_of<int>());
558 auto strides_ = Express::_Const(strides.data(), {size_}, NHWC, halide_type_of<int>());
559 auto res = Express::_StridedSlice(toVar(x), begin_, end_, strides_, begin_mask, end_mask,
560 ellipsis_mask, new_axis_mask, shrink_axis_mask);
561 auto info = res->getInfo();
562 if (!info) {
563 MNN_ERROR("subscript: unable to get variable info");
564 Py_RETURN_NONE;
565 }
566 // to scalar
567 if (info->dim.empty()) {
568 auto dtype = info->type;
569 if (dtype == halide_type_of<float>()) {
570 return toPyObj(res->readMap<float>()[0]);
571 }
572 if (dtype == halide_type_of<int>()) {
573 return toPyObj(res->readMap<int>()[0]);
574 }
575 if (dtype == halide_type_of<uint8_t>()) {
576 return toPyObj(res->readMap<uint8_t>()[0]);
577 }
578 if (dtype == halide_type_of<double>()) {
579 return toPyObj((float)res->readMap<double>()[0]);
580 }
581 }
582 return toPyObj(res);
583}
584
585static int PyMNNVar_ass_subscript(PyObject* x, PyObject* slice, PyObject* y) {
586 if (!isVar(x) || !isVar(y)) {

Callers

nothing calls this directly

Calls 15

isIdxFunction · 0.85
toVarFunction · 0.85
isBoolIdxFunction · 0.85
_WhereFunction · 0.85
_GatherNDFunction · 0.85
_ReshapeFunction · 0.85
_GatherFunction · 0.85
dealSliceFunction · 0.85
_ConstFunction · 0.85
_StridedSliceFunction · 0.85
toPyObjFunction · 0.70
getInfoMethod · 0.45

Tested by

no test coverage detected