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

using namespace std;

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 aclnnAddLayerNormQuant_Normal(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);
    WriteDTLog("aclnnAddLayerNormQuant", "aclnnAddLayerNormQuant op fuzz start \n");
    const long long kSingleInputSizeMax = 1073741824LL;
    const long long kTotalInputSizeMax = 2147483648LL;

    // dtype enums
    int dtype0Enum[] = { 0, 1, 27 };
    int dtype0Num = sizeof(dtype0Enum) / sizeof(dtype0Enum[0]);
    int dtype1Enum[] = { 2 };
    int dtype1Num = sizeof(dtype1Enum) / sizeof(dtype1Enum[0]);
    int dtype2Enum[] = { 0 };
    int dtype2Num = sizeof(dtype2Enum) / sizeof(dtype2Enum[0]);

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
    DT_FUZZ_START(0, fuzz_num, "aclnnAddLayerNormQuant", 0)
    {
        int i = 0;
        int shapeDimMax = 16;
        int aclnnRet = -1;  // aclnn 实际调用返回值(成功判定依据)
        void* x1DeviceAddr = nullptr;
        aclTensor* x1 = nullptr;
        void* x2DeviceAddr = nullptr;
        aclTensor* x2 = nullptr;
        void* gammaDeviceAddr = nullptr;
        aclTensor* gamma = nullptr;
        void* betaDeviceAddr = nullptr;
        aclTensor* beta = nullptr;
        void* biasOptionalDeviceAddr = nullptr;
        aclTensor* biasOptional = nullptr;
        void* scales1OptionalDeviceAddr = nullptr;
        aclTensor* scales1Optional = nullptr;
        void* scales2OptionalDeviceAddr = nullptr;
        aclTensor* scales2Optional = nullptr;
        void* zeroPoints1OptionalDeviceAddr = nullptr;
        aclTensor* zeroPoints1Optional = nullptr;
        void* zeroPoints2OptionalDeviceAddr = nullptr;
        aclTensor* zeroPoints2Optional = nullptr;
        void* y1OutDeviceAddr = nullptr;
        aclTensor* y1Out = nullptr;
        void* y2OutDeviceAddr = nullptr;
        aclTensor* y2Out = nullptr;
        void* xOutDeviceAddr = nullptr;
        aclTensor* xOut = nullptr;
        void* outScales1OutDeviceAddr = nullptr;
        aclTensor* outScales1Out = nullptr;
        void* outScales2OutDeviceAddr = nullptr;
        aclTensor* outScales2Out = nullptr;
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor;
        void* workspaceAddr = nullptr;

        LOG_PRINT("aclnnAddLayerNormQuant_Normal %d\n", fuzzi);
        printf("[FUZZ_LOG] plog_file: aclnnAddLayerNormQuant_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, 7, "i");  // 维度约束 [1,7]
            {
                const long long sizeTargets[4] = {1000LL, 1048576LL, 16777216LL, 134217728LL};
                int nTensors = 14;
                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);
            auto dtype1 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_INT8, dtype1Enum, dtype1Num);
            auto dtype2 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_FLOAT, dtype2Enum, dtype2Num);
            static const char* quantModeChoices[] = {"static", "dynamic"};
            char* quantMode = (char*)quantModeChoices[*(int *)DTSetGetNumberRange(&g_Element[i++], 1, 0, 1, "i")];
            if (strcmp(quantMode, "dynamic") == 0 && dtype0 == aclDataType::ACL_FLOAT) { dtype0 = aclDataType::ACL_FLOAT16; }
            if (dim_num0 < 2) { dim_num0 = 2; }  // dynamic 模式 x1 需 2-8 维,且保证 outScales 至少 1 维
            double epsilon = *(double *)DTSetGetNumberRangeDouble(&g_Element[i++], 1, -100, 100, "i");
            if (epsilon != epsilon || std::fabs(epsilon) < 1e-4) { epsilon = 1e-5; }  // tiling 按 float 读取要求 > 0
            else if (epsilon < 0) { epsilon = -epsilon; }
            bool additionalOutput = (bool)*(int *)DTSetGetNumberRange(&g_Element[i++], 0, 0, 1, "i");
            bool divMode = (bool)*(int *)DTSetGetNumberRange(&g_Element[i++], 0, 0, 1, "i");
            int dim_num1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");
            std::vector<int64_t> x1Shape;
            std::vector<int64_t> x2Shape;
            std::vector<int64_t> gammaShape;
            std::vector<int64_t> betaShape;
            std::vector<int64_t> biasOptionalShape;
            std::vector<int64_t> scales1OptionalShape;
            std::vector<int64_t> scales2OptionalShape;
            std::vector<int64_t> zeroPoints1OptionalShape;
            std::vector<int64_t> zeroPoints2OptionalShape;
            std::vector<int64_t> y1OutShape;
            std::vector<int64_t> y2OutShape;
            std::vector<int64_t> xOutShape;
            std::vector<int64_t> outScales1OutShape;
            std::vector<int64_t> outScales2OutShape;
            if (dim_num0 == 1) {
                x1Shape = {shape1};
            } else if (dim_num0 == 2) {
                x1Shape = {shape1, shape2};
            } else if (dim_num0 == 3) {
                x1Shape = {shape1, shape2, shape3};
            } else if (dim_num0 == 4) {
                x1Shape = {shape1, shape2, shape3, shape4};
            } else if (dim_num0 == 5) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5};
            } else if (dim_num0 == 6) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (dim_num0 == 7) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (dim_num0 == 8) {
                x1Shape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }
            int x2DimNum = dim_num1 < 1 ? 1 : (dim_num1 > 8 ? 8 : dim_num1);
            if (x2DimNum == 1) {
                x2Shape = {shape1};
            } else if (x2DimNum == 2) {
                x2Shape = {shape1, shape2};
            } else if (x2DimNum == 3) {
                x2Shape = {shape1, shape2, shape3};
            } else if (x2DimNum == 4) {
                x2Shape = {shape1, shape2, shape3, shape4};
            } else if (x2DimNum == 5) {
                x2Shape = {shape1, shape2, shape3, shape4, shape5};
            } else if (x2DimNum == 6) {
                x2Shape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (x2DimNum == 7) {
                x2Shape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (x2DimNum == 8) {
                x2Shape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }
            // [SHAPE] 关系约束:x2=x1;gamma/beta/bias/scales1 = x1 后 k 维(dynamic 模式 gamma 必须 1 维);outScales = x1 去掉最后一维
            int x1DimN = (int)x1Shape.size();
            int normK = 1 + (dim_num1 % x1DimN);
            if (strcmp(quantMode, "dynamic") == 0) { normK = 1; }  // dyn quant 要求 gamma.dims()==1
            x2Shape = x1Shape;
            gammaShape.assign(x1Shape.end() - normK, x1Shape.end());
            betaShape = gammaShape;
            biasOptionalShape = gammaShape;
            scales1OptionalShape = gammaShape;
            y1OutShape = x1Shape;
            y2OutShape = x1Shape;
            xOutShape = x1Shape;
            outScales1OutShape.assign(x1Shape.begin(), x1Shape.end() - 1);
            outScales2OutShape = outScales1OutShape;
            long long shape_size = GetShapeSize(x1Shape);
            if (shape_size > kSingleInputSizeMax) { printf("shape_size too big\n"); goto ERROR; }
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> x1HostData(GetShapeSize(x1Shape), 0);
                FuzzInitHostData(x1HostData.data(), x1HostData.size(), dtype0);
                ret = CreateAclTensor_New(x1HostData, x1Shape, &x1DeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &x1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> x1HostData(GetShapeSize(x1Shape), 0);
                FuzzInitHostData(x1HostData.data(), x1HostData.size(), dtype0);
                ret = CreateAclTensor_New(x1HostData, x1Shape, &x1DeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &x1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> x1HostData(GetShapeSize(x1Shape), 0);
                FuzzInitHostData(x1HostData.data(), x1HostData.size(), dtype0);
                ret = CreateAclTensor_New(x1HostData, x1Shape, &x1DeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &x1);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> x2HostData(GetShapeSize(x2Shape), 0);
                FuzzInitHostData(x2HostData.data(), x2HostData.size(), dtype0);
                ret = CreateAclTensor_New(x2HostData, x2Shape, &x2DeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &x2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> x2HostData(GetShapeSize(x2Shape), 0);
                FuzzInitHostData(x2HostData.data(), x2HostData.size(), dtype0);
                ret = CreateAclTensor_New(x2HostData, x2Shape, &x2DeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &x2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> x2HostData(GetShapeSize(x2Shape), 0);
                FuzzInitHostData(x2HostData.data(), x2HostData.size(), dtype0);
                ret = CreateAclTensor_New(x2HostData, x2Shape, &x2DeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &x2);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> gammaHostData(GetShapeSize(gammaShape), 0);
                FuzzInitHostData(gammaHostData.data(), gammaHostData.size(), dtype0);
                ret = CreateAclTensor_New(gammaHostData, gammaShape, &gammaDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &gamma);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> gammaHostData(GetShapeSize(gammaShape), 0);
                FuzzInitHostData(gammaHostData.data(), gammaHostData.size(), dtype0);
                ret = CreateAclTensor_New(gammaHostData, gammaShape, &gammaDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &gamma);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> gammaHostData(GetShapeSize(gammaShape), 0);
                FuzzInitHostData(gammaHostData.data(), gammaHostData.size(), dtype0);
                ret = CreateAclTensor_New(gammaHostData, gammaShape, &gammaDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &gamma);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> betaHostData(GetShapeSize(betaShape), 0);
                FuzzInitHostData(betaHostData.data(), betaHostData.size(), dtype0);
                ret = CreateAclTensor_New(betaHostData, betaShape, &betaDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &beta);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> betaHostData(GetShapeSize(betaShape), 0);
                FuzzInitHostData(betaHostData.data(), betaHostData.size(), dtype0);
                ret = CreateAclTensor_New(betaHostData, betaShape, &betaDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &beta);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> betaHostData(GetShapeSize(betaShape), 0);
                FuzzInitHostData(betaHostData.data(), betaHostData.size(), dtype0);
                ret = CreateAclTensor_New(betaHostData, betaShape, &betaDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &beta);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> biasOptionalHostData(GetShapeSize(biasOptionalShape), 0);
                FuzzInitHostData(biasOptionalHostData.data(), biasOptionalHostData.size(), dtype0);
                ret = CreateAclTensor_New(biasOptionalHostData, biasOptionalShape, &biasOptionalDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &biasOptional);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> biasOptionalHostData(GetShapeSize(biasOptionalShape), 0);
                FuzzInitHostData(biasOptionalHostData.data(), biasOptionalHostData.size(), dtype0);
                ret = CreateAclTensor_New(biasOptionalHostData, biasOptionalShape, &biasOptionalDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &biasOptional);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> biasOptionalHostData(GetShapeSize(biasOptionalShape), 0);
                FuzzInitHostData(biasOptionalHostData.data(), biasOptionalHostData.size(), dtype0);
                ret = CreateAclTensor_New(biasOptionalHostData, biasOptionalShape, &biasOptionalDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &biasOptional);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> scales1OptionalHostData(GetShapeSize(scales1OptionalShape), 1.0f);
                ret = CreateAclTensor_New(scales1OptionalHostData, scales1OptionalShape, &scales1OptionalDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &scales1Optional);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> scales1OptionalHostData(GetShapeSize(scales1OptionalShape), 15360);
                ret = CreateAclTensor_New(scales1OptionalHostData, scales1OptionalShape, &scales1OptionalDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &scales1Optional);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> scales1OptionalHostData(GetShapeSize(scales1OptionalShape), 16256);
                ret = CreateAclTensor_New(scales1OptionalHostData, scales1OptionalShape, &scales1OptionalDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &scales1Optional);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            // scales2Optional/zeroPoints1Optional/zeroPoints2Optional 传 nullptr:s1=T,s2=F,zp=F 为 static/dynamic 合法组合
            if (dtype0 == aclDataType::ACL_FLOAT) {
                std::vector<float> xOutHostData(GetShapeSize(xOutShape), 0);
                FuzzInitHostData(xOutHostData.data(), xOutHostData.size(), dtype0);
                ret = CreateAclTensor_New(xOutHostData, xOutShape, &xOutDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &xOut);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> xOutHostData(GetShapeSize(xOutShape), 0);
                FuzzInitHostData(xOutHostData.data(), xOutHostData.size(), dtype0);
                ret = CreateAclTensor_New(xOutHostData, xOutShape, &xOutDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &xOut);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (dtype0 == aclDataType::ACL_BF16) {
                std::vector<int16_t> xOutHostData(GetShapeSize(xOutShape), 0);
                FuzzInitHostData(xOutHostData.data(), xOutHostData.size(), dtype0);
                ret = CreateAclTensor_New(xOutHostData, xOutShape, &xOutDeviceAddr, dtype0, aclFormat::ACL_FORMAT_ND, &xOut);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            std::vector<int8_t> y1OutHostData(GetShapeSize(y1OutShape), 0);
            FuzzInitHostData(y1OutHostData.data(), y1OutHostData.size(), dtype1);
            ret = CreateAclTensor_New(y1OutHostData, y1OutShape, &y1OutDeviceAddr, dtype1, aclFormat::ACL_FORMAT_ND, &y1Out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<int8_t> y2OutHostData(GetShapeSize(y2OutShape), 0);
            FuzzInitHostData(y2OutHostData.data(), y2OutHostData.size(), dtype1);
            ret = CreateAclTensor_New(y2OutHostData, y2OutShape, &y2OutDeviceAddr, dtype1, aclFormat::ACL_FORMAT_ND, &y2Out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> outScales1OutHostData(GetShapeSize(outScales1OutShape), 0);
            FuzzInitHostData(outScales1OutHostData.data(), outScales1OutHostData.size(), dtype2);
            ret = CreateAclTensor_New(outScales1OutHostData, outScales1OutShape, &outScales1OutDeviceAddr, dtype2, aclFormat::ACL_FORMAT_ND, &outScales1Out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            std::vector<float> outScales2OutHostData(GetShapeSize(outScales2OutShape), 0);
            FuzzInitHostData(outScales2OutHostData.data(), outScales2OutHostData.size(), dtype2);
            ret = CreateAclTensor_New(outScales2OutHostData, outScales2OutShape, &outScales2OutDeviceAddr, dtype2, aclFormat::ACL_FORMAT_ND, &outScales2Out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);

            ret = aclnnAddLayerNormQuantGetWorkspaceSize(x1, x2, gamma, beta, biasOptional, scales1Optional, scales2Optional, zeroPoints1Optional, zeroPoints2Optional, quantMode, epsilon, additionalOutput, divMode, y1Out, y2Out, xOut, outScales1Out, outScales2Out, &workspaceSize, &executor);
printf("[FUZZ_PARAM] x1: dtype=%d, shape=%s; x2: dtype=%d, shape=%s; gamma: dtype=%d, shape=%s; beta: dtype=%d, shape=%s; biasOptional: dtype=%d, shape=%s; scales1Optional: dtype=%d, shape=%s; scales2Optional: dtype=%d, shape=%s; zeroPoints1Optional: dtype=%d, shape=%s; zeroPoints2Optional: dtype=%d, shape=%s; y1Out: dtype=%d, shape=%s; y2Out: dtype=%d, shape=%s; xOut: dtype=%d, shape=%s; outScales1Out: dtype=%d, shape=%s; outScales2Out: dtype=%d, shape=%s; quantMode=%s; epsilon=%f; additionalOutput=%d; divMode=%d\n", (int)dtype0, ShapeToStr(x1Shape).c_str(), (int)dtype0, ShapeToStr(x2Shape).c_str(), (int)dtype0, ShapeToStr(gammaShape).c_str(), (int)dtype0, ShapeToStr(betaShape).c_str(), (int)dtype0, ShapeToStr(biasOptionalShape).c_str(), (int)dtype0, ShapeToStr(scales1OptionalShape).c_str(), (int)dtype0, ShapeToStr(scales2OptionalShape).c_str(), (int)dtype0, ShapeToStr(zeroPoints1OptionalShape).c_str(), (int)dtype0, ShapeToStr(zeroPoints2OptionalShape).c_str(), (int)dtype1, ShapeToStr(y1OutShape).c_str(), (int)dtype1, ShapeToStr(y2OutShape).c_str(), (int)dtype0, ShapeToStr(xOutShape).c_str(), (int)dtype2, ShapeToStr(outScales1OutShape).c_str(), (int)dtype2, ShapeToStr(outScales2OutShape).c_str(), (quantMode ? quantMode : "null"), (double)epsilon, (int)additionalOutput, (int)divMode);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAddLayerNormQuantGetWorkspaceSize 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 = aclnnAddLayerNormQuant(workspaceAddr, workspaceSize, executor, stream);
            aclnnRet = (int)ret;
            if (ret == ACL_SUCCESS) { printf("aclnnAddLayerNormQuant execute success :%d\n", (int)ret); }
            else { printf("aclnnAddLayerNormQuant 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("aclnnAddLayerNormQuant execute success : %d\n", ret);
        }
        catch (std::exception& e)
        {
            printf("catch exception %s\n", e.what());
        }

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 -rin 'aicore error' /root/ascend/log/debug/plog/ ";
        printf("system return value %d\n", CheckResult(pid));
        if (x1) { aclDestroyTensor(x1); }
        if (x2) { aclDestroyTensor(x2); }
        if (gamma) { aclDestroyTensor(gamma); }
        if (beta) { aclDestroyTensor(beta); }
        if (biasOptional) { aclDestroyTensor(biasOptional); }
        if (scales1Optional) { aclDestroyTensor(scales1Optional); }
        if (scales2Optional) { aclDestroyTensor(scales2Optional); }
        if (zeroPoints1Optional) { aclDestroyTensor(zeroPoints1Optional); }
        if (zeroPoints2Optional) { aclDestroyTensor(zeroPoints2Optional); }
        if (y1Out) { aclDestroyTensor(y1Out); }
        if (y2Out) { aclDestroyTensor(y2Out); }
        if (xOut) { aclDestroyTensor(xOut); }
        if (outScales1Out) { aclDestroyTensor(outScales1Out); }
        if (outScales2Out) { aclDestroyTensor(outScales2Out); }
        if (x1DeviceAddr) { aclrtFree(x1DeviceAddr); }
        if (x2DeviceAddr) { aclrtFree(x2DeviceAddr); }
        if (gammaDeviceAddr) { aclrtFree(gammaDeviceAddr); }
        if (betaDeviceAddr) { aclrtFree(betaDeviceAddr); }
        if (biasOptionalDeviceAddr) { aclrtFree(biasOptionalDeviceAddr); }
        if (scales1OptionalDeviceAddr) { aclrtFree(scales1OptionalDeviceAddr); }
        if (scales2OptionalDeviceAddr) { aclrtFree(scales2OptionalDeviceAddr); }
        if (zeroPoints1OptionalDeviceAddr) { aclrtFree(zeroPoints1OptionalDeviceAddr); }
        if (zeroPoints2OptionalDeviceAddr) { aclrtFree(zeroPoints2OptionalDeviceAddr); }
        if (y1OutDeviceAddr) { aclrtFree(y1OutDeviceAddr); }
        if (y2OutDeviceAddr) { aclrtFree(y2OutDeviceAddr); }
        if (xOutDeviceAddr) { aclrtFree(xOutDeviceAddr); }
        if (outScales1OutDeviceAddr) { aclrtFree(outScales1OutDeviceAddr); }
        if (outScales2OutDeviceAddr) { aclrtFree(outScales2OutDeviceAddr); }
        if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
    }
    DT_FUZZ_END()
    WriteDTLog("aclnnAddLayerNormQuant", "aclnnAddLayerNormQuant 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("aclnnAddLayerNormQuant", "aclnnAddLayerNormQuant op fuzz end \n");
    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}

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