module.py 28.6 KB
Newer Older
S
Steffy-zxf 已提交
1
#coding:utf-8
W
wuzewu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
W
wuzewu 已提交
19 20 21 22 23 24 25 26 27 28

import os
import time
import sys
import functools
from shutil import copyfile

import paddle
import paddle.fluid as fluid

W
wuzewu 已提交
29 30 31
from paddlehub.common import utils
from paddlehub.common import paddle_helper
from paddlehub.common.logger import logger
S
shenyuhan 已提交
32
from paddlehub.common.lock import lock
W
wuzewu 已提交
33 34
from paddlehub.common.downloader import default_downloader
from paddlehub.module import module_desc_pb2
S
shenyuhan 已提交
35
from paddlehub.common.dir import CONF_HOME
W
wuzewu 已提交
36
from paddlehub.module import check_info_pb2
37
from paddlehub.common.hub_server import CacheUpdater
W
wuzewu 已提交
38 39 40 41
from paddlehub.module.signature import Signature, create_signature
from paddlehub.module.checker import ModuleChecker
from paddlehub.module.manager import default_module_manager
from paddlehub.module.base_processor import BaseProcessor
W
wuzewu 已提交
42
from paddlehub.io.parser import yaml_parser
W
wuzewu 已提交
43
from paddlehub import version
W
wuzewu 已提交
44 45 46

__all__ = ['Module', 'create_module']

Z
Zeyu Chen 已提交
47
# PaddleHub module dir name
W
wuzewu 已提交
48 49 50 51 52
ASSETS_DIRNAME = "assets"
MODEL_DIRNAME = "model"
MODULE_DESC_PBNAME = "module_desc.pb"
PYTHON_DIR = "python"
PROCESSOR_NAME = "processor"
Z
Zeyu Chen 已提交
53
# PaddleHub var prefix
W
wuzewu 已提交
54 55
HUB_VAR_PREFIX = "@HUB_%s@"

W
wuzewu 已提交
56

W
wuzewu 已提交
57 58
def create_module(sign_arr,
                  module_dir,
W
wuzewu 已提交
59
                  processor=None,
W
wuzewu 已提交
60 61
                  assets=None,
                  module_info=None,
W
wuzewu 已提交
62 63
                  exe=None,
                  extra_info=None):
W
wuzewu 已提交
64
    sign_arr = utils.to_list(sign_arr)
W
wuzewu 已提交
65 66 67 68
    module = Module(
        signatures=sign_arr,
        processor=processor,
        assets=assets,
W
wuzewu 已提交
69 70
        module_info=module_info,
        extra_info=extra_info)
W
wuzewu 已提交
71 72 73
    module.serialize_to_path(path=module_dir, exe=exe)


74
class ModuleHelper(object):
W
wuzewu 已提交
75 76 77 78 79 80 81 82 83
    def __init__(self, module_dir):
        self.module_dir = module_dir

    def module_desc_path(self):
        return os.path.join(self.module_dir, MODULE_DESC_PBNAME)

    def model_path(self):
        return os.path.join(self.module_dir, MODEL_DIRNAME)

W
wuzewu 已提交
84 85 86 87 88 89 90 91 92
    def processor_path(self):
        return os.path.join(self.module_dir, PYTHON_DIR)

    def processor_name(self):
        return PROCESSOR_NAME

    def assets_path(self):
        return os.path.join(self.module_dir, ASSETS_DIRNAME)

W
wuzewu 已提交
93

94
class Module(object):
W
wuzewu 已提交
95
    def __init__(self,
W
wuzewu 已提交
96
                 name=None,
W
wuzewu 已提交
97 98 99 100
                 module_dir=None,
                 signatures=None,
                 module_info=None,
                 assets=None,
W
wuzewu 已提交
101
                 processor=None,
B
BinLong 已提交
102 103
                 extra_info=None,
                 version=None):
W
wuzewu 已提交
104 105 106 107 108
        self.desc = module_desc_pb2.ModuleDesc()
        self.program = None
        self.assets = []
        self.helper = None
        self.signatures = {}
W
wuzewu 已提交
109
        self.default_signature = None
W
wuzewu 已提交
110 111
        self.module_info = None
        self.processor = None
W
wuzewu 已提交
112 113 114 115
        self.extra_info = {} if extra_info is None else extra_info
        if not isinstance(self.extra_info, dict):
            raise TypeError(
                "The extra_info should be an instance of python dict")
