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/api/service/mlmodel/v1/mlmodel.grpc.pb.h>
24
25#include <viam/sdk/common/utils.hpp>
26#include <viam/sdk/services/service.hpp>
27
28namespace viam {
29namespace sdk {
30
37class MLModelService : public Service {
38 private:
39 template <typename T>
40 struct make_tensor_view_ {
41 using shape_t = std::vector<std::size_t>;
42
43 using xt_no_ownership_t = decltype(xt::no_ownership());
44
45 using type = decltype(xt::adapt(std::declval<const T*>(),
46 std::declval<std::size_t>(),
47 std::declval<xt_no_ownership_t>(),
48 std::declval<shape_t>()));
49 };
50
51 public:
52 API api() const override;
53
54 template <typename T>
55 using tensor_view = typename make_tensor_view_<T>::type;
56
57 template <typename T>
58 static tensor_view<T> make_tensor_view(const T* data,
59 std::size_t size,
60 typename tensor_view<T>::shape_type shape) {
61 return xt::adapt(std::move(data), std::move(size), xt::no_ownership(), std::move(shape));
62 }
63
64 // Now that we have a factory for our tensor view types, use mpl
65 // to produce a variant over tensor views over the primitive types
66 // we care about, which are the signed and unsigned fixed width
67 // integral types and the two floating point types.
68 using signed_integral_base_types =
69 boost::mpl::list<std::int8_t, std::int16_t, std::int32_t, std::int64_t>;
70
71 using unsigned_integral_base_types =
72 boost::mpl::transform_view<signed_integral_base_types,
73 std::make_unsigned<boost::mpl::placeholders::_1>>;
74
75 using integral_base_types =
76 boost::mpl::joint_view<signed_integral_base_types, unsigned_integral_base_types>;
77
78 using fp_base_types = boost::mpl::list<float, double>;
79
80 using base_types = boost::mpl::joint_view<integral_base_types, fp_base_types>;
81
82 using tensor_view_types =
83 boost::mpl::transform_view<base_types, make_tensor_view_<boost::mpl::placeholders::_1>>;
84
85 // Union the tensor views for the various base types.
86 using tensor_views = boost::make_variant_over<tensor_view_types>::type;
87
88 // Our parameters to and from the model come as named tensor_views.
89 using named_tensor_views = std::unordered_map<std::string, tensor_views>;
90
97 inline std::shared_ptr<named_tensor_views> infer(const named_tensor_views& inputs) {
98 return infer(inputs, {});
99 }
100
109 virtual std::shared_ptr<named_tensor_views> infer(const named_tensor_views& inputs,
110 const AttributeMap& extra) = 0;
111
112 struct tensor_info {
113 struct file {
114 std::string name;
115 std::string description;
116
117 enum : std::uint8_t {
118 k_label_type_tensor_value = 0,
119 k_label_type_tensor_axis = 1,
120 } label_type;
121 };
122
123 std::string name;
124 std::string description;
125
126 enum class data_types : std::uint8_t {
127 k_int8 = 0,
128 k_uint8 = 1,
129 k_int16 = 2,
130 k_uint16 = 3,
131 k_int32 = 4,
132 k_uint32 = 5,
133 k_int64 = 6,
134 k_uint64 = 7,
135 k_float32 = 8,
136 k_float64 = 9,
137 } data_type;
138
139 std::vector<int> shape;
140 std::vector<file> associated_files;
141
142 AttributeMap extra;
143
144 static boost::optional<data_types> string_to_data_type(const std::string& str);
145 static const char* data_type_to_string(data_types data_type);
146
147 static data_types tensor_views_to_data_type(const tensor_views& view);
148 };
149
150 struct metadata {
151 std::string name;
152 std::string type;
153 std::string description;
154 std::vector<tensor_info> inputs;
155 std::vector<tensor_info> outputs;
156 };
157
159 inline struct metadata metadata() {
160 return metadata({});
161 }
162
166 virtual struct metadata metadata(const AttributeMap& extra) = 0;
167
168 protected:
169 explicit MLModelService(std::string name);
170};
171
172template <>
174 static API api();
175};
176
177} // namespace sdk
178} // 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:37
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:97
struct metadata metadata()
Returns metadata describing the inputs and outputs of the model.
Definition mlmodel.hpp:159
virtual std::shared_ptr< named_tensor_views > infer(const named_tensor_views &inputs, const AttributeMap &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:12
Definition resource_api.hpp:50
Definition mlmodel.hpp:150
Definition mlmodel.hpp:112