3

我想转换这个 C++ 项目(Facebook FastText

├── args.cc
├── args.h
├── dictionary.cc
├── dictionary.h
├── fasttext.cc
├── fasttext.h
├── main.cc
├── matrix.cc
├── matrix.h
├── model.cc
├── model.h
├── productquantizer.cc
├── productquantizer.h
├── qmatrix.cc
├── qmatrix.h
├── real.h
├── utils.cc
├── utils.h
├── vector.cc
└── vector.h

我想跑进去node.js。我首先将它构建为一个项目emmake make并且我已经获得了链接的 LLVM 目标文件:

-rw-r--r--   1 loretoparisi  staff   27536 30 Lug 10:20 args.o
-rw-r--r--   1 loretoparisi  staff   78632 30 Lug 10:20 dictionary.o
-rw-r--r--   1 loretoparisi  staff   23864 30 Lug 10:20 productquantizer.o
-rw-r--r--   1 loretoparisi  staff   12120 30 Lug 10:20 matrix.o
-rw-r--r--   1 loretoparisi  staff    9132 30 Lug 10:20 qmatrix.o
-rw-r--r--   1 loretoparisi  staff   10532 30 Lug 10:20 vector.o
-rw-r--r--   1 loretoparisi  staff   30036 30 Lug 10:20 model.o
-rw-r--r--   1 loretoparisi  staff    1616 30 Lug 10:20 utils.o
-rw-r--r--   1 loretoparisi  staff  118404 30 Lug 10:20 fasttext.o
-rwxr-xr-x   1 loretoparisi  staff  270940 30 Lug 10:20 fasttext

但显然这不是位代码!然后我用编译器编译,emmake make VERBOSE=1结果发现它没有使用em++编译器,所以我尝试了cmake

emconfigure cmake
emmake make VERBOSE=1

此时我可以看到em++正在运行

/usr/local/Cellar/cmake/3.11.1/bin/cmake -E cmake_link_script CMakeFiles/fasttext-shared.dir/link.txt --verbose=1
/Users/loretoparisi/Documents/MyProjects/emsdk/emscripten/1.38.10/em++ -fPIC  -pthread -std=c++11 -funroll-loops -O3 -march=native  -shared -Wl,-soname,libfasttext.so -o libfasttext.so @CMakeFiles/fasttext-shared.dir/objects1.rsp 

所以我得到以下输出:

-rw-r--r--   1 loretoparisi  staff  870544 30 Lug 10:40 libfasttext.a
-rw-r--r--   1 loretoparisi  staff  306646 30 Lug 10:40 fasttext.wasm
-rw-r--r--   1 loretoparisi  wheel  114099 30 Lug 10:40 fasttext.js
-rw-r--r--   1 loretoparisi  staff  870504 30 Lug 10:40 libfasttext_pic.a
-rw-r--r--   1 loretoparisi  staff  663728 30 Lug 10:40 libfasttext.so

结果证明这是一个有效的 javascript 文件,但是在运行它时会导致异常

$ node fasttext.js predict-prob /root/lang_id.bin - 2
exception thrown: 5280232 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.

虽然它应该来自stdin喜欢

./fasttext predict-prob /root/lang_id.bin - 2
ja das is seher gut heute
__label__deu 0.999483 __label__bar 4.1711e-05

这个错误可能是由于stdinnode.js 造成的吗?类似的命令似乎很好,因为它以正确的方式接受和解析参数:

$ node fasttext.js predict-prob
usage: fasttext predict[-prob] <model> <test-data> [<k>] [<th>]

  <model>      model filename
  <test-data>  test data filename (if -, read from stdin)
  <k>          (optional; 1 by default) predict top k labels
  <th>         (optional; 0.0 by default) probability threshold

然后我将它模块化emcc -s MODULARIZE=1 -s LEGACY_VM_SUPPORT=1 -s WASM=0 -O1 libfasttext.so -o libfasttext.js,并在节点模块中调用,如:

let FastTextModule = require('./fasttext-module.js');
let Module = null;
if (Module) {
    return;
}
Module = {
    noExitRuntime: true,
    noInitialRun: false,
    preInit: [],
    preRun: [],
    postRun: [function () {
        console.log(`Loaded Javascript Module OK`);
    }],
    locateFile: "./",
    arguments: ['predict-prob', '/root/lang_id.bin', '-', 2]
};
Module['locateFile'] = function (path, prefix) {
    return prefix + path;
}
FastTextModule(Module);

但是我遇到了同样的错误:exception thrown: 5278216 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.

以同样的方式我可以运行它设置noInitialRun:true然后做

Module.callMain(['predict-prob', '/root/ft_lang_model.bin', 'test.csv', '2']);

但无论如何都有这个错误。

4

0 回答 0