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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. 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(get_all=True, sens_param=True)
  58. self.network = network
  59. def construct(self, inputs, output_grad):
  60. return self.grad(self.network)(inputs, 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(get_by_list=True,
  67. sens_param=True)
  68. def construct(self, x, output_grad):
  69. weights = self.weights
  70. grads = self.grad(self.network, weights)(x, output_grad)
  71. return grads
  72. class DenseNet(nn.Cell):
  73. def __init__(self):
  74. super(DenseNet, self).__init__()
  75. w = np.array([[0.1, 0.8, 0.1, 0.1],
  76. [1, 1, 1, 1]]).astype(np.float32)
  77. b = np.array([0.3, 0.6]).astype(np.float32)
  78. self.dense = nn.Dense(4, 2, weight_init=Tensor(w), bias_init=Tensor(b))
  79. def construct(self, x):
  80. return self.dense(x)
  81. @pytest.mark.level0
  82. @pytest.mark.platform_x86_gpu_training
  83. @pytest.mark.env_onecard
  84. def test_dx():
  85. x = np.array([[0.1, 0.2, 0.3, 0.4],
  86. [0.1, 0.2, 0.3, 0.4],
  87. [0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  88. dy = np.array([[1, 1],
  89. [1, 1],
  90. [1, 1]]).astype(np.float32)
  91. dx_expect = np.array([[1.1, 1.8, 1.1, 1.1],
  92. [1.1, 1.8, 1.1, 1.1],
  93. [1.1, 1.8, 1.1, 1.1]]).astype(np.float32)
  94. error = np.ones(shape=[3, 4]) * 1.0e-6
  95. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  96. net = GradData(DenseNet())
  97. dx = net(Tensor(x), Tensor(dy))
  98. diff = dx[0].asnumpy() - dx_expect
  99. assert np.all(diff < error)
  100. assert np.all(-diff < error)
  101. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  102. net = GradData(DenseNet())
  103. dx = net(Tensor(x), Tensor(dy))
  104. diff = dx[0].asnumpy() - dx_expect
  105. assert np.all(diff < error)
  106. assert np.all(-diff < error)
  107. @pytest.mark.level0
  108. @pytest.mark.platform_x86_gpu_training
  109. @pytest.mark.env_onecard
  110. def test_dx_ND():
  111. x = np.array([[[0.1, 0.2, 0.3, 0.4],
  112. [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. ]).astype(np.float32)
  118. dy = np.array([[[1, 1],
  119. [1, 1],
  120. [1, 1]],
  121. [[1, 1],
  122. [1, 1],
  123. [1, 1]]]).astype(np.float32)
  124. dx_expect = np.array([[[1.1, 1.8, 1.1, 1.1],
  125. [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. ]).astype(np.float32)
  131. error = np.ones(shape=[2, 3, 4]) * 1.0e-6
  132. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  133. net = GradData(DenseNet())
  134. dx = net(Tensor(x), Tensor(dy))
  135. diff = dx[0].asnumpy() - dx_expect
  136. assert np.all(diff < error)
  137. assert np.all(-diff < error)
  138. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  139. net = GradData(DenseNet())
  140. dx = net(Tensor(x), Tensor(dy))
  141. diff = dx[0].asnumpy() - dx_expect
  142. assert np.all(diff < error)
  143. assert np.all(-diff < error)
  144. @pytest.mark.level0
  145. @pytest.mark.platform_x86_gpu_training
  146. @pytest.mark.env_onecard
  147. def test_dw():
  148. x = np.array([[0.1, 0.2, 0.3, 0.4],
  149. [0.1, 0.2, 0.3, 0.4],
  150. [0.1, 0.2, 0.3, 0.4]]).astype(np.float32)
  151. dy = np.array([[1, 1],
  152. [1, 1],
  153. [1, 1]]).astype(np.float32)
  154. dw_expect = np.array([[0.3, 0.6, 0.9, 1.2],
  155. [0.3, 0.6, 0.9, 1.2]]).astype(np.float32)
  156. dw_error = np.ones(shape=[2, 4]) * 1.0e-6
  157. db_expect = np.array([3, 3]).astype(np.float32)
  158. db_error = np.ones(shape=[2]) * 1.0e-6
  159. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  160. net = GradWeight(DenseNet())
  161. dw, db = net(Tensor(x), Tensor(dy))
  162. diff = dw.asnumpy() - dw_expect
  163. assert np.all(diff < dw_error)
  164. assert np.all(-diff < dw_error)
  165. diff = db.asnumpy() - db_expect
  166. assert np.all(diff < db_error)
  167. assert np.all(-diff < db_error)
  168. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  169. net = GradWeight(DenseNet())
  170. dw, db = net(Tensor(x), Tensor(dy))
  171. diff = dw.asnumpy() - dw_expect
  172. assert np.all(diff < dw_error)
  173. assert np.all(-diff < dw_error)
  174. diff = db.asnumpy() - db_expect
  175. assert np.all(diff < db_error)
  176. assert np.all(-diff < db_error)
  177. @pytest.mark.level0
  178. @pytest.mark.platform_x86_gpu_training
  179. @pytest.mark.env_onecard
  180. def test_dw_ND():
  181. x = np.array([[[0.1, 0.2, 0.3, 0.4],
  182. [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]]]).astype(np.float32)
  187. dy = np.array([[[1, 1],
  188. [1, 1],
  189. [1, 1]],
  190. [[1, 1],
  191. [1, 1],
  192. [1, 1]]]).astype(np.float32)
  193. dw_expect = 2 * np.array([[0.3, 0.6, 0.9, 1.2],
  194. [0.3, 0.6, 0.9, 1.2]]).astype(np.float32)
  195. dw_error = np.ones(shape=[2, 4]) * 1.0e-6
  196. db_expect = 2 * np.array([3, 3]).astype(np.float32)
  197. db_error = np.ones(shape=[2]) * 1.0e-6
  198. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  199. net = GradWeight(DenseNet())
  200. dw, db = net(Tensor(x), Tensor(dy))
  201. diff = dw.asnumpy() - dw_expect
  202. assert np.all(diff < dw_error)
  203. assert np.all(-diff < dw_error)
  204. diff = db.asnumpy() - db_expect
  205. assert np.all(diff < db_error)
  206. assert np.all(-diff < db_error)
  207. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  208. net = GradWeight(DenseNet())
  209. dw, db = net(Tensor(x), Tensor(dy))
  210. diff = dw.asnumpy() - dw_expect
  211. assert np.all(diff < dw_error)
  212. assert np.all(-diff < dw_error)
  213. diff = db.asnumpy() - db_expect
  214. assert np.all(diff < db_error)
  215. assert np.all(-diff < db_error)
  216. class Grad(nn.Cell):
  217. def __init__(self, network):
  218. super(Grad, self).__init__()
  219. self.grad = GradOperation(get_all=True, sens_param=True)
  220. self.network = network
  221. def construct(self, input_, bias, dy):
  222. return self.grad(self.network)(input_, bias, dy)
  223. @pytest.mark.level0
  224. @pytest.mark.platform_x86_gpu_training
  225. @pytest.mark.env_onecard
  226. def test_biasadd_3d():
  227. x = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  228. [9, 10, 11, 12, 13, 14, 15, 16],
  229. [17, 18, 19, 20, 21, 22, 23, 24],
  230. [25, 26, 27, 28, 29, 30, 31, 32]],
  231. [[33, 34, 35, 36, 37, 38, 39, 40],
  232. [41, 42, 43, 44, 45, 46, 47, 48],
  233. [49, 50, 51, 52, 53, 54, 55, 56],
  234. [57, 58, 59, 60, 61, 62, 63, 64]],
  235. [[65, 66, 67, 68, 69, 70, 71, 72],
  236. [73, 74, 75, 76, 77, 78, 79, 80],
  237. [81, 82, 83, 84, 85, 86, 87, 88],
  238. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  239. b = Tensor(np.array([1, 2, 3, 4]).astype(np.float32))
  240. dy = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  241. [9, 10, 11, 12, 13, 14, 15, 16],
  242. [17, 18, 19, 20, 21, 22, 23, 24],
  243. [25, 26, 27, 28, 29, 30, 31, 32]],
  244. [[33, 34, 35, 36, 37, 38, 39, 40],
  245. [41, 42, 43, 44, 45, 46, 47, 48],
  246. [49, 50, 51, 52, 53, 54, 55, 56],
  247. [57, 58, 59, 60, 61, 62, 63, 64]],
  248. [[65, 66, 67, 68, 69, 70, 71, 72],
  249. [73, 74, 75, 76, 77, 78, 79, 80],
  250. [81, 82, 83, 84, 85, 86, 87, 88],
  251. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  252. expect = np.array([[[2, 3, 4, 5, 6, 7, 8, 9],
  253. [11, 12, 13, 14, 15, 16, 17, 18],
  254. [20, 21, 22, 23, 24, 25, 26, 27],
  255. [29, 30, 31, 32, 33, 34, 35, 36]],
  256. [[34, 35, 36, 37, 38, 39, 40, 41],
  257. [43, 44, 45, 46, 47, 48, 49, 50],
  258. [52, 53, 54, 55, 56, 57, 58, 59],
  259. [61, 62, 63, 64, 65, 66, 67, 68]],
  260. [[66, 67, 68, 69, 70, 71, 72, 73],
  261. [75, 76, 77, 78, 79, 80, 81, 82],
  262. [84, 85, 86, 87, 88, 89, 90, 91],
  263. [93, 94, 95, 96, 97, 98, 99, 100]]])
  264. error = np.ones(shape=[3, 4, 8]) * 1.0e-6
  265. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  266. net = BiasAdd()
  267. net.set_grad()
  268. result = net(x, b)
  269. diff = result.asnumpy() - expect
  270. assert np.all(diff < error)
  271. assert np.all(-diff < error)
  272. net = Grad(net)
  273. _, result = net(x, b, dy)
  274. expect = np.array([876., 1068., 1260., 1452.])
  275. diff = result.asnumpy() - expect
  276. error = np.ones(shape=[4]) * 1.0e-6
  277. assert np.all(diff < error)
  278. assert np.all(-diff < error)
  279. @pytest.mark.level0
  280. @pytest.mark.platform_x86_gpu_training
  281. @pytest.mark.env_onecard
  282. def test_biasadd_4d():
  283. x = Tensor(np.array([[[[1, 2, 3, 4],
  284. [5, 6, 7, 8],
  285. [9, 10, 11, 12],
  286. [13, 14, 15, 16]],
  287. [[17, 18, 19, 20],
  288. [21, 22, 23, 24],
  289. [25, 26, 27, 28],
  290. [29, 30, 31, 32]],
  291. [[33, 34, 35, 36],
  292. [37, 38, 39, 40],
  293. [41, 42, 43, 44],
  294. [45, 46, 47, 48]]],
  295. [[[49, 50, 51, 52],
  296. [53, 54, 55, 56],
  297. [57, 58, 59, 60],
  298. [61, 62, 63, 64]],
  299. [[65, 66, 67, 68],
  300. [69, 70, 71, 72],
  301. [73, 74, 75, 76],
  302. [77, 78, 79, 80]],
  303. [[81, 82, 83, 84],
  304. [85, 86, 87, 88],
  305. [89, 90, 91, 92],
  306. [93, 94, 95, 96]]]]).astype(np.float32))
  307. b = Tensor(np.array([1, 2, 3]).astype(np.float32))
  308. dy = Tensor(np.array([[[[1, 2, 3, 4],
  309. [5, 6, 7, 8],
  310. [9, 10, 11, 12],
  311. [13, 14, 15, 16]],
  312. [[17, 18, 19, 20],
  313. [21, 22, 23, 24],
  314. [25, 26, 27, 28],
  315. [29, 30, 31, 32]],
  316. [[33, 34, 35, 36],
  317. [37, 38, 39, 40],
  318. [41, 42, 43, 44],
  319. [45, 46, 47, 48]]],
  320. [[[49, 50, 51, 52],
  321. [53, 54, 55, 56],
  322. [57, 58, 59, 60],
  323. [61, 62, 63, 64]],
  324. [[65, 66, 67, 68],
  325. [69, 70, 71, 72],
  326. [73, 74, 75, 76],
  327. [77, 78, 79, 80]],
  328. [[81, 82, 83, 84],
  329. [85, 86, 87, 88],
  330. [89, 90, 91, 92],
  331. [93, 94, 95, 96]]]]).astype(np.float32))
  332. expect = np.array([[[[2, 3, 4, 5],
  333. [6, 7, 8, 9],
  334. [10, 11, 12, 13],
  335. [14, 15, 16, 17]],
  336. [[19, 20, 21, 22],
  337. [23, 24, 25, 26],
  338. [27, 28, 29, 30],
  339. [31, 32, 33, 34]],
  340. [[36, 37, 38, 39],
  341. [40, 41, 42, 43],
  342. [44, 45, 46, 47],
  343. [48, 49, 50, 51]]],
  344. [[[50, 51, 52, 53],
  345. [54, 55, 56, 57],
  346. [58, 59, 60, 61],
  347. [62, 63, 64, 65]],
  348. [[67, 68, 69, 70],
  349. [71, 72, 73, 74],
  350. [75, 76, 77, 78],
  351. [79, 80, 81, 82]],
  352. [[84, 85, 86, 87],
  353. [88, 89, 90, 91],
  354. [92, 93, 94, 95],
  355. [96, 97, 98, 99]]]])
  356. error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-6
  357. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  358. ba = BiasAdd()
  359. result = ba(x, b)
  360. diff = result.asnumpy() - expect
  361. assert np.all(diff < error)
  362. assert np.all(-diff < error)
  363. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  364. net = BiasAdd()
  365. result = net(x, b)
  366. diff = result.asnumpy() - expect
  367. assert np.all(diff < error)
  368. assert np.all(-diff < error)
  369. net = Grad(net)
  370. _, result = net(x, b, dy)
  371. expect = np.array([1040., 1552., 2064.])
  372. diff = result.asnumpy() - expect
  373. error = np.ones(shape=[3]) * 1.0e-6
  374. assert np.all(diff < error)
  375. assert np.all(-diff < error)