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_strategy_checkpoint.py 12 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, Parameter
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.context import set_auto_parallel_context, reset_auto_parallel_context
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from tests.ut.python.ops.test_math_ops import VirtualLoss
  24. grad_all = C.GradOperation(get_all=True)
  25. # model_parallel test
  26. def test_six_matmul_save():
  27. class NetWithLoss(nn.Cell):
  28. def __init__(self, network):
  29. super(NetWithLoss, self).__init__()
  30. self.loss = VirtualLoss()
  31. self.network = network
  32. def construct(self, x1, x6):
  33. predict = self.network(x1, x6)
  34. return self.loss(predict)
  35. class GradWrap(nn.Cell):
  36. def __init__(self, network):
  37. super(GradWrap, self).__init__()
  38. self.network = network
  39. def construct(self, x1, x6):
  40. return grad_all(self.network)(x1, x6)
  41. class Net(nn.Cell):
  42. def __init__(self, strategy1, strategy2, strategy3, strategy4, strategy5, strategy6):
  43. super().__init__()
  44. self.matmul1 = P.MatMul().set_strategy(strategy1)
  45. self.matmul2 = P.MatMul().set_strategy(strategy2)
  46. self.matmul3 = P.MatMul().set_strategy(strategy3)
  47. self.matmul4 = P.MatMul().set_strategy(strategy4)
  48. self.matmul5 = P.MatMul().set_strategy(strategy5)
  49. self.matmul6 = P.MatMul().set_strategy(strategy6)
  50. self.weight1 = Parameter(Tensor(np.ones([32, 64]), dtype=ms.float32), name="weight1")
  51. self.weight2 = Parameter(Tensor(np.ones([64, 64]), dtype=ms.float32), name="weight2")
  52. self.weight3 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight3")
  53. self.weight4 = Parameter(Tensor(np.ones([128, 64]), dtype=ms.float32), name="weight4")
  54. self.weight5 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight5")
  55. self.weight6 = Parameter(Tensor(np.ones([32, 128]), dtype=ms.float32), name="weight6")
  56. def construct(self, x1, x6):
  57. out = self.matmul1(x1, self.weight1)
  58. out = self.matmul2(out, self.weight2)
  59. out = self.matmul3(out, self.weight3)
  60. out = self.matmul4(out, self.weight4)
  61. out = self.matmul5(out, self.weight5)
  62. out = out + self.weight6
  63. out = self.matmul6(out, x6)
  64. return out
  65. reset_auto_parallel_context()
  66. set_auto_parallel_context(device_num=8, global_rank=0, strategy_ckpt_save_file="./strategy_stage1.ckpt")
  67. strategy1 = ((8, 1), (1, 1))
  68. strategy2 = ((1, 8), (8, 1))
  69. strategy3 = ((2, 2), (2, 2))
  70. strategy4 = ((1, 1), (1, 8))
  71. strategy5 = ((4, 2), (2, 1))
  72. strategy6 = ((4, 1), (1, 2))
  73. net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3, strategy4, strategy5, strategy6)))
  74. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  75. net.set_auto_parallel()
  76. x1 = Tensor(np.ones([32, 32]), dtype=ms.float32)
  77. x6 = Tensor(np.ones([128, 32]), dtype=ms.float32)
  78. _executor.compile(net, x1, x6)
  79. # remove matmul2, add matmul7
  80. def test_six_matmul_load():
  81. class NetWithLoss(nn.Cell):
  82. def __init__(self, network):
  83. super(NetWithLoss, self).__init__()
  84. self.loss = VirtualLoss()
  85. self.network = network
  86. def construct(self, x1, x6, x7):
  87. predict = self.network(x1, x6, x7)
  88. return self.loss(predict)
  89. class GradWrap(nn.Cell):
  90. def __init__(self, network):
  91. super(GradWrap, self).__init__()
  92. self.network = network
  93. def construct(self, x1, x6, x7):
  94. return grad_all(self.network)(x1, x6, x7)
  95. class Net(nn.Cell):
  96. def __init__(self, strategy1, strategy3, strategy4, strategy5, strategy6, strategy7):
  97. super().__init__()
  98. self.matmul1 = P.MatMul().set_strategy(strategy1)
  99. self.matmul3 = P.MatMul().set_strategy(strategy3)
  100. self.matmul4 = P.MatMul().set_strategy(strategy4)
  101. self.matmul5 = P.MatMul().set_strategy(strategy5)
  102. self.matmul6 = P.MatMul().set_strategy(strategy6)
  103. self.matmul7 = P.MatMul().set_strategy(strategy7)
  104. self.weight1 = Parameter(Tensor(np.ones([32, 64]), dtype=ms.float32), name="weight1")
  105. self.weight3 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight3")
  106. self.weight4 = Parameter(Tensor(np.ones([128, 64]), dtype=ms.float32), name="weight4")
  107. self.weight5 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight5")
  108. self.weight6 = Parameter(Tensor(np.ones([32, 128]), dtype=ms.float32), name="weight6")
  109. def construct(self, x1, x6, x7):
  110. out = self.matmul1(x1, self.weight1)
  111. out = self.matmul3(out, self.weight3)
  112. out = self.matmul4(out, self.weight4)
  113. out = self.matmul5(out, self.weight5)
  114. out = out + self.weight6
  115. out = self.matmul6(out, x6)
  116. out = self.matmul7(out, x7)
  117. return out
  118. reset_auto_parallel_context()
  119. set_auto_parallel_context(device_num=8, global_rank=0, strategy_ckpt_load_file="./strategy_stage1.ckpt")
  120. strategy1 = ((8, 1), (1, 1))
  121. strategy3 = ((8, 1), (1, 1))
  122. strategy4 = ((8, 1), (1, 1))
  123. strategy5 = ((8, 1), (1, 1))
  124. strategy6 = ((8, 1), (1, 1))
  125. strategy7 = ((8, 1), (1, 1))
  126. net = GradWrap(NetWithLoss(Net(strategy1, strategy3, strategy4, strategy5, strategy6, strategy7)))
  127. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  128. net.set_auto_parallel()
  129. x1 = Tensor(np.ones([32, 32]), dtype=ms.float32)
  130. x6 = Tensor(np.ones([128, 32]), dtype=ms.float32)
  131. x7 = Tensor(np.ones([32, 32]), dtype=ms.float32)
  132. _executor.compile(net, x1, x6, x7)
  133. # model_parallel test
  134. def test_six_matmul_save_auto():
  135. class NetWithLoss(nn.Cell):
  136. def __init__(self, network):
  137. super(NetWithLoss, self).__init__()
  138. self.loss = VirtualLoss()
  139. self.network = network
  140. def construct(self, x1, x6):
  141. predict = self.network(x1, x6)
  142. return self.loss(predict)
  143. class GradWrap(nn.Cell):
  144. def __init__(self, network):
  145. super(GradWrap, self).__init__()
  146. self.network = network
  147. def construct(self, x1, x6):
  148. return grad_all(self.network)(x1, x6)
  149. class Net(nn.Cell):
  150. def __init__(self):
  151. super().__init__()
  152. self.matmul1 = P.MatMul()
  153. self.matmul2 = P.MatMul()
  154. self.matmul3 = P.MatMul()
  155. self.matmul4 = P.MatMul()
  156. self.matmul5 = P.MatMul()
  157. self.matmul6 = P.MatMul()
  158. self.weight1 = Parameter(Tensor(np.ones([32, 64]), dtype=ms.float32), name="weight1")
  159. self.weight2 = Parameter(Tensor(np.ones([64, 64]), dtype=ms.float32), name="weight2")
  160. self.weight3 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight3")
  161. self.weight4 = Parameter(Tensor(np.ones([128, 64]), dtype=ms.float32), name="weight4")
  162. self.weight5 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight5")
  163. self.weight6 = Parameter(Tensor(np.ones([32, 128]), dtype=ms.float32), name="weight6")
  164. def construct(self, x1, x6):
  165. out = self.matmul1(x1, self.weight1)
  166. out = self.matmul2(out, self.weight2)
  167. out = self.matmul3(out, self.weight3)
  168. out = self.matmul4(out, self.weight4)
  169. out = self.matmul5(out, self.weight5)
  170. out = out + self.weight6
  171. out = self.matmul6(out, x6)
  172. return out
  173. reset_auto_parallel_context()
  174. set_auto_parallel_context(device_num=8, global_rank=0, strategy_ckpt_save_file="./strategy_stage1_auto.ckpt")
  175. net = GradWrap(NetWithLoss(Net()))
  176. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  177. net.set_auto_parallel()
  178. x1 = Tensor(np.ones([32, 32]), dtype=ms.float32)
  179. x6 = Tensor(np.ones([128, 32]), dtype=ms.float32)
  180. _executor.compile(net, x1, x6)
  181. # remove matmul2, add matmul7
  182. def test_six_matmul_load_auto():
  183. class NetWithLoss(nn.Cell):
  184. def __init__(self, network):
  185. super(NetWithLoss, self).__init__()
  186. self.loss = VirtualLoss()
  187. self.network = network
  188. def construct(self, x1, x6, x7):
  189. predict = self.network(x1, x6, x7)
  190. return self.loss(predict)
  191. class GradWrap(nn.Cell):
  192. def __init__(self, network):
  193. super(GradWrap, self).__init__()
  194. self.network = network
  195. def construct(self, x1, x6, x7):
  196. return grad_all(self.network)(x1, x6, x7)
  197. class Net(nn.Cell):
  198. def __init__(self, strategy1, strategy3, strategy4, strategy5):
  199. super().__init__()
  200. self.matmul1 = P.MatMul().set_strategy(strategy1)
  201. self.matmul3 = P.MatMul().set_strategy(strategy3)
  202. self.matmul4 = P.MatMul().set_strategy(strategy4)
  203. self.matmul5 = P.MatMul().set_strategy(strategy5)
  204. self.matmul6 = P.MatMul()
  205. self.matmul7 = P.MatMul()
  206. self.weight1 = Parameter(Tensor(np.ones([32, 64]), dtype=ms.float32), name="weight1")
  207. self.weight3 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight3")
  208. self.weight4 = Parameter(Tensor(np.ones([128, 64]), dtype=ms.float32), name="weight4")
  209. self.weight5 = Parameter(Tensor(np.ones([64, 128]), dtype=ms.float32), name="weight5")
  210. self.weight6 = Parameter(Tensor(np.ones([32, 128]), dtype=ms.float32), name="weight6")
  211. def construct(self, x1, x6, x7):
  212. out = self.matmul1(x1, self.weight1)
  213. out = self.matmul3(out, self.weight3)
  214. out = self.matmul4(out, self.weight4)
  215. out = self.matmul5(out, self.weight5)
  216. out = out + self.weight6
  217. out = self.matmul6(out, x6)
  218. out = self.matmul7(out, x7)
  219. return out
  220. reset_auto_parallel_context()
  221. set_auto_parallel_context(device_num=8, global_rank=0, strategy_ckpt_load_file="./strategy_stage1_auto.ckpt")
  222. strategy1 = ((2, 2), (2, 2))
  223. strategy3 = ((2, 2), (2, 2))
  224. strategy4 = ((2, 2), (2, 2))
  225. strategy5 = ((2, 2), (2, 2))
  226. net = GradWrap(NetWithLoss(Net(strategy1, strategy3, strategy4, strategy5)))
  227. context.set_auto_parallel_context(parallel_mode="auto_parallel")
  228. net.set_auto_parallel()
  229. x1 = Tensor(np.ones([32, 32]), dtype=ms.float32)
  230. x6 = Tensor(np.ones([128, 32]), dtype=ms.float32)
  231. x7 = Tensor(np.ones([32, 32]), dtype=ms.float32)
  232. _executor.compile(net, x1, x6, x7)