Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyg_lib/csrc/classes/cpu/neighbor_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ void HeteroNeighborSampler::node_temporal_sample(
if (temporal_strategy == "last" && count >= 0) {
row_start = std::max(row_start, (int64_t)(row_end - count));
}
if (row_end - row_start == 0)
return;
_sample(e_type, global_src_node, local_src_node, row_start, row_end, count,
dst_mapper, generator, out_global_dst_nodes, metapath_tracker,
return_edge_id);
Expand Down
7 changes: 7 additions & 0 deletions pyg_lib/csrc/sampler/cpu/neighbor_kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class NeighborSampler {
row_start = std::max(row_start, (scalar_t)(row_end - count));
}

if (row_end - row_start == 0)
return;

if (row_end - row_start > 1) {
TORCH_CHECK(time[col_[row_start]] <= time[col_[row_end - 1]],
"Found invalid non-sorted temporal neighborhood");
Expand Down Expand Up @@ -127,6 +130,10 @@ class NeighborSampler {
if (temporal_strategy_ == "last" && count >= 0) {
row_start = std::max(row_start, (scalar_t)(row_end - count));
}

if (row_end - row_start == 0)
return;

if (row_end - row_start > 1) {
TORCH_CHECK(time[row_start] <= time[row_end - 1],
"Found invalid non-sorted temporal neighborhood");
Expand Down
45 changes: 45 additions & 0 deletions test/csrc/sampler/test_neighbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ TEST(BasicNeighborTest, BasicAssertions) {
EXPECT_TRUE(std::get<5>(out) == expected_num_edges);
}

TEST(ZeroNeighborTest, BasicAssertions) {
auto options = at::TensorOptions().dtype(at::kLong);

const auto rowptr = at::zeros(6, options);
const auto col = at::zeros(0, options);

auto out = pyg::sampler::neighbor_sample(
/*rowptr=*/rowptr,
/*col=*/col,
/*seed=*/at::arange(0, 5, options),
/*num_neighbors=*/{-1, -1});

auto expected_row = at::zeros(0, options);
EXPECT_TRUE(at::equal(std::get<0>(out), expected_row));
auto expected_col = col;
EXPECT_TRUE(at::equal(std::get<1>(out), expected_col));
auto expected_nodes = at::arange(0, 5, options);
EXPECT_TRUE(at::equal(std::get<2>(out), expected_nodes));
auto expected_edges = at::zeros(0, options);
EXPECT_TRUE(at::equal(std::get<3>(out).value(), expected_edges));
std::vector<int64_t> expected_num_nodes = {5, 0, 0};
EXPECT_TRUE(std::get<4>(out) == expected_num_nodes);
std::vector<int64_t> expected_num_edges = {0, 0};
EXPECT_TRUE(std::get<5>(out) == expected_num_edges);
}

TEST(WithoutReplacementNeighborTest, BasicAssertions) {
auto options = at::TensorOptions().dtype(at::kLong);

Expand Down Expand Up @@ -209,6 +235,25 @@ TEST(EdgeLevelTemporalNeighborTest, BasicAssertions) {
EXPECT_TRUE(at::equal(std::get<2>(out), expected_nodes.view({-1, 2})));
auto expected_edges = at::tensor({4, 5, 6, 2, 3, 4, 5}, options);
EXPECT_TRUE(at::equal(std::get<3>(out).value(), expected_edges));

auto out2 = pyg::sampler::neighbor_sample(
/*rowptr=*/rowptr,
/*col=*/col,
/*seed=*/at::arange(2, 4, options),
/*num_neighbors=*/{1, 1},
/*node_time=*/c10::nullopt,
/*edge_time=*/edge_time,
/*seed_time=*/at::tensor({-1, -1}, options),
/*edge_weight=*/c10::nullopt,
/*csc=*/false,
/*replace=*/true,
/*directed=*/true,
/*disjoint=*/true);
EXPECT_TRUE(at::equal(std::get<0>(out2), at::zeros(0, options)));
EXPECT_TRUE(at::equal(std::get<1>(out2), at::zeros(0, options)));
EXPECT_TRUE(at::equal(std::get<2>(out2),
at::tensor({0, 2, 1, 3}, options).view({-1, 2})));
EXPECT_TRUE(at::equal(std::get<3>(out2).value(), at::zeros(0, options)));
}

TEST(HeteroNeighborTest, BasicAssertions) {
Expand Down
Loading