#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_binary_cross_entropy_with_logits_target_backward.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 + "]";
}
int aclnnBinaryCrossEntropyWithLogitsTargetBackward_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("aclnnBinaryCrossEntropyWithLogitsTargetBackward", "aclnnBinaryCrossEntropyWithLogitsTargetBackward op fuzz start \n");
const long long kSingleInputSizeMax = 1073741824LL;
const long long kTotalInputSizeMax = 2147483648LL;
int dtype0Enum[] = { 1, 0, 27 };
int dtype0Num = sizeof(dtype0Enum) / sizeof(dtype0Enum[0]);
aclrtSetOpExecuteTimeOut(1091);
DT_Set_TimeOut_Second(3000);
DT_FUZZ_START(0, fuzz_num, "aclnnBinaryCrossEntropyWithLogitsTargetBackward", 0)
{
int i = 0;
int shapeDimMax = 16;
int aclnnRet = -1;
void* gradOutputDeviceAddr = nullptr;
aclTensor* gradOutput = nullptr;
void* selfDeviceAddr = nullptr;
aclTensor* self = nullptr;
void* targetDeviceAddr = nullptr;
aclTensor* target = nullptr;
void* weightOptionalDeviceAddr = nullptr;
aclTensor* weightOptional = nullptr;
void* posWeightOptionalDeviceAddr = nullptr;
aclTensor* posWeightOptional = nullptr;
void* gradTargetDeviceAddr = nullptr;
aclTensor* gradTarget = nullptr;
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
void* workspaceAddr = nullptr;
LOG_PRINT("aclnnBinaryCrossEntropyWithLogitsTargetBackward_Normal %d\n", fuzzi);
printf("[FUZZ_LOG] plog_file: aclnnBinaryCrossEntropyWithLogitsTargetBackward_fuzz_plog_case_%d.log\n", fuzzi);
aclnnRet = -1;
try
{
int sizeBucket = *(int *)DTSetGetNumberRange(&g_Element[i++], 1, 0, 3, "i");
int dim_num0 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 1, 8, "i");
{
const long long sizeTargets[4] = {1000LL, 1048576LL, 16777216LL, 134217728LL};
int nTensors = 6;
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);
int64_t reduction = *(int64_t *)DTSetGetNumberRangeS64(&g_Element[i++], 1, 0, 2, "i");
int dim_num1 = *(int *)DTSetGetNumberRange(&g_Element[i++], 2, 0, 8, "i");
std::vector<int64_t> gradOutputShape = {};
if (dim_num0 == 1) {
gradOutputShape = {shape1};
} else if (dim_num0 == 2) {
gradOutputShape = {shape1, shape2};
} else if (dim_num0 == 3) {
gradOutputShape = {shape1, shape2, shape3};
} else if (dim_num0 == 4) {
gradOutputShape = {shape1, shape2, shape3, shape4};
} else if (dim_num0 == 5) {
gradOutputShape = {shape1, shape2, shape3, shape4, shape5};
} else if (dim_num0 == 6) {
gradOutputShape = {shape1, shape2, shape3, shape4, shape5, shape6};
} else if (dim_num0 == 7) {
gradOutputShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7};
} else if (dim_num0 == 8) {
gradOutputShape = {shape1, shape2, shape3, shape4, shape5, shape6, shape7, shape8};
}
std::vector<int64_t> selfShape = gradOutputShape;
std::vector<int64_t> targetShape = gradOutputShape;
std::vector<int64_t> weightOptionalShape = gradOutputShape;
std::vector<int64_t> posWeightOptionalShape = gradOutputShape;
std::vector<int64_t> gradTargetShape = gradOutputShape;
long long shape_size = GetShapeSize(gradTargetShape);
if (shape_size > kSingleInputSizeMax) { printf("shape_size too big\n"); goto ERROR; }
if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> gradOutputHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, dtype0, &gradOutput);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> gradOutputHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, dtype0, &gradOutput);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> gradOutputHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr, dtype0, &gradOutput);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> selfHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(selfHostData, gradOutputShape, &selfDeviceAddr, dtype0, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> selfHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(selfHostData, gradOutputShape, &selfDeviceAddr, dtype0, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> selfHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(selfHostData, gradOutputShape, &selfDeviceAddr, dtype0, &self);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> targetHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(targetHostData, gradOutputShape, &targetDeviceAddr, dtype0, &target);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> targetHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(targetHostData, gradOutputShape, &targetDeviceAddr, dtype0, &target);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> targetHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(targetHostData, gradOutputShape, &targetDeviceAddr, dtype0, &target);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> weightOptionalHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(weightOptionalHostData, gradOutputShape, &weightOptionalDeviceAddr, dtype0, &weightOptional);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> weightOptionalHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(weightOptionalHostData, gradOutputShape, &weightOptionalDeviceAddr, dtype0, &weightOptional);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> weightOptionalHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(weightOptionalHostData, gradOutputShape, &weightOptionalDeviceAddr, dtype0, &weightOptional);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> posWeightOptionalHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(posWeightOptionalHostData, gradOutputShape, &posWeightOptionalDeviceAddr, dtype0, &posWeightOptional);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> posWeightOptionalHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(posWeightOptionalHostData, gradOutputShape, &posWeightOptionalDeviceAddr, dtype0, &posWeightOptional);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> posWeightOptionalHostData(GetShapeSize(gradOutputShape), 1);
ret = CreateAclTensor(posWeightOptionalHostData, gradOutputShape, &posWeightOptionalDeviceAddr, dtype0, &posWeightOptional);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
if (dtype0 == aclDataType::ACL_FLOAT16) {
std::vector<int16_t> gradTargetHostData(GetShapeSize(gradTargetShape), 1);
ret = CreateAclTensor(gradTargetHostData, gradTargetShape, &gradTargetDeviceAddr, dtype0, &gradTarget);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_FLOAT) {
std::vector<float> gradTargetHostData(GetShapeSize(gradTargetShape), 1);
ret = CreateAclTensor(gradTargetHostData, gradTargetShape, &gradTargetDeviceAddr, dtype0, &gradTarget);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
} else if (dtype0 == aclDataType::ACL_BF16) {
std::vector<int16_t> gradTargetHostData(GetShapeSize(gradTargetShape), 1);
ret = CreateAclTensor(gradTargetHostData, gradTargetShape, &gradTargetDeviceAddr, dtype0, &gradTarget);
CHECK_RET(ret == ACL_SUCCESS, goto ERROR);
}
ret = aclnnBinaryCrossEntropyWithLogitsTargetBackwardGetWorkspaceSize(gradOutput, self, target, weightOptional, posWeightOptional, reduction, gradTarget, &workspaceSize, &executor);
printf("[FUZZ_PARAM] gradOutput: dtype=%d, shape=%s; self: dtype=%d, shape=%s; target: dtype=%d, shape=%s; weightOptional: dtype=%d, shape=%s; posWeightOptional: dtype=%d, shape=%s; gradTarget: dtype=%d, shape=%s; reduction=%lld\n", (int)dtype0, ShapeToStr(gradOutputShape).c_str(), (int)dtype0, ShapeToStr(selfShape).c_str(), (int)dtype0, ShapeToStr(targetShape).c_str(), (int)dtype0, ShapeToStr(weightOptionalShape).c_str(), (int)dtype0, ShapeToStr(posWeightOptionalShape).c_str(), (int)dtype0, ShapeToStr(gradTargetShape).c_str(), (long long)reduction);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBinaryCrossEntropyWithLogitsTargetBackwardGetWorkspaceSize 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 = aclnnBinaryCrossEntropyWithLogitsTargetBackward(workspaceAddr, workspaceSize, executor, stream);
aclnnRet = (int)ret;
if (ret == ACL_SUCCESS) { printf("aclnnBinaryCrossEntropyWithLogitsTargetBackward execute success :%d\n", (int)ret); }
else { printf("aclnnBinaryCrossEntropyWithLogitsTargetBackward 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("aclnnBinaryCrossEntropyWithLogitsTargetBackward 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 -rinE 'ai(vec|core) error' /root/ascend/log/debug/plog/ ";
printf("system return value %d\n", CheckResult(pid));
if (gradOutput) { aclDestroyTensor(gradOutput); }
if (self) { aclDestroyTensor(self); }
if (target) { aclDestroyTensor(target); }
if (weightOptional) { aclDestroyTensor(weightOptional); }
if (posWeightOptional) { aclDestroyTensor(posWeightOptional); }
if (gradTarget) { aclDestroyTensor(gradTarget); }
if (gradOutputDeviceAddr) { aclrtFree(gradOutputDeviceAddr); }
if (selfDeviceAddr) { aclrtFree(selfDeviceAddr); }
if (targetDeviceAddr) { aclrtFree(targetDeviceAddr); }
if (weightOptionalDeviceAddr) { aclrtFree(weightOptionalDeviceAddr); }
if (posWeightOptionalDeviceAddr) { aclrtFree(posWeightOptionalDeviceAddr); }
if (gradTargetDeviceAddr) { aclrtFree(gradTargetDeviceAddr); }
if (workspaceSize > 0 && workspaceAddr) { aclrtFree(workspaceAddr); }
}
DT_FUZZ_END()
WriteDTLog("aclnnBinaryCrossEntropyWithLogitsTargetBackward", "aclnnBinaryCrossEntropyWithLogitsTargetBackward 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("aclnnBinaryCrossEntropyWithLogitsTargetBackward", "aclnnBinaryCrossEntropyWithLogitsTargetBackward op fuzz end \n");
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}
extern "C" int aclnnBinaryCrossEntropyWithLogitsTargetBackward_fuzz(int argc, char *argv[]);
int aclnnBinaryCrossEntropyWithLogitsTargetBackward_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("aclnnBinaryCrossEntropyWithLogitsTargetBackward_Normal :%d \n", aclnnBinaryCrossEntropyWithLogitsTargetBackward_Normal(fuzz_num));
return 0;
}