W
wuzewu 已提交
116 117 118 119 120 121 122

        # cache data
        self.last_call_name = None
        self.cache_feed_dict = None
        self.cache_fetch_dict = None
        self.cache_program = None

S
shenyuhan 已提交
123
        fp_lock = open(os.path.join(CONF_HOME, 'config.json'))
S
shenyuhan 已提交
124
        lock.flock(fp_lock, lock.LOCK_EX)
W
wuzewu 已提交
125
        if name:
B
BinLong 已提交
126
            self._init_with_name(name=name, version=version)
S
shenyuhan 已提交
127
            lock.flock(fp_lock, lock.LOCK_UN)
W
wuzewu 已提交
128
        elif module_dir:
B
BinLong 已提交
129
            self._init_with_module_file(module_dir=module_dir[0])
S
shenyuhan 已提交
130
            lock.flock(fp_lock, lock.LOCK_UN)
131
            name = module_dir[0].split("/")[-1]
S
shenyuhan 已提交
132 133 134 135
            if len(module_dir) > 1:
                version = module_dir[1]
            else:
                version = default_module_manager.search_module(name)[1]
W
wuzewu 已提交
136
        elif signatures:
W
wuzewu 已提交
137
            if processor:
W
wuzewu 已提交
138 139
                if not issubclass(processor, BaseProcessor):
                    raise TypeError(
Z
Zeyu Chen 已提交
140
                        "Processor shoule be an instance of paddlehub.BaseProcessor"
W
wuzewu 已提交
141
                    )
W
wuzewu 已提交
142 143
            if assets:
                self.assets = utils.to_list(assets)
144 145
                # for asset in assets:
                #     utils.check_path(assets)
W
wuzewu 已提交
146 147
            self.processor = processor
            self._generate_module_info(module_info)
W
wuzewu 已提交
148
            self._init_with_signature(signatures=signatures)
S
shenyuhan 已提交
149
            lock.flock(fp_lock, lock.LOCK_UN)
W
wuzewu 已提交
150
        else:
S
shenyuhan 已提交
151
            lock.flock(fp_lock, lock.LOCK_UN)
Z
Zeyu Chen 已提交
152
            raise ValueError("Module initialized parameter is empty")
153
        CacheUpdater(name, version).start()
W
wuzewu 已提交
154

B
BinLong 已提交
155 156 157 158 159
    def _init_with_name(self, name, version=None):
        log_msg = "Installing %s module" % name
        if version:
            log_msg += "-%s" % version
        logger.info(log_msg)
S
shenyuhan 已提交
160
        extra = {"command": "install"}
W
wuzewu 已提交
161
        result, tips, module_dir = default_module_manager.install_module(
S
shenyuhan 已提交
162
            module_name=name, module_version=version, extra=extra)
W
wuzewu 已提交
163 164
        if not result:
            logger.error(tips)
走神的阿圆's avatar
走神的阿圆 已提交
165 166 167 168
            raise RuntimeError(tips)
        else:
            logger.info(tips)
            self._init_with_module_file(module_dir[0])
W
wuzewu 已提交
169

W
wuzewu 已提交
170
    def _init_with_url(self, url):
W
wuzewu 已提交
171
        utils.check_url(url)
W
wuzewu 已提交
172
        result, tips, module_dir = default_downloader.download_file_and_uncompress(
W
wuzewu 已提交
173
            url, save_path=".")
W
wuzewu 已提交
174 175
        if not result:
            logger.error(tips)
走神的阿圆's avatar
走神的阿圆 已提交
176 177 178
            raise RuntimeError(tips)
        else:
            self._init_with_module_file(module_dir)
W
wuzewu 已提交
179

W
wuzewu 已提交
180 181 182 183 184
    def _dump_processor(self):
        import inspect
        pymodule = inspect.getmodule(self.processor)
        pycode = inspect.getsource(pymodule)
        processor_path = self.helper.processor_path()
185 186 187
        processor_md5 = utils.md5(pycode)
        processor_md5 += str(time.time())
        processor_name = utils.md5(processor_md5)
W
wuzewu 已提交
188 189 190 191
        output_file = os.path.join(processor_path, processor_name + ".py")
        utils.mkdir(processor_path)
        with open(output_file, "w") as file:
            file.write(pycode)
