Skip to content

Commit

Permalink
[Fix] Fix warning with torch.meshgrid. (open-mmlab#860)
Browse files Browse the repository at this point in the history
* Fix warning with torch.meshgrid

* Add torch_meshgrid_ij wrapper

* Use `digit_version` instead of packaging package.

Co-authored-by: mzr1996 <mzr1996@163.com>
  • Loading branch information
pallgeuer and mzr1996 authored Sep 30, 2022
1 parent 1b4e9cd commit 7dca27d
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tests/test_models/test_utils/test_attention.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# Copyright (c) OpenMMLab. All rights reserved.
from functools import partial
from unittest import TestCase
from unittest.mock import ANY, MagicMock

import pytest
import torch
from mmcv.utils import TORCH_VERSION, digit_version

from mmcls.models.utils.attention import ShiftWindowMSA, WindowMSA

if digit_version(TORCH_VERSION) >= digit_version('1.10.0a0'):
torch_meshgrid_ij = partial(torch.meshgrid, indexing='ij')
else:
torch_meshgrid_ij = torch.meshgrid # Uses indexing='ij' by default


def get_relative_position_index(window_size):
"""Method from original code of Swin-Transformer."""
coords_h = torch.arange(window_size[0])
coords_w = torch.arange(window_size[1])
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
coords = torch.stack(torch_meshgrid_ij([coords_h, coords_w])) # 2, Wh, Ww
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
# 2, Wh*Ww, Wh*Ww
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :]
Expand Down

0 comments on commit 7dca27d

Please sign in to comment.