run_openblas_infer.sh 2.0 KB
Newer Older
C
chengduoZH 已提交
1 2
#!/bin/bash

3 4
set -e

T
tensor-tang 已提交
5 6 7 8
function clock_to_seconds() {
  hours=`echo $1 | awk -F ':' '{print $1}'`
  mins=`echo $1 | awk -F ':' '{print $2}'`
  secs=`echo $1 | awk -F ':' '{print $3}'`
T
tensor-tang 已提交
9
  echo `awk 'BEGIN{printf "%.2f",('$secs' + '$mins' * 60 + '$hours' * 3600)}'`
T
tensor-tang 已提交
10 11
}

12
function infer() {
13
  export OPENBLAS_MAIN_FREE=1
14 15 16
  topology=$1
  layer_num=$2
  bs=$3
17 18 19
  trainers=`nproc`
  if [ $trainers -gt $bs ]; then
    trainers=$bs
20
  fi
21 22 23 24 25 26
  log="logs/infer-${topology}-${layer_num}-${trainers}openblas-${bs}.log"
  threads=$((`nproc` / trainers))
  if [ $threads -eq 0 ]; then
    threads=1
  fi
  export OPENBLAS_NUM_THREADS=$threads
27 28 29

  models_in="models/${topology}-${layer_num}/pass-00000/"
  if [ ! -d $models_in ]; then
30 31
    echo "./run_mkl_infer.sh to save the model first"
    exit 0
32
  fi
33
  log_period=$((32 / bs))
34 35
  paddle train --job=test \
    --config="${topology}.py" \
36
    --use_mkldnn=False \
37
    --use_gpu=False \
38
    --trainer_count=$trainers \
T
tensor-tang 已提交
39
    --log_period=$log_period \
40
    --config_args="batch_size=${bs},layer_num=${layer_num},is_infer=True,num_samples=256" \
41
    --init_model_path=$models_in \
T
tensor-tang 已提交
42 43
    2>&1 | tee ${log}

44
  # calculate the last 5 logs period time of 160(=32*5) samples,
T
tensor-tang 已提交
45 46 47 48 49
  # the time before are burning time.
  start=`tail ${log} -n 7 | head -n 1 | awk -F ' ' '{print $2}' | xargs`
  end=`tail ${log} -n 2 | head -n 1 | awk -F ' ' '{print $2}' | xargs`
  start_sec=`clock_to_seconds $start`
  end_sec=`clock_to_seconds $end`
50 51
  fps=`awk 'BEGIN{printf "%.2f",(160 / ('$end_sec' - '$start_sec'))}'`
  echo "Last 160 samples start: ${start}(${start_sec} sec), end: ${end}(${end_sec} sec;" >> ${log}
T
tensor-tang 已提交
52
  echo "FPS: $fps images/sec" 2>&1 | tee -a ${log}
53 54 55 56 57 58 59 60 61 62 63 64 65
}

if [ ! -f "train.list" ]; then
  echo " " > train.list
fi
if [ ! -f "test.list" ]; then
  echo " " > test.list
fi
if [ ! -d "logs" ]; then
  mkdir logs
fi

# inference benchmark
66 67
for batchsize in 1 2 4 8 16; do
  infer vgg 19 $batchsize
T
tensor-tang 已提交
68 69 70
  infer resnet 50 $batchsize 
  infer googlenet v1 $batchsize
  infer alexnet 2 $batchsize
71
done