inference_model.py 3.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 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.

15 16 17 18 19 20 21 22 23 24
import logging
import os
import six
import sys
import time

import numpy as np
import paddle
import paddle.fluid as fluid

G
Guo Sheng 已提交
25 26
from utils.input_field import InputField
from utils.configure import PDConfig
27
from utils.load import load
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

# include task-specific libs
import desc
import reader
from transformer import create_net


def do_save_inference_model(args):
    if args.use_cuda:
        dev_count = fluid.core.get_cuda_device_count()
        place = fluid.CUDAPlace(0)
    else:
        dev_count = int(os.environ.get('CPU_NUM', 1))
        place = fluid.CPUPlace()

43 44 45 46 47
    src_vocab = reader.DataProcessor.load_dict(args.src_vocab_fpath)
    trg_vocab = reader.DataProcessor.load_dict(args.trg_vocab_fpath)
    args.src_vocab_size = len(src_vocab)
    args.trg_vocab_size = len(trg_vocab)

48 49 50 51 52 53 54 55 56
    test_prog = fluid.default_main_program()
    startup_prog = fluid.default_startup_program()

    with fluid.program_guard(test_prog, startup_prog):
        with fluid.unique_name.guard():

            # define input and reader

            input_field_names = desc.encoder_data_input_fields + desc.fast_decoder_data_input_fields
57
            input_descs = desc.get_input_descs(args.args)
58 59
            input_slots = [{
                "name": name,
60 61
                "shape": input_descs[name][0],
                "dtype": input_descs[name][1]
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            } for name in input_field_names]

            input_field = InputField(input_slots)
            input_field.build(build_pyreader=True)

            # define the network

            predictions = create_net(
                is_training=False, model_input=input_field, args=args)
            out_ids, out_scores = predictions

    # This is used here to set dropout to the test mode.
    test_prog = test_prog.clone(for_test=True)

    # prepare predicting

    ## define the executor and program for training

    exe = fluid.Executor(place)

    exe.run(startup_prog)
83 84 85 86
    assert (
        args.init_from_params), "must set init_from_params to load parameters"
    load(test_prog, os.path.join(args.init_from_params, "transformer"), exe)
    print("finish initing model from params from %s" % (args.init_from_params))
87 88 89

    # saving inference model

90 91 92 93 94 95 96
    fluid.io.save_inference_model(args.inference_model_dir,
                                  feeded_var_names=list(input_field_names),
                                  target_vars=[out_ids, out_scores],
                                  executor=exe,
                                  main_program=test_prog,
                                  model_filename="model.pdmodel",
                                  params_filename="params.pdparams")
97 98 99 100 101 102 103 104 105 106

    print("save inference model at %s" % (args.inference_model_dir))


if __name__ == "__main__":
    args = PDConfig(yaml_file="./transformer.yaml")
    args.build()
    args.Print()

    do_save_inference_model(args)