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

[Refactor] Adapt mlu code to cntoolkit3.0.1 #2248

Merged
merged 3 commits into from
Oct 10, 2022

Conversation

defei-coder
Copy link
Contributor

@defei-coder defei-coder commented Sep 7, 2022

Thanks for your contribution and we appreciate it a lot. The following instructions would make your pull request more healthy and more easily get feedback. If you do not understand some items, don't worry, just make the pull request and seek help from maintainers.

Motivation

The motivation of the PR is to support building mmcv with cntoolkit3.0.1.

It includes three parts:

  1. Instruction replacement, the following substitutions are included:
  • __bang_add_const replaced by __bang_add_scalar
  • __bang_mul_const replaced by __bang_mul_scalar
  • __bang_sub_const replaced by __bang_sub_scalar
  • __nramset replaced by __bang_write_value

Modification

  1. MLU src code
  • mmcv/ops/csrc/common/mlu/bbox_overlaps_mlu_kernel.mlu
  • mmcv/ops/csrc/common/mlu/carafe_mlu_kernel.mlu
  • mmcv/ops/csrc/common/mlu/common_mlu_helper.hpp
  • mmcv/ops/csrc/common/mlu/nms_mlu_kernel.mlu
  • mmcv/ops/csrc/common/mlu/nms_utils.hpp
  • mmcv/ops/csrc/common/mlu/psamask_mlu_kernel.mlu
  • mmcv/ops/csrc/common/mlu/roi_align_mlu_kernel.mlu
  • mmcv/ops/csrc/common/mlu/roi_pool_mlu_kernel.mlu
  • mmcv/ops/csrc/common/mlu/tin_shift_mlu_kernel.mlu

BC-breaking (Optional)

Does the modification introduce changes that break the backward-compatibility of the downstream repositories?
If so, please describe how it breaks the compatibility and how the downstream projects should modify their code to keep compatibility with this PR.

Use cases (Optional)

If this PR introduces a new feature, it is better to list some use cases here, and update the documentation.

Checklist

Before PR:

  • I have read and followed the workflow indicated in the CONTRIBUTING.md to create this PR.
  • Pre-commit or linting tools indicated in CONTRIBUTING.md are used to fix the potential lint issues.
  • Bug fixes are covered by unit tests, the case that causes the bug should be added in the unit tests.
  • New functionalities are covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • The documentation has been modified accordingly, including docstring or example tutorials.

After PR:

  • If the modification has potential influence on downstream or other related projects, this PR should be tested with some of those projects, like MMDet or MMCls.
  • CLA has been signed and all committers have signed the CLA in this PR.

@grimoire
Copy link
Member

grimoire commented Sep 8, 2022

focal loss unit test failed with log:


____________________ Testfocalloss.test_sigmoid_float[mlu] _____________________

self = <test_focal_loss.Testfocalloss object at 0x7fdea3511fd0>, device = 'mlu'

    @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'))
    ])
    def test_sigmoid_float(self, device):
>       self._test_sigmoid(device=device, dtype=torch.float)

tests/test_ops/test_focal_loss.py:143:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <test_focal_loss.Testfocalloss object at 0x7fdea3511fd0>, device = 'mlu'
dtype = torch.float32

    def _test_sigmoid(self, device, dtype=torch.float):
        from mmcv.ops import sigmoid_focal_loss
        alpha = 0.25
        gamma = 2.0
        for case, output in zip(inputs, sigmoid_outputs):
            np_x = np.array(case[0])
            np_y = np.array(case[1])
            np_x_grad = np.array(output[1])

            x = torch.from_numpy(np_x).to(device).type(dtype)
            x.requires_grad_()
            y = torch.from_numpy(np_y).to(device).long()

            loss = sigmoid_focal_loss(x, y, gamma, alpha, None, 'mean')
            loss.backward()

>           assert np.allclose(loss.data.cpu().numpy(), output[0], 1e-2)
E           AssertionError: assert False
E            +  where False = <function allclose at 0x7fdedfe79f28>(array(0., dtype=float32), 0.13562961, 0.01)
E            +    where <function allclose at 0x7fdedfe79f28> = np.allclose
E            +    and   array(0., dtype=float32) = <built-in method numpy of Tensor object at 0x7fdea2080678>()
E            +      where <built-in method numpy of Tensor object at 0x7fdea2080678> = tensor(0.).numpy
E            +        where tensor(0.) = <built-in method cpu of Tensor object at 0x7fdea20804c8>()
E            +          where <built-in method cpu of Tensor object at 0x7fdea20804c8> = tensor(0., device='mlu:0').cpu
E            +            where tensor(0., device='mlu:0') = tensor(0., device='mlu:0', grad_fn=<SigmoidFocalLossFunctionBackward>).data

tests/test_ops/test_focal_loss.py:79: AssertionError
_____________________ Testfocalloss.test_sigmoid_half[mlu] _____________________

self = <test_focal_loss.Testfocalloss object at 0x7fdea351f358>, device = 'mlu'

    @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'))
    ])
    def test_sigmoid_half(self, device):
>       self._test_sigmoid(device, dtype=torch.half)

tests/test_ops/test_focal_loss.py:156:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <test_focal_loss.Testfocalloss object at 0x7fdea351f358>, device = 'mlu'
dtype = torch.float16

    def _test_sigmoid(self, device, dtype=torch.float):
        from mmcv.ops import sigmoid_focal_loss
        alpha = 0.25
        gamma = 2.0
        for case, output in zip(inputs, sigmoid_outputs):
            np_x = np.array(case[0])
            np_y = np.array(case[1])
            np_x_grad = np.array(output[1])

            x = torch.from_numpy(np_x).to(device).type(dtype)
            x.requires_grad_()
            y = torch.from_numpy(np_y).to(device).long()

            loss = sigmoid_focal_loss(x, y, gamma, alpha, None, 'mean')
            loss.backward()

>           assert np.allclose(loss.data.cpu().numpy(), output[0], 1e-2)
E           AssertionError: assert False
E            +  where False = <function allclose at 0x7fdedfe79f28>(array(0., dtype=float16), 0.13562961, 0.01)
E            +    where <function allclose at 0x7fdedfe79f28> = np.allclose
E            +    and   array(0., dtype=float16) = <built-in method numpy of Tensor object at 0x7fdea35236c0>()
E            +      where <built-in method numpy of Tensor object at 0x7fdea35236c0> = tensor(0., dtype=torch.float16).numpy
E            +        where tensor(0., dtype=torch.float16) = <built-in method cpu of Tensor object at 0x7fdea3523750>()
E            +          where <built-in method cpu of Tensor object at 0x7fdea3523750> = tensor(0., device='mlu:0', dtype=torch.float16).cpu
E            +            where tensor(0., device='mlu:0', dtype=torch.float16) = tensor(0., device='mlu:0', dtype=torch.float16,\n grad_fn=<SigmoidFocalLossFunctionBackward>).data

tests/test_ops/test_focal_loss.py:79: AssertionError

Copy link
Member

@grimoire grimoire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Success after update cntoolkit.

@grimoire grimoire self-requested a review September 20, 2022 12:15
@CLAassistant
Copy link

CLAassistant commented Oct 10, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@grimoire grimoire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zhouzaida zhouzaida merged commit b1711db into open-mmlab:master Oct 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants