Skip to content

Commit 13ee2a2

Browse files
kallebysantosnyannyachaNyannyacha
authored
feat: exposing deno_cache + ai cache interceptor (#446)
* chore: add `deno_cache` crate * feat(sb_core): expose `deno_cache` to js land **NOTE:** saving cache in `temp folder` consider move it to a better location * stamp: add example for cache api * stamp: add `onnxruntime` cache adapter - adding a cache adapter that intercepts `transformers-cache` - fetch and caching models from rust land * fix: tracing wrong import * test(sb_ai): adding `ort` snapshots in a separated file - using a separated file to store `ort` predictions snapshots for both `x64` and `arm64`. * stamp(test): add test command with debug support - Checking for `RUST_LOG` env, then executing tests as `debug` with tracing enabled * fix(sb_ai): apply url check before `load_from_url()` - Applying url check to prevent request errors, matching between `Url` and `Model Bytes`. * test(sb_ai): add integration tests for `ort cache` - Adding tests scenarios for `env.useBrowserCache = true` * stamp: clippy :) * chore: rid all things that are related to model downloading * chore: cleanup ort integration tests * refactor(sb_ai): polishing and cleanup * stamp: cleanup * stamp: polishing * chore: update dependencies * chore: update `Cargo.lock` * stamp(sb_ai): polishing * chore: allow `install_onnx.sh` able to be used in another os * chore: add x86_64 snapshots of ort integration tests * stamp: attach the os prefix to snapshots * stamp: should be cleanup unused sessions while running integration tests * chore: add x86_64 (linux) snapshots of ort integration tests * stamp: snapshots must not be created in CI * stamp: disabling `Web Cache API` for general use - blocking `Web Cache API` to only allow caching of `.onnx` files comming from `transformers.js` lib. * stamp: format * stamp: allow cache dir to be specified through env var * stamp: apply `cargo fmt` * Revert "stamp(test): add test command with debug support" This reverts commit d6bf8a7. * stamp: solve merge conflicts * chore: add aarch64 (osx) snapshots for integration tests (cherry picked from commit 8f9a535) --------- Co-authored-by: Nyannyacha <[email protected]> Co-authored-by: Nyannyacha <[email protected]>
1 parent 997f2d9 commit 13ee2a2

File tree

132 files changed

+2004
-700
lines changed

Some content is hidden

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

132 files changed

+2004
-700
lines changed

.devcontainer/Dockerfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ RUN apt-get update && apt-get install -y build-essential cmake libclang-dev lldb
88
COPY .env /tmp/.env
99
COPY .devcontainer/install.sh /tmp/install.sh
1010
COPY scripts/install_onnx.sh /tmp/install_onnx.sh
11-
COPY scripts/download_models.sh /tmp/download_models.sh
1211

1312
WORKDIR /tmp
1413

15-
ENV SB_AI_MODELS_DIR=/etc/sb_ai/models
1614
ENV DENO_INSTALL=/deno
1715

1816
RUN /tmp/install.sh $TARGETPLATFORM

.devcontainer/install.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ TARGETPLATFORM=$1
66
export $(grep -v '^#' /tmp/.env | xargs)
77

88
# ONNX Runtime
9-
/tmp/install_onnx.sh $ONNXRUNTIME_VERSION $TARGETPLATFORM /tmp/onnxruntime
9+
/tmp/install_onnx.sh $ONNXRUNTIME_VERSION linux $TARGETPLATFORM /tmp/onnxruntime
1010
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
1311

1412
# Ollama
1513
curl -fsSL https://ollama.com/install.sh | sh

.dockerignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ assets/
66
.github/
77

