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

Fix a bug in the QuantileAggregation with the dim size parameter passed #7407

Merged
merged 3 commits into from
May 23, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Changed

- Fixed an index-out-of-range bug in `QuantileAggregation` when `dim_size` is passed ([#7407](https://github.com/pyg-team/pytorch_geometric/pull/7407))
- The `filter_per_worker` option will not get automatically inferred by default based on the device of the underlying data ([#7399](https://github.com/pyg-team/pytorch_geometric/pull/7399))
- Fixed a bug in `LightGCN.recommendation_loss()` to only use the embeddings of the nodes involved in the current mini-batch ([#7384](https://github.com/pyg-team/pytorch_geometric/pull/7384))
- Added an optional `max_num_elements` argument to `SortAggregation` ([#7367](https://github.com/pyg-team/pytorch_geometric/pull/7367))
Expand Down
16 changes: 12 additions & 4 deletions test/nn/aggr/test_quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
@pytest.mark.parametrize('q', [0., .1, .2, .3, .4, .5, .6, .7, .8, .9, 1.])
@pytest.mark.parametrize('interpolation', QuantileAggregation.interpolations)
@pytest.mark.parametrize('dim', [0, 1])
def test_quantile_aggregation(q, interpolation, dim):
@pytest.mark.parametrize('dim_size', [None, 15])
@pytest.mark.parametrize('fill_value', [0.0, 10.0])
def test_quantile_aggregation(q, interpolation, dim, dim_size, fill_value):
x = torch.tensor([
[0.0, 1.0, 2.0],
[3.0, 4.0, 5.0],
Expand All @@ -22,12 +24,18 @@ def test_quantile_aggregation(q, interpolation, dim):
])
index = torch.zeros(x.size(dim), dtype=torch.long)

aggr = QuantileAggregation(q=q, interpolation=interpolation)
aggr = QuantileAggregation(q=q, interpolation=interpolation,
fill_value=fill_value)
assert str(aggr) == f"QuantileAggregation(q={q})"

out = aggr(x, index, dim=dim)
out = aggr(x, index, dim=dim, dim_size=dim_size)
expected = x.quantile(q, dim, interpolation=interpolation, keepdim=True)
assert torch.allclose(out, expected)

assert torch.allclose(out.narrow(dim, 0, 1), expected)

if out.size(0) > index.max() + 1:
padding = out.narrow(dim, 1, out.size(dim) - 1)
assert torch.allclose(padding, torch.tensor(fill_value))


def test_median_aggregation():
Expand Down
5 changes: 5 additions & 0 deletions torch_geometric/nn/aggr/quantile.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ def forward(self, x: Tensor, index: Optional[Tensor] = None,
count = torch.bincount(index, minlength=dim_size or 0)
cumsum = torch.cumsum(count, dim=0) - count

# In case there exists dangling indices (`dim_size > index.max()`), we
# need to clamp them to prevent out-of-bound issues:
if dim_size is not None:
cumsum = cumsum.clamp(max=x.size(dim) - 1)

q_point = self.q * (count - 1) + cumsum
q_point = q_point.t().reshape(-1)

Expand Down