test_element_wise_function.py 10.5 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# 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
J
jinyaohui 已提交
16

Z
zhunaipan 已提交
17 18 19
import mindspore as ms
import mindspore.nn as nn
from mindspore import Tensor
J
jinyaohui 已提交
20
from mindspore import context
Z
zhunaipan 已提交
21 22
from mindspore.common.api import _executor
from mindspore.ops import composite as C
J
jinyaohui 已提交
23 24
from mindspore.ops import operations as P
from tests.ut.python.ops.test_math_ops import VirtualLoss
Z
zhunaipan 已提交
25 26


P
panyifeng 已提交
27 28 29
grad_all = C.GradOperation('get_all', get_all=True)


Z
zhunaipan 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
class NetWithLoss(nn.Cell):
    def __init__(self, network):
        super(NetWithLoss, self).__init__()
        self.loss = VirtualLoss()
        self.network = network

    def construct(self, x, y, b):
        predict = self.network(x, y, b)
        return self.loss(predict)


class GradWrap(nn.Cell):
    def __init__(self, network):
        super(GradWrap, self).__init__()
        self.network = network

    def construct(self, x, y, b):
P
panyifeng 已提交
47
        return grad_all(self.network)(x, y, b)
Z
zhunaipan 已提交
48 49


Y
Yi Huaijie 已提交
50
def compile_net(net, x, y, b):
Y
yangzhenzhang 已提交
51 52 53 54
    net.set_auto_parallel()
    _executor.compile(net, x, y, b)


