diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2020-04-07 21:55:36 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-04-07 21:55:36 +0000 |
commit | 25e6b590c9d10f8b81721f612b0684a1bbec5056 (patch) | |
tree | 195a3792ab9909f354d689670a1e8832a2e5040e /nn/runtime/test | |
parent | 891e292510e4b6b85dc9dc75e98097300703f916 (diff) | |
parent | d422e9be0842125dd565e15ec1cc3c656bf5f65a (diff) | |
download | ml-25e6b590c9d10f8b81721f612b0684a1bbec5056.tar.gz |
Merge "Handle CONSTANT_REFERENCEs in generated CTS tests" into rvc-dev
Diffstat (limited to 'nn/runtime/test')
7 files changed, 90 insertions, 10 deletions
diff --git a/nn/runtime/test/GeneratedTestUtils.cpp b/nn/runtime/test/GeneratedTestUtils.cpp index b5cd92af4..abbf79a2b 100644 --- a/nn/runtime/test/GeneratedTestUtils.cpp +++ b/nn/runtime/test/GeneratedTestUtils.cpp @@ -46,19 +46,82 @@ static OperandType getOperandType(const TestOperand& op, bool testDynamicOutputS } } +// A Memory object that owns AHardwareBuffer +class MemoryAHWB : public Memory { + public: + static std::unique_ptr<MemoryAHWB> create(uint32_t size) { + const uint64_t usage = + AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN; + AHardwareBuffer_Desc desc = { + .width = size, + .height = 1, + .layers = 1, + .format = AHARDWAREBUFFER_FORMAT_BLOB, + .usage = usage, + }; + AHardwareBuffer* ahwb = nullptr; + EXPECT_EQ(AHardwareBuffer_allocate(&desc, &ahwb), 0); + EXPECT_NE(ahwb, nullptr); + + void* buffer = nullptr; + EXPECT_EQ(AHardwareBuffer_lock(ahwb, usage, -1, nullptr, &buffer), 0); + EXPECT_NE(buffer, nullptr); + + return std::unique_ptr<MemoryAHWB>(new MemoryAHWB(ahwb, buffer)); + } + + ~MemoryAHWB() override { + EXPECT_EQ(AHardwareBuffer_unlock(mAhwb, nullptr), 0); + AHardwareBuffer_release(mAhwb); + } + + void* getPointer() const { return mBuffer; } + + private: + MemoryAHWB(AHardwareBuffer* ahwb, void* buffer) : Memory(ahwb), mAhwb(ahwb), mBuffer(buffer) {} + + AHardwareBuffer* mAhwb; + void* mBuffer; +}; + +static std::unique_ptr<MemoryAHWB> createConstantReferenceMemory(const TestModel& testModel) { + uint32_t size = 0; + + auto processSubgraph = [&size](const TestSubgraph& subgraph) { + for (const TestOperand& operand : subgraph.operands) { + if (operand.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) { + size += operand.data.alignedSize(); + } + } + }; + + processSubgraph(testModel.main); + for (const TestSubgraph& subgraph : testModel.referenced) { + processSubgraph(subgraph); + } + return size == 0 ? nullptr : MemoryAHWB::create(size); +} + static void createModelFromSubgraph(const TestSubgraph& subgraph, bool testDynamicOutputShape, - const std::vector<TestSubgraph>& refSubgraphs, Model* model, - Model* refModels) { + const std::vector<TestSubgraph>& refSubgraphs, + const std::unique_ptr<MemoryAHWB>& memory, + uint32_t* memoryOffset, Model* model, Model* refModels) { // Operands. for (const auto& operand : subgraph.operands) { auto type = getOperandType(operand, testDynamicOutputShape); auto index = model->addOperand(&type); switch (operand.lifetime) { - case TestOperandLifeTime::CONSTANT_COPY: - case TestOperandLifeTime::CONSTANT_REFERENCE: { + case TestOperandLifeTime::CONSTANT_COPY: { model->setOperandValue(index, operand.data.get<void>(), operand.data.size()); } break; + case TestOperandLifeTime::CONSTANT_REFERENCE: { + const uint32_t length = operand.data.size(); + std::memcpy(static_cast<uint8_t*>(memory->getPointer()) + *memoryOffset, + operand.data.get<void>(), length); + model->setOperandValueFromMemory(index, memory.get(), *memoryOffset, length); + *memoryOffset += operand.data.alignedSize(); + } break; case TestOperandLifeTime::NO_VALUE: { model->setOperandValue(index, nullptr, 0); } break; @@ -69,7 +132,7 @@ static void createModelFromSubgraph(const TestSubgraph& subgraph, bool testDynam Model* refModel = &refModels[refIndex]; if (!refModel->isFinished()) { createModelFromSubgraph(refSubgraph, testDynamicOutputShape, refSubgraphs, - refModel, refModels); + memory, memoryOffset, refModel, refModels); ASSERT_EQ(refModel->finish(), Result::NO_ERROR); ASSERT_TRUE(refModel->isValid()); } @@ -95,10 +158,13 @@ static void createModelFromSubgraph(const TestSubgraph& subgraph, bool testDynam void createModel(const TestModel& testModel, bool testDynamicOutputShape, GeneratedModel* model) { ASSERT_NE(nullptr, model); + std::unique_ptr<MemoryAHWB> memory = createConstantReferenceMemory(testModel); + uint32_t memoryOffset = 0; std::vector<Model> refModels(testModel.referenced.size()); - createModelFromSubgraph(testModel.main, testDynamicOutputShape, testModel.referenced, model, - refModels.data()); + createModelFromSubgraph(testModel.main, testDynamicOutputShape, testModel.referenced, memory, + &memoryOffset, model, refModels.data()); model->setRefModels(std::move(refModels)); + model->setConstantReferenceMemory(std::move(memory)); // Relaxed computation. model->relaxComputationFloat32toFloat16(testModel.isRelaxed); diff --git a/nn/runtime/test/GeneratedTestUtils.h b/nn/runtime/test/GeneratedTestUtils.h index de0ce3f82..3ac5cba03 100644 --- a/nn/runtime/test/GeneratedTestUtils.h +++ b/nn/runtime/test/GeneratedTestUtils.h @@ -19,6 +19,7 @@ #include <gtest/gtest.h> +#include <memory> #include <utility> #include <vector> @@ -56,8 +57,14 @@ class GeneratedModel : public test_wrapper::Model { mRefModels = std::move(refModels); } + // A helper method to simplify CONSTANT_REFERENCE memory lifetime management. + void setConstantReferenceMemory(std::unique_ptr<test_wrapper::Memory> memory) { + mConstantReferenceMemory = std::move(memory); + } + private: std::vector<test_wrapper::Model> mRefModels; + std::unique_ptr<test_wrapper::Memory> mConstantReferenceMemory; }; // Convert TestModel to NDK model. diff --git a/nn/runtime/test/TestNeuralNetworksWrapper.h b/nn/runtime/test/TestNeuralNetworksWrapper.h index 6ac33dfee..6df16e217 100644 --- a/nn/runtime/test/TestNeuralNetworksWrapper.h +++ b/nn/runtime/test/TestNeuralNetworksWrapper.h @@ -62,7 +62,7 @@ class Memory { ANEURALNETWORKS_NO_ERROR; } - ~Memory() { ANeuralNetworksMemory_free(mMemory); } + virtual ~Memory() { ANeuralNetworksMemory_free(mMemory); } // Disallow copy semantics to ensure the runtime object can only be freed // once. Copy semantics could be enabled if some sort of reference counting diff --git a/nn/runtime/test/generated/spec_V1_0/mobilenet_224_gender_basic_fixed.example.cpp b/nn/runtime/test/generated/spec_V1_0/mobilenet_224_gender_basic_fixed.example.cpp index 652232188..a0d61a425 100644 --- a/nn/runtime/test/generated/spec_V1_0/mobilenet_224_gender_basic_fixed.example.cpp +++ b/nn/runtime/test/generated/spec_V1_0/mobilenet_224_gender_basic_fixed.example.cpp @@ -2317,7 +2317,7 @@ const TestModel& get_test_model() { }}, .outputIndexes = {217} }, - .minSupportedVersion = TestHalVersion::V1_0, + .minSupportedVersion = TestHalVersion::V1_2, .referenced = {} }; return model; diff --git a/nn/runtime/test/generated/spec_V1_0/mobilenet_quantized.example.cpp b/nn/runtime/test/generated/spec_V1_0/mobilenet_quantized.example.cpp index 0e3b013b9..4f085169a 100644 --- a/nn/runtime/test/generated/spec_V1_0/mobilenet_quantized.example.cpp +++ b/nn/runtime/test/generated/spec_V1_0/mobilenet_quantized.example.cpp @@ -2351,7 +2351,7 @@ const TestModel& get_test_model() { }}, .outputIndexes = {220} }, - .minSupportedVersion = TestHalVersion::V1_0, + .minSupportedVersion = TestHalVersion::V1_2, .referenced = {} }; return model; diff --git a/nn/runtime/test/specs/V1_0/mobilenet_224_gender_basic_fixed.mod.py b/nn/runtime/test/specs/V1_0/mobilenet_224_gender_basic_fixed.mod.py index e3d66798b..e4601af1a 100644 --- a/nn/runtime/test/specs/V1_0/mobilenet_224_gender_basic_fixed.mod.py +++ b/nn/runtime/test/specs/V1_0/mobilenet_224_gender_basic_fixed.mod.py @@ -257,3 +257,6 @@ output0 = {i85: [0.605085, 0.0733608, 0.0200205, 0.114568, 0.178815, 0.0580576, Example((input0, output0)) +# Disable the V1_0 compliance check because hardware_buffer_blob is not +# supported below V1_2. +Example.SetVersion("V1_2", "mobilenet_224_gender_basic_fixed") diff --git a/nn/runtime/test/specs/V1_0/mobilenet_quantized.mod.py b/nn/runtime/test/specs/V1_0/mobilenet_quantized.mod.py index 20223e292..7c7f2efd3 100644 --- a/nn/runtime/test/specs/V1_0/mobilenet_quantized.mod.py +++ b/nn/runtime/test/specs/V1_0/mobilenet_quantized.mod.py @@ -259,3 +259,7 @@ output0 = {i85: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, # Instantiate an example Example((input0, output0)) + +# Disable the V1_0 compliance check because hardware_buffer_blob is not +# supported below V1_2. +Example.SetVersion("V1_2", "mobilenet_quantized") |