* Copyright (c) 2025 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 dequantize_tiling_impl.cpp
* \brief
*/
#include "include/adv_api/quantization/dequantize_tiling.h"
#include <cstdint>
#include "graph/tensor.h"
#include "../../detail/host_log.h"
namespace AscendC {
namespace {
constexpr uint32_t EXPECTED_SHAPE_DIM = 2;
constexpr uint32_t FLOAT_PER_BLOCK = 8;
constexpr uint32_t FLOAT_PER_REPEAT = 64;
inline uint32_t GetDequantizeMaxTmpSize(const uint32_t outer, const uint32_t inner, const uint32_t scaleSize)
{
uint32_t fp32Num = FLOAT_PER_REPEAT;
fp32Num += outer * inner + scaleSize;
return fp32Num * static_cast<uint32_t>(sizeof(float));
}
inline uint32_t GetDequantizeMinTmpSize(const uint32_t outer, const uint32_t inner, const uint32_t scaleSize)
{
(void)outer;
uint32_t fp32Num = FLOAT_PER_REPEAT;
fp32Num += 1U * inner + scaleSize;
return fp32Num * static_cast<uint32_t>(sizeof(float));
}
}
void GetDequantizeTmpBufferFactorSize(const ge::Shape& srcShape, uint32_t& maxLiveNodeCnt, uint32_t& extraBuf)
{
extraBuf = FLOAT_PER_BLOCK + FLOAT_PER_REPEAT;
std::vector<int64_t> shapeDims = srcShape.GetDims();
if (shapeDims.size() == EXPECTED_SHAPE_DIM) {
extraBuf += static_cast<uint32_t>(shapeDims[1]);
maxLiveNodeCnt = 1u;
} else if (shapeDims.size() == 1) {
maxLiveNodeCnt = EXPECTED_SHAPE_DIM;
} else {
extraBuf = 0u;
maxLiveNodeCnt = 0u;
}
}
void GetDequantizeMaxMinTmpSize(
const ge::Shape& srcShape, const uint32_t typeSize, uint32_t& maxValue, uint32_t& minValue)
{
(void)typeSize;
std::vector<int64_t> shapeDims = srcShape.GetDims();
uint32_t outer = 1;
uint32_t inner = 0;
if (shapeDims.size() == EXPECTED_SHAPE_DIM) {
outer = static_cast<uint32_t>(shapeDims[0]);
inner = static_cast<uint32_t>(shapeDims[1]);
} else if (shapeDims.size() == 1) {
inner = static_cast<uint32_t>(shapeDims[0]);
} else {
ASCENDC_HOST_ASSERT(false, return, "srcShape should be 1-dimensional / 2-dimensional.");
return;
}
const uint32_t scaleSize = inner + FLOAT_PER_BLOCK;
maxValue = GetDequantizeMaxTmpSize(outer, inner, scaleSize);
minValue = GetDequantizeMinTmpSize(outer, inner, scaleSize);
}
}