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_var_grad.py 11 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # Copyright 2020 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. from mindspore import context
  17. from mindspore import Tensor, Parameter
  18. from mindspore.nn import Cell
  19. from mindspore.ops import operations as P
  20. import mindspore.ops.composite as C
  21. from mindspore.common.api import _executor
  22. from mindspore.common.parameter import ParameterTuple
  23. from mindspore.common import dtype as mstype
  24. context.set_context(mode=context.GRAPH_MODE)
  25. def test_net_vargs_expand():
  26. class AddNet(Cell):
  27. def __init__(self):
  28. super(AddNet, self).__init__()
  29. self.w = Parameter(
  30. Tensor(np.ones((3, 4, 5), np.float32)), "w2", requires_grad=True)
  31. def construct(self, x, y):
  32. return x + y
  33. x = Tensor(np.random.normal(0, 1, [3, 4, 5]).astype(np.float32))
  34. y = Tensor(np.random.normal(0, 1, [3, 4, 5]).astype(np.float32))
  35. sens = Tensor(np.random.normal(0, 1, [3, 4, 5]).astype(np.float32))
  36. net = AddNet()
  37. out = C.grad_all_with_sens(net, net.trainable_params())(x, y, sens)
  38. class VarNet(Cell):
  39. def __init__(self, net):
  40. super(VarNet, self).__init__()
  41. self.b = Parameter(
  42. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b", requires_grad=True)
  43. self.w = Parameter(
  44. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "w", requires_grad=True)
  45. self.net = net
  46. def construct(self, *args):
  47. return self.net(*args)*self.w + self.b
  48. class SecondNet(Cell):
  49. def __init__(self):
  50. super(SecondNet, self).__init__()
  51. self.b2 = Parameter(
  52. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b2", requires_grad=True)
  53. def construct(self, *args):
  54. res = args[0] + args[1]
  55. return res + self.b2
  56. class Bprop(Cell):
  57. def __init__(self, func, wrt_params, params, grad_op, sens=None):
  58. super(Bprop, self).__init__(auto_prefix=False)
  59. self.func = func
  60. self.wrt_params = wrt_params
  61. self.params = None
  62. if self.wrt_params and params:
  63. self.params = ParameterTuple(params)
  64. self.grad = grad_op
  65. self.with_sens = False
  66. self.sens = sens
  67. if sens:
  68. self.sens = Tensor(sens, dtype=mstype.float32)
  69. self.with_sens = True
  70. def construct(self, *inputs):
  71. # pylint: disable=no-else-return
  72. if self.wrt_params:
  73. if self.with_sens:
  74. return self.grad(self.func, self.params)(*inputs, self.sens)
  75. else:
  76. return self.grad(self.func, self.params)(*inputs)
  77. elif self.with_sens:
  78. return self.grad(self.func)(*inputs, self.sens)
  79. else:
  80. return self.grad(self.func)(*inputs)
  81. def test_all_var_args_grad_with_sens():
  82. """"test grad_by_list_with_sens with all var args input"""
  83. class GradNet(Cell):
  84. def __init__(self, net):
  85. super(GradNet, self).__init__()
  86. self.weights = ParameterTuple(net.trainable_params())
  87. self.net = net
  88. def construct(self, *inputs):
  89. return C.grad_by_list_with_sens(self.net, self.weights)(*inputs)
  90. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  91. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  92. sens = Tensor(1.0, dtype=mstype.float32)
  93. net = VarNet(SecondNet())
  94. grad_net = GradNet(net)
  95. out = grad_net(x, y, sens)
  96. def test_grad_list_var_args():
  97. class GradNet(Cell):
  98. def __init__(self, net):
  99. super(GradNet, self).__init__()
  100. self.weights = ParameterTuple(net.trainable_params())
  101. self.net = net
  102. def construct(self, *inputs):
  103. return C.grad_by_list(self.net, self.weights)(*inputs)
  104. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  105. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  106. net = VarNet(SecondNet())
  107. grad_net = GradNet(net)
  108. out = grad_net(x, y)
  109. def test_grad_all_var_args():
  110. class GradNet(Cell):
  111. def __init__(self, net):
  112. super(GradNet, self).__init__()
  113. self.weights = ParameterTuple(net.trainable_params())
  114. self.net = net
  115. def construct(self, *inputs):
  116. return C.grad_all(self.net)(*inputs)
  117. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  118. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  119. net = VarNet(SecondNet())
  120. grad_net = GradNet(net)
  121. out = grad_net(x, y)
  122. def test_grad_all_var_args_with_sens():
  123. class GradNet(Cell):
  124. def __init__(self, net):
  125. super(GradNet, self).__init__()
  126. self.weights = ParameterTuple(net.trainable_params())
  127. self.net = net
  128. def construct(self, *inputs):
  129. return C.grad_all_with_sens(self.net)(*inputs)
  130. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  131. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  132. sens = Tensor(1.0, dtype=mstype.float32)
  133. net = VarNet(SecondNet())
  134. grad_net = GradNet(net)
  135. out = grad_net(x, y, sens)
  136. def test_grad_var_args_with_sens():
  137. class GradNet(Cell):
  138. def __init__(self, net):
  139. super(GradNet, self).__init__()
  140. self.weights = ParameterTuple(net.trainable_params())
  141. self.net = net
  142. def construct(self, *inputs):
  143. return C.grad_with_sens(self.net)(*inputs)
  144. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  145. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  146. sens = Tensor(1.0, dtype=mstype.float32)
  147. net = VarNet(SecondNet())
  148. grad_net = GradNet(net)
  149. out = grad_net(x, y, sens)
  150. def test_var_args_grad():
  151. class VarNet(Cell):
  152. def __init__(self, net):
  153. super(VarNet, self).__init__()
  154. self.b = Parameter(
  155. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b", requires_grad=True)
  156. self.net = net
  157. def construct(self, *args):
  158. return self.net(*args) + self.b
  159. class SecondNet(Cell):
  160. def __init__(self):
  161. super(SecondNet, self).__init__()
  162. self.b2 = Parameter(
  163. Tensor(np.ones([3, 4, 5]), dtype=mstype.float32), "b2", requires_grad=True)
  164. def construct(self, *args):
  165. res = args[0] + args[1]
  166. return res + self.b2
  167. class GradNet(Cell):
  168. def __init__(self, net):
  169. super(GradNet, self).__init__()
  170. self.net = net
  171. self.weights = ParameterTuple(net.trainable_params())
  172. def construct(self, x, y, sens):
  173. return C.grad_by_list_with_sens(self.net, self.weights)(x, y, sens)
  174. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  175. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  176. sens = Tensor(1.0, dtype=mstype.float32)
  177. net = VarNet(SecondNet())
  178. grad_net = GradNet(net)
  179. out = grad_net(x, y, sens)
  180. def test_var_args_positional():
  181. """"test grad_all with var args in inner graph"""
  182. class VarNet(Cell):
  183. def __init__(self, net):
  184. super(VarNet, self).__init__()
  185. self.net = net
  186. def construct(self, x, y):
  187. return self.net(x, y)*x
  188. class SecondNet(Cell):
  189. def __init__(self):
  190. super(SecondNet, self).__init__()
  191. def construct(self, *args):
  192. return args[0] + args[1]
  193. class GradNet(Cell):
  194. def __init__(self, net):
  195. super(GradNet, self).__init__()
  196. self.net = net
  197. self.weights = ParameterTuple(net.trainable_params())
  198. def construct(self, x, y):
  199. return C.grad_all(self.net)(x, y)
  200. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  201. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  202. net = VarNet(SecondNet())
  203. grad_net = GradNet(net)
  204. out = grad_net(x, y)
  205. def test_grad_within_if_else():
  206. class GradNet(Cell):
  207. def __init__(self, net):
  208. super(GradNet, self).__init__()
  209. self.weights = ParameterTuple(net.trainable_params())
  210. self.net = net
  211. grad_op = C.GradOperation(
  212. name='grad', get_all=False, get_by_list=True, sens_param=True)
  213. self.grad = Bprop(self.net, True, self.weights, grad_op, 1.0)
  214. def construct(self, *inputs):
  215. return self.grad(*inputs)
  216. x = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  217. y = Tensor(np.ones([3, 4, 5]), dtype=mstype.float32)
  218. sens = Tensor(1.0, dtype=mstype.float32)
  219. net = VarNet(SecondNet())
  220. grad_net = GradNet(net)
  221. out = grad_net(x, y)
  222. print("test_grad_var_args_with_sens out=", out)
  223. def test_grad_for_concat():
  224. class GradNet(Cell):
  225. def __init__(self, net):
  226. super(GradNet, self).__init__()
  227. self.weights = ParameterTuple(net.trainable_params())
  228. self.net = net
  229. grad_op = C.GradOperation(
  230. name='grad', get_all=True, get_by_list=False, sens_param=True)
  231. self.grad = Bprop(self.net, False, self.weights, grad_op)
  232. def construct(self, *inputs):
  233. return self.grad(*inputs)
  234. class Concat(Cell):
  235. def __init__(self, axis):
  236. super().__init__()
  237. self.concat = P.Concat(axis=axis)
  238. def construct(self, *input1):
  239. return self.concat(input1)
  240. class ConcatFactory:
  241. def __init__(self, input_shape, axis, dtype=np.float32):
  242. super(ConcatFactory, self).__init__()
  243. self.inputs_np = []
  244. for s in input_shape:
  245. self.inputs_np.append(np.random.randn(*s).astype(dtype))
  246. self.axis = axis
  247. self.out_numpy = np.concatenate(self.inputs_np, axis=self.axis)
  248. self.out_grad_np = self.out_numpy
  249. def grad_mindspore_impl(self):
  250. inputs = []
  251. for i in self.inputs_np:
  252. inputs.append(Tensor(i))
  253. net = Concat(axis=self.axis)
  254. grad_net = GradNet(net)
  255. grad_net.set_train()
  256. input_grad = grad_net(*inputs, Tensor(self.out_grad_np))
  257. def grad_cmp(self):
  258. input_grad_mindspore = self.grad_mindspore_impl()
  259. fact = ConcatFactory(input_shape=(
  260. (2, 184320, 1), (2, 46080, 1), (2, 11520, 1), (2, 2880, 1), (2, 720, 1)), axis=1)
  261. fact.grad_cmp()