Raise NotImplementedError when parsing the numpy methods, but not the numpy constant.

This commit is contained in:
huanghui 2021-08-03 14:33:22 +08:00
parent 61ac011cfc
commit 3e17650172
2 changed files with 73 additions and 1 deletions

View File

@ -159,12 +159,17 @@ def resolve_symbol(namespace, symbol):
if getattr(resolve_, "__hash__") is None:
return resolve_
# Raise NotImplementedError when parsing the numpy methods, but not the numpy constant.
if namespace.name == "numpy" and isinstance(resolve_, (types.FunctionType, types.MethodType, types.ModuleType)):
raise NotImplementedError(
f"MindSpore does not support to use the numpy methods in the function construct with the graph mode.")
# If need trope the obj
if resolve_ in convert_object_map:
resolve_ = convert_object_map.get(resolve_)
logger.debug("convert resolve = %r", resolve_)
if resolve_ == NO_IMPLEMENT:
raise NotImplementedError(f"Not support for `{symbol}`")
raise NotImplementedError(f"Not support for `{symbol}`.")
except Exception as e:
if isinstance(e, NotImplementedError):
raise e

View File

@ -0,0 +1,67 @@
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test_parse_numpy """
import pytest
import numpy as np
from mindspore import nn
from mindspore import context
context.set_context(mode=context.GRAPH_MODE)
def test_use_numpy_constant():
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
def construct(self):
ret = np.pi
return ret
net = Net()
output = net()
assert np.allclose(output, np.pi)
def test_use_numpy_method():
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
def construct(self):
ret = np.linspace(1, 10, 4)
return ret
net = Net()
with pytest.raises(NotImplementedError) as err:
net()
assert "MindSpore does not support to use the numpy methods in the function construct with the graph mode." \
in str(err.value)
def test_use_numpy_module():
class Net(nn.Cell):
def __init__(self):
super(Net, self).__init__()
def construct(self):
ret = np.random.randint(0, 10, [1, 10])
return ret
net = Net()
with pytest.raises(NotImplementedError) as err:
net()
assert "MindSpore does not support to use the numpy methods in the function construct with the graph mode." \
in str(err.value)