#!/bin/bash
Network="MobileNetV3_for_Cifar10"
batch_size=2048
learning_rate=1.0
NUM_SAMPLES=50000
export RANK_SIZE=4
export WORLD_SIZE=4
for para in $*
do
if [[ $para == --batch_size* ]];then
batch_size=`echo ${para#*=}`
elif [[ $para == --learning_rate* ]];then
learning_rate=`echo ${para#*=}`
fi
done
cur_path=`pwd`
cur_path_last_diename=${cur_path##*/}
if [ x"${cur_path_last_diename}" == x"test" ];then
test_path_dir=${cur_path}
cd ..
cur_path=`pwd`
else
test_path_dir=${cur_path}/test
fi
start_time=$(date +%s)
check_etp_flag=`env | grep etp_running_flag`
etp_flag=`echo ${check_etp_flag#*=}`
if [ x"${etp_flag}" != x"true" ];then
source ${test_path_dir}/env_npu.sh
fi
RANK_ID_START=0
export PYTHONPATH=./:$PYTHONPATH
for((RANK_ID=$RANK_ID_START;RANK_ID<$((RANK_SIZE+RANK_ID_START));RANK_ID++))
do
echo ${RANK_ID}
ASCEND_DEVICE_ID=${RANK_ID}
KERNEL_NUM=$(($(nproc)/8))
PID_START=$((KERNEL_NUM * RANK_ID))
PID_END=$((PID_START + KERNEL_NUM - 1))
if [ -d ${test_path_dir}/output/${ASCEND_DEVICE_ID} ];then
rm -rf ${test_path_dir}/output/${ASCEND_DEVICE_ID}
mkdir -p ${test_path_dir}/output/$ASCEND_DEVICE_ID
else
mkdir -p ${test_path_dir}/output/$ASCEND_DEVICE_ID
fi
nohup taskset -c $PID_START-$PID_END python3.7 -u train.py \
--data-dir='./data' --batch-size=${batch_size} --mode=small --print-freq=100 \
--dataset=CIFAR10 --ema-decay=0 --label-smoothing=0 --lr=${learning_rate} \
--save-epoch-freq=1000 --lr-decay=cos --lr-min=0 --warmup-epochs=5 \
--weight-decay=6e-5 --num-epochs=400 --num-workers=16 --width-multiplier=1 \
--local_rank $RANK_ID \
--world_size $WORLD_SIZE > ${test_path_dir}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log &
done
wait
ASCEND_DEVICE_ID=0
end_time=$(date +%s)
e2e_time=$(( $end_time - $start_time ))
echo "------------------ Final result ------------------"
FPS=`grep -a 'Train:' ${test_path_dir}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|tail -n 5|awk -F " " '{print $9}'|awk '{sum+=$1} END {print '${batch_size}'/(sum/NR)}'`
echo "Final Performance images/sec : $FPS"
train_accuracy=`grep -a 'Val:' ${test_path_dir}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk 'END {print}'|awk -F "ACC@1:" '{print $NF}'|awk -F " " '{print $5}'`
echo "Final Train Accuracy : ${train_accuracy}"
echo "E2E Training Duration sec : $e2e_time"
BatchSize=${batch_size}
DeviceType=`uname -m`
if [[ $precision_mode == "O0" ]];then
CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'fp32'_'acc'
else
CaseName=${Network}_bs${BatchSize}_${RANK_SIZE}'p'_'acc'
fi
ActualFPS=${FPS}
TrainingTime=`awk 'BEGIN{printf "%.2f\n", '${batch_size}'*100/'${FPS}'}'`
grep Train: ${test_path_dir}/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log | awk -F "train_loss:" '{print $NF}' | awk -F " " '{print $1}' >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt
grep Val: ${test_path_dir}/output/$ASCEND_DEVICE_ID/train_$ASCEND_DEVICE_ID.log | awk -F "val_loss:" '{print $NF}' | awk -F " " '{print $1}' >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/val_${CaseName}_loss.txt
ActualLoss=`awk 'END {print}' ${test_path_dir}/output/$ASCEND_DEVICE_ID/train_${CaseName}_loss.txt`
echo "Network = ${Network}" > ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "RankSize = ${RANK_SIZE}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "BatchSize = ${BatchSize}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "DeviceType = ${DeviceType}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "CaseName = ${CaseName}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "ActualFPS = ${ActualFPS}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "TrainingTime = ${TrainingTime}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "TrainAccuracy = ${train_accuracy}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "ActualLoss = ${ActualLoss}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log
echo "E2ETrainingTime = ${e2e_time}" >> ${test_path_dir}/output/$ASCEND_DEVICE_ID/${CaseName}.log