#include "csrc/OpApiCommon.h"
#include "csrc/functions.h"
void border_align(const at::Tensor& input, const at::Tensor& rois, at::Tensor& output, int32_t pooled_size)
{
TORCH_CHECK(input.size(1) % 4 == 0, "The number of channels must be divisible by 4.");
at::Tensor feature_map = input.permute({0, 2, 3, 1}).contiguous();
at::Tensor rois_map = rois.contiguous();
EXEC_NPU_CMD(aclnnBorderAlign, feature_map, rois_map, pooled_size, output);
}
at::Tensor border_align_backward(const at::Tensor& grad_out, const at::Tensor& boxes, const at::Tensor& argmax_idx,
int32_t pool_size, int32_t height, int32_t width)
{
TORCH_CHECK_NPU(grad_out);
TORCH_CHECK_NPU(boxes);
TORCH_CHECK_NPU(argmax_idx);
TORCH_CHECK(grad_out.dim() == 4, "grad_out.dim() must be 4, but got: ", grad_out.dim());
TORCH_CHECK(boxes.dim() == 3, "idx.dim() must be 3, but got: ", boxes.dim());
TORCH_CHECK(argmax_idx.dim() == 4, "argmax_idx.dim() must be 4, but got: ", argmax_idx.dim());
int32_t batch_size = grad_out.size(0);
int32_t feat_channels = grad_out.size(1) * 4;
int32_t channels = grad_out.size(1);
int32_t box_size = boxes.size(1);
at::Tensor grad_input = at::zeros({batch_size, feat_channels, height, width}, grad_out.options());
EXEC_NPU_CMD(aclnnBorderAlignGrad, grad_out, boxes, argmax_idx, channels, box_size, height, width, pool_size,
batch_size, grad_input);
return grad_input;
}