Skip to content

Commit cdb69b0

Browse files
Merge pull request #249 from MRXLT/general-server-timeline
timeline tool support multi process
2 parents 5fe3587 + 8a82c47 commit cdb69b0

File tree

6 files changed

+49
-35
lines changed

6 files changed

+49
-35
lines changed

core/general-client/include/general_model.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ class PredictorRes {
4545
~PredictorRes() {}
4646

4747
public:
48-
const std::vector<std::vector<int64_t>> & get_int64_by_name(
49-
const std::string & name) {
48+
const std::vector<std::vector<int64_t>>& get_int64_by_name(
49+
const std::string& name) {
5050
return _int64_map[name];
5151
}
52-
const std::vector<std::vector<float>> & get_float_by_name(
53-
const std::string & name) {
52+
const std::vector<std::vector<float>>& get_float_by_name(
53+
const std::string& name) {
5454
return _float_map[name];
5555
}
5656

@@ -71,7 +71,7 @@ class PredictorClient {
7171
void set_predictor_conf(const std::string& conf_path,
7272
const std::string& conf_file);
7373

74-
int create_predictor_by_desc(const std::string & sdk_desc);
74+
int create_predictor_by_desc(const std::string& sdk_desc);
7575

7676
int create_predictor();
7777
int destroy_predictor();
@@ -81,7 +81,8 @@ class PredictorClient {
8181
const std::vector<std::vector<int64_t>>& int_feed,
8282
const std::vector<std::string>& int_feed_name,
8383
const std::vector<std::string>& fetch_name,
84-
PredictorRes & predict_res); // NOLINT
84+
PredictorRes& predict_res, // NOLINT
85+
const int& pid);
8586

8687
std::vector<std::vector<float>> predict(
8788
const std::vector<std::vector<float>>& float_feed,

core/general-client/src/general_model.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ int PredictorClient::predict(const std::vector<std::vector<float>> &float_feed,
137137
const std::vector<std::vector<int64_t>> &int_feed,
138138
const std::vector<std::string> &int_feed_name,
139139
const std::vector<std::string> &fetch_name,
140-
PredictorRes &predict_res) { // NOLINT
140+
PredictorRes &predict_res,
141+
const int &pid) { // NOLINT
141142
predict_res._int64_map.clear();
142143
predict_res._float_map.clear();
143144
Timer timeline;
@@ -241,6 +242,7 @@ int PredictorClient::predict(const std::vector<std::vector<float>> &float_feed,
241242
if (FLAGS_profile_client) {
242243
std::ostringstream oss;
243244
oss << "PROFILE\t"
245+
<< "pid:" << pid << "\t"
244246
<< "prepro_0:" << preprocess_start << " "
245247
<< "prepro_1:" << preprocess_end << " "
246248
<< "client_infer_0:" << client_infer_start << " "

core/general-client/src/pybind_general_model.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ PYBIND11_MODULE(serving_client, m) {
3131
py::class_<PredictorRes>(m, "PredictorRes", py::buffer_protocol())
3232
.def(py::init())
3333
.def("get_int64_by_name",
34-
[](PredictorRes &self, std::string & name) {
34+
[](PredictorRes &self, std::string &name) {
3535
return self.get_int64_by_name(name);
36-
}, py::return_value_policy::reference)
36+
},
37+
py::return_value_policy::reference)
3738
.def("get_float_by_name",
38-
[](PredictorRes &self, std::string & name) {
39+
[](PredictorRes &self, std::string &name) {
3940
return self.get_float_by_name(name);
40-
}, py::return_value_policy::reference);
41+
},
42+
py::return_value_policy::reference);
4143

4244
py::class_<PredictorClient>(m, "PredictorClient", py::buffer_protocol())
4345
.def(py::init())
@@ -56,26 +58,29 @@ PYBIND11_MODULE(serving_client, m) {
5658
self.set_predictor_conf(conf_path, conf_file);
5759
})
5860
.def("create_predictor_by_desc",
59-
[](PredictorClient &self, const std::string & sdk_desc) {
60-
self.create_predictor_by_desc(sdk_desc); })
61+
[](PredictorClient &self, const std::string &sdk_desc) {
62+
self.create_predictor_by_desc(sdk_desc);
63+
})
6164
.def("create_predictor",
6265
[](PredictorClient &self) { self.create_predictor(); })
6366
.def("destroy_predictor",
6467
[](PredictorClient &self) { self.destroy_predictor(); })
6568
.def("predict",
6669
[](PredictorClient &self,
67-
const std::vector<std::vector<float>> &float_feed,
68-
const std::vector<std::string> &float_feed_name,
69-
const std::vector<std::vector<int64_t>> &int_feed,
70-
const std::vector<std::string> &int_feed_name,
71-
const std::vector<std::string> &fetch_name,
72-
PredictorRes & predict_res) {
70+
const std::vector<std::vector<float>> &float_feed,
71+
const std::vector<std::string> &float_feed_name,
72+
const std::vector<std::vector<int64_t>> &int_feed,
73+
const std::vector<std::string> &int_feed_name,
74+
const std::vector<std::string> &fetch_name,
75+
PredictorRes &predict_res,
76+
const int &pid) {
7377
return self.predict(float_feed,
7478
float_feed_name,
7579
int_feed,
7680
int_feed_name,
7781
fetch_name,
78-
predict_res);
82+
predict_res,
83+
pid);
7984
})
8085
.def("batch_predict",
8186
[](PredictorClient &self,

python/examples/bert/bert_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def __init__(self,
3636
self.show_ids = show_ids
3737
self.do_lower_case = do_lower_case
3838
self.retry = retry
39+
self.pid = os.getpid()
3940
self.profile = True if ("FLAGS_profile_client" in os.environ and
4041
os.environ["FLAGS_profile_client"]) else False
4142

@@ -78,7 +79,8 @@ def run_general(self, text, fetch):
7879
}
7980
prepro_end = time.time()
8081
if self.profile:
81-
print("PROFILE\tbert_pre_0:{} bert_pre_1:{}".format(
82+
print("PROFILE\tpid:{}\tbert_pre_0:{} bert_pre_1:{}".format(
83+
self.pid,
8284
int(round(prepro_start * 1000000)),
8385
int(round(prepro_end * 1000000))))
8486
fetch_map = self.client.predict(feed=feed, fetch=fetch)
@@ -111,7 +113,8 @@ def run_batch_general(self, text, fetch):
111113
feed_batch.append(feed)
112114
prepro_end = time.time()
113115
if self.profile:
114-
print("PROFILE\tbert_pre_0:{} bert_pre_1:{}".format(
116+
print("PROFILE\tpid:{}\tbert_pre_0:{} bert_pre_1:{}".format(
117+
self.pid,
115118
int(round(prepro_start * 1000000)),
116119
int(round(prepro_end * 1000000))))
117120
fetch_map_batch = self.client.batch_predict(

python/examples/util/timeline_trace.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
profile_file = sys.argv[1]
66

77

8-
def prase(line, counter):
9-
event_list = line.split(" ")
8+
def prase(pid_str, time_str, counter):
9+
pid = pid_str.split(":")[1]
10+
event_list = time_str.split(" ")
1011
trace_list = []
1112
for event in event_list:
1213
name, ts = event.split(":")
@@ -19,7 +20,7 @@ def prase(line, counter):
1920
event_dict = {}
2021
event_dict["name"] = name
2122
event_dict["tid"] = 0
22-
event_dict["pid"] = 0
23+
event_dict["pid"] = pid
2324
event_dict["ts"] = ts
2425
event_dict["ph"] = ph
2526

@@ -36,7 +37,7 @@ def prase(line, counter):
3637
for line in f.readlines():
3738
line = line.strip().split("\t")
3839
if line[0] == "PROFILE":
39-
trace_list = prase(line[1], counter)
40+
trace_list = prase(line[1], line[2], counter)
4041
counter += 1
4142
for trace in trace_list:
4243
all_list.append(trace)

python/paddle_serving_client/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ def __init__(self):
7878
self.feed_types_ = {}
7979
self.feed_names_to_idx_ = {}
8080
self.rpath()
81+
self.pid = os.getpid()
8182

8283
def rpath(self):
8384
lib_path = os.path.dirname(paddle_serving_client.__file__)
8485
client_path = os.path.join(lib_path, 'serving_client.so')
8586
lib_path = os.path.join(lib_path, 'lib')
8687
os.popen('patchelf --set-rpath {} {}'.format(lib_path, client_path))
8788

88-
8989
def load_client_config(self, path):
9090
from .serving_client import PredictorClient
9191
from .serving_client import PredictorRes
@@ -128,9 +128,8 @@ def connect(self, endpoints):
128128
predictor_sdk.set_server_endpoints(endpoints)
129129
sdk_desc = predictor_sdk.gen_desc()
130130
print(sdk_desc)
131-
self.client_handle_.create_predictor_by_desc(
132-
sdk_desc.SerializeToString())
133-
131+
self.client_handle_.create_predictor_by_desc(sdk_desc.SerializeToString(
132+
))
134133

135134
def get_feed_names(self):
136135
return self.feed_names_
@@ -144,6 +143,7 @@ def predict(self, feed={}, fetch=[]):
144143
int_feed_names = []
145144
float_feed_names = []
146145
fetch_names = []
146+
147147
for key in feed:
148148
if key not in self.feed_names_:
149149
continue
@@ -158,16 +158,18 @@ def predict(self, feed={}, fetch=[]):
158158
if key in self.fetch_names_:
159159
fetch_names.append(key)
160160

161-
ret = self.client_handle_.predict(
162-
float_slot, float_feed_names, int_slot,
163-
int_feed_names, fetch_names, self.result_handle_)
161+
ret = self.client_handle_.predict(float_slot, float_feed_names,
162+
int_slot, int_feed_names, fetch_names,
163+
self.result_handle_, self.pid)
164164

165165
result_map = {}
166166
for i, name in enumerate(fetch_names):
167167
if self.fetch_names_to_type_[name] == int_type:
168-
result_map[name] = self.result_handle_.get_int64_by_name(name)[0]
168+
result_map[name] = self.result_handle_.get_int64_by_name(name)[
169+
0]
169170
elif self.fetch_names_to_type_[name] == float_type:
170-
result_map[name] = self.result_handle_.get_float_by_name(name)[0]
171+
result_map[name] = self.result_handle_.get_float_by_name(name)[
172+
0]
171173

172174
return result_map
173175

0 commit comments

Comments
 (0)