module.py 12.4 KB
Newer Older
Z
Zeyu Chen 已提交
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.

Z
Zeyu Chen 已提交
15 16
# coding=utf-8

Z
Zeyu Chen 已提交
17 18 19 20 21
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import paddle.fluid as fluid
Z
Zeyu Chen 已提交
22
import numpy as np
Z
Zeyu Chen 已提交
23 24
import tempfile
import os
Z
Zeyu Chen 已提交
25

Z
Zeyu Chen 已提交
26
from collections import defaultdict
Z
Zeyu Chen 已提交
27
from paddle_hub.downloader import download_and_uncompress
Z
Zeyu Chen 已提交
28
from paddle_hub import module_desc_pb2
Z
Zeyu Chen 已提交
29

Z
Zeyu Chen 已提交
30
__all__ = ["Module", "ModuleConfig", "ModuleUtils"]
Z
Zeyu Chen 已提交
31
DICT_NAME = "dict.txt"
Z
Zeyu Chen 已提交
32
ASSETS_NAME = "assets"
Z
Zeyu Chen 已提交
33 34 35 36 37 38 39


def mkdir(path):
    """ the same as the shell command mkdir -p "
    """
    if not os.path.exists(path):
        os.makedirs(path)
Z
Zeyu Chen 已提交
40

Z
Zeyu Chen 已提交
41

Z
Zeyu Chen 已提交
42
class Module(object):
Z
Zeyu Chen 已提交
43 44 45 46
    """
    A module represents a
    """

Z
Zeyu Chen 已提交
47 48 49
    def __init__(self, module_url=None, module_dir=None):
        if module_url == None and module_dir == None:
            raise Exception("Module:module_url and module_dir are None!")
Z
Zeyu Chen 已提交
50 51 52

        self.module_dir = ""
        self.module_name = ""
Z
Zeyu Chen 已提交
53
        # donwload module
Z
Zeyu Chen 已提交
54
        if module_url is not None and module_url.startswith("http"):
Z
Zeyu Chen 已提交
55
            # if it's remote url link, then download and uncompress it
Z
Zeyu Chen 已提交
56 57
            self.module_name, self.module_dir = download_and_uncompress(
                module_url)
Z
Zeyu Chen 已提交
58
            #TODO(ZeyuChen): check url link is valid url
Z
Zeyu Chen 已提交
59
        elif module_dir is not None:
Z
Zeyu Chen 已提交
60
            # otherwise it's local path, no need to deal with it
Z
Zeyu Chen 已提交
61
            self.module_dir = module_dir
Z
Zeyu Chen 已提交
62
            # use the path name as module name by default
Z
Zeyu Chen 已提交
63
            self.module_name = module_dir.split("/")[-1]
Z
Zeyu Chen 已提交
64
            #TODO(ZeyuChen) add more check about loading module from local path
Z
Zeyu Chen 已提交
65 66 67

        # load paddle inference model
        place = fluid.CPUPlace()
Z
Zeyu Chen 已提交
68 69
        model_dir = os.path.join(self.module_dir, "model")
        print("model_dir", model_dir)
Z
Zeyu Chen 已提交
70 71 72
        self.exe = fluid.Executor(fluid.CPUPlace())
        [self.inference_program, self.feed_target_names,
         self.fetch_targets] = fluid.io.load_inference_model(
Z
Zeyu Chen 已提交
73
             dirname=model_dir, executor=self.exe)
Z
Zeyu Chen 已提交
74 75 76 77 78 79 80 81

        print("inference_program")
        print(self.inference_program)
        print("feed_target_names")
        print(self.feed_target_names)
        print("fetch_targets")
        print(self.fetch_targets)

Z
Zeyu Chen 已提交
82 83
        self.config = ModuleConfig(self.module_dir)
        self.config.load()
Z
Zeyu Chen 已提交
84

Z
Zeyu Chen 已提交
85 86
    def _construct_feed_dict(self, inputs):
        """ Construct feed dict according to user's inputs and module config.
Z
Zeyu Chen 已提交
87
        """
Z
Zeyu Chen 已提交
88 89 90 91
        feed_dict = {}
        for k in inputs:
            if k in self.feed_target_names:
                feed_dict[k] = inputs[k]
