|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297 |
- # Copyright 2020 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.
- # ============================================================================
- """ test control ops """
- import numpy as np
- import pytest
-
- from mindspore import dtype as ms
- from mindspore import Tensor
- from mindspore import context
- from mindspore import nn
- from mindspore.common.parameter import Parameter, ParameterTuple
- from mindspore.ops import composite as C
- from mindspore.ops import operations as P
- # from tests.vm_impl.math_ops_vm_impl import *
- # from tests.vm_impl.vm_interface import *
- # from tests.vm_impl import *
- # context.set_context(save_graphs=True)
-
-
- grad_by_list = C.GradOperation(get_by_list=True)
- grad_all = C.GradOperation(get_all=True)
-
-
- def test_while_forward():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
-
- def construct(self, idx, end, x):
- while idx < end:
- part = x[idx, :, :]
- max_num = self.max(part)
- x[idx, :, 0:2] = max_num
- idx = idx + 1
- return x
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- net = MyWhileNet()
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(2), dtype=ms.int32)
- x = Tensor(np.arange(8).reshape(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_grad():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
-
- def construct(self, idx, end, x):
- while idx < end:
- part = x[idx, :, :]
- max_num = self.max(part)
- x[idx, :, 0:2] = max_num
- idx = idx + 1
- return x
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
-
- def construct(self, *inputs):
- return grad_all(self.net)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(2), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_forward():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- part = x[idx, :, :]
- max_num = self.max(part)
- x[idx, :, 0:2] = max_num
- out = out + x + self.param
- idx = idx + 1
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- net = MyWhileNet()
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(2), dtype=ms.int32)
- x = Tensor(np.arange(8).reshape(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_endless_case():
- """endless case when optmization"""
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- part = x[idx, :, :]
- out = out + part
- idx = idx + 1
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- net = MyWhileNet()
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(2), dtype=ms.int32)
- x = Tensor(np.arange(8).reshape(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_grad():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- part = x[idx, :, :]
- max_num = self.max(part)
- x[idx, :, 0:2] = max_num
- out = out + x + self.param
- idx = idx + 1
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(2), dtype=ms.int32)
- x = Tensor(np.arange(8).reshape(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_forward_with_const_branch():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- if 2 > 1:
- out = out + self.param
- else:
- out = out + idx + self.param
- idx = idx + 1
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = while_net
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_opt_endless():
- """endless during optimization case"""
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
- self.addn = P.AddN()
-
- def construct(self, idx, end, x):
- addn1 = self.addn((x, x, x))
- out = addn1
- while idx < end:
- out = self.addn((out, addn1))
- idx = idx + 1
- out = self.addn((out, x))
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
-
- def construct(self, *inputs):
- return grad_all(self.net)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.ones([2, 2, 2]).astype(np.float32) * 3, dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_no_while_call():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
-
- def construct(self, idx, end, x):
- out = self.zero
- if 2 > 1:
- out = out + self.param
- else:
- out = out + idx + self.param
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = while_net
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_grad_with_const_branch():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- if 2 > 1:
- out = out + self.param
- else:
- out = out + idx + self.param
- idx = idx + 1
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_for_while_with_param_grad_with_const_branch():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
- self.start = Tensor(np.array(0), dtype=ms.int32)
-
- def construct(self, idx, end, x):
- out = self.zero
- for _ in range(0, 2):
- idx = self.start
- while idx < end:
- if 2 > 1:
- out = out + self.param
- else:
- out = out + idx + self.param
- idx = idx + 1
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_for_while_with_param_grad_basic():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
- self.start = Tensor(np.array(0), dtype=ms.int32)
-
- def construct(self, idx, end, x):
- out = self.zero
- for _ in range(0, 2):
- idx = self.start
- while idx < end:
- out = out + self.param
- idx = idx + 1
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_for_while_with_param_grad_normal():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.reduce = P.ReduceSum()
- self.start = Tensor(np.array(0), dtype=ms.int32)
-
- def construct(self, idx, end, x):
- out = x
- for _ in range(0, 2):
- idx = self.start
- while idx < end:
- out = out + self.param
- idx = idx + 1
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_basic_grad():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.t2 = Tensor(np.array(2), dtype=ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- out = out + self.param
- idx = idx + 1
- return out + self.param
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(3), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_basic_grad_mul():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.ones(([2, 2, 2])), ms.float32)
- self.t2 = Tensor(np.array(2), dtype=ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- out = out * self.param
- idx = idx + 1
- return out + self.param
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(3), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_basic_grad_two():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.weight = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="loss")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.t2 = Tensor(np.array(2), dtype=ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- out = out + self.param + self.weight
- idx = idx + 1
- return out + self.param
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(3), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_basic_grad_three():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.weight = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="loss")
- self.key = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="key")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.t2 = Tensor(np.array(2), dtype=ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- out = out + self.param + self.weight + self.key
- idx = idx + 1
- return out + self.param
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(3), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_if_with_param_grad():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
- self.t2 = Tensor(np.array(2), dtype=ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- if self.max(out) < self.max(x):
- out = out + self.param * 2
- else:
- out = out + self.param
- idx = idx + 1
- return out + self.param
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(3), dtype=ms.int32)
- x = Tensor(np.ones([2, 2, 2]).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_while_with_param_grad_not_enter_while():
- class MyWhileNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, idx, end, x):
- out = self.zero
- while idx < end:
- out = out + self.param * 3
- idx = idx + 1
- return out + self.param
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, a, b, c):
- return grad_by_list(self.net, self.weights)(a, b, c)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- while_net = MyWhileNet()
- net = GradNet(while_net)
- idx = Tensor(np.array(3), dtype=ms.int32)
- end = Tensor(np.array(0), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_with_param_if_by_if_forward():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, a, b, x):
- out = self.zero
- if a < b:
- out = out + x + self.param
- else:
- out = out + x
- if a == b:
- out = out + x*3 + self.param
- else:
- out = out + x*2
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(4), dtype=ms.int32)
- x = Tensor(np.ones([2, 2, 2]).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_with_param_if_by_if_grad_inputs():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, a, b, x):
- out = self.zero
- if a < b:
- out = out + x + self.param * 4
- if a == b:
- out = out + x*3 + self.param * 3
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
-
- def construct(self, *inputs):
- return grad_all(self.net)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = GradNet(if_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(0), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_with_param_if_by_if_grad_parameter():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, a, b, x):
- out = self.zero
- if a < b:
- out = out + x + self.param * 2
- if a == b:
- out = out + x*3 + self.param
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = GradNet(if_net)
- idx = Tensor(np.array(0), dtype=ms.int32)
- end = Tensor(np.array(2), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_with_param_if_by_if_grad_param_excute_null():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, a, b, x):
- out = self.zero
- if a < b:
- out = out + x + self.param * 2
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = GradNet(if_net)
- idx = Tensor(np.array(4), dtype=ms.int32)
- end = Tensor(np.array(0), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_if_by_if_return_inside_grad():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.max = P.ReduceMax()
- self.param = Parameter(Tensor(np.arange(2 * 2 * 2).reshape((2, 2, 2)), ms.float32), name="weight")
- self.zero = Tensor(np.zeros(([2, 2, 2])), ms.float32)
-
- def construct(self, a, b, x):
- out = self.zero
- if a < b:
- return out + x + self.param
- if a == b:
- return out + self.param * 2
- return out + self.param * 3
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = GradNet(if_net)
- idx = Tensor(np.array(1), dtype=ms.int32)
- end = Tensor(np.array(0), dtype=ms.int32)
- x = Tensor(np.random.randn(2, 2, 2).astype(np.float32), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_if_by_if_forward():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- if a < b:
- a = self.add(a, b)
- else:
- a = self.sub(a, b)
- if a == x:
- a = self.mul(a, b)
- else:
- a = self.div(a, b)
- if b == x:
- b = self.add(a, b)
- else:
- b = self.add(a, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(4), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_if_by_if_forward_control_tuple_switch():
- """tuple_get from swtich op will generate new switch inside to eliminate tuple_get"""
- class Branch3Net(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- if b == x:
- b = self.add(a, b)
- else:
- b = self.add(a, x)
- return a, b, x
- class Branch2Net(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
- self.net = Branch3Net()
-
- def construct(self, a, b, x):
- if a == x:
- a = self.mul(a, b)
- else:
- a = self.div(a, b)
- return self.net(a, b, x)
-
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
- self.net = Branch2Net()
-
- def construct(self, a, b, x):
- if a < b:
- a = self.add(a, b)
- else:
- a = self.sub(a, b)
- a, b, x = self.net(a, b, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
-
-
- def test_if_by_if_forward_control_inside_net():
- class Branch3Net(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- if b == x:
- b = self.add(a, b)
- else:
- b = self.add(a, x)
- a = a * b
- out = a + b + x
- return out
- class Branch2Net(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
- self.net = Branch3Net()
-
- def construct(self, a, b, x):
- if a == x:
- a = self.mul(a, b)
- else:
- a = self.div(a, b)
- return self.net(a, b, x)
-
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
- self.net = Branch2Net()
-
- def construct(self, a, b, x):
- if a < b:
- a = self.add(a, b)
- else:
- a = self.sub(a, b)
- out = self.net(a, b, x)
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
-
- def test_if_by_if_forward_use_namespace():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- if a < b:
- a = P.TensorAdd()(a, b)
- else:
- a = P.Sub()(a, b)
- if a == x:
- a = P.Mul()(a, b)
- else:
- a = P.RealDiv()(a, b)
- if b == x:
- b = P.TensorAdd()(a, b)
- else:
- b = P.TensorAdd()(a, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_if_by_if_forward_use_global_op():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- add = P.TensorAdd()
- sub = P.Sub()
- mul = P.Mul()
- div = P.RealDiv()
- if a < b:
- a = add(a, b)
- else:
- a = sub(a, b)
- if a == x:
- a = mul(a, b)
- else:
- a = div(a, b)
- if b == x:
- b = add(a, b)
- else:
- b = add(a, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
- def test_for_with_if_by_if_forward():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
-
- def construct(self, a, b, x):
- for _ in range(0, 4):
- if a < b:
- a = self.add(a, b)
- else:
- b = self.sub(b, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
-
- def test_for_with_if_by_if_forward_namespace():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- for _ in range(0, 6):
- if a < b:
- a = P.TensorAdd()(a, b)
- else:
- b = P.Sub()(b, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
-
- def test_if_by_if_forward_const_branch_inner():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- add = P.TensorAdd()
- sub = P.Sub()
- mul = P.Mul()
- div = P.RealDiv()
- if a < b:
- a = add(a, b)
- else:
- a = sub(a, b)
- if 2 > 1:
- a = mul(a, b)
- else:
- a = div(a, b)
- if b == x:
- b = add(a, b)
- else:
- b = add(a, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
-
-
- def test_if_by_if_forward_all_const_branch():
- class MyIfByIfNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
- self.sub = P.Sub()
- self.mul = P.Mul()
- self.div = P.RealDiv()
-
- def construct(self, a, b, x):
- add = P.TensorAdd()
- sub = P.Sub()
- mul = P.Mul()
- div = P.RealDiv()
- if 2 < 12:
- a = add(a, b)
- else:
- a = sub(a, b)
- if 2 > 1:
- a = mul(a, b)
- else:
- a = div(a, b)
- if 2 == 1:
- b = add(a, b)
- else:
- b = add(a, x)
- a = a * b
- out = a + b + x
- return out
-
- context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
- if_net = MyIfByIfNet()
- net = if_net
- idx = Tensor(np.array(2), dtype=ms.float32)
- end = Tensor(np.array(3), dtype=ms.float32)
- x = Tensor(np.array(0), dtype=ms.float32)
- net(idx, end, x)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_if_const_grad():
- class MyNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
-
- def construct(self, *inputs):
- out = self.add(*inputs)
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- a = 1
- b = 2
- if a > 0:
- b = 1
- a += b
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE)
- my_net = MyNet()
- net = GradNet(my_net)
- a = Tensor(np.array(0), dtype=ms.int32)
- b = Tensor(np.array(1), dtype=ms.int32)
- net(a, b)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_if_by_if_const_grad():
- class MyNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
-
- def construct(self, *inputs):
- out = self.add(*inputs)
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- a = 1
- b = 2
- if a > 0:
- b = 1
- if a < 0:
- b = 0
- if a == 0:
- b = 3
- a += b
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE)
- my_net = MyNet()
- net = GradNet(my_net)
- a = Tensor(np.array(0), dtype=ms.int32)
- b = Tensor(np.array(1), dtype=ms.int32)
- net(a, b)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_while_const_grad():
- class MyNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
-
- def construct(self, *inputs):
- out = self.add(*inputs)
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- a = 1
- while a > 1:
- a = a - 1
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE)
- my_net = MyNet()
- net = GradNet(my_net)
- a = Tensor(np.array(0), dtype=ms.int32)
- b = Tensor(np.array(1), dtype=ms.int32)
- net(a, b)
-
-
- @pytest.mark.level0
- @pytest.mark.platform_x86_cpu
- @pytest.mark.env_onecard
- def test_if_by_while_const_grad():
- class MyNet(nn.Cell):
- def __init__(self):
- super().__init__()
- self.add = P.TensorAdd()
-
- def construct(self, *inputs):
- out = self.add(*inputs)
- return out
-
- class GradNet(nn.Cell):
- def __init__(self, net):
- super(GradNet, self).__init__()
- self.net = net
- self.weights = ParameterTuple(net.trainable_params())
-
- def construct(self, *inputs):
- a = 1
- b = 2
- if a > 0:
- b = 0
- while a > 1:
- a = a - 1
- a += b
- return grad_by_list(self.net, self.weights)(*inputs)
-
- context.set_context(mode=context.GRAPH_MODE)
- my_net = MyNet()
- net = GradNet(my_net)
- a = Tensor(np.array(0), dtype=ms.int32)
- b = Tensor(np.array(1), dtype=ms.int32)
- net(a, b)
|