AAlice_zhouupdate
97284875Add on Oct 8History
#include <iostream>
#include <vector>
#include <getopt.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctime>
#include <sys/wait.h>
#include <dlfcn.h>
#include <cstdio>
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <string.h>
#include <time.h>
#include <ctime>
#include <list>
#include <algorithm>
#include <stdarg.h>
#include <complex>
#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_abs.h"

using namespace std;

static int kDtypeEnum[] = { 0, 1, 2, 3, 4, 6, 9, 11, 12, 27, 16 };
static int kDtypeNum = sizeof(kDtypeEnum) / sizeof(kDtypeEnum[0]);

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 const char* GetDtypeName(aclDataType dt) {
    switch(dt) {
        case 0: return "FLOAT";
        case 1: return "FLOAT16";
        case 2: return "INT8";
        case 3: return "INT32";
        case 4: return "UINT8";
        case 6: return "INT16";
        case 9: return "INT64";
        case 11: return "DOUBLE";
        case 12: return "BOOL";
        case 27: return "BF16";
        case 16: return "COMPLEX64";
        default: return "UNKNOWN";
    }
}

int aclnnAbs_NonContig(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("aclnnAbs", "aclnnAbs non_contig op fuzz start \n");

    aclrtSetOpExecuteTimeOut(1091);
    DT_Set_TimeOut_Second(3000);
        int success_count = 0;
    int faild_count = 0;
DT_FUZZ_START(0, fuzz_num, (char*)__func__, 0)
    {
        int i = 0;
        void* selfDeviceAddr = nullptr;
        void* outDeviceAddr = nullptr;
        aclTensor* self = nullptr;
        aclTensor* out = nullptr;
        uint64_t workspaceSize = 0;
        aclOpExecutor* executor;
        void* workspaceAddr = nullptr;

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

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

            int sizeBucket = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 0, 3, "i");

            long long totalSizeMin = 0, totalSizeMax = 0;

            switch (sizeBucket) {

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

                case 1: totalSizeMin = 1001;       totalSizeMax = 1073741824LL;  break;

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

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

            }

            int shape1 = *(int*)DTSetGetNumberRange(&g_Element[i++], 4, 1, 256, "i");
            int shape2 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");
            int shape3 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");
            int shape4 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");
            int shape5 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");
            int shape6 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");
            int shape7 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");
            int shape8 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 256, "i");

            auto inputDtype = (aclDataType)*(int*)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_FLOAT, kDtypeEnum, kDtypeNum);

            int dim_num = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");

            std::vector<int64_t> selfShape = {};
            if (dim_num == 0) { selfShape = {}; } else 
            if (dim_num == 1) {
                selfShape = {shape1};
            } else if (dim_num == 2) {
                selfShape = {shape1, shape2};
            } else if (dim_num == 3) {
                selfShape = {shape1, shape2, shape3};
            } else if (dim_num == 4) {
                selfShape = {shape1, shape2, shape3, shape4};
            } else if (dim_num == 5) {
                selfShape = {shape1, shape2, shape3, shape4, shape5};
            } else if (dim_num == 6) {
                selfShape = {shape1, shape2, shape3, shape4, shape5, shape6};
            } else if (dim_num == 7) {
                selfShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
            } else if (dim_num == 8) {
                selfShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
            }

            std::vector<int64_t> outShape = selfShape;

            long long shape_size = GetShapeSize(selfShape);
            printf("selfShape size %lld\n", shape_size);
            if (shape_size > SHAPE_SIZE_CTL) {
                goto ERROR;
            }

            int ndim = selfShape.size();
            int sceneMax = (ndim >= 2) ? 5 : 4;

            // DT elements for NonContigParams on self - must be consumed unconditionally
            NonContigParams nc;
            nc.scene = *(int*)DTSetGetNumberRange(&g_Element[i++], 1, 1, sceneMax, "i");
            nc.viewShape = selfShape;
            nc.sliceStep = *(int64_t*)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            nc.sliceAxis = 0;
            nc.offset = 0;
            nc.pos1 = *(int*)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            nc.pos2 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            nc.strides.resize(selfShape.size());
            for (size_t j = 0; j < selfShape.size(); j++) {
                nc.strides[j] = *(int64_t*)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }

            // DT elements for NonContigParams on out - must be consumed unconditionally
            NonContigParams ncOut;
            int outSceneMax = (ndim >= 2) ? 5 : 4;
            ncOut.scene = *(int*)DTSetGetNumberRange(&g_Element[i++], 1, 2, outSceneMax, "i");
            if (ncOut.scene < 2) ncOut.scene = 2 + (ncOut.scene % (outSceneMax - 1));
            ncOut.viewShape = outShape;
            ncOut.sliceStep = *(int64_t*)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
            ncOut.sliceAxis = 0;
            ncOut.offset = 0;
            ncOut.pos1 = *(int*)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
            ncOut.pos2 = *(int*)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
            ncOut.strides.resize(outShape.size());
            for (size_t j = 0; j < outShape.size(); j++) {
                ncOut.strides[j] = *(int64_t*)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
            }

            CalcNonContiguousParams(nc);
            long long selfStorageSize = GetShapeSize(nc.storageShape);

            int outSceneOrig = ncOut.scene;
            CalcNonContiguousParams(ncOut);
            long long outStorageSize = GetShapeSize(ncOut.storageShape);

            if (inputDtype == aclDataType::ACL_FLOAT) {
                std::vector<float> selfHostData(outStorageSize, 1);

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

                long long totalSize = shape_size + selfStorageSize + outStorageSize;

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

                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<float> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create out Tensor failed. ERROR: %d\n", ret); goto ERROR);
                printf("inputDtype:ACL_FLOAT\n");
            } else if (inputDtype == aclDataType::ACL_FLOAT16) {
                std::vector<float> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<float> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create out Tensor failed. ERROR: %d\n", ret); goto ERROR);
                printf("inputDtype:ACL_FLOAT16\n");
            } else if (inputDtype == aclDataType::ACL_BF16) {
                std::vector<int16_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<int16_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Create out Tensor failed. ERROR: %d\n", ret); goto ERROR);
                printf("inputDtype:ACL_BF16\n");
            } else if (inputDtype == aclDataType::ACL_INT8) {
                std::vector<int8_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<int8_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_INT8\n");
            } else if (inputDtype == aclDataType::ACL_INT32) {
                std::vector<int32_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<int32_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_INT32\n");
            } else if (inputDtype == aclDataType::ACL_UINT8) {
                std::vector<uint8_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<uint8_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_UINT8\n");
            } else if (inputDtype == aclDataType::ACL_INT16) {
                std::vector<int16_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<int16_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_INT16\n");
            } else if (inputDtype == aclDataType::ACL_INT64) {
                std::vector<int64_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<int64_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_INT64\n");
            } else if (inputDtype == aclDataType::ACL_DOUBLE) {
                std::vector<double> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<double> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_DOUBLE\n");
            } else if (inputDtype == aclDataType::ACL_BOOL) {
                std::vector<int8_t> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<int8_t> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:ACL_BOOL\n");
            
            } else if (inputDtype == aclDataType::ACL_COMPLEX64) {
                std::vector<std::complex<float>> selfHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, inputDtype, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<std::complex<float>> outHostData(outStorageSize, 1);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, inputDtype, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                printf("inputDtype:COMPLEX64\n");
 } else {
                std::vector<float> selfHostData(GetShapeSize(nc.storageShape), 0);
                ret = CreateAclTensorByScene(selfHostData, nc, &selfDeviceAddr, ACL_FLOAT, ACL_FORMAT_ND, &self);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
                std::vector<float> outHostData(GetShapeSize(ncOut.storageShape), 0);
                ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, ACL_FLOAT, ACL_FORMAT_ND, &out);
                CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
            } 

            // Print FUZZ_PARAM
            {
                std::string selfShapeStr = "[";
                for (size_t j = 0; j < selfShape.size(); j++) { selfShapeStr += std::to_string(selfShape[j]); if (j+1 < selfShape.size()) selfShapeStr += ","; }
                selfShapeStr += "]";
                std::string outShapeStr = "[";
                for (size_t j = 0; j < outShape.size(); j++) { outShapeStr += std::to_string(outShape[j]); if (j+1 < outShape.size()) outShapeStr += ","; }
                outShapeStr += "]";
                printf("[FUZZ_PARAM] self=%s,out=%s, selfShape=%s,outShape=%s, format=ND, noncontig=self=%s,out=%s\n",
                    GetDtypeName(inputDtype), GetDtypeName(inputDtype),
                    selfShapeStr.c_str(), outShapeStr.c_str(),
                    GetNonContigSceneName(nc.scene), GetNonContigSceneName(outSceneOrig));
            }

            ret = aclnnAbsGetWorkspaceSize(self, out, &workspaceSize, &executor);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAbsGetWorkspaceSize 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 = aclnnAbs(workspaceAddr, workspaceSize, executor, stream);
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAbs failed. ERROR: %d\n", ret); goto ERROR);

            {

                aclError syncRet = aclrtSynchronizeStream(stream);

                (void)syncRet;

            }
            CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); goto ERROR);
            LOG_PRINT("aclnn execute success : %d\n", ret);
        }
        catch (std::exception& e)
        {
            printf("catch exception %s\n", e.what());
        }