W
wuzewu 已提交
192 193
        utils.from_pyobj_to_module_attr(
            processor_name, self.desc.attr.map.data['processor_info'])
W
wuzewu 已提交
194

W
wuzewu 已提交
195 196
    def _load_processor(self):
        processor_path = self.helper.processor_path()
W
wuzewu 已提交
197 198
        if os.path.exists(processor_path):
            sys.path.append(processor_path)
W
wuzewu 已提交
199 200
            processor_name = utils.from_module_attr_to_pyobj(
                self.desc.attr.map.data['processor_info'])
W
wuzewu 已提交
201 202 203
            self.processor = __import__(processor_name).Processor(module=self)
        else:
            self.processor = None
W
wuzewu 已提交
204

W
wuzewu 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218
    def _dump_assets(self):
        utils.mkdir(self.helper.assets_path())
        for asset in self.assets:
            filename = os.path.basename(asset)
            newfile = os.path.join(self.helper.assets_path(), filename)
            copyfile(asset, newfile)

    def _load_assets(self):
        assets_path = self.helper.assets_path()
        self.assets = []
        for file in os.listdir(assets_path):
            filepath = os.path.join(self.helper.assets_path(), file)
            self.assets.append(filepath)

W
wuzewu 已提交
219
    def _init_with_module_file(self, module_dir):
W
wuzewu 已提交
220
        checker = ModuleChecker(module_dir)
W
wuzewu 已提交
221
        checker.check()
W
wuzewu 已提交
222

W
wuzewu 已提交
223 224 225 226 227 228 229
        self.helper = ModuleHelper(module_dir)
        with open(self.helper.module_desc_path(), "rb") as fi:
            self.desc.ParseFromString(fi.read())

        exe = fluid.Executor(fluid.CPUPlace())
        self.program, _, _ = fluid.io.load_inference_model(
            self.helper.model_path(), executor=exe)
W
wuzewu 已提交
230 231 232 233
        for block in self.program.blocks:
            for op in block.ops:
                if "op_callstack" in op.all_attrs():
                    op._set_attr("op_callstack", [""])
W
wuzewu 已提交
234
        self._load_processor()
W
wuzewu 已提交
235
        self._load_assets()
W
wuzewu 已提交
236
        self._recover_from_desc()
W
wuzewu 已提交
237
        self._generate_sign_attr()
W
wuzewu 已提交
238
        self._generate_extra_info()
Z
Zeyu Chen 已提交
239
        self._restore_parameter(self.program)
W
wuzewu 已提交
240
        self._recover_variable_info(self.program)
W
wuzewu 已提交
241 242

    def _init_with_signature(self, signatures):
W
wuzewu 已提交
243
        self.name_prefix = HUB_VAR_PREFIX % self.name
W
wuzewu 已提交
244 245 246 247
        self._process_signatures(signatures)
        self._check_signatures()
        self._generate_desc()
        self._generate_sign_attr()
W
wuzewu 已提交
248
        self._generate_extra_info()
W
wuzewu 已提交
249 250 251 252 253 254 255 256 257

    def _init_with_program(self, program):
        pass

    def _process_signatures(self, signatures):
        self.signatures = {}
        self.program = signatures[0].inputs[0].block.program
        for sign in signatures:
            if sign.name in self.signatures:
Z
Zeyu Chen 已提交
258 259 260
                raise ValueError(
                    "Error! Signature array contains duplicated signatrues %s" %
                    sign)
W
wuzewu 已提交
261 262
            if self.default_signature is None and sign.for_predict:
                self.default_signature = sign
W
wuzewu 已提交
263 264
            self.signatures[sign.name] = sign

Z
Zeyu Chen 已提交
265
    def _restore_parameter(self, program):
W
wuzewu 已提交
266
        global_block = program.global_block()
W
wuzewu 已提交
267
        param_attrs = self.desc.attr.map.data['param_attrs']
W
wuzewu 已提交
268
        for key, param_attr in param_attrs.map.data.items():
W
wuzewu 已提交
269
            param = paddle_helper.from_module_attr_to_param(param_attr)
270
            param['name'] = self.get_var_name_with_prefix(key)
