Rust API reference · Kjarni 0.2.8

kjarni::common

Common types and utilities shared across kjarni modules.

LoadConfig

pub struct LoadConfig

Wrapper around ModelLoadConfig for the high-level API.

new

pub fn new() -> Self

Create a new default LoadConfig.

into_inner

pub fn into_inner(self) -> ModelLoadConfig

Get the inner ModelLoadConfig.

as_inner

pub fn as_inner(&self) -> &ModelLoadConfig

Get a reference to the inner config.

LoadConfigBuilder

pub struct LoadConfigBuilder

Builder for LoadConfig.

new

pub fn new() -> Self

Create a new builder with defaults.

from_config

pub fn from_config(config: LoadConfig) -> Self

Create a builder from an existing LoadConfig.

from_inner

pub fn from_inner(inner: ModelLoadConfig) -> Self

Create a builder from an existing ModelLoadConfig.

offload_embeddings

pub fn offload_embeddings(mut self, offload: bool) -> Self

Keep embedding layer on CPU to save VRAM.

offload_lm_head

pub fn offload_lm_head(mut self, offload: bool) -> Self

Keep language model head on CPU to save VRAM.

dtype

pub fn dtype(mut self, dtype: DType) -> Self

Set target dtype for weights.

f16

pub fn f16(self) -> Self

Use F16 precision.

bf16

pub fn bf16(self) -> Self

Use BF16 precision.

f32

pub fn f32(self) -> Self

Use F32 precision (default).

quantize_lm_head

pub fn quantize_lm_head(mut self, dtype: DType) -> Self

Quantize the LM head to specified dtype.

quantize_lm_head_q8

pub fn quantize_lm_head_q8(self) -> Self

Quantize LM head to Q8_0.

max_batch_size

pub fn max_batch_size(mut self, size: usize) -> Self

Set maximum batch size for KV cache pre-allocation.

max_sequence_length

pub fn max_sequence_length(mut self, length: usize) -> Self

Set maximum sequence length for KV cache pre-allocation.

prefer_gguf

pub fn prefer_gguf(mut self, prefer: bool) -> Self

Prefer GGUF format if available.

build

pub fn build(self) -> LoadConfig

Build the LoadConfig.

KjarniDevice

pub enum KjarniDevice

Execution device for models.

Variants

  • Cpu,: Run on CPU (default, always available).
  • Gpu,: Run on GPU via WebGPU.
  • Auto,: Automatically select best available device.

resolve

pub fn resolve(self) -> Self

Resolve Auto to a concrete device.

to_device

pub fn to_device(self) -> Device

Convert to the low-level Device type.

is_cpu

pub fn is_cpu(&self) -> bool

is_gpu

pub fn is_gpu(&self) -> bool

KjarniError

pub enum KjarniError

Errors that can occur when using kjarni.

Variants

  • UnknownModel(String),: Model not found in registry.
  • IncompatibleModel: Model cannot perform the requested task.
  • ModelNotDownloaded(String),: Model not downloaded and download policy is Never.
  • DownloadFailed: Failed to download model.
  • LoadFailed: Failed to load model.
  • InferenceFailed(#[from] anyhow::Error),: Inference failed.
  • GpuUnavailable,: GPU requested but not available.
  • InvalidConfig(String),: Invalid configuration.
  • NoLabels,: No labels available for classification.

KjarniResult

pub type KjarniResult<T> = Result<T, KjarniError>

Result type for kjarni operations.

default_cache_dir

pub fn default_cache_dir() -> PathBuf

Get the default cache directory.

Delegates rather than re-deriving the path, so KJARNI_CACHE_DIR is honoured here too, and kjarni model download and inference agree on one directory.

ensure_model_downloaded

pub async fn ensure_model_downloaded(model_type: ModelType, cache_dir: Option<&Path>, policy: DownloadPolicy, quiet: bool) -> KjarniResult<PathBuf>

Ensure a model is downloaded according to the policy.

Returns the model directory path.

Kjarni runs the same engine on every platform: