CTranslate2 icon indicating copy to clipboard operation
CTranslate2 copied to clipboard

How can I use the C++ interface

Open RisanLi opened this issue 4 years ago • 2 comments

I am new to the C++, I am confused to use the c++ interface

I have converted opennmt model to ctranslate2 format, and I git clone ctranslate2. Then I create a c++ file with

#include #include <ctranslate2/translator_pool.h>

int main() { const size_t num_translators = 1; const size_t num_threads_per_translator = 4; ctranslate2::TranslatorPool translator(num_translators, num_threads_per_translator, "ende_ctranslate2/", ctranslate2::Device::CPU);

const std::vector<std::vectorstd::string> batch = {{"▁H", "ello", "▁world", "!"}}; const std::vectorctranslate2::TranslationResult results = translator.translate_batch(batch);

for (const auto& token : results[0].output()) std::cout << token << ' '; std::cout << std::endl; return 0; }

But I can't run the code.

Is there any description about the problem? Thanks.

RisanLi avatar Dec 16 '21 06:12 RisanLi

It seems you are looking for general help on how to compile C++ code. However, we can't assist with that unless there is a problem that is specific to CTranslate2. You should probably look for some tutorial online, or post your question on Stack Overflow.

guillaumekln avatar Dec 17 '21 08:12 guillaumekln

For other newbie programmers of C++ like me, this is how I compile the C++ example source code.


At first, install g++ for compiling C++ code:

apt install g++

then, set LD_LIBRARY_PATH variable:

export LD_LIBRARY_PATH=/opt/ctranslate2/lib

At last, compile the source file with this command

g++ -v -g -Wall translate.cpp -I/opt/ctranslate2/include -L/opt/ctranslate2/lib -lctranslate2 -o trans


while translate.cpp is the source file, trans is the executable file to generate. You can run it with ./trans command.

/opt/ctranslate2 is the path Ctranslate2 installed or built to, there should be include and lib subdirs in it.

I use docker image opennmt/ctranslate2:2.22.0-ubuntu20.04-cuda11.2, if you use the image too, follow the three steps up there should be just work.

By the way, if you want to use GPU, the device parameter should change to ctranslate2::Device::CUDA.

Good Luck!

huache avatar Sep 07 '22 09:09 huache