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_comparison_function_info.py 14 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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_equal():
  42. class Net(nn.Cell):
  43. def __init__(self, strategy1, strategy2):
  44. super().__init__()
  45. self.matmul = P.MatMul().shard(strategy1)
  46. self.equal = P.Equal().shard(strategy2)
  47. def construct(self, x, y, b):
  48. out = self.matmul(x, y)
  49. out = self.equal(out, b)
  50. return out
  51. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  52. strategy1 = ((2, 2), (2, 2))
  53. strategy2 = ((4, 2), (4, 2))
  54. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  55. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  56. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  57. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  58. compile_net(net, x, y, b)
  59. def test_matmul_not_equal():
  60. class Net(nn.Cell):
  61. def __init__(self, strategy1, strategy2):
  62. super().__init__()
  63. self.matmul = P.MatMul().shard(strategy1)
  64. self.notequal = P.NotEqual().shard(strategy2)
  65. def construct(self, x, y, b):
  66. out = self.matmul(x, y)
  67. out = self.notequal(out, b)
  68. return out
  69. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  70. strategy1 = ((2, 2), (2, 2))
  71. strategy2 = ((4, 2), (4, 2))
  72. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  73. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  74. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  75. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  76. compile_net(net, x, y, b)
  77. def test_matmul_approximateEqual():
  78. class Net(nn.Cell):
  79. def __init__(self, strategy1, strategy2):
  80. super().__init__()
  81. self.matmul = P.MatMul().shard(strategy1)
  82. self.approximateEqual = P.ApproximateEqual(tolerance=0.5).shard(strategy2)
  83. def construct(self, x, y, b):
  84. out = self.matmul(x, y)
  85. out = self.approximateEqual(out, b)
  86. return out
  87. context.set_auto_parallel_context(device_num=8, global_rank=0)
  88. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  89. strategy1 = ((2, 2), (2, 2))
  90. strategy2 = ((4, 2), (4, 2))
  91. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  92. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  93. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  94. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  95. compile_net(net, x, y, b)
  96. def test_matmul_greater():
  97. class Net(nn.Cell):
  98. def __init__(self, strategy1, strategy2):
  99. super().__init__()
  100. self.matmul = P.MatMul().shard(strategy1)
  101. self.greater = P.Greater().shard(strategy2)
  102. def construct(self, x, y, b):
  103. out = self.matmul(x, y)
  104. out = self.greater(out, b)
  105. return out
  106. context.set_auto_parallel_context(device_num=8, global_rank=0)
  107. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  108. strategy1 = ((2, 2), (2, 2))
  109. strategy2 = ((4, 2), (4, 2))
  110. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  111. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  112. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  113. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  114. compile_net(net, x, y, b)
  115. def test_matmul_greaterEqual():
  116. class Net(nn.Cell):
  117. def __init__(self, strategy1, strategy2):
  118. super().__init__()
  119. self.matmul = P.MatMul().shard(strategy1)
  120. self.greaterEqual = P.GreaterEqual().shard(strategy2)
  121. def construct(self, x, y, b):
  122. out = self.matmul(x, y)
  123. out = self.greaterEqual(out, b)
  124. return out
  125. context.set_auto_parallel_context(device_num=8, global_rank=0)
  126. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  127. strategy1 = ((2, 2), (2, 2))
  128. strategy2 = ((4, 2), (4, 2))
  129. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  130. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  131. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  132. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  133. compile_net(net, x, y, b)
  134. def test_matmul_less():
  135. class Net(nn.Cell):
  136. def __init__(self, strategy1, strategy2):
  137. super().__init__()
  138. self.matmul = P.MatMul().shard(strategy1)
  139. self.less = P.Less().shard(strategy2)
  140. def construct(self, x, y, b):
  141. out = self.matmul(x, y)
  142. out = self.less(out, b)
  143. return out
  144. context.set_auto_parallel_context(device_num=8, global_rank=0)
  145. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  146. strategy1 = ((2, 2), (2, 2))
  147. strategy2 = ((4, 2), (4, 2))
  148. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  149. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  150. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  151. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  152. compile_net(net, x, y, b)
  153. def test_matmul_lessEqual():
  154. class Net(nn.Cell):
  155. def __init__(self, strategy1, strategy2):
  156. super().__init__()
  157. self.matmul = P.MatMul().shard(strategy1)
  158. self.lessEqual = P.LessEqual().shard(strategy2)
  159. def construct(self, x, y, b):
  160. out = self.matmul(x, y)
  161. out = self.lessEqual(out, b)
  162. return out
  163. context.set_auto_parallel_context(device_num=8, global_rank=0)
  164. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  165. strategy1 = ((2, 2), (2, 2))
  166. strategy2 = ((4, 2), (4, 2))
  167. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  168. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  169. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  170. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  171. compile_net(net, x, y, b)
  172. def test_matmul_not_equal_repeated_calculation():
  173. class Net(nn.Cell):
  174. def __init__(self, strategy1, strategy2):
  175. super().__init__()
  176. self.matmul = P.MatMul().shard(strategy1)
  177. self.notequal = P.NotEqual().shard(strategy2)
  178. def construct(self, x, y, b):
  179. out = self.matmul(x, y)
  180. out = self.notequal(out, b)
  181. return out
  182. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  183. strategy1 = ((2, 2), (2, 2))
  184. strategy2 = ((4, 1), (4, 1))
  185. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  186. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  187. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  188. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  189. compile_net(net, x, y, b)
  190. def test_matmul_maximum():
  191. class Net(nn.Cell):
  192. def __init__(self, strategy1, strategy2):
  193. super().__init__()
  194. self.matmul = P.MatMul().shard(strategy1)
  195. self.maximum = P.Maximum().shard(strategy2)
  196. def construct(self, x, y, b):
  197. out = self.matmul(x, y)
  198. out = self.maximum(out, b)
  199. return out
  200. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  201. strategy1 = ((2, 2), (2, 2))
  202. strategy2 = ((4, 2), (4, 2))
  203. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  204. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  205. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  206. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  207. compile_net(net, x, y, b)
  208. def test_matmul_maximum_broadcast():
  209. class Net(nn.Cell):
  210. def __init__(self, strategy1, strategy2):
  211. super().__init__()
  212. self.matmul = P.MatMul().shard(strategy1)
  213. self.maximum = P.Maximum().shard(strategy2)
  214. def construct(self, x, y, b):
  215. out = self.matmul(x, y)
  216. out = self.maximum(out, b)
  217. return out
  218. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  219. strategy1 = ((2, 2), (2, 2))
  220. strategy2 = ((4, 2), (2,))
  221. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  222. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  223. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  224. b = Tensor(np.ones([64]), dtype=ms.float32)
  225. compile_net(net, x, y, b)
  226. def test_matmul_maximum_broadcast2():
  227. class Net(nn.Cell):
  228. def __init__(self, strategy1, strategy2):
  229. super().__init__()
  230. self.matmul = P.MatMul().shard(strategy1)
  231. self.maximum = P.Maximum().shard(strategy2)
  232. def construct(self, x, y, b):
  233. out = self.matmul(x, y)
  234. out = self.maximum(out, b)
  235. return out
  236. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  237. strategy1 = ((2, 4), (4, 1))
  238. strategy2 = ((4, 1), (1, 2))
  239. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  240. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  241. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  242. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  243. compile_net(net, x, y, b)
  244. def test_matmul_minimum():
  245. class Net(nn.Cell):
  246. def __init__(self, strategy1, strategy2):
  247. super().__init__()
  248. self.matmul = P.MatMul().shard(strategy1)
  249. self.minimum = P.Minimum().shard(strategy2)
  250. def construct(self, x, y, b):
  251. out = self.matmul(x, y)
  252. out = self.minimum(out, b)
  253. return out
  254. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  255. strategy1 = ((2, 2), (2, 2))
  256. strategy2 = ((4, 2), (4, 2))
  257. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  258. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  259. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  260. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  261. compile_net(net, x, y, b)
  262. def test_matmul_minimum_broadcast():
  263. class Net(nn.Cell):
  264. def __init__(self, strategy1, strategy2):
  265. super().__init__()
  266. self.matmul = P.MatMul().shard(strategy1)
  267. self.minimum = P.Maximum().shard(strategy2)
  268. def construct(self, x, y, b):
  269. out = self.matmul(x, y)
  270. out = self.minimum(out, b)
  271. return out
  272. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  273. strategy1 = ((2, 2), (2, 2))
  274. strategy2 = ((4, 2), (2,))
  275. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  276. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  277. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  278. b = Tensor(np.ones([64]), dtype=ms.float32)
  279. compile_net(net, x, y, b)
  280. def test_matmul_minimum_broadcast2():
  281. class Net(nn.Cell):
  282. def __init__(self, strategy1, strategy2):
  283. super().__init__()
  284. self.matmul = P.MatMul().shard(strategy1)
  285. self.minimum = P.Minimum().shard(strategy2)
  286. def construct(self, x, y, b):
  287. out = self.matmul(x, y)
  288. out = self.minimum(out, b)
  289. return out
  290. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  291. strategy1 = ((2, 4), (4, 1))
  292. strategy2 = ((4, 1), (1, 2))
  293. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  294. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  295. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  296. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  297. compile_net(net, x, y, b)
  298. def test_matmul_minimum_auto_parallel():
  299. class Net(nn.Cell):
  300. def __init__(self):
  301. super().__init__()
  302. self.matmul = P.MatMul()
  303. self.minimum = P.Minimum()
  304. def construct(self, x, y, b):
  305. out = self.matmul(x, y)
  306. out = self.minimum(out, b)
  307. return out
  308. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  309. net = GradWrap(NetWithLoss(Net()))
  310. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  311. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  312. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  313. compile_net(net, x, y, b)