!27642 Before resolve getitem, check if the index is ValueNode.

Merge pull request !27642 from 张清华/opt_cell_list_getattr2
This commit is contained in:
i-robot 2021-12-15 03:41:00 +00:00 committed by Gitee
commit 40b10cad65
2 changed files with 9 additions and 4 deletions

View File

@ -69,9 +69,9 @@ AnfNodePtr Resolver::operator()(const OptimizerPtr &optimizer, const AnfNodePtr
if (is_getattr_getitem) {
constexpr size_t resolve_index = 1;
auto resolve_node = getitem_cnode->input(resolve_index);
if (IsPrimitiveCNode(resolve_node, prim::kPrimResolve)) {
constexpr size_t position_index = 2;
auto index_node = getitem_cnode->input(position_index);
constexpr size_t position_index = 2;
auto index_node = getitem_cnode->input(position_index);
if (IsPrimitiveCNode(resolve_node, prim::kPrimResolve) && index_node->isa<ValueNode>()) {
auto [name_space, symbol] = parse::GetNamespaceAndSymbol(resolve_node);
auto py_item = parse::GetItemObjectFromSequence(name_space, symbol, resolve_node, index_node);
return parse::ResolveCellWithAttr(optimizer->manager(), py_item, resolve_node, attr);

View File

@ -331,7 +331,12 @@ py::object GetItemObjectFromSequence(const NameSpacePtr &name_space, const Symbo
const std::string fn = PYTHON_MOD_GET_ITEM_FROM_SEQUENCE;
const std::string module = "mindspore._extends.parse.parser";
int index = GetValueNode<Int64ImmPtr>(index_node)->value();
auto imm_value = GetValueNode<Int64ImmPtr>(index_node);
if (imm_value == nullptr) {
MS_LOG(EXCEPTION) << "Expect a int64 value node, node: " << node->DebugString()
<< ", index_node: " << index_node->DebugString();
}
int index = imm_value->value();
MS_LOG(DEBUG) << "obj: " << py::str(obj) << ", index: " << index;
py::object item_obj = parse::python_adapter::GetPyFn(module, fn)(obj, py::int_(index));
return item_obj;