W
wuzewu 已提交
271 272 273 274 275 276 277 278 279 280
            if (param['name'] not in global_block.vars):
                continue
            var = global_block.var(param['name'])
            global_block.create_parameter(
                shape=var.shape,
                dtype=var.dtype,
                type=var.type,
                lod_level=var.lod_level,
                error_clip=var.error_clip,
                stop_gradient=var.stop_gradient,
W
wuzewu 已提交
281 282
                is_data=var.is_data,
                **param)
W
wuzewu 已提交
283 284

    def _recover_variable_info(self, program):
W
wuzewu 已提交
285
        var_infos = self.desc.attr.map.data['var_infos']
W
wuzewu 已提交
286
        for var_info in var_infos.map.data:
W
wuzewu 已提交
287
            idx = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
288
                var_infos.map.data[var_info].map.data['block_id'])
W
wuzewu 已提交
289
            stop_gradient = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
290 291
                var_infos.map.data[var_info].map.data['stop_gradient'])
            block = program.blocks[idx]
292
            var_name = self.get_var_name_with_prefix(var_info)
W
wuzewu 已提交
293 294 295 296
            if var_name in block.vars:
                var = block.vars[var_name]
                var.stop_gradient = stop_gradient

W
wuzewu 已提交
297 298 299 300 301 302 303 304
    def get_extra_info(self, key):
        return self.extra_info.get(key, None)

    def _generate_extra_info(self):
        for key in self.extra_info:
            self.__dict__["get_%s" % key] = functools.partial(
                self.get_extra_info, key=key)

W
wuzewu 已提交
305 306 307 308 309
    def _generate_module_info(self, module_info=None):
        if not module_info:
            self.module_info = {}
        else:
            if not utils.is_yaml_file(module_info):
Z
Zeyu Chen 已提交
310
                logger.critical("Module info file should be yaml format")
W
wuzewu 已提交
311
                exit(1)
W
wuzewu 已提交
312
            self.module_info = yaml_parser.parse(module_info)
313 314 315 316 317 318
        self.author = self.module_info.get('author', 'UNKNOWN')
        self.author_email = self.module_info.get('author_email', 'UNKNOWN')
        self.summary = self.module_info.get('summary', 'UNKNOWN')
        self.type = self.module_info.get('type', 'UNKNOWN')
        self.version = self.module_info.get('version', 'UNKNOWN')
        self.name = self.module_info.get('name', 'UNKNOWN')
W
wuzewu 已提交
319

W
wuzewu 已提交
320 321 322
    def _generate_sign_attr(self):
        self._check_signatures()
        for sign in self.signatures:
W
wuzewu 已提交
323 324
            self.__dict__[sign] = functools.partial(
                self.__call__, sign_name=sign)
W
wuzewu 已提交
325

326 327 328 329
    def get_vocab_path(self):
        for assets_file in self.assets:
            if "vocab.txt" in assets_file:
                return assets_file
K
kinghuin 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342
        return None

    def get_word_dict_path(self):
        for assets_file in self.assets:
            if "dict.wordseg.pickle" in assets_file:
                return assets_file
        return None

    def get_spm_path(self):
        for assets_file in self.assets:
            if "spm_cased_simp_sampled.model" in assets_file:
                return assets_file
        return None
343

W
wuzewu 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
    def _recover_from_desc(self):
        # recover signature
        for sign, module_var in self.desc.sign2var.items():
            inputs = []
            outputs = []
            feed_names = []
            fetch_names = []
            for var in module_var.feed_desc:
                variable = self.program.global_block().vars[var.var_name]
                inputs.append(variable)
                feed_names.append(var.alias)

            for var in module_var.fetch_desc:
                variable = self.program.global_block().vars[var.var_name]
                outputs.append(variable)
                fetch_names.append(var.alias)

            self.signatures[sign] = create_signature(
                sign,
                inputs=inputs,
                outputs=outputs,
                feed_names=feed_names,
                fetch_names=fetch_names)

W
wuzewu 已提交
368
        # recover default signature
W
wuzewu 已提交
369 370
        default_signature_name = utils.from_module_attr_to_pyobj(
            self.desc.attr.map.data['default_signature'])
W
wuzewu 已提交
371 372 373
        self.default_signature = self.signatures[
            default_signature_name] if default_signature_name else None

W
wuzewu 已提交
374
        # recover module info
W
wuzewu 已提交
375 376
        module_info = self.desc.attr.map.data['module_info']
        self.name = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
377
            module_info.map.data['name'])
W
wuzewu 已提交
378
        self.author = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
