oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
infer.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 *
6 * This file is part of oveRTOS.
7 */
8
15#pragma once
16
17#include <ove/infer.h>
18#include <ove/storage.h>
19#include <ove/error.hpp>
20
21#ifdef CONFIG_OVE_INFER
22
23namespace ove
24{
25
37template <size_t ArenaSize = 0> class Model
38{
39 public:
46 explicit Model(const struct ove_model_config &cfg)
47 {
48#ifdef CONFIG_OVE_ZERO_HEAP
49 int err = ove_model_init(&handle_, &storage_, arena_, &cfg);
50#else
51 int err = ove_model_create(&handle_, &cfg);
52#endif
53 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
54 }
55
56 ~Model() noexcept
57 {
58 if (!handle_)
59 return;
60#ifdef CONFIG_OVE_ZERO_HEAP
61 ove_model_deinit(handle_);
62#else
63 ove_model_destroy(handle_);
64#endif
65 }
66
67 Model(const Model &) = delete;
68 Model &operator=(const Model &) = delete;
69
70#ifdef CONFIG_OVE_ZERO_HEAP
71 Model(Model &&) = delete;
72 Model &operator=(Model &&) = delete;
73#else
75 Model(Model &&other) noexcept : handle_(other.handle_)
76 {
77 other.handle_ = nullptr;
78 }
80 Model &operator=(Model &&other) noexcept
81 {
82 if (this != &other) {
83 if (handle_)
84 ove_model_destroy(handle_);
85 handle_ = other.handle_;
86 other.handle_ = nullptr;
87 }
88 return *this;
89 }
90#endif
91
96 [[nodiscard]] Result<void> invoke() noexcept
97 {
98 return from_rc(ove_model_invoke(handle_));
99 }
100
102 template <typename T> T *input_data(unsigned int index = 0)
103 {
104 struct ove_tensor_info info;
105 if (ove_model_input(handle_, index, &info) != OVE_OK)
106 return nullptr;
107 return static_cast<T *>(info.data);
108 }
109
111 template <typename T> const T *output_data(unsigned int index = 0) const
112 {
113 struct ove_tensor_info info;
114 if (ove_model_output(handle_, index, &info) != OVE_OK)
115 return nullptr;
116 return static_cast<const T *>(info.data);
117 }
118
123 [[nodiscard]] Result<struct ove_tensor_info> input(unsigned int index) const noexcept
124 {
125 struct ove_tensor_info info {
126 };
127 const int rc = ove_model_input(handle_, index, &info);
128 return from_rc(rc, info);
129 }
130
132 [[nodiscard]] Result<struct ove_tensor_info> output(unsigned int index) const noexcept
133 {
134 struct ove_tensor_info info {
135 };
136 const int rc = ove_model_output(handle_, index, &info);
137 return from_rc(rc, info);
138 }
139
141 uint64_t last_inference_us() const
142 {
143 return ove_model_last_inference_us(handle_);
144 }
145
147 ove_model_t handle() const
148 {
149 return handle_;
150 }
151
152 private:
153 ove_model_t handle_ = nullptr;
154#ifdef CONFIG_OVE_ZERO_HEAP
155 ove_model_storage_t storage_ = {};
156 alignas(16) uint8_t arena_[ArenaSize] = {};
157#endif
158};
159
160} // namespace ove
161
162#endif /* CONFIG_OVE_INFER */
RAII wrapper for an ML inference model session.
Definition infer.hpp:38
T * input_data(unsigned int index=0)
Get a typed pointer to input tensor data; nullptr on failure.
Definition infer.hpp:102
Result< struct ove_tensor_info > output(unsigned int index) const noexcept
Get full tensor descriptor for an output.
Definition infer.hpp:132
Result< struct ove_tensor_info > input(unsigned int index) const noexcept
Get full tensor descriptor for an input.
Definition infer.hpp:123
Model(const struct ove_model_config &cfg)
Construct a model from the given configuration.
Definition infer.hpp:46
Result< void > invoke() noexcept
Run the model forward pass.
Definition infer.hpp:96
uint64_t last_inference_us() const
Return last inference duration in microseconds.
Definition infer.hpp:141
const T * output_data(unsigned int index=0) const
Get a typed pointer to output tensor data (const); nullptr on failure.
Definition infer.hpp:111
Model & operator=(Model &&other) noexcept
Move-assignment — destroys current model, then takes other's handle.
Definition infer.hpp:80
ove_model_t handle() const
Access the underlying C handle.
Definition infer.hpp:147
Model(Model &&other) noexcept
Move constructor — transfers handle ownership; source becomes empty.
Definition infer.hpp:75
Strong ove::Error type, Result<T> alias, and std::error_code interop for the oveRTOS C++ binding.
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:20
Result< void > from_rc(int rc) noexcept
Lifts a substrate rc-code into a Result<void>.
Definition error.hpp:254
std::expected< T, Error > Result
std::expected-based result alias.
Definition error.hpp:139