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] Fix warning with torch.meshgrid. #860

Merged
merged 3 commits into from
Sep 30, 2022
Merged
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
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