379
            module_info.map.data['author'])
W
wuzewu 已提交
380
        self.author_email = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
381
            module_info.map.data['author_email'])
W
wuzewu 已提交
382
        self.version = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
383
            module_info.map.data['version'])
W
wuzewu 已提交
384
        self.type = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
385
            module_info.map.data['type'])
W
wuzewu 已提交
386
        self.summary = utils.from_module_attr_to_pyobj(
W
wuzewu 已提交
387 388
            module_info.map.data['summary'])

W
wuzewu 已提交
389 390 391 392 393 394
        # recover extra info
        extra_info = self.desc.attr.map.data['extra_info']
        self.extra_info = {}
        for key, value in extra_info.map.data.items():
            self.extra_info[key] = utils.from_module_attr_to_pyobj(value)

W
wuzewu 已提交
395
        # recover name prefix
W
wuzewu 已提交
396 397
        self.name_prefix = utils.from_module_attr_to_pyobj(
            self.desc.attr.map.data["name_prefix"])
W
wuzewu 已提交
398

W
wuzewu 已提交
399 400
    def _generate_desc(self):
        # save fluid Parameter
W
wuzewu 已提交
401 402 403
        attr = self.desc.attr
        attr.type = module_desc_pb2.MAP
        param_attrs = attr.map.data['param_attrs']
W
wuzewu 已提交
404 405 406
        param_attrs.type = module_desc_pb2.MAP
        for param in self.program.global_block().iter_parameters():
            param_attr = param_attrs.map.data[param.name]
W
wuzewu 已提交
407
            paddle_helper.from_param_to_module_attr(param, param_attr)
W
wuzewu 已提交
408 409

        # save Variable Info
W
wuzewu 已提交
410
        var_infos = attr.map.data['var_infos']
W
wuzewu 已提交
411 412 413 414 415
        var_infos.type = module_desc_pb2.MAP
        for block in self.program.blocks:
            for var in block.vars.values():
                var_info = var_infos.map.data[var.name]
                var_info.type = module_desc_pb2.MAP
W
wuzewu 已提交
416
                utils.from_pyobj_to_module_attr(
W
wuzewu 已提交
417
                    var.stop_gradient, var_info.map.data['stop_gradient'])
W
wuzewu 已提交
418 419
                utils.from_pyobj_to_module_attr(block.idx,
                                                var_info.map.data['block_id'])
W
wuzewu 已提交
420 421 422 423 424 425 426 427 428 429

        # save signarture info
        for key, sign in self.signatures.items():
            var = self.desc.sign2var[sign.name]
            feed_desc = var.feed_desc
            fetch_desc = var.fetch_desc
            feed_names = sign.feed_names
            fetch_names = sign.fetch_names
            for index, input in enumerate(sign.inputs):
                feed_var = feed_desc.add()
430
                feed_var.var_name = self.get_var_name_with_prefix(input.name)
W
wuzewu 已提交
431 432 433 434
                feed_var.alias = feed_names[index]

            for index, output in enumerate(sign.outputs):
                fetch_var = fetch_desc.add()
435
                fetch_var.var_name = self.get_var_name_with_prefix(output.name)
W
wuzewu 已提交
436 437
                fetch_var.alias = fetch_names[index]

W
wuzewu 已提交
438
        # save default signature
W
wuzewu 已提交
439
        utils.from_pyobj_to_module_attr(
W
wuzewu 已提交
440
            self.default_signature.name if self.default_signature else None,
W
wuzewu 已提交
441
            attr.map.data['default_signature'])
W
wuzewu 已提交
442

W
wuzewu 已提交
443
        # save name prefix
W
wuzewu 已提交
444 445
        utils.from_pyobj_to_module_attr(self.name_prefix,
                                        self.desc.attr.map.data["name_prefix"])
W
wuzewu 已提交
446

W
wuzewu 已提交
447
        # save module info
W
wuzewu 已提交
448
        module_info = attr.map.data['module_info']
W
wuzewu 已提交
449
        module_info.type = module_desc_pb2.MAP
