oveRTOS C++ API
C++20 RAII wrappers for the oveRTOS C API
Loading...
Searching...
No Matches
infer.hpp
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
9#pragma once
10
11#include <ove/infer.h>
12#include <ove/storage.h>
13
14#ifdef CONFIG_OVE_INFER
15
16namespace ove {
17
29template <size_t ArenaSize = 0>
30class Model {
31public:
38 explicit Model(const struct ove_model_config &cfg) {
39#ifdef CONFIG_OVE_ZERO_HEAP
40 int err = ove_model_init(&handle_, &storage_, arena_, &cfg);
41#else
42 int err = ove_model_create(&handle_, &cfg);
43#endif
44 OVE_STATIC_INIT_ASSERT(err == OVE_OK);
45 }
46
47 ~Model() {
48 if (!handle_)
49 return;
50#ifdef CONFIG_OVE_ZERO_HEAP
51 ove_model_deinit(handle_);
52#else
53 ove_model_destroy(handle_);
54#endif
55 }
56
57 Model(const Model &) = delete;
58 Model &operator=(const Model &) = delete;
59
60#ifdef CONFIG_OVE_ZERO_HEAP
61 Model(Model &&) = delete;
62 Model &operator=(Model &&) = delete;
63#else
64 Model(Model &&other) noexcept : handle_(other.handle_) {
65 other.handle_ = nullptr;
66 }
67 Model &operator=(Model &&other) noexcept {
68 if (this != &other) {
69 if (handle_)
70 ove_model_destroy(handle_);
71 handle_ = other.handle_;
72 other.handle_ = nullptr;
73 }
74 return *this;
75 }
76#endif
77
79 [[nodiscard]] int invoke() { return ove_model_invoke(handle_); }
80
82 template <typename T>
83 T *input_data(unsigned int index = 0) {
84 struct ove_tensor_info info;
85 if (ove_model_input(handle_, index, &info) != OVE_OK)
86 return nullptr;
87 return static_cast<T *>(info.data);
88 }
89
91 template <typename T>
92 const T *output_data(unsigned int index = 0) const {
93 struct ove_tensor_info info;
94 if (ove_model_output(handle_, index, &info) != OVE_OK)
95 return nullptr;
96 return static_cast<const T *>(info.data);
97 }
98
100 int input(unsigned int index, struct ove_tensor_info &info) const {
101 return ove_model_input(handle_, index, &info);
102 }
103
105 int output(unsigned int index, struct ove_tensor_info &info) const {
106 return ove_model_output(handle_, index, &info);
107 }
108
110 uint64_t last_inference_us() const {
111 return ove_model_last_inference_us(handle_);
112 }
113
115 ove_model_t handle() const { return handle_; }
116
117private:
118 ove_model_t handle_ = nullptr;
119#ifdef CONFIG_OVE_ZERO_HEAP
120 ove_model_storage_t storage_ = {};
121 alignas(16) uint8_t arena_[ArenaSize] = {};
122#endif
123};
124
125} // namespace ove
126
127#endif /* CONFIG_OVE_INFER */
Top-level namespace for all oveRTOS C++ abstractions.
Definition app.hpp:19