|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import os |
| 15 | +from attrdict import AttrDict |
| 16 | +import argparse |
| 17 | +import time |
| 18 | + |
| 19 | +import yaml |
| 20 | +from pprint import pprint |
| 21 | +import paddle |
| 22 | + |
| 23 | +from paddlenlp.ops import FasterDecoder |
| 24 | +from paddlenlp.utils.log import logger |
| 25 | + |
| 26 | + |
| 27 | +def parse_args(): |
| 28 | + parser = argparse.ArgumentParser() |
| 29 | + parser.add_argument( |
| 30 | + "--config", |
| 31 | + default="./config/decoder.sample.yaml", |
| 32 | + type=str, |
| 33 | + help="Path of the config file. ") |
| 34 | + parser.add_argument( |
| 35 | + "--decoder_lib", |
| 36 | + default="../../build/lib/libdecoder_op.so", |
| 37 | + type=str, |
| 38 | + help="Path of libdecoder_op.so. ") |
| 39 | + parser.add_argument( |
| 40 | + "--use_fp16_decoder", |
| 41 | + action="store_true", |
| 42 | + help="Whether to use fp16 decoder to predict. ") |
| 43 | + args = parser.parse_args() |
| 44 | + return args |
| 45 | + |
| 46 | + |
| 47 | +def do_predict(args): |
| 48 | + place = "gpu" |
| 49 | + paddle.set_device(place) |
| 50 | + |
| 51 | + # Define model |
| 52 | + transformer = FasterDecoder( |
| 53 | + src_vocab_size=args.src_vocab_size, |
| 54 | + trg_vocab_size=args.trg_vocab_size, |
| 55 | + max_length=args.max_length + 1, |
| 56 | + num_encoder_layers=args.num_encoder_layers, |
| 57 | + num_decoder_layers=args.num_decoder_layers, |
| 58 | + n_head=args.n_head, |
| 59 | + d_model=args.d_model, |
| 60 | + d_inner_hid=args.d_inner_hid, |
| 61 | + dropout=args.dropout, |
| 62 | + weight_sharing=args.weight_sharing, |
| 63 | + bos_id=args.bos_idx, |
| 64 | + eos_id=args.eos_idx, |
| 65 | + max_out_len=args.max_out_len, |
| 66 | + decoder_lib=args.decoder_lib, |
| 67 | + use_fp16_decoder=args.use_fp16_decoder) |
| 68 | + |
| 69 | + # Load checkpoint. |
| 70 | + transformer.load( |
| 71 | + os.path.join(args.init_from_params, "transformer.pdparams")) |
| 72 | + # Set evaluate mode |
| 73 | + transformer.eval() |
| 74 | + |
| 75 | + # Generate data randomly |
| 76 | + dec_input = paddle.randn( |
| 77 | + shape=[args.infer_batch_size, 1, args.d_model], dtype='float32') |
| 78 | + enc_output = paddle.randn( |
| 79 | + shape=[args.infer_batch_size, args.max_length, args.d_model], |
| 80 | + dtype='float32') |
| 81 | + mem_seq_lens = paddle.full( |
| 82 | + shape=[args.infer_batch_size, 1], |
| 83 | + fill_value=args.max_length, |
| 84 | + dtype='int32') |
| 85 | + dtype = 'float32' |
| 86 | + if args.use_fp16_decoder: |
| 87 | + dtype = 'float16' |
| 88 | + dec_input = paddle.cast(dec_input, dtype=dtype) |
| 89 | + enc_output = paddle.cast(enc_output, dtype=dtype) |
| 90 | + self_cache = paddle.zeros( |
| 91 | + shape=[ |
| 92 | + args.num_decoder_layers, 2, 0, args.infer_batch_size, args.d_model |
| 93 | + ], |
| 94 | + dtype=dtype) |
| 95 | + mem_cache = paddle.zeros( |
| 96 | + shape=[ |
| 97 | + args.num_decoder_layers, 2, args.infer_batch_size, args.max_length, |
| 98 | + args.d_model |
| 99 | + ], |
| 100 | + dtype=dtype) |
| 101 | + |
| 102 | + with paddle.no_grad(): |
| 103 | + for i in range(100): |
| 104 | + # For warmup. |
| 105 | + if 50 == i: |
| 106 | + start = time.time() |
| 107 | + dec_output, self_cache, mem_cache = transformer.decoder( |
| 108 | + from_tensor=dec_input, |
| 109 | + memory_tensor=enc_output, |
| 110 | + mem_seq_len=mem_seq_lens, |
| 111 | + self_cache=self_cache, |
| 112 | + mem_cache=mem_cache) |
| 113 | + logger.info("Average test time for decoder is %f ms" % ( |
| 114 | + (time.time() - start) / 50 * 1000)) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + ARGS = parse_args() |
| 119 | + yaml_file = ARGS.config |
| 120 | + with open(yaml_file, 'rt') as f: |
| 121 | + args = AttrDict(yaml.safe_load(f)) |
| 122 | + args.decoder_lib = ARGS.decoder_lib |
| 123 | + args.use_fp16_decoder = ARGS.use_fp16_decoder |
| 124 | + pprint(args) |
| 125 | + |
| 126 | + do_predict(args) |
0 commit comments