W
wuzewu 已提交
450 451 452 453 454 455 456 457 458 459
        utils.from_pyobj_to_module_attr(self.name, module_info.map.data['name'])
        utils.from_pyobj_to_module_attr(self.version,
                                        module_info.map.data['version'])
        utils.from_pyobj_to_module_attr(self.author,
                                        module_info.map.data['author'])
        utils.from_pyobj_to_module_attr(self.author_email,
                                        module_info.map.data['author_email'])
        utils.from_pyobj_to_module_attr(self.type, module_info.map.data['type'])
        utils.from_pyobj_to_module_attr(self.summary,
                                        module_info.map.data['summary'])
W
wuzewu 已提交
460

W
wuzewu 已提交
461 462 463 464 465 466
        # save extra info
        extra_info = attr.map.data['extra_info']
        extra_info.type = module_desc_pb2.MAP
        for key, value in self.extra_info.items():
            utils.from_pyobj_to_module_attr(value, extra_info.map.data[key])

467
    def __call__(self, sign_name, data, use_gpu=False, batch_size=1, **kwargs):
W
wuzewu 已提交
468 469
        self.check_processor()

W
wuzewu 已提交
470
        def _get_reader_and_feeder(data_format, data, place):
W
wuzewu 已提交
471
            def _reader(process_data):
W
wuzewu 已提交
472 473 474 475 476 477 478 479 480
                for item in zip(*process_data):
                    yield item

            process_data = []
            feed_name_list = []
            for key in data_format:
                process_data.append([value['processed'] for value in data[key]])
                feed_name_list.append(data_format[key]['feed_key'])
            feeder = fluid.DataFeeder(feed_list=feed_name_list, place=place)
W
wuzewu 已提交
481
            return functools.partial(_reader, process_data=process_data), feeder
W
wuzewu 已提交
482

W
wuzewu 已提交
483 484 485 486 487 488 489 490
        if self.last_call_name != sign_name:
            self.last_call_name = sign_name
            self.cache_feed_dict, self.cache_fetch_dict, self.cache_program = self.context(
                sign_name, for_test=True)
        feed_dict = self.cache_feed_dict
        fetch_dict = self.cache_fetch_dict
        program = self.cache_program

W
wuzewu 已提交
491 492
        fetch_list = list(set([value for key, value in fetch_dict.items()]))
        with fluid.program_guard(program):
W
wuzewu 已提交
493 494
            result = []
            index = 0
495 496 497 498 499 500 501
            try:
                _places = os.environ["CUDA_VISIBLE_DEVICES"]
                int(_places[0])
            except:
                use_gpu = False

            place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
502

W
wuzewu 已提交
503
            exe = fluid.Executor(place=place)
W
wuzewu 已提交
504 505 506 507
            data = self.processor.preprocess(
                sign_name=sign_name, data_dict=data)
            data_format = self.processor.data_format(sign_name=sign_name)
            reader, feeder = _get_reader_and_feeder(data_format, data, place)
508
            reader = paddle.batch(reader, batch_size=batch_size)
W
wuzewu 已提交
509 510 511 512 513
            for batch in reader():
                data_out = exe.run(
                    feed=feeder.feed(batch),
                    fetch_list=fetch_list,
                    return_numpy=False)
W
wuzewu 已提交
514 515 516 517 518 519 520 521 522
                sub_data = {
                    key: value[index:index + len(batch)]
                    for key, value in data.items()
                }
                result += self.processor.postprocess(sign_name, data_out,
                                                     sub_data, **kwargs)
                index += len(batch)

        return result
W
wuzewu 已提交
523

W
wuzewu 已提交
524
    def check_processor(self):
W
wuzewu 已提交
525 526
        if not self.processor:
            raise ValueError("This Module is not callable!")
W
wuzewu 已提交
527

W
wuzewu 已提交
528
    def context(self,
529
                sign_name=None,
W
wuzewu 已提交
530
                for_test=False,
Z
Zeyu Chen 已提交
531
                trainable=True,
W
wuzewu 已提交
532
                regularizer=None,
533
                max_seq_len=128,
W
wuzewu 已提交
534
                learning_rate=1e-3):
535 536 537 538 539
        """
        Args:
            max_seq_len(int): maximum sequence length, this option is only
            available for BERT/ERNIE module
        """