88
scripts/*
9-
!scripts/install_onnx.sh
10-
!scripts/download_models.sh
9+
!scripts/install_onnx.sh

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
envFile: ".env"
5858

5959
- name: Install ONNX Runtime Library
60-
run: ./scripts/install_onnx.sh ${{ env.ONNXRUNTIME_VERSION }} x64 /tmp/onnxruntime
60+
run: ./scripts/install_onnx.sh ${{ env.ONNXRUNTIME_VERSION }} linux x64 /tmp/onnxruntime
6161

6262
- name: Install S3 credentials for testing
6363
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ deno_ast = { version = "=0.40.0", features = ["transpiling"] }
2626
deno_core = "0.293.0"
2727
deno_broadcast_channel = "0.155.0"
2828
deno_canvas = "0.30.0"
29+
deno_cache = "0.93.0"
2930
deno_console = "0.161.0"
3031
deno_crypto = "0.175.0"
3132
deno_fetch = "0.185.0"
@@ -156,4 +157,4 @@ debug-assertions = false
156157

157158
[profile.release]
158159
debug = true
159-
lto = "thin"
160+
lto = "thin"

Dockerfile

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,13 @@ COPY --from=builder /root/edge-runtime.debug /usr/local/bin/edge-runtime.debug
4040
# ONNX Runtime provider
4141
# Application runtime with ONNX
4242
FROM builder as ort
43-
RUN ./scripts/install_onnx.sh $ONNXRUNTIME_VERSION $TARGETPLATFORM /root/onnxruntime
43+
RUN ./scripts/install_onnx.sh $ONNXRUNTIME_VERSION linux $TARGETPLATFORM /root/onnxruntime
4444

4545

4646
# ONNX Runtime CUDA provider
4747
# Application runtime with ONNX CUDA
4848
FROM builder as ort-cuda
49-
RUN ./scripts/install_onnx.sh $ONNXRUNTIME_VERSION $TARGETPLATFORM /root/onnxruntime --gpu
50-
51-
52-
FROM builder as preload-models
53-
RUN ./scripts/download_models.sh
49+
RUN ./scripts/install_onnx.sh $ONNXRUNTIME_VERSION linux $TARGETPLATFORM /root/onnxruntime --gpu
5450

5551

5652
# With CUDA
@@ -59,7 +55,6 @@ FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04 as edge-runtime-cuda
5955
COPY --from=edge-runtime-base /usr/local/bin/edge-runtime /usr/local/bin/edge-runtime
6056
COPY --from=builder /root/edge-runtime.debug /usr/local/bin/edge-runtime.debug
6157
COPY --from=ort-cuda /root/onnxruntime/lib/libonnxruntime.so* /usr/lib
62-
COPY --from=preload-models /usr/src/edge-runtime/models /etc/sb_ai/models
6358

6459
ENV NVIDIA_VISIBLE_DEVICES=all
6560
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility
@@ -70,6 +65,5 @@ ENTRYPOINT ["edge-runtime"]
7065
# Base
7166
FROM edge-runtime-base as edge-runtime
7267
COPY --from=ort /root/onnxruntime/lib/libonnxruntime.so* /usr/lib
73-
COPY --from=preload-models /usr/src/edge-runtime/models /etc/sb_ai/models
7468

7569
ENTRYPOINT ["edge-runtime"]

crates/base/Cargo.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ deno_ast.workspace = true
88
deno_fs.workspace = true
99
deno_io.workspace = true
1010
deno_core.workspace = true
11+
deno_cache.workspace = true
1112
deno_console.workspace = true
1213
deno_config.workspace = true
1314
deno_crypto.workspace = true
@@ -43,6 +44,7 @@ sb_node = { version = "0.1.0", path = "../node" }
4344
sb_ai = { version = "0.1.0", path = "../sb_ai" }
4445
sb_fs = { version = "0.1.0", path = "../sb_fs" }
4546

47+
libc.workspace = true
4648
async-trait.workspace = true
4749
thiserror.workspace = true
4850
monch.workspace = true
@@ -77,6 +79,7 @@ tracing.workspace = true
7779
reqwest_v011.workspace = true
7880
tracing-subscriber = { workspace = true, optional = true, features = ["env-filter", "tracing-log"] }
7981
num-traits.workspace = true
82+
tempfile.workspace = true
8083

8184
tls-listener = { version = "0.10", features = ["rustls"] }
8285
strum = { version = "0.25", features = ["derive"] }
@@ -85,6 +88,9 @@ cooked-waker = "5"
8588
tokio-rustls = "0.25.0"
8689
arc-swap = "1.7"
8790

91+
[target.'cfg(windows)'.dependencies]
92+
winapi = { workspace = true, features = ["knownfolders", "mswsock", "objbase", "shlobj", "tlhelp32", "winbase", "winerror", "winsock2"] }
93+
8894
[dev-dependencies]
8995
tokio-util = { workspace = true, features = ["rt", "compat"] }
9096
tracing-subscriber = { workspace = true, features = ["env-filter", "tracing-log"] }
@@ -100,6 +106,7 @@ deno_io.workspace = true
100106
deno_console.workspace = true
101107
deno_crypto.workspace = true
102108
deno_fetch.workspace = true
109+
deno_cache.workspace = true
103110
deno_http.workspace = true
104111
deno_net.workspace = true
105112
deno_url.workspace = true
@@ -134,4 +141,4 @@ url.workspace = true
134141

135142
[features]
136143
tracing = ["dep:tracing-subscriber"]
137-
termination-signal-ext = []
144+
termination-signal-ext = []

crates/base/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::path::PathBuf;
44

55
mod supabase_startup_snapshot {
66
use super::*;
7+
use deno_cache::SqliteBackedCache;
78
use deno_core::error::AnyError;
89
use deno_core::snapshot::{create_snapshot, CreateSnapshotOptions};
910
use deno_core::Extension;
@@ -225,6 +226,10 @@ mod supabase_startup_snapshot {
225226
sb_core_http::init_ops_and_esm(),
226227
sb_core_http_start::init_ops_and_esm(),
227228
deno_node::init_ops_and_esm::<Permissions>(None, None, fs),
229+
// NOTE(kallebysantos):
230+
// Full `Web Cache API` via `SqliteBackedCache` is disabled. Cache flow is
231+
// handled by `sb_ai: Cache Adapter`
232+
deno_cache::deno_cache::init_ops_and_esm::<SqliteBackedCache>(None),
228233
sb_core_runtime::init_ops_and_esm(None),
229234
];
230235

crates/base/src/deno_runtime.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use base_rt::DenoRuntimeDropToken;
1313
use base_rt::{get_current_cpu_time_ns, BlockingScopeCPUUsage};
1414
use cooked_waker::{IntoWaker, WakeRef};
1515
use ctor::ctor;
16+
use deno_cache::SqliteBackedCache;
1617
use deno_core::error::{AnyError, JsError};
1718
use deno_core::url::Url;
1819
use deno_core::v8::{self, GCCallbackFlags, GCType, HeapStatistics, Isolate};
@@ -526,6 +527,30 @@ where
526527
Arc::new(DenoCompileFileSystem::from_rc(vfs))
527528
})?;
528529

530+
/* NOTE(kallebysantos): Cache via SqliteBackedCache is disabled.
531+
*
532+
* ```
533+
* let cache_base_dir = dirs::cache_dir()
534+
* .context("could not resolve cache directory")?
535+
* .join("web_caches");
536+
*
537+
* tokio::fs::create_dir_all(cache_base_dir.as_path())
538+
* .await
539+
* .context("could not make cache directory")?;
540+
*
541+
* struct CacheStorageDir(TempDir);
542+
*
543+
* let cache_storage_dir = CacheStorageDir(
544+
* tempfile::tempdir_in(cache_base_dir).context("could not make cache directory")?,
545+
* );
546+
*
547+
* let cache_backend = CreateCache(Arc::new({
548+
* let dir = cache_storage_dir.0.path().to_path_buf();
549+
* move || SqliteBackedCache::new(dir.clone())
550+
* }));
551+
* ```
552+
*/
553+
529554
let mod_code = module_code;
530555
let extensions = vec![
531556
sb_core_permissions::init_ops(net_access_disabled, allow_net),
@@ -575,6 +600,7 @@ where
575600
Some(npm_resolver),
576601
file_system,
577602
),
603+
deno_cache::deno_cache::init_ops::<SqliteBackedCache>(None),
578604
sb_core_runtime::init_ops(Some(main_module_url.clone())),
579605
];
580606

@@ -694,11 +720,12 @@ where
694720
let version: Option<&str> = option_env!("GIT_V_TAG");
695721

696722
{
697-
// @andreespirela : We do this because "NODE_DEBUG" is trying to be read during
698-
// initialization, But we need the gotham state to be up-to-date
699723
let op_state_rc = js_runtime.op_state();
700724
let mut op_state = op_state_rc.borrow_mut();
701-
op_state.put::<sb_env::EnvVars>(sb_env::EnvVars::new());
725+
726+
// NOTE(Andreespirela): We do this because "NODE_DEBUG" is trying to be read during
727+
// initialization, But we need the gotham state to be up-to-date.
728+
op_state.put(sb_env::EnvVars::default());
702729
}
703730

704731
// Bootstrapping stage
@@ -805,8 +832,8 @@ where
805832
}
806833
}
807834

808-
op_state.put::<sb_env::EnvVars>(env_vars);
809-
op_state.put(DenoRuntimeDropToken(drop_token.clone()))
835+
op_state.put(sb_env::EnvVars(env_vars));
836+
op_state.put(DenoRuntimeDropToken(drop_token.clone()));
810837
}
811838

812839
let main_module_id = {

0 commit comments

Comments
 (0)