blob: ae3587bb094344b4b0fe9833466d75160578197b [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
5#include <mojo/public/cpp/bindings/array.h>
6
7#include "mojom/tensor.mojom.h"
8
9#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.
39 mojo::Array<int64_t>& GetShape() { return tensor_->shape->value; }
40
41 const mojo::Array<int64_t>& GetShape() const {
42 return const_cast<TensorView<T>*>(this)->GetShape();
43 }
44
45 // Return the value array of the tensor. Should be specialized for each tensor
46 // data type T.
47 mojo::Array<T>& GetValues() { return mojo::Array<T>(nullptr); }
48
49 const mojo::Array<T>& GetValues() const {
50 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 {
60 const mojo::Array<int64_t>& dims = GetShape();
61
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();
80 GetShape().SetToEmpty();
81
82 tensor_->data = chromeos::machine_learning::mojom::ValueList::New();
83 AllocateValues();
84 GetValues().SetToEmpty();
85 }
86
87 private:
88 // Allocate memory for the value array of this tensor. Should be specialized
89 // for each tensor data type T.
90 void AllocateValues() {}
91
92 const chromeos::machine_learning::mojom::TensorPtr& tensor_;
93
94 DISALLOW_COPY_AND_ASSIGN(TensorView);
95};
96
97// Specializations for int tensors.
98template <>
99mojo::Array<int64_t>& TensorView<int64_t>::GetValues();
100template <>
101bool TensorView<int64_t>::IsValidType() const;
102template <>
103void TensorView<int64_t>::AllocateValues();
104
105// Specializations for float tensors.
106template <>
107mojo::Array<double>& TensorView<double>::GetValues();
108template <>
109bool TensorView<double>::IsValidType() const;
110template <>
111void TensorView<double>::AllocateValues();
112
113} // namespace ml
114
115#endif // ML_TENSOR_VIEW_H_