@@ -20,8 +20,8 @@ def test_id():
2020 ex = create_executor ()
2121 x = rand (dtype , * shape )
2222 forward , (grad ,) = ex .evaluate (back_func )(x )
23- tvm .testing .assert_allclose (forward .asnumpy (), x .asnumpy ())
24- tvm .testing .assert_allclose (grad .asnumpy (), np .ones_like (x .asnumpy ()))
23+ np .testing .assert_allclose (forward .asnumpy (), x .asnumpy ())
24+ np .testing .assert_allclose (grad .asnumpy (), np .ones_like (x .asnumpy ()))
2525
2626
2727def test_add ():
@@ -35,8 +35,8 @@ def test_add():
3535 ex = create_executor ()
3636 x = rand (dtype , * shape )
3737 forward , (grad ,) = ex .evaluate (back_func )(x )
38- tvm .testing .assert_allclose (forward .asnumpy (), 2 * x .asnumpy ())
39- tvm .testing .assert_allclose (grad .asnumpy (), 2 * np .ones_like (x .asnumpy ()))
38+ np .testing .assert_allclose (forward .asnumpy (), 2 * x .asnumpy ())
39+ np .testing .assert_allclose (grad .asnumpy (), 2 * np .ones_like (x .asnumpy ()))
4040
4141
4242def test_temp_add ():
@@ -51,8 +51,8 @@ def test_temp_add():
5151 ex = create_executor ()
5252 x = rand (dtype , * shape )
5353 forward , (grad ,) = ex .evaluate (back_func )(x )
54- tvm .testing .assert_allclose (forward .asnumpy (), 4 * x .asnumpy ())
55- tvm .testing .assert_allclose (grad .asnumpy (), 4 * np .ones_like (x .asnumpy ()))
54+ np .testing .assert_allclose (forward .asnumpy (), 4 * x .asnumpy ())
55+ np .testing .assert_allclose (grad .asnumpy (), 4 * np .ones_like (x .asnumpy ()))
5656
5757
5858def test_sub ():
@@ -66,8 +66,8 @@ def test_sub():
6666 ex = create_executor ()
6767 x = rand (dtype , * shape )
6868 forward , (grad ,) = ex .evaluate (back_func )(x )
69- tvm .testing .assert_allclose (forward .asnumpy (), np .zeros_like (x .asnumpy ()))
70- tvm .testing .assert_allclose (grad .asnumpy (), np .zeros_like (x .asnumpy ()))
69+ np .testing .assert_allclose (forward .asnumpy (), np .zeros_like (x .asnumpy ()))
70+ np .testing .assert_allclose (grad .asnumpy (), np .zeros_like (x .asnumpy ()))
7171
7272
7373def test_broadcast_add ():
@@ -90,11 +90,11 @@ def test_broadcast_add():
9090 relay .TupleType ([t1 , t2 ])]))
9191 ex = create_executor ()
9292 forward , (grad_x , grad_y ) = ex .evaluate (full_func )(x_nd , y_nd )
93- tvm .testing .assert_allclose (forward .asnumpy (), expected_forward )
94- tvm .testing .assert_allclose (grad_x .asnumpy (),
95- np .ones_like (expected_forward ).sum (axis = 2 , keepdims = True ))
96- tvm .testing .assert_allclose (grad_y .asnumpy (),
97- np .ones_like (expected_forward ).sum (axis = (0 , 1 ), keepdims = True ).squeeze (axis = 0 ))
93+ np .testing .assert_allclose (forward .asnumpy (), expected_forward )
94+ np .testing .assert_allclose (grad_x .asnumpy (),
95+ np .ones_like (expected_forward ).sum (axis = 2 , keepdims = True ))
96+ np .testing .assert_allclose (grad_y .asnumpy (),
97+ np .ones_like (expected_forward ).sum (axis = (0 , 1 ), keepdims = True ).squeeze (axis = 0 ))
9898
9999
100100def test_broadcast_subtract ():
@@ -117,11 +117,11 @@ def test_broadcast_subtract():
117117 relay .TupleType ([t1 , t2 ])]))
118118 ex = create_executor ()
119119 forward , (grad_x , grad_y ) = ex .evaluate (full_func )(x_nd , y_nd )
120- tvm .testing .assert_allclose (forward .asnumpy (), expected_forward )
121- tvm .testing .assert_allclose (grad_x .asnumpy (),
122- np .ones_like (expected_forward ).sum (axis = 2 , keepdims = True ))
123- tvm .testing .assert_allclose (grad_y .asnumpy (),
124- - np .ones_like (expected_forward ).sum (axis = (0 , 1 ), keepdims = True ).squeeze (axis = 0 ))
120+ np .testing .assert_allclose (forward .asnumpy (), expected_forward )
121+ np .testing .assert_allclose (grad_x .asnumpy (),
122+ np .ones_like (expected_forward ).sum (axis = 2 , keepdims = True ))
123+ np .testing .assert_allclose (grad_y .asnumpy (),
124+ - np .ones_like (expected_forward ).sum (axis = (0 , 1 ), keepdims = True ).squeeze (axis = 0 ))
125125
126126
127127def test_tuple ():
@@ -147,10 +147,10 @@ def test_tuple():
147147 expected_forward = x_np + y_np - z_np
148148 ex = create_executor ()
149149 forward , (grad_x , grad_y , grad_z ) = ex .evaluate (back_func )(x_nd , y_nd , z_nd )
150- tvm .testing .assert_allclose (forward .asnumpy (), expected_forward )
151- tvm .testing .assert_allclose (grad_x .asnumpy (), np .ones_like (grad_x .asnumpy ()))
152- tvm .testing .assert_allclose (grad_y .asnumpy (), np .ones_like (grad_y .asnumpy ()))
153- tvm .testing .assert_allclose (grad_z .asnumpy (), - 1 * np .ones_like (grad_z .asnumpy ()))
150+ np .testing .assert_allclose (forward .asnumpy (), expected_forward )
151+ np .testing .assert_allclose (grad_x .asnumpy (), np .ones_like (grad_x .asnumpy ()))
152+ np .testing .assert_allclose (grad_y .asnumpy (), np .ones_like (grad_y .asnumpy ()))
153+ np .testing .assert_allclose (grad_z .asnumpy (), - 1 * np .ones_like (grad_z .asnumpy ()))
154154
155155
156156def test_pow ():
@@ -168,9 +168,8 @@ def test_pow():
168168 i_nd = rand (dtype , * shape )
169169 ex = create_executor (mod = mod )
170170 forward , (grad_i ,) = ex .evaluate (back_func )(i_nd )
171- tvm .testing .assert_allclose (forward .asnumpy (), 8 * i_nd .asnumpy ())
172- tvm .testing .assert_allclose (grad_i .asnumpy (), 8 * np .ones_like (grad_i .asnumpy ()))
173-
171+ np .testing .assert_allclose (forward .asnumpy (), 8 * i_nd .asnumpy ())
172+ np .testing .assert_allclose (grad_i .asnumpy (), 8 * np .ones_like (grad_i .asnumpy ()))
174173
175174def test_ref ():
176175 shape = (10 , 10 )
@@ -188,28 +187,8 @@ def test_ref():
188187 x_nd = rand (dtype , * shape )
189188 ex = create_executor ()
190189 forward , (grad_x ,) = ex .evaluate (back_func )(x_nd )
191- tvm .testing .assert_allclose (forward .asnumpy (), 2 * x_nd .asnumpy ())
192- tvm .testing .assert_allclose (grad_x .asnumpy (), 2 * np .ones_like (grad_x .asnumpy ()))
193-
194-
195- def test_square_second_order ():
196- shape = (10 , 10 )
197- dtype = 'float32'
198- t = relay .TensorType (shape , dtype )
199- x = relay .var ("x" , t )
200- func = relay .Function ([x ], x * x )
201- back_func = relay .ir_pass .infer_type (gradient (func ))
202- y = relay .var ("y" , t )
203- back_func_adjusted = relay .Function ([y ], relay .TupleGetItem (relay .TupleGetItem (back_func (y ), 1 ), 0 ))
204- back_func_adjusted = relay .ir_pass .infer_type (back_func_adjusted )
205- back_back_func = relay .ir_pass .infer_type (gradient (back_func_adjusted ))
206- assert back_func .checked_type == relay .FuncType ([t ], relay .TupleType ([t , relay .TupleType ([t ])]))
207- x_nd = rand (dtype , * shape )
208- ex = create_executor ()
209- forward , (grad_x ,) = ex .evaluate (back_back_func )(x_nd )
210- tvm .testing .assert_allclose (forward .asnumpy (), 2 * x_nd .asnumpy ())
211- tvm .testing .assert_allclose (grad_x .asnumpy (), 2 * np .ones_like (grad_x .asnumpy ()))
212-
190+ np .testing .assert_allclose (forward .asnumpy (), 2 * x_nd .asnumpy ())
191+ np .testing .assert_allclose (grad_x .asnumpy (), 2 * np .ones_like (grad_x .asnumpy ()))
213192
214193if __name__ == "__main__" :
215194 test_id ()
@@ -221,4 +200,3 @@ def test_square_second_order():
221200 test_tuple ()
222201 test_pow ()
223202 test_ref ()
224- test_square_second_order ()
0 commit comments