poor performance on conv2d with ow=1
I try to optimize the voice synthesize model, with a lot of conv1d invoke fllowing the steps of https://www.tensorflow.org/guide/performance/overview#optimizing_for_cpu, I found the results on tensorflow v1.11.0 with mkl version is slower 50% than the same tensorflow without mkl.
First of all, based on https://github.com/intel/mkl-dnn/issues/285, I test conv2d NCHW with respectively oh=1 or ow=1. I found ow=1 slower slower than oh=1.
So I doubt how to optimize cpu performance on conv1d ???
Environment
Intel MKL-DNN includes hardware-specific optimizations and may behave differently on depending on the compiler and build environment. Include the following information to help reproduce the issue:
- Intel(R) Xeon(R) CPU E5-2682 v4 @ 2.50GHz
- company internal version based on centos
- 4.9.2
- empty value
- no cmake
- tensorflow 1.11
Steps to reproduce
isntall fllowing https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide
# Python 2.7
pip install https://storage.googleapis.com/intel-optimized-tensorflow/tensorflow-1.11.0-cp27-cp27mu-linux_x86_64.whl
ow =1 cmd
MKLDNN_VERBOSE=1 KMP_BLOCKTIME=0 CUDA_VISIBLE_DEVICES=-1 OMP_NUM_THREADS=32 python conv2d.py ow
ow=1
2018-11-05 15:42:35.614687: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
start
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nChw8c,num:1,32x128x102400x1,85.6531
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_OIhw8i8o,num:1,128x128x3x1,0.134766
mkldnn_verbose,exec,convolution,jit:avx2,forward_training,fsrc:nChw8c fwei:OIhw8i8o fbia:undef fdst:nChw8c,alg:convolution_direct,mb32_g1ic128oc128_ih102400oh102398kh3sh1dh0ph0_iw1ow1kw1sw1dw0pw0,991.858
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nChw8c out:f32_nchw,num:1,32x128x102398x1,174.348
data_format_2d=NCHW 100 epoch cost 2.131751
oh =1 cmd
MKLDNN_VERBOSE=1 KMP_BLOCKTIME=0 CUDA_VISIBLE_DEVICES=-1 OMP_NUM_THREADS=32 python conv2d.py oh
oh=1
2018-11-05 15:42:05.224622: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
start
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nchw out:f32_nChw8c,num:1,32x128x1x102400,91.1848
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_hwio out:f32_OIhw8i8o,num:1,128x128x1x3,0.152832
mkldnn_verbose,exec,convolution,jit:avx2,forward_training,fsrc:nChw8c fwei:OIhw8i8o fbia:undef fdst:nChw8c,alg:convolution_direct,mb32_g1ic128oc128_ih1oh1kh1sh1dh0ph0_iw102400ow102398kw3sw1dw0pw0,835.947
mkldnn_verbose,exec,reorder,jit:uni,undef,in:f32_nChw8c out:f32_nchw,num:1,32x128x1x102398,199.113
data_format_2d=NCHW 100 epoch cost 1.990710
the code shows
import os
import sys
import tensorflow as tf
import numpy as np
import time
"""
default NWC
input=[batch, in_width, in_channels] filter=[ filter_width, in_channels, out_channels]
=>tf.nn.conv2d [batch,1,in_width, in_channels] [1, filter_width, in_channels, out_channels]
mkl NCW
input=[batch, in_channels, in_width] filter=[filter_width, in_channels, out_channels]
=>tf.nn.conv2d []
"""
batch = 32
in_width = 102400
in_height = 1
in_channels = 128
filter_width = 3
filter_height = 1
out_channels = 128
data_format_2d = 'NCHW'
n = 100
start = 99
input_2d = {}
if len(sys.argv) == 2 and sys.argv[1] == "ow":
print("ow=1")
in_width,in_height = in_height, in_width
filter_width,filter_height = filter_height, filter_width
else:
print("oh=1")
if data_format_2d == "NCHW":
input_ = np.array(np.arange(1, 1 + batch*in_width*in_height*in_channels).reshape([batch, in_channels, in_height,in_width]), dtype=np.float32)
else:
input_ = np.array(np.arange(1, 1 + batch*in_width*in_height*in_channels).reshape([batch, in_height,in_width,in_channels]), dtype=np.float32)
for i in range(start,n):
input_2d[i] = input_ + i*n*i*1.0
kernel_2d = np.array(np.arange(1, 1 + filter_width*filter_height*in_channels*out_channels), dtype=np.float32).reshape([filter_height, filter_width, in_channels, out_channels])
a = tf.placeholder(dtype=tf.float32)
conv2d = tf.nn.conv2d(a, kernel_2d, [1,1,1,1], 'VALID', data_format=data_format_2d)
config = tf.ConfigProto()
config.intra_op_parallelism_threads = 32
config.inter_op_parallelism_threads = 2
sess = tf.Session(config=config)
print("start")
sess.run(tf.global_variables_initializer())
start_ts = time.time()
for i in range(start,n):
sess.run(conv2d, feed_dict={a:input_2d[i]})
print("data_format_2d=%s %d epoch cost %f" % (data_format_2d,n, (time.time()) - start_ts))
Intel MKL-DNN v0.17-rc includes API and performance optimizations for 1D spatial convolutions. These optimizations were not picked up by Tensorflow yet as far as I know. You can measure performance of 1D spatial convolutions using benchdnn, Intel MKL-DNN's performance benchmark.
As the question touches on Tensorflow integration with Intel MKL-DNN I would suggest that you submit the issue at Tensorflow Github and tag @agramesh1.
Thanks for your reply. Could you please tell me how to rebuild tensorflow with the Intel MKL-DNN v0.17-rc ?
Please refer to Tensorflow documentation or submit a question at Tensorflow Github. Note though, that rebuilding alone is unlikely to help, as Tensorflow does not map 1D convolutions to the new APIs.
Since there is a issue opened on the TF repo (https://github.com/tensorflow/tensorflow/issues/23516) I'm closing this.