Repository navigation
fix: make bar distribution reductions device-agnostic #1366
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
Closed
aniruddhaadak80
wants to merge
2
commits into
PriorLabs:main
from
aniruddhaadak80:fix/bardist-device-agnostic-reductions
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Regressor output types other than `mean` no longer fail when the bar distribution and the logits are on different devices. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # Copyright (c) Prior Labs GmbH 2026. | ||
|
|
||
| """Reductions must work when the criterion and the logits are on different devices. | ||
|
|
||
| `mean` already re-homed its buffer; `median`, `mode` and `icdf` indexed | ||
| `borders` directly and raised. That can happen whenever an estimator is | ||
| reassembled and only part of it is moved — e.g. restoring a fitted regressor | ||
| and placing the weights but not the bar distribution. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
| import torch | ||
|
|
||
| from tabpfn.architectures.shared.bar_distribution import FullSupportBarDistribution | ||
|
|
||
|
|
||
| def _second_device() -> torch.device | None: | ||
| if torch.cuda.is_available(): | ||
| return torch.device("cuda", 0) | ||
| if torch.backends.mps.is_available(): | ||
| return torch.device("mps") | ||
| return None | ||
|
|
||
|
|
||
| def _reductions(bd: FullSupportBarDistribution, logits: torch.Tensor) -> dict: | ||
| return { | ||
| "mean": bd.mean(logits), | ||
| "median": bd.median(logits), | ||
| "mode": bd.mode(logits), | ||
| "icdf": bd.icdf(logits, 0.9), | ||
| "quantile": bd.quantile(logits), | ||
| } | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def borders() -> torch.Tensor: | ||
| return torch.linspace(-3, 3, 101) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def logits() -> torch.Tensor: | ||
| return torch.randn(64, 100, generator=torch.Generator().manual_seed(0)) | ||
|
|
||
|
|
||
| def test_reductions_run_with_borders_on_another_device(borders, logits): | ||
| device = _second_device() | ||
| if device is None: | ||
| pytest.skip("needs a second device") | ||
|
|
||
| expected = _reductions(FullSupportBarDistribution(borders.clone()), logits) | ||
| # Borders left on CPU, logits on the compute device. | ||
| got = _reductions(FullSupportBarDistribution(borders.clone()), logits.to(device)) | ||
|
|
||
| for name, value in got.items(): | ||
| assert value.device.type == device.type, name | ||
| torch.testing.assert_close( | ||
| value.cpu().float(), expected[name].float(), rtol=0, atol=1e-4, msg=name | ||
| ) | ||
|
|
||
|
|
||
| def test_co_located_results_are_unchanged(borders, logits): | ||
| """The re-homing must be a no-op when the devices already match.""" | ||
| bd = FullSupportBarDistribution(borders) | ||
| first = _reductions(bd, logits) | ||
| second = _reductions(bd, logits) | ||
| for name, value in first.items(): | ||
| assert torch.equal(value, second[name]), name | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test docstring narrates prior failure
Low Severity
The module docstring recounts what previous code did and why it raised, including that
meanalready re-homed its buffer whilemedian,mode, andicdfindexedbordersdirectly. Docstrings should state current behavior only, not the incident or prior implementation.Reviewed by Cursor Bugbot for commit afb1eb2. Configure here.