mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

C++ api executor->Forward(false) is much slower than MXPredForward?

Open gzpyy opened this issue 8 years ago • 4 comments

Description

hi,

I am using the c++ api for prediction based on mobilenet v2. And I found that the codes belows runs 100+ms while I implement the same function using the c api, 10ms only. Both are running on GPU.

c++ version: executor = net.SimpleBind(global_ctx, args_map, map<string, NDArray>(), map<string, OpReqType>(), aux_map); executor->Forward(false); // around 110ms

c version: MXPredCreate((const char *) json_data.GetBuffer(), (const char *) param_data.GetBuffer(), static_cast<size_t>(param_data.GetLength()), dev_type, dev_id, num_input_nodes, input_keys, input_shape_indptr, input_shape_data, &pred_hnd); MXPredSetInput(pred_hnd, "data", image_data.data(), image_size);

// Do Predict Forward MXPredForward(pred_hnd); // 10ms

Is there something that i am doing wrong?

gzpyy avatar Aug 14 '18 10:08 gzpyy

@xiaojingxie : This is a good question. Could you also post this on discuss.mxnet.io ? The question will receive a larger audience.

@mxnet-label-bot [C++, C, Question]

vdantu avatar Aug 15 '18 16:08 vdantu

Hi @xiaojingxie

The Executor->Forward() method performs more tasks as compared to MXPredForward(). It invokes following 2 C APIs internally MXExecutorForward() and MXExecutorOutputs() where as MXPredForward() only runs the forward pass and does not retrieve the outputs. In addition, the outputs of the forward pass are copied to the 'output' array in the Executor object, for faster retrieval later.

Therefore, we can not compare MXPredForward() with Executor->Forward(). For the correct comparison, we should find out the time required to invoke MXPredGetOutputShape() and MXPredGetOutput() after invoking the MXPredForward()

I hope this answers the question. @mxnet-label-bot add [Pending Requester Info]

leleamol avatar Mar 14 '19 23:03 leleamol

Hi @xiaojingxie

The Executor->Forward() method performs more tasks as compared to MXPredForward(). It invokes following 2 C APIs internally MXExecutorForward() and MXExecutorOutputs() where as MXPredForward() only runs the forward pass and does not retrieve the outputs. In addition, the outputs of the forward pass are copied to the 'output' array in the Executor object, for faster retrieval later.

Therefore, we can not compare MXPredForward() with Executor->Forward(). For the correct comparison, we should find out the time required to invoke MXPredGetOutputShape() and MXPredGetOutput() after invoking the MXPredForward()

I hope this answers the question. @mxnet-label-bot add [Pending Requester Info]

Thanks a lot.

By the way, the method name Forward() is a bit misleading.

gzpyy avatar Apr 07 '19 15:04 gzpyy

Hi @xiaojingxie

The Executor->Forward() method performs more tasks as compared to MXPredForward(). It invokes following 2 C APIs internally MXExecutorForward() and MXExecutorOutputs() where as MXPredForward() only runs the forward pass and does not retrieve the outputs. In addition, the outputs of the forward pass are copied to the 'output' array in the Executor object, for faster retrieval later.

Therefore, we can not compare MXPredForward() with Executor->Forward(). For the correct comparison, we should find out the time required to invoke MXPredGetOutputShape() and MXPredGetOutput() after invoking the MXPredForward()

I hope this answers the question. @mxnet-label-bot add [Pending Requester Info]

The running time of C API MXPredForward is much shorter than the running time of MXPredGetOutput:

auto start = std::chrono::high_resolution_clock::now();
MXPredForward(pred_hnd);
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
LOGI("MXPredForward: %d microseconds.", duration.count());
std::vector<float> data(size);
start = std::chrono::high_resolution_clock::now();
MXPredGetOutput(pred_hnd, output_index, &(data[0]), static_cast<mx_uint>(size));
stop = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
LOGI("MXPredGetOutput: %d microseconds.", duration.count());

The result is:

I/MXNET: MXPredForward: 106 microseconds.
I/MXNET: MXPredGetOutput: 3748967 microseconds.

Why? Is it something related to lazy evaluation? The code runs on Pixel3 with Snapdragon 835.

sheep94lion avatar Jun 27 '19 02:06 sheep94lion