rullama 0.5.0

Browser-resident Gemma 4 inference: pure Rust → WebAssembly + WebGPU. Loads Ollama's on-disk GGUF blobs and runs the forward pass on the local GPU via hand-written WGSL.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! Tensor-byte source abstraction.
//!
//! The GGUF parser used to own the raw `Vec<u8>` for the entire model file. That kept
//! ~7 GB in CPU memory for the lifetime of the `Model` — fine on native, fatal on
//! wasm32 (4 GB linear-memory cap). M6 splits storage from access: the parser keeps
//! only the small (5–10 MB) header, and individual tensor reads go through this trait.
//!
//! Two implementations:
//!   * [`InMemoryFetcher`] — wraps a `Vec<u8>` (or `Arc<[u8]>`). Used by native callers
//!     and by tests. `fetch` is a memcpy of the requested slice.
//!   * `HttpRangeFetcher` (M6.4) — `fetch()` issues an HTTP `Range: bytes=N-M` request.
//!     Only useful in browsers; not built on native.

use std::sync::Arc;

use async_trait::async_trait;

use crate::error::{Result, RullamaError};

/// Read tensor bytes on demand by absolute offset.
///
/// The trait is `?Send` because `wasm32-unknown-unknown` is single-threaded and
/// browser-side futures are not `Send`. Native impls happen to be `Send + Sync` but
/// we don't require it on the trait.
#[async_trait(?Send)]
pub trait TensorFetcher {
    /// Total length of the underlying source in bytes (used by the parser to bounds-check).
    fn total_len(&self) -> u64;

    /// Read `len` bytes starting at `offset`. Caller owns the returned `Vec<u8>` and
    /// is expected to drop it as soon as the data has been copied to its final home
    /// (e.g. a wgpu buffer).
    async fn fetch(&self, offset: u64, len: u64) -> Result<Vec<u8>>;
}

// ---------- InMemoryFetcher ----------

/// Wraps a `Vec<u8>` (or any `Arc<[u8]>`) so existing in-memory callers fit the trait
/// without refactoring. The fetch is a `Vec::from(&slice)` — synchronous in body,
/// async in signature so it shares the trait surface with [`HttpRangeFetcher`].
pub struct InMemoryFetcher {
    bytes: Arc<[u8]>,
}

impl InMemoryFetcher {
    pub fn new(bytes: Vec<u8>) -> Self {
        Self {
            bytes: bytes.into(),
        }
    }

    pub fn from_arc(bytes: Arc<[u8]>) -> Self {
        Self { bytes }
    }

    /// Borrow the full source bytes (zero-copy). Only useful when the caller can keep
    /// the fetcher alive longer than the borrow.
    pub fn as_slice(&self) -> &[u8] {
        &self.bytes
    }
}

#[async_trait(?Send)]
impl TensorFetcher for InMemoryFetcher {
    fn total_len(&self) -> u64 {
        self.bytes.len() as u64
    }

    async fn fetch(&self, offset: u64, len: u64) -> Result<Vec<u8>> {
        let start = offset as usize;
        let end = start.checked_add(len as usize).ok_or_else(|| {
            RullamaError::Gguf(format!("InMemoryFetcher: range overflow {offset}+{len}"))
        })?;
        if end > self.bytes.len() {
            return Err(RullamaError::Gguf(format!(
                "InMemoryFetcher: range {start}..{end} extends past buffer end ({})",
                self.bytes.len()
            )));
        }
        Ok(self.bytes[start..end].to_vec())
    }
}

// ---------- FileFetcher (native-only) ----------

/// Native fetcher that reads byte ranges straight from an on-disk GGUF via
/// positioned reads (`read_at` — no seek state, no mutex). Lets native
/// examples and the CPU oracle stream blobs bigger than RAM (e.g. the 18 GB
/// `gemma4:26b`) instead of `fs::read`ing the whole file.
#[cfg(not(target_arch = "wasm32"))]
pub struct FileFetcher {
    file: std::fs::File,
    len: u64,
}

