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 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 compile(net, x, y, b):
  38. net.set_auto_parallel()
  39. _executor.compile(net, x, y, b)
  40. def test_matmul_equal():
  41. class Net(nn.Cell):
  42. def __init__(self, strategy1, strategy2):
  43. super().__init__()
  44. self.matmul = P.MatMul().set_strategy(strategy1)
  45. self.equal = P.Equal().set_strategy(strategy2)
  46. def construct(self, x, y, b):
  47. out = self.matmul(x, y)
  48. out = self.equal(out, b)
  49. return out
  50. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  51. strategy1 = ((2, 2), (2, 2))
  52. strategy2 = ((4, 2), (4, 2))
  53. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  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([128, 64]), dtype=ms.float32)
  57. compile(net, x, y, b)
  58. def test_matmul_not_equal():
  59. class Net(nn.Cell):
  60. def __init__(self, strategy1, strategy2):
  61. super().__init__()
  62. self.matmul = P.MatMul().set_strategy(strategy1)
  63. self.notequal = P.NotEqual().set_strategy(strategy2)
  64. def construct(self, x, y, b):
  65. out = self.matmul(x, y)
  66. out = self.notequal(out, b)
  67. return out
  68. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  69. strategy1 = ((2, 2), (2, 2))
  70. strategy2 = ((4, 2), (4, 2))
  71. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  72. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  73. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  74. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  75. compile(net, x, y, b)
  76. def test_matmul_not_equal_repeated_calculation():
  77. class Net(nn.Cell):
  78. def __init__(self, strategy1, strategy2):
  79. super().__init__()
  80. self.matmul = P.MatMul().set_strategy(strategy1)
  81. self.notequal = P.NotEqual().set_strategy(strategy2)
  82. def construct(self, x, y, b):
  83. out = self.matmul(x, y)
  84. out = self.notequal(out, b)
  85. return out
  86. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  87. strategy1 = ((2, 2), (2, 2))
  88. strategy2 = ((4, 1), (4, 1))
  89. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  90. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  91. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  92. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  93. compile(net, x, y, b)
  94. def test_matmul_maximum():
  95. class Net(nn.Cell):
  96. def __init__(self, strategy1, strategy2):
  97. super().__init__()
  98. self.matmul = P.MatMul().set_strategy(strategy1)
  99. self.maximum = P.Maximum().set_strategy(strategy2)
  100. def construct(self, x, y, b):
  101. out = self.matmul(x, y)
  102. out = self.maximum(out, b)
  103. return out
  104. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  105. strategy1 = ((2, 2), (2, 2))
  106. strategy2 = ((4, 2), (4, 2))
  107. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  108. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  109. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  110. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  111. compile(net, x, y, b)
  112. def test_matmul_maximum_broadcast():
  113. class Net(nn.Cell):
  114. def __init__(self, strategy1, strategy2):
  115. super().__init__()
  116. self.matmul = P.MatMul().set_strategy(strategy1)
  117. self.maximum = P.Maximum().set_strategy(strategy2)
  118. def construct(self, x, y, b):
  119. out = self.matmul(x, y)
  120. out = self.maximum(out, b)
  121. return out
  122. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  123. strategy1 = ((2, 2), (2, 2))
  124. strategy2 = ((4, 2), (2, ))
  125. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  126. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  127. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  128. b = Tensor(np.ones([64]), dtype=ms.float32)
  129. compile(net, x, y, b)
  130. def test_matmul_maximum_broadcast2():
  131. class Net(nn.Cell):
  132. def __init__(self, strategy1, strategy2):
  133. super().__init__()
  134. self.matmul = P.MatMul().set_strategy(strategy1)
  135. self.maximum = P.Maximum().set_strategy(strategy2)
  136. def construct(self, x, y, b):
  137. out = self.matmul(x, y)
  138. out = self.maximum(out, b)
  139. return out
  140. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  141. strategy1 = ((2, 4), (4, 1))
  142. strategy2 = ((4, 1), (1, 2))
  143. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  144. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  145. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  146. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  147. compile(net, x, y, b)
  148. def test_matmul_minimum():
  149. class Net(nn.Cell):
  150. def __init__(self, strategy1, strategy2):
  151. super().__init__()
  152. self.matmul = P.MatMul().set_strategy(strategy1)
  153. self.minimum = P.Minimum().set_strategy(strategy2)
  154. def construct(self, x, y, b):
  155. out = self.matmul(x, y)
  156. out = self.minimum(out, b)
  157. return out
  158. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  159. strategy1 = ((2, 2), (2, 2))
  160. strategy2 = ((4, 2), (4, 2))
  161. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  162. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  163. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  164. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  165. compile(net, x, y, b)
  166. def test_matmul_minimum_broadcast():
  167. class Net(nn.Cell):
  168. def __init__(self, strategy1, strategy2):
  169. super().__init__()
  170. self.matmul = P.MatMul().set_strategy(strategy1)
  171. self.minimum = P.Maximum().set_strategy(strategy2)
  172. def construct(self, x, y, b):
  173. out = self.matmul(x, y)
  174. out = self.minimum(out, b)
  175. return out
  176. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  177. strategy1 = ((2, 2), (2, 2))
  178. strategy2 = ((4, 2), (2, ))
  179. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  180. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  181. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  182. b = Tensor(np.ones([64]), dtype=ms.float32)
  183. compile(net, x, y, b)
  184. def test_matmul_minimum_broadcast2():
  185. class Net(nn.Cell):
  186. def __init__(self, strategy1, strategy2):
  187. super().__init__()
  188. self.matmul = P.MatMul().set_strategy(strategy1)
  189. self.minimum = P.Minimum().set_strategy(strategy2)
  190. def construct(self, x, y, b):
  191. out = self.matmul(x, y)
  192. out = self.minimum(out, b)
  193. return out
  194. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  195. strategy1 = ((2, 4), (4, 1))
  196. strategy2 = ((4, 1), (1, 2))
  197. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  198. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  199. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  200. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  201. compile(net, x, y, b)
  202. def test_matmul_minimum_auto_parallel():
  203. class Net(nn.Cell):
  204. def __init__(self):
  205. super().__init__()
  206. self.matmul = P.MatMul()
  207. self.minimum = P.Minimum()
  208. def construct(self, x, y, b):
  209. out = self.matmul(x, y)
  210. out = self.minimum(out, b)
  211. return out
  212. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="auto_parallel")
  213. net = GradWrap(NetWithLoss(Net()))
  214. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  215. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  216. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  217. compile(net, x, y, b)