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

[Feature] Support engine with NPU backend. #572

Merged
merged 20 commits into from Oct 24, 2022

Conversation

wangjiangben-hw
Copy link
Contributor

@wangjiangben-hw wangjiangben-hw commented Oct 5, 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

Added ascending device support in mmcv.

Modification

  • The main modification points are as follows:
    1. In mmengine/device/utils.py, We've added our NPU devices, When judging whether the device is available, try to import the npu plugin package.
    2. In mmengine/dist/, We have added NPU-related distributed initialization methods.
    3. In mmengine/optim/optimizer/ and mmengine/runner/amp.py, We've added some judgment to make the mixed precision mode turn on automatically on the NPU and work properly.

BC-breaking (Optional)

None

Use cases (Optional)

  • test case
    • tests/test_device/test_device.py

Checklist

  • Pre-commit or other linting tools are used to fix the potential lint issues.
  • The modification is covered by complete unit tests. If not, please add more unit test to ensure the correctness.
  • If the modification has potential influence on downstream projects, this PR should be tested with downstream projects, like MMDet or MMCls.
  • The documentation has been modified accordingly, like docstring or example tutorials.

@wangjiangben-hw wangjiangben-hw changed the title init npu [Feature] Support engine with NPU backend. Oct 5, 2022
@ZwwWayne ZwwWayne added this to the 0.3.0 milestone Oct 8, 2022
@ZwwWayne
Copy link
Collaborator

ZwwWayne commented Oct 9, 2022

Hi @wangjiangben-hw

Thanks for your kind contribution. Would you like to fix the lint issue so that we could merge this PR?
As this issue is fixed in the most recent main branch, you cam simply fix it by rebasing your code onto the most recent main.

@zhouzaida zhouzaida self-assigned this Oct 10, 2022
mmengine/dist/dist.py Outdated Show resolved Hide resolved
mmengine/dist/dist.py Outdated Show resolved Hide resolved
mmengine/runner/amp.py Outdated Show resolved Hide resolved
mmengine/runner/amp.py Outdated Show resolved Hide resolved
@zhouzaida
Copy link
Member

zhouzaida commented Oct 12, 2022

def cuda(
self,
device: Optional[Union[int, str, torch.device]] = None,
) -> nn.Module:
"""Overrides this method to call :meth:`BaseDataPreprocessor.cuda`
additionally.
Returns:
nn.Module: The model itself.
"""
if device is None or isinstance(device, int):
device = torch.device('cuda', index=device)
self._set_device(torch.device(device))
return super().cuda(device)

@zhouzaida zhouzaida added the NPU label Oct 12, 2022
wangjiangben-hw and others added 9 commits October 12, 2022 16:24
Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
@wangjiangben-hw
Copy link
Contributor Author

def cuda(
self,
device: Optional[Union[int, str, torch.device]] = None,
) -> nn.Module:
"""Overrides this method to call :meth:`BaseDataPreprocessor.cuda`
additionally.
Returns:
nn.Module: The model itself.
"""
if device is None or isinstance(device, int):
device = torch.device('cuda', index=device)
self._set_device(torch.device(device))
return super().cuda(device)

added * Add npu method in BaseModel

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

@HAOCHENYE HAOCHENYE left a comment

Choose a reason for hiding this comment

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

is_npu_avalialble should also be added to docs/en/device.rst and docs/zh_cn/device.rst

Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com>
@wangjiangben-hw
Copy link
Contributor Author

is_npu_avalialble should also be added to docs/en/device.rst and docs/zh_cn/device.rst

added

zhouzaida
zhouzaida previously approved these changes Oct 19, 2022
@codecov
Copy link

codecov bot commented Oct 24, 2022

Codecov Report

❗ No coverage uploaded for pull request base (main@276ca24). Click here to learn what that means.
Patch has no changes to coverable lines.

❗ Current head 402f555 differs from pull request most recent head 6b963f0. Consider uploading reports for the commit 6b963f0 to get more accurate results

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #572   +/-   ##
=======================================
  Coverage        ?   77.80%           
=======================================
  Files           ?      126           
  Lines           ?     9092           
  Branches        ?     1811           
=======================================
  Hits            ?     7074           
  Misses          ?     1701           
  Partials        ?      317           
Flag Coverage Δ
unittests 77.80% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ZwwWayne ZwwWayne merged commit 601db12 into open-mmlab:main Oct 24, 2022
C1rN09 pushed a commit to C1rN09/mmengine that referenced this pull request Nov 1, 2022
* init npu

* Update mmengine/optim/optimizer/amp_optimizer_wrapper.py

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

* Update mmengine/dist/dist.py

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

* change to is_hccl_backend

* Update mmengine/optim/optimizer/amp_optimizer_wrapper.py

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

* add comment with AmpOptimWrapper

* Update mmengine/runner/amp.py

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

* Update mmengine/runner/amp.py

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

* add npu fn in base_model

* Update mmengine/optim/optimizer/amp_optimizer_wrapper.py

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

* clean lint

* Update mmengine/optim/optimizer/amp_optimizer_wrapper.py

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

* Update mmengine/model/base_model/base_model.py

Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com>

* add is_npu_available

* try to fix

* Add comments

* Refine grammar

Co-authored-by: Zaida Zhou <58739961+zhouzaida@users.noreply.github.com>
Co-authored-by: Mashiro <57566630+HAOCHENYE@users.noreply.github.com>
Co-authored-by: HAOCHENYE <21724054@zju.edu.cn>
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.

None yet

4 participants