* 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.
*/
* \file scale_infer.cpp
* \brief Scale operator graph infer datatype resource
*/
#include "register/op_impl_registry.h"
#include "graph/operator_factory.h"
#include "graph/tensor.h"
#include <algorithm>
#include <vector>
#include "log/log.h"
using namespace ge;
namespace ops {
static constexpr int64_t IDX_0 = 0;
static ge::graphStatus InferDataType4Scale(gert::InferDataTypeContext* context)
{
OP_LOGD(context->GetNodeName(), "Begin to do InferDataType4Scale");
ge::DataType inputDtype = context->GetInputDataType(IDX_0);
const auto scaleDtype = context->GetInputDataType(1);
const auto biasDtype = context->GetOptionalInputDataType(2);
auto supported = [](ge::DataType dt) { return dt == ge::DT_FLOAT16 || dt == ge::DT_FLOAT || dt == ge::DT_BF16; };
if (!supported(inputDtype) || !supported(scaleDtype) || (biasDtype != ge::DT_UNDEFINED && !supported(biasDtype))) {
OP_LOGE(context->GetNodeName(), "Scale inputs must use supported floating-point dtypes");
return GRAPH_FAILED;
}
if (context->SetOutputDataType(IDX_0, inputDtype) != GRAPH_SUCCESS) {
return GRAPH_FAILED;
}
OP_LOGD(context->GetNodeName(), "End to do InferDataType4Scale");
return GRAPH_SUCCESS;
}
IMPL_OP(Scale).InferDataType(InferDataType4Scale);
}
namespace {
using Dims = std::vector<int64_t>;
bool UnknownRank(const Dims& x) { return x == Dims{-2}; }
bool ValidShape(const Dims& x)
{
return UnknownRank(x) || (x.size() <= 8 && std::all_of(x.begin(), x.end(), [](int64_t d) { return d >= -1; }));
}
bool Compatible(const Dims& a, const Dims& b)
{
if (UnknownRank(a) || UnknownRank(b))
return true;
if (a.size() != b.size())
return false;
for (size_t i = 0; i < a.size(); ++i)
if (a[i] >= 0 && b[i] >= 0 && a[i] != b[i])
return false;
return true;
}
ge::graphStatus VerifyScale(ge::Operator& op)
{
auto xd = op.GetInputDescByName("x"), sd = op.GetInputDescByName("scale");
ge::TensorDesc bd;
auto x = xd.GetShape().GetDims(), s = sd.GetShape().GetDims();
const auto dt = xd.GetDataType();
auto supported = [](ge::DataType value) {
return value == ge::DT_FLOAT16 || value == ge::DT_FLOAT || value == ge::DT_BF16;
};
if (!supported(dt) || !supported(sd.GetDataType()))
return ge::GRAPH_FAILED;
if (!ValidShape(x) || !ValidShape(s))
return ge::GRAPH_FAILED;
if (op.TryGetInputDesc("bias", bd) == ge::GRAPH_SUCCESS) {
auto b = bd.GetShape().GetDims();
const bool emptyBias = std::find(b.begin(), b.end(), 0) != b.end();
if (!supported(bd.GetDataType()) || !ValidShape(b) || (!emptyBias && !Compatible(s, b)))
return ge::GRAPH_FAILED;
}
int64_t axis = 1, axes = 1;
bool blob = true;
op.GetAttr("axis", axis);
op.GetAttr("num_axes", axes);
op.GetAttr("scale_from_blob", blob);
if (axes < -1)
return ge::GRAPH_FAILED;
if (!UnknownRank(x)) {
const int64_t rank = x.size();
if (rank == 0 || axis < -rank || axis >= rank)
return ge::GRAPH_FAILED;
if (axis < 0)
axis += rank;
if (blob && axes > rank - axis)
return ge::GRAPH_FAILED;
}
if (UnknownRank(s))
return ge::GRAPH_SUCCESS;
if (blob && axes == 0) {
return s.empty() ? ge::GRAPH_SUCCESS : ge::GRAPH_FAILED;
}
if (blob && axes > 0 && int64_t(s.size()) != axes)
return ge::GRAPH_FAILED;
if (UnknownRank(x))
return ge::GRAPH_SUCCESS;
if (!blob && (s.empty() || s == Dims{1}))
return ge::GRAPH_SUCCESS;
const int64_t span = blob ? (axes == -1 ? int64_t(x.size()) - axis : axes) : int64_t(s.size());
if (span < 0 || axis + span > int64_t(x.size()) || int64_t(s.size()) != span)
return ge::GRAPH_FAILED;
int64_t checkedSpan = span;
if (!blob && x.size() == 4) {
while (checkedSpan > 0 && s[checkedSpan - 1] == 1 && x[axis + checkedSpan - 1] >= 0 &&
x[axis + checkedSpan - 1] != 1)
--checkedSpan;
}
for (int64_t i = 0; i < checkedSpan; ++i)
if (x[axis + i] >= 0 && s[i] >= 0 && x[axis + i] != s[i])
return ge::GRAPH_FAILED;
return ge::GRAPH_SUCCESS;
}
const ge::VerifyFuncRegister verify("Scale", VerifyScale);
}