Adjust the location of syntax testcases

This commit is contained in:
huangbingjian 2022-10-11 09:27:43 +08:00
parent cedd215b56
commit 780900ae5f
111 changed files with 741 additions and 560 deletions

View File

@ -123,7 +123,6 @@
"mindspore/tests/ut/python/parallel/" "protected-access" "mindspore/tests/ut/python/parallel/" "protected-access"
"mindspore/tests/ut/python/parameter_feature/test_var_grad.py" "bad-super-call" "mindspore/tests/ut/python/parameter_feature/test_var_grad.py" "bad-super-call"
"mindspore/tests/ut/python/parameter_feature/test_var_grad.py" "redefined-outer-name" "mindspore/tests/ut/python/parameter_feature/test_var_grad.py" "redefined-outer-name"
"mindspore/tests/ut/python/pipeline/parse/test_cont_break.py" "unused-variable"
"mindspore/tests/ut/python/pynative_mode" "no-else-return" "mindspore/tests/ut/python/pynative_mode" "no-else-return"
"mindspore/tests/ut/python/pynative_mode" "superfluous-parens" "mindspore/tests/ut/python/pynative_mode" "superfluous-parens"
"mindspore/tests/ut/python/pynative_mode" "unused-variable" "mindspore/tests/ut/python/pynative_mode" "unused-variable"
@ -133,17 +132,20 @@
"mindspore/tests/ut/python/rewrite/test_node.py" "syntax-error" "mindspore/tests/ut/python/rewrite/test_node.py" "syntax-error"
"mindspore/tests/ut/python/rewrite/test_node.py" "protected-access" "mindspore/tests/ut/python/rewrite/test_node.py" "protected-access"
"mindspore/tests/ut/python/rewrite/test_for.py" "protected-access" "mindspore/tests/ut/python/rewrite/test_for.py" "protected-access"
"mindspore/tests/ut/python/python_builtin/test_list_tuple.py" "len-as-condition"
"mindspore/tests/ut/python/rewrite/test_symbol_tree.py" "len-as-condition" "mindspore/tests/ut/python/rewrite/test_symbol_tree.py" "len-as-condition"
"mindspore/tests/ut/python/rewrite/test_lenet.py" "protected-access" "mindspore/tests/ut/python/rewrite/test_lenet.py" "protected-access"
"mindspore/tests/ut/python/rewrite/test_if.py" "protected-access" "mindspore/tests/ut/python/rewrite/test_if.py" "protected-access"
"mindspore/tests/ut/python/test_log.py" "possibly-unused-variable" "mindspore/tests/ut/python/test_log.py" "possibly-unused-variable"
"mindspore/tests/ut/python/test_log.py" "protected-access" "mindspore/tests/ut/python/test_log.py" "protected-access"
"mindspore/tests/ut/python/train/summary/test_summary_collector.py" "protected-access" "mindspore/tests/ut/python/train/summary/test_summary_collector.py" "protected-access"
"mindspore/tests/ut/python/pipeline/parse/test_super.py" "bad-super-call" "mindspore/tests/ut/python/pipeline/infer/test_hypermap.py" "useless-super-delegation"
"mindspore/tests/ut/python/pipeline/parse/test_super.py" "assignment-from-none"
"mindspore/tests/ut/python/pipeline/parse/test_dictionary.py" "consider-iterating-dictionary"
"mindspore/tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py" "pointless-statement" "mindspore/tests/ut/python/pipeline/parse/test_use_undefined_name_or_unsupported_builtin_function.py" "pointless-statement"
"mindspore/tests/ut/python/graph_syntax" "useless-super-delegation"
"mindspore/tests/ut/python/graph_syntax/dict/test_dictionary.py" "consider-iterating-dictionary"
"mindspore/tests/ut/python/graph_syntax/list/test_list_add_list.py" "superfluous-parens"
"mindspore/tests/ut/python/graph_syntax/control_flow/test_cont_break.py" "unused-variable"
"mindspore/tests/ut/python/graph_syntax/python_builtin_functions/test_list_tuple.py" "len-as-condition"
"mindspore/tests/ut/python/graph_syntax/python_builtin_functions/test_super.py" "bad-super-call"
"mindspore/tests/st/networks/test_gpu_resnet.py" "superfluous-parens" "mindspore/tests/st/networks/test_gpu_resnet.py" "superfluous-parens"
"mindspore/tests/st/networks/test_gpu_resnet.py" "unused-variable" "mindspore/tests/st/networks/test_gpu_resnet.py" "unused-variable"
"mindspore/tests/st/networks/test_gpu_lstm.py" "unused-variable" "mindspore/tests/st/networks/test_gpu_lstm.py" "unused-variable"

