#include #include #include #include #include #include #include #include "xmedia_cl.h" #include "xmedia_mmz.h" #include "xmedia_sys.h" #include "xmedia_npu.h" #define ALIGN_BYTE 8 #define DEFAULT_INOUT_NUM 16 #define ALIGN_FUNC(A, ALIGN) \ ((((A) % (ALIGN)) == 0) ? (A) : ((A) + (ALIGN) - ((A) % (ALIGN)))) typedef struct _ptheadargs{ xmedia_cl_context context; int i; xmedia_cl_device_id *devices; char *nocache_buffer_start[1]; xmedia_cl_u32 worksize; } ptheadargs_t; static int array_cmp_fixed(void *p_exp, void *p_got, xmedia_cl_data_type dtype, // 0: unsigned, 1: signed int len, char *info_label, int delta) { int idx = 0; int first_error_idx = -1, first_expect_value = 0, first_got_value = 0; int max_error_idx = -1, max_expect_value = 0, max_got_value = 0; int max_error_value = 0, mismatch_cnt = 0; for (idx = 0; idx < len; idx++) { int error = 0; int exp_int = 0; int got_int = 0; if (dtype == XMEDIA_CL_INT8) { exp_int = (int)(*((char *)p_exp + idx)); got_int = (int)(*((char *)p_got + idx)); } else if (dtype == XMEDIA_CL_UINT8) { exp_int = (int)(*((unsigned char *)p_exp + idx)); got_int = (int)(*((unsigned char *)p_got + idx)); } else if (dtype == XMEDIA_CL_INT32) { exp_int = (int)(*((int *)p_exp + idx)); got_int = (int)(*((int *)p_got + idx)); } else { } error = abs(exp_int - got_int); if (error > 0) { if (first_error_idx == -1) { first_error_idx = idx; first_expect_value = exp_int; first_got_value = got_int; } if (error > max_error_value) { max_error_idx = idx; max_error_value = error; max_expect_value = exp_int; max_got_value = got_int; } mismatch_cnt++; //printf("%s mismatch at index %d exp %d got %d (delta %d)\n", info_label, idx, exp_int, got_int, delta); } if (error > delta) { printf("%s: error at index %d exp %d got %d\n", info_label, idx, exp_int, got_int); printf("%s: first mismatch at index %d exp %d got %d (delta %d)\n", info_label, first_error_idx, first_expect_value, first_got_value, delta); printf("%s: total mismatch count %d (delta %d) \n", info_label, mismatch_cnt, delta); printf("compare failed!!!\n"); return -1; } } if (max_error_idx != -1) { printf("%s: first mismatch at index %d exp %d got %d (delta %d)\n", info_label, first_error_idx, first_expect_value, first_got_value, delta); printf("%s: max mismatch at index %d exp %d got %d (delta %d)\n", info_label, max_error_idx, max_expect_value, max_got_value, delta); printf("%s: total mismatch count %d (delta %d) \n", info_label, mismatch_cnt, delta); } printf("compare succeed!!!\n"); return 0; } static xmedia_cl_s32 copy_file_to_buff(const char *src_file, char *des, unsigned int offset, unsigned int size) { int ret; xmedia_cl_u32 count; FILE *fp = fopen(src_file, "r"); if (fp == NULL) { return -1; } ret = fseek(fp, offset, SEEK_SET); if (ret != 0) { fclose(fp); return -1; } count = fread(des, 1, size, fp); if (count != size) { printf("want to read size = 0x%x, in fact count = 0x%x\n", size, count); fclose(fp); return -1; } fclose(fp); return XMEDIA_CL_SUCCESS; } static xmedia_cl_s32 malloc_inout_tensor_mem(xmedia_cl_tensor_info_inout *inout) { inout->tensor = malloc(sizeof(xmedia_cl_tensor) * inout->num); inout->current_batch = malloc(sizeof(xmedia_cl_u32) * inout->num); inout->tensor_batch = malloc(sizeof(xmedia_cl_tensor_batch) * inout->num); if (inout->tensor == NULL || inout->current_batch == NULL || inout->tensor_batch == NULL) { return XMEDIA_CL_OUT_OF_HOST_MEMORY; } return XMEDIA_CL_SUCCESS; } static void free_inout_tensor_mem(xmedia_cl_tensor_info_inout inout) { if (inout.tensor != NULL) { free(inout.tensor); } if (inout.current_batch != NULL) { free(inout.current_batch); } if (inout.tensor_batch != NULL) { free(inout.tensor_batch); } } xmedia_s32 XMEDIA_API_SYS_MmzAlloc_Cached(xmedia_u64 *pu64PhyAddr, xmedia_void **ppVirAddr, const xmedia_char *strMmb, const xmedia_char *strZone, xmedia_u32 u32Len) { *pu64PhyAddr = xmedia_mmz_alloc(strZone, strMmb, u32Len); if (*pu64PhyAddr == 0) { return XMEDIA_CL_OUT_OF_HOST_MEMORY; } *ppVirAddr = xmedia_mmz_map(*pu64PhyAddr, u32Len, 1); if (*ppVirAddr == NULL) { return XMEDIA_CL_OUT_OF_HOST_MEMORY; } return XMEDIA_CL_SUCCESS; } xmedia_s32 XMEDIA_API_SYS_MmzFlushCache(xmedia_u64 u64PhyAddr, xmedia_void *pVirAddr, xmedia_u32 u32Size) { return xmedia_mmz_flush_cache(u64PhyAddr, pVirAddr, u32Size); } xmedia_s32 XMEDIA_API_SYS_MmzFree(xmedia_u64 u64PhyAddr, xmedia_void *pVirAddr) { xmedia_cl_s32 ret; ret = xmedia_mmz_unmap(pVirAddr); if (ret != XMEDIA_CL_SUCCESS) { return -1; } return xmedia_mmz_free(u64PhyAddr); } static int load_model_file_withmem_workspace(void * args){ xmedia_cl_s32 k = 0; xmedia_cl_u32 num_devices = 0; xmedia_cl_u64 u64SrcPhyAddr[4] = { 0 }; xmedia_char *nocache_buffer_start[4] = { 0 }; xmedia_cl_device_id *devices = NULL; xmedia_cl_context context = NULL; xmedia_cl_s32 err_code = 0; ptheadargs_t *args_t = (ptheadargs_t *)args; k = (xmedia_cl_s32)args_t->i; void *buff[DEFAULT_INOUT_NUM] = { 0 }; xmedia_cl_s8 name[64]; xmedia_cl_s32 output_size[DEFAULT_INOUT_NUM]; xmedia_cl_s8 model[64]; sprintf(model, "data/gnn%d.bin", k); xmedia_cl_s32 i = 0, j, size, ret; xmedia_cl_graph graph = NULL; xmedia_cl_u32 input_num = 0, output_num = 0; xmedia_cl_tensor_info_inout input = { 0 }, output = { 0 }; xmedia_cl_u32 worksize, weightsize, inputsize = 0, outputsize = 0; //运行时初始化 ret = xmedia_cl_init(); if (ret != XMEDIA_CL_SUCCESS) { ret = xmedia_cl_uninit(); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_uninit1 err, errno %d\n", ret); return ret; } printf("xmedia_cl_init err, errno %d\n", ret); } //第一次调用该接口获取设备数量 ret = xmedia_cl_get_device_ids(XMEDIA_CL_DEVICE_ALL, NULL, &num_devices); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_get_device_ids err, errno %d\n", ret); goto ERROR; } devices = (xmedia_cl_device_id *)calloc(num_devices, sizeof(xmedia_cl_device_id)); if (devices == NULL) { printf("calloc err\n"); ret = XMEDIA_CL_OUT_OF_HOST_MEMORY; goto ERROR; } //第二次调用该接口传递获取到的设备数量申请设备资源 ret = xmedia_cl_get_device_ids(XMEDIA_CL_DEVICE_ALL, devices, &num_devices); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_get_device_ids err, errno %d\n", ret); free(devices); xmedia_cl_uninit(); xmedia_sys_exit(); return ret; } //创建资源管理的对象 context = xmedia_cl_create_context(num_devices, devices, &err_code); if (err_code != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_create_context err, errno %d\n", ret); goto ERROR; } //从模型文件中查询work和weight的大小 ret = xmedia_cl_graph_querysize_from_file(model, &worksize, &weightsize); if (XMEDIA_CL_SUCCESS != ret) { printf("xmedia_cl_graph_querysize_from_file, errno %d\n", ret); goto ERROR; } /* //根据查询到的大小申请work和weight的内存 ret = XMEDIA_API_SYS_MmzAlloc_Cached(&u64SrcPhyAddr[0], (void **)(&nocache_buffer_start[0]), NULL, NULL, worksize); if (XMEDIA_CL_SUCCESS != ret) { goto ERROR; }*/ ret = XMEDIA_API_SYS_MmzAlloc_Cached(&u64SrcPhyAddr[1], (void **)(&nocache_buffer_start[1]), "npu_weight", NULL, weightsize); if (XMEDIA_CL_SUCCESS != ret) { goto ERROR; } printf("load_model_file_withmem_workspace, model:%s\n", model); ret = xmedia_cl_get_workspace_addr(context, (void **)&nocache_buffer_start[0], &worksize); if (XMEDIA_CL_SUCCESS != ret) { printf("xmedia_cl_get_workspace_addr, errno %d\n", ret); goto ERROR; } //printf("xmedia_cl_get_workspace_addr ,nocache_buffer_start[0]:0X%x, worksize:%d\n", (xmedia_cl_u32)nocache_buffer_start[0], worksize); //加载解析模型文件,若是从文件中加载,则model为文件路径,若从内存中加载,则model为内存地址 ret = xmedia_cl_graph_loadmodel_from_file_withmem( &context, model, nocache_buffer_start[0], worksize, nocache_buffer_start[1], weightsize, &graph); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_loadmodel err, errno %d\n", ret); goto ERROR; } //第一次调用该接口获取输入数量 ret = xmedia_cl_graph_get_input(graph, input_num, &input); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_get_input, errno %d\n", ret); goto ERROR; } if (input.num > DEFAULT_INOUT_NUM) { printf("model input num is greater than default value!\n"); goto ERROR; } //根据输入数量申请内存 malloc_inout_tensor_mem(&input); input_num = input.num; //第二次调用该接口获取输入信息 ret = xmedia_cl_graph_get_input(graph, input_num, &input); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_get_input, errno %d\n", ret); goto ERROR; } //第一次调用该接口获取输出数量 ret = xmedia_cl_graph_get_output(graph, output_num, &output); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_get_output, errno %d\n", ret); goto ERROR; } if (output.num > DEFAULT_INOUT_NUM) { printf("model output num is greater than default value!\n"); goto ERROR; } //根据输出数量申请内存 malloc_inout_tensor_mem(&output); output_num = output.num; //第二次调用该接口获取输出信息 ret = xmedia_cl_graph_get_output(graph, output_num, &output); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_get_output, errno %d\n", ret); goto ERROR; } //计算输入的大小并申请内存 for (i = 0; i < input.num; i++) { size = input.tensor[i].size; inputsize += ALIGN_FUNC(size, ALIGN_BYTE); } ret = XMEDIA_API_SYS_MmzAlloc_Cached(&u64SrcPhyAddr[2], (void **)(&nocache_buffer_start[2]), "npu_input", NULL, inputsize); if (XMEDIA_CL_SUCCESS != ret) { goto ERROR; } //计算输出的大小并申请内存 for (i = 0; i < output.num; i++) { output_size[i] = output.tensor[i].size; outputsize += ALIGN_FUNC(output_size[i], ALIGN_BYTE); } ret = XMEDIA_API_SYS_MmzAlloc_Cached(&u64SrcPhyAddr[3], (void **)(&nocache_buffer_start[3]), "npu_output", NULL, outputsize); if (XMEDIA_CL_SUCCESS != ret) { goto ERROR; } //设置输入,把输入golden数据拷贝到输入地址,每个输入必须设置一次 for (i = 0; i < input.num; i++) { if (i > 0) { input.tensor[i].addr = input.tensor[i - 1].addr + ALIGN_FUNC(size, ALIGN_BYTE); } else { input.tensor[i].addr = nocache_buffer_start[2]; } size = input.tensor[i].size; memset(input.tensor[i].addr, 0, size); //清0防止运行拷贝模式对非拷贝模式的影响 memset(name, 0, sizeof(name)); sprintf(name, "data/input_data%d_%d.bin", k, i); ret = copy_file_to_buff(name, input.tensor[i].addr, 0, size); if (ret != XMEDIA_CL_SUCCESS) { printf("read input_data%d.bin failed!\n", i); goto ERROR; } printf("input.tensor[%d].addr = %p input size = 0x%x\n", i, input.tensor[i].addr, size); } //把输出的golden数据拷贝到内存,用来与npu的输出做对比 for (i = 0; i < output.num; i++) { if (i > 0) { output.tensor[i].addr = output.tensor[i - 1].addr + ALIGN_FUNC(output_size[i - 1], ALIGN_BYTE); } else { output.tensor[i].addr = nocache_buffer_start[3]; } memset(name, 0, sizeof(name)); output_size[i] = output.tensor[i].size; buff[i] = malloc(output_size[i]); if (buff[i] == NULL) { printf("malloc err\n"); ret = XMEDIA_CL_OUT_OF_HOST_MEMORY; goto ERROR; } sprintf(name, "data/output_data%d_%d.bin", k, i); ret = copy_file_to_buff(name, buff[i], 0, output_size[i]); if (ret != XMEDIA_CL_SUCCESS) { printf("read output_data%d.bin failed!\n", i); goto ERROR; } memset(output.tensor[i].addr, 0, output_size[i]); printf("output.tensor[%d].addr = %p output size = 0x%x\n", i, output.tensor[i].addr, output_size[i]); } //在xmm2下,用户需要申请输入地址,输出地址可以动态改变,把输入与输出设置为推理的输入、输出 ret = xmedia_cl_graph_set_inout(graph, &input, &output); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_set_inout, errno %d\n", ret); goto ERROR; } printf("xmedia_cl_graph_pocess\n"); for (j = 0; j < 2; j++) { printf("xmedia_cl_graph_pocess:%d, model:%s\n", j, model); //执行推理 struct timeval t_start, t_end; gettimeofday(&t_start, NULL); ret = xmedia_cl_graph_process(graph); gettimeofday(&t_end, NULL); printf("xmedia_cl_graph_process start time = [%ld]\n", t_start.tv_usec); printf("xmedia_cl_graph_process end time = [%ld]\n", t_end.tv_usec); printf("xmedia_cl_graph_process timeval = [%ld]\n",((t_end.tv_sec-t_start.tv_sec)*1000000) + t_end.tv_usec-t_start.tv_usec); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_process err, errno %d\n", ret); break; } for (i = 0; i < output.num; i++) { printf("#################################\n"); array_cmp_fixed(output.tensor[i].addr, buff[i], output.tensor[i].shape.type, output_size[i], (char *)__FILE__, 0); printf("#################################\n"); } } ERROR: //释放资源 free_inout_tensor_mem(input); free_inout_tensor_mem(output); if (nocache_buffer_start[2] != NULL) { XMEDIA_API_SYS_MmzFree(u64SrcPhyAddr[2], nocache_buffer_start[2]); } if (nocache_buffer_start[3] != NULL) { XMEDIA_API_SYS_MmzFree(u64SrcPhyAddr[3], nocache_buffer_start[3]); } if (nocache_buffer_start[1] != NULL) { XMEDIA_API_SYS_MmzFree(u64SrcPhyAddr[1], nocache_buffer_start[1]); } for (i = 0; i < output.num; i++) { if (buff[i] != NULL) { free(buff[i]); } } if (graph != NULL) { ret = xmedia_cl_graph_unload(graph); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_graph_unload1 err, errno %d\n", ret); return ret; } } if (context != NULL) { printf("xmedia_cl_release_context1_start\n"); ret = xmedia_cl_release_context(context); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_release_context1 err, errno %d\n", ret); return ret; } printf("xmedia_cl_release_context1_end\n"); } if (devices != NULL) { ret = xmedia_cl_release_device_ids(devices, &num_devices); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_release_device_ids1 err, errno %d\n", ret); return ret; } free(devices); } ret = xmedia_cl_uninit(); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_uninit1 err, errno %d\n", ret); return ret; } return 0; } int main() { pthread_t tid[3]; ptheadargs_t args[3]; xmedia_cl_s32 ret=0; xmedia_cl_u32 num_devices = 0; xmedia_cl_s32 err_code = 0; xmedia_cl_context context = NULL; xmedia_cl_device_id *devices = NULL; xmedia_cl_u32 worksize=0, weightsize=0; xmedia_cl_u32 worksize_1=0, weightsize_1=0; xmedia_cl_u32 worksize_2=0, weightsize_2=0; xmedia_u64 u64SrcPhyAddr[1] = {0}; xmedia_char *nocache_buffer_start[1] = {0}; //公共模块初始化 ret = xmedia_sys_init(XMEDIA_NULL); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_sys_init err, errno %d\n", ret); return ret; } //运行时初始化 ret = xmedia_cl_init(); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_init err, errno %d\n", ret); goto SYS_EXIT; } //第一次调用该接口获取设备数量 ret = xmedia_cl_get_device_ids(XMEDIA_CL_DEVICE_ALL, NULL, &num_devices); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_get_device_ids err, errno %d\n", ret); goto ERROR; } devices = (xmedia_cl_device_id *)calloc(num_devices, sizeof(xmedia_cl_device_id)); if (devices == NULL) { printf("calloc err\n"); ret = XMEDIA_CL_OUT_OF_HOST_MEMORY; goto ERROR; } //第二次调用该接口传递获取到的设备数量申请设备资源 ret = xmedia_cl_get_device_ids(XMEDIA_CL_DEVICE_ALL, devices, &num_devices); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_get_device_ids err, errno %d\n", ret); free(devices); xmedia_cl_uninit(); xmedia_sys_exit(); return ret; } //创建资源管理的对象 context = xmedia_cl_create_context(num_devices, devices, &err_code); if (err_code != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_create_context err, errno %d\n", ret); goto ERROR; } xmedia_cl_graph_querysize_from_file("data/gnn0.bin", &worksize, &weightsize); xmedia_cl_graph_querysize_from_file("data/gnn1.bin", &worksize_1, &weightsize_1); xmedia_cl_graph_querysize_from_file("data/gnn2.bin", &worksize_2, &weightsize_2); worksize = worksize > worksize_1 ? worksize : worksize_1; worksize = worksize > worksize_2 ? worksize : worksize_2; ret = XMEDIA_API_SYS_MmzAlloc_Cached(&u64SrcPhyAddr[0], (void **)(&nocache_buffer_start[0]), "npu_workspace", NULL, worksize); if (XMEDIA_CL_SUCCESS != ret) { goto ERROR; } ret = xmedia_cl_set_workspace_addr(context, (void *)(nocache_buffer_start[0]), worksize); if (XMEDIA_CL_SUCCESS != ret) { printf("xmedia_cl_set_workspace_addr err, errno %d\n", ret); goto ERROR; } args[0].i = 0; ret = pthread_create(&tid[0], NULL,(void *)load_model_file_withmem_workspace, (void *)&args[0]); if(ret) { printf("create pthread wait_job19 [%d] err\n", ret); } args[1].i = 1; ret = pthread_create(&tid[1], NULL,(void *)load_model_file_withmem_workspace, (void *)&args[1]); if(ret) { printf("create pthread wait_job20 [%d] err\n", ret); } args[2].i = 2; ret = pthread_create(&tid[2], NULL,(void *)load_model_file_withmem_workspace, (void *)&args[2]); if(ret) { printf("create pthread wait_job21 [%d] err\n", ret); } ret = pthread_join(tid[0], NULL); if(ret) { printf("pthread join0 [%d] err\n", ret); } ret = pthread_join(tid[1], NULL); if(ret) { printf("pthread join01 [%d] err\n", ret); } ret = pthread_join(tid[2], NULL); if(ret) { printf("pthread join02 [%d] err\n", ret); } ERROR: if (nocache_buffer_start[0] != NULL) { XMEDIA_API_SYS_MmzFree(u64SrcPhyAddr[0], nocache_buffer_start[0]); } if (context != NULL) { ret = xmedia_cl_release_context(context); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_release_context2 err, errno %d\n", ret); return ret; } } if (devices != NULL) { ret = xmedia_cl_release_device_ids(devices, &num_devices); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_release_device_ids err, errno %d\n", ret); return ret; } free(devices); } ret = xmedia_cl_uninit(); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_cl_uninit err, errno %d\n", ret); return ret; } SYS_EXIT: ret = xmedia_sys_exit(); if (ret != XMEDIA_CL_SUCCESS) { printf("xmedia_sys_exit err, errno %d\n", ret); return ret; } return ret; }