Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 1 | // 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 Abe | aa488c3 | 2018-08-31 23:49:41 +0900 | [diff] [blame] | 5 | #include "ml/mojom/tensor.mojom.h" |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 6 | |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 9 | #ifndef ML_TENSOR_VIEW_H_ |
| 10 | #define ML_TENSOR_VIEW_H_ |
| 11 | |
| 12 | namespace 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. |
| 30 | template <typename T> |
| 31 | class 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 Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 37 | TensorView(const TensorView&) = delete; |
| 38 | TensorView& operator=(const TensorView&) = delete; |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 39 | |
| 40 | // Return the shape array of the tensor. |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 41 | std::vector<int64_t>& GetShape() { return tensor_->shape->value; } |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 42 | |
Tom Hughes | 1d1c192 | 2020-08-27 16:16:53 -0700 | [diff] [blame] | 43 | const std::vector<int64_t>& GetShape() const { return tensor_->shape->value; } |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 44 | |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 45 | // Return the value array of the tensor. |
| 46 | // Defined only in each specialization for T. |
| 47 | std::vector<T>& GetValues(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 48 | |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 49 | const std::vector<T>& GetValues() const { |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 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 { |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 60 | const std::vector<int64_t>& dims = GetShape(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 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(); |
Andrew Moylan | 79b34a4 | 2020-07-08 11:13:11 +1000 | [diff] [blame] | 80 | // TODO(hidehiko): assigning std::vector<>() to `value` is unneeded |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 81 | // on libmojo uprev. Remove them after the uprev. |
| 82 | tensor_->shape->value = std::vector<int64_t>(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 83 | tensor_->data = chromeos::machine_learning::mojom::ValueList::New(); |
| 84 | AllocateValues(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | private: |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 88 | // Allocate memory for the value array of this tensor. |
| 89 | // Defined only in each specialization for T. |
| 90 | void AllocateValues(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 91 | |
| 92 | const chromeos::machine_learning::mojom::TensorPtr& tensor_; |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | // Specializations for int tensors. |
| 96 | template <> |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 97 | std::vector<int64_t>& TensorView<int64_t>::GetValues(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 98 | template <> |
| 99 | bool TensorView<int64_t>::IsValidType() const; |
| 100 | template <> |
| 101 | void TensorView<int64_t>::AllocateValues(); |
| 102 | |
| 103 | // Specializations for float tensors. |
| 104 | template <> |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame] | 105 | std::vector<double>& TensorView<double>::GetValues(); |
Michael Martis | 26abcd8 | 2018-08-08 10:57:25 +1000 | [diff] [blame] | 106 | template <> |
| 107 | bool TensorView<double>::IsValidType() const; |
| 108 | template <> |
| 109 | void TensorView<double>::AllocateValues(); |
| 110 | |
| 111 | } // namespace ml |
| 112 | |
| 113 | #endif // ML_TENSOR_VIEW_H_ |