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_tensoradd.py 4.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # Copyright 2019 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 numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.common.api import ms_function
  21. from mindspore.common.initializer import initializer
  22. from mindspore.common.parameter import Parameter
  23. from mindspore.ops import operations as P
  24. context.set_context(device_target='GPU')
  25. class TensroAdd(nn.Cell):
  26. def __init__(self):
  27. super(TensroAdd, self).__init__()
  28. self.add = P.TensorAdd()
  29. self.x1 = Parameter(initializer(
  30. Tensor(np.arange(3).reshape(3).astype(np.float32)), [3]), name='x1')
  31. self.y1 = Parameter(initializer(
  32. Tensor(np.array([2]).astype(np.float32)), [1]), name='y1')
  33. self.x2 = Parameter(initializer(
  34. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32)), [3, 3, 3, 3]), name='x2')
  35. self.y2 = Parameter(initializer(
  36. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32)), [3, 3, 3, 3]), name='y2')
  37. self.x3 = Parameter(initializer(
  38. Tensor(np.arange(1 * 1 * 3 * 3).reshape(1, 1, 3, 3).astype(np.float32)), [1, 1, 3, 3]), name='x3')
  39. self.y3 = Parameter(initializer(
  40. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32)), [3, 3, 3, 3]), name='y3')
  41. @ms_function
  42. def construct(self):
  43. return (self.add(self.x1, self.y1), self.add(self.x2, self.y2), self.add(self.x3, self.y3))
  44. @pytest.mark.level0
  45. @pytest.mark.platform_x86_gpu_training
  46. @pytest.mark.env_onecard
  47. def test_TensorAdd():
  48. add = TensroAdd()
  49. output = add()
  50. expect0 = np.array([2, 3, 4])
  51. expect1 = np.array(
  52. [[[[0., 2., 4.],
  53. [6., 8., 10.],
  54. [12., 14., 16.]],
  55. [[18., 20., 22.],
  56. [24., 26., 28.],
  57. [30., 32., 34.]],
  58. [[36., 38., 40.],
  59. [42., 44., 46.],
  60. [48., 50., 52.]]],
  61. [[[54., 56., 58.],
  62. [60., 62., 64.],
  63. [66., 68., 70.]],
  64. [[72., 74., 76.],
  65. [78., 80., 82.],
  66. [84., 86., 88.]],
  67. [[90., 92., 94.],
  68. [96., 98., 100.],
  69. [102., 104., 106.]]],
  70. [[[108., 110., 112.],
  71. [114., 116., 118.],
  72. [120., 122., 124.]],
  73. [[126., 128., 130.],
  74. [132., 134., 136.],
  75. [138., 140., 142.]],
  76. [[144., 146., 148.],
  77. [150., 152., 154.],
  78. [156., 158., 160.]]]])
  79. expect2 = np.array(
  80. [[[[0., 2., 4.],
  81. [6., 8., 10.],
  82. [12., 14., 16.]],
  83. [[9., 11., 13.],
  84. [15., 17., 19.],
  85. [21., 23., 25.]],
  86. [[18., 20., 22.],
  87. [24., 26., 28.],
  88. [30., 32., 34.]]],
  89. [[[27., 29., 31.],
  90. [33., 35., 37.],
  91. [39., 41., 43.]],
  92. [[36., 38., 40.],
  93. [42., 44., 46.],
  94. [48., 50., 52.]],
  95. [[45., 47., 49.],
  96. [51., 53., 55.],
  97. [57., 59., 61.]]],
  98. [[[54., 56., 58.],
  99. [60., 62., 64.],
  100. [66., 68., 70.]],
  101. [[63., 65., 67.],
  102. [69., 71., 73.],
  103. [75., 77., 79.]],
  104. [[72., 74., 76.],
  105. [78., 80., 82.],
  106. [84., 86., 88.]]]]
  107. )
  108. assert (output[0].asnumpy() == expect0).all()
  109. assert (output[1].asnumpy() == expect1).all()
  110. assert (output[2].asnumpy() == expect2).all()
  111. class TensorAdd2(nn.Cell):
  112. def __init__(self):
  113. super(TensorAdd2, self).__init__()
  114. self.add = P.TensorAdd()
  115. self.x = Parameter(initializer(
  116. Tensor(np.random.randn(2, 0).astype(np.float32)), [2, 0]), name='x')
  117. self.y = Parameter(initializer(
  118. Tensor(np.random.randn(2, 1).astype(np.float32)), [2, 1]), name='y')
  119. @ms_function
  120. def construct(self):
  121. return self.add(self.x, self.y)
  122. # Constructing a tensor with 0 in shape is not support, excluding empty tensor.
  123. @pytest.mark.skip(reason='0 in shape is not support')
  124. def test_TensorAdd_shape_has_zero():
  125. add = TensorAdd2()
  126. output = add()
  127. expect = np.array([])
  128. assert (output.asnumpy() == expect).all()