test_py_server.py 3.4 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
# channel data: {name(str): data(bytes)}


class CombineOp(Op):
32
    #TODO: different id of data
B
barrierye 已提交
33
    def preprocess(self, input_data):
B
barrierye 已提交
34
        data_id = None
B
barrierye 已提交
35
        cnt = 0
B
barrierye 已提交
36 37 38
        for input in input_data:
            data = input[0]  # batchsize=1
            cnt += np.frombuffer(data.insts[0].data, dtype='float')
B
barrierye 已提交
39 40 41 42 43
            if data_id is None:
                data_id = data.id
            if data_id != data.id:
                raise Exception("id not match: {} vs {}".format(data_id,
                                                                data.id))
B
barrierye 已提交
44 45 46 47 48
        data = python_service_channel_pb2.ChannelData()
        inst = python_service_channel_pb2.Inst()
        inst.data = np.ndarray.tobytes(cnt)
        inst.name = "resp"
        data.insts.append(inst)
B
barrierye 已提交
49
        data.id = data_id
B
barrierye 已提交
50 51 52 53 54
        return data


class UciOp(Op):
    def postprocess(self, output_data):
B
barrierye 已提交
55
        data_ids = self.get_data_ids()
B
barrierye 已提交
56 57 58 59 60 61
        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)
B
barrierye 已提交
62
        data.id = data_ids[0]
B
barrierye 已提交
63
        return data
B
barrierye 已提交
64 65


66
read_channel = Channel()
B
barrierye 已提交
67 68 69
cnn_out_channel = Channel()
bow_out_channel = Channel()
combine_out_channel = Channel()
70

B
barrierye 已提交
71
cnn_op = UciOp(
72
    name="cnn_op",
B
barrierye 已提交
73
    inputs=[read_channel],
B
barrierye 已提交
74
    in_dtype='float',
B
barrierye 已提交
75
    outputs=[cnn_out_channel],
B
barrierye 已提交
76
    out_dtype='float',
B
barrierye 已提交
77 78 79 80 81 82
    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",
    fetch_names=["price"])
83

B
barrierye 已提交
84
bow_op = UciOp(
85
    name="bow_op",
B
barrierye 已提交
86
    inputs=[read_channel],
B
barrierye 已提交
87
    in_dtype='float',
B
barrierye 已提交
88
    outputs=[bow_out_channel],
B
barrierye 已提交
89
    out_dtype='float',
B
barrierye 已提交
90 91 92 93 94 95
    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",
    fetch_names=["price"])
96

B
barrierye 已提交
97
combine_op = CombineOp(
98
    name="combine_op",
B
barrierye 已提交
99 100 101 102
    inputs=[cnn_out_channel, bow_out_channel],
    in_dtype='float',
    outputs=[combine_out_channel],
    out_dtype='float')
B
barrierye 已提交
103 104 105

pyserver = PyServer()
pyserver.add_channel(read_channel)
B
barrierye 已提交
106 107 108
pyserver.add_channel(cnn_out_channel)
pyserver.add_channel(bow_out_channel)
pyserver.add_channel(combine_out_channel)
B
barrierye 已提交
109 110 111
pyserver.add_op(cnn_op)
pyserver.add_op(bow_op)
pyserver.add_op(combine_op)
B
barrierye 已提交
112
pyserver.prepare_server(port=8080, worker_num=2)
B
barrierye 已提交
113
pyserver.run_server()