mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

[Feature Request] Support fp16 for C Predict API

Open IamMegatron2025 opened this issue 7 years ago • 26 comments

hello, there is some materials about how to train a mxnet model with fp16, but I can not find how to infer a batch data using fp16 by c++ api, can you give some advises?

looking forward to your reply.

IamMegatron2025 avatar Feb 14 '19 11:02 IamMegatron2025

Hi, are you planning to train on C++? Currently, we do have Python supporting float16 if you initialize the dtype to that.

lanking520 avatar Feb 14 '19 20:02 lanking520

interested as well. @lanking520 so what you're saying is that C++ inference using float16 is not yet possible? is that also true for the C predict API?

simonmaurer avatar Feb 14 '19 23:02 simonmaurer

Grab someone may know about C++: @leleamol here

lanking520 avatar Feb 15 '19 01:02 lanking520

@lanking520 I use python to train a mxnet model (such as resnet-50 ) with --dtype=float16,but I want to using C++ api to infer this trained fp16 model. So there are some documents about how to do it?

IamMegatron2025 avatar Feb 15 '19 04:02 IamMegatron2025

@leleamol

eric-haibin-lin avatar Feb 15 '19 06:02 eric-haibin-lin

I use example/image-classification/predict-cpp/image-classification-predict.cc to infer this trained fp16 model, but it can not work and shows error:

Segmentation fault: 11

Stack trace returned 8 entries:
[bt] (0) /xxx/libmxnet.so(+0x431b7a) [0x7f19e23f6b7a]
[bt] (1) /xxx/libmxnet.so(+0x3503096) [0x7f19e54c8096]
[bt] (2) /lib64/libc.so.6(+0x35270) [0x7f19e1181270]
[bt] (3) /xxx/libmxnet.so(MXPredSetInput+0x74) [0x7f19e4c08d64]
[bt] (4) ./image-classification-predict() [0x403fb3]
[bt] (5) ./image-classification-predict() [0x402b8a]
[bt] (6) /lib64/libc.so.6(__libc_start_main+0xf5) [0x7f19e116dc05]
[bt] (7) ./image-classification-predict() [0x402e98]

IamMegatron2025 avatar Feb 15 '19 07:02 IamMegatron2025

have you solved it?

@lanking520 I use python to train a mxnet model (such as resnet-50 ) with --dtype=float16,but I want to using C++ api to infer this trained fp16 model. So there are some documents about how to do it?

@lanking520 I use python to train a mxnet model (such as resnet-50 ) with --dtype=float16,but I want to using C++ api to infer this trained fp16 model. So there are some documents about how to do it?

kwanking avatar Feb 15 '19 12:02 kwanking

@IvyGongoogle Currently cpp-package APIs doesn't support fp16 datatype. We can mark this question as feature request.

leleamol avatar Feb 15 '19 20:02 leleamol

@IvyGongoogle Currently cpp-package APIs doesn't support fp16 datatype. We can mark this question as feature request.

@leleamol so @IvyGongoogle did use an example which uses <mxnet/c_predict_api.h> but not cpp-package API with <mxnet-cpp/MxNetCpp.h>. those are two different APIs, correct?

simonmaurer avatar Feb 18 '19 11:02 simonmaurer

Indeed, I concur, there is a need for fp16 computation, as shown in this forum thread. https://discuss.mxnet.io/t/network-in-float16/3710

dmidge8 avatar Apr 12 '19 17:04 dmidge8

Hi, Once the conversion pass is added in the backend this should be easy to do. I am currently working on this: https://github.com/apache/incubator-mxnet/issues/14584 . Stay tuned.

anirudh2290 avatar Apr 15 '19 15:04 anirudh2290

@anirudh2290 thanks a lot for the effort. very interested as well in float16 inference in C++ API

simonmaurer avatar Apr 15 '19 22:04 simonmaurer

I modify the src/c_api/c_predict_api.cc in L211 to:

std::vector<NDArray> arg_arrays, aux_arrays;
 for (size_t i = 0; i < arg_shapes.size(); ++i) {
   if (arg_params.count(arg_names[i]) != 0) 
   {
     NDArray nd = NDArray(arg_shapes[i], ctx,false,arg_params[arg_names[i]].dtype());
     CopyFromTo(arg_params[arg_names[i]], &nd);
     arg_arrays.push_back(nd);
   }
   else
   {
     NDArray nd = NDArray(arg_shapes[i], ctx);
     arg_arrays.push_back(nd);
   }
 }
 for (size_t i = 0; i < aux_shapes.size(); ++i) {

   if (aux_params.count(aux_names[i]) != 0)
   {
     NDArray nd = NDArray(aux_shapes[i], ctx,false,aux_params[aux_names[i]].dtype());   
     CopyFromTo(aux_params[aux_names[i]], &nd);
     aux_arrays.push_back(nd);
   }
   else
   {
       NDArray nd = NDArray(aux_shapes[i], ctx);  
       aux_arrays.push_back(nd);
    }
 }

