test_py_server.py 4.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
# channel data: {name(str): data(bytes)}
B
barrierye 已提交
29
"""
B
barrierye 已提交
30
class ImdbOp(Op):
B
barrierye 已提交
31
    def postprocess(self, output_data):
B
barrierye 已提交
32 33 34 35 36
        data = python_service_channel_pb2.ChannelData()
        inst = python_service_channel_pb2.Inst()
        pred = np.array(output_data["prediction"][0][0], dtype='float')
        inst.data = np.ndarray.tobytes(pred)
        inst.name = "prediction"
B
barrierye 已提交
37
        inst.id = 0 #TODO
B
barrierye 已提交
38
        data.insts.append(inst)
B
barrierye 已提交
39
        return data
B
barrierye 已提交
40
"""
B
barrierye 已提交
41 42 43 44


class CombineOp(Op):
    def preprocess(self, input_data):
B
barrierye 已提交
45
        data_id = None
B
barrierye 已提交
46
        cnt = 0
B
barrierye 已提交
47 48 49
        for input in input_data:
            data = input[0]  # batchsize=1
            cnt += np.frombuffer(data.insts[0].data, dtype='float')
B
barrierye 已提交
50 51 52 53 54
            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 已提交
55 56 57 58 59
        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 已提交
60
        data.id = data_id
B
barrierye 已提交
61 62 63 64 65 66
        print(data)
        return data


class UciOp(Op):
    def postprocess(self, output_data):
B
barrierye 已提交
67
        data_ids = self.get_data_ids()
B
barrierye 已提交
68 69 70 71 72 73
        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 已提交
74
        data.id = data_ids[0]
B
barrierye 已提交
75
        return data
B
barrierye 已提交
76 77 78 79 80 81


read_channel = Channel(consumer=2)
cnn_out_channel = Channel()
bow_out_channel = Channel()
combine_out_channel = Channel()
B
barrierye 已提交
82 83
cnn_op = UciOp(
    inputs=[read_channel],
B
barrierye 已提交
84
    in_dtype='float',
B
barrierye 已提交
85
    outputs=[cnn_out_channel],
B
barrierye 已提交
86
    out_dtype='float',
B
barrierye 已提交
87 88 89 90 91 92 93 94
    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"])
bow_op = UciOp(
    inputs=[read_channel],
B
barrierye 已提交
95
    in_dtype='float',
B
barrierye 已提交
96
    outputs=[bow_out_channel],
B
barrierye 已提交
97
    out_dtype='float',
B
barrierye 已提交
98 99 100 101 102 103 104
    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"])
'''
B
barrierye 已提交
105 106 107 108 109 110 111 112
cnn_op = ImdbOp(
    inputs=[read_channel],
    outputs=[cnn_out_channel],
    server_model="./imdb_cnn_model",
    server_port="9393",
    device="cpu",
    client_config="imdb_cnn_client_conf/serving_client_conf.prototxt",
    server_name="127.0.0.1:9393",
B
barrierye 已提交
113
    fetch_names=["acc", "cost", "prediction"])
B
barrierye 已提交
114 115 116 117 118 119 120 121
bow_op = ImdbOp(
    inputs=[read_channel],
    outputs=[bow_out_channel],
    server_model="./imdb_bow_model",
    server_port="9292",
    device="cpu",
    client_config="imdb_bow_client_conf/serving_client_conf.prototxt",
    server_name="127.0.0.1:9292",
B
barrierye 已提交
122 123
    fetch_names=["acc", "cost", "prediction"])
'''
B
barrierye 已提交
124
combine_op = CombineOp(
B
barrierye 已提交
125 126 127 128
    inputs=[cnn_out_channel, bow_out_channel],
    in_dtype='float',
    outputs=[combine_out_channel],
    out_dtype='float')
B
barrierye 已提交
129 130 131

pyserver = PyServer()
pyserver.add_channel(read_channel)
B
barrierye 已提交
132 133 134
pyserver.add_channel(cnn_out_channel)
pyserver.add_channel(bow_out_channel)
pyserver.add_channel(combine_out_channel)
B
barrierye 已提交
135 136 137
pyserver.add_op(cnn_op)
pyserver.add_op(bow_op)
pyserver.add_op(combine_op)
B
barrierye 已提交
138
pyserver.prepare_server(port=8080, worker_num=2)
B
barrierye 已提交
139
pyserver.run_server()