Skip to content

Commit 1052be2

Browse files
feat: exposing onnx backend to JS land (#436)
* feat: integrate `trasnformers.js` with rust backend Simple integration between `transformers.js` and rust ort backend by exposing js owned API * stamp: refactoring tensors ser/de to try zero-copy - Implementing v8 traits to cast tensors `ToV8` and `FromV8`. - Resolving `ort::Tensor` type based on Js tensor's. * stamp: refactoring to use `serde_v8` - Since `serde_v8` allows zero-copy we use it to handle the model inputs and send back the outputs * fix(ai): seq2seq models causing null pointer error - Solved the "`GetMutableData` should not be a null pointer" error while executing seq2seq models. - Ref.: pykeio/ort#185 * test(sb_ai): implementing tests for ort backend - Applying integration tests over all NLP pipelines with ort backend * stamp(sb_ai): example for generate image embeddings * test(sb_ai): implementing computer vision tests for ort backend - Applying integration tests over VISION pipelines with ort backend * stamp: clippy * fix(ci): makes share common env vars from dotenv file * fix(ci): update `ORT_DYLIB_PATH` * fix(ci): makes share common env vars from dotenv file * chore(sb_ai): update dependencies * chore(event_worker): add a dependency * chore(event_worker): install a tracing macro * chore(base): update `Cargo.toml` * chore(base): trace `malloced_mb` more precisely * chore: update an integration test case script * chore: install tracing subscriber when `base/tracing` feature is enabled * chore: update `Cargo.lock` * stamp: add `docker build` script with shared envs * fix(devcontainer): shared `.env` file path --------- Co-authored-by: Nyannyacha <[email protected]>
1 parent 1bb7f70 commit 1052be2

File tree

45 files changed

+1576
-79
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1576
-79
lines changed

.devcontainer/Dockerfile

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
11
FROM mcr.microsoft.com/devcontainers/rust:dev-1-bookworm
22

33
ARG TARGETPLATFORM
4-
ARG ONNXRUNTIME_VERSION
5-
ARG DENO_VERSION
64

75
RUN apt-get update && apt-get install -y build-essential cmake libclang-dev lldb \
86
nodejs npm hyperfine
97

8+
COPY .env /tmp/.env
9+
COPY .devcontainer/install.sh /tmp/install.sh
1010
COPY scripts/install_onnx.sh /tmp/install_onnx.sh
1111
COPY scripts/download_models.sh /tmp/download_models.sh
1212

1313
WORKDIR /tmp
14-
RUN ./install_onnx.sh $ONNXRUNTIME_VERSION $TARGETPLATFORM /usr/local/bin/libonnxruntime.so
15-
RUN ./download_models.sh
16-
RUN mkdir -p /etc/sb_ai && cp -r /tmp/models /etc/sb_ai/models
1714

18-
ENV ORT_DYLIB_PATH=/usr/local/bin/libonnxruntime.so
1915
ENV SB_AI_MODELS_DIR=/etc/sb_ai/models
20-
21-
# Ollama
22-
RUN curl -fsSL https://ollama.com/install.sh | sh
23-
24-
# Deno
2516
ENV DENO_INSTALL=/deno
26-
RUN mkdir -p /deno \
27-
&& curl -fsSL https://deno.land/install.sh | bash -s -- v$DENO_VERSION \
28-
&& chown -R vscode /deno
17+
18+
RUN /tmp/install.sh $TARGETPLATFORM

.devcontainer/devcontainer.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
"name": "Rust",
33
"build": {
44
"dockerfile": "Dockerfile",
5-
"context": "..",
6-
"args": {
7-
"ONNXRUNTIME_VERSION": "1.19.2",
8-
"DENO_VERSION": "1.45.2"
9-
}
5+
"context": ".."
106
},
117
"containerEnv": {
128
"PATH": "${localEnv:PATH}:/deno/bin"
@@ -16,6 +12,8 @@
1612
"ghcr.io/jungaretti/features/make:1": {}
1713
},
1814
"runArgs": [
15+
"--env-file",
16+
".env",
1917
"--rm",
2018
"--privileged",
2119
"--security-opt",

.devcontainer/install.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
TARGETPLATFORM=$1
5+
6+
export $(grep -v '^#' /tmp/.env | xargs)
7+
8+
# ONNX Runtime
9+
/tmp/install_onnx.sh $ONNXRUNTIME_VERSION $TARGETPLATFORM /tmp/onnxruntime
10+
mv /tmp/onnxruntime/lib/libonnxruntime.so* /usr/lib
11+
/tmp/download_models.sh
12+
mkdir -p /etc/sb_ai && cp -r /tmp/models /etc/sb_ai/models
13+
14+
# Ollama
15+
curl -fsSL https://ollama.com/install.sh | sh
16+
17+
# Deno
18+
mkdir -p /deno
19+
curl -fsSL https://deno.land/install.sh | bash -s -- v$DENO_VERSION
20+
chown -R vscode /deno

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ONNXRUNTIME_VERSION=1.19.2
2+
DENO_VERSION=1.45.2
3+
EDGE_RUNTIME_PORT=9998
4+
AI_INFERENCE_API_HOST=http://localhost:11434

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ env:
1212
CARGO_NET_RETRY: 10
1313
CARGO_TERM_COLOR: always
1414
RUSTUP_MAX_RETRIES: 10
15+
ORT_DYLIB_PATH: /tmp/onnxruntime/lib/libonnxruntime.so
1516

1617
jobs:
1718
cargo-fmt:
@@ -49,4 +50,9 @@ jobs:
4950
- uses: actions/checkout@v4
5051
- run: rustup show
5152
- uses: Swatinem/rust-cache@v2
53+
- uses: cardinalby/export-env-action@v2
54+
with:
55+
envFile: ".env"
56+
- name: Install ONNX Runtime Library
57+
run: ./scripts/install_onnx.sh ${{ env.ONNXRUNTIME_VERSION }} x64 /tmp/onnxruntime
5258
- run: ./scripts/test.sh

.github/workflows/release.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ jobs:
5252
username: ${{ secrets.DOCKER_USERNAME }}
5353
password: ${{ secrets.DOCKER_PASSWORD }}
5454

55+
- uses: cardinalby/export-env-action@v2
56+
with:
57+
envFile: ".env"
5558
- id: build
5659
uses: docker/build-push-action@v3
5760
with:
@@ -61,7 +64,8 @@ jobs:
6164
cache-from: type=gha
6265
cache-to: type=gha,mode=max
6366
build-args: |
64-
GIT_V_VERSION=${{ needs.release.outputs.version }}
67+
GIT_V_TAG=${{ needs.release.outputs.version }}
68+
ONNXRUNTIME_VERSION=${{ env.ONNXRUNTIME_VERSION }}
6569
6670
publish_arm:
6771
needs:
@@ -95,6 +99,9 @@ jobs:
9599
image=moby/buildkit:master
96100
network=host
97101
102+
- uses: cardinalby/export-env-action@v2
103+
with:
104+
envFile: ".env"
98105
- id: build
99106
uses: docker/build-push-action@v3
100107
with:
@@ -104,7 +111,8 @@ jobs:
104111
tags: ${{ steps.meta.outputs.tags }}
105112
no-cache: true
106113
build-args: |
107-
GIT_V_VERSION=${{ needs.release.outputs.version }}
114+
GIT_V_TAG=${{ needs.release.outputs.version }}
115+
ONNXRUNTIME_VERSION=${{ env.ONNXRUNTIME_VERSION }}
108116
109117
merge_manifest:
110118
needs: [release, publish_x86, publish_arm]

Cargo.lock

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Dockerfile

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
FROM rust:1.79.0-bookworm as builder
44

55
ARG TARGETPLATFORM
6-
ARG GIT_V_VERSION
7-
ARG ONNXRUNTIME_VERSION=1.19.2
6+
ARG ONNXRUNTIME_VERSION
7+
ARG GIT_V_TAG
88
ARG PROFILE=release
99
ARG FEATURES
1010

@@ -15,7 +15,7 @@ WORKDIR /usr/src/edge-runtime
1515
COPY . .
1616

1717
RUN --mount=type=cache,target=/usr/local/cargo/registry,id=${TARGETPLATFORM} --mount=type=cache,target=/usr/src/edge-runtime/target,id=${TARGETPLATFORM} \
18-
GIT_V_TAG=${GIT_V_VERSION} cargo build --profile ${PROFILE} --features "${FEATURES}" && \
18+
${GIT_V_TAG} cargo build --profile ${PROFILE} --features "${FEATURES}" && \
1919
mv /usr/src/edge-runtime/target/${PROFILE}/edge-runtime /root
2020

2121
RUN objcopy --compress-debug-sections \
@@ -36,8 +36,6 @@ RUN apt-get remove -y perl && apt-get autoremove -y
3636
COPY --from=builder /root/edge-runtime /usr/local/bin/edge-runtime
3737
COPY --from=builder /root/edge-runtime.debug /usr/local/bin/edge-runtime.debug
3838

39-
ENV ORT_DYLIB_PATH=/usr/local/bin/onnxruntime/lib/libonnxruntime.so
40-
4139

4240
# ONNX Runtime provider
4341
# Application runtime with ONNX
@@ -60,10 +58,9 @@ FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04 as edge-runtime-cuda
6058

6159
COPY --from=edge-runtime-base /usr/local/bin/edge-runtime /usr/local/bin/edge-runtime
6260
COPY --from=builder /root/edge-runtime.debug /usr/local/bin/edge-runtime.debug
63-
COPY --from=ort-cuda /root/onnxruntime /usr/local/bin/onnxruntime
61+
COPY --from=ort-cuda /root/onnxruntime/lib/libonnxruntime.so* /usr/lib
6462
COPY --from=preload-models /usr/src/edge-runtime/models /etc/sb_ai/models
6563

66-
ENV ORT_DYLIB_PATH=/usr/local/bin/onnxruntime/lib/libonnxruntime.so
6764
ENV NVIDIA_VISIBLE_DEVICES=all
6865
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility
6966

@@ -72,7 +69,7 @@ ENTRYPOINT ["edge-runtime"]
7269

7370
# Base
7471
FROM edge-runtime-base as edge-runtime
75-
COPY --from=ort /root/onnxruntime /usr/local/bin/onnxruntime
72+
COPY --from=ort /root/onnxruntime/lib/libonnxruntime.so* /usr/lib
7673
COPY --from=preload-models /usr/src/edge-runtime/models /etc/sb_ai/models
7774

7875
ENTRYPOINT ["edge-runtime"]

crates/base/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ notify.workspace = true
7474
pin-project.workspace = true
7575
rustls-pemfile.workspace = true
7676
tracing.workspace = true
77+
tracing-subscriber = { workspace = true, optional = true, features = ["env-filter", "tracing-log"] }
7778

7879
reqwest_v011 = { package = "reqwest", version = "0.11", features = ["stream", "json", "multipart"] }
7980
tls-listener = { version = "0.10", features = ["rustls"] }
@@ -129,4 +130,5 @@ tokio.workspace = true
129130
url.workspace = true
130131

131132
[features]
133+
tracing = ["dep:tracing-subscriber"]
132134
termination-signal-ext = []

crates/base/src/deno_runtime.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ impl MemCheck {
190190
}
191191
}
192192

193+
trace!(malloced_mb = bytes_to_display(total_bytes as u64));
193194
total_bytes
194195
}
195196
}
@@ -965,11 +966,9 @@ where
965966

966967
if is_user_worker {
967968
let mem_state = mem_check_state.as_ref().unwrap();
968-
let total_malloced_bytes = mem_state.check(js_runtime.v8_isolate().as_mut());
969969

970+
mem_state.check(js_runtime.v8_isolate().as_mut());
970971
mem_state.waker.register(waker);
971-
972-
trace!(malloced_mb = bytes_to_display(total_malloced_bytes as u64));
973972
}
974973

975974
// NOTE(Nyannyacha): If tasks are empty or V8 is not evaluating the

0 commit comments

Comments
 (0)