Viam C++ SDK current
Loading...
Searching...
No Matches
mlmodel.hpp
1// Copyright 2023 Viam Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <boost/mpl/joint_view.hpp>
18#include <boost/mpl/list.hpp>
19#include <boost/mpl/transform_view.hpp>
20#include <boost/variant/variant.hpp>
21#include <xtensor/xadapt.hpp>
22
23#include <viam/sdk/common/utils.hpp>
24#include <viam/sdk/services/service.hpp>
25
26namespace viam {
27namespace sdk {
28
35class MLModelService : public Service {
36 private:
37 template <typename T>
38 struct make_tensor_view_ {
39 using shape_t = std::vector<std::size_t>;
40
41 using xt_no_ownership_t = decltype(xt::no_ownership());
42
43 using type = decltype(xt::adapt(std::declval<const T*>(),
44 std::declval<std::size_t>(),
45 std::declval<xt_no_ownership_t>(),
46 std::declval<shape_t>()));
47 };
48
49 public:
50 API api() const override;
51
52 template <typename T>
53 using tensor_view = typename make_tensor_view_<T>::type;
54
55 template <typename T>
56 static tensor_view<T> make_tensor_view(const T* data,
57 std::size_t size,
58 typename tensor_view<T>::shape_type shape) {
59 return xt::adapt(std::move(data), std::move(size), xt::no_ownership(), std::move(shape));
60 }
61
62 // Now that we have a factory for our tensor view types, use mpl
63 // to produce a variant over tensor views over the primitive types
64 // we care about, which are the signed and unsigned fixed width
65 // integral types and the two floating point types.
66 using signed_integral_base_types =
67 boost::mpl::list<std::int8_t, std::int16_t, std::int32_t, std::int64_t>;
68
69 using unsigned_integral_base_types =
70 boost::mpl::transform_view<signed_integral_base_types,
71 std::make_unsigned<boost::mpl::placeholders::_1>>;
72
73 using integral_base_types =
74 boost::mpl::joint_view<signed_integral_base_types, unsigned_integral_base_types>;
75
76 using fp_base_types = boost::mpl::list<float, double>;
77
78 using base_types = boost::mpl::joint_view<integral_base_types, fp_base_types>;
79
80 using tensor_view_types =
81 boost::mpl::transform_view<base_types, make_tensor_view_<boost::mpl::placeholders::_1>>;
82
83 // Union the tensor views for the various base types.
84 using tensor_views = boost::make_variant_over<tensor_view_types>::type;
85
86 // Our parameters to and from the model come as named tensor_views.
87 using named_tensor_views = std::unordered_map<std::string, tensor_views>;
88
95 inline std::shared_ptr<named_tensor_views> infer(const named_tensor_views& inputs) {
96 return infer(inputs, {});
97 }
98
107 virtual std::shared_ptr<named_tensor_views> infer(const named_tensor_views& inputs,
108 const ProtoStruct& extra) = 0;
109
110 struct tensor_info {
111 struct file {
112 std::string name;
113 std::string description;
114
115 enum : std::uint8_t {
116 k_label_type_tensor_value = 0,
117 k_label_type_tensor_axis = 1,
118 } label_type;
119 };
120
121 std::string name;
122 std::string description;
123
124 enum class data_types : std::uint8_t {
125 k_int8 = 0,
126 k_uint8 = 1,
127 k_int16 = 2,
128 k_uint16 = 3,
129 k_int32 = 4,
130 k_uint32 = 5,
131 k_int64 = 6,
132 k_uint64 = 7,
133 k_float32 = 8,
134 k_float64 = 9,
135 } data_type;
136
137 std::vector<int> shape;
138 std::vector<file> associated_files;
139
140 ProtoStruct extra;
141
142 static boost::optional<data_types> string_to_data_type(const std::string& str);
143 static const char* data_type_to_string(data_types data_type);
144
145 static data_types tensor_views_to_data_type(const tensor_views& view);
146 };
147
148 struct metadata {
149 std::string name;
150 std::string type;
151 std::string description;
152 std::vector<tensor_info> inputs;
153 std::vector<tensor_info> outputs;
154 };
155
157 inline struct metadata metadata() {
158 return metadata({});
159 }
160
164 virtual struct metadata metadata(const ProtoStruct& extra) = 0;
165
166 protected:
167 explicit MLModelService(std::string name);
168};
169
170template <>
172 static API api();
173};
174
175} // namespace sdk
176} // namespace viam
Extends APIType to additionally define a resource's subtype (e.g., camera).
Definition resource_api.hpp:33
Represents a machine trained learning model instance.
Definition mlmodel.hpp:35
API api() const override
Returns the API associated with a particular resource.
std::shared_ptr< named_tensor_views > infer(const named_tensor_views &inputs)
Runs the model against the input tensors and returns inference results as tensors.
Definition mlmodel.hpp:95
struct metadata metadata()
Returns metadata describing the inputs and outputs of the model.
Definition mlmodel.hpp:157
virtual std::shared_ptr< named_tensor_views > infer(const named_tensor_views &inputs, const ProtoStruct &extra)=0
Runs the model against the input tensors and returns inference results as tensors.
virtual std::string name() const
Return the resource's name.
Definition service.hpp:10
Definition resource_api.hpp:50
Definition mlmodel.hpp:148
Definition mlmodel.hpp:110