Z
Zeyu Chen 已提交
92

Z
Zeyu Chen 已提交
93 94 95 96 97 98 99 100
        return feed_dict

    def __call__(self, inputs=None, sign_name="default"):
        """ Call default signature and return results
        """
        # word_ids_lod_tensor = self._preprocess_input(inputs)
        feed_dict = self._construct_feed_dict(inputs)
        print("feed_dict", feed_dict)
Z
Zeyu Chen 已提交
101

Z
Zeyu Chen 已提交
102 103
        ret_numpy = self.config.return_numpy()
        print("ret_numpy", ret_numpy)
Z
Zeyu Chen 已提交
104 105
        results = self.exe.run(
            self.inference_program,
Z
Zeyu Chen 已提交
106 107
            #feed={self.feed_target_names[0]: word_ids_lod_tensor},
            feed=feed_dict,
Z
Zeyu Chen 已提交
108
            fetch_list=self.fetch_targets,
Z
Zeyu Chen 已提交
109
            return_numpy=ret_numpy)
Z
Zeyu Chen 已提交
110

Z
Zeyu Chen 已提交
111 112 113
        print("module fetch_target_names", self.feed_target_names)
        print("module fetch_targets", self.fetch_targets)
        np_result = np.array(results[0])
Z
Zeyu Chen 已提交
114 115 116 117

        return np_result

    def get_vars(self):
Z
Zeyu Chen 已提交
118 119 120
        """
        Return variable list of the module program
        """
Z
Zeyu Chen 已提交
121 122
        return self.inference_program.list_vars()

Z
Zeyu Chen 已提交
123
    def get_feed_var(self, key, signature="default"):
Z
Zeyu Chen 已提交
124 125 126
        """
        Get feed variable according to variable key and signature
        """
Z
Zeyu Chen 已提交
127
        for var in self.inference_program.list_vars():
Z
Zeyu Chen 已提交
128
            if var.name == self.config.feed_var_name(key, signature):
Z
Zeyu Chen 已提交
129 130
                return var

Z
Zeyu Chen 已提交
131 132
        raise Exception("Can't find input var {}".format(key))

Z
Zeyu Chen 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    def get_feed_var_by_index(self, index, signature="default"):
        feed_vars = self.get_feed_vars(signature)
        assert index < len(
            feed_vars), "index out of range index {}, len {}".format(
                index, len(feed_vars))
        return feed_vars[index]

    def get_fetch_var_by_index(self, index, signature="default"):
        fetch_vars = self.get_fetch_vars(signature)
        assert index < len(
            fetch_vars), "index out of range index {}, len {}".format(
                index, len(fetch_vars))
        return fetch_vars[index]

    def get_feed_vars(self, signature="default"):
        """
        Get feed variable according to variable key and signature
        """
        feed_vars = []
        for feed_var in self.config.feed_var_names(signature):
            find_var = False
            for var in self.inference_program.list_vars():
                if var.name == feed_var.var_name:
                    feed_vars.append(var)
                    find_var = True
            if not find_var:
                raise Exception("Can't find feed var {}".format(feed_var_name))

        return feed_vars

    def get_fetch_vars(self, signature="default"):
        """
        Get feed variable according to variable key and signature
        """
        fetch_vars = []
        #TODO(ZeyuChen): use brute force to find variables, simple and easy to
        #understand
        for fetch_var in self.config.fetch_var_names(signature):
            find_var = False
            for var in self.inference_program.list_vars():
                if var.name == fetch_var.var_name:
                    fetch_vars.append(var)
                    find_var = True
            if not find_var:
                raise Exception("Can't find feed var {}".format(fetch_var_name))

        return fetch_vars

Z
Zeyu Chen 已提交
181
    def get_fetch_var(self, key, signature="default"):
Z
Zeyu Chen 已提交
182 183 184
        """
        Get fetch variable according to variable key and signature
        """
Z
Zeyu Chen 已提交
185
        for var in self.inference_program.list_vars():
Z
Zeyu Chen 已提交
186
            if var.name == self.config.fetch_var_name(key, signature):
