encoder_server.py 2.4 KB
Newer Older
C
chenxuyi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#   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 division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals

import sys
import os
import argparse
import logging
import logging.handlers
import re
from propeller.service.server import InferenceServer
from propeller import log

if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('-m', '--model_dir', type=str, required=True)
    parser.add_argument('-p', '--port', type=int, default=8888)
    parser.add_argument('-v', '--verbose', action='store_true')
    parser.add_argument('--encode_layer', type=str, choices=[
        'pooler', 
        'layer12', 
        'layer11',
        'layer10',
        'layer9',
        'layer8',
        'layer7',
        'layer6',
        'layer5',
        'layer4',
        'layer3',
        'layer2',
        'layer1',
        ], default='pooler')
    args = parser.parse_args()

    if args.verbose:
        log.setLevel(logging.DEBUG)
    cuda_env = os.getenv("CUDA_VISIBLE_DEVICES")
    if cuda_env is None:
        raise RuntimeError('CUDA_VISIBLE_DEVICES not set')
C
chenxuyi 已提交
55 56
    if not os.path.exists(args.model_dir):
        raise ValueError('model_dir not found: %s' % args.model_dir)
C
chenxuyi 已提交
57 58
    if not os.path.exists(args.model_dir):
        raise ValueError('model_dir not found: %s' % args.model_dir)
C
chenxuyi 已提交
59 60 61 62 63 64 65 66 67 68 69 70
    n_devices = len(cuda_env.split(","))
    if args.encode_layer.lower() == 'pooler':
        model_dir = os.path.join(args.model_dir, 'pooler')
    else:
        pat = re.compile(r'layer(\d+)')
        match = pat.match(args.encode_layer.lower())
        layer = int(match.group(1))
        model_dir = os.path.join(args.model_dir, 'enc%d' % layer)

    server = InferenceServer(model_dir, n_devices)
    log.info('propeller server listent on port %d' % args.port)
    server.listen(args.port)