test_auto_parallel_resnet.py 28.0 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
# Copyright 2019 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import numpy as np
import mindspore.nn as nn
import mindspore.common.dtype as mstype
from mindspore import Tensor
from mindspore.ops import operations as P
from mindspore.nn.optim.momentum import Momentum
from mindspore.common.initializer import TruncatedNormal
from mindspore.train.model import Model, ParallelMode
from mindspore import context
import os
import re
import mindspore.ops.functional as F
from mindspore.nn.loss.loss import _Loss
from mindspore.parallel._utils import _reset_op_id as resset_op_id
from mindspore.common.api import _executor
from mindspore.parallel import set_algo_parameters
from mindspore.parallel import _cost_model_context as cost_model_context

context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
context.set_context(enable_hccl=True)
context.set_context(enable_task_sink=True, device_id= 0)
context.set_context(enable_ir_fusion=True)
context.set_context(enable_loop_sink=False)

def weight_variable(shape, factor=0.1):
    return TruncatedNormal(0.02)

def _conv3x3(in_channels, out_channels, stride=1, padding=0, pad_mode='same'):
    """Get a conv2d layer with 3x3 kernel size."""
    init_value = weight_variable((out_channels, in_channels, 3, 3))
    return nn.Conv2d(in_channels, out_channels,
                     kernel_size=3, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value)

def _conv1x1(in_channels, out_channels, stride=1, padding=0, pad_mode='same'):
    """Get a conv2d layer with 1x1 kernel size."""
    init_value = weight_variable((out_channels, in_channels, 1, 1))
    return nn.Conv2d(in_channels, out_channels,
                     kernel_size=1, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value)

def _conv7x7(in_channels, out_channels, stride=1, padding=0, pad_mode='same'):
    """Get a conv2d layer with 7x7 kernel size."""
    init_value = weight_variable((out_channels, in_channels, 7, 7))
    return nn.Conv2d(in_channels, out_channels,
                     kernel_size=7, stride=stride, padding=padding, pad_mode=pad_mode, weight_init=init_value)

def _fused_bn(channels, momentum=0.9):
    """Get a fused batchnorm"""
    init_weight = weight_variable((channels,))
    init_bias = weight_variable((channels,))
    return nn.BatchNorm2d(channels, momentum=momentum)

