AAlice_zhouupdate
9d759d93Add on Sep 10History
#include <iostream>
#include <vector>
#include <getopt.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctime>
#include <sys/wait.h>
#include <dlfcn.h>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <string.h>
#include <list>
#include <algorithm>
#include <stdarg.h>
#include "acl/acl.h"
#include "aclnn/aclnn_base.h"
#include "acl/acl_base.h"
#include "acl/acl_rt.h"
#include "sec_common.h"
#include "utils.h"

#include "../fuzzlib/secodeFuzz.h"
#include "aclnnop/aclnn_apply_adam_w_v2.h"

using namespace std;



/* ========== [CONSISTENT] 一致性压测辅助函数(gen_consistent_from_normal 生成) ========== */
static const int CONSISTENT_MAX_RUN_TIMES = 64; /* hash数组上限,防越界 */
static int CONSISTENT_RUN_TIMES = 2; /* 每用例执行次数:默认2次,对比2次输出是否一致;经环境变量 CONSISTENT_RUN_TIMES 覆盖 */

static const char* ConsistentDtypeStr(aclDataType t) {
    switch (t) {
        case ACL_FLOAT: return "FLOAT"; case ACL_FLOAT16: return "FLOAT16";
        case ACL_BF16: return "BFLOAT16"; case ACL_DOUBLE: return "DOUBLE";
        case ACL_INT8: return "INT8"; case ACL_INT16: return "INT16";
        case ACL_INT32: return "INT32"; case ACL_INT64: return "INT64";
        case ACL_UINT8: return "UINT8"; case ACL_UINT16: return "UINT16";
        case ACL_UINT32: return "UINT32"; case ACL_UINT64: return "UINT64";
        case ACL_BOOL: return "BOOL"; case ACL_COMPLEX64: return "COMPLEX64";
        case ACL_COMPLEX128: return "COMPLEX128";
        default: return "UNKNOWN";
    }
}

static const char* ConsistentFormatStr(aclFormat f) {
    switch (f) {
        case ACL_FORMAT_NCHW: return "NCHW"; case ACL_FORMAT_NHWC: return "NHWC";
        case ACL_FORMAT_ND: return "ND"; case ACL_FORMAT_NC1HWC0: return "NC1HWC0";
        case ACL_FORMAT_FRACTAL_Z: return "FRACTAL_Z"; case ACL_FORMAT_HWCN: return "HWCN";
        case ACL_FORMAT_NDHWC: return "NDHWC"; case ACL_FORMAT_FRACTAL_NZ: return "FRACTAL_NZ";
        case ACL_FORMAT_NCDHW: return "NCDHW"; case ACL_FORMAT_NC: return "NC";
        case ACL_FORMAT_NCL: return "NCL";
        default: return "OTHER";
    }
}

static size_t ConsistentSizeOfDtype(aclDataType dataType) {
    switch (dataType) {
        case ACL_FLOAT: return 4; case ACL_FLOAT16: return 2;
        case ACL_INT8: return 1; case ACL_INT32: return 4;
        case ACL_UINT8: return 1; case ACL_INT16: return 2;
        case ACL_UINT16: return 2; case ACL_UINT32: return 4;
        case ACL_INT64: return 8; case ACL_UINT64: return 8;
        case ACL_DOUBLE: return 8; case ACL_BOOL: return 1;
        case ACL_BF16: return 2; case ACL_COMPLEX64: return 8;
        case ACL_COMPLEX128: return 16;
        /* 以下 dtype 的 sizeof(T)=1(vector<int8_t>),必须显式列出,禁止落入 default:4 */
        case ACL_INT4: return 1;
        case ACL_HIFLOAT8: return 1;
        case ACL_FLOAT8_E5M2: return 1;
        case ACL_FLOAT8_E4M3FN: return 1;
        case ACL_FLOAT8_E8M0: return 1;
        case ACL_FLOAT4_E2M1: return 1;
        case ACL_FLOAT4_E1M2: return 1;
        default: return 0; /* 未知 dtype 返回 0,触发 [FUZZ_CONSISTENT_BYTES] 告警 */
    }
}

