test_py_server.py 3.3 KB
Newer Older
B
barrierye 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2020 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.
# pylint: disable=doc-string-missing

B
barrierye 已提交
16 17 18
from pyserver import Op
from pyserver import Channel
from pyserver import PyServer
B
barrierye 已提交
19 20
import numpy as np
import python_service_channel_pb2
B
barrierye 已提交
21
import logging
B
barrierye 已提交
22

B
barrierye 已提交
23 24 25 26
logging.basicConfig(
    format='%(asctime)s %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
    datefmt='%Y-%m-%d %H:%M',
    level=logging.INFO)
B
barrierye 已提交
27

B
barrierye 已提交
28 29 30 31 32 33
# channel data: {name(str): data(bytes)}


class CombineOp(Op):
    def preprocess(self, input_data):
        cnt = 0
34 35
        for op_name, data in input_data.items():
            logging.debug("CombineOp preprocess: {}".format(op_name))
B
barrierye 已提交
36 37 38 39
            cnt += np.frombuffer(data.insts[0].data, dtype='float')
        data = python_service_channel_pb2.ChannelData()
        inst = python_service_channel_pb2.Inst()
        inst.data = np.ndarray.tobytes(cnt)
B
barrierye 已提交
40
        inst.name = "combine_op_output"
B
barrierye 已提交
41 42 43
        data.insts.append(inst)
        return data

44 45 46
    def postprocess(self, output_data):
        return output_data

B
barrierye 已提交
47 48 49 50 51 52 53 54 55 56

class UciOp(Op):
    def postprocess(self, output_data):
        data = python_service_channel_pb2.ChannelData()
        inst = python_service_channel_pb2.Inst()
        pred = np.array(output_data["price"][0][0], dtype='float')
        inst.data = np.ndarray.tobytes(pred)
        inst.name = "prediction"
        data.insts.append(inst)
        return data
B
barrierye 已提交
57 58


59 60 61
read_channel = Channel(name="read_channel")
combine_channel = Channel(name="combine_channel")
out_channel = Channel(name="out_channel")
62

B
barrierye 已提交
63
cnn_op = UciOp(
64
    name="cnn_op",
65
    input=read_channel,
B
barrierye 已提交
66
    in_dtype='float',
67
    outputs=[combine_channel],
B
barrierye 已提交
68
    out_dtype='float',
B
barrierye 已提交
69 70 71 72 73
    server_model="./uci_housing_model",
    server_port="9393",
    device="cpu",
    client_config="uci_housing_client/serving_client_conf.prototxt",
    server_name="127.0.0.1:9393",
74 75
    fetch_names=["price"],
    concurrency=2)
76

B
barrierye 已提交
77
bow_op = UciOp(
78
    name="bow_op",
79
    input=read_channel,
B
barrierye 已提交
80
    in_dtype='float',
81
    outputs=[combine_channel],
B
barrierye 已提交
82
    out_dtype='float',
B
barrierye 已提交
83 84 85 86 87
    server_model="./uci_housing_model",
    server_port="9292",
    device="cpu",
    client_config="uci_housing_client/serving_client_conf.prototxt",
    server_name="127.0.0.1:9393",
88 89
    fetch_names=["price"],
    concurrency=2)
90

B
barrierye 已提交
91
combine_op = CombineOp(
92
    name="combine_op",
93
    input=combine_channel,
B
barrierye 已提交
94
    in_dtype='float',
95 96 97
    outputs=[out_channel],
    out_dtype='float',
    concurrency=2)
B
barrierye 已提交
98

99 100 101
logging.info(read_channel.debug())
logging.info(combine_channel.debug())
logging.info(out_channel.debug())
B
barrierye 已提交
102 103
pyserver = PyServer()
pyserver.add_channel(read_channel)
104 105
pyserver.add_channel(combine_channel)
pyserver.add_channel(out_channel)
B
barrierye 已提交
106 107 108
pyserver.add_op(cnn_op)
pyserver.add_op(bow_op)
pyserver.add_op(combine_op)
B
barrierye 已提交
109
pyserver.prepare_server(port=8080, worker_num=2)
B
barrierye 已提交
110
pyserver.run_server()