Z
Zeyu Chen 已提交
187 188 189 190 191 192
                return var

    def get_inference_program(self):
        return self.inference_program

    # for text sequence input, transform to lod tensor as paddle graph's input
Z
Zeyu Chen 已提交
193
    def _preprocess_input(self, inputs):
Z
Zeyu Chen 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        # words id mapping and dealing with oov
        # transform to lod tensor
        seq = []
        for s in inputs:
            seq.append(self._word_id_mapping(s))

        lod_tensor = self.seq2lod_tensor(seq)

        return lod_tensor

    def seq2lod_tensor(self, seq_inputs, place=fluid.CPUPlace()):
        """ sequence to lod tensor, need to determine which space"""
        lod = []
        lod.append([])
        for s in seq_inputs:
            # generate lod
            lod[0].append(len(s))

        # print("seq", seq_inputs)
        # print("lod", lod)

        lod_tensor = fluid.create_lod_tensor(seq_inputs, lod, place)

        return lod_tensor

    def _word_id_mapping(self, inputs):
Z
Zeyu Chen 已提交
220 221 222
        word_dict = self.config.get_dict()
        return list(map(lambda x: word_dict[x], inputs))

Z
Zeyu Chen 已提交
223

Z
Zeyu Chen 已提交
224
class ModuleConfig(object):
Z
Zeyu Chen 已提交
225
    def __init__(self, module_dir, module_name=None):
Z
Zeyu Chen 已提交
226 227
        # generate model desc protobuf
        self.module_dir = module_dir
Z
Zeyu Chen 已提交
228 229 230
        self.desc = module_desc_pb2.ModuleDesc()
        if module_name == None:
            module_name = module_dir.split("/")[-1]
Z
Zeyu Chen 已提交
231
        # initialize module config default value
Z
Zeyu Chen 已提交
232 233
        self.desc.name = module_name
        self.desc.contain_assets = True
Z
Zeyu Chen 已提交
234
        self.desc.return_numpy = False
Z
Zeyu Chen 已提交
235

Z
Zeyu Chen 已提交
236 237 238 239
        # init dict
        self.dict = defaultdict(int)
        self.dict.setdefault(0)

Z
Zeyu Chen 已提交
240 241 242 243
    def get_dict(self):
        """ Return dictionary in Module"""
        return self.dict

244
    def load(self):
Z
Zeyu Chen 已提交
245 246
        """
        Load module config from module directory.
Z
Zeyu Chen 已提交
247
        """
Z
Zeyu Chen 已提交
248
        #TODO(ZeyuChen): check module_desc.pb exsitance
249 250
        pb_path = os.path.join(self.module_dir, "module_desc.pb")
        with open(pb_path, "rb") as fi:
Z
Zeyu Chen 已提交
251 252
            self.desc.ParseFromString(fi.read())

Z
Zeyu Chen 已提交
253 254 255
        print("self.desc.sign2var",
              self.desc.sign2var["default"].feed_desc[0].var_name)

Z
Zeyu Chen 已提交
256 257
        if self.desc.contain_assets:
            # load assets
Z
Zeyu Chen 已提交
258
            assets_dir = os.path.join(self.module_dir, ASSETS_NAME)
Z
Zeyu Chen 已提交
259 260 261 262 263 264 265 266 267 268
            dict_path = os.path.join(assets_dir, DICT_NAME)
            word_id = 0

            with open(dict_path) as fi:
                words = fi.readlines()
                #TODO(ZeyuChen) check whether word id is duplicated and valid
                for line in fi:
                    w, w_id = line.split()
                    self.dict[w] = int(w_id)

Z
Zeyu Chen 已提交
269
    def dump(self):
Z
Zeyu Chen 已提交
270
        """ Save Module configure file to disk.
271
        """
Z
Zeyu Chen 已提交
272
        pb_path = os.path.join(self.module_dir, "module_desc.pb")
Z
Zeyu Chen 已提交
273 274 275 276
        with open(pb_path, "wb") as fo:
            fo.write(self.desc.SerializeToString())

        # save assets/dictionary
Z
Zeyu Chen 已提交
277
        assets_dir = os.path.join(self.module_dir, ASSETS_NAME)
