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_ops_attr_infer.py 9.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. """ test nn ops """
  16. import numpy as np
  17. import mindspore.nn as nn
  18. import mindspore.context as context
  19. from mindspore import Tensor
  20. from mindspore.ops import functional as F
  21. from mindspore.ops import prim_attr_register, PrimitiveWithInfer
  22. context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
  23. class FakeOp(PrimitiveWithInfer):
  24. @prim_attr_register
  25. def __init__(self):
  26. """"""
  27. def infer_shape(self, x, y):
  28. self.second_shape = y
  29. self.add_prim_attr("second_shape", y)
  30. return x
  31. def infer_dtype(self, x, y):
  32. return x
  33. # test the normal case that should generate independent primitive because of different
  34. # generated attributes after inference
  35. def test_conv2d_same_primitive():
  36. class Conv2DSameNet(nn.Cell):
  37. def __init__(self):
  38. super(Conv2DSameNet, self).__init__()
  39. self.conv1 = nn.Conv2d(16, 64, (1, 41), (1, 4), "same", 0, 1, has_bias=True)
  40. self.conv2 = nn.Conv2d(16, 64, (1, 41), (1, 4), "same", 0, 1, has_bias=True)
  41. def construct(self, x, y):
  42. r1 = self.conv1(x)
  43. r2 = self.conv2(y)
  44. return (r1, r2)
  45. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  46. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  47. net = Conv2DSameNet()
  48. net(t1, t2)
  49. # test cell as high order argument
  50. # The graph with free variables used as argument is not supported yet
  51. # because of the limit of inference specialize system
  52. def Xtest_conv2d_op_with_arg():
  53. class Conv2dNet(nn.Cell):
  54. def __init__(self):
  55. super(Conv2dNet, self).__init__()
  56. def construct(self, op, x):
  57. return op(x)
  58. class OpsNet(nn.Cell):
  59. def __init__(self, net):
  60. super(OpsNet, self).__init__()
  61. self.opnet = net
  62. self.conv2 = nn.Conv2d(16, 64, (1, 41), (1, 4), "same", 0, 1, has_bias=True)
  63. def construct(self, x, y):
  64. conv_op = self.conv2
  65. a = self.opnet(conv_op, x)
  66. b = self.opnet(conv_op, y)
  67. return (a, b)
  68. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  69. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  70. net = OpsNet(Conv2dNet())
  71. net(t1, t2)
  72. def test_conv2d_op_with_arg():
  73. class FackOpNet(nn.Cell):
  74. def __init__(self):
  75. super(FackOpNet, self).__init__()
  76. self.op = FakeOp()
  77. def construct(self, x, y):
  78. return self.op(x, y)
  79. class OpNet(nn.Cell):
  80. def __init__(self):
  81. super(OpNet, self).__init__()
  82. def construct(self, op, x, y):
  83. return op(x, y)
  84. class OpsNet(nn.Cell):
  85. def __init__(self, net):
  86. super(OpsNet, self).__init__()
  87. self.opnet = net
  88. self.op = FackOpNet()
  89. def construct(self, x, y):
  90. op = self.op
  91. a = self.opnet(op, x, y)
  92. b = self.opnet(op, y, x)
  93. return (a, b)
  94. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  95. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  96. net = OpsNet(OpNet())
  97. net(t1, t2)
  98. def test_conv2d_op_with_arg_same_input():
  99. class FackOpNet(nn.Cell):
  100. def __init__(self):
  101. super(FackOpNet, self).__init__()
  102. self.op = FakeOp()
  103. def construct(self, x, y):
  104. return self.op(x, y)
  105. class OpNet(nn.Cell):
  106. def __init__(self):
  107. super(OpNet, self).__init__()
  108. def construct(self, op, x, y):
  109. return op(x, y)
  110. class OpsNet(nn.Cell):
  111. def __init__(self, net):
  112. super(OpsNet, self).__init__()
  113. self.opnet = net
  114. self.op = FackOpNet()
  115. def construct(self, x, y):
  116. op = self.op
  117. a = self.opnet(op, x, x)
  118. b = self.opnet(op, y, x)
  119. return (a, b)
  120. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  121. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  122. net = OpsNet(OpNet())
  123. net(t1, t2)
  124. # test op with partial
  125. def test_op_as_partial():
  126. class OpAsPartial(nn.Cell):
  127. def __init__(self):
  128. super(OpAsPartial, self).__init__()
  129. self.op = FakeOp()
  130. def construct(self, x, y, z):
  131. partial_op = F.partial(self.op, x)
  132. a = partial_op(y)
  133. b = partial_op(z)
  134. return a, b
  135. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  136. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  137. t3 = Tensor(np.ones([1, 16, 1, 1234]).astype(np.float32))
  138. net = OpAsPartial()
  139. net(t1, t2, t3)
  140. # test op with partial
  141. def test_op_as_partial_inside():
  142. class OpAsPartial(nn.Cell):
  143. def __init__(self):
  144. super(OpAsPartial, self).__init__()
  145. self.op = FakeOp()
  146. def construct(self, x, y, z):
  147. partial_op = F.partial(self.op, x)
  148. a = partial_op(y)
  149. b = partial_op(z)
  150. return a, b
  151. class OuterNet(nn.Cell):
  152. def __init__(self):
  153. super(OuterNet, self).__init__()
  154. self.net = OpAsPartial()
  155. def construct(self, x, y, z):
  156. a, b = self.net(x, y, z)
  157. return a, b
  158. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  159. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  160. t3 = Tensor(np.ones([1, 16, 1, 1234]).astype(np.float32))
  161. net = OuterNet()
  162. net(t1, t2, t3)
  163. # test op with partial case 2
  164. def test_op_as_partial_independent():
  165. class OpAsPartial(nn.Cell):
  166. def __init__(self):
  167. super(OpAsPartial, self).__init__()
  168. self.op = FakeOp()
  169. def construct(self, x, y, z):
  170. partial_op1 = F.partial(self.op, x)
  171. a = partial_op1(y)
  172. partial_op2 = F.partial(self.op, x)
  173. b = partial_op2(z)
  174. return a, b
  175. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  176. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  177. t3 = Tensor(np.ones([1, 16, 1, 1234]).astype(np.float32))
  178. net = OpAsPartial()
  179. net(t1, t2, t3)
  180. def test_nest_partial():
  181. class NestPartial(nn.Cell):
  182. def __init__(self):
  183. super(NestPartial, self).__init__()
  184. self.op = FakeOp()
  185. def construct(self, x, y, z):
  186. partial_op1 = F.partial(self.op)
  187. partial_op2 = F.partial(partial_op1, x)
  188. a = partial_op2(y)
  189. partial_op3 = F.partial(self.op)
  190. partial_op4 = F.partial(partial_op3, x)
  191. b = partial_op4(z)
  192. return a, b
  193. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  194. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  195. t3 = Tensor(np.ones([1, 16, 1, 1234]).astype(np.float32))
  196. net = NestPartial()
  197. net(t1, t2, t3)
  198. # high order argument
  199. # op and op args as network arguments
  200. def test_op_with_arg_as_input():
  201. class WithOpArgNet(nn.Cell):
  202. def __init__(self):
  203. super(WithOpArgNet, self).__init__()
  204. def construct(self, op, x, y):
  205. return op(x, y)
  206. class OpsNet(nn.Cell):
  207. def __init__(self, net):
  208. super(OpsNet, self).__init__()
  209. self.opnet = net
  210. self.op = FakeOp()
  211. def construct(self, x, y, z):
  212. op = self.op
  213. a = self.opnet(op, x, z)
  214. b = self.opnet(op, x, y)
  215. return (a, b)
  216. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  217. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  218. t3 = Tensor(np.ones([1, 16, 1, 1234]).astype(np.float32))
  219. net = OpsNet(WithOpArgNet())
  220. net(t1, t2, t3)
  221. # The partial application used as argument is not supported yet
  222. # because of the limit of inference specialize system
  223. def Xtest_partial_as_arg():
  224. class PartialArgNet(nn.Cell):
  225. def __init__(self):
  226. super(PartialArgNet, self).__init__()
  227. def construct(self, partial_op, y):
  228. return partial_op(y)
  229. class OpsNet(nn.Cell):
  230. def __init__(self, net):
  231. super(OpsNet, self).__init__()
  232. self.partial_net = net
  233. self.op = FakeOp()
  234. def construct(self, x, y, z):
  235. partial_op = F.partial(self.op, x)
  236. a = self.partial_net(partial_op, z)
  237. b = self.partial_net(partial_op, y)
  238. return (a, b)
  239. t1 = Tensor(np.ones([1, 16, 1, 1918]).astype(np.float32))
  240. t2 = Tensor(np.ones([1, 16, 1, 3840]).astype(np.float32))
  241. t3 = Tensor(np.ones([1, 16, 1, 1234]).astype(np.float32))
  242. net = OpsNet(PartialArgNet())
  243. net(t1, t2, t3)