W
wuzewu 已提交
540

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
        if sign_name:
            if sign_name not in self.signatures:
                raise KeyError(
                    "Module did not have a signature with name %s" % sign_name)
            signature = self.signatures[sign_name]
        else:
            inputs = [
                input for signature in self.signatures.values()
                for input in signature.inputs
            ]
            outputs = [
                output for signature in self.signatures.values()
                for output in signature.outputs
            ]
            feed_names = [
                feed_name for signature in self.signatures.values()
                for feed_name in signature.feed_names
            ]
            fetch_names = [
                fetch_name for signature in self.signatures.values()
                for fetch_name in signature.fetch_names
            ]
            signature = create_signature(
                name="hub_temp_signature",
                inputs=inputs,
                outputs=outputs,
                feed_names=feed_names,
                fetch_names=fetch_names,
                for_predict=False)
W
wuzewu 已提交
570

W
wuzewu 已提交
571
        program = self.program.clone(for_test=for_test)
W
wuzewu 已提交
572
        paddle_helper.remove_feed_fetch_op(program)
W
wuzewu 已提交
573 574

        if not for_test:
W
wuzewu 已提交
575
            paddle_helper.set_parameter_trainable(program, trainable)
W
wuzewu 已提交
576

W
wuzewu 已提交
577
            paddle_helper.set_parameter_learning_rate(program, learning_rate)
W
wuzewu 已提交
578

W
wuzewu 已提交
579
            paddle_helper.set_parameter_regularizer(program, regularizer)
W
wuzewu 已提交
580

Z
Zeyu Chen 已提交
581
            self._restore_parameter(program)
W
wuzewu 已提交
582

W
wuzewu 已提交
583 584
        self._recover_variable_info(program)

W
wuzewu 已提交
585
        paddle_helper.set_op_attr(program, is_test=for_test)
W
wuzewu 已提交
586 587 588 589 590 591 592 593 594 595 596 597 598 599
        feed_dict = {}
        fetch_dict = {}
        for index, var in enumerate(signature.inputs):
            feed_dict[index] = program.global_block().var(var.name)
            key = signature.feed_names[index]
            if key:
                feed_dict[key] = program.global_block().var(var.name)

        for index, var in enumerate(signature.outputs):
            fetch_dict[index] = program.global_block().var(var.name)
            key = signature.fetch_names[index]
            if key:
                fetch_dict[key] = program.global_block().var(var.name)

600
        # update BERT/ERNIE's input tensor's sequence length to max_seq_len
K
kinghuin 已提交
601
        if "bert" in self.name or self.name.startswith("ernie"):
602 603 604 605
            MAX_SEQ_LENGTH = 512
            if max_seq_len > MAX_SEQ_LENGTH or max_seq_len <= 0:
                raise ValueError(
                    "max_seq_len({}) should be in the range of [1, {}]".format(
K
kinghuin 已提交
606
                        max_seq_len, MAX_SEQ_LENGTH))
607
            logger.info(
608
                "Set maximum sequence length of input tensor to {}".format(
609
                    max_seq_len))
610 611
            if self.name.startswith("ernie_v2"):
                feed_list = [
612 613
                    "input_ids", "position_ids", "segment_ids", "input_mask",
                    "task_ids"
614
                ]
K
kinghuin 已提交
615 616
                logger.warning("For %s, it's no necessary to feed task_ids now."
                               % self.name)
617 618 619 620 621
            else:
                feed_list = [
                    "input_ids", "position_ids", "segment_ids", "input_mask"
                ]
            for tensor_name in feed_list:
622 623 624 625 626 627 628
                seq_tensor_shape = [-1, max_seq_len, 1]
                logger.info("The shape of input tensor[{}] set to {}".format(
                    tensor_name, seq_tensor_shape))
                program.global_block().var(
                    feed_dict[tensor_name].name).desc.set_shape(
                        seq_tensor_shape)

629 630
        # record num parameters loaded by paddlehub
        num_param_loaded = 0
W
wuzewu 已提交
631
        for param in program.global_block().iter_parameters():
632 633 634
            num_param_loaded += 1
        logger.info(
            "%d pretrained paramaters loaded by PaddleHub" % num_param_loaded)
W
wuzewu 已提交
635

W
wuzewu 已提交
636 637
        return feed_dict, fetch_dict, program

638
    def get_name_prefix(self):
W
wuzewu 已提交
639
        return self.name_prefix
640 641 642 643

    def get_var_name_with_prefix(self, var_name):
        return self.get_name_prefix() + var_name

W
wuzewu 已提交
644
    def _check_signatures(self):
W
wuzewu 已提交
645 646
        if not self.signatures:
            raise ValueError("Signatures should not be None")
