Michael Martis | a967f63 | 2018-08-10 10:39:00 +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 | |
| 5 | #ifndef ML_TEST_UTILS_H_ |
| 6 | #define ML_TEST_UTILS_H_ |
| 7 | |
Michael Martis | c22823d | 2018-09-14 12:54:04 +1000 | [diff] [blame] | 8 | #include <string> |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame^] | 11 | #include "ml/mojom/tensor.mojom.h" |
Michael Martis | c22823d | 2018-09-14 12:54:04 +1000 | [diff] [blame] | 12 | #include "ml/tensor_view.h" |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 13 | |
| 14 | namespace ml { |
| 15 | |
| 16 | // Create a tensor with the given shape and values. Does no validity checking |
| 17 | // (by design, as we sometimes need to pass bad tensors to test error handling). |
| 18 | template <typename T> |
| 19 | chromeos::machine_learning::mojom::TensorPtr NewTensor( |
| 20 | const std::vector<int64_t>& shape, const std::vector<T>& values) { |
| 21 | auto tensor(chromeos::machine_learning::mojom::Tensor::New()); |
| 22 | TensorView<T> tensor_view(tensor); |
| 23 | tensor_view.Allocate(); |
Hidehiko Abe | 8ab64a6 | 2018-09-19 00:04:39 +0900 | [diff] [blame^] | 24 | tensor_view.GetShape() = shape; |
| 25 | tensor_view.GetValues() = values; |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 26 | |
| 27 | return tensor; |
| 28 | } |
| 29 | |
Michael Martis | c22823d | 2018-09-14 12:54:04 +1000 | [diff] [blame] | 30 | // Return the model directory for tests (or die if it cannot be obtained). |
| 31 | std::string GetTestModelDir(); |
| 32 | |
Michael Martis | a967f63 | 2018-08-10 10:39:00 +1000 | [diff] [blame] | 33 | } // namespace ml |
| 34 | |
| 35 | #endif // ML_TEST_UTILS_H_ |