-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a66977
commit 5b16b5b
Showing
9 changed files
with
609 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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,79 @@ | ||
/*! | ||
************************************************************************************************** | ||
* Deformable DETR | ||
* Copyright (c) 2020 SenseTime. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 [see LICENSE for details] | ||
************************************************************************************************** | ||
* Modified from | ||
*https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 | ||
************************************************************************************************** | ||
*/ | ||
|
||
#include "pytorch_cpp_helper.hpp" | ||
|
||
#ifdef MMCV_WITH_CUDA | ||
Tensor ms_deform_attn_cuda_forward(const Tensor &value, | ||
const Tensor &spatial_shapes, | ||
const Tensor &level_start_index, | ||
const Tensor &sampling_loc, | ||
const Tensor &attn_weight, | ||
const int im2col_step); | ||
|
||
void ms_deform_attn_cuda_backward( | ||
const Tensor &value, const Tensor &spatial_shapes, | ||
const Tensor &level_start_index, const Tensor &sampling_loc, | ||
const Tensor &attn_weight, const Tensor &grad_output, Tensor &grad_value, | ||
Tensor &grad_sampling_loc, Tensor &grad_attn_weight, const int im2col_step); | ||
|
||
#endif | ||
|
||
Tensor ms_deform_attn_forward(const Tensor &value, const Tensor &spatial_shapes, | ||
const Tensor &level_start_index, | ||
const Tensor &sampling_loc, | ||
const Tensor &attn_weight, | ||
const int im2col_step) { | ||
if (value.type().is_cuda()) { | ||
#ifdef MMCV_WITH_CUDA | ||
CHECK_CUDA_INPUT(value) | ||
CHECK_CUDA_INPUT(spatial_shapes) | ||
CHECK_CUDA_INPUT(level_start_index) | ||
CHECK_CUDA_INPUT(sampling_loc) | ||
CHECK_CUDA_INPUT(attn_weight) | ||
return ms_deform_attn_cuda_forward(value, spatial_shapes, level_start_index, | ||
sampling_loc, attn_weight, im2col_step); | ||
#else | ||
AT_ERROR("Not compiled with GPU support"); | ||
#endif | ||
} | ||
AT_ERROR("Not implemented on the CPU"); | ||
} | ||
|
||
void ms_deform_attn_backward(const Tensor &value, const Tensor &spatial_shapes, | ||
const Tensor &level_start_index, | ||
const Tensor &sampling_loc, | ||
const Tensor &attn_weight, | ||
const Tensor &grad_output, Tensor &grad_value, | ||
Tensor &grad_sampling_loc, | ||
Tensor &grad_attn_weight, const int im2col_step) { | ||
if (value.type().is_cuda()) { | ||
#ifdef MMCV_WITH_CUDA | ||
CHECK_CUDA_INPUT(value) | ||
CHECK_CUDA_INPUT(spatial_shapes) | ||
CHECK_CUDA_INPUT(level_start_index) | ||
CHECK_CUDA_INPUT(sampling_loc) | ||
CHECK_CUDA_INPUT(attn_weight) | ||
CHECK_CUDA_INPUT(grad_output) | ||
CHECK_CUDA_INPUT(grad_value) | ||
CHECK_CUDA_INPUT(grad_sampling_loc) | ||
CHECK_CUDA_INPUT(grad_attn_weight) | ||
ms_deform_attn_cuda_backward(value, spatial_shapes, level_start_index, | ||
sampling_loc, attn_weight, grad_output, | ||
grad_value, grad_sampling_loc, | ||
grad_attn_weight, im2col_step); | ||
#else | ||
AT_ERROR("Not compiled with GPU support"); | ||
#endif | ||
} else { | ||
AT_ERROR("Not implemented on the CPU"); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,68 @@ | ||
#include <torch/extension.h> | ||
|
||
#include <parrots/compute/aten.hpp> | ||
#include <parrots/extension.hpp> | ||
#include <parrots/foundation/ssattrs.hpp> | ||
using namespace at; | ||
using namespace parrots; | ||
|
||
Tensor ms_deform_attn_forward(const Tensor &value, const Tensor &spatial_shapes, | ||
const Tensor &level_start_index, | ||
const Tensor &sampling_loc, | ||
const Tensor &attn_weight, const int im2col_step); | ||
|
||
void ms_deform_attn_backward(const Tensor &value, const Tensor &spatial_shapes, | ||
const Tensor &level_start_index, | ||
const Tensor &sampling_loc, | ||
const Tensor &attn_weight, | ||
const Tensor &grad_output, Tensor &grad_value, | ||
Tensor &grad_sampling_loc, | ||
Tensor &grad_attn_weight, const int im2col_step); | ||
|
||
void ms_deform_attn_forward_parrots(CudaContext &ctx, const SSElement &attr, | ||
const OperatorBase::in_list_t &ins, | ||
OperatorBase::out_list_t &outs) { | ||
int im2col_step; | ||
SSAttrs(attr).get<int>("im2col_step", im2col_step).done(); | ||
const auto &value = buildATensor(ctx, ins[0]); | ||
const auto &spatial_shapes = buildATensor(ctx, ins[1]); | ||
const auto &level_start_index = buildATensor(ctx, ins[2]); | ||
const auto &sampling_loc = buildATensor(ctx, ins[3]); | ||
const auto &attn_weight = buildATensor(ctx, ins[4]); | ||
auto out = ms_deform_attn_forward(value, spatial_shapes, level_start_index, | ||
sampling_loc, attn_weight, im2col_step); | ||
updateDArray(ctx, out, outs[0]); | ||
} | ||
|
||
void ms_deform_attn_backward_parrots(CudaContext &ctx, const SSElement &attr, | ||
const OperatorBase::in_list_t &ins, | ||
OperatorBase::out_list_t &outs) { | ||
int im2col_step; | ||
SSAttrs(attr).get<int>("im2col_step", im2col_step).done(); | ||
const auto &value = buildATensor(ctx, ins[0]); | ||
const auto &spatial_shapes = buildATensor(ctx, ins[1]); | ||
const auto &level_start_index = buildATensor(ctx, ins[2]); | ||
const auto &sampling_loc = buildATensor(ctx, ins[3]); | ||
const auto &attn_weight = buildATensor(ctx, ins[4]); | ||
const auto &grad_output = buildATensor(ctx, ins[5]); | ||
auto grad_value = buildATensor(ctx, outs[0]); | ||
auto grad_sampling_loc = buildATensor(ctx, outs[1]); | ||
auto grad_attn_weight = buildATensor(ctx, outs[2]); | ||
ms_deform_attn_backward(value, spatial_shapes, level_start_index, | ||
sampling_loc, attn_weight, grad_output, grad_value, | ||
grad_sampling_loc, grad_attn_weight, im2col_step); | ||
} | ||
|
||
PARROTS_EXTENSION_REGISTER(ms_deform_attn_forward) | ||
.attr("im2col_step") | ||
.input(5) | ||
.output(1) | ||
.apply(ms_deform_attn_forward_parrots) | ||
.done(); | ||
|
||
PARROTS_EXTENSION_REGISTER(ms_deform_attn_backward) | ||
.attr("im2col_step") | ||
.input(6) | ||
.output(3) | ||
.apply(ms_deform_attn_backward_parrots) | ||
.done(); |
This file contains 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 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 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 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 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
Oops, something went wrong.