mmcv/tests/test_ops/test_masked_conv2d.py
ckirchhoff de6b0021af
[Feature] Support masked_conv in Ascend device (#2387)
* init npu

* add npu extension and focal loss adapter

* clean code

* clean code

* clean code

* clean code

* fix autocast bugs on npu (#2273)

fix autocast bugs on npu (#2273)

* code format

* code format

* code format

* bug fix

* pytorch_npu_helper.hpp clean code

* Npu dev (#2306)

* fix autocast bugs on npu
* using scatter_kwargs in mmcv.device.scatter_gather

* raise ImportError when compile with npu

* add npu test case (#2307)

* add npu test case

* Update focal_loss.py

* add comment

* clean lint

* update dtype assert

* update DDP forward and comment

* fix bug

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>

* sigmoidfocalloss npu adapter bug fix

* BugFix: modify softmaxFocalLoss adapter

* BugFix: remove equal sign in the code

* add npu install information in README

* add modulatedDeformConv npu adapter

* init npu

* add npu extension and focal loss adapter

* clean code

* clean code

* clean code

* add modulatedDeformConv npu adapter

* merge master branch 20221103

* Add masked_ Conv2d operator in NPU

* add nms_npu

* fix bug

* fix code check

* fix code check

* fix code check

* Masked_conv2d NPU

* Masked_conv2d NPU

* Masked_conv2d NPU

* remove npu-install-info in README.md

* annotate the clang-format in pre-commit-config-zh-ch.yaml

* Clean code: fix the clean code problem in masked_conv2d and modulated_deform_conv

Co-authored-by: wangjiangben <wangjiangben_hw@126.com>
Co-authored-by: wangjiangben-hw <111729245+wangjiangben-hw@users.noreply.github.com>
Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: zcc-zjut <zcczxy2019@163.com>
Co-authored-by: wangxiaoxin_sherie <wangxiaoxin7@huawei.com>
Co-authored-by: momo609 <963372609@qq.com>
2022-11-15 10:23:17 +08:00

46 lines
1.8 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import numpy as np
import pytest
import torch
from mmcv.utils import IS_CUDA_AVAILABLE, IS_MLU_AVAILABLE, IS_NPU_AVAILABLE
class TestMaskedConv2d:
@pytest.mark.parametrize('device', [
pytest.param(
'cuda',
marks=pytest.mark.skipif(
not IS_CUDA_AVAILABLE, reason='requires CUDA support')),
pytest.param(
'mlu',
marks=pytest.mark.skipif(
not IS_MLU_AVAILABLE, reason='requires MLU support')),
pytest.param(
'npu',
marks=pytest.mark.skipif(
not IS_NPU_AVAILABLE, reason='requires NPU support'))
])
def test_masked_conv2d_all_close(self, device):
from mmcv.ops import MaskedConv2d
np_input = np.load(
'tests/data/for_masked_conv2d/masked_conv2d_for_input.npy')
np_mask = np.load(
'tests/data/for_masked_conv2d/masked_conv2d_for_mask.npy')
np_weight = np.load(
'tests/data/for_masked_conv2d/masked_conv2d_for_weight.npy')
np_bias = np.load(
'tests/data/for_masked_conv2d/masked_conv2d_for_bias.npy')
np_output = np.load(
'tests/data/for_masked_conv2d/masked_conv2d_for_output.npy')
input = torch.tensor(np_input, dtype=torch.float, device=device)
mask = torch.tensor(np_mask, dtype=torch.float, device=device)
weight = torch.tensor(np_weight, dtype=torch.float, device=device)
bias = torch.tensor(np_bias, dtype=torch.float, device=device)
conv = MaskedConv2d(3, 3, 3, 1, 1).to(device)
conv.weight = torch.nn.Parameter(weight)
conv.bias = torch.nn.Parameter(bias)
output = conv(input, mask)
assert np.allclose(output.data.cpu().numpy(), np_output, 1e-3)