Skip to content

Commit c2c0b39

Browse files
committed
Fix
1 parent b6804ae commit c2c0b39

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

test/legacy_test/test_cartesian_prod.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,72 @@ def test_dygraph(self):
119119
np.testing.assert_allclose(ref_res.flatten(), pd_res3)
120120

121121

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+
122188
class TestCartesianProdErrors(unittest.TestCase):
123189
def test_errors(self):
124190
def test_input_not_1D():

0 commit comments

Comments
 (0)