mkl-dnn make lenet convolution slower than c code
hi, My recent work is learning the lenet, and implement it in c code on linux platform with icc compiler. Then i use mkl to optimize it ,it seems very good after i replace the convolution model. At last ,i try to use mkl-dnn to optimize it ,but it seems slower than mkl optimization ,even slower than c code when i replace the convolution model.
i use the latest mkl-dnn git version. and my computer is i5 processor. compiler is icc or icpc.
And i use MKLDNN_VERBOSE=2 to see the infomation as follows:
mkldnn_verbose,create,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic1oc20_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,0.000976562 mkldnn_verbose,exec,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic1oc20_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,5.84204 mkldnn_verbose,create,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic20oc50_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,0.000976562 mkldnn_verbose,exec,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic20oc50_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,29.741 mkldnn_verbose,create,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic50oc500_ih4oh1kh4sh1dh0ph0_iw4ow1kw4sw1dw0pw0,0.00195312 mkldnn_verbose,exec,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic50oc500_ih4oh1kh4sh1dh0ph0_iw4ow1kw4sw1dw0pw0,7.43506 mkldnn_verbose,create,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic500oc10_ih1oh1kh1sh1dh0ph0_iw1ow1kw1sw1dw0pw0,0.00219727 mkldnn_verbose,exec,convolution,ref:any,forward_training,fsrc:nchw fwei:nchw fbia:x fdst:nchw,alg:convolution_direct,mb1_ic500oc10_ih1oh1kh1sh1dh0ph0_iw1ow1kw1sw1dw0pw0,0.139893
i dont know what is the mistake i make, can you give me some advices? thanks a lot.
i use -mkl=parallel in Makefile to call mkl cblas_xx interfaces. and i use -lmkldnn to call mkldnn interfaces.
int32 CONV_forward(BLOB_S_T *p_stru_src, BLOB_S_T *p_stru_dest, BLOB_S_T *p_stru_kernel, BLOB_S_T *p_stru_bias, CONV_PARA * p_stru_conv_para, bool is_1x1) { mkldnn_engine_t engine;
//create engine
CHECK(mkldnn_engine_create(&engine, mkldnn_cpu, 0 /* idx */));
mkldnn_primitive_t conv_user_src_memory, conv_user_weights_memory,
conv_user_bias_memory;
/* create memory for user data */
init_data_memory(4, &p_stru_src->stru_shape.u32_num, mkldnn_nchw, mkldnn_f32, engine,
(fp32 *)p_stru_src->p_data,
&conv_user_src_memory);
init_data_memory(4, &p_stru_kernel->stru_shape.u32_num, mkldnn_oihw, mkldnn_f32, engine,
(fp32 *)p_stru_kernel->p_data,
&conv_user_weights_memory);
init_data_memory(1, &p_stru_bias->stru_shape.u32_ch, mkldnn_x, mkldnn_f32, engine,
(fp32 *)p_stru_bias->p_data,
&conv_user_bias_memory);
mkldnn_memory_desc_t conv_src_md, conv_weights_md, conv_bias_md,
conv_dst_md;
/* create data descriptors for convolution w/ no specified format */
CHECK(mkldnn_memory_desc_init(&conv_src_md, 4, &p_stru_src->stru_shape.u32_num,
mkldnn_f32, mkldnn_nchw));
CHECK(mkldnn_memory_desc_init(&conv_weights_md, 4, &p_stru_kernel->stru_shape.u32_num,
mkldnn_f32, mkldnn_nchw));
CHECK(mkldnn_memory_desc_init(&conv_bias_md, 1, &p_stru_bias->stru_shape.u32_ch,
mkldnn_f32, mkldnn_x));
CHECK(mkldnn_memory_desc_init(&conv_dst_md, 4, &p_stru_dest->stru_shape.u32_num,
mkldnn_f32, mkldnn_nchw));
PRINT(LOG_DBG, "src ndim %d, nchw %d %d %d %d\n",
conv_src_md.ndims,
conv_src_md.dims[0],
conv_src_md.dims[1],
conv_src_md.dims[2],
conv_src_md.dims[3]);
PRINT(LOG_DBG, "weight ndim %d, nchw %d %d %d %d\n",
conv_weights_md.ndims,
conv_weights_md.dims[0],
conv_weights_md.dims[1],
conv_weights_md.dims[2],
conv_weights_md.dims[3]);
PRINT(LOG_DBG, "bias ndim %d, nchw %d %d %d %d\n",
conv_bias_md.ndims,
conv_bias_md.dims[0],
conv_bias_md.dims[1],
conv_bias_md.dims[2],
conv_bias_md.dims[3]);
PRINT(LOG_DBG, "dst ndim %d, nchw %d %d %d %d\n",
conv_dst_md.ndims,
conv_dst_md.dims[0],
conv_dst_md.dims[1],
conv_dst_md.dims[2],
conv_dst_md.dims[3]);
/* create a convolution */
mkldnn_convolution_desc_t conv_any_desc;
CHECK(mkldnn_convolution_forward_desc_init(&conv_any_desc, mkldnn_forward_inference,
mkldnn_convolution_direct, &conv_src_md, &conv_weights_md,
&conv_bias_md, &conv_dst_md,
&p_stru_conv_para->stru_stride.u32_height,
&p_stru_src->stru_pad.u32_height,
&p_stru_src->stru_pad.u32_height,
mkldnn_padding_zero));
mkldnn_primitive_desc_t conv_pd;
CHECK(mkldnn_primitive_desc_create(&conv_pd, &conv_any_desc,
engine, NULL));
/* create memory for dst data, we don't need reorder it to user data */
const_mkldnn_primitive_desc_t dst_pd
= mkldnn_primitive_desc_query_pd(conv_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t conv_internal_dst_memory;
CHECK(mkldnn_primitive_create(
&conv_internal_dst_memory, dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
conv_internal_dst_memory,
p_stru_dest->p_data));
mkldnn_primitive_at_t conv_srcs[] = {
mkldnn_primitive_at(conv_user_src_memory, 0),
mkldnn_primitive_at(conv_user_weights_memory, 0),
mkldnn_primitive_at(conv_user_bias_memory, 0)
};
const_mkldnn_primitive_t conv_dsts[] = { conv_internal_dst_memory };
/* finally create a convolution primitive */
mkldnn_primitive_t conv;
CHECK(mkldnn_primitive_create(&conv, conv_pd, conv_srcs, conv_dsts));
/* build a simple net */
uint32_t n = 0;
mkldnn_primitive_t net[10];
net[n++] = conv;
mkldnn_stream_t stream;
CHECK(mkldnn_stream_create(&stream, mkldnn_eager));
CHECK(mkldnn_stream_submit(stream, n, net, NULL));
CHECK(mkldnn_stream_wait(stream, n, NULL));
/* clean-up */
CHECK(mkldnn_primitive_desc_destroy(conv_pd));
mkldnn_stream_destroy(stream);
mkldnn_primitive_destroy(conv_user_src_memory);
mkldnn_primitive_destroy(conv_user_weights_memory);
mkldnn_primitive_destroy(conv_user_bias_memory);
mkldnn_primitive_destroy(conv_internal_dst_memory);
mkldnn_primitive_destroy(conv);
mkldnn_engine_destroy(engine);
return 0;
}
Hi @arthurhi,
There are few issues with the code you sent.
- Incorrect format for weights:
CHECK(mkldnn_memory_desc_init(&conv_weights_md, 4, &p_stru_kernel->stru_shape.u32_num,
mkldnn_f32, mkldnn_nchw)); // should be mkldnn_oihw
Here you use nchw, but the format for weights should be oihw. Even though the layout is the same for these two format, the gemm-based implementation isn't picked if the format doesn't match oihw. This is a known issue and will be fixed in the upcoming 1.0 version of the library.
- Hard-coded memory formats for convolutions.
As it is stated in the documentation convolutions are performance critical primitives and for the best results a user should create them with format any which allows convolution implementations to pick the best memory format. Alas this would require inserting reorder between user data format and the ones the convolutions picked.
Few examples can be found here: C example and C++ example. Both of them are based on AlexNet topology. C++ example implements almost the whole topology, while C example implements only 4 layers (due to the verbosity of C API). You can pick one of them and adjust the sizes to LeNet to check the performance.
You also can use benchdnn, to check what to expect from convolutions in LeNet.
Finally you can use one of the multiple frameworks that already have an integration with Intel MKL-DNN (like OpenVINO, Intel Caffe, MxNet, TF, etc...) to estimate the performance.
thanks a lot. now i fix the bug which you adviced. and it can works better than c code. but it is slower than -mkl=parallel. is it the fact? have you compared with mkl? now my code is like this.
int32 CONV_forward(BLOB_S_T *p_stru_src, BLOB_S_T *p_stru_dest, BLOB_S_T *p_stru_kernel, BLOB_S_T *p_stru_bias, IMOTION_CONV_PARA * p_stru_conv_para, bool is_1x1) { mkldnn_engine_t engine;
//create engine
CHECK(mkldnn_engine_create(&engine, mkldnn_cpu, 0 /* idx */));
mkldnn_primitive_t conv_user_src_memory, conv_user_weights_memory,
conv_user_bias_memory, conv_user_dst_memory;
/* create memory for user data */
init_data_memory(4, &p_stru_src->stru_shape.u32_num, mkldnn_nchw, mkldnn_f32, engine,
(fp32 *)p_stru_src->p_data,
&conv_user_src_memory);
init_data_memory(4, &p_stru_kernel->stru_shape.u32_num, mkldnn_oihw, mkldnn_f32, engine,
(fp32 *)p_stru_kernel->p_data,
&conv_user_weights_memory);
init_data_memory(1, &p_stru_bias->stru_shape.u32_ch, mkldnn_x, mkldnn_f32, engine,
(fp32 *)p_stru_bias->p_data,
&conv_user_bias_memory);
init_data_memory(4, &p_stru_dest->stru_shape.u32_num, mkldnn_nchw, mkldnn_f32, engine,
(fp32 *)p_stru_dest->p_data,
&conv_user_dst_memory);
mkldnn_memory_desc_t conv_src_md, conv_weights_md, conv_bias_md,
conv_dst_md;
/* create data descriptors for convolution w/ no specified format */
CHECK(mkldnn_memory_desc_init(&conv_src_md, 4, &p_stru_src->stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&conv_weights_md, 4, &p_stru_kernel->stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&conv_bias_md, 1, &p_stru_bias->stru_shape.u32_ch,
mkldnn_f32, mkldnn_x));
CHECK(mkldnn_memory_desc_init(&conv_dst_md, 4, &p_stru_dest->stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
/* create a convolution */
mkldnn_convolution_desc_t conv_any_desc;
CHECK(mkldnn_convolution_forward_desc_init(&conv_any_desc, mkldnn_forward_inference,
mkldnn_convolution_direct, &conv_src_md, &conv_weights_md,
&conv_bias_md, &conv_dst_md,
&p_stru_conv_para->stru_stride.u32_height,
&p_stru_src->stru_pad.u32_height,
&p_stru_src->stru_pad.u32_height,
mkldnn_padding_zero));
mkldnn_primitive_desc_t conv_pd;
CHECK(mkldnn_primitive_desc_create(&conv_pd, &conv_any_desc,
engine, NULL));
mkldnn_primitive_t conv_reorder_src = NULL;
const_mkldnn_primitive_desc_t src_pd = mkldnn_primitive_desc_query_pd(
conv_pd, mkldnn_query_src_pd, 0);
const_mkldnn_primitive_desc_t user_memory_src_pd;
CHECK(mkldnn_primitive_get_primitive_desc(conv_user_src_memory, &user_memory_src_pd));
bool src_equal = mkldnn_memory_primitive_desc_equal(src_pd,user_memory_src_pd);
mkldnn_primitive_t conv_internal_src_memory = NULL;
uint32 conv_src_size = 0;
fp32 *conv_src_buffer = NULL;
if (!src_equal) {
conv_src_size = mkldnn_memory_primitive_desc_get_size(src_pd);
conv_src_buffer = (fp32 *)malloc(conv_src_size);
CHECK(prepare_reorder(&conv_user_src_memory, &src_pd, 1,
&conv_internal_src_memory, &conv_reorder_src, conv_src_buffer));
}
mkldnn_primitive_t conv_reorder_weights = NULL;
const_mkldnn_primitive_desc_t weights_pd = mkldnn_primitive_desc_query_pd(
conv_pd, mkldnn_query_weights_pd, 0);
const_mkldnn_primitive_desc_t user_memory_weights_pd;
CHECK(mkldnn_primitive_get_primitive_desc(conv_user_weights_memory, &user_memory_weights_pd));
bool weights_equal = mkldnn_memory_primitive_desc_equal(weights_pd,user_memory_weights_pd);
mkldnn_primitive_t conv_internal_weights_memory = NULL;
uint32 conv_weights_size = 0;
fp32 *conv_weights_buffer = NULL;
if (!weights_equal) {
conv_weights_size = mkldnn_memory_primitive_desc_get_size(weights_pd);
conv_weights_buffer = (fp32 *)malloc(conv_weights_size);
CHECK(prepare_reorder(&conv_user_weights_memory, &weights_pd, 1,
&conv_internal_weights_memory, &conv_reorder_weights, conv_weights_buffer));
}
/* create memory for dst data*/
mkldnn_primitive_t conv_reorder_dst = NULL;
const_mkldnn_primitive_desc_t dst_pd
= mkldnn_primitive_desc_query_pd(conv_pd, mkldnn_query_dst_pd, 0);
const_mkldnn_primitive_desc_t user_memory_dst_pd;
CHECK(mkldnn_primitive_get_primitive_desc(conv_user_dst_memory, &user_memory_dst_pd));
bool dst_equal = mkldnn_memory_primitive_desc_equal(dst_pd,user_memory_dst_pd);
mkldnn_primitive_t conv_internal_dst_memory = NULL;
uint32 conv_dst_size = 0;
fp32 *conv_dst_buffer = NULL;
if (!dst_equal) {
conv_dst_size = mkldnn_memory_primitive_desc_get_size(dst_pd);
conv_dst_buffer = (fp32 *)malloc(conv_dst_size);
CHECK(prepare_reorder(&conv_user_dst_memory, &dst_pd, 0,
&conv_internal_dst_memory, &conv_reorder_dst, conv_dst_buffer));
}
mkldnn_primitive_at_t conv_srcs[] = {
mkldnn_primitive_at(conv_internal_src_memory ? conv_internal_src_memory : conv_user_src_memory, 0),
mkldnn_primitive_at(conv_internal_weights_memory ? conv_internal_weights_memory : conv_user_weights_memory, 0),
mkldnn_primitive_at(conv_user_bias_memory, 0)
};
const_mkldnn_primitive_t conv_dsts[] = { conv_internal_dst_memory ? conv_internal_dst_memory : conv_user_dst_memory };
/* finally create a convolution primitive */
mkldnn_primitive_t conv;
CHECK(mkldnn_primitive_create(&conv, conv_pd, conv_srcs, conv_dsts));
/* build a simple net */
uint32_t n = 0;
mkldnn_primitive_t net[10];
if (conv_reorder_src) {
net[n++] = conv_reorder_src;
}
if (conv_reorder_weights) {
net[n++] = conv_reorder_weights;
}
net[n++] = conv;
if (conv_reorder_dst) {
net[n++] = conv_reorder_dst;
}
mkldnn_stream_t stream;
CHECK(mkldnn_stream_create(&stream, mkldnn_eager));
CHECK(mkldnn_stream_submit(stream, n, net, NULL));
CHECK(mkldnn_stream_wait(stream, n, NULL));
/* clean-up */
CHECK(mkldnn_primitive_desc_destroy(conv_pd));
mkldnn_stream_destroy(stream);
mkldnn_primitive_destroy(conv_user_src_memory);
mkldnn_primitive_destroy(conv_user_weights_memory);
mkldnn_primitive_destroy(conv_user_bias_memory);
mkldnn_primitive_destroy(conv_user_dst_memory);
mkldnn_primitive_destroy(conv_internal_src_memory);
mkldnn_primitive_destroy(conv_internal_weights_memory);
mkldnn_primitive_destroy(conv_internal_dst_memory);
mkldnn_primitive_destroy(conv_reorder_src);
mkldnn_primitive_destroy(conv_reorder_weights);
mkldnn_primitive_destroy(conv_reorder_dst);
mkldnn_primitive_destroy(conv);
mkldnn_engine_destroy(engine);
if (conv_src_buffer) {
free(conv_src_buffer);
conv_src_buffer = NULL;
}
if (conv_weights_buffer) {
free(conv_weights_buffer);
conv_weights_buffer = NULL;
}
if (conv_dst_buffer) {
free(conv_dst_buffer);
conv_dst_buffer = NULL;
}
return 0;
}
I am not sure I fully understand what you are comparing the code against when you say -mkl=parallel. Is it you are running the gemm-based implementation using cblas_sgemm implemented on your own?
There are three potential issues with the code you share:
- Memory allocations on the fly
To perform reorders you allocate temporary buffers (like conv_internal_src_memory). That's inevitable, but the problem is that you allocate and deallocate the memory for every convolution run. Once memory allocated by OS it might take significant time to initialize it before a user can actually use it. So I would suggest cache the memory and see whether that improves the performance.
-
Weights reorder should happen only once. On the next iteration you can reuse already reordered weights. For the given small problem the weights reorder might take significant fraction of time.
-
The layout should be propagated between the layers. Currently the pattern is:
dat_1 -> reorder -> dat_1_r -> conv -> dat_2_r -> reorder -> dat_2 -> reorder -> dat_2_r -> conv -> ...
Notice that dat_2_r -> reorder ->dat_2 -> reorder -> dat_2_r might be redundant if the formats of dat_2_r on the left and on the right are the same. Even though Intel MKL-DNN doesn't guarantee they would be the same, in most of the cases they actually are. That's how we try avoiding unnecessary conversions.
So the ideal workflow should look like:
reorder all weights
for all layers in a net {
if (this layer is convolution) {
query the input format for the convolution
if (current input format != queried format) { // [1]
reorder input data
}
take input in Intel MKL-DNN format
execute convolution
get output in Intel MKL-DNN format
} else {
execute a primitive on the format that came from the previous layer
}
}
if (current format != user's output format)
reorder data to user's format
As I said above, in most of the case [1] should almost never happen.
You can also run your current example with MKLDNN_VERBOSE=2 to check how much time is spent in convolutions and reorders.
OpenVINO: link IntelCaffe: link Other frameworks are listed here.
thanks a lot. and i try it in my new code. int32 LENET_inference_mkldnn(LENET_HANDLE *p_lenet) { LENET_CONV_CONFIG *p_stru_conv_config = NULL; mkldnn_engine_t engine;
//create engine
CHECK(mkldnn_engine_create(&engine, mkldnn_cpu, 0 /* idx */));
/conv1*******/ p_stru_conv_config = (LENET_CONV_CONFIG*)p_lenet->stru_conv1.p_param; mkldnn_primitive_t conv1_user_src_memory, conv1_user_weights_memory, conv1_user_bias_memory; /* create memory for user data */ init_data_memory(4, &p_lenet->stru_src.stru_shape.u32_num, mkldnn_nchw, mkldnn_f32, engine, (fp32 *)p_lenet->p_u8_data0, &conv1_user_src_memory); init_data_memory(4, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_oihw, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_weight.p_data, &conv1_user_weights_memory); init_data_memory(1, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_x, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_bias.p_data, &conv1_user_bias_memory);
mkldnn_memory_desc_t conv1_src_md, conv1_weights_md, conv1_bias_md,
conv1_dst_md;
/* create data descriptors for convolution w/ no specified format */
CHECK(mkldnn_memory_desc_init(&conv1_src_md, 4, &p_lenet->stru_src.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&conv1_weights_md, 4, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&conv1_bias_md, 1, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_x));
CHECK(mkldnn_memory_desc_init(&conv1_dst_md, 4, &p_lenet->stru_conv1.stru_top.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
/* create a convolution */
mkldnn_convolution_desc_t conv1_any_desc;
CHECK(mkldnn_convolution_forward_desc_init(&conv1_any_desc, mkldnn_forward_inference,
mkldnn_convolution_auto, &conv1_src_md, &conv1_weights_md,
&conv1_bias_md, &conv1_dst_md,
&p_stru_conv_config->stru_stride.u32_height,
&p_lenet->stru_src.stru_pad.u32_height,
&p_lenet->stru_src.stru_pad.u32_height,
mkldnn_padding_zero));
mkldnn_primitive_desc_t conv1_pd;
CHECK(mkldnn_primitive_desc_create(&conv1_pd, &conv1_any_desc,
engine, NULL));
mkldnn_primitive_t conv1_reorder_src = NULL;
const_mkldnn_primitive_desc_t conv1_src_pd = mkldnn_primitive_desc_query_pd(
conv1_pd, mkldnn_query_src_pd, 0);
const_mkldnn_primitive_desc_t conv1_user_memory_src_pd;
CHECK(mkldnn_primitive_get_primitive_desc(conv1_user_src_memory, &conv1_user_memory_src_pd));
bool conv1_src_equal = mkldnn_memory_primitive_desc_equal(conv1_src_pd,conv1_user_memory_src_pd);
mkldnn_primitive_t conv1_internal_src_memory = NULL;
uint32 conv1_src_size = 0;
fp32 *conv1_src_buffer = NULL;
if (!conv1_src_equal) {
conv1_src_size = mkldnn_memory_primitive_desc_get_size(conv1_src_pd);
conv1_src_buffer = (fp32 *)imotion_malloc(conv1_src_size);
CHECK(prepare_reorder(&conv1_user_src_memory, &conv1_src_pd, 1,
&conv1_internal_src_memory, &conv1_reorder_src, conv1_src_buffer));
}
mkldnn_primitive_t conv1_reorder_weights = NULL;
const_mkldnn_primitive_desc_t conv1_weights_pd = mkldnn_primitive_desc_query_pd(
conv1_pd, mkldnn_query_weights_pd, 0);
const_mkldnn_primitive_desc_t conv1_user_memory_weights_pd;
CHECK(mkldnn_primitive_get_primitive_desc(conv1_user_weights_memory, &conv1_user_memory_weights_pd));
bool conv1_weights_equal = mkldnn_memory_primitive_desc_equal(conv1_weights_pd,conv1_user_memory_weights_pd);
mkldnn_primitive_t conv1_internal_weights_memory = NULL;
uint32 conv1_weights_size = 0;
fp32 *conv1_weights_buffer = NULL;
if (!conv1_weights_equal) {
conv1_weights_size = mkldnn_memory_primitive_desc_get_size(conv1_weights_pd);
conv1_weights_buffer = (fp32 *)imotion_malloc(conv1_weights_size);
CHECK(prepare_reorder(&conv1_user_weights_memory, &conv1_weights_pd, 1,
&conv1_internal_weights_memory, &conv1_reorder_weights, conv1_weights_buffer));
}
/* create memory for dst data*/
const_mkldnn_primitive_desc_t conv1_dst_pd
= mkldnn_primitive_desc_query_pd(conv1_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t conv1_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&conv1_internal_dst_memory, conv1_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
conv1_internal_dst_memory, p_lenet->p_u8_data1));
mkldnn_primitive_at_t conv1_srcs[] = {
mkldnn_primitive_at(conv1_internal_src_memory ? conv1_internal_src_memory : conv1_user_src_memory, 0),
mkldnn_primitive_at(conv1_internal_weights_memory ? conv1_internal_weights_memory : conv1_user_weights_memory, 0),
mkldnn_primitive_at(conv1_user_bias_memory, 0)
};
const_mkldnn_primitive_t conv1_dsts[] = { conv1_internal_dst_memory};
/* finally create a convolution primitive */
mkldnn_primitive_t conv1;
CHECK(mkldnn_primitive_create(&conv1, conv1_pd, conv1_srcs, conv1_dsts));
/conv1 end***/
/pool1*******/ uint32 pool1_padding[2] = {0,0}; POOLING_PARAM_S_T p_stru_pool_config = (POOLING_PARAM_S_T)p_lenet->stru_pool1.p_param;
/* create pooling memory descriptor on dst descriptor
* from previous primitive */
const mkldnn_memory_desc_t *pool1_src_md =
mkldnn_primitive_desc_query_memory_d(conv1_dst_pd);
/* create descriptors for dst pooling data */
mkldnn_memory_desc_t pool1_dst_md;
CHECK(mkldnn_memory_desc_init(
&pool1_dst_md, 4, &p_lenet->stru_pool1.stru_top.stru_shape.u32_num, mkldnn_f32, mkldnn_any));;
/* create a pooling */
mkldnn_pooling_desc_t pool1_desc;
CHECK(mkldnn_pooling_forward_desc_init(&pool1_desc, mkldnn_forward_inference,
mkldnn_pooling_max, pool1_src_md, &pool1_dst_md,
&p_stru_pool_config->stru_kernel.stru_stride.u32_height,
&p_stru_pool_config->stru_kernel.stru_kernel_size.u32_height,
pool1_padding, pool1_padding, mkldnn_padding_zero));
mkldnn_primitive_desc_t pool1_pd;
CHECK(mkldnn_primitive_desc_create(&pool1_pd, &pool1_desc, engine, NULL));
const_mkldnn_primitive_desc_t pool1_dst_pd
= mkldnn_primitive_desc_query_pd(pool1_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t pool1_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&pool1_internal_dst_memory, pool1_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
pool1_internal_dst_memory, p_lenet->p_u8_data0));
mkldnn_primitive_at_t pool1_srcs = { conv1_internal_dst_memory, 0 };
const_mkldnn_primitive_t pool1_dsts[] = { pool1_internal_dst_memory, 0};
/* finally create a pooling primitive */
mkldnn_primitive_t pool1;
CHECK(mkldnn_primitive_create(&pool1, pool1_pd, &pool1_srcs, pool1_dsts));
/pool1 end***/
/conv2*******/ p_stru_conv_config = (LENET_CONV_CONFIG*)p_lenet->stru_conv2.p_param; mkldnn_primitive_t conv2_user_weights_memory, conv2_user_bias_memory; /* create memory for user data */ init_data_memory(4, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_oihw, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_weight.p_data, &conv2_user_weights_memory); init_data_memory(1, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_x, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_bias.p_data, &conv2_user_bias_memory);
mkldnn_memory_desc_t conv2_src_md, conv2_weights_md, conv2_bias_md,
conv2_dst_md;
/* create data descriptors for convolution w/ no specified format */
CHECK(mkldnn_memory_desc_init(&conv2_src_md, 4, &p_lenet->stru_conv2.stru_bottom.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&conv2_weights_md, 4, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&conv2_bias_md, 1, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_x));
CHECK(mkldnn_memory_desc_init(&conv2_dst_md, 4, &p_lenet->stru_conv2.stru_top.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
/* create a convolution */
mkldnn_convolution_desc_t conv2_any_desc;
CHECK(mkldnn_convolution_forward_desc_init(&conv2_any_desc, mkldnn_forward_inference,
mkldnn_convolution_auto, &conv2_src_md, &conv2_weights_md,
&conv2_bias_md, &conv2_dst_md,
&p_stru_conv_config->stru_stride.u32_height,
&p_lenet->stru_conv2.stru_bottom.stru_pad.u32_height,
&p_lenet->stru_conv2.stru_bottom.stru_pad.u32_height,
mkldnn_padding_zero));
mkldnn_primitive_desc_t conv2_pd;
CHECK(mkldnn_primitive_desc_create(&conv2_pd, &conv2_any_desc,
engine, NULL));
mkldnn_primitive_t conv2_reorder_src = NULL;
const_mkldnn_primitive_desc_t conv2_src_pd = mkldnn_primitive_desc_query_pd(
conv2_pd, mkldnn_query_src_pd, 0);
const_mkldnn_primitive_desc_t conv2_user_memory_src_pd;
CHECK(mkldnn_primitive_get_primitive_desc(pool1_internal_dst_memory, &conv2_user_memory_src_pd));
bool conv2_src_equal = mkldnn_memory_primitive_desc_equal(conv2_src_pd,conv2_user_memory_src_pd);
mkldnn_primitive_t conv2_internal_src_memory = NULL;
uint32 conv2_src_size = 0;
fp32 *conv2_src_buffer = NULL;
if (!conv2_src_equal) {
conv2_src_size = mkldnn_memory_primitive_desc_get_size(conv2_src_pd);
conv2_src_buffer = (fp32 *)imotion_malloc(conv2_src_size);
CHECK(prepare_reorder(&pool1_internal_dst_memory, &conv2_src_pd, 1,
&conv2_internal_src_memory, &conv2_reorder_src, conv2_src_buffer));
}
mkldnn_primitive_t conv2_reorder_weights = NULL;
const_mkldnn_primitive_desc_t conv2_weights_pd = mkldnn_primitive_desc_query_pd(
conv2_pd, mkldnn_query_weights_pd, 0);
const_mkldnn_primitive_desc_t conv2_user_memory_weights_pd;
CHECK(mkldnn_primitive_get_primitive_desc(conv2_user_weights_memory, &conv2_user_memory_weights_pd));
bool conv2_weights_equal = mkldnn_memory_primitive_desc_equal(conv2_weights_pd,conv2_user_memory_weights_pd);
mkldnn_primitive_t conv2_internal_weights_memory = NULL;
uint32 conv2_weights_size = 0;
fp32 *conv2_weights_buffer = NULL;
if (!conv2_weights_equal) {
conv2_weights_size = mkldnn_memory_primitive_desc_get_size(conv2_weights_pd);
conv2_weights_buffer = (fp32 *)imotion_malloc(conv2_weights_size);
CHECK(prepare_reorder(&conv2_user_weights_memory, &conv2_weights_pd, 1,
&conv2_internal_weights_memory, &conv2_reorder_weights, conv2_weights_buffer));
}
/* create memory for dst data*/
const_mkldnn_primitive_desc_t conv2_dst_pd
= mkldnn_primitive_desc_query_pd(conv2_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t conv2_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&conv2_internal_dst_memory, conv2_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
conv2_internal_dst_memory, p_lenet->p_u8_data1));
mkldnn_primitive_at_t conv2_srcs[] = {
mkldnn_primitive_at(conv2_internal_src_memory ? conv2_internal_src_memory : pool1_internal_dst_memory, 0),
mkldnn_primitive_at(conv2_internal_weights_memory ? conv2_internal_weights_memory : conv2_user_weights_memory, 0),
mkldnn_primitive_at(conv2_user_bias_memory, 0)
};
const_mkldnn_primitive_t conv2_dsts[] = { conv2_internal_dst_memory};
/* finally create a convolution primitive */
mkldnn_primitive_t conv2;
CHECK(mkldnn_primitive_create(&conv2, conv2_pd, conv2_srcs, conv2_dsts));
/conv2 end***/
/pool2*******/ p_stru_pool_config = (POOLING_PARAM_S_T*)p_lenet->stru_pool1.p_param;
/* create pooling memory descriptor on dst descriptor
* from previous primitive */
const mkldnn_memory_desc_t *pool2_src_md =
mkldnn_primitive_desc_query_memory_d(conv2_dst_pd);
/* create descriptors for dst pooling data */
mkldnn_memory_desc_t pool2_dst_md;
CHECK(mkldnn_memory_desc_init(
&pool2_dst_md, 4, &p_lenet->stru_pool2.stru_top.stru_shape.u32_num, mkldnn_f32, mkldnn_any));;
/* create a pooling */
mkldnn_pooling_desc_t pool2_desc;
CHECK(mkldnn_pooling_forward_desc_init(&pool2_desc, mkldnn_forward_inference,
mkldnn_pooling_max, pool2_src_md, &pool2_dst_md,
&p_stru_pool_config->stru_kernel.stru_stride.u32_height,
&p_stru_pool_config->stru_kernel.stru_kernel_size.u32_height,
pool1_padding, pool1_padding, mkldnn_padding_zero));
mkldnn_primitive_desc_t pool2_pd;
CHECK(mkldnn_primitive_desc_create(&pool2_pd, &pool2_desc, engine, NULL));
const_mkldnn_primitive_desc_t pool2_dst_pd
= mkldnn_primitive_desc_query_pd(pool2_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t pool2_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&pool2_internal_dst_memory, pool2_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
pool2_internal_dst_memory, p_lenet->p_u8_data0));
mkldnn_primitive_at_t pool2_srcs = { conv2_internal_dst_memory, 0 };
const_mkldnn_primitive_t pool2_dsts[] = { pool2_internal_dst_memory, 0};
/* finally create a pooling primitive */
mkldnn_primitive_t pool2;
CHECK(mkldnn_primitive_create(&pool2, pool2_pd, &pool2_srcs, pool2_dsts));
/pool2 end***/
/ip1*******/ p_stru_conv_config = (LENET_CONV_CONFIG*)p_lenet->stru_ip1.p_param; mkldnn_primitive_t ip1_user_weights_memory, ip1_user_bias_memory; /* create memory for user data */ init_data_memory(4, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_oihw, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_weight.p_data, &ip1_user_weights_memory); init_data_memory(1, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_x, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_bias.p_data, &ip1_user_bias_memory);
mkldnn_memory_desc_t ip1_src_md, ip1_weights_md, ip1_bias_md,
ip1_dst_md;
/* create data descriptors for convolution w/ no specified format */
CHECK(mkldnn_memory_desc_init(&ip1_src_md, 4, &p_lenet->stru_ip1.stru_bottom.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&ip1_weights_md, 4, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&ip1_bias_md, 1, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_x));
CHECK(mkldnn_memory_desc_init(&ip1_dst_md, 2, &p_lenet->stru_ip1.stru_top.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
/* create a convolution */
mkldnn_inner_product_desc_t ip1_any_desc;
CHECK(mkldnn_inner_product_forward_desc_init(&ip1_any_desc, mkldnn_forward_inference,
&ip1_src_md, &ip1_weights_md,
&ip1_bias_md, &ip1_dst_md));
#if 1
mkldnn_primitive_attr_t ip1_attr = NULL;
CHECK(mkldnn_primitive_attr_create(&ip1_attr));
//CHECK(mkldnn_primitive_attr_set_int_output_round_mode(ip1_attr, mkldnn_round_nearest));
mkldnn_post_ops_t ops;
CHECK(mkldnn_post_ops_create(&ops));
CHECK(mkldnn_post_ops_append_eltwise(ops, 1.0,mkldnn_eltwise_relu,
1.0, 0.0));
CHECK(mkldnn_primitive_attr_set_post_ops(ip1_attr, ops));
const_mkldnn_post_ops_t c_ops;
CHECK(mkldnn_primitive_attr_get_post_ops(ip1_attr, &c_ops));
int ret = mkldnn_post_ops_len(c_ops);
if (ret != 1) {
IMOTION_PRINT(LOG_ERROR, "[%s] config ip1 attr failed, ret = %d\n", __FUNCTION__, ret);
exit(2);
}
CHECK(mkldnn_post_ops_destroy(ops));
mkldnn_primitive_desc_t ip1_pd;
CHECK(mkldnn_primitive_desc_create_v2(&ip1_pd, &ip1_any_desc, ip1_attr,
engine, NULL));
#else
mkldnn_primitive_desc_t ip1_pd;
CHECK(mkldnn_primitive_desc_create(&ip1_pd, &ip1_any_desc,
engine, NULL));
#endif
mkldnn_primitive_t ip1_reorder_src = NULL;
const_mkldnn_primitive_desc_t ip1_src_pd = mkldnn_primitive_desc_query_pd(
ip1_pd, mkldnn_query_src_pd, 0);
const_mkldnn_primitive_desc_t ip1_user_memory_src_pd;
CHECK(mkldnn_primitive_get_primitive_desc(pool2_internal_dst_memory, &ip1_user_memory_src_pd));
bool ip1_src_equal = mkldnn_memory_primitive_desc_equal(ip1_src_pd,ip1_user_memory_src_pd);
mkldnn_primitive_t ip1_internal_src_memory = NULL;
uint32 ip1_src_size = 0;
fp32 *ip1_src_buffer = NULL;
if (!ip1_src_equal) {
ip1_src_size = mkldnn_memory_primitive_desc_get_size(ip1_src_pd);
ip1_src_buffer = (fp32 *)imotion_malloc(ip1_src_size);
CHECK(prepare_reorder(&pool2_internal_dst_memory, &ip1_src_pd, 1,
&ip1_internal_src_memory, &ip1_reorder_src, ip1_src_buffer));
}
mkldnn_primitive_t ip1_reorder_weights = NULL;
const_mkldnn_primitive_desc_t ip1_weights_pd = mkldnn_primitive_desc_query_pd(
ip1_pd, mkldnn_query_weights_pd, 0);
const_mkldnn_primitive_desc_t ip1_user_memory_weights_pd;
CHECK(mkldnn_primitive_get_primitive_desc(ip1_user_weights_memory, &ip1_user_memory_weights_pd));
bool ip1_weights_equal = mkldnn_memory_primitive_desc_equal(ip1_weights_pd,conv2_user_memory_weights_pd);
mkldnn_primitive_t ip1_internal_weights_memory = NULL;
uint32 ip1_weights_size = 0;
fp32 *ip1_weights_buffer = NULL;
if (!ip1_weights_equal) {
ip1_weights_size = mkldnn_memory_primitive_desc_get_size(ip1_weights_pd);
ip1_weights_buffer = (fp32 *)imotion_malloc(ip1_weights_size);
CHECK(prepare_reorder(&ip1_user_weights_memory, &ip1_weights_pd, 1,
&ip1_internal_weights_memory, &ip1_reorder_weights, ip1_weights_buffer));
}
/* create memory for dst data*/
const_mkldnn_primitive_desc_t ip1_dst_pd
= mkldnn_primitive_desc_query_pd(ip1_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t ip1_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&ip1_internal_dst_memory, ip1_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
ip1_internal_dst_memory, p_lenet->p_u8_data1));
mkldnn_primitive_at_t ip1_srcs[] = {
mkldnn_primitive_at(ip1_internal_src_memory ? ip1_internal_src_memory : pool2_internal_dst_memory, 0),
mkldnn_primitive_at(ip1_internal_weights_memory ? ip1_internal_weights_memory : ip1_user_weights_memory, 0),
mkldnn_primitive_at(ip1_user_bias_memory, 0)
};
const_mkldnn_primitive_t ip1_dsts[] = { ip1_internal_dst_memory};
/* finally create a convolution primitive */
mkldnn_primitive_t ip1;
CHECK(mkldnn_primitive_create(&ip1, ip1_pd, ip1_srcs, ip1_dsts));
/ip1 end***/
/ip2*******/ p_stru_conv_config = (LENET_CONV_CONFIG*)p_lenet->stru_ip2.p_param; mkldnn_primitive_t ip2_user_weights_memory, ip2_user_bias_memory; /* create memory for user data */ init_data_memory(2, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_oi, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_weight.p_data, &ip2_user_weights_memory); init_data_memory(1, &p_stru_conv_config->stru_weight.stru_shape.u32_num, mkldnn_x, mkldnn_f32, engine, (fp32 *)p_stru_conv_config->stru_bias.p_data, &ip2_user_bias_memory);
mkldnn_memory_desc_t ip2_src_md, ip2_weights_md, ip2_bias_md,
ip2_dst_md;
/* create data descriptors for convolution w/ no specified format */
CHECK(mkldnn_memory_desc_init(&ip2_src_md, 2, &p_lenet->stru_ip2.stru_bottom.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&ip2_weights_md, 2, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
CHECK(mkldnn_memory_desc_init(&ip2_bias_md, 1, &p_stru_conv_config->stru_weight.stru_shape.u32_num,
mkldnn_f32, mkldnn_x));
CHECK(mkldnn_memory_desc_init(&ip2_dst_md, 2, &p_lenet->stru_ip2.stru_top.stru_shape.u32_num,
mkldnn_f32, mkldnn_any));
/* create a convolution */
mkldnn_inner_product_desc_t ip2_any_desc;
CHECK(mkldnn_inner_product_forward_desc_init(&ip2_any_desc, mkldnn_forward_inference,
&ip2_src_md, &ip2_weights_md,
&ip2_bias_md, &ip2_dst_md));
mkldnn_primitive_desc_t ip2_pd;
CHECK(mkldnn_primitive_desc_create(&ip2_pd, &ip2_any_desc,
engine, NULL));
mkldnn_primitive_t ip2_reorder_src = NULL;
const_mkldnn_primitive_desc_t ip2_src_pd = mkldnn_primitive_desc_query_pd(
ip2_pd, mkldnn_query_src_pd, 0);
const_mkldnn_primitive_desc_t ip2_user_memory_src_pd;
CHECK(mkldnn_primitive_get_primitive_desc(ip1_internal_dst_memory, &ip2_user_memory_src_pd));
bool ip2_src_equal = mkldnn_memory_primitive_desc_equal(ip2_src_pd,ip2_user_memory_src_pd);
mkldnn_primitive_t ip2_internal_src_memory = NULL;
uint32 ip2_src_size = 0;
fp32 *ip2_src_buffer = NULL;
if (!ip2_src_equal) {
ip2_src_size = mkldnn_memory_primitive_desc_get_size(ip2_src_pd);
ip2_src_buffer = (fp32 *)imotion_malloc(ip2_src_size);
CHECK(prepare_reorder(&ip1_internal_dst_memory, &ip2_src_pd, 1,
&ip2_internal_src_memory, &ip2_reorder_src, ip2_src_buffer));
}
mkldnn_primitive_t ip2_reorder_weights = NULL;
const_mkldnn_primitive_desc_t ip2_weights_pd = mkldnn_primitive_desc_query_pd(
ip2_pd, mkldnn_query_weights_pd, 0);
const_mkldnn_primitive_desc_t ip2_user_memory_weights_pd;
CHECK(mkldnn_primitive_get_primitive_desc(ip2_user_weights_memory, &ip2_user_memory_weights_pd));
bool ip2_weights_equal = mkldnn_memory_primitive_desc_equal(ip2_weights_pd,ip1_user_memory_weights_pd);
mkldnn_primitive_t ip2_internal_weights_memory = NULL;
uint32 ip2_weights_size = 0;
fp32 *ip2_weights_buffer = NULL;
if (!ip2_weights_equal) {
ip2_weights_size = mkldnn_memory_primitive_desc_get_size(ip2_weights_pd);
ip2_weights_buffer = (fp32 *)imotion_malloc(ip2_weights_size);
CHECK(prepare_reorder(&ip2_user_weights_memory, &ip2_weights_pd, 1,
&ip2_internal_weights_memory, &ip2_reorder_weights, ip2_weights_buffer));
}
/* create memory for dst data*/
const_mkldnn_primitive_desc_t ip2_dst_pd
= mkldnn_primitive_desc_query_pd(ip2_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t ip2_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&ip2_internal_dst_memory, ip2_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
ip2_internal_dst_memory, p_lenet->p_u8_data0));
mkldnn_primitive_at_t ip2_srcs[] = {
mkldnn_primitive_at(ip2_internal_src_memory ? ip2_internal_src_memory : ip1_internal_dst_memory, 0),
mkldnn_primitive_at(ip2_internal_weights_memory ? ip2_internal_weights_memory : ip2_user_weights_memory, 0),
mkldnn_primitive_at(ip2_user_bias_memory, 0)
};
const_mkldnn_primitive_t ip2_dsts[] = { ip2_internal_dst_memory};
/* finally create a convolution primitive */
mkldnn_primitive_t ip2;
CHECK(mkldnn_primitive_create(&ip2, ip2_pd, ip2_srcs, ip2_dsts));
/ip2 end***/
/softmax*******/
/* create softmax memory descriptor on dst descriptor
* from previous primitive */
const mkldnn_memory_desc_t *softmax_src_md =
mkldnn_primitive_desc_query_memory_d(ip2_dst_pd);
/* create descriptors for dst softmax data */
mkldnn_memory_desc_t softmax_dst_md;
CHECK(mkldnn_memory_desc_init(
&softmax_dst_md, 2, &p_lenet->stru_softmax.stru_top.stru_shape.u32_num, mkldnn_f32, mkldnn_any));;
/* create a softmax description */
mkldnn_softmax_desc_t softmax_desc;
CHECK(mkldnn_softmax_forward_desc_init(&softmax_desc, mkldnn_forward_inference,
softmax_src_md, 1));
mkldnn_primitive_desc_t softmax_pd;
CHECK(mkldnn_primitive_desc_create(&softmax_pd, &softmax_desc, engine, NULL));
const_mkldnn_primitive_desc_t softmax_dst_pd
= mkldnn_primitive_desc_query_pd(softmax_pd, mkldnn_query_dst_pd, 0);
mkldnn_primitive_t softmax_internal_dst_memory = NULL;
CHECK(mkldnn_primitive_create(
&softmax_internal_dst_memory, softmax_dst_pd, NULL, NULL));
CHECK(mkldnn_memory_set_data_handle(
softmax_internal_dst_memory, p_lenet->p_u8_data1));
mkldnn_primitive_at_t softmax_srcs = { ip2_internal_dst_memory, 0 };
const_mkldnn_primitive_t softmax_dsts[] = { softmax_internal_dst_memory, 0};
/* finally create a softmax primitive */
mkldnn_primitive_t softmax;
CHECK(mkldnn_primitive_create(&softmax, softmax_pd, &softmax_srcs, softmax_dsts));
/softmax end***/
/stream*******/
/* build a simple net */
uint32_t n = 0;
mkldnn_primitive_t net[20];
if (conv1_reorder_src) {
net[n++] = conv1_reorder_src;
}
if (conv1_reorder_weights) {
net[n++] = conv1_reorder_weights;
}
net[n++] = conv1;
net[n++] = pool1;
if (conv2_reorder_src) {
net[n++] = conv2_reorder_src;
}
if (conv2_reorder_weights) {
net[n++] = conv2_reorder_weights;
}
net[n++] = conv2;
net[n++] = pool2;
if (ip1_reorder_src) {
net[n++] = ip1_reorder_src;
}
if (ip1_reorder_weights) {
net[n++] = ip1_reorder_weights;
}
net[n++] = ip1;
if (ip2_reorder_src) {
net[n++] = ip2_reorder_src;
}
if (ip2_reorder_weights) {
net[n++] = ip2_reorder_weights;
}
net[n++] = ip2;
net[n++] = softmax;
mkldnn_stream_t stream;
CHECK(mkldnn_stream_create(&stream, mkldnn_eager));
CHECK(mkldnn_stream_submit(stream, n, net, NULL));
CHECK(mkldnn_stream_wait(stream, n, NULL));
/stream end***/
get_inference_value(&p_lenet->stru_softmax.stru_top,
&p_lenet->stru_result);
printf_blob(&p_lenet->stru_result);
/destroy***/ /* clean-up */ mkldnn_primitive_destroy(conv1); CHECK(mkldnn_primitive_desc_destroy(conv1_pd)); mkldnn_primitive_destroy(conv1_user_src_memory); mkldnn_primitive_destroy(conv1_user_weights_memory); mkldnn_primitive_destroy(conv1_user_bias_memory); mkldnn_primitive_destroy(conv1_internal_src_memory); mkldnn_primitive_destroy(conv1_internal_weights_memory); mkldnn_primitive_destroy(conv1_internal_dst_memory); mkldnn_primitive_destroy(conv1_reorder_src); mkldnn_primitive_destroy(conv1_reorder_weights);
mkldnn_primitive_destroy(pool1);
CHECK(mkldnn_primitive_desc_destroy(pool1_pd));
mkldnn_primitive_destroy(conv2);
CHECK(mkldnn_primitive_desc_destroy(conv2_pd));
mkldnn_primitive_destroy(conv2_user_weights_memory);
mkldnn_primitive_destroy(conv2_user_bias_memory);
mkldnn_primitive_destroy(conv2_internal_src_memory);
mkldnn_primitive_destroy(conv2_internal_weights_memory);
mkldnn_primitive_destroy(conv2_internal_dst_memory);
mkldnn_primitive_destroy(conv2_reorder_src);
mkldnn_primitive_destroy(conv2_reorder_weights);
mkldnn_primitive_destroy(pool2);
CHECK(mkldnn_primitive_desc_destroy(pool2_pd));
mkldnn_primitive_destroy(ip1);
mkldnn_primitive_destroy(ip1_user_weights_memory);
mkldnn_primitive_destroy(ip1_user_bias_memory);
mkldnn_primitive_destroy(ip2);
mkldnn_primitive_destroy(softmax);
mkldnn_stream_destroy(stream);
mkldnn_engine_destroy(engine);
/**destroy done/
return *((uint8 *)p_lenet->stru_result.p_data);
} it is better than before, but not better than using cblas_sgemm. can you give me more advice? and you can see the verbose: MKLDNN_VERBOSE=2 ./lenet_test mkldnn_verbose,create,reorder,simple:any,undef,in:f32_oihw out:f32_Ohwi8o,num:1,20x1x5x5,0.00195312 mkldnn_verbose,create,convolution,jit:avx2,forward_inference,fsrc:nchw fwei:Ohwi8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic1oc20_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,0.0561523 mkldnn_verbose,create,pooling,jit:avx,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic20_ih24oh12kh2sh2ph0_iw24ow12kw2sw2pw0,0.0319824 mkldnn_verbose,create,reorder,simple:any,undef,in:f32_oihw out:f32_OIhw8i8o,num:1,50x20x5x5,0.00292969 mkldnn_verbose,create,convolution,jit:avx2,forward_inference,fsrc:nChw8c fwei:OIhw8i8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic20oc50_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,0.097168 mkldnn_verbose,create,pooling,jit:avx,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic50_ih8oh4kh2sh2ph0_iw8ow4kw2sw2pw0,0.0310059 mkldnn_verbose,create,reorder,simple:any,undef,in:f32_nChw8c out:f32_nchw,num:1,1x50x4x4,0.00415039 mkldnn_verbose,create,inner_product,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nc,,mb1ic800oc500,0.00708008 mkldnn_verbose,create,inner_product,gemm:blas,forward_inference,fsrc:nc fwei:oi fbia:x fdst:nc,,mb1ic500oc10,0.00195312 mkldnn_verbose,create,softmax,ref:any,forward_inference,fdata:nc fdiff:undef,,mb1ic10,0.00292969 mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_oihw out:f32_Ohwi8o,num:1,20x1x5x5,0.0100098 mkldnn_verbose,exec,convolution,jit:avx2,forward_inference,fsrc:nchw fwei:Ohwi8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic1oc20_ih28oh24kh5sh1dh0ph0_iw28ow24kw5sw1dw0pw0,0.013916 mkldnn_verbose,exec,pooling,jit:avx,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic20_ih24oh12kh2sh2ph0_iw24ow12kw2sw2pw0,0.0119629 mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_oihw out:f32_OIhw8i8o,num:1,50x20x5x5,0.0258789 mkldnn_verbose,exec,convolution,jit:avx2,forward_inference,fsrc:nChw8c fwei:OIhw8i8o fbia:x fdst:nChw8c,alg:convolution_direct,mb1_ic20oc50_ih12oh8kh5sh1dh0ph0_iw12ow8kw5sw1dw0pw0,0.0239258 mkldnn_verbose,exec,pooling,jit:avx,forward_inference,fdata:nChw8c fws:undef,alg:pooling_max,mb1ic50_ih8oh4kh2sh2ph0_iw8ow4kw2sw2pw0,0.00415039 mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nChw8c out:f32_nchw,num:1,1x50x4x4,0.00488281 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nchw fwei:oihw fbia:x fdst:nc,,mb1ic800oc500,1.33301 mkldnn_verbose,exec,inner_product,gemm:blas,forward_inference,fsrc:nc fwei:oi fbia:x fdst:nc,,mb1ic500oc10,0.0131836 mkldnn_verbose,exec,softmax,ref:any,forward_inference,fdata:nc fdiff:undef,,mb1ic10,0.0410156
it seems inner product is too slow, i dont know why
when i run this: MKLDNN_VERBOSE=2 ./benchdnn --ip mb1ic50ih4iw4oc500 it is really cost 1.32715 ms. it is too long . and it use gemm:blas, not ,jit:avx2. can you give me some advices to optimize it? thanks.
Hi @arthurhi,
What is your HW? And how many cores do you use? Did you build Intel MKL-DNN with Intel MKL-ML?
I would also suggest you to run the whole topology at least for several iterations to get the sustainable performance numbers (of course w/o primitive recreation, only to execute them). It is also important to set the right affinity to get reproducible performance results. A single run is not representative.
In particular, to run benchdnn in performance mode you need to pass --mode=P:
export OMP_NUM_THREADS=${number_of_phys_cores}
export KMP_AFFINITY=granularity=fine,compact # if HT is off
export KMP_AFFINITY=granularity=fine,compact,1,0 # if HT is on
./benchdnn --mode=P --ip mb1ic50ih4iw4oc500
On my system that gives:
perf,1,500,50,1,4,4,"wip",FWD_D,f32,0.00756836,105.703,0.00851656,93.9346
tests:1 passed:0 skipped:0 mistrusted:0 unimplemented:0 failed:0
total perf: min(ms):0.00756836 avg(ms):0.00851656
Note that average time is avg(ms):0.0085.
But if I use only one run to measure performance I would get 91 ms:
# same export as above
# MKLDNN_VERBOSE=1 ./benchdnn --mode=C --ip mb1ic50ih4iw4oc500
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nchw out:f32_nchw,num:1,1x50x4x4,0.462891
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nchw out:f32_nchw,num:1,1x50x4x4,0.785889
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_oihw out:f32_oihw,num:1,500x50x4x4,0.340088
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_oihw out:f32_oihw,num:1,500x50x4x4,0.486816
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nc out:f32_nc,num:1,1x500,0.302002
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nc out:f32_nc,num:1,1x500,0.221924
mkldnn_verbose,exec,inner_product,gemm:blas,forward_training,fsrc:nchw fwei:oihw fbia:undef fdst:nc,,mb1ic800oc500,91.2021
mkldnn_verbose,exec,reorder,simple:any,undef,in:f32_nc out:f32_nc,num:1,1x500,0.461914
0:PASSED __REPRO: --dir=FWD_D mb1oc500ic50ih4n"wip"
tests:1 passed:1 skipped:0 mistrusted:0 unimplemented:0 failed:0
The mode --mode=P is a standard way to measure performance with benchdnn. The mode --mode=C is a correctness check. It runs a primitive only once and then compares the results with the golden ones.
thank you very much. now i do the operation what you adviced. and now it really works well. good job! thank you .now my follows:
create all the primitives, memory, and the stream
for(all the images )
{
if(first run)
stream submit and wait
else
stream rerun and wait
}
it looks fine.