Z
zhunaipan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
def test_matmul_pow():
    class Net(nn.Cell):
        def __init__(self, strategy1, strategy2):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.pow = P.Pow().set_strategy(strategy2)
            self.matmul2 = P.MatMul().set_strategy(strategy1)

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            out = self.pow(out, 2.0)
            out = self.matmul2(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
71
    strategy2 = ((4, 2), ())
Z
zhunaipan 已提交
72 73 74 75 76 77
    net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")

    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
78
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96


def test_matmul_exp():
    class Net(nn.Cell):
        def __init__(self, strategy1, strategy2):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.exp = P.Exp().set_strategy(strategy2)
            self.matmul2 = P.MatMul().set_strategy(strategy1)

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            out = self.exp(out)
            out = self.matmul2(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
J
jinyaohui 已提交
97
    strategy2 = ((4, 2),)
Z
zhunaipan 已提交
98 99 100 101 102 103
    net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")

    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
104
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122


def test_matmul_log():
    class Net(nn.Cell):
        def __init__(self, strategy1, strategy2):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.log = P.Log().set_strategy(strategy2)
            self.matmul2 = P.MatMul().set_strategy(strategy1)

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            out = self.log(out)
            out = self.matmul2(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
J
jinyaohui 已提交
123
    strategy2 = ((4, 2),)
Z
zhunaipan 已提交
124 125 126 127 128 129
    net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")

    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
130
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148


def test_matmul_logical_not():
    class Net(nn.Cell):
        def __init__(self, strategy1, strategy2, strategy3):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.logicalnot = P.LogicalNot().set_strategy(strategy2)
            self.equal = P.Equal().set_strategy(strategy3)

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            out = self.equal(out, b)
            out = self.logicalnot(out)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
J
jinyaohui 已提交
149
    strategy2 = ((4, 2),)
Z
zhunaipan 已提交
150 151 152 153 154 155 156
    strategy3 = ((4, 2), (4, 2))
    net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")

    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
    b = Tensor(np.ones([128, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
157
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
158

J
jinyaohui 已提交
159

Z
zhunaipan 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
def test_matmul_cast():
    class Net(nn.Cell):
        def __init__(self, strategy1, strategy2, strategy3):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.cast = P.Cast().set_strategy(strategy2)
            self.matmul2 = P.MatMul().set_strategy(strategy3)

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            b = self.cast(b, ms.float32)
            out = self.matmul2(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
J
jinyaohui 已提交
176
    strategy2 = ((4, 2),)
Z
zhunaipan 已提交
177 178 179 180 181 182 183
    strategy3 = ((1, 4), (4, 2))
    net = GradWrap(NetWithLoss(Net(strategy1, strategy2, strategy3)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")

    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
    b = Tensor(np.ones([64, 64]), dtype=ms.int32)
Y
Yi Huaijie 已提交
184
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203


def test_cast_before_mirror():
    class Net(nn.Cell):
        def __init__(self, strategy1):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.cast = P.Cast()

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            b = self.cast(b, ms.float32)
            out = self.matmul(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0, cast_before_mirror=True)
    strategy1 = ((2, 2), (2, 2))
    net = GradWrap(NetWithLoss(Net(strategy1)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
J
jinyaohui 已提交
204

Z
zhunaipan 已提交
205 206 207
    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([32, 64]), dtype=ms.float32)
    b = Tensor(np.ones([64, 64]), dtype=ms.float16)
Y
Yi Huaijie 已提交
208
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227


def test_cast_before_mirror1():
    class Net(nn.Cell):
        def __init__(self, strategy1):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.cast = P.Cast()

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            b = self.cast(b, ms.float16)
            out = self.matmul(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0, cast_before_mirror=True)
    strategy1 = ((2, 2), (2, 2))
    net = GradWrap(NetWithLoss(Net(strategy1)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
J
jinyaohui 已提交
228

Z
zhunaipan 已提交
229 230 231
    x = Tensor(np.ones([128, 32]), dtype=ms.float16)
    y = Tensor(np.ones([32, 64]), dtype=ms.float16)
    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
232
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251


def test_cast_before_mirror2():
    class Net(nn.Cell):
        def __init__(self, strategy1):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.cast = P.Cast()

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            b = self.cast(b, ms.float16)
            out = self.matmul(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0, cast_before_mirror=False)
    strategy1 = ((2, 2), (2, 2))
    net = GradWrap(NetWithLoss(Net(strategy1)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
J
jinyaohui 已提交
252

Z
zhunaipan 已提交
253 254 255
    x = Tensor(np.ones([128, 32]), dtype=ms.float16)
    y = Tensor(np.ones([32, 64]), dtype=ms.float16)
    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
256
    compile_net(net, x, y, b)
Z
zhunaipan 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275


def test_cast_before_mirror3():
    class Net(nn.Cell):
        def __init__(self, strategy1):
            super().__init__()
            self.matmul = P.MatMul().set_strategy(strategy1)
            self.cast = P.Cast()

        def construct(self, x, y, b):
            out = self.matmul(x, y)
            b = self.cast(b, ms.float16)
            out = self.matmul(out, b)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
    net = GradWrap(NetWithLoss(Net(strategy1)))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
J
jinyaohui 已提交
276

Z
zhunaipan 已提交
277 278 279
    x = Tensor(np.ones([128, 32]), dtype=ms.float16)
    y = Tensor(np.ones([32, 64]), dtype=ms.float16)
    b = Tensor(np.ones([64, 64]), dtype=ms.float32)
Y
Yi Huaijie 已提交
280
    compile_net(net, x, y, b)
L
lichenever 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301


def test_mul_two_cast():
    class Net(nn.Cell):
        def __init__(self, strategy1, strategy2, strategy3):
            super().__init__()
            self.mul = P.Mul().set_strategy(strategy1)
            self.mul2 = P.Mul().set_strategy(strategy2)
            self.cast = P.Cast().set_strategy(strategy3)
            self.cast2 = P.Cast().set_strategy(strategy3)

        def construct(self, x, y, b):
            out = self.mul(x, y)
            out = self.mul2(out, b)
            out = self.cast(out, ms.int32)
            out = self.cast2(out, ms.bool_)
            return out

    context.set_auto_parallel_context(device_num=8, global_rank=0)
    strategy1 = ((2, 2), (2, 2))
    strategy2 = ((8, 1), (8, 1))
J
jinyaohui 已提交
302
    strategy3 = ((8, 1),)
L
lichenever 已提交
303 304 305 306 307 308
    net = GradWrap(Net(strategy1, strategy2, strategy3))
    context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")

    x = Tensor(np.ones([128, 32]), dtype=ms.float32)
    y = Tensor(np.ones([128, 32]), dtype=ms.float32)
    b = Tensor(np.ones([128, 32]), dtype=ms.float32)
Y
Yi Huaijie 已提交
309
    compile_net(net, x, y, b)