class ResidualBlock(nn.Cell):
    expansion = 4

    def __init__(self,
                 in_channels,
                 out_channels,
                 stride=1,
                 momentum=0.9):
        super(ResidualBlock, self).__init__()

        out_chls = out_channels // self.expansion
        self.conv1 = _conv1x1(in_channels, out_chls, stride=1)
        self.bn1 = _fused_bn(out_chls, momentum=momentum)

        self.conv2 = _conv3x3(out_chls, out_chls, stride=stride)
        self.bn2 = _fused_bn(out_chls, momentum=momentum)

        self.conv3 = _conv1x1(out_chls, out_channels, stride=1)
        self.bn3 = _fused_bn(out_channels, momentum=momentum)

        self.relu = P.ReLU()
        self.downsample = (in_channels != out_channels)
        self.stride = stride
        if self.downsample:
            self.conv_down_sample = _conv1x1(in_channels, out_channels,
                                             stride=stride)
            self.bn_down_sample = _fused_bn(out_channels, momentum=momentum)
        elif self.stride != 1:
            self.maxpool_down = nn.MaxPool2d(kernel_size=1, stride=2, pad_mode='same')

        self.add = P.TensorAdd()

    def construct(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample:
            identity = self.conv_down_sample(identity)
            identity = self.bn_down_sample(identity)
        elif self.stride != 1:
            identity = self.maxpool_down(identity)

        out = self.add(out, identity)
        out = self.relu(out)

        return out


class ResNet(nn.Cell):
    def __init__(self,
                 block,
                 layer_nums,
                 in_channels,
                 out_channels,
                 strides=[1,2,2,2],
                 num_classes=100):
        super(ResNet, self).__init__()

        if not len(layer_nums) == len(in_channels) == len(out_channels) == 4:
            raise ValueError("the length of "
                             "layer_num, inchannel, outchannel list must be 4!")

        self.conv1 = _conv7x7(3, 64, stride=2)
        self.bn1 = _fused_bn(64)
        self.relu = P.ReLU()
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, pad_mode='same')

        self.layer1 = self._make_layer(block,
                                       layer_nums[0],
                                       in_channel=in_channels[0],
                                       out_channel=out_channels[0],
                                       stride=strides[0])
        self.layer2 = self._make_layer(block,
                                       layer_nums[1],
                                       in_channel=in_channels[1],
                                       out_channel=out_channels[1],
                                       stride=strides[1])
        self.layer3 = self._make_layer(block,
                                       layer_nums[2],
                                       in_channel=in_channels[2],
                                       out_channel=out_channels[2],
                                       stride=strides[2])
        self.layer4 = self._make_layer(block,
                                       layer_nums[3],
                                       in_channel=in_channels[3],
                                       out_channel=out_channels[3],
                                       stride=strides[3])

        self.mean = P.ReduceMean(keep_dims=True)
        self.end_point = nn.Dense(2048, num_classes, has_bias=True,
                                  weight_init=weight_variable((num_classes, 2048)),
                                  bias_init=weight_variable((num_classes,))).add_flags_recursive(fp16=True)
        self.squeeze = P.Squeeze()
        self.cast = P.Cast()

    def _make_layer(self, block, layer_num, in_channel, out_channel, stride):
        layers = []
        down_sample = False
        if stride != 1 or in_channel != out_channel:
            down_sample = True

        resblk = block(in_channel, out_channel, stride=1)
        layers.append(resblk)

        for _ in range(1, layer_num - 1):
            resblk = block(out_channel, out_channel, stride=1)
            layers.append(resblk)

        resblk = block(out_channel, out_channel, stride=stride)
        layers.append(resblk)

        return nn.SequentialCell(layers)

    def construct(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        c1 = self.maxpool(x)
        c2 = self.layer1(c1)
        c3 = self.layer2(c2)
        c4 = self.layer3(c3)
        c5 = self.layer4(c4)
        out = self.mean(c5, (2, 3))
        out = self.squeeze(out)
        out = self.end_point(out)

        return out


def resnet50(class_num=10):
    return ResNet(ResidualBlock,
                  [3, 4, 6, 3],
                  [64, 256, 512, 1024],
                  [256, 512, 1024, 2048],
                  [2, 2, 2, 1],
                  class_num)

class SoftmaxCrossEntropyExpand(_Loss):
    def __init__(self, sparse=False):
        super(SoftmaxCrossEntropyExpand, self).__init__()
        self.exp = P.Exp()
        self.sum = P.ReduceSum(keep_dims=True)
        self.onehot = P.OneHot()
        self.on_value = Tensor(1.0, mstype.float32)
        self.off_value = Tensor(0.0, mstype.float32)
        self.div = P.Div()
        self.log = P.Log()
        self.sum_cross_entropy = P.ReduceSum(keep_dims=False)
        self.mul = P.Mul()
        self.mul2 = P.Mul()
        self.cast = P.Cast()
        self.mean = P.ReduceMean(keep_dims=False).add_prim_attr("cross_batch", True)
        self.sparse = sparse
        self.max = P.ReduceMax(keep_dims=True)
        self.sub = P.Sub()
        self.cast1 = P.Cast()

    def construct(self, logit, label):
        logit = self.cast1(logit, mstype.float32)
        logit_max = self.max(logit)
        exp = self.exp(self.sub(logit, logit_max))
        exp_sum = self.sum(exp, -1)
        softmax_result = self.div(exp, exp_sum)
        if self.sparse:
            label = self.onehot(label, F.shape(logit)[1], self.on_value, self.off_value)

        softmax_result_log = self.log(softmax_result)
        loss = self.sum_cross_entropy((self.mul(softmax_result_log, label)), -1)
        loss = self.mul2(F.scalar_to_array(-1.0), loss)
        loss = self.mean(loss, -1)

        return loss


class DatasetLenet():
    def __init__(self, predict, label, length=3):
        self.predict = predict
        self.label = label
        self.index = 0
        self.length = length

    def __iter__(self):
        return self

    def __next__(self):
        if self.index >= self.length:
            raise StopIteration
        self.index += 1
        return self.predict, self.label

    def reset(self):
        self.index = 0

    def get_dataset_size(self):
        return 32

    def get_repeat_count(self):
        return 1


276
def train_32k_8p(epoch_size=3, batch_size=32, num_classes=32768):
Z
zhunaipan 已提交
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
    dev_num = 8
    context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL, device_num=dev_num)
    set_algo_parameters(elementwise_op_strategy_follow=True)
    resset_op_id()
    np.random.seed(6)
    input_np = np.ones([batch_size, 3, 224, 224]).astype(np.float32)
    label_np = np.zeros([batch_size]).astype(np.int32)
    for i in range(0, batch_size):
        label_np[i] = i % num_classes
    dataset = DatasetLenet(Tensor(input_np), Tensor(label_np), 1)
    net = resnet50(num_classes)
    loss = SoftmaxCrossEntropyExpand(sparse=True)
    opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
    model = Model(net, loss_fn=loss, optimizer=opt)
    model.train(5, dataset, dataset_sink_mode=False)
    strategies = _executor._get_strategy(model._train_network)
    for (k, v) in strategies.items():
