Skip to content

Implement weighted random walks #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Fix edge weight normalization
  • Loading branch information
pbielak committed Sep 9, 2022
commit 2b63087b865b1db6de7ecb0128417b38d16f059e
10 changes: 9 additions & 1 deletion csrc/cpu/rw_cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,18 @@ void compute_cdf(const int64_t *rowptr, const float_t *edge_weight,
at::parallel_for(0, numel - 1, at::internal::GRAIN_SIZE, [&](int64_t begin, int64_t end) {
for(int64_t i = begin; i < end; i++) {
int64_t row_start = rowptr[i], row_end = rowptr[i + 1];

// Compute sum to normalize weights
float_t sum = 0.0;

for(int64_t j = row_start; j < row_end; j++) {
sum += edge_weight[j];
}

float_t acc = 0.0;

for(int64_t j = row_start; j < row_end; j++) {
acc += edge_weight[j];
acc += edge_weight[j] / sum;
edge_weight_cdf[j] = acc;
}
}
Expand Down
8 changes: 7 additions & 1 deletion csrc/cuda/rw_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,16 @@ __global__ void cdf_kernel(const int64_t *rowptr, const float_t *edge_weight,
if (thread_idx < numel - 1) {
int64_t row_start = rowptr[thread_idx], row_end = rowptr[thread_idx + 1];

float_t sum = 0.0;

for(int64_t i = row_start; i < row_end; i++) {
sum += edge_weight[i];
}

float_t acc = 0.0;

for(int64_t i = row_start; i < row_end; i++) {
acc += edge_weight[i];
acc += edge_weight[i] / sum;
edge_weight_cdf[i] = acc;
}
}
Expand Down
3 changes: 0 additions & 3 deletions torch_cluster/rw.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ def random_walk(
rowptr, col, start, walk_length, p, q,
)
else:
# Normalize edge weights by node degrees
edge_weight = edge_weight / deg[row]

node_seq, edge_seq = torch.ops.torch_cluster.random_walk_weighted(
rowptr, col, edge_weight, start, walk_length, p, q,
)
Expand Down