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 6.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. from mindspore.ops.operations import _inner_ops as inner
  25. context.set_context(device_target='GPU')
  26. class TensroAdd(nn.Cell):
  27. def __init__(self):
  28. super(TensroAdd, self).__init__()
  29. self.add = P.TensorAdd()
  30. self.x = Parameter(initializer(
  31. Tensor(np.random.randn(2, 0).astype(np.float32)), [2, 0]), name='x')
  32. self.y = Parameter(initializer(
  33. Tensor(np.random.randn(2, 1).astype(np.float32)), [2, 1]), name='y')
  34. self.x1 = Parameter(initializer(
  35. Tensor(np.arange(3).reshape(3).astype(np.float32)), [3]), name='x1')
  36. self.y1 = Parameter(initializer(
  37. Tensor(np.array([2]).astype(np.float32)), [1]), name='y1')
  38. self.x2 = Parameter(initializer(
  39. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32)), [3, 3, 3, 3]), name='x2')
  40. self.y2 = Parameter(initializer(
  41. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32)), [3, 3, 3, 3]), name='y2')
  42. self.x3 = Parameter(initializer(
  43. Tensor(np.arange(1 * 1 * 3 * 3).reshape(1, 1, 3, 3).astype(np.float32)), [1, 1, 3, 3]), name='x3')
  44. self.y3 = Parameter(initializer(
  45. Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32)), [3, 3, 3, 3]), name='y3')
  46. @ms_function
  47. def construct(self):
  48. return (
  49. self.add(self.x, self.y), self.add(self.x1, self.y1), self.add(self.x2, self.y2),
  50. self.add(self.x3, self.y3))
  51. @pytest.mark.level0
  52. @pytest.mark.platform_x86_gpu_training
  53. @pytest.mark.env_onecard
  54. def test_TensorAdd():
  55. add = TensroAdd()
  56. output = add()
  57. expect0 = np.array([])
  58. expect1 = np.array([2, 3, 4])
  59. expect2 = np.array(
  60. [[[[0., 2., 4.],
  61. [6., 8., 10.],
  62. [12., 14., 16.]],
  63. [[18., 20., 22.],
  64. [24., 26., 28.],
  65. [30., 32., 34.]],
  66. [[36., 38., 40.],
  67. [42., 44., 46.],
  68. [48., 50., 52.]]],
  69. [[[54., 56., 58.],
  70. [60., 62., 64.],
  71. [66., 68., 70.]],
  72. [[72., 74., 76.],
  73. [78., 80., 82.],
  74. [84., 86., 88.]],
  75. [[90., 92., 94.],
  76. [96., 98., 100.],
  77. [102., 104., 106.]]],
  78. [[[108., 110., 112.],
  79. [114., 116., 118.],
  80. [120., 122., 124.]],
  81. [[126., 128., 130.],
  82. [132., 134., 136.],
  83. [138., 140., 142.]],
  84. [[144., 146., 148.],
  85. [150., 152., 154.],
  86. [156., 158., 160.]]]])
  87. expect3 = np.array(
  88. [[[[0., 2., 4.],
  89. [6., 8., 10.],
  90. [12., 14., 16.]],
  91. [[9., 11., 13.],
  92. [15., 17., 19.],
  93. [21., 23., 25.]],
  94. [[18., 20., 22.],
  95. [24., 26., 28.],
  96. [30., 32., 34.]]],
  97. [[[27., 29., 31.],
  98. [33., 35., 37.],
  99. [39., 41., 43.]],
  100. [[36., 38., 40.],
  101. [42., 44., 46.],
  102. [48., 50., 52.]],
  103. [[45., 47., 49.],
  104. [51., 53., 55.],
  105. [57., 59., 61.]]],
  106. [[[54., 56., 58.],
  107. [60., 62., 64.],
  108. [66., 68., 70.]],
  109. [[63., 65., 67.],
  110. [69., 71., 73.],
  111. [75., 77., 79.]],
  112. [[72., 74., 76.],
  113. [78., 80., 82.],
  114. [84., 86., 88.]]]]
  115. )
  116. assert (output[0].asnumpy() == expect0).all()
  117. assert (output[1].asnumpy() == expect1).all()
  118. assert (output[2].asnumpy() == expect2).all()
  119. assert (output[3].asnumpy() == expect3).all()
  120. class Tensoradd_d(nn.Cell):
  121. def __init__(self):
  122. super(Tensoradd_d, self).__init__()
  123. self.test_dynamic = inner.GpuConvertToDynamicShape()
  124. self.add = P.TensorAdd()
  125. def construct(self, x, y):
  126. x = self.test_dynamic(x)
  127. y = self.test_dynamic(y)
  128. return self.add(x, y)
  129. @pytest.mark.level0
  130. @pytest.mark.platform_x86_gpu_training
  131. @pytest.mark.env_onecard
  132. def test_TensorAdd_dynamic():
  133. context.set_context(device_target='GPU', mode=context.GRAPH_MODE)
  134. net = Tensoradd_d()
  135. x1 = Tensor(np.arange(3).reshape(3).astype(np.float32))
  136. y1 = Tensor(np.array([2]).astype(np.float32))
  137. x2 = Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32))
  138. y2 = Tensor(np.arange(3 * 3 * 3 * 3).reshape(3, 3, 3, 3).astype(np.float32))
  139. expect1 = np.array([2, 3, 4])
  140. expect2 = np.array(
  141. [[[[0., 2., 4.],
  142. [6., 8., 10.],
  143. [12., 14., 16.]],
  144. [[18., 20., 22.],
  145. [24., 26., 28.],
  146. [30., 32., 34.]],
  147. [[36., 38., 40.],
  148. [42., 44., 46.],
  149. [48., 50., 52.]]],
  150. [[[54., 56., 58.],
  151. [60., 62., 64.],
  152. [66., 68., 70.]],
  153. [[72., 74., 76.],
  154. [78., 80., 82.],
  155. [84., 86., 88.]],
  156. [[90., 92., 94.],
  157. [96., 98., 100.],
  158. [102., 104., 106.]]],
  159. [[[108., 110., 112.],
  160. [114., 116., 118.],
  161. [120., 122., 124.]],
  162. [[126., 128., 130.],
  163. [132., 134., 136.],
  164. [138., 140., 142.]],
  165. [[144., 146., 148.],
  166. [150., 152., 154.],
  167. [156., 158., 160.]]]])
  168. output1 = net(x1, y1)
  169. output2 = net(x2, y2)
  170. assert (output1.asnumpy() == expect1).all()
  171. assert (output2.asnumpy() == expect2).all()