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_loop_two_matmul.py 2.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 math
  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. import mindspore as ms
  22. from mindspore.common.api import _executor
  23. from mindspore.ops import composite as C
  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 C.grad_all(self.network)(x, y, b)
  38. def loop_config(size):
  39. config_list = []
  40. num = 1
  41. split_list = [num]
  42. for i in range(int(math.log2(size))):
  43. num = num * 2
  44. split_list.append(num)
  45. for a in split_list:
  46. for b in split_list:
  47. if a * b > size:
  48. continue
  49. c = int(size / (a * b))
  50. config_list.append(((a, b), (b, c)))
  51. return config_list
  52. # model_parallel test
  53. def test_two_matmul():
  54. class Net(nn.Cell):
  55. def __init__(self, strategy1, strategy2):
  56. super().__init__()
  57. self.matmul1 = P.MatMul().set_strategy(strategy1)
  58. self.matmul2 = P.MatMul().set_strategy(strategy2)
  59. def construct(self, x, y, b):
  60. out = self.matmul1(x, y)
  61. out = self.matmul2(out, b)
  62. return out
  63. size = 4
  64. context.set_auto_parallel_context(device_num=size, global_rank=0)
  65. x = Tensor(np.ones([128, 32]), dtype=ms.float32)
  66. y = Tensor(np.ones([32, 64]), dtype=ms.float32)
  67. b = Tensor(np.ones([64, 64]), dtype=ms.float32)
  68. config_list = loop_config(size)
  69. count = 0
  70. for strategy1 in config_list:
  71. for strategy2 in config_list:
  72. print("=======current config {}=========".format(count))
  73. print(strategy1, strategy2)
  74. net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
  75. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  76. _executor.compile(net, x, y, b)
  77. count = count + 1