/**
 * Copyright (c) 2025-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 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 "tilefwk/tilefwk.h"
#include "interface/inner/tilefwk.h"

namespace npu::tile_fwk {

constexpr float STUB_F_1 = 1.0;
constexpr float STUB_F_NEGA_1 = -1.0;
constexpr float STUB_F_127 = 127.0;
constexpr float STUB_F_255 = 255.0;
constexpr float STUB_F_1E_12 = 1e-12f;

Tensor Sigmoid(Tensor& input)
{
    auto dtype = input.GetStorage()->Datatype();
    if (dtype != DT_FP32) {
        input = Cast(input, DataType::DT_FP32);
    }
    auto expRes = Exp(Mul(input, Element(DataType::DT_FP32, STUB_F_NEGA_1)));
    auto res = Add(expRes, Element(DataType::DT_FP32, STUB_F_1));
    Element src(DataType::DT_FP32, 1.0f);
    auto ones = Full(src, DataType::DT_FP32, res.GetShape());
    res = Div(ones, res);
    if (dtype != DT_FP32) {
        res = Cast(res, dtype);
    }
    return res;
}

Tensor SoftmaxNew(const Tensor& operand)
{
    auto inputDtype = operand.GetStorage()->Datatype();
    Tensor castOperand = operand;
    if (inputDtype != DataType::DT_FP32) {
        castOperand = Cast(operand, DataType::DT_FP32);
    }
    auto rowmax = Amax(castOperand, -1, true);
    auto sub = Sub(castOperand, rowmax);
    auto exp = Exp(sub);
    auto esum = Sum(exp, -1, true);
    auto softmax = Div(exp, esum);
    if (inputDtype != softmax.GetStorage()->Datatype()) {
        softmax = Cast(softmax, inputDtype);
    }
    return softmax;
}

std::tuple<Tensor, Tensor> Quant(const Tensor& input, bool isSymmetry, bool hasSmoothFactor, const Tensor& smoothFactor)
{
    auto inputFp32 = Cast(input, DataType::DT_FP32, CAST_NONE);
    if (hasSmoothFactor) {
        inputFp32 = Mul(inputFp32, smoothFactor);
    }
    if (isSymmetry) {
        auto absRes = Abs(inputFp32);
        auto maxValue = Amax(absRes, -1, true);
        auto scaleQuant = ScalarDivS(maxValue, Element(DataType::DT_FP32, STUB_F_127), true);
        auto outFp32 = Mul(inputFp32, scaleQuant);
        auto outInt32 = Cast(outFp32, DataType::DT_INT32, CAST_RINT);
        auto outHalf = Cast(outInt32, DataType::DT_FP16, CAST_ROUND);
        auto outInt8 = Cast(outHalf, DataType::DT_INT8, CAST_TRUNC, SaturationMode::ON);
        auto scaleDeQuant = ScalarDivS(scaleQuant, Element(DataType::DT_FP32, STUB_F_1), true);
        return std::tie(outInt8, scaleDeQuant);
    } else {
        auto maxValue = Amax(inputFp32, -1, true);
        auto minValue = Amin(inputFp32, -1, true);
        auto scaleDeQuant = ScalarMaxS(
            ScalarDivS(ScalarSub(maxValue, minValue), Element(DataType::DT_FP32, STUB_F_255)),
            Element(DataType::DT_FP32, STUB_F_1E_12));
        auto offset = ScalarSubS(ScalarDiv(maxValue, scaleDeQuant), Element(DataType::DT_FP32, STUB_F_127), true);
        auto scaleQuant = ScalarDivS(scaleDeQuant, Element(DataType::DT_FP32, STUB_F_1), true);
        auto outFp32 = Mul(inputFp32, scaleQuant);
        auto outInt32 = Cast(outFp32, DataType::DT_INT32, CAST_RINT);
        auto outHalf = Cast(outInt32, DataType::DT_FP16, CAST_ROUND);
        auto outInt8 = Cast(outHalf, DataType::DT_INT8, CAST_TRUNC, SaturationMode::ON);
        return std::tie(outInt8, scaleDeQuant);
    }
}

} // namespace npu::tile_fwk