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_dense_op.py 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 pytest
  16. import numpy as np
  17. from mindspore import Tensor
  18. from mindspore.ops import operations as P
  19. import mindspore.nn as nn
  20. import mindspore.context as context
  21. from mindspore.ops.composite import GradOperation
  22. from mindspore.common.parameter import ParameterTuple
  23. from mindspore.ops import composite as C
  24. class BiasAdd(nn.Cell):
  25. def __init__(self):
  26. super(BiasAdd, self).__init__()
  27. self.ba = P.BiasAdd()
  28. def construct(self, x, b):
  29. return self.ba(x, b)
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_gpu_training
  32. @pytest.mark.env_onecard
  33. def test_biasadd():
  34. x = Tensor(np.array([[0.1, 0.2, 0.3, 0.4],
  35. [0.5, 0.6, 0.7, 0.8],
  36. [0.9, 1.0, 1.1, 1.2]]).astype(np.float32))
  37. b = Tensor(np.array([0.1, 0.2, 0.3, 0.4]).astype(np.float32))
  38. expect = np.array([[0.2, 0.4, 0.6, 0.8],
  39. [0.6, 0.8, 1.0, 1.2],
  40. [1.0, 1.2, 1.4, 1.6]])
  41. error = np.ones(shape=[3, 4]) * 1.0e-6
  42. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  43. ba = BiasAdd()
  44. result = ba(x, b)
  45. diff = result.asnumpy() - expect
  46. assert np.all(diff < error)
  47. assert np.all(-diff < error)
  48. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  49. ba = BiasAdd()
  50. result = ba(x, b)
  51. diff = result.asnumpy() - expect
  52. assert np.all(diff < error)
  53. assert np.all(-diff < error)
  54. class GradData(nn.Cell):
  55. def __init__(self, network):
  56. super(GradData, self).__init__()
  57. self.grad = GradOperation(name="get_all", get_all=True, sens_param=True)
  58. self.network = network
  59. def construct(self, input, output_grad):
  60. return self.grad(self.network)(input, output_grad)
  61. class GradWeight(nn.Cell):
  62. def __init__(self, network):
  63. super(GradWeight, self).__init__()
  64. self.network = network
  65. self.weights = ParameterTuple(network.trainable_params())
  66. self.grad = C.GradOperation('grad',
  67. get_by_list=True,
  68. sens_param=True)
  69. def construct(self, x, output_grad):
  70. weights = self.weights
  71. grads = self.grad(self.network, weights)(x, output_grad)
  72. return grads
  73. class DenseNet(nn.Cell):
  74. def __init__(self):
  75. super(DenseNet, self).__init__()
  76. w = np.array([[0.1, 0.8, 0.1, 0.1],
  77. [1, 1, 1, 1]]).astype(np.float32)
  78. b = np.array([0.3, 0.6]).astype(np.float32)
  79. self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b))
  80. def construct(self, x):
  81. return self.dense(x)
  82. @pytest.mark.level0
  83. @pytest.mark.platform_x86_gpu_training
  84. @pytest.mark.env_onecard
  85. def test_dx():
  86. x = np.array([[0.1, 0.2, 0.3, 0.4],
  87. [0.1, 0.2, 0.3, 0.4],
  88. [0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  89. dy = np.array([[1, 1],
  90. [1, 1],
  91. [1, 1]]).astype(np.float32)
  92. dx_expect = np.array([[1.1, 1.8, 1.1, 1.1],
  93. [1.1, 1.8, 1.1, 1.1],
  94. [1.1, 1.8, 1.1, 1.1]]).astype(np.float32)
  95. error = np.ones(shape=[3, 4]) * 1.0e-6
  96. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  97. net = GradData(DenseNet())
  98. dx = net(Tensor(x), Tensor(dy))
  99. diff = dx[0].asnumpy() - dx_expect
  100. assert np.all(diff < error)
  101. assert np.all(-diff < error)
  102. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  103. net = GradData(DenseNet())
  104. dx = net(Tensor(x), Tensor(dy))
  105. diff = dx[0].asnumpy() - dx_expect
  106. assert np.all(diff < error)
  107. assert np.all(-diff < error)
  108. @pytest.mark.level0
  109. @pytest.mark.platform_x86_gpu_training
  110. @pytest.mark.env_onecard
  111. def test_dw():
  112. x = np.array([[0.1, 0.2, 0.3, 0.4],
  113. [0.1, 0.2, 0.3, 0.4],
  114. [0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  115. dy = np.array([[1, 1],
  116. [1, 1],
  117. [1, 1]]).astype(np.float32)
  118. dw_expect = np.array([[0.3, 0.6, 0.9, 1.2],
  119. [0.3, 0.6, 0.9, 1.2]]).astype(np.float32)
  120. dw_error = np.ones(shape=[2, 4]) * 1.0e-6
  121. db_expect = np.array([3, 3]).astype(np.float32)
  122. db_error = np.ones(shape=[2]) * 1.0e-6
  123. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  124. net = GradWeight(DenseNet())
  125. dw, db = net(Tensor(x), Tensor(dy))
  126. diff = dw.asnumpy() - dw_expect
  127. assert np.all(diff < dw_error)
  128. assert np.all(-diff < dw_error)
  129. diff = db.asnumpy() - db_expect
  130. assert np.all(diff < db_error)
  131. assert np.all(-diff < db_error)
  132. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  133. net = GradWeight(DenseNet())
  134. dw, db = net(Tensor(x), Tensor(dy))
  135. diff = dw.asnumpy() - dw_expect
  136. assert np.all(diff < dw_error)
  137. assert np.all(-diff < dw_error)
  138. diff = db.asnumpy() - db_expect
  139. assert np.all(diff < db_error)
  140. assert np.all(-diff < db_error)
  141. class Grad(nn.Cell):
  142. def __init__(self, network):
  143. super(Grad, self).__init__()
  144. self.grad = GradOperation(name="get_all", get_all=True, sens_param=True)
  145. self.network = network
  146. def construct(self, input, bias, dy):
  147. return self.grad(self.network)(input, bias, dy)
  148. @pytest.mark.level0
  149. @pytest.mark.platform_x86_gpu_training
  150. @pytest.mark.env_onecard
  151. def test_biasadd_3d():
  152. x = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  153. [9, 10, 11, 12, 13, 14, 15, 16],
  154. [17, 18, 19, 20, 21, 22, 23, 24],
  155. [25, 26, 27, 28, 29, 30, 31, 32]],
  156. [[33, 34, 35, 36, 37, 38, 39, 40],
  157. [41, 42, 43, 44, 45, 46, 47, 48],
  158. [49, 50, 51, 52, 53, 54, 55, 56],
  159. [57, 58, 59, 60, 61, 62, 63, 64]],
  160. [[65, 66, 67, 68, 69, 70, 71, 72],
  161. [73, 74, 75, 76, 77, 78, 79, 80],
  162. [81, 82, 83, 84, 85, 86, 87, 88],
  163. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  164. b = Tensor(np.array([1, 2, 3, 4]).astype(np.float32))
  165. dy = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  166. [9, 10, 11, 12, 13, 14, 15, 16],
  167. [17, 18, 19, 20, 21, 22, 23, 24],
  168. [25, 26, 27, 28, 29, 30, 31, 32]],
  169. [[33, 34, 35, 36, 37, 38, 39, 40],
  170. [41, 42, 43, 44, 45, 46, 47, 48],
  171. [49, 50, 51, 52, 53, 54, 55, 56],
  172. [57, 58, 59, 60, 61, 62, 63, 64]],
  173. [[65, 66, 67, 68, 69, 70, 71, 72],
  174. [73, 74, 75, 76, 77, 78, 79, 80],
  175. [81, 82, 83, 84, 85, 86, 87, 88],
  176. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  177. expect = np.array([[[2, 3, 4, 5, 6, 7, 8, 9],
  178. [11, 12, 13, 14, 15, 16, 17, 18],
  179. [20, 21, 22, 23, 24, 25, 26, 27],
  180. [29, 30, 31, 32, 33, 34, 35, 36]],
  181. [[34, 35, 36, 37, 38, 39, 40, 41],
  182. [43, 44, 45, 46, 47, 48, 49, 50],
  183. [52, 53, 54, 55, 56, 57, 58, 59],
  184. [61, 62, 63, 64, 65, 66, 67, 68]],
  185. [[66, 67, 68, 69, 70, 71, 72, 73],
  186. [75, 76, 77, 78, 79, 80, 81, 82],
  187. [84, 85, 86, 87, 88, 89, 90, 91],
  188. [93, 94, 95, 96, 97, 98, 99, 100]]])
  189. error = np.ones(shape=[3, 4, 8]) * 1.0e-6
  190. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  191. net = BiasAdd()
  192. result = net(x, b)
  193. diff = result.asnumpy() - expect
  194. assert np.all(diff < error)
  195. assert np.all(-diff < error)
  196. net = Grad(net)
  197. _, result = net(x, b, dy)
  198. expect = np.array([876., 1068., 1260., 1452.])
  199. diff = result.asnumpy() - expect
  200. error = np.ones(shape=[4]) * 1.0e-6
  201. assert np.all(diff < error)
  202. assert np.all(-diff < error)
  203. @pytest.mark.level0
  204. @pytest.mark.platform_x86_gpu_training
  205. @pytest.mark.env_onecard
  206. def test_biasadd_4d():
  207. x = Tensor(np.array([[[[1, 2, 3, 4],
  208. [5, 6, 7, 8],
  209. [9, 10, 11, 12],
  210. [13, 14, 15, 16]],
  211. [[17, 18, 19, 20],
  212. [21, 22, 23, 24],
  213. [25, 26, 27, 28],
  214. [29, 30, 31, 32]],
  215. [[33, 34, 35, 36],
  216. [37, 38, 39, 40],
  217. [41, 42, 43, 44],
  218. [45, 46, 47, 48]]],
  219. [[[49, 50, 51, 52],
  220. [53, 54, 55, 56],
  221. [57, 58, 59, 60],
  222. [61, 62, 63, 64]],
  223. [[65, 66, 67, 68],
  224. [69, 70, 71, 72],
  225. [73, 74, 75, 76],
  226. [77, 78, 79, 80]],
  227. [[81, 82, 83, 84],
  228. [85, 86, 87, 88],
  229. [89, 90, 91, 92],
  230. [93, 94, 95, 96]]]]).astype(np.float32))
  231. b = Tensor(np.array([1, 2, 3]).astype(np.float32))
  232. dy = Tensor(np.array([[[[1, 2, 3, 4],
  233. [5, 6, 7, 8],
  234. [9, 10, 11, 12],
  235. [13, 14, 15, 16]],
  236. [[17, 18, 19, 20],
  237. [21, 22, 23, 24],
  238. [25, 26, 27, 28],
  239. [29, 30, 31, 32]],
  240. [[33, 34, 35, 36],
  241. [37, 38, 39, 40],
  242. [41, 42, 43, 44],
  243. [45, 46, 47, 48]]],
  244. [[[49, 50, 51, 52],
  245. [53, 54, 55, 56],
  246. [57, 58, 59, 60],
  247. [61, 62, 63, 64]],
  248. [[65, 66, 67, 68],
  249. [69, 70, 71, 72],
  250. [73, 74, 75, 76],
  251. [77, 78, 79, 80]],
  252. [[81, 82, 83, 84],
  253. [85, 86, 87, 88],
  254. [89, 90, 91, 92],
  255. [93, 94, 95, 96]]]]).astype(np.float32))
  256. expect = np.array([[[[2, 3, 4, 5],
  257. [6, 7, 8, 9],
  258. [10, 11, 12, 13],
  259. [14, 15, 16, 17]],
  260. [[19, 20, 21, 22],
  261. [23, 24, 25, 26],
  262. [27, 28, 29, 30],
  263. [31, 32, 33, 34]],
  264. [[36, 37, 38, 39],
  265. [40, 41, 42, 43],
  266. [44, 45, 46, 47],
  267. [48, 49, 50, 51]]],
  268. [[[50, 51, 52, 53],
  269. [54, 55, 56, 57],
  270. [58, 59, 60, 61],
  271. [62, 63, 64, 65]],
  272. [[67, 68, 69, 70],
  273. [71, 72, 73, 74],
  274. [75, 76, 77, 78],
  275. [79, 80, 81, 82]],
  276. [[84, 85, 86, 87],
  277. [88, 89, 90, 91],
  278. [92, 93, 94, 95],
  279. [96, 97, 98, 99]]]])
  280. error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-6
  281. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  282. ba = BiasAdd()
  283. result = ba(x, b)
  284. diff = result.asnumpy() - expect
  285. assert np.all(diff < error)
  286. assert np.all(-diff < error)
  287. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  288. net = BiasAdd()
  289. result = net(x, b)
  290. diff = result.asnumpy() - expect
  291. assert np.all(diff < error)
  292. assert np.all(-diff < error)
  293. net = Grad(net)
  294. _, result = net(x, b, dy)
  295. expect = np.array([1040., 1552., 2064.])
  296. diff = result.asnumpy() - expect
  297. error = np.ones(shape=[3]) * 1.0e-6
  298. assert np.all(diff < error)
  299. assert np.all(-diff < error)