/**
 * 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 quant_compressor.cpp
 * \brief
 */

#if (__NPU_ARCH__ == 9201)
#include "arch92/quant_compressor_kernel_arch92.h"
#else
#include "arch35/quant_compressor_kernel.h"
#endif
using namespace QuantCompressor;

#define INVOKE_QUANT_COMPRESSOR_GENERAL_OP_IMPL(templateClass, isFullLoad, ...) \
    do { \
        templateClass<COMPType<__VA_ARGS__>, isFullLoad> op(&pipe, tilingData); \
        op.Init(x, wKv, wGate, stateCache, ape, xDescale, wKvDescale, wGateDescale, stateBlockTable, cuSeqlens, \
                seqUsed, startPos, cmpKvOut, workspace); \
        op.Process(); \
    } while (0)

template <uint8_t XLayout, uint8_t XDType, uint8_t Coff, uint8_t CacheMode, uint8_t QuantMode, uint8_t TemplateId>
__global__ __aicore__ void quant_compressor(__gm__ uint8_t* x, __gm__ uint8_t* wKv, __gm__ uint8_t* wGate,
                                            __gm__ uint8_t* stateCache, __gm__ uint8_t* ape, __gm__ uint8_t* xDescale,
                                            __gm__ uint8_t* wKvDescale, __gm__ uint8_t* wGateDescale,
                                            __gm__ uint8_t* stateBlockTable, __gm__ uint8_t* cuSeqlens,
                                            __gm__ uint8_t* seqUsed, __gm__ uint8_t* startPos, __gm__ uint8_t* cmpKvOut,
                                            __gm__ uint8_t* stateCacheOut, __gm__ uint8_t* workspace,
                                            __gm__ uint8_t* tiling)
{
    REGISTER_TILING_DEFAULT(optiling::QuantCompressorTilingData);
#if (defined(__NPU_ARCH__) && (__NPU_ARCH__ == 9201))
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_1);
#else
    KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_MIX_AIC_1_2);
#endif
    if constexpr (static_cast<TEMPLATE_ID>(TemplateId) == TEMPLATE_ID::EMPTY_X) {
        return;
    }
#if (defined(__NPU_ARCH__) && ((__NPU_ARCH__ == 9201) || (__NPU_ARCH__ == 3510)))
    // arch35(3510) / arch92(9201):静态 shape 编译(含 static kernel)下 tiling 是编译期常量,
    // WITH_STRUCT 会展开成 static constexpr、保留常量折叠;动态 shape 用 PTR 零拷贝。
#ifdef CONST_TILING
    GET_TILING_DATA_WITH_STRUCT(optiling::QuantCompressorTilingData, tilingDataIn, tiling);
    const optiling::QuantCompressorTilingData* __restrict tilingData = &tilingDataIn;
#else
    GET_TILING_DATA_PTR_WITH_STRUCT(optiling::QuantCompressorTilingData, tilingData, tiling);
#endif
#else
    // arch22:保持 WITH_STRUCT
    GET_TILING_DATA_WITH_STRUCT(optiling::QuantCompressorTilingData, tilingDataIn, tiling);
    const optiling::QuantCompressorTilingData* __restrict tilingData = &tilingDataIn;
#endif
    TPipe pipe;
    constexpr auto xLayout = static_cast<X_LAYOUT>(XLayout);
    constexpr auto xDtype = static_cast<X_DTYPE>(XDType);
    constexpr auto coff = static_cast<COFF>(Coff);
    constexpr auto cacheMode = static_cast<CACHE_MODE>(CacheMode);
    constexpr auto quantMode = static_cast<QUANT_MODE>(QuantMode);
    if constexpr (static_cast<TEMPLATE_ID>(TemplateId) == TEMPLATE_ID::FULL_LOAD) {
        INVOKE_QUANT_COMPRESSOR_GENERAL_OP_IMPL(QuantCompressorKernel, true, xLayout, xDtype, coff, cacheMode,
                                                quantMode);
    } else {
        INVOKE_QUANT_COMPRESSOR_GENERAL_OP_IMPL(QuantCompressorKernel, false, xLayout, xDtype, coff, cacheMode,
                                                quantMode);
    }
}