From e0ab15bb6b478949a4e474ef4c4a0070941f8205 Mon Sep 17 00:00:00 2001 From: simson Date: Mon, 28 Sep 2020 14:57:07 +0800 Subject: [PATCH] add st for pynative --- .../st/pynative/loss_scale/test_loss_scale.py | 2 +- .../pynative/parser/test_parser_construct.py | 70 +++++++++++++++++++ .../{ => parser}/test_parser_operator.py | 9 ++- .../{ => parser}/test_parser_tensor_assign.py | 2 +- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 tests/st/pynative/parser/test_parser_construct.py rename tests/st/pynative/{ => parser}/test_parser_operator.py (88%) rename tests/st/pynative/{ => parser}/test_parser_tensor_assign.py (99%) diff --git a/tests/st/pynative/loss_scale/test_loss_scale.py b/tests/st/pynative/loss_scale/test_loss_scale.py index b17f6fbdc1f..4558b0f6aa2 100644 --- a/tests/st/pynative/loss_scale/test_loss_scale.py +++ b/tests/st/pynative/loss_scale/test_loss_scale.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================ - +""" test_loss_scale """ import numpy as np import pytest import mindspore.nn as nn diff --git a/tests/st/pynative/parser/test_parser_construct.py b/tests/st/pynative/parser/test_parser_construct.py new file mode 100644 index 00000000000..65d8cace8e1 --- /dev/null +++ b/tests/st/pynative/parser/test_parser_construct.py @@ -0,0 +1,70 @@ +# Copyright 2020 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_parser_construct """ +import pytest +import numpy as np +from mindspore import context +from mindspore.nn import Cell +from mindspore.common.tensor import Tensor +from mindspore.ops import operations as P +from mindspore.ops.composite import GradOperation + +def setup_module(): + context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend") + +@pytest.mark.level0 +@pytest.mark.platform_arm_ascend_training +@pytest.mark.platform_x86_ascend_training +@pytest.mark.env_onecard +def test_parser_construct(): + class ParentNet(Cell): + def __init__(self): + super().__init__() + self.relu = P.ReLU() + + def construct(self, x): + return self.relu(x) + + class UncleNet(Cell): + def __init__(self): + super(UncleNet, self).__init__() + self.sigmoid = P.Sigmoid() + + def construct(self, x): + return self.sigmoid(x) + + class Net(UncleNet, ParentNet): + def __init__(self): + super().__init__() + super(UncleNet, self).__init__() + + def construct(self, x): + return super(UncleNet, self).construct(x) + + input_np_x = np.ones([2, 3, 4, 5]).astype(np.float32) + out_np = np.ones([2, 3, 4, 5]).astype(np.float32) + + input_me = Tensor(input_np_x) + output_grad_me = Tensor(out_np) + net = Net() + out_me = net(input_me) + + net1 = Net() + grad = GradOperation(sens_param=True) + grad_op = grad(net1) + grad_me = grad_op(input_me, output_grad_me) + + assert np.allclose(input_np_x, out_me.asnumpy(), 0.001, 0.001) + assert np.allclose(input_np_x, grad_me.asnumpy(), 0.001, 0.001) diff --git a/tests/st/pynative/test_parser_operator.py b/tests/st/pynative/parser/test_parser_operator.py similarity index 88% rename from tests/st/pynative/test_parser_operator.py rename to tests/st/pynative/parser/test_parser_operator.py index 7dea734b5be..93633690b56 100644 --- a/tests/st/pynative/test_parser_operator.py +++ b/tests/st/pynative/parser/test_parser_operator.py @@ -12,16 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================ - +""" test_parser_operator """ +import pytest +import numpy as np from mindspore import context from mindspore.nn import ReLU from mindspore.nn import Cell from mindspore.common.tensor import Tensor -import numpy as np def setup_module(): context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend") +@pytest.mark.level0 +@pytest.mark.platform_arm_ascend_training +@pytest.mark.platform_x86_ascend_training +@pytest.mark.env_onecard def test_parser_operator_floor_div(): class Net(Cell): def __init__(self): diff --git a/tests/st/pynative/test_parser_tensor_assign.py b/tests/st/pynative/parser/test_parser_tensor_assign.py similarity index 99% rename from tests/st/pynative/test_parser_tensor_assign.py rename to tests/st/pynative/parser/test_parser_tensor_assign.py index 7b4bc672cd7..43892224ce2 100644 --- a/tests/st/pynative/test_parser_tensor_assign.py +++ b/tests/st/pynative/parser/test_parser_tensor_assign.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================ - +""" test_parser_tensor_assign """ import pytest import numpy as np import mindspore as ms