C+ setup
Native API for projects using the C+ toolchain
Slab's native API for C+ apps. It is the same runtime that the C interface
exposes, with C+ types: objects free themselves when they go out of scope,
strings are str, and options are booleans.
- API reference
- Examples: classify, faces, tensors, chat
Setup
Add Slab's packages to your Cplus.toml:
[dependencies]
stdlib = "*"
objc = "*"
slab_format = "*"
slab_runtime = "*"
slab_core = "*"
slab_cpu = "*"
slab_mpsgraph = "*"
slab_license = "*"
slab_image = "*"
slab_vlm = "*"
ffi = "*"
Then import the API:
import "ffi/api" as slab;
The five objects
| Object | Role |
|---|---|
slab::Config |
App-wide settings and the runtime license. Create once. |
slab::Session |
One model. Open once, run many times. |
slab::Frame |
A decoded image, shared by any number of sessions. |
slab::Input |
What goes into a run: an image, tensors, or a prompt. |
slab::Output |
What comes out: tensors, classes, boxes, text, timing. |
A first run
var cfg = slab::Config::new();
cfg.set_models_dir("models");
if !cfg.set_runtime_license(token) {
io::eprintln(cfg.runtime_license_error_message());
return 1;
}
var session = slab::Session::new(cfg);
if !session.open("classifier.slab") {
io::eprintln(session.error_message());
return 1;
}
var frame = slab::Frame::new();
let _ = frame.load_file("photo.jpg");
var inp = slab::Input::new();
var out = slab::Output::new();
inp.set_frame(frame);
if session.run(inp, out) {
let n = out.compute_top_k(0, 5, true);
for r in 0..n {
io::println("${out.top_class_id(r)}: ${out.top_score(r)}");
}
}
The model file supplies the input size, color order, normalization and labels, so the app passes only the image.
Things to know
License first. Every model needs a runtime license on the Config
before sessions open. On Android, first bind Slab to the app once, with the
JNI environment and the application context: bind(env, context) from
import "slab_license/bootstrap", which returns 0 on success.
Errors. Calls return bool (or a count). When one fails, ask the
object that failed: session.error_message(), frame.error_message(),
or cfg.runtime_license_error_message() for license problems.
Backends. By default Slab uses the model's preferred backend, or
the first of NPU → GPU → CPU that runs the whole model. Request one with
session.set_backend(...) before open. out.backend_used() tells you
where a run went.
Reuse. Keep sessions open and reuse Inputs and Outputs from run to
run. Call inp.clear() before giving an Input different data.
Borrowed strings. Strings returned by a Session or Output (labels,
names, text, error messages) point into that object. Read any other
values you need from the same object before taking such a string, and copy
the string (.to_text()) if you keep it past the next call.
Threads. One operation at a time per session; separate sessions run in
parallel. A loaded Frame can be shared across threads. Give each thread
its own Input and Output. session.cancel() works from any thread.
Text generation. start(inp, out) reads the prompt, then call
next_token(out) in a loop until it returns 0. Each call puts the new text
in out.text(). The session keeps the conversation for the next turn;
reset() clears it.