• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2020 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15import numpy as np
16
17import mindspore.context as context
18import mindspore.nn as nn
19from mindspore import Tensor
20from mindspore.ops import operations as P
21
22context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
23
24
25class Net1(nn.Cell):
26    def __init__(self):
27        super(Net1, self).__init__()
28        self.relu1 = P.ReLU()
29        self.relu2 = P.ReLU()
30        self.mul = P.Mul()
31        self.depend = P.Depend()
32
33    def construct(self, x, y):
34        a = self.relu1(x)
35        y = self.depend(y, a)
36        b = self.relu2(y)
37        c = self.mul(a, b)
38        return c, a
39
40
41class Net2(nn.Cell):
42    def __init__(self):
43        super(Net2, self).__init__()
44        self.relu1 = P.ReLU()
45        self.relu2 = P.ReLU().add_prim_attr("primitive_target", "CPU")
46        self.mul = P.Mul()
47        self.depend = P.Depend()
48
49    def construct(self, x, y):
50        a = self.relu1(x)
51        y = self.depend(y, a)
52        b = self.relu2(y)
53        c = self.mul(a, b)
54        return c, a
55
56
57def test_net():
58    x = np.random.randn(2, 3, 3, 4).astype(np.float32)
59    y = np.random.randn(2, 3, 3, 4).astype(np.float32)
60    net1 = Net1()
61    output1 = net1(Tensor(x), Tensor(y))
62
63    net2 = Net2()
64    output2 = net2(Tensor(x), Tensor(y))
65    assert np.allclose(output1[0].asnumpy(), output2[0].asnumpy())
66    print("##success##")
67
68
69if __name__ == "__main__":
70    test_net()
71