#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 <unistd.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 "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 <complex>
#include "../fuzzlib/secodeFuzz.h"
#include "aclnnop/aclnn_any.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";
}
}
int aclnnAny_non_contig(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("aclnnAny", "aclnnAny non_contig op fuzz start \n");
int dtype0Enum[] = { 12, 2, 4, 6, 3, 9, 27, 1, 0, 11 };
int dtype0Num = sizeof(dtype0Enum) / sizeof(dtype0Enum[0]);
int dtype1Enum[] = { 12, 4 };
int dtype1Num = sizeof(dtype1Enum) / sizeof(dtype1Enum[0]);
int dim0Enum[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int dim0Num = sizeof(dim0Enum) / sizeof(dim0Enum[0]);
aclrtSetOpExecuteTimeOut(1091);
DT_Set_TimeOut_Second(3000);
int success_count = 0;
int faild_count = 0;
DT_FUZZ_START(0, fuzz_num, "aclnnAny_non_contig",0)
{
int i=0;
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
aclIntArray* dim = nullptr;
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
void* workspaceAddr = nullptr;
LOG_PRINT("aclnnAny_non_contig %d\n",fuzzi);
printf("[FUZZ_LOG] plog_file: aclnnAny_non_contig_plog_case_%d\n", fuzzi);
try
{
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;
}
auto dtype0 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_BOOL, dtype0Enum, dtype0Num);
printf("dtype0: %d\n",dtype0);
auto dtype1 = (aclDataType)*(int *)DT_SetGetNumberEnumV3(i++, aclDataType::ACL_BOOL, dtype1Enum, dtype1Num);
printf("dtype1: %d\n",dtype1);
int dim_num0 = *(int *)DT_SetGetNumberEnumV3(i++, 1, dim0Enum, dim0Num);
bool keepdim = (bool) *(int *)DTSetGetNumberRange(&g_Element[i++],0,0,1,"i");
printf("keepdim: %d\n", keepdim);
printf("dim_num0: %d\n", dim_num0);
std::vector <int64_t> dimArray = {};
int dimArrayLen = 0;
for (int64_t index = 0; index < dim_num0; index++){
int isDim = *(int *)DTSetGetNumberRange(&g_Element[i++],1, 0, 1, "i");
printf("isDim: %d\n", isDim);
if (isDim == 0){
continue;
}
else{
dimArray.push_back(index);
dimArrayLen += 1;
}
}
if (dimArrayLen != 0){
for (int64_t i = 0; i < dimArrayLen; i++){
printf("dimArray_%d: %d\n", i, dimArray[i]);
}
}
else{
printf("dimArray: {}\n");
goto ERROR;
}
aclIntArray * dim = aclCreateIntArray(dimArray.data(), dimArray.size());
std::vector <int64_t> selfShape = {};
std::vector <int64_t> outShape = {};
for (int64_t index = 0; index < dim_num0; index++){
int64_t dim_value = *(int * )DTSetGetNumberRange(&g_Element[i++], 5, 1, 256, "i");
selfShape.push_back(dim_value);
bool isReduceDim = false;
for (int64_t i = 0; i < dimArrayLen; i++){
if (index == dimArray[i]) {
isReduceDim = true;
break;
}
}
if (isReduceDim && keepdim == 1) {
outShape.push_back(1);
} else if (isReduceDim && keepdim == 0) {
continue;
} else {
outShape.push_back(dim_value);
}
}
aclFormat format0;
if(dim_num0 == 1){
printf("selfShape:{%d}\n",selfShape[0]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 2){
printf("selfShape:{%d,%d}\n",selfShape[0],selfShape[1]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 3){
printf("selfShape:{%d,%d,%d}\n",selfShape[0],selfShape[1],selfShape[2]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 4){
printf("selfShape:{%d,%d,%d,%d}\n",selfShape[0],selfShape[1],selfShape[2],selfShape[3]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 5){
printf("selfShape:{%d,%d,%d,%d,%d}\n",selfShape[0],selfShape[1],selfShape[2],selfShape[3],selfShape[4]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 6){
printf("selfShape:{%d,%d,%d,%d,%d,%d}\n",selfShape[0],selfShape[1],selfShape[2],selfShape[3],selfShape[4],selfShape[5]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 7){
printf("selfShape:{%d,%d,%d,%d,%d,%d,%d}\n",selfShape[0],selfShape[1],selfShape[2],selfShape[3],selfShape[4],selfShape[5],selfShape[6]);
format0 = aclFormat::ACL_FORMAT_ND;
} else if(dim_num0 == 8){
printf("selfShape:{%d,%d,%d,%d,%d,%d,%d,%d}\n",selfShape[0],selfShape[1],selfShape[2],selfShape[3],selfShape[4],selfShape[5],selfShape[6],selfShape[7]);
format0 = aclFormat::ACL_FORMAT_ND;
}
long long selfShape_size = GetShapeSize(selfShape);
if(selfShape_size > SHAPE_SIZE_CTL)
{
printf("selfShape_size too big\n");
goto ERROR;
}
long long outShape_size = GetShapeSize(outShape);
if(outShape_size > SHAPE_SIZE_CTL)
{
printf("outShape_size too big\n");
goto ERROR;
}
NonContigParams ncSelf;
ncSelf.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 2, 5, "i");
if (ncSelf.scene < 2) ncSelf.scene = 2;
ncSelf.viewShape = selfShape;
ncSelf.sliceStep = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 2, 5, "i");
ncSelf.sliceAxis = 0;
ncSelf.offset = 0;
ncSelf.pos1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 1, 4, "i");
ncSelf.pos2 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 4, "i");
ncSelf.strides.resize(selfShape.size());
for (size_t j = 0; j < selfShape.size(); j++) {
ncSelf.strides[j] = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 2, 1, 20, "i");
}
CalcNonContiguousParams(ncSelf);
long long selfStorageSize = GetShapeSize(ncSelf.storageShape);
printf("[NON_CONTIG] ncSelf scene=%s(%d), originalShape=[", GetNonContigSceneName(ncSelf.scene), ncSelf.scene);
for (int _i = 0; _i < (int)selfShape.size(); _i++) printf("%lld%s", (long long)selfShape[_i], _i < (int)selfShape.size() - 1 ? "," : "");
printf("], viewShape=[");
for (int _i = 0; _i < (int)ncSelf.viewShape.size(); _i++) printf("%lld%s", (long long)ncSelf.viewShape[_i], _i < (int)ncSelf.viewShape.size() - 1 ? "," : "");
printf("], viewStrides=[");
for (int _i = 0; _i < (int)ncSelf.strides.size(); _i++) printf("%lld%s", (long long)ncSelf.strides[_i], _i < (int)ncSelf.strides.size() - 1 ? "," : "");
printf("], storageShape=[");
for (int _i = 0; _i < (int)ncSelf.storageShape.size(); _i++) printf("%lld%s", (long long)ncSelf.storageShape[_i], _i < (int)ncSelf.storageShape.size() - 1 ? "," : "");
printf("], offset=%lld\n", (long long)ncSelf.offset);
if (dtype0 == aclDataType::ACL_BOOL) {
std::vector<int8_t> selfHostData (selfStorageSize, 1);
long long totalSize = selfShape_size + outShape_size + selfStorageSize;
if (totalSize < totalSizeMin || totalSize > totalSizeMax) { goto ERROR; }
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_INT8) {
std::vector<int8_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_UINT8) {
std::vector<uint8_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_INT16) {
std::vector<int16_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_INT32) {
std::vector<int32_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_INT64) {
std::vector<int64_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_DOUBLE) {
std::vector<double> selfHostData (selfStorageSize, 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_COMPLEX64) {
std::vector<std::complex<float>> selfHostData (selfShape_size , 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_COMPLEX128) {
std::vector<std::complex<double>> selfHostData (selfShape_size , 1);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, dtype0, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else {
std::vector<float> selfHostData(GetShapeSize(ncSelf.storageShape), 0);
ret = CreateAclTensorByScene(selfHostData, ncSelf, &selfDeviceAddr, ACL_FLOAT, ACL_FORMAT_ND, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
NonContigParams ncOut;
ncOut.scene = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 2, 5, "i");
if (ncOut.scene < 2) ncOut.scene = 2;
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(ncOut);
long long outStorageSize = GetShapeSize(ncOut.storageShape);
printf("[NON_CONTIG] ncOut scene=%s(%d), originalShape=[", GetNonContigSceneName(ncOut.scene), ncOut.scene);
for (int _i = 0; _i < (int)outShape.size(); _i++) printf("%lld%s", (long long)outShape[_i], _i < (int)outShape.size() - 1 ? "," : "");
printf("], viewShape=[");
for (int _i = 0; _i < (int)ncOut.viewShape.size(); _i++) printf("%lld%s", (long long)ncOut.viewShape[_i], _i < (int)ncOut.viewShape.size() - 1 ? "," : "");
printf("], viewStrides=[");
for (int _i = 0; _i < (int)ncOut.strides.size(); _i++) printf("%lld%s", (long long)ncOut.strides[_i], _i < (int)ncOut.strides.size() - 1 ? "," : "");
printf("], storageShape=[");
for (int _i = 0; _i < (int)ncOut.storageShape.size(); _i++) printf("%lld%s", (long long)ncOut.storageShape[_i], _i < (int)ncOut.storageShape.size() - 1 ? "," : "");
printf("], offset=%lld\n", (long long)ncOut.offset);
if (dtype1 == aclDataType::ACL_BOOL) {
std::vector<int8_t> outHostData (outStorageSize, 1);
ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, dtype1, ACL_FORMAT_ND, &out);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype1 == aclDataType::ACL_UINT8) {
std::vector<uint8_t> outHostData (outStorageSize, 1);
ret = CreateAclTensorByScene(outHostData, ncOut, &outDeviceAddr, dtype1, ACL_FORMAT_ND, &out);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else {
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);
}
ret = aclnnAnyGetWorkspaceSize(self, dim, keepdim, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAnyGetWorkspaceSize 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 = aclnnAny(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAny 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 (dim != nullptr) { aclDestroyIntArray(dim); dim = 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("=== aclnnAny_non_contig SUMMARY: total=%d, success=%d, faild=%d ===\n", fuzz_num, success_count, faild_count);
WriteDTLog("aclnnAny", "aclnnAny 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("aclnnAny", "aclnnAny non_contig op fuzz end \n");
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}
extern "C" int aclnnAny_non_contig_fuzz(int argc, char *argv[]);
int aclnnAny_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("aclnnAny : %d \n", aclnnAny_non_contig(fuzz_num));
return 0;
}