Continuum C++ API
Unified runtime for token + tensor execution
Loading...
Searching...
No Matches
backend.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <cstdint>
8#include <memory>
9#include <optional>
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14namespace continuum::backend {
15
17 void* handle = nullptr; // Opaque backend-owned state (e.g. KV cache handle).
18};
19
23 std::int32_t reused_prefix_len = 0;
24 std::int32_t compute_steps = 0;
25 std::int32_t tokens_sent = 0;
26 std::int32_t tokens_saved = 0;
27 bool used_cached_state = false;
28};
29
31 bool supports_tensor = false;
32 bool supports_token = false;
33 bool supports_cache = false;
34};
35
36class Backend {
37 public:
38 virtual ~Backend() = default;
39 virtual BackendCapabilities capabilities() const = 0;
40 virtual std::string tensor_backend_type() const { return ""; }
41 // Optional cross-process state portability. Backends that can externalize a
42 // state handle return its bytes from export_state and rebuild a live handle
43 // in import_state. Default: not portable (empty / nullopt); checkpoints then
44 // drop the state and resume falls back to a cold cache.
45 virtual std::vector<std::uint8_t> export_state(const BackendState& /*state*/) const { return {}; }
46 virtual std::optional<BackendState> import_state(const std::vector<std::uint8_t>& /*bytes*/) { return std::nullopt; }
48 const ir::Node& node,
49 const std::vector<continuum::Value>& inputs,
50 const std::optional<BackendState>& prefix_state,
51 std::int32_t remaining_tokens) = 0;
52};
53
55 public:
57 std::string name;
58 std::shared_ptr<Backend> backend;
60 int priority = 0;
61 };
62
63 void register_backend(const std::string& name, std::shared_ptr<Backend> backend, int priority = 0);
65 void load_plugin(const std::string& name, const std::string& path, int priority = 0);
69 std::size_t load_plugins_from_env(const char* env_var = "CONTINUUM_BACKEND_PLUGINS");
71 std::vector<std::string> names() const;
72 std::shared_ptr<Backend> get(const std::string& name) const;
73 bool has(const std::string& name) const;
75 std::shared_ptr<Backend> get_backend_for(ir::NodeKind kind) const;
77
78 private:
79 std::unordered_map<std::string, BackendSelection> backends_;
80};
81
84std::shared_ptr<Backend> MakeBackendFromAbi(continuum_backend_vtable_t vtable);
85
90std::shared_ptr<Backend> LoadBackendPlugin(const std::string& path);
91
93struct PluginSpec {
94 std::string name;
95 std::string path;
96 int priority = 0;
97};
98
102std::vector<PluginSpec> ParsePluginSpecs(const std::string& spec);
103
104} // namespace continuum::backend
Definition backend.hpp:54
std::size_t load_plugins_from_env(const char *env_var="CONTINUUM_BACKEND_PLUGINS")
std::vector< std::string > names() const
Registered backend names, in no particular order.
bool has(const std::string &name) const
void load_plugin(const std::string &name, const std::string &path, int priority=0)
Load a shared-library backend (see LoadBackendPlugin) and register it.
BackendSelection select_backend(const ir::Node &node) const
std::shared_ptr< Backend > get(const std::string &name) const
void register_backend(const std::string &name, std::shared_ptr< Backend > backend, int priority=0)
std::shared_ptr< Backend > get_backend_for(ir::NodeKind kind) const
Returns a backend that supports kind, or nullptr if none is registered.
Definition backend.hpp:36
virtual std::string tensor_backend_type() const
Definition backend.hpp:40
virtual std::optional< BackendState > import_state(const std::vector< std::uint8_t > &)
Definition backend.hpp:46
virtual ~Backend()=default
virtual BackendCapabilities capabilities() const =0
virtual std::vector< std::uint8_t > export_state(const BackendState &) const
Definition backend.hpp:45
virtual BackendRunResult run_with_cache(const ir::Node &node, const std::vector< continuum::Value > &inputs, const std::optional< BackendState > &prefix_state, std::int32_t remaining_tokens)=0
Definition anthropic.hpp:4
std::vector< PluginSpec > ParsePluginSpecs(const std::string &spec)
std::shared_ptr< Backend > MakeBackendFromAbi(continuum_backend_vtable_t vtable)
std::shared_ptr< Backend > LoadBackendPlugin(const std::string &path)
NodeKind
Definition node.hpp:15
std::variant< TensorValue, MlxTensorValue, TokensValue, SchemaValue, std::string, double, int64_t > Value
Definition value.hpp:30
bool supports_cache
Definition backend.hpp:33
bool supports_tensor
Definition backend.hpp:31
bool supports_token
Definition backend.hpp:32
BackendCapabilities capabilities
Definition backend.hpp:59
std::string name
Definition backend.hpp:57
std::shared_ptr< Backend > backend
Definition backend.hpp:58
Definition backend.hpp:20
std::int32_t tokens_sent
Definition backend.hpp:25
std::int32_t tokens_saved
Definition backend.hpp:26
bool used_cached_state
Definition backend.hpp:27
std::int32_t reused_prefix_len
Definition backend.hpp:23
BackendState resulting_state
Definition backend.hpp:22
std::int32_t compute_steps
Definition backend.hpp:24
continuum::Value output
Definition backend.hpp:21
Definition backend.hpp:16
void * handle
Definition backend.hpp:17
One entry of a plugin list.
Definition backend.hpp:93
std::string name
Definition backend.hpp:94
std::string path
Definition backend.hpp:95
int priority
Definition backend.hpp:96
Definition node.hpp:51
Definition backend_abi.h:74