diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cd4d37d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "rust-analyzer.runnables.extraEnv": { + "RUSTFLAGS": "--cfg=web_sys_unstable_apis" + }, + "rust-analyzer.cargo.extraEnv": { + "RUSTFLAGS": "--cfg=web_sys_unstable_apis" + } +} diff --git a/Cargo.toml b/Cargo.toml index 4a01085..f9c73d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,9 @@ repository = "https://github.com/text-yoga/transformers-wasm" [lib] crate-type = ["cdylib", "rlib"] +[build] +rustflags = ["--cfg=web_sys_unstable_apis"] + [features] default = ["console_error_panic_hook"] @@ -45,7 +48,11 @@ features = [ 'RequestMode', 'Response', 'Window', + 'Navigator', + 'Gpu', + 'WgslLanguageFeatures' ] + version = "0.3.64" [dev-dependencies] diff --git a/src/utils.rs b/src/utils.rs index 691ff5f..2ef882a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,7 +3,7 @@ use js_sys::{ArrayBuffer, Uint8Array}; use wasm_bindgen::{prelude::*, JsValue}; use wasm_bindgen_futures::JsFuture; -use web_sys::{Request, RequestInit, RequestMode, Response}; +use web_sys::{Navigator, Request, RequestInit, RequestMode, Response, Window}; pub fn set_panic_hook() { // When the `console_error_panic_hook` feature is enabled, we can call the @@ -48,3 +48,20 @@ pub async fn load_binary(url: &str) -> Result, JsValue> { log!(url, x); Ok(vec) } + +#[cfg(web_sys_unstable_apis)] +pub async fn has_gpu() -> bool { + let window = web_sys::window().expect("no global `window` exists"); + let navigator = window.navigator(); + + let gpu: web_sys::Gpu = navigator.gpu(); + let has_gpu_check = JsFuture::from(gpu.request_adapter()).await; + + let mut has_gpu = false; + match has_gpu_check { + Ok(_) => has_gpu = true, + Err(err) => {} + } + log!("wgsl_language_features"); + has_gpu +} diff --git a/tests/web.rs b/tests/web.rs index 0f57f19..307c865 100644 --- a/tests/web.rs +++ b/tests/web.rs @@ -13,13 +13,15 @@ use transformers_wasm::utils; use wasm_bindgen::{prelude::*, JsValue}; use wasm_bindgen_futures::JsFuture; use wasm_bindgen_test::*; -use web_sys::console; -use web_sys::{Request, RequestInit, RequestMode, Response}; +use web_sys::{console, Request, RequestInit, RequestMode, Response}; wasm_bindgen_test_configure!(run_in_browser); #[wasm_bindgen_test] async fn pass() -> Result<(), JsValue> { + #[cfg(web_sys_unstable_apis)] + log!(utils::has_gpu().await); + let tokenizer_url = "http://localhost:31300/TinyLlama_TinyLlama-1.1B-Chat-v1.0/tokenizer.json"; let model_url = "http://localhost:31300/TheBloke_TinyLlama-1.1B-Chat-v1.0-GGUF/tinyllama-1.1b-chat-v1.0.Q4_K_M.gguf";