View File

@ -0,0 +1,41 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
import numpy as np
from mindspore import Tensor, context, nn
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_abs_tensor():
"""
Feature: JIT Fallback
Description: Test abs(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, y):
x = Tensor([-1, 2])
return abs(x + y)
net = Net()
assert np.all(net(Tensor([-1, 2])).asnumpy() == np.array([2, 4]))

View File

@ -0,0 +1,96 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
import numpy as np
from mindspore import Tensor, context, nn, ms_function
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.skip(reason='Not support yet')
def test_fallback_all_tensor():
"""
Feature: JIT Fallback
Description: Test all(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x, y):
return all(x), all(y)
net = Net()
x = Tensor(np.array([0, 1, 2, 3]))
y = Tensor(np.array([1, 1]))
out1, out2 = net(x, y)
assert (not out1) and out2
@pytest.mark.skip(reason='Not support yet')
def test_fallback_all_list_hybrid():
"""
Feature: JIT Fallback
Description: Test all(List) in graph mode
Expectation: No exception
"""
@ms_function
def foo(a, b):
x = [a, np.array([1]), Tensor(1)]
y = [a, np.array([0]), Tensor(1)]
z = [b, np.array([1]), Tensor(1)]
return all(x), all(y), all(z)
x, y, z = foo(Tensor([1]), Tensor([0]))
assert x and (not y) and (not z)
@pytest.mark.skip(reason='Not support yet')
def test_fallback_any_tensor():
"""
Feature: JIT Fallback
Description: Test any(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x, y):
return any(x), any(y)
net = Net()
x = Tensor(np.array([0, 0]))
y = Tensor(np.array([1, 0]))
out1, out2 = net(x, y)
assert (not out1) and out2
@pytest.mark.skip(reason='Not support yet')
def test_fallback_any_list_hybrid():
"""
Feature: JIT Fallback
Description: Test any(List) in graph mode
Expectation: No exception
"""
@ms_function
def foo(a, b):
x = [a, np.array([1]), Tensor(1)]
y = [a, np.array([0]), Tensor(1)]
z = [b, np.array([1]), Tensor(1)]
return any(x), any(y), any(z)
x, y, z = foo(Tensor([1]), Tensor([0]))
assert x and y and z

View File

@ -0,0 +1,38 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
from mindspore import Tensor, context, ms_function
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_bool_with_input_tensor():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with tensor input.
Expectation: No exception.
"""
@ms_function
def foo():
x = Tensor([10])
return bool(x)
assert foo()

View File

@ -17,7 +17,6 @@ import pytest
import numpy as np import numpy as np
from mindspore import ms_function, context, Tensor from mindspore import ms_function, context, Tensor
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)

View File

