@@ -119,6 +119,84 @@ def test_dygraph(self):
119
119
np .testing .assert_allclose (ref_res .flatten (), pd_res3 )
120
120
121
121
122
+ class TestCartesianProd_ZeroSize (unittest .TestCase ):
123
+ def setUp (self ):
124
+ self .init_setting ()
125
+ self .a_shape = [random .randint (1 , 5 )]
126
+ self .b_shape = [0 ]
127
+ self .a_np = np .random .random (self .a_shape ).astype (self .dtype_np )
128
+ self .b_np = np .empty (0 , self .dtype_np )
129
+
130
+ self .place = []
131
+ if (
132
+ os .environ .get ('FLAGS_CI_both_cpu_and_gpu' , 'False' ).lower ()
133
+ in ['1' , 'true' , 'on' ]
134
+ or not paddle .is_compiled_with_cuda ()
135
+ ):
136
+ self .place .append ('cpu' )
137
+ if paddle .is_compiled_with_cuda ():
138
+ self .place .append ('gpu' )
139
+
140
+ def init_setting (self ):
141
+ self .dtype_np = 'float32'
142
+
143
+ def test_static_graph (self ):
144
+ paddle .enable_static ()
145
+ startup_program = paddle .static .Program ()
146
+ main_program = paddle .static .Program ()
147
+ for place in self .place :
148
+ with paddle .static .program_guard (main_program , startup_program ):
149
+ a = paddle .static .data (
150
+ name = "a" , shape = self .a_shape , dtype = self .dtype_np
151
+ )
152
+ b = paddle .static .data (
153
+ name = "b" , shape = self .b_shape , dtype = self .dtype_np
154
+ )
155
+ out1 = paddle .cartesian_prod ([a , b ])
156
+ exe = paddle .static .Executor (place = place )
157
+ feed_list = {
158
+ "a" : self .a_np ,
159
+ "b" : self .b_np ,
160
+ }
161
+ pd_res = exe .run (
162
+ main_program ,
163
+ feed = feed_list ,
164
+ fetch_list = [out1 ],
165
+ )
166
+
167
+ ref_res = np .array (list (product (self .a_np , self .b_np ))).reshape (
168
+ [0 , 2 ]
169
+ )
170
+ np .testing .assert_allclose (ref_res , pd_res [0 ])
171
+
172
+ def test_dygraph (self ):
173
+ paddle .disable_static ()
174
+ for place in self .place :
175
+ paddle .device .set_device (place )
176
+ a = paddle .to_tensor (self .a_np )
177
+ b = paddle .to_tensor (self .b_np )
178
+
179
+ pd_res = paddle .cartesian_prod ([a , b ])
180
+ ref_res = np .array (list (product (self .a_np , self .b_np ))).reshape (
181
+ [0 , 2 ]
182
+ )
183
+ np .testing .assert_allclose (ref_res , pd_res )
184
+
185
+ def test_grad (self ):
186
+ paddle .disable_static ()
187
+ for place in self .place :
188
+ paddle .device .set_device (place )
189
+ a = paddle .to_tensor (self .a_np )
190
+ a .stop_gradient = False
191
+ b = paddle .to_tensor (self .b_np )
192
+ b .stop_gradient = False
193
+
194
+ out = paddle .cartesian_prod ([a , b ])
195
+ loss = paddle .sum (out )
196
+ loss .backward ()
197
+ np .testing .assert_allclose (a .grad .shape , a .shape )
198
+
199
+
122
200
class TestCartesianProdErrors (unittest .TestCase ):
123
201
def test_errors (self ):
124
202
def test_input_not_1D ():
0 commit comments