Then I can successfully infer this trained fp16 model using C++ api, and the speed is twice faster then with the fp32 when I infer a resentv1-50 cnn model . But when I infer a ocr recognition model, I find the speed is same with the fp32. What causes this ?

IamMegatron2025 avatar Apr 16 '19 03:04 IamMegatron2025

@IvyGongoogle Are your inputs and weights in fp16. Your change should work to run fp16 inference. What batch size are you using and what is the model ? For smaller batch sizes you may not see a big speedup. Also what hardware are you running it on ?

anirudh2290 avatar Apr 16 '19 18:04 anirudh2290

@IvyGongoogle @anirudh2290 just to make sure are we talking about the real C++ API as documented in https://github.com/apache/incubator-mxnet/tree/master/cpp-package or https://github.com/apache/incubator-mxnet/tree/master/src/c_api ? Could you guys maybe elaborate a bit on the backend process ? I'm missing the point regarding why this API does not support FP16 as the whole backend of MXNet (Python being bound to this) is based on C/C++... very appreciated

simonmaurer avatar Apr 16 '19 22:04 simonmaurer

The backend of MXNet is exposed via C APIs for different modules like ndarray, executor, symbol, dependency engine etc. The frontend bindings implement wrappers which internally call the C API to support different frontends. CPP-Package is a frontend binding just like Python. Thus once the support is added in backend you should have the C API for frontends to call, but the frontend interface for example amp.convert_model mentioned here: https://github.com/apache/incubator-mxnet/issues/14584 has to be implemented.

anirudh2290 avatar Apr 16 '19 23:04 anirudh2290

@anirudh2290 why does the float16 inference still work with Python if that is exactly a wrapper to the C API? as seen on https://mxnet.incubator.apache.org/versions/master/faq/float16.html

simonmaurer avatar Apr 17 '19 10:04 simonmaurer

if you see the mixed precision doc it asks you to modify symbol code and add cast layers at the start and before softmax. This probably can be done in another frontends too if you are writing the model by yourself. But in most cases you are loading a pre-trained model and if you have a pre-trained model there is no easy way to do this. Also, it gets more complicated when you want to introduce different cast layers not just at start and before softmax but in other places in the computation graph, and you want to customize it to test accuracy. (See the PR on AMP here: https://github.com/apache/incubator-mxnet/pull/14173 , it has specific customizable lists for ops to run in fp16 or fp32 https://github.com/apache/incubator-mxnet/pull/14173/files#diff-b79bfa3e02355c43ca5b195ef67172a5)

anirudh2290 avatar Apr 17 '19 18:04 anirudh2290

I can give a quick summary of my experience with fp16 and C++ so far:

I believe the best way to do this is as in other front-ends such as python. As @anirudh2290 mentions, add casts around numerically sensitive and insensitive sections (batchnorms and softmaxs for example should be fp32, convs and FC should be fp16) and then make sure your inputs are in fp16. You should then be able to run inference as normal (and you should see that fp16 operations are properly running).

One caveat is that depending on your graph the time spent casting inputs may be more than the time you save using fp16. That's where AMP and TensorRT integration can help. They'll fuse many operators that are numerically sensitive, removing them from the computation graph, which means you'll get larger sections of a graph that you can run in fp16 mode. They'll also fuse casting operations into numerical operations which saves you from doing two full memory copies on your tensors when casting. These methods should be a much more practical way of running fp16 for inference (with C++).

KellenSunderland avatar Apr 21 '19 20:04 KellenSunderland

@IvyGongoogle Are your inputs and weights in fp16. Your change should work to run fp16 inference. What batch size are you using and what is the model ? For smaller batch sizes you may not see a big speedup. Also what hardware are you running it on ?

@anirudh2290 @KellenSunderland Sorry, my test results show that when using fp16, the speed is twice faster then with the fp32 when inference a resentv1-50 cnn model, but not works with ocr recognition model. I have updated the above comment. But if you have experiences with ocr inference using mxnet fp16, please give me some advises.

IamMegatron2025 avatar Apr 23 '19 15:04 IamMegatron2025