// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // 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. #include "paddle/phi/kernels/yolo_box_kernel.h" #include "paddle/phi/backends/xpu/enforce_xpu.h" #include "paddle/phi/core/kernel_registry.h" #include "paddle/phi/kernels/full_kernel.h" #include "paddle/phi/kernels/funcs/yolo_box_util.h" namespace phi { template void YoloBoxKernel(const Context& dev_ctx, const DenseTensor& x, const DenseTensor& img_size, const std::vector& anchors_, int class_num, float conf_thresh, int downsample_ratio, bool clip_bbox, float scale_x_y, bool iou_aware, float iou_aware_factor, DenseTensor* boxes, DenseTensor* scores) { if (x.numel() == 0 || img_size.numel() == 0) { Full(dev_ctx, boxes->dims(), 0, boxes); Full(dev_ctx, scores->dims(), 0, scores); return; } using XPUType = typename XPUTypeTrait::Type; int r = 0; auto* input = &x; // auto* imgsize = &img_size; float scale = scale_x_y; float bias = -0.5f * (scale - 1.f); std::vector anchors(anchors_.begin(), anchors_.end()); const int64_t n = input->dims()[0]; const int64_t h = input->dims()[2]; const int64_t w = input->dims()[3]; const int64_t box_num = boxes->dims()[1]; const int64_t an_num = anchors.size() / 2; boxes->Resize({n, box_num, 4}); dev_ctx.template Alloc(boxes); scores->Resize({n, box_num, class_num}); dev_ctx.template Alloc(scores); auto x_data = reinterpret_cast(x.data()); auto img_size_data = reinterpret_cast(img_size.data()); auto boxes_data = reinterpret_cast(boxes->data()); auto scores_data = reinterpret_cast(scores->data()); r = xpu::yolo_box(dev_ctx.x_context(), x_data, img_size_data, boxes_data, scores_data, n, h, w, anchors, an_num, class_num, conf_thresh, downsample_ratio, scale, bias, false); PADDLE_ENFORCE_XDNN_SUCCESS(r, "yolo_box"); } } // namespace phi PD_REGISTER_KERNEL(yolo_box, XPU, ALL_LAYOUT, phi::YoloBoxKernel, float) {}