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

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