blob: c8418fee4f216a6c861c563e465e45c1dd8efe71 [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) {}
Qijiang Fan6bc59e12020-11-11 02:51:06 +090037 TensorView(const TensorView&) = delete;
38 TensorView& operator=(const TensorView&) = delete;
Michael Martis26abcd82018-08-08 10:57:25 +100039
40 // Return the shape array of the tensor.
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090041 std::vector<int64_t>& GetShape() { return tensor_->shape->value; }
Michael Martis26abcd82018-08-08 10:57:25 +100042
Tom Hughes1d1c1922020-08-27 16:16:53 -070043 const std::vector<int64_t>& GetShape() const { return tensor_->shape->value; }
Michael Martis26abcd82018-08-08 10:57:25 +100044
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090045 // Return the value array of the tensor.
46 // Defined only in each specialization for T.
47 std::vector<T>& GetValues();
Michael Martis26abcd82018-08-08 10:57:25 +100048
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090049 const std::vector<T>& GetValues() const {
Michael Martis26abcd82018-08-08 10:57:25 +100050 return const_cast<TensorView<T>*>(this)->GetValues();
51 }
52
53 // Return true if the tensor contains values of the correct type. Should be
54 // specialized for each tensor data type T.
55 bool IsValidType() const { return false; }
56
57 // Return true if the tensor is in a valid format (i.e. valid dimensions and
58 // the right number of entries for its shape).
59 bool IsValidFormat() const {
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090060 const std::vector<int64_t>& dims = GetShape();
Michael Martis26abcd82018-08-08 10:57:25 +100061
62 // Special case: no entries.
63 if (dims.empty())
64 return GetValues().empty();
65
66 // Otherwise, values size should be the product of all dimension lengths.
67 int64_t num_entries = 1;
68 for (const int64_t dim : dims) {
69 if (dim < 0)
70 return false;
71
72 num_entries *= dim;
73 }
74 return num_entries == GetValues().size();
75 }
76
77 // Allocate memory for the members of the tensor object (including values).
78 void Allocate() {
79 tensor_->shape = chromeos::machine_learning::mojom::Int64List::New();
Andrew Moylan79b34a42020-07-08 11:13:11 +100080 // TODO(hidehiko): assigning std::vector<>() to `value` is unneeded
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090081 // on libmojo uprev. Remove them after the uprev.
82 tensor_->shape->value = std::vector<int64_t>();
Michael Martis26abcd82018-08-08 10:57:25 +100083 tensor_->data = chromeos::machine_learning::mojom::ValueList::New();
84 AllocateValues();
Michael Martis26abcd82018-08-08 10:57:25 +100085 }
86
87 private:
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090088 // Allocate memory for the value array of this tensor.
89 // Defined only in each specialization for T.
90 void AllocateValues();
Michael Martis26abcd82018-08-08 10:57:25 +100091
92 const chromeos::machine_learning::mojom::TensorPtr& tensor_;
Michael Martis26abcd82018-08-08 10:57:25 +100093};
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_