!32672 add ut for save_graphs

Merge pull request !32672 from huanghui/add-ut-for-save_graphs
This commit is contained in:
i-robot 2022-04-13 01:33:12 +00:00 committed by Gitee
commit fb4ac3e4cb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
11 changed files with 223 additions and 43 deletions

View File

@ -21,7 +21,6 @@ from mindspore.ops import composite as C
from mindspore import context
from mindspore.ops import functional as F
from mindspore.common.parameter import Parameter
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
@ -79,11 +78,9 @@ def test_forward():
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_backward():
# Graph Mode
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
graph_forward_net = ForwardNet(max_cycles=10)

View File

@ -19,7 +19,6 @@ from mindspore import nn
from mindspore import Tensor
from mindspore.ops import composite as C
from mindspore import context
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
@ -70,10 +69,8 @@ def test_forward():
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_backward():
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
forward_net = ForwardNet(max_cycles=3)

View File

@ -20,7 +20,6 @@ from mindspore import nn
from mindspore import Tensor
from mindspore.ops import composite as C
from mindspore import context
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
@ -75,10 +74,8 @@ def test_forward():
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_backward():
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
forward_net = ForwardNet(max_cycles=3)

View File

@ -20,7 +20,6 @@ from mindspore.common.parameter import Parameter
from mindspore.ops import composite as C
from mindspore.ops import operations as P
from mindspore.common import dtype as mstype
from tests.security_utils import security_off_wrap
grad_all = C.GradOperation(get_all=True)
@pytest.mark.level0
@ -81,7 +80,6 @@ def test_for_in_while_01():
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_for_in_while_02():
class ForInWhileNet(nn.Cell):
def __init__(self):
@ -115,7 +113,6 @@ def test_for_in_while_02():
# graph mode
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
for_in_while_net = ForInWhileNet()
net = GradNet(for_in_while_net)
graph_forward_res = for_in_while_net(x)

View File

@ -175,7 +175,6 @@ def test_if_after_if_in_for_02():
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_if_after_if_in_for_03():
context.set_context(save_graphs=True, save_graphs_path="./test_112_graph_ir")
x = Tensor(2, mstype.int32)
expect1 = Tensor(11, mstype.int32)
expect2 = (Tensor(1, mstype.int32),)

View File

@ -22,7 +22,6 @@ from mindspore.ops import composite as C
from mindspore.ops import functional as F
from mindspore import context
from mindspore.common.parameter import Parameter
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
@ -133,13 +132,11 @@ class BackwardNetNoAssign(nn.Cell):
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_backward_no_assign():
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
# Graph Mode
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
graph_forward_net = ForwardNetNoAssign(max_cycles=3)
graph_backward_net = BackwardNetNoAssign(graph_forward_net)
graph_mode_grads = graph_backward_net(x, y)

View File

@ -20,7 +20,6 @@ from mindspore import nn
from mindspore import Tensor
from mindspore.ops import composite as C
from mindspore import context
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
@ -76,10 +75,8 @@ def test_forward():
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_backward():
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
forward_net = ForwardNet(max_cycles=3)

View File

@ -20,7 +20,6 @@ from mindspore import nn
from mindspore import Tensor
from mindspore.ops import composite as C
from mindspore import context
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
@ -64,10 +63,8 @@ class BackwardNet(nn.Cell):
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_forward():
context.set_context(mode=context.GRAPH_MODE)
context.set_context(save_graphs=True)
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
forward_net = ForwardNet(max_cycles=3)

View File

