-
Notifications
You must be signed in to change notification settings - Fork 416
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
Make _stable_1d_sort(nb)
optional
#196
Closed
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 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 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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
from typing import Any, Callable, List, Mapping, Optional, Sequence, Union | ||
from typing import Any, Callable, List, Mapping, Optional, Sequence, Tuple, Union | ||
|
||
import numpy as np | ||
import torch | ||
|
@@ -151,7 +151,7 @@ def get_num_classes( | |
return num_classes | ||
|
||
|
||
def _stable_1d_sort(x: torch, nb: int = 2049): | ||
def _stable_1d_sort(x: Tensor, nb: Optional[int] = None) -> Tuple[Tensor, Tensor]: | ||
""" | ||
Stable sort of 1d tensors. Pytorch defaults to a stable sorting algorithm | ||
if number of elements are larger than 2048. This function pads the tensors, | ||
|
@@ -172,12 +172,13 @@ def _stable_1d_sort(x: torch, nb: int = 2049): | |
if x.ndim > 1: | ||
raise ValueError('Stable sort only works on 1d tensors') | ||
n = x.numel() | ||
if n < nb: | ||
x_max = x.max() | ||
x = torch.cat([x, (x_max + 1) * torch.ones(nb - n, dtype=x.dtype, device=x.device)], 0) | ||
if nb is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should just remove |
||
if n < nb: | ||
x_max = x.max() | ||
x = torch.cat([x, (x_max + 1) * torch.ones(nb - n, dtype=x.dtype, device=x.device)], 0) | ||
n = min(nb, n) | ||
x_sort = x.sort() | ||
i = min(nb, n) | ||
return x_sort.values[:i], x_sort.indices[:i] | ||
return x_sort.values[:n], x_sort.indices[:n] | ||
|
||
|
||
def apply_to_collection( | ||
|
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.
in your test, you are actually not testing stability at all, you are just testing if the sort is working at all. Can you try seeing if sorting an array of 8 zeros (and other equal values) produces indices=[0,1,2,3,..7] (so no changes are made by sort, meaning it's stable)