Skip to content

Commit

Permalink
[TIPC]Support @to_static train for base-transformer (#3277)
Browse files Browse the repository at this point in the history
* [TIPC]Support @to_static train for base-transformer

* Fix to_static args
  • Loading branch information
0x45f authored Sep 17, 2022
1 parent 1e919ba commit 9a25764
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/machine_translation/transformer/tls/to_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ def apply_to_static(config, model):
if support_to_static:
specs = create_input_specs()
model = to_static(model, input_spec=specs)
print("Successfully to apply @to_static with specs: {}".format(specs))
return model
18 changes: 18 additions & 0 deletions examples/machine_translation/transformer/train.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import time

Expand Down Expand Up @@ -101,6 +115,9 @@ def parse_args():
help=
'The option of profiler, which should be in format \"key1=value1;key2=value2;key3=value3\".'
)
parser.add_argument("--to_static",
action="store_true",
help="Whether to_static to train Transformer. ")
args = parser.parse_args()
return args

Expand Down Expand Up @@ -383,6 +400,7 @@ def do_train(args):
args.unk_token = ARGS.unk_token
args.bos_token = ARGS.bos_token
args.eos_token = ARGS.eos_token
args.to_static = ARGS.to_static
pprint(args)

args.profiler_options = ARGS.profiler_options
Expand Down
26 changes: 20 additions & 6 deletions tests/test_tipc/benchmark_train.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,21 @@ FILENAME=$new_filename
# MODE must be one of ['benchmark_train']
MODE=$2
PARAMS=$3
# bash test_tipc/benchmark_train.sh test_tipc/configs/det_mv3_db_v2_0/train_benchmark.txt benchmark_train dynamic_bs8_null_DP_N1C1
REST_ARGS=$4
# bash test_tipc/benchmark_train.sh test_tipc/configs/transformer/base/train_infer_python.txt benchmark_train to_static

to_static="d2sF"
# parse "to_static" options and modify trainer into "to_static_trainer"
if [ $REST_ARGS = "to_static" ] || [ $PARAMS = "to_static" ] ;then
to_static="d2sT"
sed -i 's/trainer:norm_train/trainer:to_static_train/g' $FILENAME
# clear PARAM contents
if [ $PARAMS = "to_static" ] ;then
PARAMS=""
fi
fi


IFS=$'\n'
# parser params from train_benchmark.txt
dataline=`cat $FILENAME`
Expand Down Expand Up @@ -206,7 +220,7 @@ for batch_size in ${batch_size_list[*]}; do
if [ ${#gpu_id} -le 1 ];then
log_path="$SAVE_LOG/profiling_log"
mkdir -p $log_path
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_profiling"
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}_profiling"
func_sed_params "$FILENAME" "${line_gpuid}" "0" # sed used gpu_id
# set profile_option params
tmp=`sed -i "${line_profile}s/.*/${profile_option}/" "${FILENAME}"`
Expand All @@ -222,8 +236,8 @@ for batch_size in ${batch_size_list[*]}; do
speed_log_path="$SAVE_LOG/index"
mkdir -p $log_path
mkdir -p $speed_log_path
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_log"
speed_log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_speed"
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}_log"
speed_log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}_speed"
func_sed_params "$FILENAME" "${line_profile}" "null" # sed profile_id as null
cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
echo $cmd
Expand Down Expand Up @@ -257,8 +271,8 @@ for batch_size in ${batch_size_list[*]}; do
speed_log_path="$SAVE_LOG/index"
mkdir -p $log_path
mkdir -p $speed_log_path
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_log"
speed_log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_speed"
log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}_log"
speed_log_name="${repo_name}_${model_name}_bs${batch_size}_${precision}_${run_mode}_${device_num}_${to_static}_speed"
func_sed_params "$FILENAME" "${line_gpuid}" "$gpu_id" # sed used gpu_id
func_sed_params "$FILENAME" "${line_profile}" "null" # sed --profile_option as null
cmd="bash test_tipc/test_train_inference_python.sh ${FILENAME} benchmark_train > ${log_path}/${log_name} 2>&1 "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ norm_train:../examples/machine_translation/transformer/train.py --config ../exam
pact_train:null
fpgm_train:null
distill_train:null
null:null
to_static_train:--to_static
null:null
##
===========================eval_params===========================
Expand Down
28 changes: 23 additions & 5 deletions tests/test_tipc/test_train_inference_python.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
#!/bin/bash

# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

source test_tipc/common_func.sh

FILENAME=$1
Expand Down Expand Up @@ -40,8 +55,8 @@ fpgm_key=$(func_parser_key "${lines[17]}")
fpgm_trainer=$(func_parser_value "${lines[17]}")
distill_key=$(func_parser_key "${lines[18]}")
distill_trainer=$(func_parser_value "${lines[18]}")
trainer_key1=$(func_parser_key "${lines[19]}")
trainer_value1=$(func_parser_value "${lines[19]}")
to_static_key=$(func_parser_key "${lines[19]}")
to_static_trainer=$(func_parser_value "${lines[19]}")
trainer_key2=$(func_parser_key "${lines[20]}")
trainer_value2=$(func_parser_value "${lines[20]}")

Expand Down Expand Up @@ -295,9 +310,12 @@ else
elif [ ${trainer} = "${distill_key}" ]; then
run_train=${distill_trainer}
run_export=${distill_export}
elif [ ${trainer} = ${trainer_key1} ]; then
run_train=${trainer_value1}
run_export=${export_value1}
# In case of @to_static, we re-used norm_traier,
# but append "--to_static" for config
# to trigger "apply_to_static" logic in 'train.py'
elif [ ${trainer} = "${to_static_key}" ]; then
run_train="${norm_trainer} ${to_static_trainer}"
run_export=${norm_export}
elif [[ ${trainer} = ${trainer_key2} ]]; then
run_train=${trainer_value2}
run_export=${export_value2}
Expand Down

0 comments on commit 9a25764

Please sign in to comment.