SSCMA-Micro CPP SDK  v2.0.0
SSCMA-Micro is a cross-platform machine learning inference framework designed for embedded devices.
model.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 
5 #include "core/ma_core.h"
6 #include "porting/ma_porting.h"
7 #include "resource.hpp"
8 
9 namespace ma::server::callback {
10 
11 using namespace ma::model;
12 
13 void getAvailableModels(const std::vector<std::string>& args, Transport& transport, Encoder& encoder) {
14  MA_ASSERT(args.size() >= 1);
15  const auto& cmd = args[0];
16 
17  auto& models = static_resource->device->getModels();
18  encoder.begin(MA_MSG_TYPE_RESP, MA_OK, cmd);
19  encoder.write(models);
20  encoder.end();
21  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
22 }
23 
24 void configureModel(const std::vector<std::string>& args,
25  Transport& transport,
26  Encoder& encoder,
27  bool called_by_event = false) {
28  MA_ASSERT(args.size() >= 2);
29  const auto& cmd = args[0];
30  const size_t model_id = std::atoi(args[1].c_str());
31 
32  ma_err_t ret = MA_OK;
33  auto& models = static_resource->device->getModels();
34 
35  auto it = std::find_if(models.begin(), models.end(), [&](const ma_model_t& m) { return m.id == model_id; });
36  if (it == models.end()) {
37  ret = MA_ENOENT;
38  goto exit;
39  }
40 
41 #if MA_USE_FILESYSTEM_POSIX
42  ret = static_resource->engine->load(static_cast<const char*>(it->addr));
43 #else
44  ret = static_resource->engine->load(it->addr, it->size);
45 #endif
46  if (ret != MA_OK) [[unlikely]]
47  goto exit;
48 
49  if (static_resource->current_model_id != model_id) {
50  static_resource->current_model_id = model_id;
51  if (!called_by_event) {
52  MA_STORAGE_SET_POD(
53  ret, static_resource->device->getStorage(), MA_STORAGE_KEY_MODEL_ID, static_resource->current_model_id);
54  }
55  }
56 
57 exit:
58  encoder.begin(MA_MSG_TYPE_RESP, ret, cmd);
59  if (static_resource->current_model_id) {
60  encoder.write("model", it == models.end() ? ma_model_t{} : *it);
61  }
62  encoder.end();
63  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
64 }
65 
66 void initDefaultModel(Encoder& encoder) {
67  if (static_resource->device->getTransports().empty()) {
68  MA_LOGD(MA_TAG, "No transport available");
69  return;
70  }
71  auto& transport = static_resource->device->getTransports().front();
72  if (!transport || !*transport) {
73  MA_LOGD(MA_TAG, "Transport not available");
74  return;
75  }
76 
77  size_t model_id = 0;
78 
79  MA_STORAGE_GET_POD(static_resource->device->getStorage(), MA_STORAGE_KEY_MODEL_ID, model_id, 1);
80 
81  std::vector<std::string> args{"INIT@MODEL", std::to_string(static_cast<int>(model_id))};
82  configureModel(args, *transport, encoder, true);
83 }
84 
85 void getModelInfo(const std::vector<std::string>& args, Transport& transport, Encoder& encoder) {
86  MA_ASSERT(args.size() >= 1);
87  const auto& cmd = args[0];
88 
89  ma_err_t ret = MA_OK;
90  auto& models = static_resource->device->getModels();
91 
92  auto it = std::find_if(
93  models.begin(), models.end(), [&](const ma_model_t& m) { return m.id == static_resource->current_model_id; });
94  if (it == models.end()) {
95  ret = MA_ENOENT;
96  }
97 
98  encoder.begin(MA_MSG_TYPE_RESP, ret, cmd);
99  if (it != models.end()) {
100  std::vector<ma_model_t> model{*it};
101  encoder.write(model);
102  }
103  encoder.end();
104  transport.send(reinterpret_cast<const char*>(encoder.data()), encoder.size());
105 }
106 
107 } // namespace ma::server::callback
Definition: ma_codec_base.h:14
virtual ma_err_t end()=0
Encoder type for end.
virtual const void * data() const =0
Encoder type for get data.
virtual ma_err_t begin()=0
Encoder type for begin.
virtual ma_err_t write(const std::string &key, int8_t value)=0
Encoder type for write int8_t value.
virtual const size_t size() const =0
Encoder type for get size.
Definition: ma_transport.h:12
virtual size_t send(const char *data, size_t length) noexcept=0
#define MA_LOGD(TAG,...)
Definition: ma_debug.h:79
#define MA_ASSERT(expr)
Definition: ma_debug.h:119
#define MA_TAG
Definition: ma_debug.h:9
#define MA_STORAGE_KEY_MODEL_ID
Definition: ma_definations.h:19
@ MA_MSG_TYPE_RESP
Definition: ma_types.h:252
ma_err_t
Definition: ma_types.h:21
@ MA_ENOENT
Definition: ma_types.h:33
@ MA_OK
Definition: ma_types.h:23
Definition: ma_model_classifier.cpp:5
Definition: algorithm.hpp:11
void configureModel(const std::vector< std::string > &args, Transport &transport, Encoder &encoder, bool called_by_event=false)
Definition: model.hpp:24
void initDefaultModel(Encoder &encoder)
Definition: model.hpp:66
void getModelInfo(const std::vector< std::string > &args, Transport &transport, Encoder &encoder)
Definition: model.hpp:85
void getAvailableModels(const std::vector< std::string > &args, Transport &transport, Encoder &encoder)
Definition: model.hpp:13
#define static_resource
Definition: resource.hpp:64
Definition: ma_types.h:289