blob: aa8c339cc0556f01345243caf022ee02b471e3a3 [file] [log] [blame]
Michael Martis26abcd82018-08-08 10:57:25 +10001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Hidehiko Abeaa488c32018-08-31 23:49:41 +09005#include "ml/mojom/tensor.mojom.h"
Michael Martis26abcd82018-08-08 10:57:25 +10006
Hidehiko Abe8ab64a62018-09-19 00:04:39 +09007#include <vector>
8
Michael Martis26abcd82018-08-08 10:57:25 +10009#ifndef ML_TENSOR_VIEW_H_
10#define ML_TENSOR_VIEW_H_
11
12namespace ml {
13
14// Provides basic error checking and a common interface for mojom::TensorPtrs of
15// any underlying data type.
16//
17// Basic usage of a TensorView is as follows.
18// Non-const view:
19// TensorPtr ptr;
20// TensorView<double> view(ptr);
21// view.Allocate(); // Creates a FloatList in ptr.
22// view.GetValues().push_back(0.5); // Adds first elem of FloatList in ptr.
23//
24// Const view:
25// const TensorPtr ptr = ...
26// const TensorView<double> view(ptr);
27// double v = view.GetValues()[0]; // Gets first elem of FloatList in ptr.
28//
29// Type-specific funtionality is implemented in template specializations below.
30template <typename T>
31class TensorView {
32 public:
33 // The given tensor must outlive this view.
34 explicit TensorView(
35 const chromeos::machine_learning::mojom::TensorPtr& tensor)
36 : tensor_(tensor) {}
37
38 // Return the shape array of the tensor.
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090039 std::vector<int64_t>& GetShape() { return tensor_->shape->value; }
Michael Martis26abcd82018-08-08 10:57:25 +100040
Tom Hughes1d1c1922020-08-27 16:16:53 -070041 const std::vector<int64_t>& GetShape() const { return tensor_->shape->value; }
Michael Martis26abcd82018-08-08 10:57:25 +100042
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090043 // Return the value array of the tensor.
44 // Defined only in each specialization for T.
45 std::vector<T>& GetValues();
Michael Martis26abcd82018-08-08 10:57:25 +100046
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090047 const std::vector<T>& GetValues() const {
Michael Martis26abcd82018-08-08 10:57:25 +100048 return const_cast<TensorView<T>*>(this)->GetValues();
49 }
50
51 // Return true if the tensor contains values of the correct type. Should be
52 // specialized for each tensor data type T.
53 bool IsValidType() const { return false; }
54
55 // Return true if the tensor is in a valid format (i.e. valid dimensions and
56 // the right number of entries for its shape).
57 bool IsValidFormat() const {
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090058 const std::vector<int64_t>& dims = GetShape();
Michael Martis26abcd82018-08-08 10:57:25 +100059
60 // Special case: no entries.
61 if (dims.empty())
62 return GetValues().empty();
63
64 // Otherwise, values size should be the product of all dimension lengths.
65 int64_t num_entries = 1;
66 for (const int64_t dim : dims) {
67 if (dim < 0)
68 return false;
69
70 num_entries *= dim;
71 }
72 return num_entries == GetValues().size();
73 }
74
75 // Allocate memory for the members of the tensor object (including values).
76 void Allocate() {
77 tensor_->shape = chromeos::machine_learning::mojom::Int64List::New();
Andrew Moylan79b34a42020-07-08 11:13:11 +100078 // TODO(hidehiko): assigning std::vector<>() to `value` is unneeded
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090079 // on libmojo uprev. Remove them after the uprev.
80 tensor_->shape->value = std::vector<int64_t>();
Michael Martis26abcd82018-08-08 10:57:25 +100081 tensor_->data = chromeos::machine_learning::mojom::ValueList::New();
82 AllocateValues();
Michael Martis26abcd82018-08-08 10:57:25 +100083 }
84
85 private:
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090086 // Allocate memory for the value array of this tensor.
87 // Defined only in each specialization for T.
88 void AllocateValues();
Michael Martis26abcd82018-08-08 10:57:25 +100089
90 const chromeos::machine_learning::mojom::TensorPtr& tensor_;
91
92 DISALLOW_COPY_AND_ASSIGN(TensorView);
93};
94
95// Specializations for int tensors.
96template <>
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090097std::vector<int64_t>& TensorView<int64_t>::GetValues();
Michael Martis26abcd82018-08-08 10:57:25 +100098template <>
99bool TensorView<int64_t>::IsValidType() const;
100template <>
101void TensorView<int64_t>::AllocateValues();
102
103// Specializations for float tensors.
104template <>
Hidehiko Abe8ab64a62018-09-19 00:04:39 +0900105std::vector<double>& TensorView<double>::GetValues();
Michael Martis26abcd82018-08-08 10:57:25 +1000106template <>
107bool TensorView<double>::IsValidType() const;
108template <>
109void TensorView<double>::AllocateValues();
110
111} // namespace ml
112
113#endif // ML_TENSOR_VIEW_H_