Z
Zeyu Chen 已提交
278 279
        mkdir(assets_dir)
        with open(os.path.join(assets_dir, DICT_NAME), "w") as fo:
Z
Zeyu Chen 已提交
280 281
            for w in self.dict:
                w_id = self.dict[w]
Z
Zeyu Chen 已提交
282
                fo.write("{}\t{}\n".format(w, w_id))
Z
Zeyu Chen 已提交
283

Z
Zeyu Chen 已提交
284 285 286 287 288
    def return_numpy(self):
        """Return numpy or not according to the proto config.
        """
        return self.desc.return_numpy

Z
Zeyu Chen 已提交
289
    def save_dict(self, word_dict, dict_name=DICT_NAME):
Z
Zeyu Chen 已提交
290 291
        """ Save dictionary for NLP module
        """
Z
Zeyu Chen 已提交
292 293
        for w in word_dict:
            self.dict[w] = word_dict[w]
Z
Zeyu Chen 已提交
294

Z
Zeyu Chen 已提交
295
    def register_feed_signature(self, feed_desc, sign_name="default"):
Z
Zeyu Chen 已提交
296 297 298 299 300 301
        """ Register feed signature to the Module

        Args:
            fetch_desc: a dictionary of signature to input variable
            sign_name: signature name, use "default" as default signature
        """
Z
Zeyu Chen 已提交
302 303 304 305 306 307 308
        #TODO(ZeyuChen) check fetch_desc key is valid and no duplicated
        for k in feed_desc:
            feed = self.desc.sign2var[sign_name].feed_desc.add()
            feed.key = k
            feed.var_name = feed_desc[k]

    def register_fetch_signature(self, fetch_desc, sign_name="default"):
Z
Zeyu Chen 已提交
309 310 311 312 313 314
        """ Register fetch signature to the Module

        Args:
            fetch_desc: a dictionary of signature to input variable
            sign_name: signature name, use "default" as default signature
        """
Z
Zeyu Chen 已提交
315 316 317 318 319 320
        #TODO(ZeyuChen) check fetch_desc key is valid and no duplicated
        for k in fetch_desc:
            fetch = self.desc.sign2var[sign_name].fetch_desc.add()
            fetch.key = k
            fetch.var_name = fetch_desc[k]

Z
Zeyu Chen 已提交
321 322 323 324 325 326
    def feed_var_names(self, sign_name="default"):
        return self.desc.sign2var[sign_name].feed_desc

    def fetch_var_names(self, sign_name="default"):
        return self.desc.sign2var[sign_name].fetch_desc

Z
Zeyu Chen 已提交
327
    def feed_var_name(self, key, sign_name="default"):
Z
Zeyu Chen 已提交
328 329
        """get module's feed/input variable name
        """
Z
Zeyu Chen 已提交
330 331 332 333 334 335
        for desc in self.desc.sign2var[sign_name].feed_desc:
            if desc.key == key:
                return desc.var_name
        raise Exception("feed variable {} not found".format(key))

    def fetch_var_name(self, key, sign_name="default"):
Z
Zeyu Chen 已提交
336 337
        """get module's fetch/output variable name
        """
Z
Zeyu Chen 已提交
338 339 340 341 342
        for desc in self.desc.sign2var[sign_name].fetch_desc:
            if desc.key == key:
                return desc.var_name
        raise Exception("fetch variable {} not found".format(key))

Z
Zeyu Chen 已提交
343 344 345

class ModuleUtils(object):
    def __init__(self):
Z
Zeyu Chen 已提交
346
        pass
Z
Zeyu Chen 已提交
347

Z
Zeyu Chen 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    @staticmethod
    def remove_feed_fetch_op(program):
        """ remove feed and fetch operator and variable for fine-tuning
        """
        print("remove feed fetch op")
        block = program.global_block()
        need_to_remove_op_index = []
        for i, op in enumerate(block.ops):
            if op.type == "feed" or op.type == "fetch":
                need_to_remove_op_index.append(i)

        for index in need_to_remove_op_index[::-1]:
            block._remove_op(index)

        block._remove_var("feed")
        block._remove_var("fetch")

        program.desc.flush()
Z
Zeyu Chen 已提交
366 367 368
        # print("********************************")
        # print(program)
        # print("********************************")