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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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(name="get_all", 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('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. net.set_grad()
  193. result = net(x, b)
  194. diff = result.asnumpy() - expect
  195. assert np.all(diff < error)
  196. assert np.all(-diff < error)
  197. net = Grad(net)
  198. _, result = net(x, b, dy)
  199. expect = np.array([876., 1068., 1260., 1452.])
  200. diff = result.asnumpy() - expect
  201. error = np.ones(shape=[4]) * 1.0e-6
  202. assert np.all(diff < error)
  203. assert np.all(-diff < error)
  204. @pytest.mark.level0
  205. @pytest.mark.platform_x86_gpu_training
  206. @pytest.mark.env_onecard
  207. def test_biasadd_4d():
  208. x = Tensor(np.array([[[[1, 2, 3, 4],
  209. [5, 6, 7, 8],
  210. [9, 10, 11, 12],
  211. [13, 14, 15, 16]],
  212. [[17, 18, 19, 20],
  213. [21, 22, 23, 24],
  214. [25, 26, 27, 28],
  215. [29, 30, 31, 32]],
  216. [[33, 34, 35, 36],
  217. [37, 38, 39, 40],
  218. [41, 42, 43, 44],
  219. [45, 46, 47, 48]]],
  220. [[[49, 50, 51, 52],
  221. [53, 54, 55, 56],
  222. [57, 58, 59, 60],
  223. [61, 62, 63, 64]],
  224. [[65, 66, 67, 68],
  225. [69, 70, 71, 72],
  226. [73, 74, 75, 76],
  227. [77, 78, 79, 80]],
  228. [[81, 82, 83, 84],
  229. [85, 86, 87, 88],
  230. [89, 90, 91, 92],
  231. [93, 94, 95, 96]]]]).astype(np.float32))
  232. b = Tensor(np.array([1, 2, 3]).astype(np.float32))
  233. dy = Tensor(np.array([[[[1, 2, 3, 4],
  234. [5, 6, 7, 8],
  235. [9, 10, 11, 12],
  236. [13, 14, 15, 16]],
  237. [[17, 18, 19, 20],
  238. [21, 22, 23, 24],
  239. [25, 26, 27, 28],
  240. [29, 30, 31, 32]],
  241. [[33, 34, 35, 36],
  242. [37, 38, 39, 40],
  243. [41, 42, 43, 44],
  244. [45, 46, 47, 48]]],
  245. [[[49, 50, 51, 52],
  246. [53, 54, 55, 56],
  247. [57, 58, 59, 60],
  248. [61, 62, 63, 64]],
  249. [[65, 66, 67, 68],
  250. [69, 70, 71, 72],
  251. [73, 74, 75, 76],
  252. [77, 78, 79, 80]],
  253. [[81, 82, 83, 84],
  254. [85, 86, 87, 88],
  255. [89, 90, 91, 92],
  256. [93, 94, 95, 96]]]]).astype(np.float32))
  257. expect = np.array([[[[2, 3, 4, 5],
  258. [6, 7, 8, 9],
  259. [10, 11, 12, 13],
  260. [14, 15, 16, 17]],
  261. [[19, 20, 21, 22],
  262. [23, 24, 25, 26],
  263. [27, 28, 29, 30],
  264. [31, 32, 33, 34]],
  265. [[36, 37, 38, 39],
  266. [40, 41, 42, 43],
  267. [44, 45, 46, 47],
  268. [48, 49, 50, 51]]],
  269. [[[50, 51, 52, 53],
  270. [54, 55, 56, 57],
  271. [58, 59, 60, 61],
  272. [62, 63, 64, 65]],
  273. [[67, 68, 69, 70],
  274. [71, 72, 73, 74],
  275. [75, 76, 77, 78],
  276. [79, 80, 81, 82]],
  277. [[84, 85, 86, 87],
  278. [88, 89, 90, 91],
  279. [92, 93, 94, 95],
  280. [96, 97, 98, 99]]]])
  281. error = np.ones(shape=[2, 3, 4, 4]) * 1.0e-6
  282. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  283. ba = BiasAdd()
  284. result = ba(x, b)
  285. diff = result.asnumpy() - expect
  286. assert np.all(diff < error)
  287. assert np.all(-diff < error)
  288. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  289. net = BiasAdd()
  290. result = net(x, b)
  291. diff = result.asnumpy() - expect
  292. assert np.all(diff < error)
  293. assert np.all(-diff < error)
  294. net = Grad(net)
  295. _, result = net(x, b, dy)
  296. expect = np.array([1040., 1552., 2064.])
  297. diff = result.asnumpy() - expect
  298. error = np.ones(shape=[3]) * 1.0e-6
  299. assert np.all(diff < error)
  300. assert np.all(-diff < error)