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 11 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y, b):
  29. predict = self.network(x, y, b)
  30. return self.loss(predict)
  31. class GradWrap(nn.Cell):
  32. def __init__(self, network):
  33. super(GradWrap, self).__init__()
  34. self.network = network
  35. def construct(self, x, y, b):
  36. return C.grad_all(self.network)(x, y, b)
  37. def compile_net(net, x, y, b):
  38. net.set_auto_parallel()
  39. _executor.compile(net, x, y, b)
  40. def test_matmul_pow():
  41. class Net(nn.Cell):
  42. def __init__(self, strategy1, strategy2):
  43. super().__init__()
  44. self.matmul = P.MatMul().set_strategy(strategy1)
  45. self.pow = P.Pow().set_strategy(strategy2)
  46. self.matmul2 = P.MatMul().set_strategy(strategy1)
  47. def construct(self, x, y, b):
  48. out = self.matmul(x, y)
  49. out = self.pow(out, 2.0)
  50. out = self.matmul2(out, b)
  51. return out
  52. context.set_auto_parallel_context(device_num=8, global_rank=0)
  53. strategy1 = ((2, 2), (2, 2))
  54. strategy2 = ((4, 2), ())
  55. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  56. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  57. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  58. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  59. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  60. compile_net(net, x, y, b)
  61. def test_matmul_exp():
  62. class Net(nn.Cell):
  63. def __init__(self, strategy1, strategy2):
  64. super().__init__()
  65. self.matmul = P.MatMul().set_strategy(strategy1)
  66. self.exp = P.Exp().set_strategy(strategy2)
  67. self.matmul2 = P.MatMul().set_strategy(strategy1)
  68. def construct(self, x, y, b):
  69. out = self.matmul(x, y)
  70. out = self.exp(out)
  71. out = self.matmul2(out, b)
  72. return out
  73. context.set_auto_parallel_context(device_num=8, global_rank=0)
  74. strategy1 = ((2, 2), (2, 2))
  75. strategy2 = ((4, 2),)
  76. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  77. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  78. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  79. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  80. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  81. compile_net(net, x, y, b)
  82. def test_matmul_log():
  83. class Net(nn.Cell):
  84. def __init__(self, strategy1, strategy2):
  85. super().__init__()
  86. self.matmul = P.MatMul().set_strategy(strategy1)
  87. self.log = P.Log().set_strategy(strategy2)
  88. self.matmul2 = P.MatMul().set_strategy(strategy1)
  89. def construct(self, x, y, b):
  90. out = self.matmul(x, y)
  91. out = self.log(out)
  92. out = self.matmul2(out, b)
  93. return out
  94. context.set_auto_parallel_context(device_num=8, global_rank=0)
  95. strategy1 = ((2, 2), (2, 2))
  96. strategy2 = ((4, 2),)
  97. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  98. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  99. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  100. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  101. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  102. compile_net(net, x, y, b)
  103. def test_matmul_logical_not():
  104. class Net(nn.Cell):
  105. def __init__(self, strategy1, strategy2, strategy3):
  106. super().__init__()
  107. self.matmul = P.MatMul().set_strategy(strategy1)
  108. self.logicalnot = P.LogicalNot().set_strategy(strategy2)
  109. self.equal = P.Equal().set_strategy(strategy3)
  110. def construct(self, x, y, b):
  111. out = self.matmul(x, y)
  112. out = self.equal(out, b)
  113. out = self.logicalnot(out)
  114. return out
  115. context.set_auto_parallel_context(device_num=8, global_rank=0)
  116. strategy1 = ((2, 2), (2, 2))
  117. strategy2 = ((4, 2),)
  118. strategy3 = ((4, 2), (4, 2))
  119. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  120. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  121. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  122. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  123. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  124. compile_net(net, x, y, b)
  125. def test_matmul_cast():
  126. class Net(nn.Cell):
  127. def __init__(self, strategy1, strategy2, strategy3):
  128. super().__init__()
  129. self.matmul = P.MatMul().set_strategy(strategy1)
  130. self.cast = P.Cast().set_strategy(strategy2)
  131. self.matmul2 = P.MatMul().set_strategy(strategy3)
  132. def construct(self, x, y, b):
  133. out = self.matmul(x, y)
  134. b = self.cast(b, ms.float32)
  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. strategy3 = ((1, 4), (4, 2))
  141. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
  142. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  143. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  144. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  145. b = Tensor(np.ones([64, 64]), dtype=ms.int32)
  146. compile_net(net, x, y, b)
  147. def test_cast_before_mirror():
  148. class Net(nn.Cell):
  149. def __init__(self, strategy1):
  150. super().__init__()
  151. self.matmul = P.MatMul().set_strategy(strategy1)
  152. self.cast = P.Cast()
  153. def construct(self, x, y, b):
  154. out = self.matmul(x, y)
  155. b = self.cast(b, ms.float32)
  156. out = self.matmul(out, b)
  157. return out
  158. context.set_auto_parallel_context(device_num=8, global_rank=0, cast_before_mirror=True)
  159. strategy1 = ((2, 2), (2, 2))
  160. net = GradWrap(NetWithLoss(Net(strategy1)))
  161. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  162. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  163. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  164. b = Tensor(np.ones([64, 64]), dtype=ms.float16)
  165. compile_net(net, x, y, b)
  166. def test_cast_before_mirror1():
  167. class Net(nn.Cell):
  168. def __init__(self, strategy1):
  169. super().__init__()
  170. self.matmul = P.MatMul().set_strategy(strategy1)
  171. self.cast = P.Cast()
  172. def construct(self, x, y, b):
  173. out = self.matmul(x, y)
  174. b = self.cast(b, ms.float16)
  175. out = self.matmul(out, b)
  176. return out
  177. context.set_auto_parallel_context(device_num=8, global_rank=0, cast_before_mirror=True)
  178. strategy1 = ((2, 2), (2, 2))
  179. net = GradWrap(NetWithLoss(Net(strategy1)))
  180. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  181. x = Tensor(np.ones([128, 32]), dtype=ms.float16)
  182. y = Tensor(np.ones([32, 64]), dtype=ms.float16)
  183. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  184. compile_net(net, x, y, b)
  185. def test_cast_before_mirror2():
  186. class Net(nn.Cell):
  187. def __init__(self, strategy1):
  188. super().__init__()
  189. self.matmul = P.MatMul().set_strategy(strategy1)
  190. self.cast = P.Cast()
  191. def construct(self, x, y, b):
  192. out = self.matmul(x, y)
  193. b = self.cast(b, ms.float16)
  194. out = self.matmul(out, b)
  195. return out
  196. context.set_auto_parallel_context(device_num=8, global_rank=0, cast_before_mirror=False)
  197. strategy1 = ((2, 2), (2, 2))
  198. net = GradWrap(NetWithLoss(Net(strategy1)))
  199. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  200. x = Tensor(np.ones([128, 32]), dtype=ms.float16)
  201. y = Tensor(np.ones([32, 64]), dtype=ms.float16)
  202. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  203. compile_net(net, x, y, b)
  204. def test_cast_before_mirror3():
  205. class Net(nn.Cell):
  206. def __init__(self, strategy1):
  207. super().__init__()
  208. self.matmul = P.MatMul().set_strategy(strategy1)
  209. self.cast = P.Cast()
  210. def construct(self, x, y, b):
  211. out = self.matmul(x, y)
  212. b = self.cast(b, ms.float16)
  213. out = self.matmul(out, b)
  214. return out
  215. context.set_auto_parallel_context(device_num=8, global_rank=0)
  216. strategy1 = ((2, 2), (2, 2))
  217. net = GradWrap(NetWithLoss(Net(strategy1)))
  218. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  219. x = Tensor(np.ones([128, 32]), dtype=ms.float16)
  220. y = Tensor(np.ones([32, 64]), dtype=ms.float16)
  221. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  222. compile_net(net, x, y, b)
  223. def test_mul_two_cast():
  224. class Net(nn.Cell):
  225. def __init__(self, strategy1, strategy2, strategy3):
  226. super().__init__()
  227. self.mul = P.Mul().set_strategy(strategy1)
  228. self.mul2 = P.Mul().set_strategy(strategy2)
  229. self.cast = P.Cast().set_strategy(strategy3)
  230. self.cast2 = P.Cast().set_strategy(strategy3)
  231. def construct(self, x, y, b):
  232. out = self.mul(x, y)
  233. out = self.mul2(out, b)
  234. out = self.cast(out, ms.int32)
  235. out = self.cast2(out, ms.bool_)
  236. return out
  237. context.set_auto_parallel_context(device_num=8, global_rank=0)
  238. strategy1 = ((2, 2), (2, 2))
  239. strategy2 = ((8, 1), (8, 1))
  240. strategy3 = ((8, 1),)
  241. net = GradWrap(Net(strategy1, strategy2, strategy3))
  242. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  243. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  244. y = Tensor(np.ones([128, 32]), dtype=ms.float32)
  245. b = Tensor(np.ones([128, 32]), dtype=ms.float32)
  246. compile_net(net, x, y, b)