294
        if re.search('Conv2D-op', k) is not None:
Z
zhunaipan 已提交
295
            assert v[0][0] == dev_num
296
        elif re.search('MatMul-op', k) is not None:
Z
zhunaipan 已提交
297
            assert v == [[dev_num, 1], [1, 1]]
298
        elif re.search('ReduceSum-op', k) is not None:
Z
zhunaipan 已提交
299 300 301 302
            assert v == [[dev_num, 1]]

    allreduce_fusion_dict = _executor._get_allreduce_fusion(model._train_network)
    print(allreduce_fusion_dict)
303 304 305 306 307 308 309 310 311
    return allreduce_fusion_dict


def test_train_32k_8p_fusion1(epoch_size=3, batch_size=32, num_classes=32768): #1048576 #131072 #32768 #8192
    cost_model_context.set_cost_model_context(costmodel_gamma=0.001, costmodel_beta=260.0)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=1)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_times=2)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_percent=0.5)
    allreduce_fusion_dict = train_32k_8p(epoch_size, batch_size, num_classes)
Z
zhunaipan 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    expect_dict = {'end_point.bias': 2,
                   'end_point.weight': 2,
                   'layer4.2.bn3.beta': 2,
                   'layer4.2.bn3.gamma': 2,
                   'layer4.2.conv3.weight': 2,
                   'layer4.2.bn2.beta': 2,
                   'layer4.2.bn2.gamma': 2,
                   'layer4.2.conv2.weight': 2,
                   'layer4.2.bn1.beta': 2,
                   'layer4.2.bn1.gamma': 2,
                   'layer4.2.conv1.weight': 2,
                   'layer4.1.bn3.beta': 2,
                   'layer4.1.bn3.gamma': 2,
                   'layer4.1.conv3.weight': 2,
                   'layer4.1.bn2.beta': 2,
                   'layer4.1.bn2.gamma': 2,
                   'layer4.1.conv2.weight': 2,
                   'layer4.1.bn1.beta': 2,
                   'layer4.1.bn1.gamma': 2,
                   'layer4.1.conv1.weight': 2,
                   'layer4.0.bn_down_sample.beta': 2,
                   'layer4.0.bn_down_sample.gamma': 2,
                   'layer4.0.conv_down_sample.weight': 2,
                   'layer4.0.bn3.beta': 2,
                   'layer4.0.bn3.gamma': 2,
                   'layer4.0.conv3.weight': 2,
                   'layer4.0.bn2.beta': 2,
                   'layer4.0.bn2.gamma': 2,
                   'layer4.0.conv2.weight': 2,
                   'layer4.0.bn1.beta': 2,
                   'layer4.0.bn1.gamma': 2,
                   'layer4.0.conv1.weight': 2,
                   'layer3.5.bn3.beta': 2,
                   'layer3.5.bn3.gamma': 2,
                   'layer3.5.conv3.weight': 2,
                   'layer3.5.bn2.beta': 2,
                   'layer3.5.bn2.gamma': 2,
                   'layer3.5.conv2.weight': 2,
                   'layer3.5.bn1.beta': 2,
                   'layer3.5.bn1.gamma': 2,
                   'layer3.5.conv1.weight': 2,
                   'layer3.4.bn3.beta': 2,
                   'layer3.4.bn3.gamma': 2,
                   'layer3.4.conv3.weight': 2,
                   'layer3.4.bn2.beta': 2,
                   'layer3.4.bn2.gamma': 2,
                   'layer3.4.conv2.weight': 2,
                   'layer3.4.bn1.beta': 2,
                   'layer3.4.bn1.gamma': 2,
                   'layer3.4.conv1.weight': 2,
                   'layer3.3.bn3.beta': 2,
                   'layer3.3.bn3.gamma': 2,
                   'layer3.3.conv3.weight': 2,
                   'layer3.3.bn2.beta': 2,
                   'layer3.3.bn2.gamma': 2,
                   'layer3.3.conv2.weight': 2,
                   'layer3.3.bn1.beta': 2,
                   'layer3.3.bn1.gamma': 2,
                   'layer3.3.conv1.weight': 2,
                   'layer3.2.bn3.beta': 2,
                   'layer3.2.bn3.gamma': 2,
                   'layer3.2.conv3.weight': 2,
                   'layer3.2.bn2.beta': 2,
                   'layer3.2.bn2.gamma': 2,
                   'layer3.2.conv2.weight': 2,
                   'layer3.2.bn1.beta': 2,
                   'layer3.2.bn1.gamma': 2,
                   'layer3.2.conv1.weight': 2,
                   'layer3.1.bn3.beta': 2,
                   'layer3.1.bn3.gamma': 2,
                   'layer3.1.conv3.weight': 2,
                   'layer3.1.bn2.beta': 2,
                   'layer3.1.bn2.gamma': 2,
                   'layer3.1.conv2.weight': 2,
                   'layer3.1.bn1.beta': 2,
                   'layer3.1.bn1.gamma': 2,
                   'layer3.1.conv1.weight': 2,
389 390
                   'layer3.0.bn_down_sample.beta': 2,
                   'layer3.0.bn_down_sample.gamma': 2,
Z
zhunaipan 已提交
391
                   'layer3.0.conv_down_sample.weight': 2,
392 393
                   'layer3.0.bn3.beta': 2,
                   'layer3.0.bn3.gamma': 2,
Z
zhunaipan 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
                   'layer3.0.conv3.weight': 2,
                   'layer3.0.bn2.beta': 2,
                   'layer3.0.bn2.gamma': 2,
                   'layer3.0.conv2.weight': 2,
                   'layer3.0.bn1.beta': 2,
                   'layer3.0.bn1.gamma': 2,
                   'layer3.0.conv1.weight': 2,
                   'layer2.3.bn3.beta': 2,
                   'layer2.3.bn3.gamma': 2,
                   'layer2.3.conv3.weight': 2,
                   'layer2.3.bn2.beta': 2,
                   'layer2.3.bn2.gamma': 2,
                   'layer2.3.conv2.weight': 2,
                   'layer2.3.bn1.beta': 2,
                   'layer2.3.bn1.gamma': 2,
                   'layer2.3.conv1.weight': 2,
                   'layer2.2.bn3.beta': 2,
                   'layer2.2.bn3.gamma': 2,
                   'layer2.2.conv3.weight': 2,
                   'layer2.2.bn2.beta': 2,
                   'layer2.2.bn2.gamma': 2,
                   'layer2.2.conv2.weight': 2,
                   'layer2.2.bn1.beta': 2,
                   'layer2.2.bn1.gamma': 2,
                   'layer2.2.conv1.weight': 2,
419 420
                   'layer2.1.bn3.beta': 2,
                   'layer2.1.bn3.gamma': 2,
Z
zhunaipan 已提交
421 422 423 424 425 426 427
                   'layer2.1.conv3.weight': 2,
                   'layer2.1.bn2.beta': 2,
                   'layer2.1.bn2.gamma': 2,
                   'layer2.1.conv2.weight': 2,
                   'layer2.1.bn1.beta': 2,
                   'layer2.1.bn1.gamma': 2,
                   'layer2.1.conv1.weight': 2,
428 429
                   'layer2.0.bn_down_sample.beta': 2,
                   'layer2.0.bn_down_sample.gamma': 2,
Z
zhunaipan 已提交
430
                   'layer2.0.conv_down_sample.weight': 2,
431 432
                   'layer2.0.bn3.beta': 2,
                   'layer2.0.bn3.gamma': 2,
Z
zhunaipan 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
                   'layer2.0.conv3.weight': 2,
                   'layer2.0.bn2.beta': 2,
                   'layer2.0.bn2.gamma': 2,
                   'layer2.0.conv2.weight': 2,
                   'layer2.0.bn1.beta': 2,
                   'layer2.0.bn1.gamma': 2,
                   'layer2.0.conv1.weight': 2,
                   'layer1.2.bn3.beta': 2,
                   'layer1.2.bn3.gamma': 2,
                   'layer1.2.conv3.weight': 2,
                   'layer1.2.bn2.beta': 2,
                   'layer1.2.bn2.gamma': 2,
                   'layer1.2.conv2.weight': 2,
                   'layer1.2.bn1.beta': 2,
                   'layer1.2.bn1.gamma': 2,
                   'layer1.2.conv1.weight': 2,
449 450
                   'layer1.1.bn3.beta': 2,
                   'layer1.1.bn3.gamma': 2,
Z
zhunaipan 已提交
451 452 453 454 455 456 457
                   'layer1.1.conv3.weight': 2,
                   'layer1.1.bn2.beta': 2,
                   'layer1.1.bn2.gamma': 2,
                   'layer1.1.conv2.weight': 2,
                   'layer1.1.bn1.beta': 2,
                   'layer1.1.bn1.gamma': 2,
                   'layer1.1.conv1.weight': 2,
458 459
                   'layer1.0.bn_down_sample.beta': 2,
                   'layer1.0.bn_down_sample.gamma': 2,
Z
zhunaipan 已提交
460
                   'layer1.0.conv_down_sample.weight': 2,
461 462
                   'layer1.0.bn3.beta': 2,
                   'layer1.0.bn3.gamma': 2,
Z
zhunaipan 已提交
463 464 465 466 467 468 469 470 471
                   'layer1.0.conv3.weight': 2,
                   'layer1.0.bn2.beta': 2,
                   'layer1.0.bn2.gamma': 2,
                   'layer1.0.conv2.weight': 2,
                   'layer1.0.bn1.beta': 2,
                   'layer1.0.bn1.gamma': 2,
                   'layer1.0.conv1.weight': 2,
                   'bn1.beta': 1,
                   'bn1.gamma': 1,
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
                   'conv1.weight': 1}

    assert (allreduce_fusion_dict == expect_dict)
    cost_model_context.reset_cost_model_context()