ERROR:
        {
            aclError syncRet = aclrtSynchronizeStream(stream);
            (void)syncRet;
        }
        printf("end \n");
        CheckAndLog(ret);
        if (ret == 0) { success_count++; } else { faild_count++; }
        printf("[FUZZ_RESULT] case_%d: %s, ret=%d\n", fuzzi, ret == 0 ? "success" : "faild", 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 != nullptr) { aclDestroyTensor(self); self = nullptr; }
        if (out != nullptr) { aclDestroyTensor(out); out = nullptr; }

        if (selfDeviceAddr != nullptr) { aclrtFree(selfDeviceAddr); selfDeviceAddr = nullptr; }
        if (outDeviceAddr != nullptr) { aclrtFree(outDeviceAddr); outDeviceAddr = nullptr; }
        if (workspaceSize > 0) {
            if (workspaceAddr != nullptr) { aclrtFree(workspaceAddr); workspaceAddr = nullptr; }
        }
    }
    DT_FUZZ_END()
    printf("=== aclnnAbs_non_contig SUMMARY: total=%d, success=%d, faild=%d ===\n", fuzz_num, success_count, faild_count);
    WriteDTLog("aclnnAbs", "aclnnAbs non_contig 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("aclnnAbs", "aclnnAbs non_contig op fuzz end \n");
    aclrtDestroyStream(stream);
    aclrtDestroyContext(context);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}

extern "C" int aclnnAbs_non_contig_fuzz(int argc, char *argv[]);
int aclnnAbs_non_contig_fuzz(int argc, char *argv[])
{
    int fuzz_num = MAXFUZZNUMBER;

    if (argc < 1) {
        cout << "[ERROR] input arg num must be bigger than 1 \n" << endl;
        cout << "The second arg stand for testcase num  \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("aclnnAbs_NonContig :%d \n", aclnnAbs_NonContig(fuzz_num));

    return 0;
}