# Runtime scope and positioning lmLite is a lightweight orchestration layer for browser-local language-model inference from Python. JupyterLite, with Xeus-Python or Pyodide, is its primary integration. The package provides async generation, embeddings, classification, and in-memory cosine similarity search, Transformers.js task pipelines (text, vision, speech and image-output tasks), bare ONNX models through `OnnxModel`, and a microphone widget, together with browser model caching, hosted assets, and portable model ZIP import/export. ## What lmLite adds Transformers.js and ONNX Runtime load and execute the models. lmLite bridges those capabilities into browser Python and handles notebook-oriented loading progress, model-source discovery, and independent embedding device/dtype configuration. Models and browser/runtime capabilities determine inference quality, compatibility, and resource requirements. This makes lmLite useful for teaching, interactive experimentation, and small browser-local prototypes without a remote inference service. Model and runtime downloads may still require network access; local inference does not mean a fresh installation works offline. lmLite is not a model-training toolkit, a production inference service, a vector database, or a complete RAG framework. Its similarity helper embeds documents and ranks them by cosine similarity in memory. Applications remain responsible for chunking, model-specific input preparation, persistent indexes, retrieval policy, prompt construction, and evaluation. ## Inference outside JupyterLite ### Standalone Pyodide browser applications JupyterLite's notebook interface is not inherently required for browser inference. A standalone Pyodide application is a potential integration target, but there is no turnkey standalone integration documented or guaranteed here. The host would need to provide the Python-to-JavaScript bridge, load the package and Transformers.js dependencies, and integrate asynchronous initialization and progress display. Notebook widget progress is tied to notebook frontend support. A custom host would need to accommodate that behavior or provide suitable progress rendering. It must also account for browser memory, model/backend/dtype compatibility, and HTTP(S) access and CORS for model files. Automatic hosted-model discovery recognizes JupyterLite URLs. A host without those URLs should provide `hosted_models_url` pointing to an HTTP(S) model directory with `manifest.json` and the expected namespace/model layout. Model preparation is described in [the model guide](MODELS.md). ### JavaScript browser applications **JavaScript runtime:** `lmlite.js` exposes `BrowserRuntime` and helper functions under a single `globalThis.lmLite` namespace for integration with the Python API. These interfaces are internal implementation details, not a separately documented or versioned JavaScript SDK. Direct JavaScript usage may be possible, but its initialization, integration, and compatibility are not guaranteed independently of the Python API. For an application already written in JavaScript, direct Transformers.js is usually the simpler choice unless lmLite's model-loading and cache workflows provide a specific benefit. ## Native Python and Node.js The Python API calls a browser `js` bridge. A conventional CPython process or server-backed Jupyter kernel does not supply that bridge. There, `pip install 'lmlite[native]'` adds a headless onnxruntime backend that runs the same ONNX files for `Embedder`, `Classifier`, greedy `LLM.generate()`, `OnnxModel` and `tokenizer()` (see [the native guide](guides/native-backend.md)); the other calls, such as `pipeline()`, raise an error that says they need a browser kernel. The native backend is for tuning models before shipping them, not an inference service. Node.js is also not supported for inference as-is: the runtime uses browser-oriented dependency loading and APIs such as the browser Cache API. Using Node.js for development tests is separate from supporting it as an application inference runtime. ## Desktop and CI model preparation Model preparation does work outside the browser. The `lmlite-models` CLI and `host_models` / `pack_models` helpers in `lmlite.model_assets` prepare downloaded assets for static hosting or ZIP portability without performing inference. They can be used from desktop Python or CI independently of JupyterLite. ## Choosing a kernel: xeus-python or Pyodide lmLite runs the same Python API on both, and both are covered by the browser tests (see [DEVELOPMENT.md](DEVELOPMENT.md)). The model code is the same JavaScript (`lmlite.js`, Transformers.js, ONNX Runtime Web) in both, so inference speed does not depend on the kernel. | | xeus-python | Pyodide | |:--|:--|:--| | What it is | CPython built with emscripten, packages from the emscripten-forge conda channel, bridged with `pyjs` | CPython built with emscripten, wheels from the Pyodide distribution and PyPI, bridged with `pyodide.ffi` | | Cross-origin isolation (multi-threaded WASM) | works with pyjs 4.1.0 or later (jl-forge 0.6.0 and later); 137 ms to 50 ms on a long input | works; the same 2.5 to 3.5 times speedup (see [NATIVE.md](NATIVE.md)) | | Python call overhead | about 0.05 ms per `encode()` | about 0.02 ms per `encode()` | | Differences lmLite works around | a function passed to JavaScript is an object, not a callable; binary widget traits set by the browser never reach the kernel (the microphone recording travels as base64); loading a FreeType font hangs (use `ImageFont.load_default_imagefont()`) | none found so far | | Site build | `jl-forge` solves a conda environment on the first build | `jl-forge` with `uv` and the wheels in `pyproject.toml`; the site fetches nothing from PyPI | Both kernels need the same things from the host: the page's service worker proxies cross-origin requests, so headers for isolation have to come from the server (GitHub Pages cannot send them). **Recommendation.** Use xeus-python by default: it is the kernel of the hosted demos and of most tests (the real JupyterLab UI test, `lab-smoke`, runs on it). Use Pyodide when you want the smallest differences from desktop Python: an install of any pure-Python wheel from PyPI at run time, no workarounds in widget or font code, and a site build without a conda solve. The workarounds above are in lmLite and the notebooks, so a notebook of this project runs on either. Not measured: kernel start-up time, the size of each site, and memory use of the kernel itself. Measure those with your own notebooks before choosing on size or start-up. ## Browsers tested - **Chromium and Firefox:** the notebooks for images, speech and the microphone run in the real JupyterLab UI with xeus-python (`lab-smoke`, with Playwright's browsers; see [DEVELOPMENT.md](DEVELOPMENT.md)). A user report adds stock Firefox 159 on an Apple-silicon Mac: model downloads, text to speech and the text-to-image recipe on WebGPU worked. - **Safari:** Playwright's WebKit is tested for cross-origin isolation (see [NATIVE.md](NATIVE.md)); the notebooks have not been run in Safari on a Mac. On an iPad it was run by hand, see the next section. - **WebGPU:** needs a hardware adapter. Headless Chromium uses a software adapter without float16 shaders, which is enough for small models but not for the text-to-image recipe. ### Observed on real devices These are one owner's hand runs of the demos and the two apps (`camera-objects`, `speak-french`), mostly single runs, not a test matrix. The numbers are times for one photo or one generation. The apps' Debug box prints the browser, the WebGPU adapter and the backend that ran. The issues have the details. | Device and browser | What was seen | |---|---| | **iPad (A16), Safari** | WebGPU adapter: Apple, `shader-f16`, 44 storage buffers. A shared WebAssembly memory of 1 GB was accepted and 2 GB refused, so onnxruntime-web's 4 GB request failed with `RangeError: Out of memory` for any model until #127 and #128: with them, the camera app runs on WebGPU (DETR, one 640 x 480 photo in 10.5 s) and DistilGPT-2 loads. Model files of about 300 MB fail with `std::bad_alloc`; what fits is in [MODELS.md](MODELS.md#limited-memory-an-ipad-running-safari) (#45). A browser that kept the old runtime files kept failing until its site data was cleared; the files now carry a version in their URL (#130). | | **Galaxy S20 FE (Android 13, Adreno 6xx), Chrome 158** | WebGPU adapter: Qualcomm, `shader-f16`, 16 storage buffers. WebGPU ran **without an error and returned nothing** (DETR: no candidate above 0.05, after 28.6 s). The apps now treat that as a failure and retry on WASM (#124): DETR then takes about 25.7 s per photo at the default size and 8.9 s with `image_size=480` (#133; different scenes, one photo each, so the ratio is approximate). The first photo of a page load also pays the failed WebGPU attempt, which is not shortened. Chrome's user agent says Android 10 (it freezes the version); Firefox's says 13. Chrome lists four cameras (front and rear, two each), so `CameraCapture.switch()` takes four taps to come back to the first (#117, #126). | | **Galaxy S20 FE, Firefox 160 for Android** | No WebGPU: `navigator.gpu` is absent (`webgpu: False`, no adapter), so the camera app runs on WASM from the start, through the ordinary load-time fallback, with no failed attempt. DETR with `image_size=480`: 9.4 s for a 480 x 640 photo (laptop 1.00, keyboard 0.92, person 0.73), about the same as Chrome on WASM. | | **Galaxy Tab S10 Lite, Chrome and Firefox** | The camera app and the translator worked in both. The backend was not shown when they were run, so it is not known whether either used WebGPU. | | **Mac (Apple silicon), Firefox 161** | WebGPU adapter present (`shader-f16`, **9** storage buffers). DETR failed at its first run with `Failed to create a WebGPU compute pipeline ... Metal: no matching function for call to 'set_output_by_indices'`, and a retry on WASM then failed with the same error until onnxruntime-web was started afresh (#122). With that fix the camera app swaps to WASM and detects (`person 0.97`, `person 0.69`, 5.7 s for a 640 x 480 photo, `wasm after webgpu failed`): the fix was first tested with a simulated failure, then confirmed on this Firefox. Nine storage buffers is also why 4-bit models fail on Firefox's WebGPU (see [MODELS.md](MODELS.md#generation-dtype-selection)). | | **Mac, Chrome and Safari** | The camera app ran against the deployed site. Chromium (Playwright, Metal) runs both backends; DETR on WASM, single-threaded, took 5.0 to 5.6 s per 640 x 480 photo. | What this means in practice: - **Do not assume a WebGPU adapter works.** Two devices with an adapter (the S20 FE in Chrome and Firefox on the Mac) failed in different ways: one returned nothing, the other failed at the first inference, after a successful load. `backend_fallback` covers only a load that fails, so the apps check the result themselves and retry on WASM. A library-level run-time fallback is in #114. - **Check the backend that ran,** with `llm.backend` or the apps' Debug box, and keep `backend_fallback=False` for any proof that inference ran on the GPU. - **A phone on WASM is slow for large inputs.** One DETR photo took about 25 s on the S20 FE at the default size and about 9 s with `image_size=480` (#126, #133); WASM is about 9 s in Chrome and in Firefox there. In Chrome the first photo of a page load also pays the failed WebGPU attempt. - **The numbers are single runs on one device each.** A different device, Safari version or reload can differ, and the iPad's memory limit in particular is reported to vary.