Skip to content

Vectorize Recency Sampler Using Circular Buffers - #62

Merged
Jacob-Chmura merged 19 commits into
mainfrom
perf/recency
Jul 3, 2025
Merged

Vectorize Recency Sampler Using Circular Buffers#62
Jacob-Chmura merged 19 commits into
mainfrom
perf/recency

Conversation

@Jacob-Chmura

@Jacob-Chmura Jacob-Chmura commented Jun 24, 2025

Copy link
Copy Markdown
Member

Purpose

The purpose of this PR is to make an implementation change in our recency sampler hook to speed up it's execution.

Specifically, I moved away from python queues and wrote a fully torch-native implementation using circular buffers, which can be stored on GPU.

Outcome (independent of sparse dedup hook improvements #60)

End to end latency reduction by ~81%

Case (perf/dict_idx)

Screenshot from 2025-06-24 16-17-56

Control (perf/recency)

Screenshot from 2025-06-25 08-26-21

Key Changes

  • TODO

Relevant Prs

Close #61

@Jacob-Chmura Jacob-Chmura self-assigned this Jun 24, 2025
@Jacob-Chmura Jacob-Chmura changed the title Vectorize Recency Sampler Vectorize Recency Sampler Using Circular Buffers Jun 25, 2025
@Jacob-Chmura
Jacob-Chmura marked this pull request as ready for review June 25, 2025 12:27
Comment thread examples/linkproppred/tgat.py Outdated
Comment thread examples/linkproppred/tgat.py
Comment thread tgm/hooks.py
Comment thread tgm/hooks.py Outdated
@codecov

codecov Bot commented Jul 2, 2025

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 8.00000% with 46 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
tgm/hooks.py 8.00% 46 Missing ⚠️

📢 Thoughts on this report? Let us know!

Comment thread tgm/hooks.py
fake_times = torch.randint(
int(batch.time.min().item()),
int(batch.time.max().item()),
int(batch.time.max().item()) + 1,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is to be inclusive of the maxtime right

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment for us to remember in the future
timestamps for negatives destinations in a batch is sampled from [batch_min_time, batch_max_time] inclusive.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah will do

Comment thread tgm/hooks.py
@@ -293,7 +303,7 @@ def __call__(self, dg: DGraph, batch: DGBatch) -> DGBatch:
# leakage, making the prediction easier than it should be.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this mean? should we remove the leakage comment?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This explains why we chose our heuristic for assigning timestamps on negative edges. We don't do random timestamps because this would give an implicit signal to the model about which edges are positive and negative.

Comment thread tgm/hooks.py
def _get_recency_indices(self, node_ids: torch.Tensor, k: int) -> torch.Tensor:
ptr = self._nbr_ptr[node_ids].unsqueeze(1)
offsets = torch.arange(k, device=node_ids.device).unsqueeze(0)
indices = (ptr - 1 - offsets) % self._max_nbrs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks correct, though with pointer and everything, should make sure it is working as intended. What happens when a node has no neighbors whatsoever?

@Jacob-Chmura
Jacob-Chmura merged commit 40f0bdb into main Jul 3, 2025
5 checks passed
@Jacob-Chmura
Jacob-Chmura deleted the perf/recency branch July 3, 2025 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vectorize the Recency Sampler Hook

2 participants