Skip to content
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

Add faster mask_select method #8369

Merged
merged 6 commits into from
Nov 14, 2023
Merged

Add faster mask_select method #8369

merged 6 commits into from
Nov 14, 2023

Conversation

toenshoff
Copy link
Contributor

This PR addresses a minor performance issue with the utils.mask.mask_select method.

The previous implementation unsqueezed the mask to be broadcastable along all dimensions.
This broadcasted masking is significantly slower in torch than applying a 1-dimensional mask to the first dimension of a tensor.
The new implementation simply transposes the tensor such that the masked dimension comes first and later undoes this.

This mainly affects the methods for computing subgraphs which are needlessly slow on bigger graphs with many features.

Here is a code example to illustrate the difference:

import torch
from torch_geometric.data import Data
from timeit import timeit


N = 1000000
E = 10000000

data = Data(
    x=torch.randn((N, 200)),
    edge_index=torch.randint(0, N, (2, E)),
    edge_attr=torch.randn((E, 200), dtype=torch.float16)
)

subset_bool = torch.rand((N,)) < 0.5
time_bool = timeit(lambda: data.subgraph(subset_bool), number=1)
print(f'Time: {time_bool:.4f}s')

The prior version yields:

Time: 13.7826s

The updated version yields:

Time:  0.3541s

So more than a 30-fold speedup. We thought this may be helpful to accelerate workflows involving subgraph extraction.

Copy link

codecov bot commented Nov 13, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (b1f8535) 88.80% compared to head (3a2d630) 88.41%.
Report is 1 commits behind head on master.

❗ Current head 3a2d630 differs from pull request most recent head 959e03b. Consider uploading reports for the commit 959e03b to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8369      +/-   ##
==========================================
- Coverage   88.80%   88.41%   -0.39%     
==========================================
  Files         475      475              
  Lines       28841    28837       -4     
==========================================
- Hits        25611    25497     -114     
- Misses       3230     3340     +110     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@akihironitta akihironitta left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@rusty1s rusty1s left a comment

Choose a reason for hiding this comment

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

Great catch, thank you!

@rusty1s rusty1s enabled auto-merge (squash) November 14, 2023 07:40
@rusty1s rusty1s merged commit bdaa2e7 into pyg-team:master Nov 14, 2023
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants