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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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.parameter import ParameterTuple
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from mindspore.ops.composite import GradOperation
  24. from mindspore.ops.operations import _inner_ops as inner
  25. class BiasAdd(nn.Cell):
  26. def __init__(self):
  27. super(BiasAdd, self).__init__()
  28. self.ba = P.BiasAdd()
  29. def construct(self, x, b):
  30. return self.ba(x, b)
  31. @pytest.mark.level0
  32. @pytest.mark.platform_x86_gpu_training
  33. @pytest.mark.env_onecard
  34. def test_biasadd():
  35. x = Tensor(np.array([[0.1, 0.2, 0.3, 0.4],
  36. [0.5, 0.6, 0.7, 0.8],
  37. [0.9, 1.0, 1.1, 1.2]]).astype(np.float32))
  38. b = Tensor(np.array([0.1, 0.2, 0.3, 0.4]).astype(np.float32))
  39. expect = np.array([[0.2, 0.4, 0.6, 0.8],
  40. [0.6, 0.8, 1.0, 1.2],
  41. [1.0, 1.2, 1.4, 1.6]])
  42. error = np.ones(shape=[3, 4]) * 1.0e-6
  43. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  44. ba = BiasAdd()
  45. result = ba(x, b)
  46. diff = result.asnumpy() - expect
  47. assert np.all(diff < error)
  48. assert np.all(-diff < error)
  49. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  50. ba = BiasAdd()
  51. result = ba(x, b)
  52. diff = result.asnumpy() - expect
  53. assert np.all(diff < error)
  54. assert np.all(-diff < error)
  55. class GradData(nn.Cell):
  56. def __init__(self, network):
  57. super(GradData, self).__init__()
  58. self.grad = GradOperation(get_all=True, sens_param=True)
  59. self.network = network
  60. def construct(self, inputs, output_grad):
  61. return self.grad(self.network)(inputs, output_grad)
  62. class GradWeight(nn.Cell):
  63. def __init__(self, network):
  64. super(GradWeight, self).__init__()
  65. self.network = network
  66. self.weights = ParameterTuple(network.trainable_params())
  67. self.grad = C.GradOperation(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_dx_ND():
  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]],
  115. [[0.1, 0.2, 0.3, 0.4],
  116. [0.1, 0.2, 0.3, 0.4],
  117. [0.1, 0.2, 0.3, 0.4]]
  118. ]).astype(np.float32)
  119. dy = np.array([[[1, 1],
  120. [1, 1],
  121. [1, 1]],
  122. [[1, 1],
  123. [1, 1],
  124. [1, 1]]]).astype(np.float32)
  125. dx_expect = np.array([[[1.1, 1.8, 1.1, 1.1],
  126. [1.1, 1.8, 1.1, 1.1],
  127. [1.1, 1.8, 1.1, 1.1]],
  128. [[1.1, 1.8, 1.1, 1.1],
  129. [1.1, 1.8, 1.1, 1.1],
  130. [1.1, 1.8, 1.1, 1.1]]
  131. ]).astype(np.float32)
  132. error = np.ones(shape=[2, 3, 4]) * 1.0e-6
  133. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  134. net = GradData(DenseNet())
  135. dx = net(Tensor(x), Tensor(dy))
  136. diff = dx[0].asnumpy() - dx_expect
  137. assert np.all(diff < error)
  138. assert np.all(-diff < error)
  139. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  140. net = GradData(DenseNet())
  141. dx = net(Tensor(x), Tensor(dy))
  142. diff = dx[0].asnumpy() - dx_expect
  143. assert np.all(diff < error)
  144. assert np.all(-diff < error)
  145. @pytest.mark.level0
  146. @pytest.mark.platform_x86_gpu_training
  147. @pytest.mark.env_onecard
  148. def test_dw():
  149. x = np.array([[0.1, 0.2, 0.3, 0.4],
  150. [0.1, 0.2, 0.3, 0.4],
  151. [0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  152. dy = np.array([[1, 1],
  153. [1, 1],
  154. [1, 1]]).astype(np.float32)
  155. dw_expect = np.array([[0.3, 0.6, 0.9, 1.2],
  156. [0.3, 0.6, 0.9, 1.2]]).astype(np.float32)
  157. dw_error = np.ones(shape=[2, 4]) * 1.0e-6
  158. db_expect = np.array([3, 3]).astype(np.float32)
  159. db_error = np.ones(shape=[2]) * 1.0e-6
  160. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  161. net = GradWeight(DenseNet())
  162. dw, db = net(Tensor(x), Tensor(dy))
  163. diff = dw.asnumpy() - dw_expect
  164. assert np.all(diff < dw_error)
  165. assert np.all(-diff < dw_error)
  166. diff = db.asnumpy() - db_expect
  167. assert np.all(diff < db_error)
  168. assert np.all(-diff < db_error)
  169. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  170. net = GradWeight(DenseNet())
  171. dw, db = net(Tensor(x), Tensor(dy))
  172. diff = dw.asnumpy() - dw_expect
  173. assert np.all(diff < dw_error)
  174. assert np.all(-diff < dw_error)
  175. diff = db.asnumpy() - db_expect
  176. assert np.all(diff < db_error)
  177. assert np.all(-diff < db_error)
  178. @pytest.mark.level0
  179. @pytest.mark.platform_x86_gpu_training
  180. @pytest.mark.env_onecard
  181. def test_dw_ND():
  182. x = np.array([[[0.1, 0.2, 0.3, 0.4],
  183. [0.1, 0.2, 0.3, 0.4],
  184. [0.1, 0.2, 0.3, 0.4]],
  185. [[0.1, 0.2, 0.3, 0.4],
  186. [0.1, 0.2, 0.3, 0.4],
  187. [0.1, 0.2, 0.3, 0.4]]]).astype(np.float32)
  188. dy = np.array([[[1, 1],
  189. [1, 1],
  190. [1, 1]],
  191. [[1, 1],
  192. [1, 1],
  193. [1, 1]]]).astype(np.float32)
  194. dw_expect = 2 * np.array([[0.3, 0.6, 0.9, 1.2],
  195. [0.3, 0.6, 0.9, 1.2]]).astype(np.float32)
  196. dw_error = np.ones(shape=[2, 4]) * 1.0e-6
  197. db_expect = 2 * np.array([3, 3]).astype(np.float32)
  198. db_error = np.ones(shape=[2]) * 1.0e-6
  199. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  200. net = GradWeight(DenseNet())
  201. dw, db = net(Tensor(x), Tensor(dy))
  202. diff = dw.asnumpy() - dw_expect
  203. assert np.all(diff < dw_error)
  204. assert np.all(-diff < dw_error)
  205. diff = db.asnumpy() - db_expect
  206. assert np.all(diff < db_error)
  207. assert np.all(-diff < db_error)
  208. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  209. net = GradWeight(DenseNet())
  210. dw, db = net(Tensor(x), Tensor(dy))
  211. diff = dw.asnumpy() - dw_expect
  212. assert np.all(diff < dw_error)
  213. assert np.all(-diff < dw_error)
  214. diff = db.asnumpy() - db_expect
  215. assert np.all(diff < db_error)
  216. assert np.all(-diff < db_error)
  217. class Grad(nn.Cell):
  218. def __init__(self, network):
  219. super(Grad, self).__init__()
  220. self.grad = GradOperation(get_all=True, sens_param=True)
  221. self.network = network
  222. def construct(self, input_, bias, dy):
  223. return self.grad(self.network)(input_, bias, dy)
  224. @pytest.mark.level0
  225. @pytest.mark.platform_x86_gpu_training
  226. @pytest.mark.env_onecard
  227. def test_biasadd_3d():
  228. x = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  229. [9, 10, 11, 12, 13, 14, 15, 16],
  230. [17, 18, 19, 20, 21, 22, 23, 24],
  231. [25, 26, 27, 28, 29, 30, 31, 32]],
  232. [[33, 34, 35, 36, 37, 38, 39, 40],
  233. [41, 42, 43, 44, 45, 46, 47, 48],
  234. [49, 50, 51, 52, 53, 54, 55, 56],
  235. [57, 58, 59, 60, 61, 62, 63, 64]],
  236. [[65, 66, 67, 68, 69, 70, 71, 72],
  237. [73, 74, 75, 76, 77, 78, 79, 80],
  238. [81, 82, 83, 84, 85, 86, 87, 88],
  239. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  240. b = Tensor(np.array([1, 2, 3, 4]).astype(np.float32))
  241. dy = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  242. [9, 10, 11, 12, 13, 14, 15, 16],
  243. [17, 18, 19, 20, 21, 22, 23, 24],
  244. [25, 26, 27, 28, 29, 30, 31, 32]],
  245. [[33, 34, 35, 36, 37, 38, 39, 40],
  246. [41, 42, 43, 44, 45, 46, 47, 48],
  247. [49, 50, 51, 52, 53, 54, 55, 56],
  248. [57, 58, 59, 60, 61, 62, 63, 64]],
  249. [[65, 66, 67, 68, 69, 70, 71, 72],
  250. [73, 74, 75, 76, 77, 78, 79, 80],
  251. [81, 82, 83, 84, 85, 86, 87, 88],
  252. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  253. expect = np.array([[[2, 3, 4, 5, 6, 7, 8, 9],
  254. [11, 12, 13, 14, 15, 16, 17, 18],
  255. [20, 21, 22, 23, 24, 25, 26, 27],
  256. [29, 30, 31, 32, 33, 34, 35, 36]],
  257. [[34, 35, 36, 37, 38, 39, 40, 41],
  258. [43, 44, 45, 46, 47, 48, 49, 50],
  259. [52, 53, 54, 55, 56, 57, 58, 59],
  260. [61, 62, 63, 64, 65, 66, 67, 68]],
  261. [[66, 67, 68, 69, 70, 71, 72, 73],
  262. [75, 76, 77, 78, 79, 80, 81, 82],
  263. [84, 85, 86, 87, 88, 89, 90, 91],
  264. [93, 94, 95, 96, 97, 98, 99, 100]]])
  265. error = np.ones(shape=[3, 4, 8]) * 1.0e-6
  266. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  267. net = BiasAdd()
  268. net.set_grad()
  269. result = net(x, b)
  270. diff = result.asnumpy() - expect
  271. assert np.all(diff < error)
  272. assert np.all(-diff < error)
  273. net = Grad(net)
  274. _, result = net(x, b, dy)
  275. expect = np.array([876., 1068., 1260., 1452.])
  276. diff = result.asnumpy() - expect
  277. error = np.ones(shape=[4]) * 1.0e-6
  278. assert np.all(diff < error)
  279. assert np.all(-diff < error)
  280. @pytest.mark.level0
  281. @pytest.mark.platform_x86_gpu_training
  282. @pytest.mark.env_onecard
  283. def test_biasadd_4d():
  284. x = Tensor(np.array([[[[1, 2, 3, 4],
  285. [5, 6, 7, 8],
  286. [9, 10, 11, 12],
  287. [13, 14, 15, 16]],
  288. [[17, 18, 19, 20],
  289. [21, 22, 23, 24],
  290. [25, 26, 27, 28],
  291. [29, 30, 31, 32]],
  292. [[33, 34, 35, 36],
  293. [37, 38, 39, 40],
  294. [41, 42, 43, 44],
  295. [45, 46, 47, 48]]],
  296. [[[49, 50, 51, 52],
  297. [53, 54, 55, 56],
  298. [57, 58, 59, 60],
  299. [61, 62, 63, 64]],
  300. [[65, 66, 67, 68],
  301. [69, 70, 71, 72],
  302. [73, 74, 75, 76],
  303. [77, 78, 79, 80]],
  304. [[81, 82, 83, 84],
  305. [85, 86, 87, 88],
  306. [89, 90, 91, 92],
  307. [93, 94, 95, 96]]]]).astype(np.float32))
  308. b = Tensor(np.array([1, 2, 3]).astype(np.float32))
  309. dy = Tensor(np.array([[[[1, 2, 3, 4],
  310. [5, 6, 7, 8],
  311. [9, 10, 11, 12],
  312. [13, 14, 15, 16]],
  313. [[17, 18, 19, 20],
  314. [21, 22, 23, 24],
  315. [25, 26, 27, 28],
  316. [29, 30, 31, 32]],
  317. [[33, 34, 35, 36],
  318. [37, 38, 39, 40],
  319. [41, 42, 43, 44],
  320. [45, 46, 47, 48]]],
  321. [[[49, 50, 51, 52],
  322. [53, 54, 55, 56],
  323. [57, 58, 59, 60],
  324. [61, 62, 63, 64]],
  325. [[65, 66, 67, 68],
  326. [69, 70, 71, 72],
  327. [73, 74, 75, 76],
  328. [77, 78, 79, 80]],
  329. [[81, 82, 83, 84],
  330. [85, 86, 87, 88],
  331. [89, 90, 91, 92],
  332. [93, 94, 95, 96]]]]).astype(np.float32))
  333. expect = np.array([[[[2, 3, 4, 5],
  334. [6, 7, 8, 9],
  335. [10, 11, 12, 13],
  336. [14, 15, 16, 17]],
  337. [[19, 20, 21, 22],
  338. [23, 24, 25, 26],
  339. [27, 28, 29, 30],
  340. [31, 32, 33, 34]],
  341. [[36, 37, 38, 39],
  342. [40, 41, 42, 43],
  343. [44, 45, 46, 47],
  344. [48, 49, 50, 51]]],
  345. [[[50, 51, 52, 53],
  346. [54, 55, 56, 57],
  347. [58, 59, 60, 61],
  348. [62, 63, 64, 65]],
  349. [[67, 68, 69, 70],
  350. [71, 72, 73, 74],
  351. [75, 76, 77, 78],
  352. [79, 80, 81, 82]],
  353. [[84, 85, 86, 87],
  354. [88, 89, 90, 91],
  355. [92, 93, 94, 95],
  356. [96, 97, 98, 99]]]])
  357. error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-6
  358. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  359. ba = BiasAdd()
  360. result = ba(x, b)
  361. diff = result.asnumpy() - expect
  362. assert np.all(diff < error)
  363. assert np.all(-diff < error)
  364. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  365. net = BiasAdd()
  366. result = net(x, b)
  367. diff = result.asnumpy() - expect
  368. assert np.all(diff < error)
  369. assert np.all(-diff < error)
  370. net = Grad(net)
  371. _, result = net(x, b, dy)
  372. expect = np.array([1040., 1552., 2064.])
  373. diff = result.asnumpy() - expect
  374. error = np.ones(shape=[3]) * 1.0e-6
  375. assert np.all(diff < error)
  376. assert np.all(-diff < error)
  377. class BiasAddDynamic(nn.Cell):
  378. def __init__(self):
  379. super(BiasAddDynamic, self).__init__()
  380. self.ba = P.BiasAdd()
  381. self.test_dynamic = inner.GpuConvertToDynamicShape()
  382. def construct(self, x, b):
  383. x = self.test_dynamic(x)
  384. output = self.ba(x, b)
  385. return output
  386. @pytest.mark.level0
  387. @pytest.mark.platform_x86_gpu_training
  388. @pytest.mark.env_onecard
  389. def test_bias_add_dynamic_two_inputs():
  390. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  391. net = BiasAddDynamic()
  392. x_1 = Tensor(np.array([[0.1, 0.2, 0.3, 0.4],
  393. [0.5, 0.6, 0.7, 0.8],
  394. [0.9, 1.0, 1.1, 1.2]]).astype(np.float32))
  395. b_1 = Tensor(np.array([0.1, 0.2, 0.3, 0.4]).astype(np.float32))
  396. expect_1 = np.array([[0.2, 0.4, 0.6, 0.8],
  397. [0.6, 0.8, 1.0, 1.2],
  398. [1.0, 1.2, 1.4, 1.6]])
  399. error_1 = np.ones(shape=[3, 4]) * 1.0e-6
  400. result_1 = net(x_1, b_1)
  401. diff_1 = result_1.asnumpy() - expect_1
  402. assert np.all(diff_1 < error_1)
  403. assert np.all(-diff_1 < error_1)
  404. x_2 = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  405. [9, 10, 11, 12, 13, 14, 15, 16],
  406. [17, 18, 19, 20, 21, 22, 23, 24],
  407. [25, 26, 27, 28, 29, 30, 31, 32]],
  408. [[33, 34, 35, 36, 37, 38, 39, 40],
  409. [41, 42, 43, 44, 45, 46, 47, 48],
  410. [49, 50, 51, 52, 53, 54, 55, 56],
  411. [57, 58, 59, 60, 61, 62, 63, 64]],
  412. [[65, 66, 67, 68, 69, 70, 71, 72],
  413. [73, 74, 75, 76, 77, 78, 79, 80],
  414. [81, 82, 83, 84, 85, 86, 87, 88],
  415. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  416. b_2 = Tensor(np.array([1, 2, 3, 4]).astype(np.float32))
  417. expect_2 = np.array([[[2, 3, 4, 5, 6, 7, 8, 9],
  418. [11, 12, 13, 14, 15, 16, 17, 18],
  419. [20, 21, 22, 23, 24, 25, 26, 27],
  420. [29, 30, 31, 32, 33, 34, 35, 36]],
  421. [[34, 35, 36, 37, 38, 39, 40, 41],
  422. [43, 44, 45, 46, 47, 48, 49, 50],
  423. [52, 53, 54, 55, 56, 57, 58, 59],
  424. [61, 62, 63, 64, 65, 66, 67, 68]],
  425. [[66, 67, 68, 69, 70, 71, 72, 73],
  426. [75, 76, 77, 78, 79, 80, 81, 82],
  427. [84, 85, 86, 87, 88, 89, 90, 91],
  428. [93, 94, 95, 96, 97, 98, 99, 100]]])
  429. error_2 = np.ones(shape=[3, 4, 8]) * 1.0e-6
  430. result_2 = net(x_2, b_2)
  431. diff_2 = result_2.asnumpy() - expect_2
  432. assert np.all(diff_2 < error_2)
  433. assert np.all(-diff_2 < error_2)