diff --git a/configs/cyclegan_horse2zebra.yaml b/configs/cyclegan_horse2zebra.yaml index e0b0c294d91108a1207bae11540b3fef2c48e6dc..8967500f1b65a8ed9bf5a21dd75cc56dd4980f9c 100644 --- a/configs/cyclegan_horse2zebra.yaml +++ b/configs/cyclegan_horse2zebra.yaml @@ -26,6 +26,8 @@ model: gan_criterion: name: GANLoss gan_mode: lsgan + # training model under @to_static + to_static: False export_model: - {name: 'netG_A', inputs_num: 1} diff --git a/ppgan/models/__init__.py b/ppgan/models/__init__.py index 7229c61839675e22209a9145f65a2bb25e45df74..6b116a71ea334b034da33819de081f1d1347d2cb 100644 --- a/ppgan/models/__init__.py +++ b/ppgan/models/__init__.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .base_model import BaseModel +from .base_model import BaseModel, apply_to_static from .gan_model import GANModel from .cycle_gan_model import CycleGANModel from .pix2pix_model import Pix2PixModel diff --git a/ppgan/models/base_model.py b/ppgan/models/base_model.py index ce1ee528add6e5068090a287b6264188f53f6c50..ec6d0336118681a2494859a438018d72f1189e21 100755 --- a/ppgan/models/base_model.py +++ b/ppgan/models/base_model.py @@ -19,9 +19,13 @@ import numpy as np from collections import OrderedDict from abc import ABC, abstractmethod +from paddle.jit import to_static +from paddle.static import InputSpec + from .criterions.builder import build_criterion from ..solver import build_lr_scheduler, build_optimizer from ..utils.visual import tensor2img +from ..utils.logger import get_logger class BaseModel(ABC): @@ -217,3 +221,13 @@ class BaseModel(ABC): model_name), model_filename="{}.pdmodel".format(model_name), params_filename="{}.pdiparams".format(model_name)) + +def apply_to_static(support_to_static, image_shape, model): + if support_to_static: + specs = None + if image_shape is not None: + specs = [InputSpec([None] + image_shape)] + model = to_static(model, input_spec=specs) + logger = get_logger('ppgan') + logger.info("Successfully to apply @to_static with specs: {}".format(specs)) + return model \ No newline at end of file diff --git a/ppgan/models/cycle_gan_model.py b/ppgan/models/cycle_gan_model.py index 000938eaa3042eaa7e9a8a6cb3ac48bb6f131cef..eddddde5e03f8ee3624adaaaa94bf5f50f74aac3 100644 --- a/ppgan/models/cycle_gan_model.py +++ b/ppgan/models/cycle_gan_model.py @@ -13,7 +13,7 @@ # limitations under the License. import paddle -from .base_model import BaseModel +from .base_model import BaseModel, apply_to_static from .builder import MODELS from .generators.builder import build_generator @@ -40,7 +40,9 @@ class CycleGANModel(BaseModel): pool_size=50, direction='a2b', lambda_a=10., - lambda_b=10.): + lambda_b=10., + to_static=False, + image_shape=None): """Initialize the CycleGAN class. Args: @@ -59,6 +61,9 @@ class CycleGANModel(BaseModel): # Code (vs. paper): G_A (G), G_B (F), D_A (D_Y), D_B (D_X) self.nets['netG_A'] = build_generator(generator) self.nets['netG_B'] = build_generator(generator) + # set @to_static for benchmark, skip this by default. + apply_to_static(to_static, image_shape, self.nets['netG_A']) + apply_to_static(to_static, image_shape, self.nets['netG_B']) init_weights(self.nets['netG_A']) init_weights(self.nets['netG_B']) @@ -66,6 +71,9 @@ class CycleGANModel(BaseModel): if discriminator: self.nets['netD_A'] = build_discriminator(discriminator) self.nets['netD_B'] = build_discriminator(discriminator) + # set @to_static for benchmark, skip this by default. + apply_to_static(to_static, image_shape, self.nets['netD_A']) + apply_to_static(to_static, image_shape, self.nets['netD_B']) init_weights(self.nets['netD_A']) init_weights(self.nets['netD_B']) diff --git a/test_tipc/benchmark_train.sh b/test_tipc/benchmark_train.sh index 8d79fad6f5b8f8a24a38a16f6bc8d8b15a571307..152e52eb51e08acc9bf6eab87b1f90beba71edee 100644 --- a/test_tipc/benchmark_train.sh +++ b/test_tipc/benchmark_train.sh @@ -67,6 +67,19 @@ FILENAME=$new_filename # MODE must be one of ['benchmark_train'] MODE=$2 PARAMS=$3 +REST_ARGS=$4 + +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` @@ -161,7 +174,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}"` @@ -177,8 +190,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 @@ -212,8 +225,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 " diff --git a/test_tipc/configs/CycleGAN/train_infer_python.txt b/test_tipc/configs/CycleGAN/train_infer_python.txt index b5a7f4ee23d2b6d72a8744885ff33c3f3f645f73..6ff71d98c3c912ef5120a3c211b30bc70e198264 100644 --- a/test_tipc/configs/CycleGAN/train_infer_python.txt +++ b/test_tipc/configs/CycleGAN/train_infer_python.txt @@ -17,7 +17,7 @@ norm_train:tools/main.py -c configs/cyclegan_horse2zebra.yaml --seed 123 -o log_ pact_train:null fpgm_train:null distill_train:null -null:null +to_static_train:-o model.to_static=True null:null ## ===========================eval_params=========================== diff --git a/test_tipc/test_train_inference_python.sh b/test_tipc/test_train_inference_python.sh index d10e9db39ffba926c5f5b4d2d8f9299a68fa617f..c11e9b9a22d43628fad832e68f18beff9803516e 100644 --- a/test_tipc/test_train_inference_python.sh +++ b/test_tipc/test_train_inference_python.sh @@ -33,8 +33,8 @@ trainer_list=$(func_parser_value "${lines[14]}") trainer_norm=$(func_parser_key "${lines[15]}") norm_trainer=$(func_parser_value "${lines[15]}") -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]}") @@ -218,8 +218,16 @@ else fi for trainer in ${trainer_list[*]}; do flag_quant=False - run_train=${norm_trainer} - run_export=${norm_export} + # In case of @to_static, we re-used norm_traier, + # but append "-o Global.to_static=True" for config + # to trigger "apply_to_static" logic in 'engine.py' + if [ ${trainer} = "${to_static_key}" ]; then + run_train="${norm_trainer} ${to_static_trainer}" + run_export=${norm_export} + else + run_train=${norm_trainer} + run_export=${norm_export} + fi if [ ${run_train} = "null" ]; then continue