static void ConsistentPrintShape(const std::vector<int64_t>& shape) {
    printf("[");
    for (size_t k = 0; k < shape.size(); k++) {
        printf("%lld%s", (long long)shape[k], (k + 1 < shape.size()) ? "," : "");
    }
    printf("]");
}

static uint64_t ConsistentHashBuffer(const void* data, size_t len) {
    const uint8_t* p = (const uint8_t*)data;
    uint64_t hash = 1469598103934665603ULL; /* FNV-1a offset basis */
    for (size_t k = 0; k < len; k++) {
        hash ^= (uint64_t)p[k];
        hash *= 1099511628211ULL; /* FNV-1a prime */
    }
    return hash;
}

static uint64_t ConsistentHashDeviceTensor(void* deviceAddr, size_t bytes) {
    if (deviceAddr == nullptr || bytes == 0) return 0;
    void* hostBuf = malloc(bytes);
    if (hostBuf == nullptr) return 0;
    if (aclrtMemcpy(hostBuf, bytes, deviceAddr, bytes, ACL_MEMCPY_DEVICE_TO_HOST) != ACL_SUCCESS) {
        free(hostBuf);
        return 0;
    }
    uint64_t h = ConsistentHashBuffer(hostBuf, bytes);
    free(hostBuf);
    return h;
}
/* ========== [CONSISTENT] 辅助函数结束 ========== */

static int kDtypeEnum[] = {0, 1};
static int kDtypeNum = sizeof(kDtypeEnum) / sizeof(kDtypeEnum[0]);

