-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[Feature] Support engine with NPU backend. #2262
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
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
093f462
init npu
wangjiangben-hw ba9c36b
Merge pull request #2261 from wangjiangben-hw/npu-dev
ckirchhoff2021 716b3b3
add npu extension and focal loss adapter
ckirchhoff2021 6e53b3f
clean code
ckirchhoff2021 08f0a16
clean code
ckirchhoff2021 da659cb
clean code
ckirchhoff2021 448476e
clean code
ckirchhoff2021 afbd351
fix autocast bugs on npu (#2273)
wangjiangben-hw 26f35e0
code format
ckirchhoff2021 58618ac
code format
ckirchhoff2021 d75dcb1
code format
ckirchhoff2021 7af2a6f
bug fix
ckirchhoff2021 268ff0e
pytorch_npu_helper.hpp clean code
ckirchhoff2021 9b0133c
Merge pull request #2269 from ckirchhoff2021/npu-dev
ckirchhoff2021 a043541
Npu dev (#2306)
wangjiangben-hw 90fc3dc
raise ImportError when compile with npu
wangjiangben-hw 6ffdb57
add npu test case (#2307)
wangjiangben-hw 3f11861
Update focal_loss.py
wangjiangben-hw c81daa3
add comment
wangjiangben-hw 1cee865
clean lint
wangjiangben-hw 5841f92
update dtype assert
wangjiangben-hw 4ce938c
update DDP forward and comment
wangjiangben-hw ea1d8f8
fix bug
wangjiangben-hw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add npu extension and focal loss adapter
- Loading branch information
commit 716b3b3693e97217bf908c34e7ca1544963d9699
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /****************************************************************************** | ||
| * Copyright (c) 2022 Huawei Technologies Co., Ltd | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under the BSD 3-Clause License (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://opensource.org/licenses/BSD-3-Clause | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| ******************************************************************************/ | ||
|
|
||
| #ifndef PYTORCH_NPU_HELPER_HPP_ | ||
| #define PYTORCH_NPU_HELPER_HPP_ | ||
|
|
||
| #include "pytorch_cpp_helper.hpp" | ||
| #include "pytorch_device_registry.hpp" | ||
|
|
||
| #ifdef MMCV_WITH_NPU | ||
| #include <torch_npu/csrc/framework/utils/CalcuOpUtil.h> | ||
| #include <torch_npu/csrc/framework/utils/OpAdapter.h> | ||
| #include <torch_npu/csrc/aten/NPUNativeFunctions.h> | ||
| #define NPU_NAME_SPACE at_npu::native | ||
| #define REGISTER_NPU_IMPL(key, value) REGISTER_DEVICE_IMPL(key, XLA, value) | ||
| #define CHECK_NPU(x) \ | ||
| TORCH_CHECK(x.device().type() == at::kXLA, #x " must be a NPU tensor") | ||
| #else | ||
| // for torch 1.5.0 adapter only | ||
| #include <torch/csrc/ATen/native/npu/utils/OpAdapter.h> | ||
| #include <torch/csrc/ATen/native/npu/utils/CalcuOpUtil.h> | ||
| #define NPU_NAME_SPACE at::native::npu | ||
| #define REGISTER_NPU_IMPL(key, value) REGISTER_DEVICE_IMPL(key, NPU, value); | ||
| #define CHECK_NPU(x) \ | ||
| TORCH_CHECK(x.device().type() == at::kNPU, #x " must be a NPU tensor") | ||
| #endif | ||
|
|
||
| #endif // PYTORCH_NPU_HELPER_HPP_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| #include <iostream> | ||
| #include "pytorch_npu_helper.hpp" | ||
|
|
||
| using namespace NPU_NAME_SPACE; | ||
| using namespace std; | ||
|
|
||
|
|
||
| void sigmoid_focal_loss_forward_npu(Tensor input, Tensor target, Tensor weight, | ||
| Tensor output, float gamma, float alpha) { | ||
|
|
||
| at::Tensor target_y = at::reshape(target, input.sizes()); | ||
| target_y = at_npu::native::NPUNativeFunctions::npu_dtype_cast(target_y, at::kInt); | ||
| at::Tensor grad_up = at::ones_like(input); | ||
| int64_t weight_size = weight.size(0); | ||
| at::Tensor weight_y = at::ones_like(input); | ||
| if(weight_size > 0) { | ||
| weight_y = at_npu::native::NPUNativeFunctions::npu_broadcast(weight, input.sizes()); | ||
| } | ||
|
|
||
| OpCommand cmd; | ||
| cmd.Name("SigmoidFocalLoss") | ||
| .Input(input) | ||
| .Input(target_y) | ||
| .Input(grad_up) | ||
| .Input(weight_y) | ||
| .Output(grad_input) | ||
| .Attr("gamma", gamma) | ||
| .Attr("alpha", alpha) | ||
| .Attr("reduction", "none") | ||
| .Run(); | ||
| } | ||
|
|
||
| void sigmoid_focal_loss_forward_impl(Tensor input, Tensor target, Tensor weight, | ||
| Tensor output, float gamma, float alpha); | ||
|
|
||
| void sigmoid_focal_loss_backward_npu(Tensor input, Tensor target, Tensor weight, | ||
| Tensor grad_input, float gamma, float alpha) { | ||
|
|
||
| at::Tensor target_y = at::reshape(target, input.sizes()); | ||
| target_y = at_npu::native::NPUNativeFunctions::npu_dtype_cast(target_y, at::kInt); | ||
| at::Tensor grad_up = at::ones_like(input); | ||
| int64_t weight_size = weight.size(0); | ||
| at::Tensor weight_y = at::ones_like(input); | ||
| if(weight_size > 0) { | ||
| weight_y = at_npu::native::NPUNativeFunctions::npu_broadcast(weight, input.sizes()); | ||
| } | ||
|
|
||
| OpCommand cmd; | ||
| cmd.Name("SigmoidFocalLossGrad") | ||
| .Input(input) | ||
| .Input(target_y) | ||
| .Input(grad_up) | ||
| .Input(weight_y) | ||
| .Output(grad_input) | ||
| .Attr("gamma", gamma) | ||
| .Attr("alpha", alpha) | ||
| .Attr("reduction", "none") | ||
| .Run(); | ||
| } | ||
|
|
||
| void sigmoid_focal_loss_backward_impl(Tensor input, Tensor target, Tensor weight, | ||
| Tensor grad_input, float gamma, float alpha); | ||
|
|
||
| void softmax_focal_loss_forward_npu(Tensor input, Tensor target, Tensor weight, | ||
| Tensor output, float gamma, float alpha) { | ||
|
|
||
| int64_t n_class = input.size(1); | ||
| at::Tensor target_y = at_npu::native::NPUNativeFunctions::one_hot(target, n_class); | ||
| target_y = at_npu::native::NPUNativeFunctions::npu_dtype_cast(target_y, at::kInt); | ||
| at::Tensor grad_up = at::ones_like(input); | ||
| int64_t weight_size = weight.size(0); | ||
| at::Tensor weight_y = at::ones_like(input); | ||
| if(weight_size > 0) { | ||
| weight_y = at_npu::native::NPUNativeFunctions::npu_broadcast(weight, input.sizes()); | ||
| } | ||
|
|
||
| OpCommand cmd; | ||
| cmd.Name("SoftmaxFocalLoss") | ||
| .Input(input) | ||
| .Input(target_y) | ||
| .Input(grad_up) | ||
| .Input(weight_y) | ||
| .Output(grad_input) | ||
| .Attr("gamma", gamma) | ||
| .Attr("alpha", alpha) | ||
| .Attr("reduction", "none") | ||
| .Run(); | ||
| } | ||
|
|
||
| void softmax_focal_loss_forward_impl(Tensor input, Tensor target, Tensor weight, | ||
| Tensor grad_input, float gamma, float alpha); | ||
|
|
||
| void softmax_focal_loss_backward_npu(Tensor input, Tensor target, Tensor weight, Tensor buff, | ||
| Tensor grad_input, float gamma, float alpha) { | ||
|
|
||
| int64_t n_class = input.size(1); | ||
| at::Tensor target_y = at_npu::native::NPUNativeFunctions::one_hot(target, n_class); | ||
| target_y = at_npu::native::NPUNativeFunctions::npu_dtype_cast(target_y, at::kInt); | ||
| at::Tensor grad_up = at::ones_like(input); | ||
| int64_t weight_size = weight.size(0); | ||
| at::Tensor weight_y = at::ones_like(input); | ||
| if(weight_size > 0) { | ||
| weight_y = at_npu::native::NPUNativeFunctions::npu_broadcast(weight, input.sizes()); | ||
| } | ||
|
|
||
| OpCommand cmd; | ||
| cmd.Name("SoftmaxFocalLossGrad") | ||
| .Input(input) | ||
| .Input(target_y) | ||
| .Input(grad_up) | ||
| .Input(weight_y) | ||
| .Output(grad_input) | ||
| .Attr("gamma", gamma) | ||
| .Attr("alpha", alpha) | ||
| .Attr("reduction", "none") | ||
| .Run(); | ||
| } | ||
|
|
||
| void softmax_focal_loss_backward_impl(Tensor input, Tensor target, Tensor weight, Tensor buff, | ||
| Tensor grad_input, float gamma, float alpha); | ||
|
|
||
| REGISTER_NPU_IMPL(sigmoid_focal_loss_forward_impl, sigmoid_focal_loss_forward_npu); | ||
|
|
||
| REGISTER_NPU_IMPL(sigmoid_focal_loss_backward_impl, sigmoid_focal_loss_backward_npu); | ||
|
|
||
| REGISTER_NPU_IMPL(softmax_focal_loss_forward_impl, softmax_focal_loss_forward_npu); | ||
|
|
||
| REGISTER_NPU_IMPL(softmax_focal_loss_backward_impl, softmax_focal_loss_backward_npu); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -280,7 +280,7 @@ def get_extensions(): | |
| if is_rocm_pytorch or torch.cuda.is_available() or os.getenv( | ||
| 'FORCE_CUDA', '0') == '1': | ||
| if is_rocm_pytorch: | ||
| define_macros += [('HIP_DIFF', None)] | ||
| define_macros += [('MMCV_WITH_HIP', None)] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we remove
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://github.com/open-mmlab/mmcv/blob/master/setup.py#L283 This might be a comparison error... |
||
| define_macros += [('MMCV_WITH_CUDA', None)] | ||
| cuda_args = os.getenv('MMCV_CUDA_ARGS') | ||
| extra_compile_args['nvcc'] = [cuda_args] if cuda_args else [] | ||
|
|
@@ -289,6 +289,7 @@ def get_extensions(): | |
| glob.glob('./mmcv/ops/csrc/pytorch/cuda/*.cu') + \ | ||
| glob.glob('./mmcv/ops/csrc/pytorch/cuda/*.cpp') | ||
| extension = CUDAExtension | ||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/pytorch')) | ||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/common')) | ||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/common/cuda')) | ||
| elif (hasattr(torch, 'is_mlu_available') and | ||
|
|
@@ -329,6 +330,32 @@ def get_extensions(): | |
| extension = CppExtension | ||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/common')) | ||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/common/mps')) | ||
| elif (os.getenv('FORCE_NPU', '0') == '1'): | ||
| print(f'Compiling {ext_name} only with CPU and NPU') | ||
| try: | ||
| has_npu = torch.npu.is_available() | ||
| print('torch_npu version 1.5 is available. ', has_npu) | ||
| extension = CppExtension | ||
| except: | ||
| try: | ||
| import torch_npu | ||
| from torch_npu.utils.cpp_extension import NpuExtension | ||
| has_npu = torch_npu.npu.is_available() | ||
| print('torch_npu version 1.8 is available.: ', has_npu) | ||
| define_macros += [('MMCV_WITH_NPU', None)] | ||
| extension = NpuExtension | ||
| except: | ||
| print('can not find any torch_npu') | ||
| return extensions | ||
|
|
||
| # src | ||
| op_files = glob.glob('./mmcv/ops/csrc/pytorch/*.cpp') + \ | ||
| glob.glob('./mmcv/ops/csrc/pytorch/cpu/*.cpp') + \ | ||
| glob.glob('./mmcv/ops/csrc/common/npu/*.cpp') + \ | ||
| glob.glob('./mmcv/ops/csrc/pytorch/npu/*.cpp') | ||
|
|
||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/common')) | ||
| include_dirs.append(os.path.abspath('./mmcv/ops/csrc/common/npu')) | ||
| else: | ||
| print(f'Compiling {ext_name} only with CPU') | ||
| op_files = glob.glob('./mmcv/ops/csrc/pytorch/*.cpp') + \ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.