---
title: "Backends and webR"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Backends and webR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

<!--
  =====================================================================
  WARNING: THIS FILE IS GENERATED by rawvignette. Do not edit by hand.
  Source: vignettes-raw/backend-and-webr.Rmd
  Regenerate with: rawvignette::precompile_raw_vignettes()
  =====================================================================
-->



`Rbebelm` uses runtime backend dispatch so a portable R package can load a backend
that matches the current platform. The R shared library owns registration and
dispatch; model code lives in Rust backend libraries.


``` r
library(Rbebelm)
rbebelm_cpuid_info()
#> <Rbebelm CPU features>
#>   x86_64-v3: yes 
#>   x86_64-v4: no 
#>   NEON: no 
#>   ARM dotprod: no 
#>   wasm simd128: no
rbebelm_backend_info()
#> <Rbebelm backend dispatch>
#>   mode: dynamic 
#>   requested: auto 
#>   selected: avx2 
#>   loaded: yes 
#>   installed: scalar,avx2,avx512 
#>   supported: scalar,avx2
```

## Backend selection

Backend loading happens once per R process. If you want to request a backend,
call `rbebelm_set_backend()` before the first call that loads backend symbols.


``` r
rbebelm_set_backend("auto")
#> Error in `rbebelm_set_backend()`:
#> ! Rbebelm backend is already initialized; call rbebelm_set_backend() before loading a model or querying backend features
rbebelm_backend_features()
#> <Rbebelm backend features>
#>   backend: avx2 
#>   target: x86_64-linux 
#>   Rust crate: rbebelm_backend 0.1.0 
#>   native SIMD feature: yes 
#>   compiled features:
#>     AVX2: yes 
#>     AVX-512F: no 
#>     NEON: no 
#>     ARM dotprod: no 
#>     wasm simd128: no
rbebelm_backend_info()
#> <Rbebelm backend dispatch>
#>   mode: dynamic 
#>   requested: auto 
#>   selected: avx2 
#>   loaded: yes 
#>   installed: scalar,avx2,avx512 
#>   supported: scalar,avx2
```

Supported backend names depend on the platform. Typical native builds include:

- `scalar`
- `avx2` and `avx512` on x86_64 when built
- `neon` on arm64 when built

If the requested backend is not installed or not supported by the current CPU,
the dispatcher reports an error before model code is loaded.

## webR

The webR build links a static `wasm_simd128` Rust backend. It uses a patched local
copy of upstream BebeLM for Emscripten:

- GGUF files are read from the webR filesystem into memory instead of using
  native `mmap`.
- Matmul runs serially because native Rayon threading is not used in webR.
- `bebel_model_load()` attempts to load a GGUF path from the webR virtual
  filesystem.

Very large GGUF files can exhaust browser or webR memory. Use smaller models or
browser/runtime settings appropriate for the target deployment.

The webR diagnostics report the static backend:


``` r
rbebelm_backend_info()
rbebelm_backend_features()
```
