#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_apply_adam.h"

using namespace std;


static const char* GetNonContigSceneName(int scene) {
    switch(scene) {
        case 0: return "CONTINUOUS";
        case 1: return "BROADCAST";
        case 2: return "SLICE";
        case 3: return "ASSTRIDED";
        case 4: return "STORAGE_PADDED";
        case 5: return "TRANSPOSE";
        default: return "UNKNOWN";
    }
}

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] 大 tensor 输入数据按 dtype 有界随机泛化(避免 fp16 溢出/NaN/Inf) =====
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 FuzzFillUniform(void* data, size_t count, aclDataType dtype, float lo, float hi) {
    if (data == nullptr || count == 0) { return; }
    for (size_t k = 0; k < count; k++) {
        float r = lo + ((float)rand() / (float)RAND_MAX) * (hi - lo);
        switch ((int)dtype) {
        case ACL_FLOAT:   ((float*)data)[k] = r; break;
        case ACL_FLOAT16: ((int16_t*)data)[k] = (int16_t)FuzzFloatToHalf(r); break;
        case ACL_BF16:    ((int16_t*)data)[k] = (int16_t)FuzzFloatToBf16(r); break;
        default:          ((float*)data)[k] = r; break;
        }
    }
}
// ===== [FUZZ-FILL END] =====

