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 <sys/wait.h>
#include <dlfcn.h>
#include <cstdio>
#include <string>
#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_atan.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 std::string ShapeToStr(const std::vector<int64_t>& s) {
    std::string r = "[";
    for (size_t k = 0; k < s.size(); k++) {
        if (k) r += ",";
        r += std::to_string(s[k]);
    }
    return r + "]";
}

// ===== [FUZZ-FILL] 输入数据按 dtype 随机泛化 =====
static uint16_t FuzzFloatToHalf(float f) {
    uint32_t x; memcpy(&x, &f, 4);
    uint16_t sign = (uint16_t)((x >> 16) & 0x8000);
    int32_t exp = (int32_t)((x >> 23) & 0xFF) - 127 + 15;
    uint32_t mant = (x >> 13) & 0x3FF;
    if (exp <= 0) { return sign; }
    if (exp >= 31) { return (uint16_t)(sign | 0x7C00); }
    return (uint16_t)(sign | (uint32_t)(exp << 10) | mant);
}
static uint16_t FuzzFloatToBf16(float f) {
    uint32_t x; memcpy(&x, &f, 4);
    uint32_t lsb = (x >> 16) & 1;
    x += 0x7FFF + lsb;
    return (uint16_t)(x >> 16);
}
static void FuzzInitHostData(void* data, size_t count, aclDataType dtype) {
    if (data == nullptr || count == 0) { return; }
    for (size_t i = 0; i < count; i++) {
        float rf = ((float)rand() / (float)RAND_MAX) * 2000.0f - 1000.0f;
        double rd = ((double)rand() / (double)RAND_MAX) * 2000.0 - 1000.0;
        switch ((int)dtype) {
        case ACL_FLOAT:   ((float*)data)[i] = rf; break;
        case ACL_FLOAT16: ((int16_t*)data)[i] = (int16_t)FuzzFloatToHalf(rf); break;
        case ACL_BF16:    ((int16_t*)data)[i] = (int16_t)FuzzFloatToBf16(rf); break;
        case ACL_DOUBLE:  ((double*)data)[i] = rd; break;
        case ACL_INT8:    ((int8_t*)data)[i] = (int8_t)(rand() % 256 - 128); break;
        case ACL_INT16:   ((int16_t*)data)[i] = (int16_t)(rand() - 16384); break;
        case ACL_INT32:   ((int32_t*)data)[i] = (int32_t)(((uint32_t)rand() << 16) ^ (uint32_t)rand()); break;
        case ACL_INT64:   ((int64_t*)data)[i] = (int64_t)(((uint64_t)rand() << 48) ^ ((uint64_t)rand() << 32) ^ ((uint64_t)rand() << 16) ^ (uint64_t)rand()); break;
        case ACL_UINT8:   ((uint8_t*)data)[i] = (uint8_t)rand(); break;
        case ACL_UINT16:  ((uint16_t*)data)[i] = (uint16_t)rand(); break;
        case ACL_UINT32:  ((uint32_t*)data)[i] = (((uint32_t)rand() << 16) ^ (uint32_t)rand()); break;
        case ACL_UINT64:  ((uint64_t*)data)[i] = (((uint64_t)rand() << 48) ^ ((uint64_t)rand() << 32) ^ ((uint64_t)rand() << 16) ^ (uint64_t)rand()); break;
        case ACL_BOOL:    ((int8_t*)data)[i] = (int8_t)(rand() & 1); break;
        default:          ((uint8_t*)data)[i] = (uint8_t)rand(); break;
        }
    }
}
// ===== [FUZZ-FILL END] =====

