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