|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955 |
- # 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 as ms
- import mindspore.nn as nn
- from mindspore import Tensor
- from mindspore import context
- from mindspore.common.api import _executor
- from mindspore.ops import composite as C
- from mindspore.ops import operations as P
- from tests.ut.python.ops.test_math_ops import VirtualLoss
-
-
- grad_all = C.GradOperation(get_all=True)
-
-
- 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):
- return grad_all(self.network)(x, y, b)
-
-
- def compile_net(net, x, y, b):
- net.set_auto_parallel()
- _executor.compile(net, x, y, b)
-
-
- def test_matmul_pow():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.pow = P.Pow().shard(strategy2)
- self.matmul2 = P.MatMul().shard(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))
- strategy2 = ((4, 2), ())
- 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)
- compile_net(net, x, y, b)
-
-
- def test_matmul_exp():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.exp = P.Exp().shard(strategy2)
- self.matmul2 = P.MatMul().shard(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))
- strategy2 = ((4, 2),)
- 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)
- compile_net(net, x, y, b)
-
-
- def test_matmul_log():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.log = P.Log().shard(strategy2)
- self.matmul2 = P.MatMul().shard(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))
- strategy2 = ((4, 2),)
- 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)
- compile_net(net, x, y, b)
-
- def test_matmul_abs():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.abs = P.Abs().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.abs(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
- def test_matmul_sign():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.sign = P.Sign().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.sign(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_floor():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.floor = P.Floor().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.floor(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
- def test_matmul_round():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.round = P.Round().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.round(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_reciprocal():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.reciprocal = P.Reciprocal().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.reciprocal(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_inv():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.inv = P.Inv().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.inv(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_rsqrt():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.rsqrt = P.Rsqrt().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.rsqrt(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_tan():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.tan = P.Tan().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.tan(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_sin():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.sin = P.Sin().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.sin(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_sinh():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.sinh = P.Sinh().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.sinh(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_log1p():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.log1p = P.Log1p().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.log1p(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_expm1():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.expm1 = P.Expm1().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.expm1(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_cosh():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.cosh = P.Cosh().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.cosh(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
- def test_matmul_erf():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.erf = P.Erf().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.erf(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_erfc():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.erfc = P.Erfc().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.erfc(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_zeroslike():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.zeroslike = P.ZerosLike().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.zeroslike(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_oneslike():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.oneslike = P.OnesLike().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.oneslike(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_BesselI0e():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.BesselI0e = P.BesselI0e().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.BesselI0e(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_BesselI1e():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.BesselI1e = P.BesselI1e().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.BesselI1e(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(1, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(1, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(1, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_ceil():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.Ceil = P.Ceil().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.Ceil(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_atan():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.atan = P.Atan().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.atan(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_Atanh():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.atanh = P.Atanh().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.atanh(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_asin():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.asin = P.Asin().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.asin(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_asinh():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.asinh = P.Asinh().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.asinh(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_acosh():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.acosh = P.Acosh().shard(strategy2)
- self.matmul2 = P.MatMul().shard(strategy1)
-
- def construct(self, x, y, b):
- out = self.matmul(x, y)
- out = self.acosh(out)
- out = self.matmul2(out, b)
- return out
-
- context.set_auto_parallel_context(device_num=8, global_rank=0)
- strategy1 = ((2, 2), (2, 2))
- strategy2 = ((4, 2),)
- net = GradWrap(NetWithLoss(Net(strategy1, strategy2)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- x = Tensor(np.random.uniform(-5, 5, size=(128, 32)), dtype=ms.float32)
- y = Tensor(np.random.uniform(-5, 5, size=(32, 64)), dtype=ms.float32)
- b = Tensor(np.random.uniform(-5, 5, size=(64, 64)), dtype=ms.float32)
- compile_net(net, x, y, b)
-
-
- def test_matmul_logical_not():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2, strategy3):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.logicalnot = P.LogicalNot().shard(strategy2)
- self.equal = P.Equal().shard(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))
- strategy2 = ((4, 2),)
- 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)
- compile_net(net, x, y, b)
-
-
- def test_matmul_cast():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2, strategy3):
- super().__init__()
- self.matmul = P.MatMul().shard(strategy1)
- self.cast = P.Cast().shard(strategy2)
- self.matmul2 = P.MatMul().shard(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))
- strategy2 = ((4, 2),)
- 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)
- compile_net(net, x, y, b)
-
-
- def test_gradient_fp32_sync():
- class Net(nn.Cell):
- def __init__(self, strategy1):
- super().__init__()
- self.matmul = P.MatMul().shard(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, gradient_fp32_sync=True)
- strategy1 = ((2, 2), (2, 2))
- net = GradWrap(NetWithLoss(Net(strategy1)))
- 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.float16)
- compile_net(net, x, y, b)
-
-
- def test_gradient_fp32_sync1():
- class Net(nn.Cell):
- def __init__(self, strategy1):
- super().__init__()
- self.matmul = P.MatMul().shard(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, gradient_fp32_sync=True)
- strategy1 = ((2, 2), (2, 2))
- net = GradWrap(NetWithLoss(Net(strategy1)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- 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)
- compile_net(net, x, y, b)
-
-
- def test_gradient_fp32_sync2():
- class Net(nn.Cell):
- def __init__(self, strategy1):
- super().__init__()
- self.matmul = P.MatMul().shard(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, gradient_fp32_sync=False)
- strategy1 = ((2, 2), (2, 2))
- net = GradWrap(NetWithLoss(Net(strategy1)))
- context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
-
- 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)
- compile_net(net, x, y, b)
-
-
- def test_gradient_fp32_sync3():
- class Net(nn.Cell):
- def __init__(self, strategy1):
- super().__init__()
- self.matmul = P.MatMul().shard(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")
-
- 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)
- compile_net(net, x, y, b)
-
-
- def test_mul_two_cast():
- class Net(nn.Cell):
- def __init__(self, strategy1, strategy2, strategy3):
- super().__init__()
- self.mul = P.Mul().shard(strategy1)
- self.mul2 = P.Mul().shard(strategy2)
- self.cast = P.Cast().shard(strategy3)
- self.cast2 = P.Cast().shard(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))
- strategy3 = ((8, 1),)
- 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)
- compile_net(net, x, y, b)
|