* Copyright (c) 2026 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include "register/op_impl_registry.h"
#include "exe_graph/runtime/infer_shape_context.h"
#include "op_common/log/log.h"
#include "op_common/op_host/util/shape_util.h"
using namespace ge;
namespace ops {
static constexpr int64_t IDX_ANCHOR = 0;
static constexpr int64_t IDX_GT = 1;
static constexpr int64_t IDX_OUT = 0;
static constexpr size_t RANK_2 = 2;
static constexpr size_t LAST_DIM_IDX = 1;
static constexpr int64_t BOX_COORD_NUM = 4;
static ge::graphStatus CheckBoxShape(gert::InferShapeContext* context, const gert::Shape& shape, const char* name)
{
if (Ops::Base::IsUnknownRank(shape)) {
return ge::GRAPH_SUCCESS;
}
if (shape.GetDimNum() != RANK_2) {
OP_LOGE(context->GetNodeName(), "%s rank must be 2 (shape (N, 4)), but got rank %zu.", name, shape.GetDimNum());
return ge::GRAPH_FAILED;
}
int64_t lastDim = shape.GetDim(LAST_DIM_IDX);
if (lastDim != ge::UNKNOWN_DIM && lastDim != BOX_COORD_NUM) {
OP_LOGE(context->GetNodeName(), "%s last dim must be 4 (x1,y1,x2,y2), but got %ld.", name, lastDim);
return ge::GRAPH_FAILED;
}
return ge::GRAPH_SUCCESS;
}
static ge::graphStatus InferShape4BoundingBoxEncode(gert::InferShapeContext* context)
{
const gert::Shape* anchorShape = context->GetInputShape(IDX_ANCHOR);
OP_CHECK_NULL_WITH_CONTEXT(context, anchorShape);
const gert::Shape* gtShape = context->GetInputShape(IDX_GT);
OP_CHECK_NULL_WITH_CONTEXT(context, gtShape);
gert::Shape* outputShape = context->GetOutputShape(IDX_OUT);
OP_CHECK_NULL_WITH_CONTEXT(context, outputShape);
const auto* anchorDesc = context->GetInputDesc(IDX_ANCHOR);
OP_CHECK_NULL_WITH_CONTEXT(context, anchorDesc);
const auto* gtDesc = context->GetInputDesc(IDX_GT);
OP_CHECK_NULL_WITH_CONTEXT(context, gtDesc);
if (anchorDesc->GetDataType() != gtDesc->GetDataType()) {
OP_LOGE(context->GetNodeName(), "anchor_box dtype (%d) and ground_truth_box dtype (%d) must be the same.",
static_cast<int>(anchorDesc->GetDataType()), static_cast<int>(gtDesc->GetDataType()));
return ge::GRAPH_FAILED;
}
if (CheckBoxShape(context, *anchorShape, "anchor_box") != ge::GRAPH_SUCCESS) {
return ge::GRAPH_FAILED;
}
if (CheckBoxShape(context, *gtShape, "ground_truth_box") != ge::GRAPH_SUCCESS) {
return ge::GRAPH_FAILED;
}
if (!Ops::Base::IsUnknownRank(*anchorShape) && !Ops::Base::IsUnknownRank(*gtShape)) {
if (anchorShape->GetDimNum() != gtShape->GetDimNum()) {
OP_LOGE(context->GetNodeName(), "anchor_box rank (%zu) and ground_truth_box rank (%zu) must be the same.",
anchorShape->GetDimNum(), gtShape->GetDimNum());
return ge::GRAPH_FAILED;
}
for (size_t i = 0; i < anchorShape->GetDimNum(); ++i) {
int64_t aDim = anchorShape->GetDim(i);
int64_t gDim = gtShape->GetDim(i);
if (aDim != ge::UNKNOWN_DIM && gDim != ge::UNKNOWN_DIM && aDim != gDim) {
OP_LOGE(context->GetNodeName(),
"anchor_box and ground_truth_box shapes must be exactly the same, but dim[%zu] differs "
"(%ld vs %ld).",
i, aDim, gDim);
return ge::GRAPH_FAILED;
}
}
}
*outputShape = *anchorShape;
return ge::GRAPH_SUCCESS;
}
IMPL_OP_INFERSHAPE(BoundingBoxEncode).InferShape(InferShape4BoundingBoxEncode);
}