You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_zip_operation.py 3.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. import pytest
  16. import numpy as np
  17. from mindspore import Tensor, Parameter
  18. from mindspore.ops import operations as P
  19. from mindspore.nn import Cell
  20. import mindspore as ms
  21. def test_zip_operation_args_size():
  22. """
  23. Feature: Check the size of inputs of ZipOperation.
  24. Description: The inputs of ZipOperation must not be empty.
  25. Expectation: The size of inputs of ZipOperation must be greater than 0.
  26. """
  27. class AssignInZipLoop(Cell):
  28. def __init__(self):
  29. super().__init__()
  30. self.conv1 = ms.nn.Conv2d(3, 2, 1, weight_init="zero")
  31. self.conv2 = ms.nn.Conv2d(3, 2, 1, weight_init="zero")
  32. self.params1 = self.conv1.trainable_params()
  33. self.params2 = self.conv2.trainable_params()
  34. def construct(self, x):
  35. for p1, p2 in zip():
  36. P.Assign()(p2, p1 + x)
  37. out = 0
  38. for p1, p2 in zip(self.params1, self.params2):
  39. out = p1 + p2
  40. return out
  41. x = Tensor.from_numpy(np.ones([1], np.float32))
  42. net = AssignInZipLoop()
  43. with pytest.raises(Exception, match="The zip operator must have at least 1 argument"):
  44. out = net(x)
  45. assert np.all(out.asnumpy() == 1)
  46. def test_zip_operation_args_type():
  47. """
  48. Feature: Check the type of inputs of ZipOperation.
  49. Description: Check whether all inputs in zip is sequeue.
  50. Expectation: All inputs in zip must be sequeue.
  51. """
  52. class AssignInZipLoop(Cell):
  53. def __init__(self):
  54. super().__init__()
  55. self.conv1 = ms.nn.Conv2d(3, 2, 1, weight_init="zero")
  56. self.conv2 = ms.nn.Conv2d(3, 2, 1, weight_init="zero")
  57. self.params1 = self.conv1.trainable_params()
  58. self.params2 = self.conv2.trainable_params()
  59. self.param = Parameter(Tensor(5, ms.float32), name="param")
  60. def construct(self, x):
  61. for p1, p2 in zip(self.params1, self.params2, self.param):
  62. P.Assign()(p2, p1 + x)
  63. out = 0
  64. for p1, p2 in zip(self.params1, self.params2):
  65. out = p1 + p2
  66. return out
  67. x = Tensor.from_numpy(np.ones([1], np.float32))
  68. net = AssignInZipLoop()
  69. with pytest.raises(Exception, match="For 'zip', the all inputs must be list or tuple."):
  70. out = net(x)
  71. assert np.all(out.asnumpy() == 1)