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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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_dw():
  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]]).astype(np.float32)
  114. dy = np.array([[1, 1],
  115. [1, 1],
  116. [1, 1]]).astype(np.float32)
  117. dw_expect = np.array([[0.3, 0.6, 0.9, 1.2],
  118. [0.3, 0.6, 0.9, 1.2]]).astype(np.float32)
  119. dw_error = np.ones(shape=[2, 4]) * 1.0e-6
  120. db_expect = np.array([3, 3]).astype(np.float32)
  121. db_error = np.ones(shape=[2]) * 1.0e-6
  122. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  123. net = GradWeight(DenseNet())
  124. dw, db = net(Tensor(x), Tensor(dy))
  125. diff = dw.asnumpy() - dw_expect
  126. assert np.all(diff < dw_error)
  127. assert np.all(-diff < dw_error)
  128. diff = db.asnumpy() - db_expect
  129. assert np.all(diff < db_error)
  130. assert np.all(-diff < db_error)
  131. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  132. net = GradWeight(DenseNet())
  133. dw, db = net(Tensor(x), Tensor(dy))
  134. diff = dw.asnumpy() - dw_expect
  135. assert np.all(diff < dw_error)
  136. assert np.all(-diff < dw_error)
  137. diff = db.asnumpy() - db_expect
  138. assert np.all(diff < db_error)
  139. assert np.all(-diff < db_error)
  140. class Grad(nn.Cell):
  141. def __init__(self, network):
  142. super(Grad, self).__init__()
  143. self.grad = GradOperation(get_all=True, sens_param=True)
  144. self.network = network
  145. def construct(self, input_, bias, dy):
  146. return self.grad(self.network)(input_, bias, dy)
  147. @pytest.mark.level0
  148. @pytest.mark.platform_x86_gpu_training
  149. @pytest.mark.env_onecard
  150. def test_biasadd_3d():
  151. x = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  152. [9, 10, 11, 12, 13, 14, 15, 16],
  153. [17, 18, 19, 20, 21, 22, 23, 24],
  154. [25, 26, 27, 28, 29, 30, 31, 32]],
  155. [[33, 34, 35, 36, 37, 38, 39, 40],
  156. [41, 42, 43, 44, 45, 46, 47, 48],
  157. [49, 50, 51, 52, 53, 54, 55, 56],
  158. [57, 58, 59, 60, 61, 62, 63, 64]],
  159. [[65, 66, 67, 68, 69, 70, 71, 72],
  160. [73, 74, 75, 76, 77, 78, 79, 80],
  161. [81, 82, 83, 84, 85, 86, 87, 88],
  162. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  163. b = Tensor(np.array([1, 2, 3, 4]).astype(np.float32))
  164. dy = Tensor(np.array([[[1, 2, 3, 4, 5, 6, 7, 8],
  165. [9, 10, 11, 12, 13, 14, 15, 16],
  166. [17, 18, 19, 20, 21, 22, 23, 24],
  167. [25, 26, 27, 28, 29, 30, 31, 32]],
  168. [[33, 34, 35, 36, 37, 38, 39, 40],
  169. [41, 42, 43, 44, 45, 46, 47, 48],
  170. [49, 50, 51, 52, 53, 54, 55, 56],
  171. [57, 58, 59, 60, 61, 62, 63, 64]],
  172. [[65, 66, 67, 68, 69, 70, 71, 72],
  173. [73, 74, 75, 76, 77, 78, 79, 80],
  174. [81, 82, 83, 84, 85, 86, 87, 88],
  175. [89, 90, 91, 92, 93, 94, 95, 96]]]).astype(np.float32))
  176. expect = np.array([[[2, 3, 4, 5, 6, 7, 8, 9],
  177. [11, 12, 13, 14, 15, 16, 17, 18],
  178. [20, 21, 22, 23, 24, 25, 26, 27],
  179. [29, 30, 31, 32, 33, 34, 35, 36]],
  180. [[34, 35, 36, 37, 38, 39, 40, 41],
  181. [43, 44, 45, 46, 47, 48, 49, 50],
  182. [52, 53, 54, 55, 56, 57, 58, 59],
  183. [61, 62, 63, 64, 65, 66, 67, 68]],
  184. [[66, 67, 68, 69, 70, 71, 72, 73],
  185. [75, 76, 77, 78, 79, 80, 81, 82],
  186. [84, 85, 86, 87, 88, 89, 90, 91],
  187. [93, 94, 95, 96, 97, 98, 99, 100]]])
  188. error = np.ones(shape=[3, 4, 8]) * 1.0e-6
  189. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  190. net = BiasAdd()
  191. net.set_grad()
  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)