def test_train_32k_8p_fusion2(epoch_size=3, batch_size=32, num_classes=32768): #1048576 #131072 #32768 #8192
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_algorithm=2)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_tail_time=0.1)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_allreduce_inherent_time=0.05)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_allreduce_bandwidth=0.000001)
    cost_model_context.set_cost_model_context(costmodel_allreduce_fusion_computation_time_parameter=0.0000015)
    allreduce_fusion_dict = train_32k_8p(epoch_size, batch_size, num_classes)
    expect_dict = {'end_point.bias': 2,
                   'end_point.weight': 2,
                   'layer4.2.bn3.beta': 2,
                   'layer4.2.bn3.gamma': 2,
                   'layer4.2.conv3.weight': 2,
                   'layer4.2.bn2.beta': 2,
                   'layer4.2.bn2.gamma': 2,
                   'layer4.2.conv2.weight': 2,
                   'layer4.2.bn1.beta': 2,
                   'layer4.2.bn1.gamma': 2,
                   'layer4.2.conv1.weight': 2,
                   'layer4.1.bn3.beta': 2,
                   'layer4.1.bn3.gamma': 2,
                   'layer4.1.conv3.weight': 2,
                   'layer4.1.bn2.beta': 2,
                   'layer4.1.bn2.gamma': 2,
                   'layer4.1.conv2.weight': 2,
                   'layer4.1.bn1.beta': 2,
                   'layer4.1.bn1.gamma': 2,
                   'layer4.1.conv1.weight': 2,
                   'layer4.0.bn_down_sample.beta': 2,
                   'layer4.0.bn_down_sample.gamma': 2,
                   'layer4.0.conv_down_sample.weight': 2,
                   'layer4.0.bn3.beta': 2,
                   'layer4.0.bn3.gamma': 2,
                   'layer4.0.conv3.weight': 2,
                   'layer4.0.bn2.beta': 2,
                   'layer4.0.bn2.gamma': 2,
                   'layer4.0.conv2.weight': 2,
                   'layer4.0.bn1.beta': 2,
                   'layer4.0.bn1.gamma': 2,
                   'layer4.0.conv1.weight': 2,
                   'layer3.5.bn3.beta': 2,
                   'layer3.5.bn3.gamma': 2,
                   'layer3.5.conv3.weight': 2,
                   'layer3.5.bn2.beta': 2,
                   'layer3.5.bn2.gamma': 2,
                   'layer3.5.conv2.weight': 2,
                   'layer3.5.bn1.beta': 2,
                   'layer3.5.bn1.gamma': 2,
                   'layer3.5.conv1.weight': 2,
                   'layer3.4.bn3.beta': 2,
                   'layer3.4.bn3.gamma': 2,
                   'layer3.4.conv3.weight': 2,
                   'layer3.4.bn2.beta': 2,
                   'layer3.4.bn2.gamma': 2,
                   'layer3.4.conv2.weight': 2,
                   'layer3.4.bn1.beta': 2,
                   'layer3.4.bn1.gamma': 2,
                   'layer3.4.conv1.weight': 2,
                   'layer3.3.bn3.beta': 2,
                   'layer3.3.bn3.gamma': 2,
                   'layer3.3.conv3.weight': 2,
                   'layer3.3.bn2.beta': 2,
                   'layer3.3.bn2.gamma': 2,
                   'layer3.3.conv2.weight': 2,
                   'layer3.3.bn1.beta': 2,
                   'layer3.3.bn1.gamma': 2,
                   'layer3.3.conv1.weight': 2,
                   'layer3.2.bn3.beta': 2,
                   'layer3.2.bn3.gamma': 2,
                   'layer3.2.conv3.weight': 2,
                   'layer3.2.bn2.beta': 2,
                   'layer3.2.bn2.gamma': 2,
                   'layer3.2.conv2.weight': 2,
                   'layer3.2.bn1.beta': 2,
                   'layer3.2.bn1.gamma': 2,
                   'layer3.2.conv1.weight': 2,
                   'layer3.1.bn3.beta': 2,
                   'layer3.1.bn3.gamma': 2,
                   'layer3.1.conv3.weight': 2,
                   'layer3.1.bn2.beta': 2,
                   'layer3.1.bn2.gamma': 2,
                   'layer3.1.conv2.weight': 2,
                   'layer3.1.bn1.beta': 2,
                   'layer3.1.bn1.gamma': 2,
                   'layer3.1.conv1.weight': 2,
                   'layer3.0.bn_down_sample.beta': 2,
                   'layer3.0.bn_down_sample.gamma': 2,
                   'layer3.0.conv_down_sample.weight': 2,
                   'layer3.0.bn3.beta': 2,
                   'layer3.0.bn3.gamma': 2,
                   'layer3.0.conv3.weight': 2,
                   'layer3.0.bn2.beta': 2,
                   'layer3.0.bn2.gamma': 2,
                   'layer3.0.conv2.weight': 2,
                   'layer3.0.bn1.beta': 2,
                   'layer3.0.bn1.gamma': 2,
                   'layer3.0.conv1.weight': 2,
                   'layer2.3.bn3.beta': 2,
                   'layer2.3.bn3.gamma': 2,
                   'layer2.3.conv3.weight': 2,
                   'layer2.3.bn2.beta': 2,
                   'layer2.3.bn2.gamma': 2,
                   'layer2.3.conv2.weight': 2,
                   'layer2.3.bn1.beta': 2,
                   'layer2.3.bn1.gamma': 2,
                   'layer2.3.conv1.weight': 2,
                   'layer2.2.bn3.beta': 2,
                   'layer2.2.bn3.gamma': 2,
                   'layer2.2.conv3.weight': 2,
                   'layer2.2.bn2.beta': 2,
                   'layer2.2.bn2.gamma': 2,
                   'layer2.2.conv2.weight': 2,
                   'layer2.2.bn1.beta': 2,
                   'layer2.2.bn1.gamma': 2,
                   'layer2.2.conv1.weight': 2,
                   'layer2.1.bn3.beta': 2,
                   'layer2.1.bn3.gamma': 2,
                   'layer2.1.conv3.weight': 2,
                   'layer2.1.bn2.beta': 2,
                   'layer2.1.bn2.gamma': 2,
                   'layer2.1.conv2.weight': 2,
                   'layer2.1.bn1.beta': 2,
                   'layer2.1.bn1.gamma': 2,
                   'layer2.1.conv1.weight': 2,
                   'layer2.0.bn_down_sample.beta': 2,
                   'layer2.0.bn_down_sample.gamma': 2,
                   'layer2.0.conv_down_sample.weight': 2,
                   'layer2.0.bn3.beta': 2,
                   'layer2.0.bn3.gamma': 2,
                   'layer2.0.conv3.weight': 2,
                   'layer2.0.bn2.beta': 2,
                   'layer2.0.bn2.gamma': 2,
                   'layer2.0.conv2.weight': 2,
                   'layer2.0.bn1.beta': 2,
                   'layer2.0.bn1.gamma': 2,
                   'layer2.0.conv1.weight': 2,
                   'layer1.2.bn3.beta': 2,
                   'layer1.2.bn3.gamma': 2,
                   'layer1.2.conv3.weight': 2,
                   'layer1.2.bn2.beta': 2,
                   'layer1.2.bn2.gamma': 2,
                   'layer1.2.conv2.weight': 2,
                   'layer1.2.bn1.beta': 2,
                   'layer1.2.bn1.gamma': 2,
                   'layer1.2.conv1.weight': 2,
                   'layer1.1.bn3.beta': 2,
                   'layer1.1.bn3.gamma': 2,
                   'layer1.1.conv3.weight': 2,
                   'layer1.1.bn2.beta': 2,
                   'layer1.1.bn2.gamma': 2,
                   'layer1.1.conv2.weight': 2,
                   'layer1.1.bn1.beta': 2,
                   'layer1.1.bn1.gamma': 2,
                   'layer1.1.conv1.weight': 2,
                   'layer1.0.bn_down_sample.beta': 2,
                   'layer1.0.bn_down_sample.gamma': 2,
                   'layer1.0.conv_down_sample.weight': 2,
                   'layer1.0.bn3.beta': 2,
                   'layer1.0.bn3.gamma': 2,
                   'layer1.0.conv3.weight': 2,
                   'layer1.0.bn2.beta': 2,
                   'layer1.0.bn2.gamma': 2,
                   'layer1.0.conv2.weight': 1,
                   'layer1.0.bn1.beta': 1,
                   'layer1.0.bn1.gamma': 1,
                   'layer1.0.conv1.weight': 1,
                   'bn1.beta': 1,
                   'bn1.gamma': 1,
                   'conv1.weight': 1}
