Menoh
menoh.hpp
Go to the documentation of this file.
1 #ifndef MENOH_HPP
2 #define MENOH_HPP
3 
4 #include <memory>
5 #include <vector>
6 
7 #include <menoh/menoh.h>
8 
9 #ifndef DOXYGEN_SHOULD_SKIP_THIS
10 
11 #define MENOH_CPP_API_ERROR_CHECK(statement) \
12  { \
13  auto ec = statement; \
14  if(ec) { \
15  throw error(ec, menoh_get_last_error_message()); \
16  } \
17  }
18 
19 #endif // DOXYGEN_SHOULD_SKIP_THIS
20 
21 namespace menoh {
27  enum class error_code_t {
51  };
52 
54 
58  class error : public std::runtime_error {
59  public:
60  explicit error(menoh_error_code ec, std::string const& message)
61  : runtime_error(message), ec_(static_cast<error_code_t>(ec)) {}
62 
63  explicit error(error_code_t ec, std::string const& message)
64  : error(static_cast<menoh_error_code>(ec), message) {}
65 
66  error_code_t error_code() const noexcept { return ec_; }
67 
68  private:
69  error_code_t ec_;
70  };
73  enum class dtype_t { float_ = menoh_dtype_float };
74 
75  class variable_profile_table;
76 
79  class model_data {
81  public:
82  model_data() : impl_(nullptr, menoh_delete_model_data) {
85  impl_.reset(h);
86  }
87 
92  : impl_(h, menoh_delete_model_data) {}
93 
98  menoh_model_data_handle get() const noexcept { return impl_.get(); }
99 
101 
109  void reset() noexcept { impl_.reset(); }
110 
112 
114  void optimize(variable_profile_table const& vpt);
115 
116  void add_new_node(std::string const& op_type) {
118  menoh_model_data_add_new_node(impl_.get(), op_type.c_str()));
119  }
120 
121  void add_input_name_to_current_node(std::string const& input_name) {
124  impl_.get(), input_name.c_str()));
125  }
126 
127  void add_output_name_to_current_node(std::string const& output_name) {
130  impl_.get(), output_name.c_str()));
131  }
132 
133  void
134  add_attribute_int_to_current_node(std::string const& attribute_name,
135  int32_t value) {
138  impl_.get(), attribute_name.c_str(), value));
139  }
140 
141  void
142  add_attribute_ints_to_current_node(std::string const& attribute_name,
143  std::vector<int32_t> const& value) {
146  impl_.get(), attribute_name.c_str(), value.size(),
147  value.data()));
148  }
149 
150  void
151  add_attribute_float_to_current_node(std::string const& attribute_name,
152  float value) {
155  impl_.get(), attribute_name.c_str(), value));
156  }
157 
158  void
159  add_attribute_floats_to_current_node(std::string const& attribute_name,
160  std::vector<float> const& value) {
163  impl_.get(), attribute_name.c_str(), value.size(),
164  value.data()));
165  }
166 
167  void add_parameter(std::string const& parameter_name, dtype_t dtype,
168  std::vector<int> const& dims,
169  void* buffer_handle) {
171  impl_.get(), parameter_name.c_str(),
172  static_cast<menoh_dtype>(dtype), dims.size(), dims.data(),
173  buffer_handle));
174  }
175 
176  private:
177  std::unique_ptr<menoh_model_data, decltype(&menoh_delete_model_data)>
178  impl_;
179  };
180 
182  inline model_data
183  make_model_data_from_onnx(std::string const& onnx_filename) {
186  menoh_make_model_data_from_onnx(onnx_filename.c_str(), &h));
187  return model_data(h);
188  }
190  inline model_data
192  uint32_t size) {
196  return model_data(h);
197  }
204  std::vector<int32_t> dims;
205  };
206 
209  public:
216 
218 
221  variable_profile get_variable_profile(std::string const& name) const {
222  menoh_dtype dtype;
224  impl_.get(), name.c_str(), &dtype));
225  int32_t dims_size;
228  impl_.get(), name.c_str(), &dims_size));
229  std::vector<int32_t> dims(dims_size);
230  for(int i = 0; i < dims_size; ++i) {
233  impl_.get(), name.c_str(), i, &dims.at(i)));
234  }
235  return variable_profile{static_cast<dtype_t>(dtype), dims};
236  }
237 
239 
242  menoh_variable_profile_table_handle get() const noexcept {
243  return impl_.get();
244  }
245 
246  private:
247  std::unique_ptr<menoh_variable_profile_table,
249  impl_;
250  };
257  menoh_model_data_optimize(impl_.get(), vpt.get()));
258  }
265  public:
271  impl_.reset(h);
272  }
273 
275  void add_input_profile(std::string const& name, dtype_t dtype,
276  std::vector<int32_t> const& dims) {
279  impl_.get(), name.c_str(), static_cast<menoh_dtype>(dtype),
280  dims.size(), dims.data()));
281  }
282 
284 
286  void add_output_name(std::string const& name) {
289  impl_.get(), name.c_str()));
290  }
291 
293 
297  [[deprecated("Use add_output_name() instead")]]
298  void add_output_profile(std::string const& name, dtype_t) {
299  add_output_name(name);
300  }
301 
307  impl_.get(), model_data.get(), &h));
308  return variable_profile_table(h);
309  }
310 
311  private:
312  std::unique_ptr<menoh_variable_profile_table_builder,
314  impl_;
315  };
320  struct variable {
322  std::vector<int32_t> dims;
324  };
325 
327  class model {
328  public:
332  explicit model(menoh_model_handle h) : impl_(h, menoh_delete_model) {}
333 
335 
339  variable get_variable(std::string const& name) const {
340  void* buff;
342  impl_.get(), name.c_str(), &buff));
343  menoh_dtype dtype;
345  impl_.get(), name.c_str(), &dtype));
346  int32_t dims_size;
348  impl_.get(), name.c_str(), &dims_size));
349  std::vector<int32_t> dims(dims_size);
350  for(int i = 0; i < dims_size; ++i) {
352  impl_.get(), name.c_str(), i, &dims.at(i)));
353  }
354  return variable{static_cast<dtype_t>(dtype), dims, buff};
355  }
356 
358 
362  void run() { menoh_model_run(impl_.get()); }
363 
364  private:
365  std::unique_ptr<menoh_model, decltype(&menoh_delete_model)> impl_;
366  };
367 
372  public:
373  explicit model_builder(
375  : impl_(nullptr, menoh_delete_model_builder) {
379  impl_.reset(h);
380  }
381 
383 
389  void attach_external_buffer(std::string const& name,
390  void* buffer_handle) {
393  impl_.get(), name.c_str(), buffer_handle));
394  }
395 
397 
402  std::string const& backend_name,
403  std::string const& backend_config = "") {
406  impl_.get(), model_data.get(), backend_name.c_str(),
407  backend_config.c_str(), &h));
408  return model(h);
409  }
410 
411  private:
412  std::unique_ptr<menoh_model_builder,
413  decltype(&menoh_delete_model_builder)>
414  impl_;
415  };
420 } // namespace menoh
421 
422 #undef MENOH_CPP_API_ERROR_CHECK
423 #endif // MENOH_HPP
menoh_model_builder is helper for creation of model.
menoh_error_code MENOH_API menoh_build_model(const menoh_model_builder_handle builder, const menoh_model_data_handle model_data, const char *backend_name, const char *backend_config, menoh_model_handle *dst_model_handle)
Factory function for menoh_model.
variable_profile_table(menoh_variable_profile_table_handle h)
Definition: menoh.hpp:214
Definition: menoh.hpp:320
menoh_error_code MENOH_API menoh_model_data_add_output_name_to_current_node(menoh_model_data_handle model_data, const char *output_name)
Add a new output name to latest added node in model_data.
menoh_model_data_handle get() const noexcept
Definition: menoh.hpp:98
menoh_error_code MENOH_API menoh_make_model_data(menoh_model_data_handle *dst_handle)
Make empty model_data.
model build_model(model_data const &model_data, std::string const &backend_name, std::string const &backend_config="")
Factory function for model.
Definition: menoh.hpp:401
dtype_t dtype
Definition: menoh.hpp:203
menoh_error_code MENOH_API menoh_variable_profile_table_get_dims_size(const menoh_variable_profile_table_handle variable_profile_table, const char *variable_name, int32_t *dst_size)
Accessor function for variable_profile_table.
menoh_error_code MENOH_API menoh_model_data_add_attribute_int_to_current_node(menoh_model_data_handle model_data, const char *attribute_name, int32_t value)
Add a new int attribute to latest added node in model_data.
menoh_error_code MENOH_API menoh_variable_profile_table_get_dims_at(const menoh_variable_profile_table_handle variable_profile_table, const char *variable_name, int32_t index, int32_t *dst_size)
Accessor function for variable_profile_table.
menoh_error_code MENOH_API menoh_variable_profile_table_builder_add_input_profile(menoh_variable_profile_table_builder_handle builder, const char *name, menoh_dtype dtype, int32_t dims_size, const int32_t *dims)
Add input profile.
void add_output_profile(std::string const &name, dtype_t)
Add output profile. That profile contains name and dtype.
Definition: menoh.hpp:298
menoh_error_code MENOH_API menoh_model_get_variable_buffer_handle(const menoh_model_handle model, const char *variable_name, void **dst_data)
Get a buffer handle attached to target variable.
model_data(menoh_model_data_handle h)
Definition: menoh.hpp:91
The error class thrown when any error occured.
Definition: menoh.hpp:58
menoh_error_code MENOH_API menoh_model_builder_attach_external_buffer(menoh_model_builder_handle builder, const char *variable_name, void *buffer_handle)
Attach a buffer which allocated by users.
void add_attribute_floats_to_current_node(std::string const &attribute_name, std::vector< float > const &value)
Definition: menoh.hpp:159
menoh_error_code MENOH_API menoh_model_data_optimize(menoh_model_data_handle model_data, const menoh_variable_profile_table_handle variable_profile_table)
Optimize function for menoh_model_data.
menoh_model is the main component to execute model inference.
menoh_error_code MENOH_API menoh_make_model_data_from_onnx(const char *onnx_filename, menoh_model_data_handle *dst_handle)
Load onnx file and make model_data.
void run()
Run model inference.
Definition: menoh.hpp:362
menoh_error_code MENOH_API menoh_make_model_builder(const menoh_variable_profile_table_handle variable_profile_table, menoh_model_builder_handle *dst_handle)
Factory function for menoh_model_builder.
void add_input_profile(std::string const &name, dtype_t dtype, std::vector< int32_t > const &dims)
Add input profile. That profile contains name, dtype and dims.
Definition: menoh.hpp:275
variable_profile_table build_variable_profile_table(model_data const &model_data)
Factory function for variable_profile_table.
Definition: menoh.hpp:304
The builder class to build variable_profile_table.
Definition: menoh.hpp:264
Definition: menoh.h:63
dtype_t
Definition: menoh.hpp:73
int32_t menoh_dtype
Definition: menoh.h:51
menoh_variable_profile_table_handle get() const noexcept
Accessor to internal handle.
Definition: menoh.hpp:242
Definition: menoh.hpp:21
void add_parameter(std::string const &parameter_name, dtype_t dtype, std::vector< int > const &dims, void *buffer_handle)
Definition: menoh.hpp:167
void add_attribute_float_to_current_node(std::string const &attribute_name, float value)
Definition: menoh.hpp:151
menoh_error_code MENOH_API menoh_model_data_add_input_name_to_current_node(menoh_model_data_handle model_data, const char *input_name)
Add a new input name to latest added node in model_data.
Definition: menoh.h:61
variable_profile_table_builder()
Definition: menoh.hpp:266
void add_input_name_to_current_node(std::string const &input_name)
Definition: menoh.hpp:121
void MENOH_API menoh_delete_model_data(menoh_model_data_handle model_data)
Model_data delete function.
Definition: menoh.h:76
void add_attribute_int_to_current_node(std::string const &attribute_name, int32_t value)
Definition: menoh.hpp:134
model_data make_model_data_from_onnx(std::string const &onnx_filename)
Load ONNX file and make model_data.
Definition: menoh.hpp:183
model_builder(variable_profile_table const &variable_profile_table)
Definition: menoh.hpp:373
void MENOH_API menoh_delete_model(menoh_model_handle model)
Delete function for model.
menoh_error_code MENOH_API menoh_model_get_variable_dtype(const menoh_model_handle model, const char *variable_name, menoh_dtype *dst_dtype)
Get dtype of target variable.
void add_output_name_to_current_node(std::string const &output_name)
Definition: menoh.hpp:127
error_code_t error_code() const noexcept
Definition: menoh.hpp:66
menoh_variable_profile_table_builder is the builder for creation of menoh_variable_profile_table.
The builder class to build model.
Definition: menoh.hpp:371
model data class
Definition: menoh.hpp:80
Definition: menoh.hpp:202
menoh_error_code MENOH_API menoh_model_data_add_attribute_float_to_current_node(menoh_model_data_handle model_data, const char *attribute_name, float value)
Add a new float attribute to latest added node in model_data.
menoh_error_code MENOH_API menoh_model_data_add_attribute_ints_to_current_node(menoh_model_data_handle model_data, const char *attribute_name, int32_t size, const int32_t *value)
Add a new int array attribute to latest added node in model_data.
menoh_error_code MENOH_API menoh_model_get_variable_dims_at(const menoh_model_handle model, const char *variable_name, int32_t index, int32_t *dst_size)
Get an element of dims of target variable specified by index.
model_data make_model_data_from_onnx_data_on_memory(const uint8_t *onnx_data, uint32_t size)
Make model_data from onnx binary data on memory.
Definition: menoh.hpp:191
Definition: menoh.h:72
menoh_error_code MENOH_API menoh_variable_profile_table_builder_add_output_name(menoh_variable_profile_table_builder_handle builder, const char *name)
Add output name.
Definition: menoh.h:60
void MENOH_API menoh_delete_variable_profile_table_builder(menoh_variable_profile_table_builder_handle builder)
Delete function for variable_profile_table_builder.
variable_profile get_variable_profile(std::string const &name) const
Accessor to variable profile.
Definition: menoh.hpp:221
void reset() noexcept
Release internal alocated memory.
Definition: menoh.hpp:109
menoh_error_code MENOH_API menoh_model_data_add_attribute_floats_to_current_node(menoh_model_data_handle model_data, const char *attribute_name, int32_t size, const float *value)
Add a new float array attribute to latest added node in model_data.
void MENOH_API menoh_delete_model_builder(menoh_model_builder_handle model_builder)
Delete function for model_builder.
menoh_error_code MENOH_API menoh_make_model_data_from_onnx_data_on_memory(const uint8_t *onnx_data, int32_t size, menoh_model_data_handle *dst_handle)
make model_data from onnx binary data on memory
menoh_error_code MENOH_API menoh_variable_profile_table_get_dtype(const menoh_variable_profile_table_handle variable_profile_table, const char *variable_name, menoh_dtype *dst_dtype)
Accessor function for variable_profile_table.
error(menoh_error_code ec, std::string const &message)
Definition: menoh.hpp:60
Definition: menoh.h:62
void add_output_name(std::string const &name)
Add a name of required output.
Definition: menoh.hpp:286
error_code_t
Definition: menoh.hpp:27
dtype_t dtype
Definition: menoh.hpp:321
#define MENOH_CPP_API_ERROR_CHECK(statement)
Definition: menoh.hpp:11
void optimize(variable_profile_table const &vpt)
Optimize model_data.
Definition: menoh.hpp:255
int32_t menoh_error_code
Definition: menoh.h:83
variable get_variable(std::string const &name) const
Accsessor to internal variable.
Definition: menoh.hpp:339
model(menoh_model_handle h)
Definition: menoh.hpp:332
Definition: menoh.h:47
void attach_external_buffer(std::string const &name, void *buffer_handle)
Users can attach external buffers to variables.
Definition: menoh.hpp:389
void add_attribute_ints_to_current_node(std::string const &attribute_name, std::vector< int32_t > const &value)
Definition: menoh.hpp:142
menoh_error_code MENOH_API menoh_build_variable_profile_table(const menoh_variable_profile_table_builder_handle builder, const menoh_model_data_handle model_data, menoh_variable_profile_table_handle *dst_handle)
Factory function for variable_profile_table.
Definition: menoh.h:66
menoh_variable_profile_table contains information of dims of variables.
menoh_error_code MENOH_API menoh_model_data_add_new_node(menoh_model_data_handle model_data, const char *op_type)
Add a new node to model_data.
void add_new_node(std::string const &op_type)
Definition: menoh.hpp:116
menoh_error_code MENOH_API menoh_model_data_add_parameter(menoh_model_data_handle model_data, const char *parameter_name, menoh_dtype dtype, int32_t dims_size, const int32_t *dims, void *buffer_handle)
Add a new parameter in model_data.
error(error_code_t ec, std::string const &message)
Definition: menoh.hpp:63
std::vector< int32_t > dims
Definition: menoh.hpp:204
Key value store for variable_profile.
Definition: menoh.hpp:208
void MENOH_API menoh_delete_variable_profile_table(menoh_variable_profile_table_handle variable_profile_table)
Delete function for variable_profile_table.
menoh_model_data contains model parameters and computation graph structure.
Definition: menoh.h:65
model_data()
Definition: menoh.hpp:82
std::vector< int32_t > dims
Definition: menoh.hpp:322
menoh_error_code MENOH_API menoh_model_get_variable_dims_size(const menoh_model_handle model, const char *variable_name, int32_t *dst_size)
Get size of dims of target variable.
menoh_error_code MENOH_API menoh_make_variable_profile_table_builder(menoh_variable_profile_table_builder_handle *dst_handle)
Factory function for variable_profile_table_builder.
void * buffer_handle
Definition: menoh.hpp:323
The main component to run inference.
Definition: menoh.hpp:327
menoh_error_code MENOH_API menoh_model_run(menoh_model_handle model)
Run model inference.