mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

[C++] We need C++ interface, and C++ examples

Open hjk41 opened this issue 10 years ago • 22 comments

Many people are calling for C++ examples. Currently we don't have C++ interface properly exposed, and we don't have many C++ examples.

I have written a simple example with the NDArray interface in examples/cpp. But I don't have time to write a full C++ example for MLP or other algorithms yet. The problem is, we don't expose the C++ interface properly. We do have a C interface, but we don't have C examples either.

I would like to call for building C++ interface, and writing some end-to-end C++ examples. This is important, since C++ programmers will mostly likely contribute to the development of core pieces like ops, engines, etc.

hjk41 avatar Dec 09 '15 02:12 hjk41

What do you mean by C++ interface is not properly exposed? What changes do we need?

BTW, a Makefile for your C++ example should be a good start. Unfortunately I'm not good with makefiles. Maybe @mli can help

piiswrong avatar Dec 09 '15 02:12 piiswrong

  1. C++ interface cannot be directly exposed in DLL/.so files, since the compilation environment might be different when compiling libmxnet and when compiling applications, leading to application crash due to ABI violations. This of course can be solved by forcing the user to always compile mxnet from source and use the same environment to compile application, but not really desirable: building libmxnet is painfully slow.
  2. The C api is a minimum interface. It provides apis such as CreateSymbols(char*, ...), instead of mx.sym.FullyConnect(...) as provided in Python. So users has to lookup the symbols first, which currently is not documented at all. I think we should wrap those libraries so they look more like Python interface.

I am not familiar with Cmake either. That's why I haven't written it for the example. :-P

On Wed, Dec 9, 2015 at 10:37 AM, Eric Junyuan Xie [email protected] wrote:

What do you mean by C++ interface is not properly exposed? What changes do we need?

BTW, a Makefile for your C++ example should be a good start. Unfortunately I'm not good with makefiles.

— Reply to this email directly or view it on GitHub https://github.com/dmlc/mxnet/issues/868#issuecomment-163087946.

HONG Chuntao System Research Group Microsoft Research Asia

hjk41 avatar Dec 09 '15 02:12 hjk41

really need, +1

teng88 avatar Dec 09 '15 03:12 teng88

need it very much

sofiawu avatar Dec 09 '15 04:12 sofiawu

+1 This is the most important reason that holds me back from moving to MXNet.

happynear avatar Dec 09 '15 04:12 happynear

hi, @hjk41 1. I use the following cmake setup, with your code, and I think this is a general setup for other tasks(this works for linux only). Just add them to the tail of the original Makefile.

COMPILE_com=$(CXX) -std=c++0x $(CFLAGS) -L./lib -lmxnet  $(LDFLAGS) -I ./src

example_cpp: example/cpp/use_ndarray.cpp lib/libmxnet.a lib/libmxnet.so
  $(COMPILE_com)  -o $@ $(filter %.cpp %.o %.c %.a %.cc, $^)

example_cpp_run: example_cpp
  LD_LIBRARY_PATH=./lib ./example_cpp

and ,of course, run as the following:

make example_cpp_run

Do we need to compile the whole mxnet everytime? Just using the heads in ./include and the compiled libraries libmxnet.a/libmxnet.so should be ok. Also, maybe the heads in ./src is needed because the operators are defined there.

However, I haven't made any advancement in using CPP-version-mxnet by now yet. But again, is it that necessary to try to do all the work in C/CPP, Python just does well.

zhangchen-qinyinghua avatar Dec 09 '15 09:12 zhangchen-qinyinghua

@hjk41 I think require the user to build from source when using C++ api should be fine. Maintaining a complete C API seems too much work load

piiswrong avatar Dec 09 '15 20:12 piiswrong

@teng88 @sofiawu @happynear Could you describe your use case? Why do you need C++ interface and not the other language bindings? Which part of the interface do you need most?

piiswrong avatar Dec 09 '15 20:12 piiswrong

@piiswrong The first thing is to support C++ testing. I am working on a project for face recognition. I don't want my clients to see my codes, and they would not always have a python environment on their PC.

The second thing is personal. I am not very familiar with python. My programming skill only includes C++ and Matlab. Unfortunately, these two languages are not supported by MXNet. Thus, I gave up moving to MXNet. I am glad to see a C++ interface and continue to learn MXNet because your codes are really awesome.

happynear avatar Dec 10 '15 01:12 happynear

