Skip to content

Commit c31ffbe

Browse files
authored
【Hackathon No.31】 Fix Null pointer bug for Case4:paddle.incubate.graph_khop_sampler (#51784)
* fix divide zero bug for softmax_with_cross_entropy * change the single test way * fix the null pointer in graph_khop_sampler * fix the case5
1 parent 01eeba5 commit c31ffbe

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

paddle/fluid/operators/graph_khop_sampler_op.cu

+25
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,31 @@ class GraphKhopSamplerOpCUDAKernel : public framework::OpKernel<T> {
423423
std::vector<int> sample_sizes = ctx.Attr<std::vector<int>>("sample_sizes");
424424
bool return_eids = ctx.Attr<bool>("return_eids");
425425

426+
auto row_dims = src->dims();
427+
auto row_dims_lens = row_dims.size();
428+
auto col_dims = dst_count->dims();
429+
auto col_dims_lens = col_dims.size();
430+
auto x_dims = vertices->dims();
431+
auto x_dims_lens = x_dims.size();
432+
for (int i = 0; i < row_dims_lens; i++) {
433+
PADDLE_ENFORCE_NE(
434+
row_dims[i],
435+
0,
436+
phi::errors::InvalidArgument("The size of Row(X) should not be 0."));
437+
}
438+
for (int i = 0; i < col_dims_lens; i++) {
439+
PADDLE_ENFORCE_NE(col_dims[i],
440+
0,
441+
phi::errors::InvalidArgument(
442+
"The size of Col_Ptr(X) should not be 0."));
443+
}
444+
for (int i = 0; i < x_dims_lens; i++) {
445+
PADDLE_ENFORCE_NE(x_dims[i],
446+
0,
447+
phi::errors::InvalidArgument(
448+
"The size of Input_Node(X) should not be 0."));
449+
}
450+
426451
const T* src_data = src->data<T>();
427452
const T* dst_count_data = dst_count->data<T>();
428453
const T* p_vertices = vertices->data<T>();

paddle/fluid/operators/graph_khop_sampler_op.h

+25
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ class GraphKhopSamplerOpKernel : public framework::OpKernel<T> {
199199
auto* src = ctx.Input<phi::DenseTensor>("Row");
200200
auto* dst_count = ctx.Input<phi::DenseTensor>("Col_Ptr");
201201
auto* vertices = ctx.Input<phi::DenseTensor>("X");
202+
auto row_dims = src->dims();
203+
auto row_dims_lens = row_dims.size();
204+
auto col_dims = dst_count->dims();
205+
auto col_dims_lens = col_dims.size();
206+
auto x_dims = vertices->dims();
207+
auto x_dims_lens = x_dims.size();
208+
for (int i = 0; i < row_dims_lens; i++) {
209+
PADDLE_ENFORCE_NE(
210+
row_dims[i],
211+
0,
212+
phi::errors::InvalidArgument("The size of Row(X) should not be 0."));
213+
}
214+
for (int i = 0; i < col_dims_lens; i++) {
215+
PADDLE_ENFORCE_NE(col_dims[i],
216+
0,
217+
phi::errors::InvalidArgument(
218+
"The size of Col_Ptr(X) should not be 0."));
219+
}
220+
for (int i = 0; i < x_dims_lens; i++) {
221+
PADDLE_ENFORCE_NE(x_dims[i],
222+
0,
223+
phi::errors::InvalidArgument(
224+
"The size of Input_Node(X) should not be 0."));
225+
}
226+
202227
std::vector<int> sample_sizes = ctx.Attr<std::vector<int>>("sample_sizes");
203228
bool return_eids = ctx.Attr<bool>("return_eids");
204229

python/paddle/fluid/tests/unittests/test_graph_khop_sampler.py

+30
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,36 @@ def test_sample_result_static_without_eids(self):
259259
in_neighbors = np.isin(edge_src_n, self.dst_src_dict[n])
260260
self.assertTrue(np.sum(in_neighbors) == in_neighbors.shape[0])
261261

262+
def test_for_null_pointer_error(self):
263+
def test_in_row():
264+
array = np.array([], dtype=np.float32)
265+
x = paddle.to_tensor(np.reshape(array, [0]), dtype='int32')
266+
y = paddle.to_tensor([10], dtype='int32')
267+
layer = paddle.incubate.graph_khop_sampler(
268+
row=x, colptr=x, input_nodes=y, sample_sizes=[0]
269+
)
270+
271+
def test_in_col():
272+
array = np.array([], dtype=np.float32)
273+
x = paddle.to_tensor([10], dtype='int32')
274+
col = paddle.to_tensor(np.reshape(array, [0]), dtype='int32')
275+
y = paddle.to_tensor([10], dtype='int32')
276+
layer = paddle.incubate.graph_khop_sampler(
277+
row=x, colptr=col, input_nodes=y, sample_sizes=[0]
278+
)
279+
280+
def test_in_input_nodes():
281+
array = np.array([], dtype=np.float32)
282+
x = paddle.to_tensor(np.reshape(array, [0]), dtype='int32')
283+
y = paddle.to_tensor([10], dtype='int32')
284+
layer = paddle.incubate.graph_khop_sampler(
285+
row=y, colptr=y, input_nodes=x, sample_sizes=[0]
286+
)
287+
288+
self.assertRaises(ValueError, test_in_row)
289+
self.assertRaises(ValueError, test_in_col)
290+
self.assertRaises(ValueError, test_in_input_nodes)
291+
262292

263293
if __name__ == "__main__":
264294
unittest.main()

0 commit comments

Comments
 (0)