#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_clamp.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 aclnnClampMinTensor_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("aclnnClampMinTensor", "aclnnClampMinTensor op fuzz start \n");
    const long long kSingleInputSizeMax = 1073741824LL;
    const long long kTotalInputSizeMax = 2147483648LL;

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

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
    DT_FUZZ_START(0, fuzz_num, "aclnnClampMinTensor", 0)
    {
        int i = 0;
        int shapeDimMax = 16;
        int aclnnRet = -1;  // aclnn 实际调用返回值(成功判定依据)
        void* selfDeviceAddr = nullptr;
        aclTensor* self = nullptr;
        void* clipValueMinDeviceAddr = nullptr;
        aclTensor* clipValueMin = nullptr;
        void* outDeviceAddr = nullptr;
        aclTensor* out = nullptr;
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor;
        void* workspaceAddr = nullptr;

        LOG_PRINT("aclnnClampMinTensor_Normal %d\n", fuzzi);
        printf("[FUZZ_LOG] plog_file: aclnnClampMinTensor_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");  // 维度约束 [1,8]
            {
                const long long sizeTargets[4] = {1000LL, 1048576LL, 16777216LL, 134217728LL};
                int nTensors = 3;
                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_FLOAT16, dtype0Enum, dtype0Num);
            // === Tiling coverage: per-tensor dtype aliases ===
            aclDataType selfDtype = dtype0;
            aclDataType clipValueMinDtype = dtype0;
            aclDataType outDtype = dtype0;
            int dim_num1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");
            std::vector<int64_t> selfShape;
            std::vector<int64_t> clipValueMinShape;
            std::vector<int64_t> outShape;
            if (dim_num0 == 1) {
                selfShape = {shape1};
            } else if (dim_num0 == 2) {
                selfShape = {shape1, shape2};
            } else if (dim_num0 == 3) {
                selfShape = {shape1, shape2, shape3};
            } else if (dim_num0 == 4) {
                selfShape = {shape1, shape2, shape3, shape4};
            } else if (dim_num0 == 5) {
                selfShape = {shape1, shape2, shape3, shape4, shape5};
            } else if (dim_num0 == 6) {
                selfShape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (dim_num0 == 7) {
                selfShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (dim_num0 == 8) {
                selfShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }
            outShape = selfShape;
            clipValueMinShape = selfShape;
            // === Tiling key coverage: 15 scenarios, round-robin (fuzzi % 15), every key >= MIN cases ===
            {
                int caseIdx = fuzzi % 15;
                int modeIdx = -1;
                switch (caseIdx) {
                case 0: { // TK001: xDtype == ge::DT_FLOAT16 || xDtype == ge::DT_BF16
                    selfDtype = aclDataType::ACL_FLOAT16;
                    clipValueMinDtype = aclDataType::ACL_FLOAT16;
                    outDtype = aclDataType::ACL_FLOAT16;
                    if (fuzzi < 15) { selfShape = {4096}; }
                    else { selfShape = {(1 + rand() % 8192)}; } // tiling one_dim: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 1; (void)dim_num0;
                    dim_num0 = 1; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK001, shape_mode=one_dim, dtype=self:FLOAT16,clipValueMin:FLOAT16,out:FLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 1: { // TK002: xDtype == ge::DT_FLOAT16 || xDtype == ge::DT_BF16
                    selfDtype = aclDataType::ACL_FLOAT16;
                    clipValueMinDtype = aclDataType::ACL_FLOAT16;
                    outDtype = aclDataType::ACL_FLOAT16;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling contiguous: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK002, shape_mode=contiguous_multidim, dtype=self:FLOAT16,clipValueMin:FLOAT16,out:FLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 2: { // TK003: xDtype == ge::DT_FLOAT16 || xDtype == ge::DT_BF16
                    selfDtype = aclDataType::ACL_FLOAT16;
                    clipValueMinDtype = aclDataType::ACL_FLOAT16;
                    outDtype = aclDataType::ACL_FLOAT16;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling broadcast: base shape
                    clipValueMinShape.clear();
                    for (int _d = 1; _d < 3; _d++) clipValueMinShape.push_back(selfShape[_d]);
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK003, shape_mode=broadcast_ub, dtype=self:FLOAT16,clipValueMin:FLOAT16,out:FLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 3: { // TK004: xDtype == ge::DT_FLOAT16 || xDtype == ge::DT_BF16
                    selfDtype = aclDataType::ACL_BF16;
                    clipValueMinDtype = aclDataType::ACL_BF16;
                    outDtype = aclDataType::ACL_BF16;
                    if (fuzzi < 15) { selfShape = {4096}; }
                    else { selfShape = {(1 + rand() % 8192)}; } // tiling one_dim: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 1; (void)dim_num0;
                    dim_num0 = 1; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK004, shape_mode=one_dim, dtype=self:BFLOAT16,clipValueMin:BFLOAT16,out:BFLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 4: { // TK005: xDtype == ge::DT_FLOAT16 || xDtype == ge::DT_BF16
                    selfDtype = aclDataType::ACL_BF16;
                    clipValueMinDtype = aclDataType::ACL_BF16;
                    outDtype = aclDataType::ACL_BF16;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling contiguous: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK005, shape_mode=contiguous_multidim, dtype=self:BFLOAT16,clipValueMin:BFLOAT16,out:BFLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 5: { // TK006: xDtype == ge::DT_FLOAT16 || xDtype == ge::DT_BF16
                    selfDtype = aclDataType::ACL_BF16;
                    clipValueMinDtype = aclDataType::ACL_BF16;
                    outDtype = aclDataType::ACL_BF16;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling broadcast: base shape
                    clipValueMinShape.clear();
                    for (int _d = 1; _d < 3; _d++) clipValueMinShape.push_back(selfShape[_d]);
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK006, shape_mode=broadcast_ub, dtype=self:BFLOAT16,clipValueMin:BFLOAT16,out:BFLOAT16\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 6: { // TK007: xDtype == ge::DT_FLOAT
                    selfDtype = aclDataType::ACL_FLOAT;
                    clipValueMinDtype = aclDataType::ACL_FLOAT;
                    outDtype = aclDataType::ACL_FLOAT;
                    if (fuzzi < 15) { selfShape = {4096}; }
                    else { selfShape = {(1 + rand() % 8192)}; } // tiling one_dim: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 1; (void)dim_num0;
                    dim_num0 = 1; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK007, shape_mode=one_dim, dtype=self:FLOAT,clipValueMin:FLOAT,out:FLOAT\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 7: { // TK008: xDtype == ge::DT_FLOAT
                    selfDtype = aclDataType::ACL_FLOAT;
                    clipValueMinDtype = aclDataType::ACL_FLOAT;
                    outDtype = aclDataType::ACL_FLOAT;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling contiguous: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK008, shape_mode=contiguous_multidim, dtype=self:FLOAT,clipValueMin:FLOAT,out:FLOAT\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 8: { // TK009: xDtype == ge::DT_FLOAT
                    selfDtype = aclDataType::ACL_FLOAT;
                    clipValueMinDtype = aclDataType::ACL_FLOAT;
                    outDtype = aclDataType::ACL_FLOAT;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling broadcast: base shape
                    clipValueMinShape.clear();
                    for (int _d = 1; _d < 3; _d++) clipValueMinShape.push_back(selfShape[_d]);
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK009, shape_mode=broadcast_ub, dtype=self:FLOAT,clipValueMin:FLOAT,out:FLOAT\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 9: { // TK010: xDtype == ge::DT_INT32
                    selfDtype = aclDataType::ACL_INT32;
                    clipValueMinDtype = aclDataType::ACL_INT32;
                    outDtype = aclDataType::ACL_INT32;
                    if (fuzzi < 15) { selfShape = {4096}; }
                    else { selfShape = {(1 + rand() % 8192)}; } // tiling one_dim: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 1; (void)dim_num0;
                    dim_num0 = 1; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK010, shape_mode=one_dim, dtype=self:INT32,clipValueMin:INT32,out:INT32\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 10: { // TK011: xDtype == ge::DT_INT32
                    selfDtype = aclDataType::ACL_INT32;
                    clipValueMinDtype = aclDataType::ACL_INT32;
                    outDtype = aclDataType::ACL_INT32;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling contiguous: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK011, shape_mode=contiguous_multidim, dtype=self:INT32,clipValueMin:INT32,out:INT32\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 11: { // TK012: xDtype == ge::DT_INT32
                    selfDtype = aclDataType::ACL_INT32;
                    clipValueMinDtype = aclDataType::ACL_INT32;
                    outDtype = aclDataType::ACL_INT32;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling broadcast: base shape
                    clipValueMinShape.clear();
                    for (int _d = 1; _d < 3; _d++) clipValueMinShape.push_back(selfShape[_d]);
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK012, shape_mode=broadcast_ub, dtype=self:INT32,clipValueMin:INT32,out:INT32\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 12: { // TK013: xDtype == ge::DT_INT64
                    selfDtype = aclDataType::ACL_INT64;
                    clipValueMinDtype = aclDataType::ACL_INT64;
                    outDtype = aclDataType::ACL_INT64;
                    if (fuzzi < 15) { selfShape = {4096}; }
                    else { selfShape = {(1 + rand() % 8192)}; } // tiling one_dim: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 1; (void)dim_num0;
                    dim_num0 = 1; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK013, shape_mode=one_dim, dtype=self:INT64,clipValueMin:INT64,out:INT64\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 13: { // TK014: xDtype == ge::DT_INT64
                    selfDtype = aclDataType::ACL_INT64;
                    clipValueMinDtype = aclDataType::ACL_INT64;
                    outDtype = aclDataType::ACL_INT64;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling contiguous: shared random shape
                    clipValueMinShape = selfShape;
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK014, shape_mode=contiguous_multidim, dtype=self:INT64,clipValueMin:INT64,out:INT64\n", fuzzi);
                    break; (void)modeIdx;
                }
                case 14: { // TK015: xDtype == ge::DT_INT64
                    selfDtype = aclDataType::ACL_INT64;
                    clipValueMinDtype = aclDataType::ACL_INT64;
                    outDtype = aclDataType::ACL_INT64;
                    if (fuzzi < 15) { selfShape = {2, 4, 16}; }
                    else { selfShape = {(1 + rand() % 64), (1 + rand() % 64), (1 + rand() % 64)}; } // tiling broadcast: base shape
                    clipValueMinShape.clear();
                    for (int _d = 1; _d < 3; _d++) clipValueMinShape.push_back(selfShape[_d]);
                    outShape = selfShape;
                    dim_num0 = 3; (void)dim_num0;
                    dim_num0 = 3; (void)dim_num0;
                    printf("[FUZZ_TILING] case_%d: tiling_id=TK015, shape_mode=broadcast_ub, dtype=self:INT64,clipValueMin:INT64,out:INT64\n", fuzzi);
                    break; (void)modeIdx;
                }
                default: break;
                }
            }
            long long shape_size = GetShapeSize(selfShape);
            if (shape_size > kSingleInputSizeMax) { printf("shape_size too big\n"); goto ERROR; }
            if (selfDtype == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_FLOAT) {
                std::vector<float> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_DOUBLE) {
                std::vector<double> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_INT8) {
                std::vector<int8_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_UINT8) {
                std::vector<uint8_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_INT16) {
                std::vector<int16_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_INT32) {
                std::vector<int32_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_INT64) {
                std::vector<int64_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (selfDtype == aclDataType::ACL_BF16) {
                std::vector<int16_t> selfHostData(GetShapeSize(selfShape), 0);
                FuzzInitHostData(selfHostData.data(), selfHostData.size(), selfDtype);
                ret = CreateAclTensor_New(selfHostData, selfShape, &selfDeviceAddr, selfDtype, aclFormat::ACL_FORMAT_ND, &self);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            if (clipValueMinDtype == aclDataType::ACL_FLOAT16) {
                std::vector<int16_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_FLOAT) {
                std::vector<float> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_DOUBLE) {
                std::vector<double> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_INT8) {
                std::vector<int8_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_UINT8) {
                std::vector<uint8_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_INT16) {
                std::vector<int16_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_INT32) {
                std::vector<int32_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_INT64) {
                std::vector<int64_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (clipValueMinDtype == aclDataType::ACL_BF16) {
                std::vector<int16_t> clipValueMinHostData(GetShapeSize(clipValueMinShape), 0);
                FuzzInitHostData(clipValueMinHostData.data(), clipValueMinHostData.size(), clipValueMinDtype);
                ret = CreateAclTensor_New(clipValueMinHostData, clipValueMinShape, &clipValueMinDeviceAddr, clipValueMinDtype, aclFormat::ACL_FORMAT_ND, &clipValueMin);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }
            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);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else 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);
            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);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_INT8) {
                std::vector<int8_t> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_UINT8) {
                std::vector<uint8_t> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_INT16) {
                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);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_INT32) {
                std::vector<int32_t> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } else if (outDtype == aclDataType::ACL_INT64) {
                std::vector<int64_t> outHostData(GetShapeSize(outShape), 0);
                FuzzInitHostData(outHostData.data(), outHostData.size(), outDtype);
                ret = CreateAclTensor_New(outHostData, outShape, &outDeviceAddr, outDtype, aclFormat::ACL_FORMAT_ND, &out);
            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);
            CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            }

            ret = aclnnClampMinTensorGetWorkspaceSize(self, clipValueMin, out, &workspaceSize, &executor);
printf("[FUZZ_PARAM] self: dtype=%d, shape=%s; clipValueMin: dtype=%d, shape=%s; out: dtype=%d, shape=%s\n", (int)dtype0, ShapeToStr(selfShape).c_str(), (int)dtype0, ShapeToStr(clipValueMinShape).c_str(), (int)dtype0, ShapeToStr(outShape).c_str());
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnClampMinTensorGetWorkspaceSize 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 = aclnnClampMinTensor(workspaceAddr, workspaceSize, executor, stream);
            aclnnRet = (int)ret;
            if (ret == ACL_SUCCESS) { printf("aclnnClampMinTensor execute success :%d\n", (int)ret); }
            else { printf("aclnnClampMinTensor 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("aclnnClampMinTensor 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 (self) { aclDestroyTensor(self); }
        if (clipValueMin) { aclDestroyTensor(clipValueMin); }
        if (out) { aclDestroyTensor(out); }
        if (selfDeviceAddr) { aclrtFree(selfDeviceAddr); }
        if (clipValueMinDeviceAddr) { aclrtFree(clipValueMinDeviceAddr); }
        if (outDeviceAddr) { aclrtFree(outDeviceAddr); }
        if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
    }
    DT_FUZZ_END()
    WriteDTLog("aclnnClampMinTensor", "aclnnClampMinTensor 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("aclnnClampMinTensor", "aclnnClampMinTensor op fuzz end \n");
    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}

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