@ -21,7 +21,6 @@ from mindspore.ops import composite as C
from mindspore.ops import operations as P
from mindspore import context
from mindspore.common.parameter import Parameter
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
grad_all = C.GradOperation(get_all=True)
@ -185,12 +184,11 @@ def test_if_after_if_in_while_break_forward():
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
@security_off_wrap
def test_if_after_if_in_while_break_backward():
x = Tensor(np.array(1), mstype.int32)
y = Tensor(np.array(3), mstype.int32)
# Graph Mode
context.set_context(mode=context.GRAPH_MODE, save_graphs=True)
context.set_context(mode=context.GRAPH_MODE)
graph_forward_net = IfAfterIfInWhileBreakForwardNet(max_cycles=10)
graph_backward_net = Grad(graph_forward_net)
graph_mode_grads = graph_backward_net(x, y)

View File

@ -17,37 +17,39 @@ Watchpoints test script for dump analyze_fail.dat when infer failed.
"""
# pylint: disable=too-many-function-args
import os
import shutil
import pytest
import mindspore
from mindspore import ops, Tensor, nn
from tests.security_utils import security_off_wrap
class Net(nn.Cell):
def __init__(self):
super().__init__()
self.add = ops.Add()
self.sub = ops.Sub()
self.mul = ops.Mul()
self.div = ops.Div()
def func(self, x, y):
return self.div(x, y)
def construct(self, x, y):
a = self.sub(x, 1)
b = self.add(a, y)
c = self.mul(b, self.func(a, a, b))
return c
@security_off_wrap
def test_infer_fail_generate_analyze_fail_dat():
def test_infer_fail_generate_analyze_fail_dat1():
"""
Feature: test dump analyze_fail.dat.
Description: test dump analyze_fail.dat if infer failed.
Expectation: success.
"""
class Net(nn.Cell):
def __init__(self):
super().__init__()
self.add = ops.Add()
self.sub = ops.Sub()
self.mul = ops.Mul()
self.div = ops.Div()
def func(self, x, y):
return self.div(x, y)
def construct(self, x, y):
a = self.sub(x, 1)
b = self.add(a, y)
c = self.mul(b, self.func(a, a, b))
return c
input1 = Tensor(3, mindspore.float32)
input2 = Tensor(2, mindspore.float32)
net = Net()
@ -56,3 +58,24 @@ def test_infer_fail_generate_analyze_fail_dat():
net(input1, input2)
assert "rank_0/om/analyze_fail.dat" in str(excinfo.value)
assert os.path.exists("./rank_0/om/analyze_fail.dat") is True
@security_off_wrap
def test_infer_fail_generate_analyze_fail_dat2():
"""
Feature: test dump analyze_fail.dat.
Description: test dump analyze_fail.dat if infer failed.
Expectation: success.
"""
input1 = Tensor(3, mindspore.float32)
input2 = Tensor(2, mindspore.float32)
net = Net()
os.environ["MS_OM_PATH"] = "./analyze_fail_dat2"
with pytest.raises(TypeError) as excinfo:
net(input1, input2)
assert "analyze_fail_dat2/rank_0/om/analyze_fail.dat" in str(excinfo.value)
assert os.path.exists("./analyze_fail_dat2/rank_0/om/analyze_fail.dat") is True
shutil.rmtree("analyze_fail_dat2")
del os.environ['MS_OM_PATH']

View File

@ -0,0 +1,181 @@
# 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.
# ============================================================================
import os
import shutil
import numpy as np
import mindspore
from mindspore import nn, Tensor, ops, context, Parameter
from tests.security_utils import security_off_wrap
context.set_context(mode=context.GRAPH_MODE)
def count_ir_files(path):
ir_files = 0
dat_files = 0
dot_files = 0
for file in os.listdir(path):
if file.endswith(".ir"):
ir_files += 1
if file.endswith(".dat"):
dat_files += 1
if file.endswith(".dot"):
dot_files += 1
return ir_files, dat_files, dot_files
def remove_path(path):
if os.path.exists(path):
shutil.rmtree(path)
class BackwardNet(nn.Cell):
def __init__(self, net):
super(BackwardNet, self).__init__(auto_prefix=False)
self.forward_net = net
self.grad = ops.GradOperation(get_all=True)
def construct(self, *inputs):
grads = self.grad(self.forward_net)(*inputs)
return grads
@security_off_wrap
def test_save_graphs1():
"""
Feature: test save_graphs.
Description: test save_graphs.
Expectation: success.
"""
class ForwardNet(nn.Cell):
def __init__(self, max_cycles=10):
super(ForwardNet, self).__init__()
self.max_cycles = max_cycles
self.i = Tensor(np.array(0), mindspore.int32)
self.zero = Tensor(np.array(0), mindspore.int32)
self.weight = Parameter(Tensor(np.array(0), mindspore.int32))
def construct(self, x, y):
i = self.i
out = self.zero
while i < self.max_cycles:
if out <= 19:
out = out + x * y
# use F.Assign will throw NameSpace error.
ops.assign(self.weight, i)
self.weight = i
i = i + 2
return out
context.set_context(save_graphs=True, save_graphs_path="test_save_graphs1")
a = Tensor(np.array(1), mindspore.int32)
b = Tensor(np.array(3), mindspore.int32)
graph_forward_net = ForwardNet(max_cycles=10)
graph_backward_net = BackwardNet(graph_forward_net)
graph_backward_net(a, b)
ir, dat, dot = count_ir_files("test_save_graphs1")
assert ir > 15
assert dat > 15
assert dot > 15
remove_path("./test_save_graphs1")
context.set_context(save_graphs=False)
@security_off_wrap
def test_save_graphs2():
"""
Feature: test save_graphs.
Description: test save_graphs.
Expectation: success.
"""
class ForwardNet(nn.Cell):
def __init__(self, max_cycles=10):
super(ForwardNet, self).__init__()
self.max_cycles = max_cycles
self.zero = Tensor(np.array(0), mindspore.int32)
self.i = Tensor(np.array(0), mindspore.int32)
def construct(self, x, y):
out = self.zero
j = self.i
while j < self.max_cycles:
i = self.i
while i < self.max_cycles:
out = out + x * y
i = i + 1
j = j + 2
return out
a = Tensor(np.array(1), mindspore.int32)
b = Tensor(np.array(3), mindspore.int32)
forward_net = ForwardNet(max_cycles=4)
backward_net = BackwardNet(forward_net)
context.set_context(save_graphs=True, save_graphs_path="./test_save_graphs2/tmp")
backward_net(a, b)
ir, dat, dot = count_ir_files("test_save_graphs2/tmp")
assert ir > 15
assert dat > 15
assert dot > 15
remove_path("./test_save_graphs2")
context.set_context(save_graphs=False)
@security_off_wrap
def test_save_graphs3():
"""
Feature: test save_graphs.
Description: test save_graphs.
Expectation: success.
"""
class ForwardNetNoAssign(nn.Cell):
def __init__(self, max_cycles=10):
super(ForwardNetNoAssign, self).__init__()
self.max_cycles = max_cycles
self.zero = Tensor(np.array(0), mindspore.int32)
self.i = Tensor(np.array(0), mindspore.int32)
self.weight = Parameter(Tensor(np.array(0), mindspore.int32))
def construct(self, x, y):
out = self.zero
i = self.i
while x < y:
while i < self.max_cycles:
out = out + x * y
i = i + 1
x = x + 1
if out < 19:
out = out - 19
return out
a = Tensor(np.array(1), mindspore.int32)
b = Tensor(np.array(3), mindspore.int32)
graph_forward_net = ForwardNetNoAssign(max_cycles=4)
graph_backward_net = BackwardNet(graph_forward_net)
context.set_context(save_graphs=True, save_graphs_path="./test_save_graphs3/../test_save_graphs3")
graph_backward_net(a, b)
ir, dat, dot = count_ir_files("test_save_graphs3")
assert ir > 15
assert dat > 15
assert dot > 15
remove_path("./test_save_graphs3")
context.set_context(save_graphs=False)