Menu ▾ ▴

#15 Add `candle` provider (in-process local inference) to `orko-providers` crate

open
nobody
2026-08-18
2026-08-18
Anonymous
No

Originally created by: SAMAD101

Summary

Add a candle module to orko-providers: an in-process Provider that runs open-weight
models locally through Candle (candle-core is already
pinned in [workspace.dependencies]). No HTTP, no SSE — this is the "local inference" half of
the crate's remit and the only provider that does not sit on the codec/transport split from [#3].

Why a separate module (not a preset)

Every other row in the providers table is a wire format (#3 OpenAI-compat, [#6] Anthropic) or a
preset over one. Local servers (#12 ollama, [#13] vllm, [#14] llamacpp) are still HTTP presets.
Candle is the case where the model lives in the same process: load weights → tokenize →
autoregressive decode → stream tokens. It implements orko_core::Provider directly, which is
exactly the escape hatch the crate design leaves open ("users can … implement Provider directly").

Scope (v0.1)

  • Feature-gated: candle (off by default). Pulls candle-core, candle-nn,
    candle-transformers, tokenizers, hf-hub only behind the flag. Default build of
    orko-providers must not compile any of it.
  • Optional accel features that just forward: candle-cuda, candle-metal, candle-accelerate.
  • CandleProvider — construction:
  • CandleProvider::from_hub(repo_id) — downloads via hf-hub into the standard HF cache
    (HF_HOME / HF_TOKEN honoured), picks safetensors + tokenizer.json.
  • CandleProvider::from_files(weights, tokenizer, config) — fully offline path.
  • Builder knobs: device (auto → cuda/metal/cpu), dtype, seed, max_new_tokens.
  • Model coverage for v0.1: one decoder family shipped, behind an enum so more can be
    added: start with Llama (covers Llama-3.x, and via candle-transformers the same code
    path serves many Llama-arch finetunes). Qwen2 / Mistral / Phi are follow-ups, one PR each.
  • Prompting: apply the tokenizer's chat template (tokenizer_config.json → chat_template)
    when present; otherwise a simple system:/user:/assistant: fallback. Map Roles from
    orko_core::Message accordingly. Tool calls / structured outputs are out of scope for
    v0.1 — return Error::Unsupported if the request carries tools.
  • Streaming: return a CompletionStream that yields one CompletionChunk per decoded token
    (text delta), then a final chunk with finish_reason (stop on EOS / stop token,
    length on max_new_tokens) and usage (prompt / completion token counts).
  • Sampling: temperature, top-p, top-k, repetition penalty from CompletionOptions, mapped to
    candle_transformers::generation::LogitsProcessor.
  • Runtime rule: no tokio in the public API. Decoding is CPU/GPU-bound and blocking; the
    provider must not block the caller's executor. Run the decode loop on a dedicated
    std::thread and bridge to the async stream with a futures::channel::mpsc (bounded).
    Dropping the stream must stop the thread (check tx.is_closed() per token).
  • Send + Sync: model + tokenizer behind Arc<Mutex<_>> is acceptable for v0.1 — one
    in-flight generation per provider instance. Document the ceiling; batching is a later issue.
  • Errors: map candle / tokenizer / hub errors into orko_core::Error with the underlying
    message; missing files and unsupported architectures must fail at construction, not at
    first complete().

Acceptance

  • cargo build -p orko-providers (no features) does not compile candle.
  • cargo build -p orko-providers --features candle builds on macOS (metal) and Linux (cpu).
  • Unit tests (no network, no weights): chat-template application, CompletionOptions →
    LogitsProcessor mapping, and that a dropped stream halts the generation thread
    (use a fake “model” closure that yields token ids).
  • One #[ignore] integration test that loads a tiny model (e.g. a Llama-arch smoke model
    from the Hub) and streams a completion end-to-end.
  • clippy -D warnings clean with and without the feature.
  • Doc example in the module header showing from_hub + create_agent(provider).

Non-goals (tracked separately if wanted)

  • Multi-request batching / paged KV cache.
  • Quantized GGUF loading (candle supports it; separate issue once safetensors path lands).
  • Embeddings / rerankers.
  • Tool calling via constrained decoding.

Depends on

Nothing in [#3] — this bypasses the HTTP stack. It only depends on the frozen orko-core
Provider / CompletionStream types.

Related

Tickets: #13
Tickets: #14
Tickets: #3
Tickets: #6

Discussion


Log in to post a comment.