@@ -119,6 +119,72 @@ 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
+ print (pd_res )
171
+ print (ref_res .shape )
172
+ np .testing .assert_allclose (ref_res , pd_res [0 ])
173
+
174
+ def test_dygraph (self ):
175
+ paddle .disable_static ()
176
+ for place in self .place :
177
+ paddle .device .set_device (place )
178
+ a = paddle .to_tensor (self .a_np )
179
+ b = paddle .to_tensor (self .b_np )
180
+
181
+ pd_res = paddle .cartesian_prod ([a , b ])
182
+ ref_res = np .array (list (product (self .a_np , self .b_np ))).reshape (
183
+ [0 , 2 ]
184
+ )
185
+ np .testing .assert_allclose (ref_res , pd_res )
186
+
187
+
122
188
class TestCartesianProdErrors (unittest .TestCase ):
123
189
def test_errors (self ):
124
190
def test_input_not_1D ():
0 commit comments