int aclnnAtan_consistent(int fuzz_num)
{
    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=aclnnAtan, enable_deterministic=0, reason=md文档中未找到「确定性计算/确定性说明」章节,默认按确定性实现处理(不打开开关)\n");
    WriteDTLog("aclnnAtan", "aclnnAtan consistent op fuzz start \n");
    const long long kSingleInputSizeMax = 1073741824LL;
    const long long kTotalInputSizeMax = 2147483648LL;

    // dtype enums
    int dtype0Enum[] = { 2, 6, 3, 9, 4, 12, 0, 27, 1, 11 };
    int dtype0Num = sizeof(dtype0Enum) / sizeof(dtype0Enum[0]);
    int dtype1Enum[] = { 0, 27, 1, 11 };
    int dtype1Num = sizeof(dtype1Enum) / sizeof(dtype1Enum[0]);

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
    DT_FUZZ_START(0, fuzz_num, "aclnnAtan", 0)
    {
        int i = 0;
        int shapeDimMax = 16;
        int aclnnRet = -1;  // aclnn 实际调用返回值(成功判定依据)
        void* inputDeviceAddr = nullptr;
        aclTensor* input = nullptr;
        void* outDeviceAddr = nullptr;
        aclTensor* out = 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 cstInputShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstInputDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstInputBytes = 0;
        void* cstInputSaveBuf = nullptr;
        uint64_t cstInputHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstInputHashArr, 0, sizeof(cstInputHashArr));
        uint64_t cstOutShapeSz = 0;  // 创建语句处捕获的 shape size(作用域安全)
        int cstOutDtype = 0;  // 创建语句处捕获的 dtype(作用域安全)
        size_t cstOutBytes = 0;
        void* cstOutSaveBuf = nullptr;
        uint64_t cstOutHashArr[CONSISTENT_MAX_RUN_TIMES];
        memset(cstOutHashArr, 0, sizeof(cstOutHashArr));

        printf("[FUZZ_LOG] plog_file: aclnnAtan_consistent_plog_case_%d\n", fuzzi);
        LOG_PRINT("aclnnAtan_consistent %d\n", fuzzi);
        printf("[FUZZ_LOG] plog_file: aclnnAtan_fuzz_plog_case_%d.log\n", fuzzi);
        aclnnRet = -1;
        try
        {
        // DT mutation data
            std::srand((unsigned int)((fuzzi + 1) * 2654435761u));
            int sizeBucket = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 0, 1, "i");  // [CONSISTENT] 收紧size档位(仅小/中规模), 避免大shape快照导致host OOM
            int dim_num0 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");  // 维度约束 [0,8]
            {
                const long long sizeTargets[4] = {1000LL, 1048576LL, 4194304LL, 8388608LL};  // [CONSISTENT] 收紧size分布, 避免大shape显存耗尽干扰
                int nTensors = 2;
                long long target = sizeTargets[sizeBucket] / nTensors;
                if (target < 1) { target = 1; }
                int nd = (dim_num0 > 0) ? dim_num0 : 1;
                long long perDim = (long long)pow((double)target, 1.0 / nd);
                if (perDim < 1) { perDim = 1; }
                if (perDim > MAX_SHAPE_SIZE) { perDim = MAX_SHAPE_SIZE; }
                shapeDimMax = (int)perDim;
            }
            int shape1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape3 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape4 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape5 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape6 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape7 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            int shape8 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, shapeDimMax, "i");
            auto dtype0 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_INT8, dtype0Enum, dtype0Num);
            auto dtype1 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_FLOAT, dtype1Enum, dtype1Num);
            // === Tiling coverage: per-tensor dtype aliases ===
            aclDataType inputDtype = dtype0;
            aclDataType outDtype = dtype1;
            int dim_num1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");
            std::vector<int64_t> inputShape;
            std::vector<int64_t> outShape;
            if (dim_num0 == 1) {
                inputShape = {shape1};
            } else if (dim_num0 == 2) {
                inputShape = {shape1, shape2};
            } else if (dim_num0 == 3) {
                inputShape = {shape1, shape2, shape3};
            } else if (dim_num0 == 4) {
                inputShape = {shape1, shape2, shape3, shape4};
            } else if (dim_num0 == 5) {
                inputShape = {shape1, shape2, shape3, shape4, shape5};
            } else if (dim_num0 == 6) {
                inputShape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (dim_num0 == 7) {
                inputShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (dim_num0 == 8) {
                inputShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }
            outShape = inputShape;
            // === Tiling key coverage: 3 scenarios, round-robin (fuzzi % 3), every key >= MIN cases ===
            {
                int caseIdx = fuzzi % 3;
                int modeIdx = -1;
                switch (caseIdx) {
                case 0: { // TK001: input0 dtype == ge::DT_BF16
                    inputDtype = aclDataType::ACL_BF16;
                    dim_num0 = 3; (void)dim_num0;
                    snprintf(curTilingKey, sizeof(curTilingKey), "TK001");
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK001, shape_mode=any, dtype=input:BFLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 1: { // TK002: input0 dtype == ge::DT_FLOAT
                    inputDtype = aclDataType::ACL_FLOAT;
                    dim_num0 = 3; (void)dim_num0;
                    snprintf(curTilingKey, sizeof(curTilingKey), "TK002");
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK002, shape_mode=any, dtype=input:FLOAT\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 2: { // TK003: input0 dtype == ge::DT_FLOAT16
                    inputDtype = aclDataType::ACL_FLOAT16;
                    dim_num0 = 3; (void)dim_num0;
                    snprintf(curTilingKey, sizeof(curTilingKey), "TK003");
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK003, shape_mode=any, dtype=input:FLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                default: break;
                }
            }
            long long shape_size = GetShapeSize(outShape);
            if (shape_size > kSingleInputSizeMax) { printf("shape_size too big\n"); goto ERROR; }
            if (inputDtype == aclDataType::ACL_INT8) {
                std::vector<int8_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_INT16) {
                std::vector<int16_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_INT32) {
                std::vector<int32_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_INT64) {
                std::vector<int64_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_UINT8) {
                std::vector<uint8_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_BOOL) {
                std::vector<int8_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_FLOAT) {
                std::vector<float> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_BF16) {
                std::vector<int16_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (inputDtype == aclDataType::ACL_DOUBLE) {
                std::vector<double> inputHostData(GetShapeSize(inputShape), 0);
                FuzzInitHostData(inputHostData.data(), inputHostData.size(), inputDtype);
                ret = CreateAclTensor_New(inputHostData, inputShape, &inputDeviceAddr, inputDtype, aclFormat::ACL_FORMAT_ND, &input);
                cstInputShapeSz = (uint64_t)GetShapeSize(inputShape);
                cstInputDtype = (int)(inputDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (outDtype == aclDataType::ACL_FLOAT) {
                std::vector<float> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
                cstOutShapeSz = (uint64_t)GetShapeSize(outShape);
                cstOutDtype = (int)(outDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_BF16) {
                std::vector<int16_t> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
                cstOutShapeSz = (uint64_t)GetShapeSize(outShape);
                cstOutDtype = (int)(outDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
                cstOutShapeSz = (uint64_t)GetShapeSize(outShape);
                cstOutDtype = (int)(outDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_DOUBLE) {
                std::vector<double> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
                cstOutShapeSz = (uint64_t)GetShapeSize(outShape);
                cstOutDtype = (int)(outDtype);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }

                        // ===== [CONSISTENT] 保存全部 tensor 设备侧初始内容(固定输入,含输入tensor的值) =====
            cstInputBytes = (inputDeviceAddr != nullptr) ? (size_t)cstInputShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstInputDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=input, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstInputDtype, ConsistentDtypeStr((aclDataType)cstInputDtype), (unsigned long long)cstInputShapeSz, cstInputBytes, ConsistentSizeOfDtype((aclDataType)cstInputDtype));
            if (cstInputBytes == 0 && inputDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=input addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstInputSaveBuf = (cstInputBytes > 0) ? malloc(cstInputBytes) : nullptr;
            if (cstInputSaveBuf != nullptr && inputDeviceAddr != nullptr) {
                aclrtMemcpy(cstInputSaveBuf, cstInputBytes, inputDeviceAddr, cstInputBytes, ACL_MEMCPY_DEVICE_TO_HOST);
            }
            cstOutBytes = (outDeviceAddr != nullptr) ? (size_t)cstOutShapeSz * (size_t)ConsistentSizeOfDtype((aclDataType)cstOutDtype) : 0;
            printf("[FUZZ_CONSISTENT_BYTES] case_%d: tensor=out, dtype=%d(%s), shapeSize=%llu, cstBytes=%zu, ConsistentSizeOfDtype=%zu\n", fuzzi, (int)cstOutDtype, ConsistentDtypeStr((aclDataType)cstOutDtype), (unsigned long long)cstOutShapeSz, cstOutBytes, ConsistentSizeOfDtype((aclDataType)cstOutDtype));
            if (cstOutBytes == 0 && outDeviceAddr != nullptr) {
                printf("[FUZZ_CONSISTENT_BYTES] case_%d: WARN tensor=out addr!=nullptr but cstBytes=0, dtype may be uncovered by ConsistentSizeOfDtype\n", fuzzi);
            }
            cstOutSaveBuf = (cstOutBytes > 0) ? malloc(cstOutBytes) : nullptr;
            if (cstOutSaveBuf != nullptr && outDeviceAddr != nullptr) {
                aclrtMemcpy(cstOutSaveBuf, cstOutBytes, outDeviceAddr, cstOutBytes, 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 (cstInputSaveBuf != nullptr && inputDeviceAddr != nullptr) {
                    aclrtMemcpy(inputDeviceAddr, cstInputBytes, cstInputSaveBuf, cstInputBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                if (cstOutSaveBuf != nullptr && outDeviceAddr != nullptr) {
                    aclrtMemcpy(outDeviceAddr, cstOutBytes, cstOutSaveBuf, cstOutBytes, ACL_MEMCPY_HOST_TO_DEVICE);
                }
                ret = aclnnAtanGetWorkspaceSize(input, out, &workspaceSize, &executor);
                if (ret != ACL_SUCCESS) {
                    printf("aclnnAtan 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 = aclnnAtan(workspaceAddr, workspaceSize, executor, stream);
                if (ret != ACL_SUCCESS) {
                    printf("aclnnAtan 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 (inputDeviceAddr != nullptr && cstInputBytes > 0) {
                    cstInputHashArr[cstRun] = ConsistentHashDeviceTensor(inputDeviceAddr, cstInputBytes);
                }
                if (outDeviceAddr != nullptr && cstOutBytes > 0) {
                    cstOutHashArr[cstRun] = ConsistentHashDeviceTensor(outDeviceAddr, cstOutBytes);
                }
                cstRunCount++;
                printf("aclnnAtan execute success :%d\n", (int)ret);
            }

            // ===== [CONSISTENT] 对比 2 轮 hash,判定一致性(2次执行输出一致即为确定性) =====
            if (cstAllOk && cstRunCount == CONSISTENT_RUN_TIMES) {
                bool cstInputSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstInputHashArr[cstR] != cstInputHashArr[0]) { cstInputSame = false; break; }
                }
                bool cstOutSame = true;
                for (int cstR = 1; cstR < CONSISTENT_RUN_TIMES; cstR++) {
                    if (cstOutHashArr[cstR] != cstOutHashArr[0]) { cstOutSame = false; break; }
                }
                bool cstAllSame = cstInputSame && cstOutSame;
                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 (!cstInputSame) printf("input ");
                    if (!cstOutSame) printf("out ");
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: input hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstInputHashArr[cstR]);
                    printf("\n");
                    printf("[FUZZ_CONSISTENT_DETAIL] case_%d: out hash:", fuzzi);
                    for (int cstR = 0; cstR < CONSISTENT_RUN_TIMES; cstR++) printf(" run%d=%llu", cstR, (unsigned long long)cstOutHashArr[cstR]);
                    printf("\n");
                    // [CONSISTENT] 失败时打印全部入参信息(dtype/format/shape/attr/tiling_key)
                    printf("[FUZZ_PARAM] ");
                    printf("input: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstInputDtype), ConsistentFormatStr(aclFormat::ACL_FORMAT_ND)); printf("shapeSize=%llu", (unsigned long long)cstInputShapeSz); printf("; ");
                    printf("out: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstOutDtype), ConsistentFormatStr(aclFormat::ACL_FORMAT_ND)); printf("shapeSize=%llu", (unsigned long long)cstOutShapeSz); printf("; ");
                    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("input: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstInputDtype), ConsistentFormatStr(aclFormat::ACL_FORMAT_ND)); printf("shapeSize=%llu", (unsigned long long)cstInputShapeSz); printf("; ");
                printf("out: dtype=%s, format=%s, ", ConsistentDtypeStr((aclDataType)cstOutDtype), ConsistentFormatStr(aclFormat::ACL_FORMAT_ND)); printf("shapeSize=%llu", (unsigned long long)cstOutShapeSz); printf("; ");
                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:
        ret = aclrtSynchronizeStream(stream);
        if (!cstResultPrinted) { printf("[FUZZ_RESULT] case_%d: faild, ret=%d\n", fuzzi, aclnnRet); }  // [CONSISTENT] 仅早退路径(goto 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 (cstInputSaveBuf != nullptr) { free(cstInputSaveBuf); cstInputSaveBuf = nullptr; }
        if (cstOutSaveBuf != nullptr) { free(cstOutSaveBuf); cstOutSaveBuf = nullptr; }
        if (input) { aclDestroyTensor(input); }
        if (out) { aclDestroyTensor(out); }
        if (inputDeviceAddr) { aclrtFree(inputDeviceAddr); }
        if (outDeviceAddr) { aclrtFree(outDeviceAddr); }
        if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
    }
    DT_FUZZ_END()
    WriteDTLog("aclnnAtan", "aclnnAtan 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("aclnnAtan", "aclnnAtan consistent op fuzz end \n");
    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}

extern "C" int aclnnAtan_consistent_fuzz(int argc, char *argv[]);
int aclnnAtan_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("aclnnAtan_consistent :%d \n", aclnnAtan_consistent(fuzz_num));
    return 0;
}