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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_equal():
  38. class Net(nn.Cell):
  39. def __init__(self, strategy1, strategy2):
  40. super().__init__()
  41. self.matmul = P.MatMul().set_strategy(strategy1)
  42. self.equal = P.Equal().set_strategy(strategy2)
  43. def construct(self, x, y, b):
  44. out = self.matmul(x, y)
  45. out = self.equal(out, b)
  46. return out
  47. context.set_auto_parallel_context(device_num=8, global_rank=0)
  48. strategy1 = ((2, 2), (2, 2))
  49. strategy2 = ((4, 2), (4, 2))
  50. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  51. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  52. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  53. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  54. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  55. _executor.compile(net, x, y, b)
  56. def test_matmul_not_equal():
  57. class Net(nn.Cell):
  58. def __init__(self, strategy1, strategy2):
  59. super().__init__()
  60. self.matmul = P.MatMul().set_strategy(strategy1)
  61. self.notequal = P.NotEqual().set_strategy(strategy2)
  62. def construct(self, x, y, b):
  63. out = self.matmul(x, y)
  64. out = self.notequal(out, b)
  65. return out
  66. context.set_auto_parallel_context(device_num=8, global_rank=0)
  67. strategy1 = ((2, 2), (2, 2))
  68. strategy2 = ((4, 2), (4, 2))
  69. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  70. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  71. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  72. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  73. b = Tensor(np.ones([128, 64]), dtype=ms.float32)
  74. _executor.compile(net, x, y, b)
  75. def test_matmul_not_equal_repeated_calculation():
  76. class Net(nn.Cell):
  77. def __init__(self, strategy1, strategy2):
  78. super().__init__()
  79. self.matmul = P.MatMul().set_strategy(strategy1)
  80. self.notequal = P.NotEqual().set_strategy(strategy2)
  81. def construct(self, x, y, b):
  82. out = self.matmul(x, y)
  83. out = self.notequal(out, b)
  84. return out
  85. context.set_auto_parallel_context(device_num=8, global_rank=0)
  86. strategy1 = ((2, 2), (2, 2))
  87. strategy2 = ((4, 1), (4, 1))
  88. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  89. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  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. _executor.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)
  105. strategy1 = ((2, 2), (2, 2))
  106. strategy2 = ((4, 2), (4, 2))
  107. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  108. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  109. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  110. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  111. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  112. _executor.compile(net, x, y, b)
  113. def test_matmul_maximum_broadcast():
  114. class Net(nn.Cell):
  115. def __init__(self, strategy1, strategy2):
  116. super().__init__()
  117. self.matmul = P.MatMul().set_strategy(strategy1)
  118. self.maximum = P.Maximum().set_strategy(strategy2)
  119. def construct(self, x, y, b):
  120. out = self.matmul(x, y)
  121. out = self.maximum(out, b)
  122. return out
  123. context.set_auto_parallel_context(device_num=8, global_rank=0)
  124. strategy1 = ((2, 2), (2, 2))
  125. strategy2 = ((4, 2), (2, ))
  126. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  127. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  128. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  129. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  130. b = Tensor(np.ones([64]), dtype=ms.float32)
  131. _executor.compile(net, x, y, b)
  132. def test_matmul_maximum_broadcast2():
  133. class Net(nn.Cell):
  134. def __init__(self, strategy1, strategy2):
  135. super().__init__()
  136. self.matmul = P.MatMul().set_strategy(strategy1)
  137. self.maximum = P.Maximum().set_strategy(strategy2)
  138. def construct(self, x, y, b):
  139. out = self.matmul(x, y)
  140. out = self.maximum(out, b)
  141. return out
  142. context.set_auto_parallel_context(device_num=8, global_rank=0)
  143. strategy1 = ((2, 4), (4, 1))
  144. strategy2 = ((4, 1), (1, 2))
  145. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  146. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  147. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  148. y = Tensor(np.ones([32, 1]), dtype=ms.float32)
  149. b = Tensor(np.ones([1, 64]), dtype=ms.float32)
  150. _executor.compile(net, x, y, b)