* 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 sparse4to2quant_matmul_update.h
* \brief
*/
#ifndef SPARSE4TO2_QUANT_MATMUL_UPDATE_H
#define SPARSE4TO2_QUANT_MATMUL_UPDATE_H
#include "sparse4to2quant_matmul_base.h"
namespace AscendC {
struct SparseUpdateInfo {
uint64_t mBaseTail;
uint64_t nBaseTail;
uint64_t alignedKaSize;
uint64_t alignedKbSize;
};
constexpr uint32_t K0_INT8 = 32;
constexpr uint32_t K0_INDEX = 8;
class Sparse4to2QuantMatmulUpdate {
public:
__aicore__ inline Sparse4to2QuantMatmulUpdate() {}
template <CubeFormat x1Format, CubeFormat x2Format, bool aTrans, bool bTrans>
__aicore__ inline void Init(const TCubeTiling* mmTiling, const SparseBaseBlockArgs& params);
template <CubeFormat x1Format, CubeFormat x2Format, bool aTrans, bool bTrans>
__aicore__ inline void UpdateBlockParamsAndCalcGmOffset(SparseBaseBlockArgs& params, SparseBlockOffset& offset,
uint64_t mTileIndex, uint64_t nTileIndex);
__aicore__ inline void UpdateBlockParams(SparseBaseBlockArgs& params, uint64_t mTileIndex, uint64_t nTileIndex);
template <CubeFormat x1Format, CubeFormat x2Format, bool aTrans, bool bTrans>
__aicore__ inline void CalcGMOffset(SparseBaseBlockArgs& params, SparseBlockOffset& offset);
private:
SparseUpdateInfo info_;
const TCubeTiling* mmTiling_;
};
template <CubeFormat x1Format, CubeFormat x2Format, bool aTrans, bool bTrans>
__aicore__ inline void Sparse4to2QuantMatmulUpdate::Init(const TCubeTiling* mmTiling, const SparseBaseBlockArgs& params)
{
mmTiling_ = mmTiling;
info_.nBaseTail = static_cast<uint64_t>(mmTiling_->N) -
(params.nTotalCnt - 1) * mmTiling_->singleCoreN;
info_.mBaseTail = static_cast<uint64_t>(mmTiling_->M) -
(params.mTotalCnt - 1) * mmTiling_->singleCoreM;
info_.alignedKaSize = SparseQmm::Align(mmTiling_->Ka, K0_INT8);
info_.alignedKbSize = SparseQmm::Align(mmTiling_->Kb, K0_INT8);
}
template <CubeFormat x1Format, CubeFormat x2Format, bool aTrans, bool bTrans>
__aicore__ inline void Sparse4to2QuantMatmulUpdate::UpdateBlockParamsAndCalcGmOffset(SparseBaseBlockArgs& params,
SparseBlockOffset& offset,
uint64_t mTileIndex,
uint64_t nTileIndex)
{
UpdateBlockParams(params, mTileIndex, nTileIndex);
CalcGMOffset<x1Format, x2Format, aTrans, bTrans>(params, offset);
}
__aicore__ inline void Sparse4to2QuantMatmulUpdate::UpdateBlockParams(SparseBaseBlockArgs& params, uint64_t mTileIndex,
uint64_t nTileIndex)
{
if ((mTileIndex == (params.mTileCntL2 - 1)) && (nTileIndex == (params.nTileCntL2 - 1)) &&
(params.index == (params.totalTileCnt - 1))) {
params.singleCoreM = info_.mBaseTail;
params.singleCoreN = info_.nBaseTail;
} else if ((mTileIndex == (params.mTileCntL2 - 1)) && (params.index >= (params.mCntUse - 1) * params.nCntUse)) {
params.singleCoreM = info_.mBaseTail;
params.singleCoreN = mmTiling_->baseN;
} else if ((nTileIndex == (params.nTileCntL2 - 1)) && ((params.index + 1) % params.nCntUse == 0)) {
params.singleCoreM = mmTiling_->baseM;
params.singleCoreN = info_.nBaseTail;
} else {
params.singleCoreM = mmTiling_->baseM;
params.singleCoreN = mmTiling_->baseN;
}
}
template <CubeFormat x1Format, CubeFormat x2Format, bool aTrans, bool bTrans>
__aicore__ inline void Sparse4to2QuantMatmulUpdate::CalcGMOffset(SparseBaseBlockArgs& params, SparseBlockOffset& offset)
{
uint64_t mCntIndex = params.index / params.nCntUse;
uint64_t nCntIndex = params.index - mCntIndex * params.nCntUse;
uint64_t mOffset = mCntIndex * mmTiling_->singleCoreM + params.mTileAddrOffset;
if constexpr (x1Format == CubeFormat::ND) {
offset.offsetA = mOffset * mmTiling_->Ka;
} else if constexpr (x1Format == CubeFormat::NZ) {
offset.offsetA = mOffset * K0_INT8;
}
uint64_t nOffset = nCntIndex * mmTiling_->singleCoreN + params.nTileAddrOffset;
offset.offsetB = nOffset * K0_INT8;
offset.offsetSparseIndex = nOffset * K0_INDEX;
offset.offsetC = mOffset * mmTiling_->N + nOffset;
offset.offsetScale = nOffset;
offset.offsetBias = nOffset;
offset.offsetPertoken = mOffset;
}
}
#endif