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_element_wise_function.py 35 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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. import numpy as np
  15. import mindspore as ms
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.ops import composite as C
  21. from mindspore.ops import operations as P
  22. from tests.ut.python.ops.test_math_ops import VirtualLoss
  23. grad_all = C.GradOperation(get_all=True)
  24. class NetWithLoss(nn.Cell):
  25. def __init__(self, network):
  26. super(NetWithLoss, self).__init__()
  27. self.loss = VirtualLoss()
  28. self.network = network
  29. def construct(self, x, y, b):
  30. predict = self.network(x, y, b)
  31. return self.loss(predict)
  32. class GradWrap(nn.Cell):
  33. def __init__(self, network):
  34. super(GradWrap, self).__init__()
  35. self.network = network
  36. def construct(self, x, y, b):
  37. return grad_all(self.network)(x, y, b)
  38. def compile_net(net, x, y, b):
  39. net.set_auto_parallel()
  40. _executor.compile(net, x, y, b)
  41. def test_matmul_pow():
  42. class Net(nn.Cell):
  43. def __init__(self, strategy1, strategy2):
  44. super().__init__()
  45. self.matmul = P.MatMul().shard(strategy1)
  46. self.pow = P.Pow().shard(strategy2)
  47. self.matmul2 = P.MatMul().shard(strategy1)
  48. def construct(self, x, y, b):
  49. out = self.matmul(x, y)
  50. out = self.pow(out, 2.0)
  51. out = self.matmul2(out, b)
  52. return out
  53. context.set_auto_parallel_context(device_num=8, global_rank=0)
  54. strategy1 = ((2, 2), (2, 2))
  55. strategy2 = ((4, 2), ())
  56. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  57. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  58. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  59. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  60. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  61. compile_net(net, x, y, b)
  62. def test_matmul_exp():
  63. class Net(nn.Cell):
  64. def __init__(self, strategy1, strategy2):
  65. super().__init__()
  66. self.matmul = P.MatMul().shard(strategy1)
  67. self.exp = P.Exp().shard(strategy2)
  68. self.matmul2 = P.MatMul().shard(strategy1)
  69. def construct(self, x, y, b):
  70. out = self.matmul(x, y)
  71. out = self.exp(out)
  72. out = self.matmul2(out, b)
  73. return out
  74. context.set_auto_parallel_context(device_num=8, global_rank=0)
  75. strategy1 = ((2, 2), (2, 2))
  76. strategy2 = ((4, 2),)
  77. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  78. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  79. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  80. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  81. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  82. compile_net(net, x, y, b)
  83. def test_matmul_log():
  84. class Net(nn.Cell):
  85. def __init__(self, strategy1, strategy2):
  86. super().__init__()
  87. self.matmul = P.MatMul().shard(strategy1)
  88. self.log = P.Log().shard(strategy2)
  89. self.matmul2 = P.MatMul().shard(strategy1)
  90. def construct(self, x, y, b):
  91. out = self.matmul(x, y)
  92. out = self.log(out)
  93. out = self.matmul2(out, b)
  94. return out
  95. context.set_auto_parallel_context(device_num=8, global_rank=0)
  96. strategy1 = ((2, 2), (2, 2))
  97. strategy2 = ((4, 2),)
  98. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  99. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  100. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  101. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  102. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  103. compile_net(net, x, y, b)
  104. def test_matmul_abs():
  105. class Net(nn.Cell):
  106. def __init__(self, strategy1, strategy2):
  107. super().__init__()
  108. self.matmul = P.MatMul().shard(strategy1)
  109. self.abs = P.Abs().shard(strategy2)
  110. self.matmul2 = P.MatMul().shard(strategy1)
  111. def construct(self, x, y, b):
  112. out = self.matmul(x, y)
  113. out = self.abs(out)
  114. out = self.matmul2(out, b)
  115. return out
  116. context.set_auto_parallel_context(device_num=8, global_rank=0)
  117. strategy1 = ((2, 2), (2, 2))
  118. strategy2 = ((4, 2),)
  119. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  120. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  121. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  122. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  123. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  124. compile_net(net, x, y, b)
  125. def test_matmul_sign():
  126. class Net(nn.Cell):
  127. def __init__(self, strategy1, strategy2):
  128. super().__init__()
  129. self.matmul = P.MatMul().shard(strategy1)
  130. self.sign = P.Sign().shard(strategy2)
  131. self.matmul2 = P.MatMul().shard(strategy1)
  132. def construct(self, x, y, b):
  133. out = self.matmul(x, y)
  134. out = self.sign(out)
  135. out = self.matmul2(out, b)
  136. return out
  137. context.set_auto_parallel_context(device_num=8, global_rank=0)
  138. strategy1 = ((2, 2), (2, 2))
  139. strategy2 = ((4, 2),)
  140. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  141. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  142. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  143. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  144. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  145. compile_net(net, x, y, b)
  146. def test_matmul_floor():
  147. class Net(nn.Cell):
  148. def __init__(self, strategy1, strategy2):
  149. super().__init__()
  150. self.matmul = P.MatMul().shard(strategy1)
  151. self.floor = P.Floor().shard(strategy2)
  152. self.matmul2 = P.MatMul().shard(strategy1)
  153. def construct(self, x, y, b):
  154. out = self.matmul(x, y)
  155. out = self.floor(out)
  156. out = self.matmul2(out, b)
  157. return out
  158. context.set_auto_parallel_context(device_num=8, global_rank=0)
  159. strategy1 = ((2, 2), (2, 2))
  160. strategy2 = ((4, 2),)
  161. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  162. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  163. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  164. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  165. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  166. compile_net(net, x, y, b)
  167. def test_matmul_round():
  168. class Net(nn.Cell):
  169. def __init__(self, strategy1, strategy2):
  170. super().__init__()
  171. self.matmul = P.MatMul().shard(strategy1)
  172. self.round = P.Round().shard(strategy2)
  173. self.matmul2 = P.MatMul().shard(strategy1)
  174. def construct(self, x, y, b):
  175. out = self.matmul(x, y)
  176. out = self.round(out)
  177. out = self.matmul2(out, b)
  178. return out
  179. context.set_auto_parallel_context(device_num=8, global_rank=0)
  180. strategy1 = ((2, 2), (2, 2))
  181. strategy2 = ((4, 2),)
  182. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  183. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  184. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  185. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  186. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  187. compile_net(net, x, y, b)
  188. def test_matmul_reciprocal():
  189. class Net(nn.Cell):
  190. def __init__(self, strategy1, strategy2):
  191. super().__init__()
  192. self.matmul = P.MatMul().shard(strategy1)
  193. self.reciprocal = P.Reciprocal().shard(strategy2)
  194. self.matmul2 = P.MatMul().shard(strategy1)
  195. def construct(self, x, y, b):
  196. out = self.matmul(x, y)
  197. out = self.reciprocal(out)
  198. out = self.matmul2(out, b)
  199. return out
  200. context.set_auto_parallel_context(device_num=8, global_rank=0)
  201. strategy1 = ((2, 2), (2, 2))
  202. strategy2 = ((4, 2),)
  203. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  204. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  205. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  206. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  207. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  208. compile_net(net, x, y, b)
  209. def test_matmul_inv():
  210. class Net(nn.Cell):
  211. def __init__(self, strategy1, strategy2):
  212. super().__init__()
  213. self.matmul = P.MatMul().shard(strategy1)
  214. self.inv = P.Inv().shard(strategy2)
  215. self.matmul2 = P.MatMul().shard(strategy1)
  216. def construct(self, x, y, b):
  217. out = self.matmul(x, y)
  218. out = self.inv(out)
  219. out = self.matmul2(out, b)
  220. return out
  221. context.set_auto_parallel_context(device_num=8, global_rank=0)
  222. strategy1 = ((2, 2), (2, 2))
  223. strategy2 = ((4, 2),)
  224. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  225. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  226. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  227. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  228. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  229. compile_net(net, x, y, b)
  230. def test_matmul_rsqrt():
  231. class Net(nn.Cell):
  232. def __init__(self, strategy1, strategy2):
  233. super().__init__()
  234. self.matmul = P.MatMul().shard(strategy1)
  235. self.rsqrt = P.Rsqrt().shard(strategy2)
  236. self.matmul2 = P.MatMul().shard(strategy1)
  237. def construct(self, x, y, b):
  238. out = self.matmul(x, y)
  239. out = self.rsqrt(out)
  240. out = self.matmul2(out, b)
  241. return out
  242. context.set_auto_parallel_context(device_num=8, global_rank=0)
  243. strategy1 = ((2, 2), (2, 2))
  244. strategy2 = ((4, 2),)
  245. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  246. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  247. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  248. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  249. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  250. compile_net(net, x, y, b)
  251. def test_matmul_tan():
  252. class Net(nn.Cell):
  253. def __init__(self, strategy1, strategy2):
  254. super().__init__()
  255. self.matmul = P.MatMul().shard(strategy1)
  256. self.tan = P.Tan().shard(strategy2)
  257. self.matmul2 = P.MatMul().shard(strategy1)
  258. def construct(self, x, y, b):
  259. out = self.matmul(x, y)
  260. out = self.tan(out)
  261. out = self.matmul2(out, b)
  262. return out
  263. context.set_auto_parallel_context(device_num=8, global_rank=0)
  264. strategy1 = ((2, 2), (2, 2))
  265. strategy2 = ((4, 2),)
  266. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  267. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  268. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  269. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  270. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  271. compile_net(net, x, y, b)
  272. def test_matmul_sin():
  273. class Net(nn.Cell):
  274. def __init__(self, strategy1, strategy2):
  275. super().__init__()
  276. self.matmul = P.MatMul().shard(strategy1)
  277. self.sin = P.Sin().shard(strategy2)
  278. self.matmul2 = P.MatMul().shard(strategy1)
  279. def construct(self, x, y, b):
  280. out = self.matmul(x, y)
  281. out = self.sin(out)
  282. out = self.matmul2(out, b)
  283. return out
  284. context.set_auto_parallel_context(device_num=8, global_rank=0)
  285. strategy1 = ((2, 2), (2, 2))
  286. strategy2 = ((4, 2),)
  287. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  288. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  289. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  290. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  291. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  292. compile_net(net, x, y, b)
  293. def test_matmul_sinh():
  294. class Net(nn.Cell):
  295. def __init__(self, strategy1, strategy2):
  296. super().__init__()
  297. self.matmul = P.MatMul().shard(strategy1)
  298. self.sinh = P.Sinh().shard(strategy2)
  299. self.matmul2 = P.MatMul().shard(strategy1)
  300. def construct(self, x, y, b):
  301. out = self.matmul(x, y)
  302. out = self.sinh(out)
  303. out = self.matmul2(out, b)
  304. return out
  305. context.set_auto_parallel_context(device_num=8, global_rank=0)
  306. strategy1 = ((2, 2), (2, 2))
  307. strategy2 = ((4, 2),)
  308. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  309. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  310. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  311. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  312. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  313. compile_net(net, x, y, b)
  314. def test_matmul_log1p():
  315. class Net(nn.Cell):
  316. def __init__(self, strategy1, strategy2):
  317. super().__init__()
  318. self.matmul = P.MatMul().shard(strategy1)
  319. self.log1p = P.Log1p().shard(strategy2)
  320. self.matmul2 = P.MatMul().shard(strategy1)
  321. def construct(self, x, y, b):
  322. out = self.matmul(x, y)
  323. out = self.log1p(out)
  324. out = self.matmul2(out, b)
  325. return out
  326. context.set_auto_parallel_context(device_num=8, global_rank=0)
  327. strategy1 = ((2, 2), (2, 2))
  328. strategy2 = ((4, 2),)
  329. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  330. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  331. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  332. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  333. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  334. compile_net(net, x, y, b)
  335. def test_matmul_expm1():
  336. class Net(nn.Cell):
  337. def __init__(self, strategy1, strategy2):
  338. super().__init__()
  339. self.matmul = P.MatMul().shard(strategy1)
  340. self.expm1 = P.Expm1().shard(strategy2)
  341. self.matmul2 = P.MatMul().shard(strategy1)
  342. def construct(self, x, y, b):
  343. out = self.matmul(x, y)
  344. out = self.expm1(out)
  345. out = self.matmul2(out, b)
  346. return out
  347. context.set_auto_parallel_context(device_num=8, global_rank=0)
  348. strategy1 = ((2, 2), (2, 2))
  349. strategy2 = ((4, 2),)
  350. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  351. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  352. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  353. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  354. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  355. compile_net(net, x, y, b)
  356. def test_matmul_cosh():
  357. class Net(nn.Cell):
  358. def __init__(self, strategy1, strategy2):
  359. super().__init__()
  360. self.matmul = P.MatMul().shard(strategy1)
  361. self.cosh = P.Cosh().shard(strategy2)
  362. self.matmul2 = P.MatMul().shard(strategy1)
  363. def construct(self, x, y, b):
  364. out = self.matmul(x, y)
  365. out = self.cosh(out)
  366. out = self.matmul2(out, b)
  367. return out
  368. context.set_auto_parallel_context(device_num=8, global_rank=0)
  369. strategy1 = ((2, 2), (2, 2))
  370. strategy2 = ((4, 2),)
  371. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  372. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  373. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  374. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  375. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  376. compile_net(net, x, y, b)
  377. def test_matmul_erf():
  378. class Net(nn.Cell):
  379. def __init__(self, strategy1, strategy2):
  380. super().__init__()
  381. self.matmul = P.MatMul().shard(strategy1)
  382. self.erf = P.Erf().shard(strategy2)
  383. self.matmul2 = P.MatMul().shard(strategy1)
  384. def construct(self, x, y, b):
  385. out = self.matmul(x, y)
  386. out = self.erf(out)
  387. out = self.matmul2(out, b)
  388. return out
  389. context.set_auto_parallel_context(device_num=8, global_rank=0)
  390. strategy1 = ((2, 2), (2, 2))
  391. strategy2 = ((4, 2),)
  392. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  393. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  394. x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
  395. y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
  396. b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
  397. compile_net(net, x, y, b)
  398. def test_matmul_erfc():
  399. class Net(nn.Cell):
  400. def __init__(self, strategy1, strategy2):
  401. super().__init__()
  402. self.matmul = P.MatMul().shard(strategy1)
  403. self.erfc = P.Erfc().shard(strategy2)
  404. self.matmul2 = P.MatMul().shard(strategy1)
  405. def construct(self, x, y, b):
  406. out = self.matmul(x, y)
  407. out = self.erfc(out)
  408. out = self.matmul2(out, b)
  409. return out
  410. context.set_auto_parallel_context(device_num=8, global_rank=0)
  411. strategy1 = ((2, 2), (2, 2))
  412. strategy2 = ((4, 2),)
  413. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  414. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  415. x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
  416. y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
  417. b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
  418. compile_net(net, x, y, b)
  419. def test_matmul_zeroslike():
  420. class Net(nn.Cell):
  421. def __init__(self, strategy1, strategy2):
  422. super().__init__()
  423. self.matmul = P.MatMul().shard(strategy1)
  424. self.zeroslike = P.ZerosLike().shard(strategy2)
  425. self.matmul2 = P.MatMul().shard(strategy1)
  426. def construct(self, x, y, b):
  427. out = self.matmul(x, y)
  428. out = self.zeroslike(out)
  429. out = self.matmul2(out, b)
  430. return out
  431. context.set_auto_parallel_context(device_num=8, global_rank=0)
  432. strategy1 = ((2, 2), (2, 2))
  433. strategy2 = ((4, 2),)
  434. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  435. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  436. x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
  437. y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
  438. b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
  439. compile_net(net, x, y, b)
  440. def test_matmul_oneslike():
  441. class Net(nn.Cell):
  442. def __init__(self, strategy1, strategy2):
  443. super().__init__()
  444. self.matmul = P.MatMul().shard(strategy1)
  445. self.oneslike = P.OnesLike().shard(strategy2)
  446. self.matmul2 = P.MatMul().shard(strategy1)
  447. def construct(self, x, y, b):
  448. out = self.matmul(x, y)
  449. out = self.oneslike(out)
  450. out = self.matmul2(out, b)
  451. return out
  452. context.set_auto_parallel_context(device_num=8, global_rank=0)
  453. strategy1 = ((2, 2), (2, 2))
  454. strategy2 = ((4, 2),)
  455. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  456. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  457. x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
  458. y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
  459. b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
  460. compile_net(net, x, y, b)
  461. def test_matmul_BesselI0e():
  462. class Net(nn.Cell):
  463. def __init__(self, strategy1, strategy2):
  464. super().__init__()
  465. self.matmul = P.MatMul().shard(strategy1)
  466. self.BesselI0e = P.BesselI0e().shard(strategy2)
  467. self.matmul2 = P.MatMul().shard(strategy1)
  468. def construct(self, x, y, b):
  469. out = self.matmul(x, y)
  470. out = self.BesselI0e(out)
  471. out = self.matmul2(out, b)
  472. return out
  473. context.set_auto_parallel_context(device_num=8, global_rank=0)
  474. strategy1 = ((2, 2), (2, 2))
  475. strategy2 = ((4, 2),)
  476. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  477. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  478. x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
  479. y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
  480. b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
  481. compile_net(net, x, y, b)
  482. def test_matmul_BesselI1e():
  483. class Net(nn.Cell):
  484. def __init__(self, strategy1, strategy2):
  485. super().__init__()
  486. self.matmul = P.MatMul().shard(strategy1)
  487. self.BesselI1e = P.BesselI1e().shard(strategy2)
  488. self.matmul2 = P.MatMul().shard(strategy1)
  489. def construct(self, x, y, b):
  490. out = self.matmul(x, y)
  491. out = self.BesselI1e(out)
  492. out = self.matmul2(out, b)
  493. return out
  494. context.set_auto_parallel_context(device_num=8, global_rank=0)
  495. strategy1 = ((2, 2), (2, 2))
  496. strategy2 = ((4, 2),)
  497. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  498. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  499. x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
  500. y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
  501. b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
  502. compile_net(net, x, y, b)
  503. def test_matmul_ceil():
  504. class Net(nn.Cell):
  505. def __init__(self, strategy1, strategy2):
  506. super().__init__()
  507. self.matmul = P.MatMul().shard(strategy1)
  508. self.Ceil = P.Ceil().shard(strategy2)
  509. self.matmul2 = P.MatMul().shard(strategy1)
  510. def construct(self, x, y, b):
  511. out = self.matmul(x, y)
  512. out = self.Ceil(out)
  513. out = self.matmul2(out, b)
  514. return out
  515. context.set_auto_parallel_context(device_num=8, global_rank=0)
  516. strategy1 = ((2, 2), (2, 2))
  517. strategy2 = ((4, 2),)
  518. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  519. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  520. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  521. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  522. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  523. compile_net(net, x, y, b)
  524. def test_matmul_atan():
  525. class Net(nn.Cell):
  526. def __init__(self, strategy1, strategy2):
  527. super().__init__()
  528. self.matmul = P.MatMul().shard(strategy1)
  529. self.atan = P.Atan().shard(strategy2)
  530. self.matmul2 = P.MatMul().shard(strategy1)
  531. def construct(self, x, y, b):
  532. out = self.matmul(x, y)
  533. out = self.atan(out)
  534. out = self.matmul2(out, b)
  535. return out
  536. context.set_auto_parallel_context(device_num=8, global_rank=0)
  537. strategy1 = ((2, 2), (2, 2))
  538. strategy2 = ((4, 2),)
  539. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  540. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  541. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  542. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  543. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  544. compile_net(net, x, y, b)
  545. def test_matmul_Atanh():
  546. class Net(nn.Cell):
  547. def __init__(self, strategy1, strategy2):
  548. super().__init__()
  549. self.matmul = P.MatMul().shard(strategy1)
  550. self.atanh = P.Atanh().shard(strategy2)
  551. self.matmul2 = P.MatMul().shard(strategy1)
  552. def construct(self, x, y, b):
  553. out = self.matmul(x, y)
  554. out = self.atanh(out)
  555. out = self.matmul2(out, b)
  556. return out
  557. context.set_auto_parallel_context(device_num=8, global_rank=0)
  558. strategy1 = ((2, 2), (2, 2))
  559. strategy2 = ((4, 2),)
  560. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  561. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  562. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  563. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  564. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  565. compile_net(net, x, y, b)
  566. def test_matmul_asin():
  567. class Net(nn.Cell):
  568. def __init__(self, strategy1, strategy2):
  569. super().__init__()
  570. self.matmul = P.MatMul().shard(strategy1)
  571. self.asin = P.Asin().shard(strategy2)
  572. self.matmul2 = P.MatMul().shard(strategy1)
  573. def construct(self, x, y, b):
  574. out = self.matmul(x, y)
  575. out = self.asin(out)
  576. out = self.matmul2(out, b)
  577. return out
  578. context.set_auto_parallel_context(device_num=8, global_rank=0)
  579. strategy1 = ((2, 2), (2, 2))
  580. strategy2 = ((4, 2),)
  581. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  582. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  583. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  584. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  585. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  586. compile_net(net, x, y, b)
  587. def test_matmul_asinh():
  588. class Net(nn.Cell):
  589. def __init__(self, strategy1, strategy2):
  590. super().__init__()
  591. self.matmul = P.MatMul().shard(strategy1)
  592. self.asinh = P.Asinh().shard(strategy2)
  593. self.matmul2 = P.MatMul().shard(strategy1)
  594. def construct(self, x, y, b):
  595. out = self.matmul(x, y)
  596. out = self.asinh(out)
  597. out = self.matmul2(out, b)
  598. return out
  599. context.set_auto_parallel_context(device_num=8, global_rank=0)
  600. strategy1 = ((2, 2), (2, 2))
  601. strategy2 = ((4, 2),)
  602. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  603. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  604. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  605. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  606. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  607. compile_net(net, x, y, b)
  608. def test_matmul_acosh():
  609. class Net(nn.Cell):
  610. def __init__(self, strategy1, strategy2):
  611. super().__init__()
  612. self.matmul = P.MatMul().shard(strategy1)
  613. self.acosh = P.Acosh().shard(strategy2)
  614. self.matmul2 = P.MatMul().shard(strategy1)
  615. def construct(self, x, y, b):
  616. out = self.matmul(x, y)
  617. out = self.acosh(out)
  618. out = self.matmul2(out, b)
  619. return out
  620. context.set_auto_parallel_context(device_num=8, global_rank=0)
  621. strategy1 = ((2, 2), (2, 2))
  622. strategy2 = ((4, 2),)
  623. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  624. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  625. x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
  626. y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
  627. b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
  628. compile_net(net, x, y, b)
  629. def test_matmul_logical_not():
  630. class Net(nn.Cell):
  631. def __init__(self, strategy1, strategy2, strategy3):
  632. super().__init__()
  633. self.matmul = P.MatMul().shard(strategy1)
  634. self.logicalnot = P.LogicalNot().shard(strategy2)
  635. self.equal = P.Equal().shard(strategy3)
  636. def construct(self, x, y, b):
  637. out = self.matmul(x, y)
  638. out = self.equal(out, b)
  639. out = self.logicalnot(out)
  640. return out
  641. context.set_auto_parallel_context(device_num=8, global_rank=0)
  642. strategy1 = ((2, 2), (2, 2))
  643. strategy2 = ((4, 2),)
  644. strategy3 = ((4, 2), (4, 2))
  645. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  646. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  647. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  648. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  649. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  650. compile_net(net, x, y, b)
  651. def test_matmul_cast():
  652. class Net(nn.Cell):
  653. def __init__(self, strategy1, strategy2, strategy3):
  654. super().__init__()
  655. self.matmul = P.MatMul().shard(strategy1)
  656. self.cast = P.Cast().shard(strategy2)
  657. self.matmul2 = P.MatMul().shard(strategy3)
  658. def construct(self, x, y, b):
  659. out = self.matmul(x, y)
  660. b = self.cast(b, ms.float32)
  661. out = self.matmul2(out, b)
  662. return out
  663. context.set_auto_parallel_context(device_num=8, global_rank=0)
  664. strategy1 = ((2, 2), (2, 2))
  665. strategy2 = ((4, 2),)
  666. strategy3 = ((1, 4), (4, 2))
  667. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  668. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  669. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  670. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  671. b = Tensor(np.ones([64, 64]), dtype=ms.int32)
  672. compile_net(net, x, y, b)
  673. def test_gradient_fp32_sync():
  674. class Net(nn.Cell):
  675. def __init__(self, strategy1):
  676. super().__init__()
  677. self.matmul = P.MatMul().shard(strategy1)
  678. self.cast = P.Cast()
  679. def construct(self, x, y, b):
  680. out = self.matmul(x, y)
  681. b = self.cast(b, ms.float32)
  682. out = self.matmul(out, b)
  683. return out
  684. context.set_auto_parallel_context(device_num=8, global_rank=0, gradient_fp32_sync=True)
  685. strategy1 = ((2, 2), (2, 2))
  686. net = GradWrap(NetWithLoss(Net(strategy1)))
  687. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  688. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  689. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  690. b = Tensor(np.ones([64, 64]), dtype=ms.float16)
  691. compile_net(net, x, y, b)
  692. def test_gradient_fp32_sync1():
  693. class Net(nn.Cell):
  694. def __init__(self, strategy1):
  695. super().__init__()
  696. self.matmul = P.MatMul().shard(strategy1)
  697. self.cast = P.Cast()
  698. def construct(self, x, y, b):
  699. out = self.matmul(x, y)
  700. b = self.cast(b, ms.float16)
  701. out = self.matmul(out, b)
  702. return out
  703. context.set_auto_parallel_context(device_num=8, global_rank=0, gradient_fp32_sync=True)
  704. strategy1 = ((2, 2), (2, 2))
  705. net = GradWrap(NetWithLoss(Net(strategy1)))
  706. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  707. x = Tensor(np.ones([128, 32]), dtype=ms.float16)
  708. y = Tensor(np.ones([32, 64]), dtype=ms.float16)
  709. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  710. compile_net(net, x, y, b)
  711. def test_gradient_fp32_sync2():
  712. class Net(nn.Cell):
  713. def __init__(self, strategy1):
  714. super().__init__()
  715. self.matmul = P.MatMul().shard(strategy1)
  716. self.cast = P.Cast()
  717. def construct(self, x, y, b):
  718. out = self.matmul(x, y)
  719. b = self.cast(b, ms.float16)
  720. out = self.matmul(out, b)
  721. return out
  722. context.set_auto_parallel_context(device_num=8, global_rank=0, gradient_fp32_sync=False)
  723. strategy1 = ((2, 2), (2, 2))
  724. net = GradWrap(NetWithLoss(Net(strategy1)))
  725. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  726. x = Tensor(np.ones([128, 32]), dtype=ms.float16)
  727. y = Tensor(np.ones([32, 64]), dtype=ms.float16)
  728. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  729. compile_net(net, x, y, b)
  730. def test_gradient_fp32_sync3():
  731. class Net(nn.Cell):
  732. def __init__(self, strategy1):
  733. super().__init__()
  734. self.matmul = P.MatMul().shard(strategy1)
  735. self.cast = P.Cast()
  736. def construct(self, x, y, b):
  737. out = self.matmul(x, y)
  738. b = self.cast(b, ms.float16)
  739. out = self.matmul(out, b)
  740. return out
  741. context.set_auto_parallel_context(device_num=8, global_rank=0)
  742. strategy1 = ((2, 2), (2, 2))
  743. net = GradWrap(NetWithLoss(Net(strategy1)))
  744. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  745. x = Tensor(np.ones([128, 32]), dtype=ms.float16)
  746. y = Tensor(np.ones([32, 64]), dtype=ms.float16)
  747. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  748. compile_net(net, x, y, b)
  749. def test_mul_two_cast():
  750. class Net(nn.Cell):
  751. def __init__(self, strategy1, strategy2, strategy3):
  752. super().__init__()
  753. self.mul = P.Mul().shard(strategy1)
  754. self.mul2 = P.Mul().shard(strategy2)
  755. self.cast = P.Cast().shard(strategy3)
  756. self.cast2 = P.Cast().shard(strategy3)
  757. def construct(self, x, y, b):
  758. out = self.mul(x, y)
  759. out = self.mul2(out, b)
  760. out = self.cast(out, ms.int32)
  761. out = self.cast2(out, ms.bool_)
  762. return out
  763. context.set_auto_parallel_context(device_num=8, global_rank=0)
  764. strategy1 = ((2, 2), (2, 2))
  765. strategy2 = ((8, 1), (8, 1))
  766. strategy3 = ((8, 1),)
  767. net = GradWrap(Net(strategy1, strategy2, strategy3))
  768. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  769. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  770. y = Tensor(np.ones([128, 32]), dtype=ms.float32)
  771. b = Tensor(np.ones([128, 32]), dtype=ms.float32)
  772. compile_net(net, x, y, b)