@ -0,0 +1,168 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
import numpy as np
from mindspore import Tensor, context, ms_function
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_list_with_input_constant_tensor():
"""
Feature: JIT Fallback
Description: Test list() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = list(Tensor([1, 2, 3]))
x.append(Tensor([4]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 4
assert isinstance(out[0], Tensor)
assert out[0].asnumpy() == 1
assert isinstance(out[1], Tensor)
assert out[1].asnumpy() == 2
assert isinstance(out[2], Tensor)
assert out[2].asnumpy() == 3
assert isinstance(out[3], Tensor)
assert out[3].asnumpy() == 4
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_list_with_input_constant_tensor_2():
"""
Feature: JIT Fallback
Description: Test list() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = list(Tensor([[1, 2], [3, 4]]))
x.append(Tensor([5, 6]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 3
assert isinstance(out[0], Tensor)
assert np.allclose(out[0].asnumpy(), np.array([1, 2]))
assert isinstance(out[1], Tensor)
assert np.allclose(out[1].asnumpy(), np.array([3, 4]))
assert isinstance(out[2], Tensor)
assert np.allclose(out[2].asnumpy(), np.array([5, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_builtin_function_list_with_non_constant_tensor():
"""
Feature: Graph list function.
Description: When the input to list() is non constant tensor, list function will return correct result.
Expectation: No exception.
"""
@ms_function
def foo(x):
return list(x)
ret = foo(Tensor([[1, 2, 3], [4, 5, 6]]))
assert len(ret) == 2
assert np.all(ret[0].asnumpy() == np.array([1, 2, 3]))
assert np.all(ret[1].asnumpy() == np.array([4, 5, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_tuple_with_input_constant_tensor():
"""
Feature: JIT Fallback
Description: Test tuple() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = tuple(Tensor([1, 2, 3]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 3
assert isinstance(out[0], Tensor)
assert out[0].asnumpy() == 1
assert isinstance(out[1], Tensor)
assert out[1].asnumpy() == 2
assert isinstance(out[2], Tensor)
assert out[2].asnumpy() == 3
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_tuple_with_input_constant_tensor_2():
"""
Feature: JIT Fallback
Description: Test tuple() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = list(Tensor([[1, 2], [3, 4]]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 2
assert isinstance(out[0], Tensor)
assert np.allclose(out[0].asnumpy(), np.array([1, 2]))
assert isinstance(out[1], Tensor)
assert np.allclose(out[1].asnumpy(), np.array([3, 4]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_builtin_function_tuple_with_non_constant_tensor():
"""
Feature: Graph tuple function.
Description: When the input to tuple() is non constant tensor, list function will return correct result.
Expectation: No exception.
"""
@ms_function
def foo(x):
return tuple(x)
ret = foo(Tensor([[1, 2, 3], [4, 5, 6]]))
assert len(ret) == 2
assert np.all(ret[0].asnumpy() == np.array([1, 2, 3]))
assert np.all(ret[1].asnumpy() == np.array([4, 5, 6]))

View File

@ -0,0 +1,66 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
import numpy as np
from mindspore import Tensor, context, nn
from mindspore import dtype as mstype
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_round_tensor():
"""
Feature: JIT Fallback
Description: Test round(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x):
return round(x)
net = Net()
x = Tensor(np.array([0.1, 4.51, 9.9]), mstype.float32)
out = net(x)
expect = Tensor(np.array([0.0, 5.0, 10.0]))
np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_round_tensor_constant():
"""
Feature: JIT Fallback
Description: Test any(Tensor) with a constant tensor in graph mode
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self):
x = Tensor(np.array([0.1, 4.51, 9.9]), mstype.float32)
return round(x)
net = Net()
out = net()
expect = Tensor(np.array([0.0, 5.0, 10.0]))
np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())

View File

@ -0,0 +1,126 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
import numpy as np
from mindspore import Tensor, context, nn
from mindspore import dtype as mstype
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_tensor_n_default_1():
"""
Feature: JIT Fallback
Description: Description: Test sum(Tensor) in graph mode with tensor and input n is default.
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x):
return sum(x)
net = Net()
x = Tensor([3, 4, 5], dtype=mstype.float32)
out = net(x)
assert out == 12
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_tensor_n_default_2():
"""
Feature: JIT Fallback
Description: Description: Test sum(Tensor) in graph mode with tensor and input n is default.
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x):
return sum(x)
net = Net()
x = Tensor([[1, 2], [3, 4]], dtype=mstype.float32)
out = net(x)
assert np.allclose(out.asnumpy(), np.array([4, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_with_x_tensor_n_not_default_1():
"""
Feature: JIT Fallback
Description: Test sum() in graph mode with input x tensor and input n not default.
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self, x, y):
return sum(x, y)
net = Net()
x, y = Tensor([3, 4, 5], dtype=mstype.float32), 4
out = net(x, y)
assert out == 16
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_with_x_tensor_n_not_default_2():
"""
Feature: JIT Fallback
Description: Test sum() in graph mode with input x tensor and input n not default.
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self, x, y):
return sum(x, y)
net = Net()
x, y = Tensor([[1, 2], [3, 4]], dtype=mstype.float32), [5, 6]
out = net(x, y)
assert np.allclose(out.asnumpy(), np.array([9, 12]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_with_x_list_of_tensor():
"""
Feature: JIT Fallback
Description: Test sum() in graph mode when input x is list of tensor.
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self, x, y):
return sum(x, y)
net = Net()
x, y = [1, Tensor([[1, 2], [3, 4]]), Tensor([[1, 2], [3, 4]])], Tensor([[1, 1], [1, 1]])
out = net(x, y)
assert np.allclose(out.asnumpy(), np.array([[4, 6], [8, 10]]))

View File

@ -13,11 +13,17 @@
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
import pytest
import mindspore as ms import mindspore as ms
from mindspore import Tensor, Parameter from mindspore import Tensor, Parameter
from mindspore.nn import Cell from mindspore.nn import Cell
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_hyper_param(): def test_hyper_param():
""" """
Feature: Resolve parameter. Feature: Resolve parameter.
@ -47,6 +53,11 @@ def test_hyper_param():
assert output == output_expect assert output == output_expect
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_hyper_param_with_control_sink(): def test_hyper_param_with_control_sink():
""" """
Feature: Resolve parameter. Feature: Resolve parameter.

View File

@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
""" test graph fallback """ """ test ms_class """
import pytest import pytest
import numpy as np import numpy as np
@ -28,7 +28,7 @@ context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_attr(): def test_ms_class_attr():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the attributes of user-defined classes decorated by ms_class. Description: Access the attributes of user-defined classes decorated by ms_class.
@ -58,7 +58,7 @@ def test_fallback_class_attr():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_method(): def test_ms_class_method():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the methods of user-defined classes decorated by ms_class. Description: Access the methods of user-defined classes decorated by ms_class.
@ -93,7 +93,7 @@ def test_fallback_class_method():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_call(): def test_ms_class_call():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Call the __call__ function of user-defined classes decorated by ms_class. Description: Call the __call__ function of user-defined classes decorated by ms_class.
@ -129,7 +129,7 @@ def test_fallback_class_call():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_input_attr(): def test_ms_class_input_attr():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the attributes of user-defined classes decorated by ms_class. Description: Access the attributes of user-defined classes decorated by ms_class.
@ -160,7 +160,7 @@ def test_fallback_class_input_attr():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_input_method(): def test_ms_class_input_method():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the methods of user-defined classes decorated by ms_class. Description: Access the methods of user-defined classes decorated by ms_class.
@ -193,7 +193,7 @@ def test_fallback_class_input_method():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_class_nested(): def test_ms_class_nested():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Test nested ms_class in graph. Description: Test nested ms_class in graph.
@ -228,7 +228,7 @@ def test_fallback_class_class_nested():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_cell_nested(): def test_ms_class_cell_nested():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Test nested ms_class and cell in graph. Description: Test nested ms_class and cell in graph.
@ -278,7 +278,7 @@ def test_fallback_class_cell_nested():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_type_attr(): def test_ms_class_type_attr():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the attributes of class type. Description: Access the attributes of class type.
@ -312,7 +312,7 @@ def test_fallback_class_type_attr():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_create_instance_attr(): def test_ms_class_create_instance_attr():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the attributes of the created class instance. Description: Access the attributes of the created class instance.
@ -342,7 +342,7 @@ def test_fallback_class_create_instance_attr():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_create_instance_method(): def test_ms_class_create_instance_method():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Access the methods of the created class instance. Description: Access the methods of the created class instance.
@ -378,7 +378,7 @@ def test_fallback_class_create_instance_method():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_class_create_instance_call(): def test_ms_class_create_instance_call():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Call the __call__ function of the created class instance. Description: Call the __call__ function of the created class instance.
@ -415,7 +415,7 @@ def test_fallback_class_create_instance_call():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_raise_error_not_class_type(): def test_raise_error_not_class_type():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Decorator ms_class cannot be used for non-class types. Description: Decorator ms_class cannot be used for non-class types.
@ -434,7 +434,7 @@ def test_fallback_raise_error_not_class_type():
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_fallback_raise_error_decorate_cell(): def test_raise_error_decorate_cell():
""" """
Feature: JIT Fallback Feature: JIT Fallback
Description: Decorator ms_class cannot be used for nn.Cell Description: Decorator ms_class cannot be used for nn.Cell

View File

@ -1,435 +0,0 @@
# Copyright 2022 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 python built-in functions in graph mode"""
import pytest
import numpy as np
from mindspore import Tensor, context, nn, ms_function
from mindspore import dtype as mstype
context.set_context(mode=context.GRAPH_MODE)
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_abs_tensor():
"""
Feature: JIT Fallback
Description: Test abs(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, y):
x = Tensor([-1, 2])
return abs(x + y)
net = Net()
assert np.all(net(Tensor([-1, 2])).asnumpy() == np.array([2, 4]))
@pytest.mark.skip(reason='Not support yet')
def test_fallback_all_tensor():
"""
Feature: JIT Fallback
Description: Test all(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x, y):
return all(x), all(y)
net = Net()
x = Tensor(np.array([0, 1, 2, 3]))
y = Tensor(np.array([1, 1]))
out1, out2 = net(x, y)
assert (not out1) and out2
@pytest.mark.skip(reason='Not support yet')
def test_fallback_all_list_hybrid():
"""
Feature: JIT Fallback
Description: Test all(List) in graph mode
Expectation: No exception
"""
@ms_function
def foo(a, b):
x = [a, np.array([1]), Tensor(1)]
y = [a, np.array([0]), Tensor(1)]
z = [b, np.array([1]), Tensor(1)]
return all(x), all(y), all(z)
x, y, z = foo(Tensor([1]), Tensor([0]))
assert x and (not y) and (not z)
@pytest.mark.skip(reason='Not support yet')
def test_fallback_any_tensor():
"""
Feature: JIT Fallback
Description: Test any(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x, y):
return any(x), any(y)
net = Net()
x = Tensor(np.array([0, 0]))
y = Tensor(np.array([1, 0]))
out1, out2 = net(x, y)
assert (not out1) and out2
@pytest.mark.skip(reason='Not support yet')
def test_fallback_any_list_hybrid():
"""
Feature: JIT Fallback
Description: Test any(List) in graph mode
Expectation: No exception
"""
@ms_function
def foo(a, b):
x = [a, np.array([1]), Tensor(1)]
y = [a, np.array([0]), Tensor(1)]
z = [b, np.array([1]), Tensor(1)]
return any(x), any(y), any(z)
x, y, z = foo(Tensor([1]), Tensor([0]))
assert x and y and z
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_round_tensor():
"""
Feature: JIT Fallback
Description: Test round(Tensor) with a variable tensor in graph mode
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x):
return round(x)
net = Net()
x = Tensor(np.array([0.1, 4.51, 9.9]), mstype.float32)
out = net(x)
expect = Tensor(np.array([0.0, 5.0, 10.0]))
np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_round_tensor_constant():
"""
Feature: JIT Fallback
Description: Test any(Tensor) with a constant tensor in graph mode
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self):
x = Tensor(np.array([0.1, 4.51, 9.9]), mstype.float32)
return round(x)
net = Net()
out = net()
expect = Tensor(np.array([0.0, 5.0, 10.0]))
np.testing.assert_almost_equal(out.asnumpy(), expect.asnumpy())
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_tensor_n_default_1():
"""
Feature: JIT Fallback
Description: Description: Test sum(Tensor) in graph mode with tensor and input n is default.
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x):
return sum(x)
net = Net()
x = Tensor([3, 4, 5], dtype=mstype.float32)
out = net(x)
assert out == 12
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_tensor_n_default_2():
"""
Feature: JIT Fallback
Description: Description: Test sum(Tensor) in graph mode with tensor and input n is default.
Expectation: No exception
"""
class Net(nn.Cell):
def construct(self, x):
return sum(x)
net = Net()
x = Tensor([[1, 2], [3, 4]], dtype=mstype.float32)
out = net(x)
assert np.allclose(out.asnumpy(), np.array([4, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_with_x_tensor_n_not_default_1():
"""
Feature: JIT Fallback
Description: Test sum() in graph mode with input x tensor and input n not default.
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self, x, y):
return sum(x, y)
net = Net()
x, y = Tensor([3, 4, 5], dtype=mstype.float32), 4
out = net(x, y)
assert out == 16
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_with_x_tensor_n_not_default_2():
"""
Feature: JIT Fallback
Description: Test sum() in graph mode with input x tensor and input n not default.
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self, x, y):
return sum(x, y)
net = Net()
x, y = Tensor([[1, 2], [3, 4]], dtype=mstype.float32), [5, 6]
out = net(x, y)
assert np.allclose(out.asnumpy(), np.array([9, 12]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_sum_with_x_list_of_tensor():
"""
Feature: JIT Fallback
Description: Test sum() in graph mode when input x is list of tensor.
Expectation: No exception.
"""
class Net(nn.Cell):
def construct(self, x, y):
return sum(x, y)
net = Net()
x, y = [1, Tensor([[1, 2], [3, 4]]), Tensor([[1, 2], [3, 4]])], Tensor([[1, 1], [1, 1]])
out = net(x, y)
assert np.allclose(out.asnumpy(), np.array([[4, 6], [8, 10]]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_list_with_input_constant_tensor():
"""
Feature: JIT Fallback
Description: Test list() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = list(Tensor([1, 2, 3]))
x.append(Tensor([4]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 4
assert isinstance(out[0], Tensor)
assert out[0].asnumpy() == 1
assert isinstance(out[1], Tensor)
assert out[1].asnumpy() == 2
assert isinstance(out[2], Tensor)
assert out[2].asnumpy() == 3
assert isinstance(out[3], Tensor)
assert out[3].asnumpy() == 4
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_list_with_input_constant_tensor_2():
"""
Feature: JIT Fallback
Description: Test list() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = list(Tensor([[1, 2], [3, 4]]))
x.append(Tensor([5, 6]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 3
assert isinstance(out[0], Tensor)
assert np.allclose(out[0].asnumpy(), np.array([1, 2]))
assert isinstance(out[1], Tensor)
assert np.allclose(out[1].asnumpy(), np.array([3, 4]))
assert isinstance(out[2], Tensor)
assert np.allclose(out[2].asnumpy(), np.array([5, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_builtin_function_list_with_non_constant_tensor():
"""
Feature: Graph list function.
Description: When the input to list() is non constant tensor, list function will return correct result.
Expectation: No exception.
"""
@ms_function
def foo(x):
return list(x)
ret = foo(Tensor([[1, 2, 3], [4, 5, 6]]))
assert len(ret) == 2
assert np.all(ret[0].asnumpy() == np.array([1, 2, 3]))
assert np.all(ret[1].asnumpy() == np.array([4, 5, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_tuple_with_input_constant_tensor():
"""
Feature: JIT Fallback
Description: Test tuple() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = tuple(Tensor([1, 2, 3]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 3
assert isinstance(out[0], Tensor)
assert out[0].asnumpy() == 1
assert isinstance(out[1], Tensor)
assert out[1].asnumpy() == 2
assert isinstance(out[2], Tensor)
assert out[2].asnumpy() == 3
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_tuple_with_input_constant_tensor_2():
"""
Feature: JIT Fallback
Description: Test tuple() in graph mode with constant tensor.
Expectation: No exception.
"""
@ms_function
def foo():
x = list(Tensor([[1, 2], [3, 4]]))
return x
out = foo()
assert isinstance(out, tuple)
assert len(out) == 2
assert isinstance(out[0], Tensor)
assert np.allclose(out[0].asnumpy(), np.array([1, 2]))
assert isinstance(out[1], Tensor)
assert np.allclose(out[1].asnumpy(), np.array([3, 4]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_builtin_function_tuple_with_non_constant_tensor():
"""
Feature: Graph tuple function.
Description: When the input to tuple() is non constant tensor, list function will return correct result.
Expectation: No exception.
"""
@ms_function
def foo(x):
return tuple(x)
ret = foo(Tensor([[1, 2, 3], [4, 5, 6]]))
assert len(ret) == 2
assert np.all(ret[0].asnumpy() == np.array([1, 2, 3]))
assert np.all(ret[1].asnumpy() == np.array([4, 5, 6]))
@pytest.mark.level0
@pytest.mark.platform_x86_gpu_training
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_fallback_bool_with_input_tensor_2():
"""
Feature: JIT Fallback
Description: Test bool() in graph mode with tensor input.
Expectation: No exception.
"""
@ms_function
def foo():
x = Tensor([10])
return bool(x)
assert foo()

View File

@ -0,0 +1,21 @@
# Copyright 2022 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.
# ============================================================================
"""setup for pytest"""
import mindspore.context as context
# pylint: disable=unused-argument
def setup_module(module):
context.set_context(mode=context.GRAPH_MODE)

View File

@ -71,6 +71,7 @@ class for_loop_with_continue(Cell):
def test_for_loop_with_continue(): def test_for_loop_with_continue():
run_test(for_loop_with_continue, 10) run_test(for_loop_with_continue, 10)
# pylint: disable=unnecessary-pass # pylint: disable=unnecessary-pass
class for_loop_with_cont_break(Cell): class for_loop_with_cont_break(Cell):
def __init__(self): def __init__(self):
@ -84,7 +85,6 @@ class for_loop_with_cont_break(Cell):
if i > 5: if i > 5:
x *= 3 x *= 3
break break
# x *= 2
x = x * 2 x = x * 2
pass pass
return x return x

View File

@ -21,6 +21,7 @@ from mindspore import context
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
def test_if_function(): def test_if_function():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self, func): def __init__(self, func):

View File

@ -44,6 +44,7 @@ def test_while_loop_phi():
net = WhileSubGraphParam() net = WhileSubGraphParam()
net(x, y, z) net(x, y, z)
class WhileSubGraphParam2(Cell): class WhileSubGraphParam2(Cell):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -91,6 +92,7 @@ def test_while_loop_phi_3():
net = WhileSubGraphParam3(x) net = WhileSubGraphParam3(x)
net() net()
class ControlMixedWhileIf(nn.Cell): class ControlMixedWhileIf(nn.Cell):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -130,6 +132,7 @@ class ControlMixedWhileIf(nn.Cell):
out = out + x out = out + x
return out return out
def test_mixed_while_if(): def test_mixed_while_if():
context.set_context(mode=context.PYNATIVE_MODE) context.set_context(mode=context.PYNATIVE_MODE)
x = np.array(2).astype(np.int32) x = np.array(2).astype(np.int32)

View File

@ -29,6 +29,7 @@ class Net(nn.Cell):
def construct(self): def construct(self):
return self.value1 + self.value2 return self.value1 + self.value2
def test_list_add_list(): def test_list_add_list():
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
net = Net() net = Net()

View File

@ -29,6 +29,7 @@ class Net(nn.Cell):
def construct(self): def construct(self):
return self.list_ * self.number1, self.list_ * self.number2 return self.list_ * self.number1, self.list_ * self.number2
def test_list_mul_number(): def test_list_mul_number():
""" """
Description: test_list_mul_number Description: test_list_mul_number

View File

@ -29,6 +29,7 @@ class Net(nn.Cell):
def construct(self): def construct(self):
return self.number1 * self.list_, self.number2 * self.list_ return self.number1 * self.list_, self.number2 * self.list_
def test_number_mul_list(): def test_number_mul_list():
""" """
Description: test_number_mul_list Description: test_number_mul_list

View File

@ -51,6 +51,7 @@ def test_use_numpy_method():
net() net()
assert "Should not use Python object in runtime" in str(err.value) assert "Should not use Python object in runtime" in str(err.value)
def test_use_numpy_module(): def test_use_numpy_module():
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):

View File

@ -13,12 +13,12 @@
# limitations under the License. # limitations under the License.
# ============================================================================ # ============================================================================
""" test enumerate""" """ test enumerate"""
import operator
import numpy as np import numpy as np
import pytest import pytest
import mindspore.nn as nn import mindspore.nn as nn
from mindspore import Tensor from mindspore import ms_function, context, Tensor
from mindspore import context
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
@ -287,3 +287,19 @@ def test_enumerate_start_type_error():
with pytest.raises(TypeError) as ex: with pytest.raises(TypeError) as ex:
net(x) net(x)
assert "For 'enumerate', the 'start'" in str(ex.value) assert "For 'enumerate', the 'start'" in str(ex.value)
def test_fallback_enumerate_with_numpy():
"""
Feature: JIT Fallback
Description: Test enumerate in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2])
y = enumerate(x)
return tuple(y)
out = foo()
assert operator.eq(out, ((0, 1), (1, 2)))

View File

@ -0,0 +1,56 @@
# Copyright 2022 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 graph fallback hybrid syntax"""
import operator
import numpy as np
from mindspore import ms_function, context
context.set_context(mode=context.GRAPH_MODE)
def test_fallback_filter_with_numpy_and_tensor():
"""
Feature: JIT Fallback
Description: Test filter in graph mode with numpy.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2, 3, 4])
ret = filter(lambda x: x > 2, x)
return tuple(ret)
out = foo()
assert operator.eq(out, (3, 4))
def filter_fn(x):
return x > 2
def test_fallback_filter_with_numpy_and_tensor_2():
"""
Feature: JIT Fallback
Description: Test filter in graph mode with numpy.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2, 3, 4])
ret = filter(filter_fn, x)
return tuple(ret)
out = foo()
assert operator.eq(out, (3, 4))

View File

@ -19,6 +19,7 @@ from mindspore import ms_function, context, Tensor
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
def test_fallback_bin(): def test_fallback_bin():
""" """
Feature: JIT Fallback Feature: JIT Fallback
@ -31,6 +32,7 @@ def test_fallback_bin():
return x return x
assert foo() == '0b11' assert foo() == '0b11'
def test_fallback_chr(): def test_fallback_chr():
""" """
Feature: JIT Fallback Feature: JIT Fallback
@ -98,6 +100,7 @@ def test_fallback_hex():
return x return x
assert foo() == '0xff' assert foo() == '0xff'
def test_fallback_oct(): def test_fallback_oct():
""" """
Feature: JIT Fallback Feature: JIT Fallback

View File

@ -20,71 +20,6 @@ from mindspore import ms_function, context, Tensor
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
def test_fallback_len_with_numpy():
"""
Feature: JIT Fallback
Description: Test len in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2, 3, 4])
return len(x)
out = foo()
assert out == 4
def test_fallback_enumerate_with_numpy():
"""
Feature: JIT Fallback
Description: Test enumerate in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2])
y = enumerate(x)
return tuple(y)
out = foo()
assert operator.eq(out, ((0, 1), (1, 2)))
def test_fallback_zip_with_numpy():
"""
Feature: JIT Fallback
Description: Test zip in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2])
y = np.array([10, 20])
ret = zip(x, y)
return tuple(ret)
out = foo()
assert operator.eq(out, ((1, 10), (2, 20)))
def test_fallback_zip_with_numpy_and_tensor():
"""
Feature: JIT Fallback
Description: Test zip in graph mode with numpy and tensor input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2])
y = Tensor([10, 20])
ret = zip(x, y)
return tuple(ret)
out = foo()
assert operator.eq(out, ((1, 10), (2, 20)))
def test_fallback_map_with_numpy(): def test_fallback_map_with_numpy():
""" """
Feature: JIT Fallback Feature: JIT Fallback
@ -156,39 +91,3 @@ def test_fallback_map_with_numpy_and_tensor_2():
out = foo() out = foo()
assert operator.eq(out, (2, 3, 4, 5)) assert operator.eq(out, (2, 3, 4, 5))
def test_fallback_filter_with_numpy_and_tensor():
"""
Feature: JIT Fallback
Description: Test filter in graph mode with numpy.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2, 3, 4])
ret = filter(lambda x: x > 2, x)
return tuple(ret)
out = foo()
assert operator.eq(out, (3, 4))
def filter_fn(x):
return x > 2
def test_fallback_filter_with_numpy_and_tensor_2():
"""
Feature: JIT Fallback
Description: Test filter in graph mode with numpy.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2, 3, 4])
ret = filter(filter_fn, x)
return tuple(ret)
out = foo()
assert operator.eq(out, (3, 4))

View File

@ -18,6 +18,7 @@ from mindspore import ms_function, context, Tensor
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
def test_fallback_type_with_input_int(): def test_fallback_type_with_input_int():
""" """
Feature: JIT Fallback Feature: JIT Fallback

View File

@ -0,0 +1,54 @@
# Copyright 2022 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 graph fallback hybrid syntax"""
import operator
import numpy as np
from mindspore import ms_function, context, Tensor
context.set_context(mode=context.GRAPH_MODE)
def test_fallback_zip_with_numpy():
"""
Feature: JIT Fallback
Description: Test zip in graph mode with numpy input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2])
y = np.array([10, 20])
ret = zip(x, y)
return tuple(ret)
out = foo()
assert operator.eq(out, ((1, 10), (2, 20)))
def test_fallback_zip_with_numpy_and_tensor():
"""
Feature: JIT Fallback
Description: Test zip in graph mode with numpy and tensor input.
Expectation: No exception.
"""
@ms_function
def foo():
x = np.array([1, 2])
y = Tensor([10, 20])
ret = zip(x, y)
return tuple(ret)
out = foo()
assert operator.eq(out, ((1, 10), (2, 20)))

View File

@ -17,26 +17,31 @@ import pytest
from mindspore import context, ms_function from mindspore import context, ms_function
@ms_function @ms_function
def get_list_comp_1(): def get_list_comp_1():
l = [x for x in range(1, 6)] l = [x for x in range(1, 6)]
return l return l
@ms_function @ms_function
def get_list_comp_2(): def get_list_comp_2():
l = [x * x for x in range(1, 6)] l = [x * x for x in range(1, 6)]
return l return l
@ms_function @ms_function
def get_list_comp_3(): def get_list_comp_3():
l = [x * x for x in range(1, 11) if x % 2 == 0] l = [x * x for x in range(1, 11) if x % 2 == 0]
return l return l
@ms_function @ms_function
def get_list_comp_4(): def get_list_comp_4():
l = [x * x for x in range(1, 11) if x > 5 if x % 2 == 0] l = [x * x for x in range(1, 11) if x > 5 if x % 2 == 0]
return l return l
@ms_function @ms_function
def get_list_comp_5(): def get_list_comp_5():
# Create a ListComp with multiple comprehension. # Create a ListComp with multiple comprehension.
@ -44,16 +49,19 @@ def get_list_comp_5():
l = [y for x in ((1, 2), (3, 4), (5, 6)) for y in x] # [1, 2, 3, 4, 5, 6] l = [y for x in ((1, 2), (3, 4), (5, 6)) for y in x] # [1, 2, 3, 4, 5, 6]
return l return l
@ms_function @ms_function
def get_generator_exp_1(): def get_generator_exp_1():
t = (x for x in range(1, 6)) t = (x for x in range(1, 6))
return t return t
@ms_function @ms_function
def get_generator_exp_2(): def get_generator_exp_2():
t = (x * x for x in range(1, 11) if x > 5 if x % 2 == 0) t = (x * x for x in range(1, 11) if x > 5 if x % 2 == 0)
return t return t
def test_list_comp(): def test_list_comp():
context.set_context(mode=context.GRAPH_MODE) context.set_context(mode=context.GRAPH_MODE)
assert get_list_comp_1() == (1, 2, 3, 4, 5) assert get_list_comp_1() == (1, 2, 3, 4, 5)

Some files were not shown because too many files have changed in this diff Show More