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.5 kB

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