Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add ratchet-downloader #23

Closed
wants to merge 14 commits into from
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/ratchet-core",
"crates/ratchet-downloader",
"crates/ratchet-integration-tests",
"crates/ratchet-loader",
"crates/ratchet-models",
Expand Down Expand Up @@ -28,6 +29,7 @@ derive-new = "0.6.0"
log = "0.4.20"
thiserror = "1.0.56"
byteorder = "1.5.0"
wasm-bindgen-test = "0.3.34"

[workspace.dev-dependencies]
hf-hub = "0.3.0"
1 change: 1 addition & 0 deletions crates/ratchet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ glam = "0.25.0"
pollster = "0.3.0"
futures-intrusive = "0.5.0"
anyhow = "1.0.79"
getrandom = { version = "0.2", features = ["js"] } # Needed for wasm support in `num` trait
num = "0.4.1"
rand_distr = { version = "0.4.3", optional = true }
rand = { version = "0.8.4", optional = true }
Expand Down
12 changes: 6 additions & 6 deletions crates/ratchet-core/src/gpu/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl PartialEq for WgpuDevice {
impl WgpuDevice {
pub async fn new() -> Result<Self, DeviceError> {
#[cfg(target_arch = "wasm32")]
let adapter = Self::select_adapter().await;
let adapter = Self::select_adapter().await?;
#[cfg(not(target_arch = "wasm32"))]
let adapter = Self::select_adapter()?;

Expand Down Expand Up @@ -106,7 +106,7 @@ impl WgpuDevice {
}

#[cfg(target_arch = "wasm32")]
async fn select_adapter() -> Adapter {
async fn select_adapter() -> Result<Adapter, DeviceError> {
let instance = wgpu::Instance::default();
let backends = wgpu::util::backend_bits_from_env().unwrap_or(wgpu::Backends::PRIMARY);
instance
Expand All @@ -116,10 +116,10 @@ impl WgpuDevice {
force_fallback_adapter: false,
})
.await
.map_err(|e| {
log::error!("Failed to create device: {:?}", e);
e
})?
.ok_or({
log::error!("Failed to request adapter.");
DeviceError::AdapterRequestFailed
})
}

#[cfg(not(target_arch = "wasm32"))]
Expand Down
6 changes: 3 additions & 3 deletions crates/ratchet-core/src/kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ lazy_static! {
m.insert(
"qgemm_vec4",
include_str!(
"/Users/fleetwood/Code/ratchet/crates/ratchet-core/kernels/qgemm_vec4.wgsl"
"/Users/janschulte/code/ratchet/crates/ratchet-core/kernels/qgemm_vec4.wgsl"
),
);
m.insert(
"sgemm_scalar",
include_str!(
"/Users/fleetwood/Code/ratchet/crates/ratchet-core/kernels/sgemm_scalar.wgsl"
"/Users/janschulte/code/ratchet/crates/ratchet-core/kernels/sgemm_scalar.wgsl"
),
);
m.insert(
"add_scalar",
include_str!(
"/Users/fleetwood/Code/ratchet/crates/ratchet-core/kernels/add_scalar.wgsl"
"/Users/janschulte/code/ratchet/crates/ratchet-core/kernels/add_scalar.wgsl"
),
);
m
Expand Down
41 changes: 41 additions & 0 deletions crates/ratchet-downloader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "ratchet-downloader"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ratchet-loader = { path = "../ratchet-loader" }
wasm-bindgen = "0.2.84"
wasm-bindgen-futures = "0.4.39"
js-sys = "0.3.64"
gloo = "0.11.0"
wasm-streams = "0.4.0"
futures-util = { version = "^0.3.28", features = ["io", "sink"] }
winnow = "0.5.34"
circular = "0.3.0"
anyhow.workspace = true

[dependencies.web-sys]
features = [
'console',
'Headers',
'Request',
'RequestInit',
'RequestMode',
'Response',
'ReadableStream',
'ReadableStreamGetReaderOptions',
'ReadableStreamReaderMode',
'Window',
'Navigator',
'StorageManager',
'CacheStorage'
]
version = "0.3.64"

[dev-dependencies]
wasm-bindgen-test.workspace = true

[lib]
crate-type = ["cdylib", "rlib"]
35 changes: 35 additions & 0 deletions crates/ratchet-downloader/src/fetch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use js_sys::{ArrayBuffer, Uint8Array, JSON};

use wasm_bindgen::{prelude::*, JsValue};
use wasm_bindgen_futures::JsFuture;
use web_sys::{Request, RequestInit, RequestMode, Response};

fn to_error(value: JsValue) -> JsError {
JsError::new(
JSON::stringify(&value)
.map(|js_string| {
js_string
.as_string()
.unwrap_or(String::from("An unknown error occurred."))
})
.unwrap_or(String::from("An unknown error occurred."))
.as_str(),
)
}
pub(crate) async fn fetch(url: &str) -> Result<Response, JsError> {
let mut opts = RequestInit::new();
opts.method("GET");
opts.mode(RequestMode::Cors);

let request = Request::new_with_str_and_init(&url, &opts).map_err(to_error)?;

let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_request(&request))
.await
.map_err(to_error)?;

assert!(resp_value.is_instance_of::<Response>());
let resp: Response = resp_value.dyn_into().unwrap();

Ok(resp)
}
1 change: 1 addition & 0 deletions crates/ratchet-downloader/src/huggingface/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod repo;
7 changes: 7 additions & 0 deletions crates/ratchet-downloader/src/huggingface/repo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub struct Repo {
pub id: String,
pub revision: String,
pub repo_type: String,
}

impl Repo {}
Loading
Loading