Skip to content

Latest commit

 

History

History
91 lines (75 loc) · 3.79 KB

README.md

File metadata and controls

91 lines (75 loc) · 3.79 KB

Gemma 4 E2B runtime

A bundled fix for WebLLM 0.2.86, for gemma-4-E2B-it-q4f16_1-MLC only. It fixes cache corruption and projection rounding while keeping the original model weights. Included in strands-web-llm, langchain-web-llm, and pi-ai-web-llm; no local compiler is needed. Use it only with its matching WebLLM model runtime, v0_2_86/base.

Use

For Vite, import the asset URL and its pinned integrity hash. For LangChain or Pi, use your adapter's package name in both asset imports:

import { CreateMLCEngine, modelVersion, prebuiltAppConfig } from '@mlc-ai/web-llm'
import runtimeUrl from 'strands-web-llm/runtime/gemma-4-e2b.wasm?url'
import runtime from 'strands-web-llm/runtime/manifest.json'

const runtimeVersion = `v${runtime.webllm.replaceAll('.', '_')}/base`
const appConfig =
  modelVersion === runtimeVersion
    ? {
        ...prebuiltAppConfig,
        model_list: prebuiltAppConfig.model_list.map((model) =>
          model.model_id === runtime.model_id
            ? {
                ...model,
                model_lib: new URL(runtimeUrl, window.location.href).href,
                integrity: { ...model.integrity, model_lib: runtime.integrity },
              }
            : model,
        ),
      }
    : prebuiltAppConfig
const engine = await CreateMLCEngine(
  runtime.model_id,
  { appConfig },
  {
    context_window_size: 16384,
  },
)

Pass this engine to your adapter as usual. Workers accept the same appConfig. The repository's examples select the asset only when its runtime version matches. The provider itself leaves engine setup to the caller.

Without an asset bundler, copy the exported Wasm file to your site's public directory and use its absolute URL as model_lib. Serve it as application/wasm; cross-origin hosting also needs CORS. Keep the supplied integrity value.

Protocol limits

Gemma tools and thinking use its native prompt template through raw completions. That mode accepts text and tool results, but cannot combine media, constrained output, or native generation recovery. WebLLM accepts ignore_eos without tool parsing; generation may run to max_tokens and return length. Gemma tool calls need the normal end token. WebLLM re-prefills raw prompts on each turn. Ordinary chat uses WebLLM's chat API and KV cache. Past thoughts stay in agent history. Set request.extra_body.preserve_thinking: true to replay earlier tool-call reasoning.

Rebuild and implementation details (maintainers)

Sliding-attention and full-attention metadata use separate buffers. Projections accumulate in float32 before returning to float16 activations. Local M4 Pro checks measured about 0.8% more input processing time and unchanged generation speed; results on other devices may differ.

Rebuilding requires a repository checkout. Use Python 3.12, LLVM 22.1.8 (with llvm-config on PATH), Emscripten 6.0.12 (emsdk_env.sh sourced), and a C++ toolchain. From the repository root:

python3.12 -m venv .cache/runtime-venv
.cache/runtime-venv/bin/python -m pip install -r shared/runtime/requirements.txt
.cache/runtime-venv/bin/python shared/runtime/build.py

The script verifies pinned source archives and build inputs, applies cache.patch and projection.patch, checks that all four cache metadata arrays stay separate at both depths, and builds under .cache/runtime-build. It downloads compiler sources, not model weights. The output has a .build.json record; compiler output can make rebuilt bytes differ. The shipped file's hash is in manifest.json.

Before replacing a runtime, verify its cache behavior and run the real Harness tests. Ordinary CI checks hashes and package contents without using a GPU.