Our current api already supports pure c++ testing. There is a prediction api Folder On Dec 9, 2015 5:54 PM, "Feng Wang" [email protected] wrote:

@piiswrong https://github.com/piiswrong The first thing is to support C++ testing. I am working on a project for surveillance analysis. I don't want my clients to see my codes, and they would not always have a python environment on their PC.

The second thing is personal. I am not very familiar with python. My programming skill only includes C++ and Matlab. Unfortunately, these two languages are not supported by MXNet. Thus, I gave up moving to MXNet. I am glad to see a C++ interface and continue to learn MXNet because your codes are really awesome.

— Reply to this email directly or view it on GitHub https://github.com/dmlc/mxnet/issues/868#issuecomment-163461637.

piiswrong avatar Dec 10 '15 02:12 piiswrong

I want C++ because I would like to build a "standalone" package that runs MXNET training. I don't want to install Python on every machine that I will use, because maintaining the Python version will be a nightmare.

I tried using some of the "portable Python" packages such as Anaconda, but they don't really work well, and they are huge.

On Thu, Dec 10, 2015 at 10:05 AM, Eric Junyuan Xie <[email protected]

wrote:

Our current api already supports pure c++ testing. There is a prediction api Folder On Dec 9, 2015 5:54 PM, "Feng Wang" [email protected] wrote:

@piiswrong https://github.com/piiswrong The first thing is to support C++ testing. I am working on a project for surveillance analysis. I don't want my clients to see my codes, and they would not always have a python environment on their PC.

The second thing is personal. I am not very familiar with python. My programming skill only includes C++ and Matlab. Unfortunately, these two languages are not supported by MXNet. Thus, I gave up moving to MXNet. I am glad to see a C++ interface and continue to learn MXNet because your codes are really awesome.

— Reply to this email directly or view it on GitHub https://github.com/dmlc/mxnet/issues/868#issuecomment-163461637.

— Reply to this email directly or view it on GitHub https://github.com/dmlc/mxnet/issues/868#issuecomment-163463218.

HONG Chuntao System Research Group Microsoft Research Asia

hjk41 avatar Dec 10 '15 02:12 hjk41

Yes, there is a c++ api file, but without examples. I still don't know how to run a trained model.

happynear avatar Dec 10 '15 02:12 happynear

I am writing a wrapper here: https://github.com/hjk41/MxNet.cpp

hjk41 avatar Dec 22 '15 09:12 hjk41

@happynear the matlab binding is on going. #1013

mli avatar Dec 22 '15 19:12 mli

I think it a good idea to separate the interface apart from the kernel.

zhangchen-qinyinghua avatar Dec 23 '15 02:12 zhangchen-qinyinghua

@mli I had already tested it before it got merged. It is great.

happynear avatar Dec 23 '15 09:12 happynear

C++ interface is needed in practical work!

sunshineInmoon avatar Dec 24 '15 09:12 sunshineInmoon

+1, I want c++ api too. It is essential for building standalone package.I do not mind to build the libraries by myself(thanks for your efforts, mxnet is much easier to cross-platform than many deep learning libraries) .

stereomatchingkiss avatar Jan 16 '16 15:01 stereomatchingkiss

The C++ api development is here: https://github.com/hjk41/MxNet.cpp It is almost complete, and we will merge it back to dmlc/mxnet after some clean up

On Sat, Jan 16, 2016 at 11:29 PM, stereomatchingkiss < [email protected]> wrote:

+1, I want a c++ api too. It is essential for building standalone package.I do not mind to build the libraries by myself(thanks for your efforts, mxnet is much easier to cross-platform than many deep learning libraries) .

— Reply to this email directly or view it on GitHub https://github.com/dmlc/mxnet/issues/868#issuecomment-172216733.

HONG Chuntao System Research Group Microsoft Research Asia

hjk41 avatar Jan 18 '16 01:01 hjk41

@hjk41 Thanks for that. One question about the use_ndarray.cpp example: it looks like we are required to manually perform arithmetic to index into the array. Seems error prone and labor intensive. And dangerous too in that it means client code must have knowledge of the internal representation of the NDArray, and is coupled to it. Is there any intention to provide something like the Caffe Blob's offset() or data_at() methods? I didn't notice anything like that in ndarray.h or MxNetCpp.h..

jyegerlehner avatar Jan 18 '16 23:01 jyegerlehner