|
47 | 47 | import squad_lib_sp |
48 | 48 | import tokenization |
49 | 49 | import gpu_affinity |
| 50 | +import tf_trt |
50 | 51 | from official.utils.misc import distribution_utils |
51 | 52 | from official.utils.misc import keras_utils |
52 | 53 | from official.utils.misc import tpu_lib |
53 | 54 | import dllogger_class |
54 | 55 |
|
55 | 56 | flags.DEFINE_enum( |
56 | 57 | 'mode', 'train_and_predict', |
57 | | - ['train_and_predict', 'train', 'predict', 'export_only'], |
58 | | - 'One of {"train_and_predict", "train", "predict", "export_only"}. ' |
| 58 | + ['train_and_predict', 'train', 'predict', 'export_only', 'sm_predict', 'trt_predict'], |
| 59 | + 'One of {"train_and_predict", "train", "predict", "export_only", "sm_predict", "trt_predict"}. ' |
59 | 60 | '`train_and_predict`: both train and predict to a json file. ' |
60 | 61 | '`train`: only trains the model. ' |
61 | 62 | 'trains the model and evaluates in the meantime. ' |
62 | 63 | '`predict`: predict answers from the squad json file. ' |
63 | 64 | '`export_only`: will take the latest checkpoint inside ' |
64 | | - 'model_dir and export a `SavedModel`.') |
| 65 | + 'model_dir and export a `SavedModel`.' |
| 66 | + '`sm_predict`: will load SavedModel from savedmodel_dir and predict answers' |
| 67 | + '`trt_predict`: will load SavedModel from savedmodel_dir, convert and predict answers with TF-TRT') |
65 | 68 | flags.DEFINE_string('train_data_path', '', |
66 | 69 | 'Training data path with train tfrecords.') |
67 | 70 | flags.DEFINE_string( |
|
101 | 104 | 'sp_model_file', None, |
102 | 105 | 'The path to the sentence piece model. Used by sentence piece tokenizer ' |
103 | 106 | 'employed by ALBERT.') |
| 107 | +flags.DEFINE_string( |
| 108 | + 'savedmodel_dir', None, |
| 109 | + 'The path of SavedModel for Savedmodel and TF-TRT prediction.') |
104 | 110 |
|
105 | 111 | common_flags.define_common_bert_flags() |
106 | 112 |
|
@@ -194,18 +200,25 @@ def predict_squad_customized(strategy, input_meta_data, bert_config, |
194 | 200 | else: |
195 | 201 | predict_iterator = iter(predict_dataset_fn()) |
196 | 202 |
|
197 | | - with distribution_utils.get_strategy_scope(strategy): |
198 | | - squad_model, _ = bert_models.squad_model( |
199 | | - bert_config, input_meta_data['max_seq_length'], float_type=tf.float16 if FLAGS.use_fp16 else tf.float32) |
| 203 | + if FLAGS.mode == 'trt_predict': |
| 204 | + squad_model = tf_trt.TFTRTModel(FLAGS.savedmodel_dir, "amp" if FLAGS.use_fp16 else "fp32") |
200 | 205 |
|
201 | | - if FLAGS.init_checkpoint: |
202 | | - checkpoint = tf.train.Checkpoint(model=squad_model) |
203 | | - checkpoint.restore(FLAGS.init_checkpoint).expect_partial() |
| 206 | + elif FLAGS.mode == 'sm_predict': |
| 207 | + squad_model = tf_trt.SavedModel(FLAGS.savedmodel_dir, "amp" if FLAGS.use_fp16 else "fp32") |
| 208 | + |
| 209 | + else: |
| 210 | + with distribution_utils.get_strategy_scope(strategy): |
| 211 | + squad_model, _ = bert_models.squad_model( |
| 212 | + bert_config, input_meta_data['max_seq_length'], float_type=tf.float16 if FLAGS.use_fp16 else tf.float32) |
| 213 | + |
| 214 | + if FLAGS.init_checkpoint: |
| 215 | + checkpoint = tf.train.Checkpoint(model=squad_model) |
| 216 | + checkpoint.restore(FLAGS.init_checkpoint).expect_partial() |
204 | 217 |
|
205 | | - checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir) |
206 | | - logging.info('Restoring checkpoints from %s', checkpoint_path) |
207 | | - checkpoint = tf.train.Checkpoint(model=squad_model) |
208 | | - checkpoint.restore(checkpoint_path).expect_partial() |
| 218 | + checkpoint_path = tf.train.latest_checkpoint(FLAGS.model_dir) |
| 219 | + logging.info('Restoring checkpoints from %s', checkpoint_path) |
| 220 | + checkpoint = tf.train.Checkpoint(model=squad_model) |
| 221 | + checkpoint.restore(checkpoint_path).expect_partial() |
209 | 222 |
|
210 | 223 | @tf.function |
211 | 224 | def predict_step(iterator): |
@@ -621,12 +634,13 @@ def main(_): |
621 | 634 | policy = tf.keras.mixed_precision.experimental.Policy("mixed_float16") |
622 | 635 | tf.keras.mixed_precision.experimental.set_policy(policy) |
623 | 636 |
|
| 637 | + os.makedirs(FLAGS.model_dir, exist_ok=True) |
624 | 638 | dllogging = dllogger_class.dllogger_class(FLAGS.dllog_path) |
625 | 639 | input_meta_data['dllogging'] = dllogging |
626 | 640 |
|
627 | 641 | if FLAGS.mode in ('train', 'train_and_predict'): |
628 | 642 | train_squad(strategy, input_meta_data) |
629 | | - if FLAGS.mode in ('predict', 'train_and_predict') and (not FLAGS.use_horovod or hvd.rank() == 0): |
| 643 | + if FLAGS.mode in ('predict', 'sm_predict', 'trt_predict', 'train_and_predict') and (not FLAGS.use_horovod or hvd.rank() == 0): |
630 | 644 | predict_squad(strategy, input_meta_data) |
631 | 645 |
|
632 | 646 |
|
|
0 commit comments