|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- # 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.
- # ==============================================================================
- import pytest
- import mindspore as ms
- from mindspore.nn import Cell
- from mindspore.common.parameter import Parameter
- from mindspore.common import ParameterTuple
- from mindspore import Tensor, context
-
- context.set_context(mode=context.GRAPH_MODE)
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_1_1():
- """
- Feature: Check the names of parameters and the names of inputs of construct.
- Description: If the name of the input of construct is same as the parameters, add suffix to the name of the input.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_b = Parameter(Tensor([2], ms.float32), name="name_b")
-
- def construct(self, name_a):
- return self.param_a + self.param_b - name_a
-
- net = ParamNet()
- res = net(Tensor([3], ms.float32))
- assert res == 0
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_1_2():
- """
- Feature: Check the names of parameters and the names of inputs of construct.
- Description: If the name of the input of construct is same as the parameters, add suffix to the name of the input.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_b = ParameterTuple((Parameter(Tensor([2], ms.float32), name="name_b"), self.param_a))
-
- def construct(self, name_b):
- return self.param_a + self.param_b[0] - name_b
-
- net = ParamNet()
- res = net(Tensor([3], ms.float32))
- assert res == 0
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_2_1():
- """
- Feature: Check the names of parameters.
- Description: If parameters in init have same name, an exception will be thrown.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_b = Parameter(Tensor([2], ms.float32), name="name_a")
-
- def construct(self):
- return self.param_a + self.param_b
-
- with pytest.raises(ValueError, match="its name 'name_a' already exists."):
- net = ParamNet()
- res = net()
- assert res == 3
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_2_2():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32)), self.param_a))
- self.param_a = Parameter(Tensor([3], ms.float32), name="name_a")
- self.res2 = self.res1[0] + self.param_a
-
- def construct(self):
- return self.param_a + self.res1[0] + self.res2
-
- with pytest.raises(ValueError, match="its name 'name_a' already exists."):
- net = ParamNet()
- res = net()
- assert res == 10
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_3():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32))
- self.param_b = Parameter(Tensor([2], ms.float32))
-
- def construct(self):
- return self.param_a + self.param_b
-
- net = ParamNet()
- res = net()
- assert res == 3
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_4():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32), name="name_a"),
- Parameter(Tensor([4], ms.float32), name="name_a")))
-
- def construct(self):
- return self.res1[0] + self.res1[1]
-
- with pytest.raises(ValueError, match="its name 'name_a' already exists."):
- net = ParamNet()
- res = net()
- assert res == 6
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_5_1():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32)), Parameter(Tensor([4], ms.float32))))
-
- def construct(self):
- return self.res1[0] + self.res1[1]
-
- with pytest.raises(ValueError, match="its name 'Parameter' already exists."):
- net = ParamNet()
- res = net()
- assert res == 6
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_5_2():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.res1 = ParameterTuple((Parameter(Tensor([2], ms.float32)), self.param_a))
- self.param_a = Parameter(Tensor([3], ms.float32), name="name_b")
- self.res2 = self.res1[0] + self.param_a
-
- def construct(self):
- return self.param_a + self.res1[0] + self.res2
-
- net = ParamNet()
- res = net()
- assert res == 10
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_list_tuple_no_name():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_tuple = (Parameter(Tensor([5], ms.float32)), Parameter(Tensor([6], ms.float32)))
- self.param_list = [Parameter(Tensor([7], ms.float32)), Parameter(Tensor([8], ms.float32))]
-
- def construct(self):
- return self.param_tuple[0] + self.param_tuple[1] + self.param_list[0] + self.param_list[1]
-
- net = ParamNet()
- res = net()
- assert res == 26
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_in_tuple():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_b = Parameter(Tensor([2], ms.float32), name="name_b")
- self.param_tuple = ParameterTuple((self.param_a, self.param_b))
-
- def construct(self):
- return self.param_a + self.param_b + self.param_tuple[0] + self.param_tuple[1]
-
- net = ParamNet()
- res = net()
- assert res == 6
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_parameter_tuple_1():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_tuple = ParameterTuple((Parameter(Tensor([5], ms.float32), name="name_a"),
- Parameter(Tensor([5], ms.float32), name="name_b")))
-
- def construct(self):
- return self.param_a + self.param_tuple[0] + self.param_tuple[1]
-
- with pytest.raises(ValueError, match="its name 'name_a' already exists."):
- net = ParamNet()
- res = net()
- assert res == 11
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_ascend_training
- @pytest.mark.env_onecard
- def test_parameter_parameter_tuple_2():
- """
- Feature: Check the names of parameters.
- Description: Check the name of parameter in init.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_tuple = ParameterTuple((self.param_a, self.param_a, self.param_a))
-
- def construct(self):
- return self.param_a + self.param_tuple[0] + self.param_tuple[1] + self.param_tuple[2]
-
- net = ParamNet()
- res = net()
- assert res == 4
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_parameter():
- """
- Feature: Check the names of parameters.
- Description: If parameter in list or tuple is not given a name, will give it a unique name.
- Expectation: No exception.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_a = Parameter(Tensor([1], ms.float32), name="name_a")
- self.param_b = Parameter(Tensor([2], ms.float32), name="name_b")
- self.param_c = Parameter(Tensor([3], ms.float32))
- self.param_d = Parameter(Tensor([4], ms.float32))
- self.param_tuple = (Parameter(Tensor([5], ms.float32)),
- Parameter(Tensor([6], ms.float32)))
- self.param_list = [Parameter(Tensor([5], ms.float32)),
- Parameter(Tensor([6], ms.float32))]
-
- def construct(self, x):
- res1 = self.param_a + self.param_b + self.param_c + self.param_d
- res1 = res1 - self.param_list[0] + self.param_list[1] + x
- res2 = self.param_list[0] + self.param_list[1]
- return res1, res2
-
- net = ParamNet()
- x = Tensor([10], ms.float32)
- output1, output2 = net(x)
- output1_expect = Tensor(21, ms.float32)
- output2_expect = Tensor(11, ms.float32)
- assert output1 == output1_expect
- assert output2 == output2_expect
-
-
- @pytest.mark.level1
- @pytest.mark.platform_arm_ascend_training
- @pytest.mark.platform_x86_gpu_training
- @pytest.mark.env_onecard
- def test_parameter_same_name_between_tuple_or_list():
- """
- Feature: Check the names of parameters between tuple or list.
- Description: If the same name exists between tuple and list, an exception will be thrown.
- Expectation: Get the expected exception report.
- """
- class ParamNet(Cell):
- def __init__(self):
- super(ParamNet, self).__init__()
- self.param_tuple = (Parameter(Tensor([1], ms.float32), name="name_a"),
- Parameter(Tensor([2], ms.float32)))
- self.param_list = [Parameter(Tensor([3], ms.float32), name="name_a"),
- Parameter(Tensor([4], ms.float32))]
-
- def construct(self, x):
- res = self.param_tuple[0] + self.param_tuple[1] + self.param_list[0] + self.param_listp[1] + x
- return res
-
- with pytest.raises(ValueError, match="its name 'name_a' already exists."):
- net = ParamNet()
- x = Tensor([10], ms.float32)
- output = net(x)
- output_expect = Tensor(20, ms.float32)
- assert output == output_expect
|