onehot_model_parallel.py 5.7 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# 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.
14
# ============================================================================
Z
zhunaipan 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

import os
import pytest
import numpy as np
import mindspore as ms
from mindspore.nn import Cell
from mindspore.ops import operations as P
from mindspore.common.tensor import Tensor
import mindspore.context as context
import mindspore.communication.management as distributedTool

device_num = 2
device_id = int(os.getenv('DEVICE_ID'))
rank_id = 0

30

Z
zhunaipan 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
def setup_module():
    global device_num
    global rank_id
    np.random.seed(0)
    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
    context.set_context(enable_task_sink=True,
                        device_id=device_id)
    context.set_context(enable_ir_fusion=True)
    context.set_context(enable_loop_sink=False)
    distributedTool.init()
    device_num = distributedTool.get_group_size()
    rank_id = distributedTool.get_rank()
    context.set_auto_parallel_context(device_num=device_num,
                                      global_rank=rank_id)

46

Z
zhunaipan 已提交
47 48 49
def teardown_module():
    distributedTool.release()

50

Z
zhunaipan 已提交
51 52 53 54 55 56 57 58 59 60 61
class Onehot(Cell):
    def __init__(self, axis=-1, depth=1, on_value=1.0, off_value=0.0, strategy=None):
        super(Onehot, self).__init__()
        trans_stra = None
        if strategy:
            trans_stra = (strategy[0],)
        self.onehot = P.OneHot().set_strategy(strategy=strategy)
        self.depth = depth
        self.on_value = Tensor(on_value, ms.float32)
        self.off_value = Tensor(off_value, ms.float32)
        self.transpose = P.Transpose().set_strategy(strategy=trans_stra)
62
        self.sub = P.Sub().set_strategy(strategy=((1, 1), (1, 1)))
Z
zhunaipan 已提交
63 64 65

    def construct(self, input, indices):
        x = self.onehot(indices, self.depth, self.on_value, self.off_value)
66
        x = self.transpose(x, (1, 0))
Z
zhunaipan 已提交
67 68 69
        x = self.sub(input, x)
        return x

70

Z
zhunaipan 已提交
71 72 73 74 75 76
class DataGenerator():
    def get_parallel_blocks(self, input_, strategy):
        blocks = [input_]
        i = 0
        for stra in strategy:
            temp = []
77
            while len(blocks) > 0:
Z
zhunaipan 已提交
78 79 80
                block = blocks.pop(0)
                temp.extend(np.split(block, stra, axis=i))
            blocks.extend(temp)
81
            i += 1
Z
zhunaipan 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        return blocks

    def generate_data(self, shape):
        data = np.random.rand(*shape)
        return data

    def input_data(self, shape):
        data = (self.generate_data(shape)*2).astype(np.float32)
        stra = [1]*len(shape)
        stra[0] = device_num
        datas = self.get_parallel_blocks(data, stra)
        return Tensor(data), Tensor(datas[rank_id])

    def label_data(self, shape, classes):
        data = (self.generate_data(shape)*(classes-1)).astype(np.int32)
        stra = [1]*len(shape)
        stra[0] = device_num
        datas = self.get_parallel_blocks(data, stra)
100 101
        return Tensor(data), Tensor(datas[rank_id])

Z
zhunaipan 已提交
102 103 104 105 106

class OneHotFactory:
    def __init__(self, batch_size, classes, on_value=1.0, off_value=0.0, axis=None, strategy=None):
        dataGen = DataGenerator()
        self.input_full, self.input_part = dataGen.input_data((classes, batch_size))
107
        self.label_full, self.label_part = dataGen.label_data((batch_size,), classes)
Z
zhunaipan 已提交
108 109 110 111 112
        self.depth = classes
        self.on_value = on_value
        self.off_value = off_value
        self.axis = axis
        self.strategy = strategy
113

Z
zhunaipan 已提交
114
    def forward_mindspore_single_impl(self):
115 116 117
        net = Onehot(axis=self.axis,
                     depth=self.depth,
                     on_value=self.on_value,
Z
zhunaipan 已提交
118 119 120
                     off_value=self.off_value)
        out = net(self.input_full, self.label_full)
        return out
121

Z
zhunaipan 已提交
122 123
    def forward_mindspore_parallel_impl(self):
        context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
124 125 126
        net = Onehot(axis=self.axis,
                     depth=self.depth,
                     on_value=self.on_value,
Z
zhunaipan 已提交
127 128 129 130 131 132 133 134 135 136 137
                     off_value=self.off_value, strategy=self.strategy)
        out = net.compile_and_run(self.input_full, self.label_full)
        return out

    def forward_cmp(self):
        out_mindspore_single = self.forward_mindspore_single_impl().asnumpy()
        context.reset_auto_parallel_context()
        out_mindspore_parallel = self.forward_mindspore_parallel_impl().asnumpy()
        context.reset_auto_parallel_context()
        assert np.allclose(out_mindspore_single, out_mindspore_parallel, 0.0001, 0.0001)

L
lichenever 已提交
138

Z
zhunaipan 已提交
139 140 141 142 143 144
def test_reid_onehot_forward_int32_128_depth1024_model_parallel():
    fact = OneHotFactory(batch_size=128,
                         classes=1024,
                         on_value=1.000000,
                         off_value=0.000000,
                         axis=-1,
145
                         strategy=((1, device_num), (), ()))
Z
zhunaipan 已提交
146 147
    fact.forward_cmp()

L
lichenever 已提交
148

Z
zhunaipan 已提交
149 150 151 152 153 154
def test_reid_onehot_forward_int32_1024_depth128_model_parallel():
    fact = OneHotFactory(batch_size=1024,
                         classes=128,
                         on_value=1.000000,
                         off_value=0.000000,
                         axis=-1,
155
                         strategy=((1, device_num), (), ()))
Z
zhunaipan 已提交
156
    fact.forward_cmp()