int aclnnApplyAdam_Normal(int fuzz_num, aclrtContext context)
{
    auto ret = aclrtSetCurrentContext(context);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdam aclrtSetCurrentContext failed. ERROR: %d\n", ret); return ret);
    aclrtStream stream;
    ret = aclrtCreateStream(&stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdam aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
    WriteDTLog("aclnnApplyAdam", "aclnnApplyAdam multithread op fuzz start \n");
    const long long kSingleInputSizeMax = 1073741824LL;
    const long long kTotalInputSizeMax = 2147483648LL;

    // varRef/m/v/grad 共用 dtype:md 支持 FLOAT16/BFLOAT16/FLOAT32(0/1/27)
    int dtype0Enum[] = { 0, 1, 27 };
    int dtype0Num = sizeof(dtype0Enum) / sizeof(dtype0Enum[0]);

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
    int random_int = aclnn_multithread_RANDOM_INT(1, 99999999);
    DT_FUZZ_START(random_int, fuzz_num, "aclnnApplyAdam", 0)
    {
        int i = 0;
        int shapeDimMax = 16;
        int aclnnRet = -1;  // aclnn 实际调用返回值(成功判定依据)
        void* varRefDeviceAddr = nullptr;
        aclTensor* varRef = nullptr;
        void* mDeviceAddr = nullptr;
        aclTensor* m = nullptr;
        void* vDeviceAddr = nullptr;
        aclTensor* v = nullptr;
        void* beta1PowerDeviceAddr = nullptr;
        aclTensor* beta1Power = nullptr;
        void* beta2PowerDeviceAddr = nullptr;
        aclTensor* beta2Power = nullptr;
        void* lrDeviceAddr = nullptr;
        aclTensor* lr = nullptr;
        void* beta1DeviceAddr = nullptr;
        aclTensor* beta1 = nullptr;
        void* beta2DeviceAddr = nullptr;
        aclTensor* beta2 = nullptr;
        void* epsilonDeviceAddr = nullptr;
        aclTensor* epsilon = nullptr;
        void* gradDeviceAddr = nullptr;
        aclTensor* grad = nullptr;
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor = nullptr;
        void* workspaceAddr = nullptr;

        LOG_PRINT("aclnnApplyAdam_multithread %d\n", fuzzi);
        printf("[FUZZ_LOG] plog_file: aclnnApplyAdam_multithread_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, 3, "i");
            int dim_num0 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 8, "i");  // md: varRef/m/v/grad 维度 1-8
            {
                const long long sizeTargets[4] = {1000LL, 1048576LL, 16777216LL, 67108864LL};
                int nTensors = 4;  // varRef/m/v/grad 同 shape
                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_FLOAT, dtype0Enum, dtype0Num);
            // 标量类参数数据值(数据敏感:合法值域内变异,禁止全1/非法值)
            float beta1PowerVal = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1, 0.01, 0.9, "i");    // beta1^t ∈ (0,1)
            float beta2PowerVal = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1, 0.01, 0.9, "i");    // beta2^t ∈ (0,1)
            float lrVal = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1, 0.0005, 0.5, "i");          // lr > 0
            float beta1Val = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1, 0.1, 0.95, "i");         // beta1 ∈ (0,1)
            float beta2Val = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1, 0.9, 0.999, "i");        // beta2 ∈ (0,1)
            float epsilonVal = *(float *)DTSetGetNumberRangeFloat(&g_Element[i++], 1, 0.00000001, 0.01, "i");// eps > 0
            bool useLocking = (bool)*(int *)DTSetGetNumberRange(&g_Element[i++], 0, 0, 1, "i");
            bool useNesterov = (bool)*(int *)DTSetGetNumberRange(&g_Element[i++], 0, 0, 1, "i");

            // varRef/m/v/grad shape 严格一致(md: shape要求与varRef保持一致)
            std::vector<int64_t> varShape = {};
            if (dim_num0 == 1) {
                varShape = {shape1};
            } else if (dim_num0 == 2) {
                varShape = {shape1, shape2};
            } else if (dim_num0 == 3) {
                varShape = {shape1, shape2, shape3};
            } else if (dim_num0 == 4) {
                varShape = {shape1, shape2, shape3, shape4};
            } else if (dim_num0 == 5) {
                varShape = {shape1, shape2, shape3, shape4, shape5};
            } else if (dim_num0 == 6) {
                varShape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (dim_num0 == 7) {
                varShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (dim_num0 == 8) {
                varShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }
            std::vector<int64_t> mShape = varShape;
            std::vector<int64_t> vShape = varShape;
            std::vector<int64_t> gradShape = varShape;
            // 标量类参数 shape 严格 [1](md: shape要求为[1],大小不为1报161002)
            std::vector<int64_t> beta1PowerShape = {1};
            std::vector<int64_t> beta2PowerShape = {1};
            std::vector<int64_t> lrShape = {1};
            std::vector<int64_t> beta1Shape = {1};
            std::vector<int64_t> beta2Shape = {1};
            std::vector<int64_t> epsilonShape = {1};

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

            NonContigParams ncVarRef;
            ncVarRef.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncVarRef.viewShape = varShape;
            ncVarRef.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncVarRef.sliceAxis = 0;
            ncVarRef.offset = 0;
            ncVarRef.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncVarRef.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncVarRef.strides.resize(varShape.size());
            for (size_t j = 0; j < varShape.size(); j++) {
                ncVarRef.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncVarRef);
            long long varRefStorageSize = GetShapeSize(ncVarRef.storageShape);
            printf("[NON_CONTIG] ncVarRef scene=%s(%d), originalShape=[", GetNonContigSceneName(ncVarRef.scene), ncVarRef.scene);
            for (int _i = 0; _i < (int)varShape.size(); _i++) printf("%lld%s", (long long)varShape[_i], _i < (int)varShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncVarRef.viewShape.size(); _i++) printf("%lld%s", (long long)ncVarRef.viewShape[_i], _i < (int)ncVarRef.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncVarRef.strides.size(); _i++) printf("%lld%s", (long long)ncVarRef.strides[_i], _i < (int)ncVarRef.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncVarRef.storageShape.size(); _i++) printf("%lld%s", (long long)ncVarRef.storageShape[_i], _i < (int)ncVarRef.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncVarRef.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> varRefHostData(GetShapeSize(varShape), 0);
                FuzzFillUniform(varRefHostData.data(), varRefHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(varRefHostData, ncVarRef, &varRefDeviceAddr, dtype0, ACL_FORMAT_ND, &varRef);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> varRefHostData(GetShapeSize(varShape), 0);
                FuzzFillUniform(varRefHostData.data(), varRefHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(varRefHostData, ncVarRef, &varRefDeviceAddr, dtype0, ACL_FORMAT_ND, &varRef);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> varRefHostData(GetShapeSize(varShape), 0);
                FuzzFillUniform(varRefHostData.data(), varRefHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(varRefHostData, ncVarRef, &varRefDeviceAddr, dtype0, ACL_FORMAT_ND, &varRef);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else { printf("unsupported varRef dtype %d\n", (int)dtype0); goto ERROR; }
            NonContigParams ncM;
            ncM.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncM.viewShape = mShape;
            ncM.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncM.sliceAxis = 0;
            ncM.offset = 0;
            ncM.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncM.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncM.strides.resize(mShape.size());
            for (size_t j = 0; j < mShape.size(); j++) {
                ncM.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncM);
            long long mStorageSize = GetShapeSize(ncM.storageShape);
            printf("[NON_CONTIG] ncM scene=%s(%d), originalShape=[", GetNonContigSceneName(ncM.scene), ncM.scene);
            for (int _i = 0; _i < (int)mShape.size(); _i++) printf("%lld%s", (long long)mShape[_i], _i < (int)mShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncM.viewShape.size(); _i++) printf("%lld%s", (long long)ncM.viewShape[_i], _i < (int)ncM.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncM.strides.size(); _i++) printf("%lld%s", (long long)ncM.strides[_i], _i < (int)ncM.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncM.storageShape.size(); _i++) printf("%lld%s", (long long)ncM.storageShape[_i], _i < (int)ncM.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncM.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> mHostData(GetShapeSize(mShape), 0);
                FuzzFillUniform(mHostData.data(), mHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(mHostData, ncM, &mDeviceAddr, dtype0, ACL_FORMAT_ND, &m);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> mHostData(GetShapeSize(mShape), 0);
                FuzzFillUniform(mHostData.data(), mHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(mHostData, ncM, &mDeviceAddr, dtype0, ACL_FORMAT_ND, &m);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> mHostData(GetShapeSize(mShape), 0);
                FuzzFillUniform(mHostData.data(), mHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(mHostData, ncM, &mDeviceAddr, dtype0, ACL_FORMAT_ND, &m);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else { printf("unsupported m dtype %d\n", (int)dtype0); goto ERROR; }
            NonContigParams ncV;
            ncV.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncV.viewShape = vShape;
            ncV.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncV.sliceAxis = 0;
            ncV.offset = 0;
            ncV.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncV.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncV.strides.resize(vShape.size());
            for (size_t j = 0; j < vShape.size(); j++) {
                ncV.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncV);
            long long vStorageSize = GetShapeSize(ncV.storageShape);
            printf("[NON_CONTIG] ncV scene=%s(%d), originalShape=[", GetNonContigSceneName(ncV.scene), ncV.scene);
            for (int _i = 0; _i < (int)vShape.size(); _i++) printf("%lld%s", (long long)vShape[_i], _i < (int)vShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncV.viewShape.size(); _i++) printf("%lld%s", (long long)ncV.viewShape[_i], _i < (int)ncV.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncV.strides.size(); _i++) printf("%lld%s", (long long)ncV.strides[_i], _i < (int)ncV.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncV.storageShape.size(); _i++) printf("%lld%s", (long long)ncV.storageShape[_i], _i < (int)ncV.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncV.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> vHostData(GetShapeSize(vShape), 0);
                FuzzFillUniform(vHostData.data(), vHostData.size(), dtype0, 0.01f, 1.0f);  // v >= 0 保证 sqrt(v) 合法
                ret = CreateAclTensorByScene(vHostData, ncV, &vDeviceAddr, dtype0, ACL_FORMAT_ND, &v);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> vHostData(GetShapeSize(vShape), 0);
                FuzzFillUniform(vHostData.data(), vHostData.size(), dtype0, 0.01f, 1.0f);
                ret = CreateAclTensorByScene(vHostData, ncV, &vDeviceAddr, dtype0, ACL_FORMAT_ND, &v);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> vHostData(GetShapeSize(vShape), 0);
                FuzzFillUniform(vHostData.data(), vHostData.size(), dtype0, 0.01f, 1.0f);
                ret = CreateAclTensorByScene(vHostData, ncV, &vDeviceAddr, dtype0, ACL_FORMAT_ND, &v);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else { printf("unsupported v dtype %d\n", (int)dtype0); goto ERROR; }
            NonContigParams ncGrad;
            ncGrad.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncGrad.viewShape = gradShape;
            ncGrad.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncGrad.sliceAxis = 0;
            ncGrad.offset = 0;
            ncGrad.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncGrad.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncGrad.strides.resize(gradShape.size());
            for (size_t j = 0; j < gradShape.size(); j++) {
                ncGrad.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncGrad);
            long long gradStorageSize = GetShapeSize(ncGrad.storageShape);
            printf("[NON_CONTIG] ncGrad scene=%s(%d), originalShape=[", GetNonContigSceneName(ncGrad.scene), ncGrad.scene);
            for (int _i = 0; _i < (int)gradShape.size(); _i++) printf("%lld%s", (long long)gradShape[_i], _i < (int)gradShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncGrad.viewShape.size(); _i++) printf("%lld%s", (long long)ncGrad.viewShape[_i], _i < (int)ncGrad.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncGrad.strides.size(); _i++) printf("%lld%s", (long long)ncGrad.strides[_i], _i < (int)ncGrad.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncGrad.storageShape.size(); _i++) printf("%lld%s", (long long)ncGrad.storageShape[_i], _i < (int)ncGrad.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncGrad.offset);
            NonContigParams ncBeta1Power;
            ncBeta1Power.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncBeta1Power.viewShape = beta1PowerShape;
            ncBeta1Power.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncBeta1Power.sliceAxis = 0;
            ncBeta1Power.offset = 0;
            ncBeta1Power.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncBeta1Power.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncBeta1Power.strides.resize(beta1PowerShape.size());
            for (size_t j = 0; j < beta1PowerShape.size(); j++) {
                ncBeta1Power.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncBeta1Power);
            long long beta1PowerStorageSize = GetShapeSize(ncBeta1Power.storageShape);
            printf("[NON_CONTIG] ncBeta1Power scene=%s(%d), originalShape=[", GetNonContigSceneName(ncBeta1Power.scene), ncBeta1Power.scene);
            for (int _i = 0; _i < (int)beta1PowerShape.size(); _i++) printf("%lld%s", (long long)beta1PowerShape[_i], _i < (int)beta1PowerShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncBeta1Power.viewShape.size(); _i++) printf("%lld%s", (long long)ncBeta1Power.viewShape[_i], _i < (int)ncBeta1Power.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncBeta1Power.strides.size(); _i++) printf("%lld%s", (long long)ncBeta1Power.strides[_i], _i < (int)ncBeta1Power.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncBeta1Power.storageShape.size(); _i++) printf("%lld%s", (long long)ncBeta1Power.storageShape[_i], _i < (int)ncBeta1Power.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncBeta1Power.offset);
            NonContigParams ncBeta2Power;
            ncBeta2Power.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncBeta2Power.viewShape = beta2PowerShape;
            ncBeta2Power.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncBeta2Power.sliceAxis = 0;
            ncBeta2Power.offset = 0;
            ncBeta2Power.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncBeta2Power.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncBeta2Power.strides.resize(beta2PowerShape.size());
            for (size_t j = 0; j < beta2PowerShape.size(); j++) {
                ncBeta2Power.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncBeta2Power);
            long long beta2PowerStorageSize = GetShapeSize(ncBeta2Power.storageShape);
            printf("[NON_CONTIG] ncBeta2Power scene=%s(%d), originalShape=[", GetNonContigSceneName(ncBeta2Power.scene), ncBeta2Power.scene);
            for (int _i = 0; _i < (int)beta2PowerShape.size(); _i++) printf("%lld%s", (long long)beta2PowerShape[_i], _i < (int)beta2PowerShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncBeta2Power.viewShape.size(); _i++) printf("%lld%s", (long long)ncBeta2Power.viewShape[_i], _i < (int)ncBeta2Power.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncBeta2Power.strides.size(); _i++) printf("%lld%s", (long long)ncBeta2Power.strides[_i], _i < (int)ncBeta2Power.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncBeta2Power.storageShape.size(); _i++) printf("%lld%s", (long long)ncBeta2Power.storageShape[_i], _i < (int)ncBeta2Power.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncBeta2Power.offset);
            NonContigParams ncLr;
            ncLr.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncLr.viewShape = lrShape;
            ncLr.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncLr.sliceAxis = 0;
            ncLr.offset = 0;
            ncLr.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncLr.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncLr.strides.resize(lrShape.size());
            for (size_t j = 0; j < lrShape.size(); j++) {
                ncLr.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncLr);
            long long lrStorageSize = GetShapeSize(ncLr.storageShape);
            printf("[NON_CONTIG] ncLr scene=%s(%d), originalShape=[", GetNonContigSceneName(ncLr.scene), ncLr.scene);
            for (int _i = 0; _i < (int)lrShape.size(); _i++) printf("%lld%s", (long long)lrShape[_i], _i < (int)lrShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncLr.viewShape.size(); _i++) printf("%lld%s", (long long)ncLr.viewShape[_i], _i < (int)ncLr.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncLr.strides.size(); _i++) printf("%lld%s", (long long)ncLr.strides[_i], _i < (int)ncLr.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncLr.storageShape.size(); _i++) printf("%lld%s", (long long)ncLr.storageShape[_i], _i < (int)ncLr.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncLr.offset);
            NonContigParams ncBeta1;
            ncBeta1.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncBeta1.viewShape = beta1Shape;
            ncBeta1.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncBeta1.sliceAxis = 0;
            ncBeta1.offset = 0;
            ncBeta1.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncBeta1.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncBeta1.strides.resize(beta1Shape.size());
            for (size_t j = 0; j < beta1Shape.size(); j++) {
                ncBeta1.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncBeta1);
            long long beta1StorageSize = GetShapeSize(ncBeta1.storageShape);
            printf("[NON_CONTIG] ncBeta1 scene=%s(%d), originalShape=[", GetNonContigSceneName(ncBeta1.scene), ncBeta1.scene);
            for (int _i = 0; _i < (int)beta1Shape.size(); _i++) printf("%lld%s", (long long)beta1Shape[_i], _i < (int)beta1Shape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncBeta1.viewShape.size(); _i++) printf("%lld%s", (long long)ncBeta1.viewShape[_i], _i < (int)ncBeta1.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncBeta1.strides.size(); _i++) printf("%lld%s", (long long)ncBeta1.strides[_i], _i < (int)ncBeta1.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncBeta1.storageShape.size(); _i++) printf("%lld%s", (long long)ncBeta1.storageShape[_i], _i < (int)ncBeta1.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncBeta1.offset);
            NonContigParams ncBeta2;
            ncBeta2.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncBeta2.viewShape = beta2Shape;
            ncBeta2.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncBeta2.sliceAxis = 0;
            ncBeta2.offset = 0;
            ncBeta2.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncBeta2.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncBeta2.strides.resize(beta2Shape.size());
            for (size_t j = 0; j < beta2Shape.size(); j++) {
                ncBeta2.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncBeta2);
            long long beta2StorageSize = GetShapeSize(ncBeta2.storageShape);
            printf("[NON_CONTIG] ncBeta2 scene=%s(%d), originalShape=[", GetNonContigSceneName(ncBeta2.scene), ncBeta2.scene);
            for (int _i = 0; _i < (int)beta2Shape.size(); _i++) printf("%lld%s", (long long)beta2Shape[_i], _i < (int)beta2Shape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncBeta2.viewShape.size(); _i++) printf("%lld%s", (long long)ncBeta2.viewShape[_i], _i < (int)ncBeta2.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncBeta2.strides.size(); _i++) printf("%lld%s", (long long)ncBeta2.strides[_i], _i < (int)ncBeta2.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncBeta2.storageShape.size(); _i++) printf("%lld%s", (long long)ncBeta2.storageShape[_i], _i < (int)ncBeta2.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncBeta2.offset);
            NonContigParams ncEpsilon;
            ncEpsilon.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 5, "i");
            ncEpsilon.viewShape = epsilonShape;
            ncEpsilon.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncEpsilon.sliceAxis = 0;
            ncEpsilon.offset = 0;
            ncEpsilon.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncEpsilon.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncEpsilon.strides.resize(epsilonShape.size());
            for (size_t j = 0; j < epsilonShape.size(); j++) {
                ncEpsilon.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }
            CalcNonContiguousParams(ncEpsilon);
            long long epsilonStorageSize = GetShapeSize(ncEpsilon.storageShape);
            printf("[NON_CONTIG] ncEpsilon scene=%s(%d), originalShape=[", GetNonContigSceneName(ncEpsilon.scene), ncEpsilon.scene);
            for (int _i = 0; _i < (int)epsilonShape.size(); _i++) printf("%lld%s", (long long)epsilonShape[_i], _i < (int)epsilonShape.size() - 1 ? "," : "");
            printf("], viewShape=[");
            for (int _i = 0; _i < (int)ncEpsilon.viewShape.size(); _i++) printf("%lld%s", (long long)ncEpsilon.viewShape[_i], _i < (int)ncEpsilon.viewShape.size() - 1 ? "," : "");
            printf("], viewStrides=[");
            for (int _i = 0; _i < (int)ncEpsilon.strides.size(); _i++) printf("%lld%s", (long long)ncEpsilon.strides[_i], _i < (int)ncEpsilon.strides.size() - 1 ? "," : "");
            printf("], storageShape=[");
            for (int _i = 0; _i < (int)ncEpsilon.storageShape.size(); _i++) printf("%lld%s", (long long)ncEpsilon.storageShape[_i], _i < (int)ncEpsilon.storageShape.size() - 1 ? "," : "");
            printf("], offset=%lld\n", (long long)ncEpsilon.offset);
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> gradHostData(GetShapeSize(gradShape), 0);
                FuzzFillUniform(gradHostData.data(), gradHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(gradHostData, ncGrad, &gradDeviceAddr, dtype0, ACL_FORMAT_ND, &grad);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> gradHostData(GetShapeSize(gradShape), 0);
                FuzzFillUniform(gradHostData.data(), gradHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(gradHostData, ncGrad, &gradDeviceAddr, dtype0, ACL_FORMAT_ND, &grad);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> gradHostData(GetShapeSize(gradShape), 0);
                FuzzFillUniform(gradHostData.data(), gradHostData.size(), dtype0, -1.0f, 1.0f);
                ret = CreateAclTensorByScene(gradHostData, ncGrad, &gradDeviceAddr, dtype0, ACL_FORMAT_ND, &grad);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else { printf("unsupported grad dtype %d\n", (int)dtype0); goto ERROR; }

            // 标量类 tensor:FLOAT32 + shape=[1] + 合法值(md: 数据类型不为FLOAT32/shape大小不为1 均报161002)
            std::vector<float> beta1PowerHostData = {beta1PowerVal};
            ret = CreateAclTensorByScene(beta1PowerHostData, ncBeta1Power, &beta1PowerDeviceAddr, aclDataType::ACL_FLOAT, ACL_FORMAT_ND, &beta1Power);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> beta2PowerHostData = {beta2PowerVal};
            ret = CreateAclTensorByScene(beta2PowerHostData, ncBeta2Power, &beta2PowerDeviceAddr, aclDataType::ACL_FLOAT, ACL_FORMAT_ND, &beta2Power);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> lrHostData = {lrVal};
            ret = CreateAclTensorByScene(lrHostData, ncLr, &lrDeviceAddr, aclDataType::ACL_FLOAT, ACL_FORMAT_ND, &lr);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> beta1HostData = {beta1Val};
            ret = CreateAclTensorByScene(beta1HostData, ncBeta1, &beta1DeviceAddr, aclDataType::ACL_FLOAT, ACL_FORMAT_ND, &beta1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> beta2HostData = {beta2Val};
            ret = CreateAclTensorByScene(beta2HostData, ncBeta2, &beta2DeviceAddr, aclDataType::ACL_FLOAT, ACL_FORMAT_ND, &beta2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> epsilonHostData = {epsilonVal};
            ret = CreateAclTensorByScene(epsilonHostData, ncEpsilon, &epsilonDeviceAddr, aclDataType::ACL_FLOAT, ACL_FORMAT_ND, &epsilon);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);

            printf("[FUZZ_PARAM] varRef: dtype=%d, shape=%s; m: dtype=%d, shape=%s; v: dtype=%d, shape=%s; beta1Power: dtype=%d, shape=%s; beta2Power: dtype=%d, shape=%s; lr: dtype=%d, shape=%s; beta1: dtype=%d, shape=%s; beta2: dtype=%d, shape=%s; epsilon: dtype=%d, shape=%s; grad: dtype=%d, shape=%s; useLocking=%d; useNesterov=%d; beta1PowerVal=%f; beta2PowerVal=%f; lrVal=%f; beta1Val=%f; beta2Val=%f; epsilonVal=%f\n", (int)dtype0, ShapeToStr(varShape).c_str(), (int)dtype0, ShapeToStr(mShape).c_str(), (int)dtype0, ShapeToStr(vShape).c_str(), (int)aclDataType::ACL_FLOAT, ShapeToStr(beta1PowerShape).c_str(), (int)aclDataType::ACL_FLOAT, ShapeToStr(beta2PowerShape).c_str(), (int)aclDataType::ACL_FLOAT, ShapeToStr(lrShape).c_str(), (int)aclDataType::ACL_FLOAT, ShapeToStr(beta1Shape).c_str(), (int)aclDataType::ACL_FLOAT, ShapeToStr(beta2Shape).c_str(), (int)aclDataType::ACL_FLOAT, ShapeToStr(epsilonShape).c_str(), (int)dtype0, ShapeToStr(gradShape).c_str(), (int)useLocking, (int)useNesterov, (double)beta1PowerVal, (double)beta2PowerVal, (double)lrVal, (double)beta1Val, (double)beta2Val, (double)epsilonVal);

            ret = aclnnApplyAdamGetWorkspaceSize(varRef, m, v, beta1Power, beta2Power, lr, beta1, beta2, epsilon, grad, useLocking, useNesterov, &workspaceSize, &executor);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdamGetWorkspaceSize failed. ERROR: %d\n", ret); goto ERROR);
            if (workspaceSize > 0) {
                ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
                CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); goto ERROR);
            }
            ret = aclnnApplyAdam(workspaceAddr, workspaceSize, executor, stream);
            aclnnRet = (int)ret;
            if (ret == ACL_SUCCESS) { printf("aclnnApplyAdam execute success :%d\n", (int)ret); }
            else { printf("aclnnApplyAdam execute failed :%d\n", (int)ret); goto ERROR; }
            ret = aclrtSynchronizeStream(stream);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); goto ERROR);
            LOG_PRINT("aclnnApplyAdam execute success : %d\n", ret);
        }
        catch (std::exception& e)
        {
            printf("catch exception %s\n", e.what());
            printf("[FUZZ_RESULT] case_%d: faild, ret=%d\n", fuzzi, (int)ret);
        }

ERROR:
        ret = aclrtSynchronizeStream(stream);
        if (aclnnRet == -1) { printf("[FUZZ_RESULT] case_%d: skip, ret=-1\n", fuzzi); }
        else { printf("[FUZZ_RESULT] case_%d: %s, ret=%d\n", fuzzi, (aclnnRet == 0) ? "success" : "faild", aclnnRet); }
        printf("end \n");
        CheckAndLog(ret);
        pid_t pid = getpid();
        std::string cmd = "grep -rinE 'ai(vec|core) error' /root/ascend/log/debug/plog/ ";
        printf("system return value %d\n", CheckResult(pid));
        if (varRef) { aclDestroyTensor(varRef); }
        if (m) { aclDestroyTensor(m); }
        if (v) { aclDestroyTensor(v); }
        if (beta1Power) { aclDestroyTensor(beta1Power); }
        if (beta2Power) { aclDestroyTensor(beta2Power); }
        if (lr) { aclDestroyTensor(lr); }
        if (beta1) { aclDestroyTensor(beta1); }
        if (beta2) { aclDestroyTensor(beta2); }
        if (epsilon) { aclDestroyTensor(epsilon); }
        if (grad) { aclDestroyTensor(grad); }
        if (varRefDeviceAddr) { aclrtFree(varRefDeviceAddr); }
        if (mDeviceAddr) { aclrtFree(mDeviceAddr); }
        if (vDeviceAddr) { aclrtFree(vDeviceAddr); }
        if (beta1PowerDeviceAddr) { aclrtFree(beta1PowerDeviceAddr); }
        if (beta2PowerDeviceAddr) { aclrtFree(beta2PowerDeviceAddr); }
        if (lrDeviceAddr) { aclrtFree(lrDeviceAddr); }
        if (beta1DeviceAddr) { aclrtFree(beta1DeviceAddr); }
        if (beta2DeviceAddr) { aclrtFree(beta2DeviceAddr); }
        if (epsilonDeviceAddr) { aclrtFree(epsilonDeviceAddr); }
        if (gradDeviceAddr) { aclrtFree(gradDeviceAddr); }
        if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
    }
    DT_FUZZ_END()
    WriteDTLog("aclnnApplyAdam", "aclnnApplyAdam multithread 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("aclnnApplyAdam", "aclnnApplyAdam multithread op fuzz end \n");
    aclrtDestroyStream(stream);
    return 0;
}

extern "C" int aclnnApplyAdam_fuzz(int argc, char *argv[], aclrtContext context);
int aclnnApplyAdam_fuzz(int argc, char *argv[], aclrtContext context)
{
    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("aclnnApplyAdam_non_contig :%d \n", aclnnApplyAdam_Normal(fuzz_num, context));
    return 0;
}