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

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