forked from open-mmlab/mmcv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add torch_meshgrid wrapper due to PyTorch change (open-mmlab#2044)
* Add torch_meshgrid_ij wrapper due to PyTorch change * Update torch_meshgrid name/doc/version implementation * Make imports local * add ut * ignore ut when torch is not available Co-authored-by: zhouzaida <zhouzaida@163.com>
- Loading branch information
Showing
4 changed files
with
63 additions
and
2 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import torch | ||
|
||
from .parrots_wrapper import TORCH_VERSION | ||
from .version_utils import digit_version | ||
|
||
_torch_version_meshgrid_indexing = ( | ||
'parrots' not in TORCH_VERSION | ||
and digit_version(TORCH_VERSION) >= digit_version('1.10.0a0')) | ||
|
||
|
||
def torch_meshgrid(*tensors): | ||
"""A wrapper of torch.meshgrid to compat different PyTorch versions. | ||
Since PyTorch 1.10.0a0, torch.meshgrid supports the arguments ``indexing``. | ||
So we implement a wrapper here to avoid warning when using high-version | ||
PyTorch and avoid compatibility issues when using previous versions of | ||
PyTorch. | ||
Args: | ||
tensors (List[Tensor]): List of scalars or 1 dimensional tensors. | ||
Returns: | ||
Sequence[Tensor]: Sequence of meshgrid tensors. | ||
""" | ||
if _torch_version_meshgrid_indexing: | ||
return torch.meshgrid(*tensors, indexing='ij') | ||
else: | ||
return torch.meshgrid(*tensors) # Uses indexing='ij' by default |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
import pytest | ||
import torch | ||
|
||
from mmcv.utils import torch_meshgrid | ||
|
||
|
||
def test_torch_meshgrid(): | ||
# torch_meshgrid should not throw warning | ||
with pytest.warns(None) as record: | ||
x = torch.tensor([1, 2, 3]) | ||
y = torch.tensor([4, 5, 6]) | ||
grid_x, grid_y = torch_meshgrid(x, y) | ||
|
||
assert len(record) == 0 |