Search code examples
chainer

How to plugin already written C/C++ layer in chainer


I have already written C++ layer which is using CPU, I want to plugin chainer framework, How to do that ? Can chainer mix of CPU and GPU layers together?


Solution

  • You can use Cython, pybind11, or whatever tool to call C++ code from Python to embed your C++ layer into Chainer. You have to write a little bit of glue code to do that (e.g. converting NumPy array buffer from/to the data format used in the layer written in C++ and converting the interface of you layer into Chainer style Function; the latter should be easily done by writing a small Python class).

    In order to mix CPU and GPU in your forward/backward computations, you can use F.copy(); it supports backprop (see https://docs.chainer.org/en/stable/reference/generated/chainer.functions.copy.html?highlight=copy).