W
wuzewu 已提交
647 648

        for key, sign in self.signatures.items():
W
wuzewu 已提交
649 650 651 652
            if not isinstance(sign, Signature):
                raise TypeError(
                    "Item in Signatures shoule be an instance of paddlehub.Signature"
                )
W
wuzewu 已提交
653 654 655

            for input in sign.inputs:
                _tmp_program = input.block.program
W
wuzewu 已提交
656 657 658 659
                if not self.program == _tmp_program:
                    raise ValueError(
                        "All input and outputs variables in signature should come from the same Program"
                    )
W
wuzewu 已提交
660 661 662

            for output in sign.outputs:
                _tmp_program = output.block.program
W
wuzewu 已提交
663 664 665 666
                if not self.program == _tmp_program:
                    raise ValueError(
                        "All input and outputs variables in signature should come from the same Program"
                    )
W
wuzewu 已提交
667 668 669 670 671 672 673 674 675 676 677 678

    def serialize_to_path(self, path=None, exe=None):
        self._check_signatures()
        self._generate_desc()
        # create module path for saving
        if path is None:
            path = os.path.join(".", self.name)
        self.helper = ModuleHelper(path)
        utils.mkdir(self.helper.module_dir)

        # create module pb
        module_desc = module_desc_pb2.ModuleDesc()
679 680 681 682
        logger.info("PaddleHub version = %s" % version.hub_version)
        logger.info("PaddleHub Module proto version = %s" %
                    version.module_proto_version)
        logger.info("Paddle version = %s" % paddle.__version__)
W
wuzewu 已提交
683 684 685 686 687 688 689 690 691 692 693 694 695 696

        feeded_var_names = [
            input.name for key, sign in self.signatures.items()
            for input in sign.inputs
        ]
        target_vars = [
            output for key, sign in self.signatures.items()
            for output in sign.outputs
        ]
        feeded_var_names = list(set(feeded_var_names))
        target_vars = list(set(target_vars))

        # save inference program
        program = self.program.clone()
W
wuzewu 已提交
697 698 699 700 701 702

        for block in program.blocks:
            for op in block.ops:
                if "op_callstack" in op.all_attrs():
                    op._set_attr("op_callstack", [""])

W
wuzewu 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
        if not exe:
            place = fluid.CPUPlace()
            exe = fluid.Executor(place=place)
        utils.mkdir(self.helper.model_path())
        fluid.io.save_inference_model(
            self.helper.model_path(),
            feeded_var_names=list(feeded_var_names),
            target_vars=list(target_vars),
            main_program=program,
            executor=exe)

        with open(os.path.join(self.helper.model_path(), "__model__"),
                  "rb") as file:
            program_desc_str = file.read()
            rename_program = fluid.framework.Program.parse_from_string(
                program_desc_str)
            varlist = {
                var: block
                for block in rename_program.blocks for var in block.vars
722
                if self.get_name_prefix() not in var
W
wuzewu 已提交
723 724 725
            }
            for var, block in varlist.items():
                old_name = var
726
                new_name = self.get_var_name_with_prefix(old_name)
W
wuzewu 已提交
727 728 729 730 731 732 733 734
                block._rename_var(old_name, new_name)
            utils.mkdir(self.helper.model_path())
            with open(
                    os.path.join(self.helper.model_path(), "__model__"),
                    "wb") as f:
                f.write(rename_program.desc.serialize_to_string())

            for file in os.listdir(self.helper.model_path()):
735
                if (file == "__model__" or self.get_name_prefix() in file):
W
wuzewu 已提交
736 737 738 739
                    continue
                os.rename(
                    os.path.join(self.helper.model_path(), file),
                    os.path.join(self.helper.model_path(),
740
                                 self.get_var_name_with_prefix(file)))
W
wuzewu 已提交
741 742

        # create processor file
W
wuzewu 已提交
743 744
        if self.processor:
            self._dump_processor()
W
wuzewu 已提交
745 746 747

        # create assets
        self._dump_assets()
W
wuzewu 已提交
748 749 750 751

        # create check info
        checker = ModuleChecker(self.helper.module_dir)
        checker.generate_check_info()
752 753 754 755 756

        # Serialize module_desc pb
        module_pb = self.desc.SerializeToString()
        with open(self.helper.module_desc_path(), "wb") as f:
            f.write(module_pb)