#[cfg(not(target_arch = "wasm32"))]
impl FileFetcher {
    pub fn open(path: &std::path::Path) -> Result<Self> {
        let file = std::fs::File::open(path)
            .map_err(|e| RullamaError::Gguf(format!("open {}: {e}", path.display())))?;
        let len = file
            .metadata()
            .map_err(|e| RullamaError::Gguf(format!("stat {}: {e}", path.display())))?
            .len();
        Ok(Self { file, len })
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[async_trait(?Send)]
impl TensorFetcher for FileFetcher {
    fn total_len(&self) -> u64 {
        self.len
    }

    async fn fetch(&self, offset: u64, len: u64) -> Result<Vec<u8>> {
        use std::os::unix::fs::FileExt;
        let end = offset.checked_add(len).ok_or_else(|| {
            RullamaError::Gguf(format!("FileFetcher: range overflow {offset}+{len}"))
        })?;
        if end > self.len {
            return Err(RullamaError::Gguf(format!(
                "FileFetcher: range {offset}..{end} extends past file end ({})",
                self.len
            )));
        }
        let mut buf = vec![0u8; len as usize];
        self.file
            .read_exact_at(&mut buf, offset)
            .map_err(|e| RullamaError::Gguf(format!("FileFetcher read {offset}+{len}: {e}")))?;
        Ok(buf)
    }
}

// ---------- HttpRangeFetcher (wasm32-only) ----------

/// Browser-side fetcher that pulls byte ranges from an HTTP URL via `fetch()` with a
/// `Range: bytes=N-M` header. Native callers don't need this — they wrap the bytes
/// they already have in [`InMemoryFetcher`] — so the impl lives behind `cfg(wasm32)`.
///
/// Construction is async because we need the total file length (read from the
/// Content-Length of an initial HEAD-equivalent Range request) before any tensor
/// reads can be bounds-checked.
#[cfg(target_arch = "wasm32")]
pub struct HttpRangeFetcher {
    url: String,
    total: u64,
}

#[cfg(target_arch = "wasm32")]
impl HttpRangeFetcher {
    /// Build a fetcher for `url`. Issues one initial Range request to discover the
    /// total size (read from `Content-Range` or `X-Total-Size`).
    pub async fn new(url: String) -> Result<Self> {
        use wasm_bindgen::JsCast;
        use wasm_bindgen_futures::JsFuture;

        let req_init = web_sys::RequestInit::new();
        req_init.set_method("GET");
        let headers = web_sys::Headers::new()
            .map_err(|e| RullamaError::Gguf(format!("Headers::new: {e:?}")))?;
        headers
            .set("Range", "bytes=0-0")
            .map_err(|e| RullamaError::Gguf(format!("set Range: {e:?}")))?;
        req_init.set_headers(&headers);

        let request = web_sys::Request::new_with_str_and_init(&url, &req_init)
            .map_err(|e| RullamaError::Gguf(format!("Request::new: {e:?}")))?;

        let resp_value = JsFuture::from(global_fetch(&request)?)
            .await
            .map_err(|e| RullamaError::Gguf(format!("fetch failed: {e:?}")))?;
        let resp: web_sys::Response = resp_value
            .dyn_into()
            .map_err(|e| RullamaError::Gguf(format!("response cast: {e:?}")))?;
        if !resp.ok() && resp.status() != 206 {
            return Err(RullamaError::Gguf(format!(
                "HTTP {} from {url}",
                resp.status()
            )));
        }

        // Prefer Content-Range "bytes 0-0/<total>"; fall back to X-Total-Size.
        let total = if let Some(cr) = resp.headers().get("Content-Range").ok().flatten() {
            cr.rsplit('/')
                .next()
                .and_then(|s| s.parse::<u64>().ok())
                .ok_or_else(|| RullamaError::Gguf(format!("bad Content-Range: {cr}")))?
        } else if let Some(xs) = resp.headers().get("X-Total-Size").ok().flatten() {
            xs.parse::<u64>()
                .map_err(|e| RullamaError::Gguf(format!("bad X-Total-Size: {e}")))?
        } else {
            return Err(RullamaError::Gguf(
                "server returned no Content-Range or X-Total-Size; cannot determine GGUF length"
                    .into(),
            ));
        };

        Ok(Self { url, total })
    }
}

#[cfg(target_arch = "wasm32")]
fn global_fetch(request: &web_sys::Request) -> Result<js_sys::Promise> {
    use wasm_bindgen::JsCast;
    // Works in both Window and DedicatedWorkerGlobalScope contexts.
    let global = js_sys::global();
    if let Some(window) = global.dyn_ref::<web_sys::Window>() {
        return Ok(window.fetch_with_request(request));
    }
    if let Some(scope) = global.dyn_ref::<web_sys::WorkerGlobalScope>() {
        return Ok(scope.fetch_with_request(request));
    }
    Err(RullamaError::Gguf(
        "no Window or WorkerGlobalScope for fetch()".into(),
    ))
}

#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
impl TensorFetcher for HttpRangeFetcher {
    fn total_len(&self) -> u64 {
        self.total
    }

    async fn fetch(&self, offset: u64, len: u64) -> Result<Vec<u8>> {
        use wasm_bindgen::JsCast;
        use wasm_bindgen_futures::JsFuture;

        if len == 0 {
            return Ok(Vec::new());
        }
        let end = offset.checked_add(len - 1).ok_or_else(|| {
            RullamaError::Gguf(format!("HttpRangeFetcher: range overflow {offset}+{len}"))
        })?;
        if end >= self.total {
            return Err(RullamaError::Gguf(format!(
                "HttpRangeFetcher: range {offset}..={end} extends past file end ({})",
                self.total
            )));
        }

        let req_init = web_sys::RequestInit::new();
        req_init.set_method("GET");
        let headers = web_sys::Headers::new()
            .map_err(|e| RullamaError::Gguf(format!("Headers::new: {e:?}")))?;
        headers
            .set("Range", &format!("bytes={offset}-{end}"))
            .map_err(|e| RullamaError::Gguf(format!("set Range: {e:?}")))?;
        req_init.set_headers(&headers);

        let request = web_sys::Request::new_with_str_and_init(&self.url, &req_init)
            .map_err(|e| RullamaError::Gguf(format!("Request::new: {e:?}")))?;

        let resp_value = JsFuture::from(global_fetch(&request)?)
            .await
            .map_err(|e| RullamaError::Gguf(format!("fetch failed: {e:?}")))?;
        let resp: web_sys::Response = resp_value
            .dyn_into()
            .map_err(|e| RullamaError::Gguf(format!("response cast: {e:?}")))?;
        if !resp.ok() && resp.status() != 206 {
            return Err(RullamaError::Gguf(format!(
                "HTTP {} fetching range {offset}-{end}",
                resp.status()
            )));
        }

        let buf_promise = resp
            .array_buffer()
            .map_err(|e| RullamaError::Gguf(format!("array_buffer: {e:?}")))?;
        let array_buffer = JsFuture::from(buf_promise)
            .await
            .map_err(|e| RullamaError::Gguf(format!("await array_buffer: {e:?}")))?;
        let bytes = js_sys::Uint8Array::new(&array_buffer).to_vec();
        if bytes.len() as u64 != len {
            return Err(RullamaError::Gguf(format!(
                "HttpRangeFetcher: server returned {} bytes, expected {len}",
                bytes.len()
            )));
        }
        Ok(bytes)
    }
}

// ---------- OpfsFetcher (wasm32-only) ----------

/// Browser-side fetcher backed by a JS callback that resolves ranges from an
/// **OPFS** (Origin Private File System) file.
///
/// Why this exists: iOS Safari silently caps a combined Blob at ~5.6 GiB and
/// kills the WebContent process around ~2 GiB of *live* JS memory — both apply
/// during the IndexedDB-Blob path used by `HttpRangeFetcher` callers that
/// cache. OPFS sidesteps both: bytes are written through a
/// `FileSystemSyncAccessHandle` in a Worker (no JS-heap residency) and reads
/// go through `file.slice(offset, end).arrayBuffer()` (disk-backed, also no
/// JS-heap residency for the source).
///
/// The wasm side never touches OPFS directly — it calls into the JS
/// `read_fn(offset_f64, len_f64) -> Promise<Uint8Array>` callback. JS owns the
/// `FileSystemFileHandle` lifetime; this struct just holds the callback and
/// the total file size (passed in at construction time so the GGUF parser can
/// bounds-check without a round-trip).
#[cfg(target_arch = "wasm32")]
pub struct OpfsFetcher {
    read_fn: js_sys::Function,
    total: u64,
}

#[cfg(target_arch = "wasm32")]
impl OpfsFetcher {
    pub fn new(read_fn: js_sys::Function, total: u64) -> Self {
        Self { read_fn, total }
    }
}

#[cfg(target_arch = "wasm32")]
#[async_trait(?Send)]
impl TensorFetcher for OpfsFetcher {
    fn total_len(&self) -> u64 {
        self.total
    }

    async fn fetch(&self, offset: u64, len: u64) -> Result<Vec<u8>> {
        use wasm_bindgen::{JsCast, JsValue};
        use wasm_bindgen_futures::JsFuture;

        if len == 0 {
            return Ok(Vec::new());
        }
        let end = offset.checked_add(len).ok_or_else(|| {
            RullamaError::Gguf(format!("OpfsFetcher: range overflow {offset}+{len}"))
        })?;
        if end > self.total {
            return Err(RullamaError::Gguf(format!(
                "OpfsFetcher: range {offset}..{end} extends past file end ({})",
                self.total
            )));
        }

        let result = self
            .read_fn
            .call2(
                &JsValue::NULL,
                &JsValue::from_f64(offset as f64),
                &JsValue::from_f64(len as f64),
            )
            .map_err(|e| RullamaError::Gguf(format!("OPFS read_fn call failed: {e:?}")))?;

        // The JS side may return a Uint8Array directly (sync) or a Promise.
        // Probe for thenable and await it if present.
        let value = if let Ok(promise) = result.clone().dyn_into::<js_sys::Promise>() {
            JsFuture::from(promise)
                .await
                .map_err(|e| RullamaError::Gguf(format!("OPFS read_fn promise rejected: {e:?}")))?
        } else {
            result
        };

        let array = js_sys::Uint8Array::new(&value);
        let bytes = array.to_vec();
        if bytes.len() as u64 != len {
            return Err(RullamaError::Gguf(format!(
                "OpfsFetcher: read_fn returned {} bytes, expected {len}",
                bytes.len()
            )));
        }
        Ok(bytes)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn block<F: core::future::Future>(f: F) -> F::Output {
        // Native test path; cfg-guarded out of wasm32 by the surrounding `#[cfg(test)]`
        // attribute on the parent module, which `cargo test` only runs on the host.
        pollster::block_on(f)
    }

    #[test]
    fn in_memory_fetcher_returns_correct_slice() {
        let bytes: Vec<u8> = (0..=255u8).collect();
        let f = InMemoryFetcher::new(bytes);
        assert_eq!(f.total_len(), 256);
        let chunk = block(f.fetch(10, 8)).unwrap();
        assert_eq!(chunk, vec![10, 11, 12, 13, 14, 15, 16, 17]);
    }

    #[test]
    fn in_memory_fetcher_rejects_out_of_range() {
        let f = InMemoryFetcher::new(vec![0u8; 16]);
        assert!(block(f.fetch(0, 17)).is_err());
        assert!(block(f.fetch(20, 1)).is_err());
    }

    #[test]
    fn in_memory_fetcher_zero_length() {
        let f = InMemoryFetcher::new(vec![1, 2, 3, 4]);
        let chunk = block(f.fetch(2, 0)).unwrap();
        assert_eq!(chunk, Vec::<u8>::new());
    }
}