Z
zhunaipan 已提交
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669

    assert (allreduce_fusion_dict == expect_dict)
    cost_model_context.reset_cost_model_context()


def test_train_64k_8p(epoch_size=3, batch_size=32, num_classes=65536): #1048576 #131072 #32768 #8192
    dev_num = 8
    context.set_auto_parallel_context(parallel_mode=ParallelMode.AUTO_PARALLEL, device_num=dev_num)
    cost_model_context.set_cost_model_context(costmodel_gamma=0.001, costmodel_beta=260.0)
    set_algo_parameters(elementwise_op_strategy_follow=True)
    resset_op_id()
    np.random.seed(6)
    input_np = np.ones([batch_size, 3, 224, 224]).astype(np.float32)
    label_np = np.zeros([batch_size]).astype(np.int32)
    for i in range(0, batch_size):
        label_np[i] = i % num_classes
    dataset = DatasetLenet(Tensor(input_np), Tensor(label_np), 1)
    net = resnet50(num_classes)
    loss = SoftmaxCrossEntropyExpand(sparse=True)
    opt = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), 0.01, 0.9)
    model = Model(net, loss_fn=loss, optimizer=opt)
    model.train(5, dataset, dataset_sink_mode=False)
    strategies = _executor._get_strategy(model._train_network)
    for (k, v) in strategies.items():
670
        if re.search('Conv2D-op', k ) is not None:
Z
zhunaipan 已提交
671
            assert v[0][0] == dev_num
672
        elif re.search('MatMul-op', k) is not None:
Z
zhunaipan 已提交
673
            assert v == [[1, 1], [dev_num, 1]]
674
        elif re.search('ReduceSum-op', k) is not None:
Z
zhunaipan 已提交
675
            assert v == [[1, dev_num]]