seanchas116 / node-tflite

TensorFlow Lite bindings for Node.js
https://www.npmjs.com/package/node-tflite
MIT License
14 stars 6 forks source link

Native module error #9

Open jlarmstrongiv opened 3 years ago

jlarmstrongiv commented 3 years ago

すみません Sorry to bother you again

Lambda

My main goal is to deploy node-tflite inside a lambda environment.

I had hoped that since it’s already being compiled to linux for electron, it would just work. Unfortunately, not.

It does work locally on my mac though.

Here’s the long error message:

[POST] /api/lobe
09:24:24:96
2020-11-26T14:24:25.460Z    f56572ce-0459-4d60-a8b5-ef9f28aeebe3
    ERROR
    Error: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/node_modules/node-tflite/build/Release/libtensorflowlite_c.so)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1065:18)
    at Module.load (internal/modules/cjs/loader.js:879:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Module.require (internal/modules/cjs/loader.js:903:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at bindings (/var/task/node_modules/bindings/bindings.js:112:48)
    at Object.<anonymous> (/var/task/node_modules/node-tflite/index.js:4:32)
    at Module._compile (internal/modules/cjs/loader.js:1015:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
    at Module.load (internal/modules/cjs/loader.js:879:32)2020-11-26T14:24:25.461Z
    f56572ce-0459-4d60-a8b5-ef9f28aeebe3
    ERROR   Unhandled Promise Rejection
    {
"errorType":"Runtime.UnhandledPromiseRejection",
"errorMessage":"Error: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/node_modules/node-tflite/build/Release/libtensorflowlite_c.so)",
"reason":{"errorType":"Error",
"errorMessage":"/lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/node_modules/node-tflite/build/Release/libtensorflowlite_c.so)",
"stack":["Error: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/node_modules/node-tflite/build/Release/libtensorflowlite_c.so)","
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1065:18)","
    at Module.load (internal/modules/cjs/loader.js:879:32)","
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)","
    at Module.require (internal/modules/cjs/loader.js:903:19)","
    at require (internal/modules/cjs/helpers.js:74:18)","
   at bindings (/var/task/node_modules/bindings/bindings.js:112:48)","
    at Object.<anonymous> (/var/task/node_modules/node-tflite/index.js:4:32)","
    at Module._compile (internal/modules/cjs/loader.js:1015:30)","
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)","
    at Module.load (internal/modules/cjs/loader.js:879:32)"]},
"promise":{},"stack":["Runtime.UnhandledPromiseRejection: Error: /lib64/libm.so.6: version `GLIBC_2.27' not found (required by /var/task/node_modules/node-tflite/build/Release/libtensorflowlite_c.so)","
    at process.<anonymous> (/var/runtime/index.js:35:15)","
    at process.emit (events.js:326:22)","
    at processPromiseRejections (internal/process/promises.js:209:33)","
    at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
Unknown application error occurred

I am using Next.js and Vercel as to manage deployment.

The good news: it automatically installs all dependencies in the serverless environment.
The bad news: I do not have access to features like lambda layers or Amazon EFS for Lambda.

Other packages, like sharp.js, which run with Native Modules, do compile.

Here is a repository to reproduce the problem https://github.com/jlarmstrongiv/tflite-next/blob/main/src/pages/api/lobe.ts

With a vercel account, run vercel --prod in the repo to deploy the code on lambda. To run the code locally, run npm run dev.

In case it’s helpful, here are docker containers with the AWS environment.

Electron

On a side note, I think that this module will also need to be updated for future electron versions.

According to this electron issue, native modules will need to be NAPI or context aware.

I searched for NAN_MODULE_WORKER_ENABLED inside this repo, but couldn‘t find it.

seanchas116 commented 3 years ago

Thanks for sharing the issue!

Looks similar: https://izhangzhihao.github.io/2020/03/17/Build-tflite-runtime-with-amazon-linux-1/ Probably the error happens because Lambda uses glibc 2.17 while the prebuilt tflite in node-tflite is linked against glibc 2.27. (I don't know how to solve the issue yet)

For Electron, node-tflite already uses NAPI so probably no action is required.

seanchas116 commented 3 years ago

The glibc error might be fixed if we build tflite inside the Amazon Linux 1 docker container (which is used in Lambda environments).

jlarmstrongiv commented 3 years ago

Thanks for helping and looking into it!

This github workflow is able to compile for aws lambda.

I am also looking into doing the same thing, but with a docker command for comparison. I’ll add a reference here if I get it working 👍

jlarmstrongiv commented 3 years ago

Yes! I also managed to get docker working for compiling node in lambda environments:

{
  "scripts": {
    "install-modules": "docker run -it -v `pwd`:`pwd` -w `pwd` lambci/lambda:build-nodejs12.x npm install"
  }
}

This was the most helpful link I found https://www.rookout.com/blog/3-min-hack-for-locally-building-a-native-extension

Not quite sure how to get it working with node-tflite though

jlarmstrongiv commented 3 years ago

I ended up refactoring that package from scratch as https://www.npmjs.com/package/tfjs-node-lambda

Works like a charm. I would still prefer to use node-tflite though, it’s so much smaller and faster.

The GitHub actions in the new repo is more complete. Not quite sure how it would translate to node-tflite though

jlarmstrongiv commented 3 years ago

Updated docker command for lambda 14.x (used in https://github.com/jlarmstrongiv/libtorchjs):

docker run --entrypoint /bin/bash -v "$PWD":/var/task amazon/aws-lambda-nodejs -c 'yum install tar -y && yum install git -y && yum groupinstall "Development Tools" -y && yum install python3 -y && yum install cmake3 -y && ln -sf /usr/bin/cmake3 /usr/bin/cmake && npm install'

nestarz commented 2 years ago

Here is a Dockerfile to build libtensorflowlite_c.so for Amazon Linux Lambda:

FROM amazon/aws-lambda-nodejs
RUN yum install tar git wget openssl-devel python-pip -y \
    && yum groupinstall "Development Tools" -y \
    && pip install cmake --upgrade
RUN git clone --depth 1 https://github.com/tensorflow/tensorflow.git tensorflow_src
RUN mkdir tflite_build
WORKDIR tflite_build
RUN sed -i -e 's/common.c/common.cc/g' ../tensorflow_src/tensorflow/lite/c/CMakeLists.txt
RUN cmake ../tensorflow_src/tensorflow/lite/c
RUN cmake --build . -j 2

# docker cp my-container:/var/task/tflite_build/libtensorflowlite_c.so .
# OR

RUN yum install python3 -y
COPY package.json ./
RUN npm install
RUN cp /var/task/tflite_build/libtensorflowlite_c.so /var/task/tflite_build/node_modules/node-tflite/build/Release/libtensorflowlite_c.so