int aclnnApplyAdamWV2_consistent(int fuzz_num)
{
    const long long kSingleInputSizeMax = 1073741824LL;

    auto ret = Init(deviceId, &context, &stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    // [CONSISTENT] md文档标注默认确定性实现,无需打开确定性计算开关
    printf("[FUZZ_DETERMINISTIC] api=aclnnApplyAdamWV2, enable_deterministic=0, reason=md文档标注:aclnnApplyAdamWV2默认确定性实现。\n");
    WriteDTLog("aclnnApplyAdamWV2", "aclnnApplyAdamWV2 consistent op fuzz start \n");

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
    DT_FUZZ_START(0, fuzz_num, (char*)__func__, 0)
    {
        int i = 0;

        void* varRefDeviceAddr = nullptr;
        aclTensor* varRef = nullptr;
        void* mRefDeviceAddr = nullptr;
        aclTensor* mRef = nullptr;
        void* vRefDeviceAddr = nullptr;
        aclTensor* vRef = nullptr;
        void* maxGradNormOptRefDeviceAddr = nullptr;
        aclTensor* maxGradNormOptRef = nullptr;
        void* gradDeviceAddr = nullptr;
        aclTensor* grad = nullptr;
        void* stepDeviceAddr = nullptr;
        aclTensor* step = nullptr;

        uint64_t workspaceSize = 0;
        aclOpExecutor* executor;
        void* workspaceAddr = nullptr;

        // [CONSISTENT] 一致性压测:固定输入重复执行 + hash 对比用变量
        char curTilingKey[64];
        snprintf(curTilingKey, sizeof(curTilingKey), "N/A");
        int cstRunCount = 0;
        bool cstAllOk = true;
        bool cstResultPrinted = false;  // 判定路径是否已打印 FUZZ_RESULT(去重 ERROR 标签处打印)
        uint64_t cstVarRefShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstVarRefDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstVarRefBytes = 0;
        void* cstVarRefSaveBuf = nullptr;
        uint64_t cstVarRefHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstVarRefHashArr, 0, sizeof(cstVarRefHashArr));
        uint64_t cstMRefShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstMRefDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstMRefBytes = 0;
        void* cstMRefSaveBuf = nullptr;
        uint64_t cstMRefHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstMRefHashArr, 0, sizeof(cstMRefHashArr));
        uint64_t cstVRefShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstVRefDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstVRefBytes = 0;
        void* cstVRefSaveBuf = nullptr;
        uint64_t cstVRefHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstVRefHashArr, 0, sizeof(cstVRefHashArr));
        uint64_t cstGradShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstGradDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstGradBytes = 0;
        void* cstGradSaveBuf = nullptr;
        uint64_t cstGradHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstGradHashArr, 0, sizeof(cstGradHashArr));
        uint64_t cstStepShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstStepDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstStepBytes = 0;
        void* cstStepSaveBuf = nullptr;
        uint64_t cstStepHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstStepHashArr, 0, sizeof(cstStepHashArr));

        printf("[FUZZ_LOG] plog_file: aclnnApplyAdamWV2_consistent_plog_case_%d\n", fuzzi);
        LOG_PRINT("aclnnApplyAdamWV2_consistent %d\n", fuzzi);
        try
        {

            // === Tensor Size distribution control ===

            int sizeBucket = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 0, 1, "i");  // [CONSISTENT] 收紧size档位(仅小/中规模), 避免大shape快照导致host OOM

            long long totalSizeMin = 0, totalSizeMax = 0;

            switch (sizeBucket) {

                case 0: totalSizeMin = 1;          totalSizeMax = 1000;          break;

                case 1: totalSizeMin = 1001;       totalSizeMax = 67108864LL;  break;  // [CONSISTENT] 中档上限收紧至64M元素(快照双份内存峰值约3GB)

                case 2: totalSizeMin = 1073741825LL; totalSizeMax = 2147483648LL; break;

                case 3: totalSizeMin = 2147483649LL; totalSizeMax = 4294967296LL; break;

            }

            int N = *(int*)DTSetGetNumberRange(&g_Element[i++], 4, 1, 64, "i");

            auto dtype = (aclDataType)*(int*)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_FLOAT16, kDtypeEnum, kDtypeNum);

            if (N < 1) N = 1;

            float lr_val = 0.001f;
            float beta1_val = 0.9f;
            float beta2_val = 0.999f;
            float weightDecay_val = 0.01f;
            float eps_val = 1e-8f;
            bool amsgrad = false;
            bool maximize = false;

            std::vector<int64_t> shape = {N};
            std::vector<int64_t> stepShape = {1};

            long long shapeSize = GetShapeSize(shape);
            if (shapeSize > kSingleInputSizeMax) { printf("shape_size too big\n"); goto ERROR; }

            printf("dtype=%d, N=%d, amsgrad=%d, maximize=%d\n", (int)dtype, N, (int)amsgrad, (int)maximize);

            aclFormat fmt = aclFormat::ACL_FORMAT_ND;

            if (dtype == aclDataType::ACL_FLOAT) {
                std::vector<float> zerosData(shapeSize, 0.0f);
                std::vector<float> onesData(shapeSize, 1.0f);

                // === Tensor Size distribution check ===

                long long totalSize = shapeSize;

                if (totalSize < totalSizeMin || totalSize > totalSizeMax) { goto ERROR; }

                ret = CreateAclTensor_New(zerosData, shape, &varRefDeviceAddr, dtype, fmt, &varRef);
                cstVarRefShapeSz = (uint64_t)GetShapeSize(shape);
                cstVarRefDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                ret = CreateAclTensor_New(zerosData, shape, &mRefDeviceAddr, dtype, fmt, &mRef);
                cstMRefShapeSz = (uint64_t)GetShapeSize(shape);
                cstMRefDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                ret = CreateAclTensor_New(onesData, shape, &vRefDeviceAddr, dtype, fmt, &vRef);
                cstVRefShapeSz = (uint64_t)GetShapeSize(shape);
                cstVRefDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                ret = CreateAclTensor_New(onesData, shape, &gradDeviceAddr, dtype, fmt, &grad);
                cstGradShapeSz = (uint64_t)GetShapeSize(shape);
                cstGradDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> zerosData(shapeSize, 0);
                std::vector<int16_t> onesData(shapeSize, 15360);
                ret = CreateAclTensor_New(zerosData, shape, &varRefDeviceAddr, dtype, fmt, &varRef);
                cstVarRefShapeSz = (uint64_t)GetShapeSize(shape);
                cstVarRefDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                ret = CreateAclTensor_New(zerosData, shape, &mRefDeviceAddr, dtype, fmt, &mRef);
                cstMRefShapeSz = (uint64_t)GetShapeSize(shape);
                cstMRefDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                ret = CreateAclTensor_New(onesData, shape, &vRefDeviceAddr, dtype, fmt, &vRef);
                cstVRefShapeSz = (uint64_t)GetShapeSize(shape);
                cstVRefDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                ret = CreateAclTensor_New(onesData, shape, &gradDeviceAddr, dtype, fmt, &grad);
                cstGradShapeSz = (uint64_t)GetShapeSize(shape);
                cstGradDtype = (int)(dtype);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            std::vector<float> stepData(1, 1.0f);
            ret = CreateAclTensor_New(stepData, stepShape, &stepDeviceAddr, ACL_FLOAT, fmt, &step);
            cstStepShapeSz = (uint64_t)GetShapeSize(stepShape);
            cstStepDtype = (int)(ACL_FLOAT);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);

                        // ===== [CONSISTENT] 保存全部 tensor 设备侧初始内容(固定输入,含输入tensor的值) =====
            cstVarRefBytes = (varRefDeviceAddr != nullptr) ? (size_t)cstVarRefShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstVarRefDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=varRef, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstVarRefDtype, ConsistentDtypeStr((aclDataType)cstVarRefDtype), (unsigned long long)cstVarRefShapeSz, cstVarRefBytes, ConsistentSizeOfDtype((aclDataType)cstVarRefDtype));
            if (cstVarRefBytes == 0 && varRefDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=varRef addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstVarRefSaveBuf = (cstVarRefBytes > 0) ? malloc(cstVarRefBytes) : nullptr;
            if (cstVarRefSaveBuf != nullptr && varRefDeviceAddr != nullptr) {
                aclrtMemcpy(cstVarRefSaveBuf, cstVarRefBytes, varRefDeviceAddr, cstVarRefBytes, ACL_MEMCPY_DEVICE_TO_HOST);
            }
            cstMRefBytes = (mRefDeviceAddr != nullptr) ? (size_t)cstMRefShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstMRefDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=mRef, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstMRefDtype, ConsistentDtypeStr((aclDataType)cstMRefDtype), (unsigned long long)cstMRefShapeSz, cstMRefBytes, ConsistentSizeOfDtype((aclDataType)cstMRefDtype));
            if (cstMRefBytes == 0 && mRefDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=mRef addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstMRefSaveBuf = (cstMRefBytes > 0) ? malloc(cstMRefBytes) : nullptr;
            if (cstMRefSaveBuf != nullptr && mRefDeviceAddr != nullptr) {
                aclrtMemcpy(cstMRefSaveBuf, cstMRefBytes, mRefDeviceAddr, cstMRefBytes, ACL_MEMCPY_DEVICE_TO_HOST);
            }
            cstVRefBytes = (vRefDeviceAddr != nullptr) ? (size_t)cstVRefShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstVRefDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=vRef, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstVRefDtype, ConsistentDtypeStr((aclDataType)cstVRefDtype), (unsigned long long)cstVRefShapeSz, cstVRefBytes, ConsistentSizeOfDtype((aclDataType)cstVRefDtype));
            if (cstVRefBytes == 0 && vRefDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=vRef addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstVRefSaveBuf = (cstVRefBytes > 0) ? malloc(cstVRefBytes) : nullptr;
            if (cstVRefSaveBuf != nullptr && vRefDeviceAddr != nullptr) {
                aclrtMemcpy(cstVRefSaveBuf, cstVRefBytes, vRefDeviceAddr, cstVRefBytes, ACL_MEMCPY_DEVICE_TO_HOST);
            }
            cstGradBytes = (gradDeviceAddr != nullptr) ? (size_t)cstGradShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstGradDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=grad, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstGradDtype, ConsistentDtypeStr((aclDataType)cstGradDtype), (unsigned long long)cstGradShapeSz, cstGradBytes, ConsistentSizeOfDtype((aclDataType)cstGradDtype));
            if (cstGradBytes == 0 && gradDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=grad addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstGradSaveBuf = (cstGradBytes > 0) ? malloc(cstGradBytes) : nullptr;
            if (cstGradSaveBuf != nullptr && gradDeviceAddr != nullptr) {
                aclrtMemcpy(cstGradSaveBuf, cstGradBytes, gradDeviceAddr, cstGradBytes, ACL_MEMCPY_DEVICE_TO_HOST);
            }
            cstStepBytes = (stepDeviceAddr != nullptr) ? (size_t)cstStepShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstStepDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=step, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstStepDtype, ConsistentDtypeStr((aclDataType)cstStepDtype), (unsigned long long)cstStepShapeSz, cstStepBytes, ConsistentSizeOfDtype((aclDataType)cstStepDtype));
            if (cstStepBytes == 0 && stepDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=step addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstStepSaveBuf = (cstStepBytes > 0) ? malloc(cstStepBytes) : nullptr;
            if (cstStepSaveBuf != nullptr && stepDeviceAddr != nullptr) {
                aclrtMemcpy(cstStepSaveBuf, cstStepBytes, stepDeviceAddr, cstStepBytes, ACL_MEMCPY_DEVICE_TO_HOST);
            }

            // ===== [CONSISTENT] 固定输入重复执行 CONSISTENT_RUN_TIMES 次,逐轮对全部 tensor 计算 hash =====
            cstRunCount = 0;
            cstAllOk = true;
            for (int cstRun = 0; cstRun < CONSISTENT_RUN_TIMES; cstRun++) {
                // [CONSISTENT] 恢复固定输入(输出 tensor 同时恢复初始值,覆盖 inplace/累加类算子)
                if (cstVarRefSaveBuf != nullptr && varRefDeviceAddr != nullptr) {
                    aclrtMemcpy(varRefDeviceAddr, cstVarRefBytes, cstVarRefSaveBuf, cstVarRefBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                if (cstMRefSaveBuf != nullptr && mRefDeviceAddr != nullptr) {
                    aclrtMemcpy(mRefDeviceAddr, cstMRefBytes, cstMRefSaveBuf, cstMRefBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                if (cstVRefSaveBuf != nullptr && vRefDeviceAddr != nullptr) {
                    aclrtMemcpy(vRefDeviceAddr, cstVRefBytes, cstVRefSaveBuf, cstVRefBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                if (cstGradSaveBuf != nullptr && gradDeviceAddr != nullptr) {
                    aclrtMemcpy(gradDeviceAddr, cstGradBytes, cstGradSaveBuf, cstGradBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                if (cstStepSaveBuf != nullptr && stepDeviceAddr != nullptr) {
                    aclrtMemcpy(stepDeviceAddr, cstStepBytes, cstStepSaveBuf, cstStepBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                ret = aclnnApplyAdamWV2GetWorkspaceSize(
                varRef, mRef, vRef, maxGradNormOptRef,
                grad, step,
                lr_val, beta1_val, beta2_val, weightDecay_val, eps_val,
                amsgrad, maximize,
                &workspaceSize, &executor);
                if (ret != ACL_SUCCESS) {
                    printf("aclnnApplyAdamWV2 execute failed :%d\n", (int)ret);
                    printf("[FUZZ_CONSISTENT] case_%d: faild, type=execute_error, stage=GetWorkspaceSize, run=%d/%d, ret=%d\n", fuzzi, cstRun + 1, CONSISTENT_RUN_TIMES, (int)ret);
                    cstAllOk = false;
                    break;
                }
                if (workspaceSize > 0) {
                    if (workspaceAddr != nullptr) { aclrtFree(workspaceAddr); workspaceAddr = nullptr; }
                    ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
                    if (ret != ACL_SUCCESS) {
                        printf("[FUZZ_CONSISTENT] case_%d: faild, type=execute_error, stage=workspace_malloc, run=%d/%d, ret=%d\n", fuzzi, cstRun + 1, CONSISTENT_RUN_TIMES, (int)ret);
                        cstAllOk = false;
                        break;
                    }
                }
                ret = aclnnApplyAdamWV2(workspaceAddr, workspaceSize, executor, stream);
                if (ret != ACL_SUCCESS) {
                    printf("aclnnApplyAdamWV2 execute failed :%d\n", (int)ret);
                    printf("[FUZZ_CONSISTENT] case_%d: faild, type=execute_error, stage=execute, run=%d/%d, ret=%d\n", fuzzi, cstRun + 1, CONSISTENT_RUN_TIMES, (int)ret);
                    cstAllOk = false;
                    break;
                }
                ret = aclrtSynchronizeStream(stream);
                if (ret != ACL_SUCCESS) {
                    printf("[FUZZ_CONSISTENT] case_%d: faild, type=execute_error, stage=sync, run=%d/%d, ret=%d\n", fuzzi, cstRun + 1, CONSISTENT_RUN_TIMES, (int)ret);
                    cstAllOk = false;
                    break;
                }
                // [CONSISTENT] 本轮执行结果 hash
                if (varRefDeviceAddr != nullptr && cstVarRefBytes > 0) {
                    cstVarRefHashArr[cstRun] = ConsistentHashDeviceTensor(varRefDeviceAddr, cstVarRefBytes);
                }
                if (mRefDeviceAddr != nullptr && cstMRefBytes > 0) {
                    cstMRefHashArr[cstRun] = ConsistentHashDeviceTensor(mRefDeviceAddr, cstMRefBytes);
                }
                if (vRefDeviceAddr != nullptr && cstVRefBytes > 0) {
                    cstVRefHashArr[cstRun] = ConsistentHashDeviceTensor(vRefDeviceAddr, cstVRefBytes);
                }
                if (gradDeviceAddr != nullptr && cstGradBytes > 0) {
                    cstGradHashArr[cstRun] = ConsistentHashDeviceTensor(gradDeviceAddr, cstGradBytes);
                }
                if (stepDeviceAddr != nullptr && cstStepBytes > 0) {
                    cstStepHashArr[cstRun] = ConsistentHashDeviceTensor(stepDeviceAddr, cstStepBytes);
                }
                cstRunCount++;
                printf("aclnnApplyAdamWV2 execute success :%d\n", (int)ret);
            }

            // ===== [CONSISTENT] 对比 2 轮 hash,判定一致性(2次执行输出一致即为确定性) =====
            if (cstAllOk && cstRunCount == CONSISTENT_RUN_TIMES) {
                bool cstVarRefSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstVarRefHashArr[cstR] != cstVarRefHashArr[0]) { cstVarRefSame = false; break; }
                }
                bool cstMRefSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstMRefHashArr[cstR] != cstMRefHashArr[0]) { cstMRefSame = false; break; }
                }
                bool cstVRefSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstVRefHashArr[cstR] != cstVRefHashArr[0]) { cstVRefSame = false; break; }
                }
                bool cstGradSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstGradHashArr[cstR] != cstGradHashArr[0]) { cstGradSame = false; break; }
                }
                bool cstStepSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstStepHashArr[cstR] != cstStepHashArr[0]) { cstStepSame = false; break; }
                }
                bool cstAllSame = cstVarRefSame && cstMRefSame && cstVRefSame && cstGradSame && cstStepSame;
                if (cstAllSame) {
                    printf("[FUZZ_CONSISTENT] case_%d: success, type=consistent, runs=%d, hash_consistent=all\n", fuzzi, CONSISTENT_RUN_TIMES);
                    printf("[FUZZ_RESULT] case_%d: success, ret=%d\n", fuzzi, (int)ret);
                    cstResultPrinted = true;
                } else {
                    printf("[FUZZ_CONSISTENT] case_%d: faild, type=hash_mismatch, runs=%d, inconsistent=", fuzzi, CONSISTENT_RUN_TIMES);
                    if (!cstVarRefSame) printf("varRef ");
                    if (!cstMRefSame) printf("mRef ");
                    if (!cstVRefSame) printf("vRef ");
                    if (!cstGradSame) printf("grad ");
                    if (!cstStepSame) printf("step ");
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: varRef hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstVarRefHashArr[cstR]);
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: mRef hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstMRefHashArr[cstR]);
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: vRef hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstVRefHashArr[cstR]);
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: grad hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstGradHashArr[cstR]);
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: step hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstStepHashArr[cstR]);
                    printf("\n");
                    // [CONSISTENT] 失败时打印全部入参信息(dtype/format/shape/attr/tiling_key)
                    printf("[FUZZ_PARAM] ");
                    printf("varRef: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstVarRefDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstVarRefShapeSz); printf("; ");
                    printf("mRef: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstMRefDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstMRefShapeSz); printf("; ");
                    printf("vRef: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstVRefDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstVRefShapeSz); printf("; ");
                    printf("grad: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstGradDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstGradShapeSz); printf("; ");
                    printf("step: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstStepDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstStepShapeSz); printf("; ");
                    printf("lr_val=%g; ", (double)lr_val);
                    printf("beta1_val=%g; ", (double)beta1_val);
                    printf("beta2_val=%g; ", (double)beta2_val);
                    printf("weightDecay_val=%g; ", (double)weightDecay_val);
                    printf("eps_val=%g; ", (double)eps_val);
                    printf("amsgrad=%d; ", (int)((amsgrad) ? 1 : 0));
                    printf("maximize=%d; ", (int)((maximize) ? 1 : 0));
                    printf("tiling_key=%s\n", curTilingKey);
                    // ret=-2 为 hash_mismatch 专用标记(非 aclnn 错误码:执行成功但输出不确定)
                    printf("[FUZZ_RESULT] case_%d: faild, ret=%d\n", fuzzi, -2);
                    cstResultPrinted = true;
                }
            } else if (!cstAllOk) {
                // [CONSISTENT] 执行失败也打印全部入参信息
                printf("[FUZZ_PARAM] ");
                printf("varRef: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstVarRefDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstVarRefShapeSz); printf("; ");
                printf("mRef: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstMRefDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstMRefShapeSz); printf("; ");
                printf("vRef: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstVRefDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstVRefShapeSz); printf("; ");
                printf("grad: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstGradDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstGradShapeSz); printf("; ");
                printf("step: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstStepDtype), ConsistentFormatStr(fmt)); printf("shapeSize=%llu", (unsigned long long)cstStepShapeSz); printf("; ");
                printf("lr_val=%g; ", (double)lr_val);
                printf("beta1_val=%g; ", (double)beta1_val);
                printf("beta2_val=%g; ", (double)beta2_val);
                printf("weightDecay_val=%g; ", (double)weightDecay_val);
                printf("eps_val=%g; ", (double)eps_val);
                printf("amsgrad=%d; ", (int)((amsgrad) ? 1 : 0));
                printf("maximize=%d; ", (int)((maximize) ? 1 : 0));
                printf("tiling_key=%s\n", curTilingKey);
                printf("[FUZZ_RESULT] case_%d: faild, ret=%d\n", fuzzi, (int)ret);
                cstResultPrinted = true;
            }
        }
        catch (std::exception& e)
        {
            printf("catch exception %s\n", e.what());
            printf("[FUZZ_CONSISTENT] case_%d: faild, type=exception, ret=%d\n", fuzzi, (int)ret);
            printf("[FUZZ_RESULT] case_%d: faild, ret=%d\n", fuzzi, (int)ret);
            cstResultPrinted = true;
        }

    ERROR:
        printf("end \n");
        CheckAndLog(ret);
        pid_t pid = getpid();
        std::string cmd = "grep -rin \'aicore error\' /root/ascend/log/debug/plog/";
        printf("system return value %d\n", CheckResult(pid));

        // [CONSISTENT] 释放固定输入快照
        if (cstVarRefSaveBuf != nullptr) { free(cstVarRefSaveBuf); cstVarRefSaveBuf = nullptr; }
        if (cstMRefSaveBuf != nullptr) { free(cstMRefSaveBuf); cstMRefSaveBuf = nullptr; }
        if (cstVRefSaveBuf != nullptr) { free(cstVRefSaveBuf); cstVRefSaveBuf = nullptr; }
        if (cstGradSaveBuf != nullptr) { free(cstGradSaveBuf); cstGradSaveBuf = nullptr; }
        if (cstStepSaveBuf != nullptr) { free(cstStepSaveBuf); cstStepSaveBuf = nullptr; }
        aclDestroyTensor(varRef);
        aclDestroyTensor(mRef);
        aclDestroyTensor(vRef);
        aclDestroyTensor(grad);
        aclDestroyTensor(step);
        aclrtFree(varRefDeviceAddr);
        aclrtFree(mRefDeviceAddr);
        aclrtFree(vRefDeviceAddr);
        aclrtFree(gradDeviceAddr);
        aclrtFree(stepDeviceAddr);
        if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
    }
    DT_FUZZ_END()

    WriteDTLog("aclnnApplyAdamWV2", "aclnnApplyAdamWV2 consistent op fuzz total number:%u, para error number %u, execute error number %u \n", fuzz_num, aclnn_INVALID_Para, aclnn_Inner_Error + Runtime_Error);
    WriteDTLog("aclnnApplyAdamWV2", "aclnnApplyAdamWV2 consistent op fuzz end \n");
    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}

extern "C" int aclnnApplyAdamWV2_consistent_fuzz(int argc, char *argv[]);
int aclnnApplyAdamWV2_consistent_fuzz(int argc, char *argv[])
{
    // [CONSISTENT] 运行时覆盖每用例执行次数(环境变量 CONSISTENT_RUN_TIMES:默认2次,可按需覆盖)
    const char* cstRunEnv = getenv("CONSISTENT_RUN_TIMES");
    if (cstRunEnv != nullptr) {
        int cstRunV = atoi(cstRunEnv);
        if (cstRunV > 0 && cstRunV <= CONSISTENT_MAX_RUN_TIMES) { CONSISTENT_RUN_TIMES = cstRunV; }
    }
    printf("[FUZZ_CONSISTENT_RUNTIMES] run_times=%d\n", CONSISTENT_RUN_TIMES);
    int fuzz_num = MAXFUZZNUMBER;
    if (argc < 1) {
        cout << "[ERROR] input arg num must be bigger than 1 \n" << endl;
        return -1;
    }
    else if (argc > 1) {
        fuzz_num = atoi(argv[2]);
    }
    if (argc == 4) { deviceId = atoi(argv[3]); }

    printf("fuzz_num %d \n", fuzz_num);
    printf("aclnnApplyAdamWV2_consistent :%d \n", aclnnApplyAdamWV2_consistent(fuzz_num));
    return 0;
}