1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include <iostream>
17 #include <unordered_map>
18
19 #include "frontend/optimizer/ad/grad.h"
20 #include "common/common_test.h"
21 #include "common/py_func_graph_fetcher.h"
22 #include "ir/manager.h"
23 #include "ir/value.h"
24 #include "ir/func_graph_cloner.h"
25 #include "utils/log_adapter.h"
26 #include "ir/graph_utils.h"
27 #include "pipeline/jit/resource.h"
28 #include "pipeline/jit/parse/parse.h"
29 #include "debug/draw.h"
30 #include "frontend/operator/ops.h"
31
32 namespace mindspore {
33 namespace ad {
34 class TestAD : public UT::Common {
35 public:
TestAD()36 TestAD() : getPyFun("gtest_input.optimizer.ad", true) {}
37
38 public:
39 UT::PyFuncGraphFetcher getPyFun;
40 pipeline::ResourcePtr resourcePtr = std::make_shared<pipeline::Resource>();
41
42 protected:
AssertExpect(const std::string & testCase)43 void AssertExpect(const std::string& testCase) {
44 FuncGraphPtr g = getPyFun(testCase);
45 resourcePtr->manager()->RemoveRoots();
46 resourcePtr->manager()->AddFuncGraph(g, true);
47 FuncGraphPtr dg = Grad(g, resourcePtr);
48 AssertExpect(testCase, dg);
49 }
50
AssertExpect(const std::string & testCase,const FuncGraphPtr & dg)51 void AssertExpect(const std::string& testCase, const FuncGraphPtr& dg) { ASSERT_TRUE(dg != nullptr); }
52 };
53
TEST_F(TestAD,test_null)54 TEST_F(TestAD, test_null) { AssertExpect("test_null"); }
55
TEST_F(TestAD,test_grad_add)56 TEST_F(TestAD, test_grad_add) { AssertExpect("test_grad_add"); }
57
TEST_F(TestAD,test_grad_expr)58 TEST_F(TestAD, test_grad_expr) { AssertExpect("test_grad_expr"); }
59
TEST_F(TestAD,test_constant)60 TEST_F(TestAD, test_constant) { AssertExpect("test_constant"); }
61
TEST_F(TestAD,test_dup_args_in_call)62 TEST_F(TestAD, test_dup_args_in_call) { AssertExpect("test_dup_args_in_call"); }
63
TEST_F(TestAD,test_quadruple_args_in_call)64 TEST_F(TestAD, test_quadruple_args_in_call) { AssertExpect("test_quadruple_args_in_call"); }
65
TEST_F(TestAD,test_tuples)66 TEST_F(TestAD, test_tuples) { AssertExpect("test_tuples"); }
67
TEST_F(TestAD,test_hof)68 TEST_F(TestAD, test_hof) { AssertExpect("test_hof"); }
69
TEST_F(TestAD,test_more_hof)70 TEST_F(TestAD, test_more_hof) { AssertExpect("test_more_hof"); }
71
TEST_F(TestAD,test_simple_closure)72 TEST_F(TestAD, test_simple_closure) { AssertExpect("test_simple_closure"); }
73
TEST_F(TestAD,test_closure)74 TEST_F(TestAD, test_closure) { AssertExpect("test_closure"); }
75
TEST_F(TestAD,test_if)76 TEST_F(TestAD, test_if) { AssertExpect("test_if"); }
77
TEST_F(TestAD,test_if2)78 TEST_F(TestAD, test_if2) { AssertExpect("test_if2"); }
79
TEST_F(TestAD,test_fact)80 TEST_F(TestAD, test_fact) { AssertExpect("test_fact"); }
81
TEST_F(TestAD,test_while)82 TEST_F(TestAD, test_while) { AssertExpect("test_while"); }
83
TEST_F(TestAD,test_while_2)84 TEST_F(TestAD, test_while_2) { AssertExpect("test_while_2"); }
85
TEST_F(TestAD,test_pow10)86 TEST_F(TestAD, test_pow10) { AssertExpect("test_pow10"); }
87
TEST_F(TestAD,test_closures_in_tuples)88 TEST_F(TestAD, test_closures_in_tuples) { AssertExpect("test_closures_in_tuples"); }
89
TEST_F(TestAD,test_ops_fn)90 TEST_F(TestAD, test_ops_fn) { AssertExpect("test_ops_fn"); }
91
TEST_F(TestAD,test_more_closure)92 TEST_F(TestAD, test_more_closure) { AssertExpect("test_more_closure"); }
93
TEST_F(TestAD,test_prim_scalar_add)94 TEST_F(TestAD, test_prim_scalar_add) {
95 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarAdd), resourcePtr);
96 AssertExpect("test_prim_scalar_add", dg);
97 }
98
TEST_F(TestAD,test_prim_scalar_mul)99 TEST_F(TestAD, test_prim_scalar_mul) {
100 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarMul), resourcePtr);
101 AssertExpect("test_prim_scalar_mul", dg);
102 }
103
TEST_F(TestAD,test_prim_scalar_sub)104 TEST_F(TestAD, test_prim_scalar_sub) {
105 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarSub), resourcePtr);
106 AssertExpect("test_prim_scalar_sub", dg);
107 }
108
TEST_F(TestAD,test_prim_scalar_div)109 TEST_F(TestAD, test_prim_scalar_div) {
110 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarDiv), resourcePtr);
111 AssertExpect("test_prim_scalar_div", dg);
112 }
113
TEST_F(TestAD,test_prim_scalar_pow)114 TEST_F(TestAD, test_prim_scalar_pow) {
115 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarPow), resourcePtr);
116 AssertExpect("test_prim_scalar_pow", dg);
117 }
118
TEST_F(TestAD,test_prim_scalar_exp)119 TEST_F(TestAD, test_prim_scalar_exp) {
120 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarExp), resourcePtr);
121 AssertExpect("test_prim_scalar_exp", dg);
122 }
123
TEST_F(TestAD,test_prim_scalar_uadd)124 TEST_F(TestAD, test_prim_scalar_uadd) {
125 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarUadd), resourcePtr);
126 AssertExpect("test_prim_scalar_uadd", dg);
127 }
128
TEST_F(TestAD,test_prim_scalar_usub)129 TEST_F(TestAD, test_prim_scalar_usub) {
130 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarUsub), resourcePtr);
131 AssertExpect("test_prim_scalar_usub", dg);
132 }
133
TEST_F(TestAD,test_prim_scalar_gt)134 TEST_F(TestAD, test_prim_scalar_gt) {
135 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarGt), resourcePtr);
136 AssertExpect("test_prim_scalar_gt", dg);
137 }
138
TEST_F(TestAD,test_prim_scalar_lt)139 TEST_F(TestAD, test_prim_scalar_lt) {
140 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarLt), resourcePtr);
141 AssertExpect("test_prim_scalar_lt", dg);
142 }
143
TEST_F(TestAD,test_prim_scalar_ge)144 TEST_F(TestAD, test_prim_scalar_ge) {
145 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarGe), resourcePtr);
146 AssertExpect("test_prim_scalar_ge", dg);
147 }
148
TEST_F(TestAD,test_prim_scalar_le)149 TEST_F(TestAD, test_prim_scalar_le) {
150 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarLe), resourcePtr);
151 AssertExpect("test_prim_scalar_le", dg);
152 }
153
TEST_F(TestAD,test_prim_tuple_getitem)154 TEST_F(TestAD, test_prim_tuple_getitem) {
155 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimTupleGetItem), resourcePtr);
156 AssertExpect("test_prim_tuple_getitem", dg);
157 }
158
TEST_F(TestAD,test_prim_identity)159 TEST_F(TestAD, test_prim_identity) {
160 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimIdentity), resourcePtr);
161 AssertExpect("test_prim_identity", dg);
162 }
163
TEST_F(TestAD,test_prim_scalar_to_array)164 TEST_F(TestAD, test_prim_scalar_to_array) {
165 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimScalarToArray), resourcePtr);
166 AssertExpect("test_prim_scalar_to_array", dg);
167 }
168
TEST_F(TestAD,test_prim_array_to_scalar)169 TEST_F(TestAD, test_prim_array_to_scalar) {
170 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimArrayToScalar), resourcePtr);
171 AssertExpect("test_prim_array_to_scalar", dg);
172 }
173
TEST_F(TestAD,test_prim_distribute)174 TEST_F(TestAD, test_prim_distribute) {
175 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimDistribute), resourcePtr);
176 AssertExpect("test_prim_distribute", dg);
177 }
178
TEST_F(TestAD,test_prim_broadcast_shape)179 TEST_F(TestAD, test_prim_broadcast_shape) {
180 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimBroadcastShape), resourcePtr);
181 AssertExpect("test_prim_broadcast_shape", dg);
182 }
183
TEST_F(TestAD,test_prim_switch)184 TEST_F(TestAD, test_prim_switch) {
185 FuncGraphPtr dg = Kprim(NewValueNode(prim::kPrimSwitch), resourcePtr);
186 AssertExpect("test_prim_switch", dg);
187 }
188
TEST_F(TestAD,test_grad_cache)189 TEST_F(TestAD, test_grad_cache) {
190 FuncGraphPtr g = getPyFun("test_null");
191 FuncGraphPtr dg1 = Grad(g, resourcePtr);
192 FuncGraphPtr dg2 = Grad(g, resourcePtr);
193 ASSERT_TRUE(dg1 == dg2);
194 }
195
TEST_F(TestAD,test_constant_output)196 TEST_F(TestAD, test_constant_output) { AssertExpect("test_constant_output"); }
197
198 } // namespace ad
199 } // namespace mindspore
200