blob: 2785ec495ed545b18834db4ccc2f322e177fdd6f [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
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090041 const std::vector<int64_t>& GetShape() const {
42 return tensor_->shape->value;
Michael Martis26abcd82018-08-08 10:57:25 +100043 }
44
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();
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090080 // TODO(hidehiko): assigning std::vector<>() to |value| is unneeded
81 // 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_;
93
94 DISALLOW_COPY_AND_ASSIGN(TensorView);
95};
96
97// Specializations for int tensors.
98template <>
Hidehiko Abe8ab64a62018-09-19 00:04:39 +090099std::vector<int64_t>& TensorView<int64_t>::GetValues();
Michael Martis26abcd82018-08-08 10:57:25 +1000100template <>
101bool TensorView<int64_t>::IsValidType() const;
102template <>
103void TensorView<int64_t>::AllocateValues();
104
105// Specializations for float tensors.
106template <>
Hidehiko Abe8ab64a62018-09-19 00:04:39 +0900107std::vector<double>& TensorView<double>::GetValues();
Michael Martis26abcd82018-08-08 10:57:25 +1000108template <>
109bool TensorView<double>::IsValidType() const;
110template <>
111void TensorView<double>::AllocateValues();
112
113} // namespace ml
114
115#endif // ML_TENSOR_VIEW_H_