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_parse_exception.py 7.6 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import numpy as np
  2. import mindspore.nn as nn
  3. from mindspore import context
  4. from mindspore.common.parameter import Parameter
  5. from mindspore.common.tensor import Tensor
  6. import mindspore.ops as ops
  7. import mindspore
  8. context.set_context(mode=context.GRAPH_MODE)
  9. class TestNoReturn(nn.Cell):
  10. def __init__(self):
  11. super(TestNoReturn, self).__init__()
  12. self.m = 1
  13. def construct(self, x, y):
  14. and_v = x * y
  15. and_v += 1
  16. # return and_v
  17. def test_no_return():
  18. net = TestNoReturn()
  19. x = Tensor(np.ones([2, 2], np.float))
  20. y = Tensor(np.zeros([2, 2], np.float))
  21. ret = net(x, y)
  22. print(ret)
  23. class TestSuper(nn.Cell):
  24. def __init__(self):
  25. super().__init__()
  26. self.m = 1
  27. def construct(self, x, y):
  28. super(TestSuper, 2, 3).aa()
  29. and_v = x * y
  30. return and_v
  31. def test_super():
  32. net = TestSuper()
  33. x = Tensor(np.ones([2, 2], np.float))
  34. y = Tensor(np.zeros([2, 2], np.float))
  35. ret = net(x, y)
  36. print(ret)
  37. class TestCompare(nn.Cell):
  38. def __init__(self):
  39. super(TestCompare, self).__init__()
  40. self.m = 1
  41. def construct(self, x, y):
  42. return x > y > 10
  43. def test_compare():
  44. net = TestCompare()
  45. x = Tensor(np.ones([2, 2], np.float))
  46. y = Tensor(np.zeros([2, 2], np.float))
  47. ret = net(x, y)
  48. print(ret)
  49. class TestUndefMemberChange(nn.Cell):
  50. def __init__(self):
  51. super(TestUndefMemberChange, self).__init__()
  52. self.m = 1
  53. def construct(self, x, y):
  54. self.t = x
  55. return x > y
  56. def test_undef_member_changer():
  57. net = TestUndefMemberChange()
  58. x = Tensor(np.ones([2, 2], np.float))
  59. y = Tensor(np.zeros([2, 2], np.float))
  60. ret = net(x, y)
  61. print(ret)
  62. class TestMemberChange(nn.Cell):
  63. def __init__(self):
  64. super(TestMemberChange, self).__init__()
  65. self.t = Tensor(np.zeros([2, 2], np.float))
  66. def construct(self, x, y):
  67. self.t = x
  68. return x > y
  69. def test_member_changer():
  70. net = TestMemberChange()
  71. x = Tensor(np.ones([2, 2], np.float))
  72. y = Tensor(np.zeros([2, 2], np.float))
  73. ret = net(x, y)
  74. print(ret)
  75. class TestUnsupportSTMT(nn.Cell):
  76. def __init__(self):
  77. super(TestUnsupportSTMT, self).__init__()
  78. self.m = 1
  79. def construct(self, x, y):
  80. try:
  81. val = x + y
  82. finally:
  83. val = x
  84. return val
  85. def test_UnsupportSTMT():
  86. net = TestUnsupportSTMT()
  87. x = Tensor(np.ones([2, 2], np.float))
  88. y = Tensor(np.zeros([2, 2], np.float))
  89. ret = net(x, y)
  90. print(ret)
  91. class TestUnsupportNum(nn.Cell):
  92. def __init__(self):
  93. super(TestUnsupportNum, self).__init__()
  94. self.m = 1
  95. def construct(self, x, y):
  96. a = x + 3.14j
  97. return a
  98. def test_UnsupportNum():
  99. net = TestUnsupportNum()
  100. x = Tensor(np.ones([2, 2], np.float))
  101. y = Tensor(np.zeros([2, 2], np.float))
  102. ret = net(x, y)
  103. print(ret)
  104. class TestAssignAdd(nn.Cell):
  105. def __init__(self):
  106. super(TestAssignAdd, self).__init__()
  107. self.m = 1
  108. def construct(self, x, y):
  109. x.id_ += y
  110. # x[1] += y
  111. return x
  112. def test_AssignAdd():
  113. net = TestAssignAdd()
  114. ret = net([3, 1], 2)
  115. print(ret)
  116. class TestParseListComp(nn.Cell):
  117. def __init__(self):
  118. super(TestParseListComp, self).__init__()
  119. self.m = 1
  120. def construct(self, x, y):
  121. ret = [m + y for l in x for m in l]
  122. return ret
  123. def test_ParseListComp():
  124. net = TestParseListComp()
  125. ret = net([[1, 2], [3, 4]], 2)
  126. print(ret)
  127. class TestAssign(nn.Cell):
  128. def __init__(self):
  129. super(TestAssign, self).__init__()
  130. self.m = 1
  131. def construct(self, x, y):
  132. x.id_ = y
  133. return x
  134. def test_Assign():
  135. net = TestAssign()
  136. ret = net([3, 1], 2)
  137. print(ret)
  138. class TestAssignList(nn.Cell):
  139. def __init__(self):
  140. super(TestAssignList, self).__init__()
  141. self.m = 1
  142. def construct(self, x, y):
  143. [m, n] = [x, y]
  144. return m, n
  145. def test_AssignList():
  146. net = TestAssignList()
  147. ret = net([3, 1], 2)
  148. print(ret)
  149. class TestParaDef(nn.Cell):
  150. def __init__(self):
  151. super(TestParaDef, self).__init__()
  152. self.m = 1
  153. def construct(self, x=1, y=1):
  154. ret = x + y
  155. return ret
  156. def test_para_def():
  157. net = TestParaDef()
  158. ret = net(1, 2)
  159. print(ret)
  160. class TestParameterNameNone(nn.Cell):
  161. def __init__(self):
  162. super(TestParameterNameNone, self).__init__()
  163. self.matmul = ops.MatMul()
  164. # self.weight = Parameter(Tensor(np.ones((1, 2)), mindspore.float32), name="w", requires_grad=True)
  165. self.weight = Parameter(Tensor(np.ones((1, 2)), mindspore.float32), name=None, requires_grad=True)
  166. def construct(self, x):
  167. out = self.matmul(self.weight, x)
  168. return out
  169. def test_parameter_name_none():
  170. net = TestParameterNameNone()
  171. x = Tensor(np.ones((2, 1)), mindspore.float32)
  172. print(net(x))
  173. class TestBranchReturn(nn.Cell):
  174. def __init__(self):
  175. super(TestBranchReturn, self).__init__()
  176. self.m = 1
  177. def construct(self, x):
  178. if x > 0:
  179. return x + 1
  180. return x
  181. def test_branch_return():
  182. net = TestBranchReturn()
  183. print(net(1))
  184. class TestSliceNotInt(nn.Cell):
  185. def __init__(self):
  186. super(TestSliceNotInt, self).__init__()
  187. self.m = 1
  188. def construct(self, x):
  189. s = "ABCDEFGHIJKL"
  190. sl = slice(x, 4.5)
  191. return s[sl]
  192. def test_slice_not_int():
  193. net = TestSliceNotInt()
  194. print(net(1))
  195. class TestSliceNotIntDefInInit(nn.Cell):
  196. def __init__(self):
  197. super(TestSliceNotIntDefInInit, self).__init__()
  198. self.sl = slice(1, 4.5)
  199. def construct(self, x):
  200. s = "ABCDEFGHIJKL"
  201. return s[self.sl]
  202. def test_slice_not_int_def_in_init():
  203. net = TestSliceNotIntDefInInit()
  204. print(net(1))
  205. class MatMulCell(nn.Cell):
  206. def __init__(self):
  207. super().__init__()
  208. self.m = 1
  209. def construct(self, x):
  210. return x
  211. class TestCellPipelineStage(nn.Cell):
  212. def __init__(self, strategy1, strategy2, param=None):
  213. super().__init__()
  214. self.block = nn.CellList()
  215. cell = MatMulCell()
  216. cell.pipeline_stage = -1
  217. self.block.append(cell)
  218. cell = MatMulCell()
  219. cell.pipeline_stage = -1
  220. self.block.append(cell)
  221. def construct(self, x):
  222. for i in range(2):
  223. x = self.block[i](x)
  224. return x
  225. def test_cell_pipeline_state():
  226. strategy1 = Tensor((4, 1), mindspore.int64)
  227. strategy2 = Tensor((2, 1), mindspore.int64)
  228. net = TestCellPipelineStage(strategy1, strategy2)
  229. print(net(1))
  230. class TestArgsKwArgs(nn.Cell):
  231. def __init__(self):
  232. super(TestArgsKwArgs, self).__init__()
  233. self.m = 1
  234. def construct(self, *args, **kwargs):
  235. x = 0
  236. for v in args:
  237. x += v
  238. # for k, v in kwargs.items():
  239. # x += v
  240. return x
  241. def test_args_kwargs():
  242. net = TestArgsKwArgs()
  243. print(net(1, 2, 3, 4, k1=5, k2=6))
  244. class TestArgs(nn.Cell):
  245. def __init__(self):
  246. super(TestArgs, self).__init__()
  247. self.m = 1
  248. def construct(self, x, *args):
  249. for v in args:
  250. x += v
  251. return x
  252. def test_args():
  253. net = TestArgs()
  254. print(net(1, 2, 3, 4))
  255. class TestNoDef(nn.Cell):
  256. def __init__(self):
  257. super().__init__()
  258. self.m = 1
  259. def construct(self, x):
  260. x += self.y
  261. return x
  262. def test_no_def():
  263. net = TestNoDef()
  264. print(net(1))