From 0beb613a077d27f2d8037d7d832a578e425efe39 Mon Sep 17 00:00:00 2001 From: zhengya01 <43601548+zhengya01@users.noreply.github.com> Date: Tue, 14 May 2019 14:41:07 +0800 Subject: [PATCH] add ce for deep_attention_matching (#2226) --- .../deep_attention_matching/.run_ce.sh | 25 ++++++++ .../deep_attention_matching/__init__.py | 0 .../deep_attention_matching/_ce.py | 60 +++++++++++++++++++ .../deep_attention_matching/main.py | 13 +++- 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100755 PaddleNLP/dialogue_model_toolkit/deep_attention_matching/.run_ce.sh create mode 100644 PaddleNLP/dialogue_model_toolkit/deep_attention_matching/__init__.py create mode 100644 PaddleNLP/dialogue_model_toolkit/deep_attention_matching/_ce.py diff --git a/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/.run_ce.sh b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/.run_ce.sh new file mode 100755 index 00000000..601eaaab --- /dev/null +++ b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/.run_ce.sh @@ -0,0 +1,25 @@ +export CE_MODE_X=ce + +export CUDA_VISIBLE_DEVICES=0 + +python -u main.py \ + --do_train True \ + --use_cuda \ + --data_path ./data/ubuntu/data_small.pkl \ + --save_path ./model_files/ubuntu \ + --use_pyreader \ + --vocab_size 434512 \ + --_EOS_ 28270 \ + --batch_size 32 | python _ce.py + +export CUDA_VISIBLE_DEVICES=0,1,2,3 + +python -u main.py \ + --do_train True \ + --use_cuda \ + --data_path ./data/ubuntu/data_small.pkl \ + --save_path ./model_files/ubuntu \ + --use_pyreader \ + --vocab_size 434512 \ + --_EOS_ 28270 \ + --batch_size 32 | python _ce.py diff --git a/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/__init__.py b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/_ce.py b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/_ce.py new file mode 100644 index 00000000..28c3ea32 --- /dev/null +++ b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/_ce.py @@ -0,0 +1,60 @@ +# this file is only used for continuous evaluation test! + +import os +import sys +sys.path.append(os.environ['ceroot']) +from kpi import CostKpi +from kpi import DurationKpi + +train_cost_card1 = CostKpi('train_cost_card1', 0.02, 0, actived=True) +train_cost_card4 = CostKpi('train_cost_card4', 0.06, 0, actived=True) +train_duration_card1 = DurationKpi('train_duration_card1', 0.01, 0, actived=True) +train_duration_card4 = DurationKpi('train_duration_card4', 0.01, 0, actived=True) + +tracking_kpis = [ + train_cost_card1, + train_cost_card4, + train_duration_card1, + train_duration_card4, +] + + +def parse_log(log): + ''' + This method should be implemented by model developers. + + The suggestion: + + each line in the log should be key, value, for example: + + " + train_cost\t1.0 + test_cost\t1.0 + train_cost\t1.0 + train_cost\t1.0 + train_acc\t1.2 + " + ''' + for line in log.split('\n'): + fs = line.strip().split('\t') + print(fs) + if len(fs) == 3 and fs[0] == 'kpis': + kpi_name = fs[1] + kpi_value = float(fs[2]) + yield kpi_name, kpi_value + + +def log_to_ce(log): + kpi_tracker = {} + for kpi in tracking_kpis: + kpi_tracker[kpi.name] = kpi + + for (kpi_name, kpi_value) in parse_log(log): + print(kpi_name, kpi_value) + kpi_tracker[kpi_name].add_record(kpi_value) + kpi_tracker[kpi_name].persist() + + +if __name__ == '__main__': + log = sys.stdin.read() + log_to_ce(log) diff --git a/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/main.py b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/main.py index b7f05e54..fdcf8278 100755 --- a/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/main.py +++ b/PaddleNLP/dialogue_model_toolkit/deep_attention_matching/main.py @@ -336,8 +336,9 @@ def train(args): .format(epoch, "%2.2f sec" % pass_time_cost)) # For internal continuous evaluation if "CE_MODE_X" in os.environ: - print("kpis train_cost %f" % last_cost) - print("kpis train_duration %f" % train_time) + card_num = get_cards() + print("kpis\ttrain_cost_card%d\t%f" % (card_num, last_cost)) + print("kpis\ttrain_duration_card%d\t%f" % (card_num, train_time)) def test(args): @@ -455,6 +456,14 @@ def test(args): print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) +def get_cards(): + num = 0 + cards = os.environ.get('CUDA_VISIBLE_DEVICES', '') + if cards != '': + num = len(cards.split(",")) + return num + + if __name__ == '__main__': args = config.parse_args() config.print_arguments(args) -- GitLab