diff options
author | Slava Shklyaev <slavash@google.com> | 2019-11-08 08:57:01 -0800 |
---|---|---|
committer | android-build-merger <android-build-merger@google.com> | 2019-11-08 08:57:01 -0800 |
commit | ea2b03f09b25572b7822ee0e878666eb1b98e913 (patch) | |
tree | 731b92575984385b938f213d411ad5a134d50c60 | |
parent | 4409f3a7a92f225ce7a0259deac1d9d66021cebc (diff) | |
parent | 9d56495f5cc98c409eb5fe8c0e35d9d0dad0681e (diff) | |
download | ml-ea2b03f09b25572b7822ee0e878666eb1b98e913.tar.gz |
Merge changes I2fc1bac8,I660e1949 am: f529e1bfb7
am: 9d56495f5c
Change-Id: I3ce87298b81fc42055ac564f889861141916ffaf
-rw-r--r-- | nn/runtime/test/TestNeuralNetworksWrapper.h | 39 | ||||
-rw-r--r-- | nn/runtime/test/TestTrivialModel.cpp | 20 |
2 files changed, 41 insertions, 18 deletions
diff --git a/nn/runtime/test/TestNeuralNetworksWrapper.h b/nn/runtime/test/TestNeuralNetworksWrapper.h index 8bc8a9e7c..05e68ace2 100644 --- a/nn/runtime/test/TestNeuralNetworksWrapper.h +++ b/nn/runtime/test/TestNeuralNetworksWrapper.h @@ -20,15 +20,17 @@ #ifndef ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_TEST_NEURAL_NETWORKS_WRAPPER_H #define ANDROID_FRAMEWORKS_ML_NN_RUNTIME_TEST_TEST_NEURAL_NETWORKS_WRAPPER_H -#include "NeuralNetworks.h" -#include "NeuralNetworksWrapper.h" -#include "NeuralNetworksWrapperExtensions.h" - #include <math.h> + #include <optional> #include <string> +#include <utility> #include <vector> +#include "NeuralNetworks.h" +#include "NeuralNetworksWrapper.h" +#include "NeuralNetworksWrapperExtensions.h" + namespace android { namespace nn { namespace test_wrapper { @@ -147,6 +149,16 @@ class Model { return mNextOperandId++; } + template <typename T> + uint32_t addConstantOperand(const OperandType* type, const T& value) { + static_assert(sizeof(T) <= ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES, + "Values larger than ANEURALNETWORKS_MAX_SIZE_OF_IMMEDIATELY_COPIED_VALUES " + "not supported"); + uint32_t index = addOperand(type); + setOperandValue(index, &value); + return index; + } + void setOperandValue(uint32_t index, const void* buffer, size_t length) { if (ANeuralNetworksModel_setOperandValue(mModel, index, buffer, length) != ANEURALNETWORKS_NO_ERROR) { @@ -154,6 +166,12 @@ class Model { } } + template <typename T> + void setOperandValue(uint32_t index, const T* value) { + static_assert(!std::is_pointer<T>(), "No operand may have a pointer as its value"); + return setOperandValue(index, value, sizeof(T)); + } + void setOperandValueFromMemory(uint32_t index, const Memory* memory, uint32_t offset, size_t length) { if (ANeuralNetworksModel_setOperandValueFromMemory(mModel, index, memory->get(), offset, @@ -290,6 +308,13 @@ class Execution { ANeuralNetworksExecution_setInput(mExecution, index, type, buffer, length)); } + template <typename T> + Result setInput(uint32_t index, const T* value, + const ANeuralNetworksOperandType* type = nullptr) { + static_assert(!std::is_pointer<T>(), "No operand may have a pointer as its value"); + return setInput(index, value, sizeof(T), type); + } + Result setInputFromMemory(uint32_t index, const Memory* memory, uint32_t offset, uint32_t length, const ANeuralNetworksOperandType* type = nullptr) { return static_cast<Result>(ANeuralNetworksExecution_setInputFromMemory( @@ -302,6 +327,12 @@ class Execution { ANeuralNetworksExecution_setOutput(mExecution, index, type, buffer, length)); } + template <typename T> + Result setOutput(uint32_t index, T* value, const ANeuralNetworksOperandType* type = nullptr) { + static_assert(!std::is_pointer<T>(), "No operand may have a pointer as its value"); + return setOutput(index, value, sizeof(T), type); + } + Result setOutputFromMemory(uint32_t index, const Memory* memory, uint32_t offset, uint32_t length, const ANeuralNetworksOperandType* type = nullptr) { return static_cast<Result>(ANeuralNetworksExecution_setOutputFromMemory( diff --git a/nn/runtime/test/TestTrivialModel.cpp b/nn/runtime/test/TestTrivialModel.cpp index 7280e6ae0..85da1d1f0 100644 --- a/nn/runtime/test/TestTrivialModel.cpp +++ b/nn/runtime/test/TestTrivialModel.cpp @@ -26,6 +26,8 @@ namespace { typedef float Matrix3x4[3][4]; typedef float Matrix4[4]; +const int32_t kNoActivation = ANEURALNETWORKS_FUSED_NONE; + class TrivialTest : public ::testing::Test { protected: virtual void SetUp() {} @@ -58,12 +60,10 @@ class TrivialTest : public ::testing::Test { void CreateAddTwoTensorModel(Model* model) { OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4}); OperandType scalarType(Type::INT32, {}); - int32_t activation(ANEURALNETWORKS_FUSED_NONE); auto a = model->addOperand(&matrixType); auto b = model->addOperand(&matrixType); auto c = model->addOperand(&matrixType); - auto d = model->addOperand(&scalarType); - model->setOperandValue(d, &activation, sizeof(activation)); + auto d = model->addConstantOperand(&scalarType, kNoActivation); model->addOperation(ANEURALNETWORKS_ADD, {a, b, d}, {c}); model->identifyInputsAndOutputs({a, b}, {c}); ASSERT_TRUE(model->isValid()); @@ -75,15 +75,13 @@ void CreateAddTwoTensorModel(Model* model) { void CreateAddThreeTensorModel(Model* model, const Matrix3x4 bias) { OperandType matrixType(Type::TENSOR_FLOAT32, {3, 4}); OperandType scalarType(Type::INT32, {}); - int32_t activation(ANEURALNETWORKS_FUSED_NONE); auto a = model->addOperand(&matrixType); auto b = model->addOperand(&matrixType); auto c = model->addOperand(&matrixType); auto d = model->addOperand(&matrixType); auto e = model->addOperand(&matrixType); - auto f = model->addOperand(&scalarType); + auto f = model->addConstantOperand(&scalarType, kNoActivation); model->setOperandValue(e, bias, sizeof(Matrix3x4)); - model->setOperandValue(f, &activation, sizeof(activation)); model->addOperation(ANEURALNETWORKS_ADD, {a, c, f}, {b}); model->addOperation(ANEURALNETWORKS_ADD, {b, e, f}, {d}); model->identifyInputsAndOutputs({c, a}, {d}); @@ -154,11 +152,8 @@ TEST_F(TrivialTest, AddThree) { TEST_F(TrivialTest, BroadcastAddTwo) { Model modelBroadcastAdd2; - // activation: NONE. - int32_t activation_init[] = {ANEURALNETWORKS_FUSED_NONE}; OperandType scalarType(Type::INT32, {}); - auto activation = modelBroadcastAdd2.addOperand(&scalarType); - modelBroadcastAdd2.setOperandValue(activation, activation_init, sizeof(int32_t) * 1); + auto activation = modelBroadcastAdd2.addConstantOperand(&scalarType, kNoActivation); OperandType matrixType(Type::TENSOR_FLOAT32, {1, 1, 3, 4}); OperandType matrixType2(Type::TENSOR_FLOAT32, {4}); @@ -186,11 +181,8 @@ TEST_F(TrivialTest, BroadcastAddTwo) { TEST_F(TrivialTest, BroadcastMulTwo) { Model modelBroadcastMul2; - // activation: NONE. - int32_t activation_init[] = {ANEURALNETWORKS_FUSED_NONE}; OperandType scalarType(Type::INT32, {}); - auto activation = modelBroadcastMul2.addOperand(&scalarType); - modelBroadcastMul2.setOperandValue(activation, activation_init, sizeof(int32_t) * 1); + auto activation = modelBroadcastMul2.addConstantOperand(&scalarType, kNoActivation); OperandType matrixType(Type::TENSOR_FLOAT32, {1, 1, 3, 4}); OperandType matrixType2(Type::TENSOR_FLOAT32, {4}); |