Skip to content

Commit

Permalink
Merge pull request #1269 from HexToString/develop-p
Browse files Browse the repository at this point in the history
fix pybind bug
  • Loading branch information
TeslaZhao authored Jun 3, 2021
2 parents 38ad6c1 + 3b29eba commit 4cd9a94
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 22 deletions.
19 changes: 6 additions & 13 deletions core/general-client/src/general_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include "core/sdk-cpp/include/common.h"
#include "core/sdk-cpp/include/predictor_sdk.h"
#include "core/util/include/timer.h"

DEFINE_bool(profile_client, false, "");
DEFINE_bool(profile_server, false, "");

Expand Down Expand Up @@ -46,7 +45,7 @@ void PredictorClient::init_gflags(std::vector<std::string> argv) {
int argc = argv.size();
char **arr = new char *[argv.size()];
std::string line;
for (size_t i = 0; i < argv.size(); i++) {
for (size_t i = 0; i < argv.size(); ++i) {
arr[i] = &argv[i][0];
line += argv[i];
line += ' ';
Expand Down Expand Up @@ -189,7 +188,6 @@ int PredictorClient::numpy_predict(
}

int vec_idx = 0;

for (int bi = 0; bi < batch_size; bi++) {
VLOG(2) << "prepare batch " << bi;
std::vector<Tensor *> tensor_vec;
Expand Down Expand Up @@ -220,11 +218,10 @@ int PredictorClient::numpy_predict(
return -1;
}
int nbytes = float_feed[vec_idx].nbytes();
// int ndims = float_feed[vec_idx].ndim();
void *rawdata_ptr = (void *)float_feed[vec_idx].data(0);
void *rawdata_ptr = (void *)(float_feed[vec_idx].data(0));
int total_number = float_feed[vec_idx].size();
// float* end_ptr = (rawdata_ptr + total_number);
Tensor *tensor = tensor_vec[idx];

VLOG(2) << "prepare float feed " << name << " shape size "
<< float_shape[vec_idx].size();
for (uint32_t j = 0; j < float_shape[vec_idx].size(); ++j) {
Expand All @@ -234,6 +231,7 @@ int PredictorClient::numpy_predict(
tensor->add_lod(float_lod_slot_batch[vec_idx][j]);
}
tensor->set_elem_type(P_FLOAT32);

tensor->mutable_float_data()->Resize(total_number, 0);
memcpy(tensor->mutable_float_data()->mutable_data(), rawdata_ptr, nbytes);
vec_idx++;
Expand All @@ -251,7 +249,7 @@ int PredictorClient::numpy_predict(
}
Tensor *tensor = tensor_vec[idx];
int nbytes = int_feed[vec_idx].nbytes();
void *rawdata_ptr = (void *)int_feed[vec_idx].data(0);
void *rawdata_ptr = (void *)(int_feed[vec_idx].data(0));
int total_number = int_feed[vec_idx].size();

for (uint32_t j = 0; j < int_shape[vec_idx].size(); ++j) {
Expand All @@ -263,19 +261,14 @@ int PredictorClient::numpy_predict(
tensor->set_elem_type(_type[idx]);

if (_type[idx] == P_INT64) {
VLOG(2) << "prepare int feed " << name << " shape size "
<< int_shape[vec_idx].size();
tensor->mutable_int64_data()->Resize(total_number, 0);
memcpy(
tensor->mutable_int64_data()->mutable_data(), rawdata_ptr, nbytes);
vec_idx++;
} else {
VLOG(2) << "prepare int32 feed " << name << " shape size "
<< int_shape[vec_idx].size();
tensor->mutable_int_data()->Resize(total_number, 0);
memcpy(tensor->mutable_int_data()->mutable_data(), rawdata_ptr, nbytes);
vec_idx++;
}
vec_idx++;
}

VLOG(2) << "batch [" << bi << "] "
Expand Down
19 changes: 11 additions & 8 deletions python/paddle_serving_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ def predict(self,
int_feed_names.append(key)
shape_lst = []
if batch == False:
feed_i[key] = feed_i[key][np.newaxis, :]
feed_i[key] = np.expand_dims(feed_i[key], 0).repeat(
1, axis=0)
if isinstance(feed_i[key], np.ndarray):
shape_lst.extend(list(feed_i[key].shape))
int_shape.append(shape_lst)
Expand All @@ -369,18 +370,19 @@ def predict(self,
int_lod_slot_batch.append([])

if isinstance(feed_i[key], np.ndarray):
int_slot.append(feed_i[key])
int_slot.append(np.ascontiguousarray(feed_i[key]))
self.has_numpy_input = True
else:
int_slot.append(feed_i[key])
int_slot.append(np.ascontiguousarray(feed_i[key]))
self.all_numpy_input = False

elif self.feed_types_[key] in float_type:
if i == 0:
float_feed_names.append(key)
shape_lst = []
if batch == False:
feed_i[key] = feed_i[key][np.newaxis, :]
feed_i[key] = np.expand_dims(feed_i[key], 0).repeat(
1, axis=0)
if isinstance(feed_i[key], np.ndarray):
shape_lst.extend(list(feed_i[key].shape))
float_shape.append(shape_lst)
Expand All @@ -393,10 +395,10 @@ def predict(self,
float_lod_slot_batch.append([])

if isinstance(feed_i[key], np.ndarray):
float_slot.append(feed_i[key])
float_slot.append(np.ascontiguousarray(feed_i[key]))
self.has_numpy_input = True
else:
float_slot.append(feed_i[key])
float_slot.append(np.ascontiguousarray(feed_i[key]))
self.all_numpy_input = False
#if input is string, feed is not numpy.
elif self.feed_types_[key] in string_type:
Expand All @@ -408,7 +410,7 @@ def predict(self,
key)])
else:
string_lod_slot_batch.append([])
string_slot.append(feed_i[key])
string_slot.append(np.ascontiguousarray(feed_i[key]))
self.has_numpy_input = True
int_slot_batch.append(int_slot)
int_lod_slot_batch.append(int_lod_slot)
Expand Down Expand Up @@ -626,6 +628,7 @@ def _pack_inference_request(self, feed, fetch, is_python, log_id):
raise Exception("error tensor value type.")
else:
raise Exception("var must be list or ndarray.")
data = np.ascontiguousarray(data)
tensor.data = data.tobytes()
tensor.shape.extend(list(var.shape))
if "{}.lod".format(name) in feed.keys():
Expand Down Expand Up @@ -700,7 +703,7 @@ def predict(self,
if batch is False:
for key in feed:
if ".lod" not in key:
feed[key] = feed[key][np.newaxis, :]
feed[key] = np.expand_dims(feed[key], 0).repeat(1, axis=0)
if not asyn:
try:
self.profile_.record('py_prepro_0')
Expand Down
2 changes: 1 addition & 1 deletion python/paddle_serving_server/rpc_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _unpack_inference_request(self, request):
else:
raise Exception("error type.")
data.shape = list(feed_inst.tensor_array[idx].shape)
feed_dict[name] = data
feed_dict[name] = np.ascontiguousarray(data)
if len(var.lod) > 0:
feed_dict["{}.lod".format(name)] = var.lod
feed_batch.append(feed_dict)
Expand Down

0 comments on commit 4cd9a94

Please sign in to comment.