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_allreduce_fusion.py 14 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
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # Copyright 2020 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, context
  18. from mindspore.common.api import _executor
  19. from mindspore.nn.loss import SoftmaxCrossEntropyWithLogits
  20. from mindspore.nn.optim.momentum import Momentum
  21. from mindspore.parallel import _cost_model_context as cost_model_context
  22. from mindspore.parallel._auto_parallel_context import auto_parallel_context
  23. from mindspore.train import Model
  24. from mindspore.context import ParallelMode
  25. from tests.dataset_mock import MindData
  26. class Dataset(MindData):
  27. def __init__(self, predict, label, length=3):
  28. super(Dataset, self).__init__(size=length)
  29. self.predict = predict
  30. self.label = label
  31. self.index = 0
  32. self.length = length
  33. def __iter__(self):
  34. return self
  35. def __next__(self):
  36. if self.index >= self.length:
  37. raise StopIteration
  38. self.index += 1
  39. return self.predict, self.label
  40. def reset(self):
  41. self.index = 0
  42. class DenseNet1(nn.Cell):
  43. def __init__(self, has_bias=True, activation='relu'):
  44. super(DenseNet1, self).__init__()
  45. self.fc1 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  46. self.fc2 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  47. self.fc3 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  48. self.fc4 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  49. def construct(self, x):
  50. q = self.fc1(x)
  51. k = self.fc2(q)
  52. v = self.fc3(k)
  53. s = self.fc4(v)
  54. return s
  55. class DenseNet2(nn.Cell):
  56. def __init__(self, has_bias=True, activation='relu'):
  57. super(DenseNet2, self).__init__()
  58. self.fc1 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  59. self.fc2 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  60. self.fc3 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  61. self.fc4 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  62. self.fc5 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  63. self.fc6 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  64. self.fc7 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  65. self.fc8 = nn.Dense(128, 128, has_bias=has_bias, activation=activation)
  66. def construct(self, x):
  67. q = self.fc1(x)
  68. k = self.fc2(q)
  69. v = self.fc3(k)
  70. s = self.fc4(v)
  71. t = self.fc5(s)
  72. u = self.fc6(t)
  73. w = self.fc7(u)
  74. z = self.fc8(w)
  75. return z
  76. class SimpleDMLNet(nn.Cell):
  77. def __init__(self, net1, net2):
  78. super(SimpleDMLNet, self).__init__()
  79. self.backbone1 = net1
  80. self.backbone2 = net2
  81. def construct(self, x):
  82. x1 = self.backbone1(x)
  83. x2 = self.backbone2(x)
  84. return x1 + x2
  85. def train_common(net):
  86. batch_size = 32
  87. learning_rate = 0.1
  88. momentum = 0.9
  89. epoch_size = 2
  90. device_num = 4
  91. auto_parallel_context().set_enable_all_reduce_fusion(enable_all_reduce_fusion=True)
  92. context.set_auto_parallel_context(device_num=device_num, parameter_broadcast=False)
  93. context.set_context(mode=context.GRAPH_MODE)
  94. predict = Tensor(np.ones([batch_size, 128]), dtype=ms.float32)
  95. label = Tensor(np.ones([batch_size]), dtype=ms.int32)
  96. dataset = Dataset(predict, label, 2)
  97. loss = SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  98. opt = Momentum(net.trainable_params(), learning_rate, momentum)
  99. model = Model(net, loss, opt)
  100. model.train(epoch_size, dataset, dataset_sink_mode=False)
  101. allreduce_fusion_dict = _executor._get_allreduce_fusion(model._train_network)
  102. print(allreduce_fusion_dict)
  103. return allreduce_fusion_dict
  104. def test_allreduce_fusion_parameters():
  105. cost_model_context.reset_cost_model_context()
  106. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=2)
  107. algorithm = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_algorithm')
  108. assert algorithm == 2
  109. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=1)
  110. algorithm = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_algorithm')
  111. assert algorithm == 1
  112. cost_model_context.reset_cost_model_context()
  113. algorithm = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_algorithm')
  114. assert algorithm == 0
  115. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_times=2)
  116. fusion_times = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_times')
  117. assert fusion_times == 2
  118. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_percent=0.2)
  119. tail_percent = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_tail_percent')
  120. assert tail_percent == 0.2
  121. cost_model_context.reset_cost_model_context()
  122. tail_percent = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_tail_percent')
  123. assert tail_percent == 0.1
  124. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_time=0.2)
  125. tail_time = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_tail_time')
  126. assert tail_time == 0.2
  127. cost_model_context.reset_cost_model_context()
  128. tail_time = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_tail_time')
  129. assert tail_time == 0.1
  130. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_allreduce_inherent_time=0.2)
  131. allreduce_inherent_time = cost_model_context.get_cost_model_context(
  132. 'costmodel_allreduce_fusion_allreduce_inherent_time')
  133. assert allreduce_inherent_time == 0.2
  134. cost_model_context.reset_cost_model_context()
  135. allreduce_inherent_time = cost_model_context.get_cost_model_context(
  136. 'costmodel_allreduce_fusion_allreduce_inherent_time')
  137. assert allreduce_inherent_time == 0.1
  138. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_allreduce_bandwidth=0.2)
  139. allreduce_bandwidth = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_allreduce_bandwidth')
  140. assert allreduce_bandwidth == 0.2
  141. cost_model_context.reset_cost_model_context()
  142. allreduce_bandwidth = cost_model_context.get_cost_model_context('costmodel_allreduce_fusion_allreduce_bandwidth')
  143. assert allreduce_bandwidth == 0.1
  144. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_computation_time_parameter=0.2)
  145. computation_time_parameter = cost_model_context.get_cost_model_context(
  146. 'costmodel_allreduce_fusion_computation_time_parameter')
  147. assert computation_time_parameter == 0.2
  148. cost_model_context.reset_cost_model_context()
  149. computation_time_parameter = cost_model_context.get_cost_model_context(
  150. 'costmodel_allreduce_fusion_computation_time_parameter')
  151. assert computation_time_parameter == 0.1
  152. def test_allreduce_fusion1():
  153. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=1)
  154. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_times=2)
  155. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_percent=0.5)
  156. context.reset_auto_parallel_context()
  157. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
  158. net = SimpleDMLNet(DenseNet1(has_bias=False, activation=None), DenseNet2(has_bias=False, activation=None))
  159. allreduce_fusion_dict = train_common(net)
  160. expect_dict = {'backbone2.fc8.weight': 2,
  161. 'backbone2.fc7.weight': 2,
  162. 'backbone2.fc6.weight': 2,
  163. 'backbone1.fc4.weight': 2,
  164. 'backbone1.fc3.weight': 2,
  165. 'backbone1.fc2.weight': 2,
  166. 'backbone2.fc5.weight': 1,
  167. 'backbone2.fc4.weight': 1,
  168. 'backbone2.fc3.weight': 1,
  169. 'backbone2.fc2.weight': 1,
  170. 'backbone2.fc1.weight': 1,
  171. 'backbone1.fc1.weight': 1}
  172. assert allreduce_fusion_dict == expect_dict
  173. cost_model_context.reset_cost_model_context()
  174. # reset_cost_model_context is called, the default value of costmodel_allreduce_fusion_times is 0, step_allreduce_fusion
  175. # is bypassed.
  176. def test_allreduce_fusion2():
  177. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_times=2)
  178. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_percent=0.5)
  179. cost_model_context.reset_cost_model_context()
  180. context.reset_auto_parallel_context()
  181. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
  182. net = SimpleDMLNet(DenseNet1(has_bias=False, activation=None), DenseNet2(has_bias=False, activation=None))
  183. allreduce_fusion_dict = train_common(net)
  184. expect_dict = {}
  185. assert allreduce_fusion_dict == expect_dict
  186. cost_model_context.reset_cost_model_context()
  187. def test_allreduce_fusion3():
  188. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=1)
  189. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_times=3)
  190. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_percent=0.3333333)
  191. context.reset_auto_parallel_context()
  192. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
  193. net = SimpleDMLNet(DenseNet1(has_bias=True, activation='relu'), DenseNet2(has_bias=False, activation='relu'))
  194. allreduce_fusion_dict = train_common(net)
  195. expect_dict = {'backbone2.fc8.weight': 3,
  196. 'backbone2.fc7.weight': 3,
  197. 'backbone2.fc6.weight': 2,
  198. 'backbone2.fc5.weight': 2,
  199. 'backbone2.fc4.weight': 2,
  200. 'backbone2.fc3.weight': 1,
  201. 'backbone2.fc2.weight': 1,
  202. 'backbone2.fc1.weight': 1,
  203. 'backbone1.fc4.bias': 3,
  204. 'backbone1.fc4.weight': 3,
  205. 'backbone1.fc3.bias': 3,
  206. 'backbone1.fc3.weight': 2,
  207. 'backbone1.fc2.bias': 2,
  208. 'backbone1.fc2.weight': 2,
  209. 'backbone1.fc1.bias': 2,
  210. 'backbone1.fc1.weight': 2}
  211. assert allreduce_fusion_dict == expect_dict
  212. cost_model_context.reset_cost_model_context()
  213. def test_allreduce_fusion4():
  214. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=1)
  215. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_times=2)
  216. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_percent=0.5)
  217. context.reset_auto_parallel_context()
  218. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
  219. net = SimpleDMLNet(DenseNet2(has_bias=False, activation=None), DenseNet2(has_bias=False, activation=None))
  220. allreduce_fusion_dict = train_common(net)
  221. expect_dict = {'backbone2.fc8.weight': 2,
  222. 'backbone2.fc7.weight': 2,
  223. 'backbone2.fc6.weight': 2,
  224. 'backbone1.fc8.weight': 2,
  225. 'backbone1.fc7.weight': 2,
  226. 'backbone1.fc6.weight': 2,
  227. 'backbone2.fc5.weight': 1,
  228. 'backbone2.fc4.weight': 1,
  229. 'backbone2.fc3.weight': 1,
  230. 'backbone2.fc2.weight': 1,
  231. 'backbone2.fc1.weight': 1,
  232. 'backbone1.fc5.weight': 1,
  233. 'backbone1.fc4.weight': 1,
  234. 'backbone1.fc3.weight': 1,
  235. 'backbone1.fc2.weight': 1,
  236. 'backbone1.fc1.weight': 1}
  237. assert allreduce_fusion_dict == expect_dict
  238. cost_model_context.reset_cost_model_context()
  239. def test_allreduce_fusion5():
  240. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=2)
  241. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_time=0.1)
  242. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_allreduce_inherent_time=0.05)
  243. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_allreduce_bandwidth=0.000001)
  244. cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_computation_time_parameter=0.0000015)
  245. context.reset_auto_parallel_context()
  246. context.set_auto_parallel_context(parallel_mode=ParallelMode.SEMI_AUTO_PARALLEL)
  247. net = SimpleDMLNet(DenseNet2(has_bias=False, activation=None), DenseNet2(has_bias=False, activation=None))
  248. allreduce_fusion_dict = train_common(net)
  249. expect_dict = {'backbone2.fc8.weight': 3,
  250. 'backbone2.fc7.weight': 3,
  251. 'backbone2.fc6.weight': 3,
  252. 'backbone2.fc5.weight': 3,
  253. 'backbone2.fc4.weight': 2,
  254. 'backbone2.fc3.weight': 2,
  255. 'backbone2.fc2.weight': 1,
  256. 'backbone2.fc1.weight': 1,
  257. 'backbone1.fc8.weight': 3,
  258. 'backbone1.fc7.weight': 3,
  259. 'backbone1.fc6.weight': 3,
  260. 'backbone1.fc5.weight': 3,
  261. 'backbone1.fc4.weight': 2,
  262. 'backbone1.fc3.weight': 2,
  263. 'backbone1.fc2.weight': 1,
  264. 'backbone1.fc1.weight': 1,}
  265. assert allreduce_fusion_dict == expect_dict
  266. cost_model_context.reset_cost_model_context()