diff options
author | Xusong Wang <xusongw@google.com> | 2019-08-05 15:36:24 -0700 |
---|---|---|
committer | Xusong Wang <xusongw@google.com> | 2019-08-21 17:49:36 -0700 |
commit | 988144e0d88cc5766e50e35fb1d5eb249675ebe3 (patch) | |
tree | fa389340159dcb843790f309468c1e68dee30dd7 /nn/runtime/test/TestGenerated.cpp | |
parent | fab95c0a95b6e01c53a0cd583df90e5af9545d70 (diff) | |
download | ml-988144e0d88cc5766e50e35fb1d5eb249675ebe3.tar.gz |
Fix issues with output shapes.
This CL fixes two issues related to output shapes:
Dynamic output shape tests
- Before: Dynamic output shape tests have output dimensions all set to
zero in the TestModel struct.
- After: The TestModel will always have fully-specified dimensions. The
converter function createModel will assign zero dimensions to
output operands according to a flag testDynamicOutputShape.
Zero-sized outputs
- Before: Zero-sized outputs are assigned dummy values in test generator
to generate a non-zero buffer.
- After: Zero-sized outputs have empty buffer in test generator and
TestModel struct. The converter function createRequest will
allocate non-zero buffer.
Bug: 120601396
Test: NNT_static
Test: All VTS
Test: CTS
Change-Id: I8503ed4c439e9c490aef105ac01a662b4d6430d8
Diffstat (limited to 'nn/runtime/test/TestGenerated.cpp')
-rw-r--r-- | nn/runtime/test/TestGenerated.cpp | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/nn/runtime/test/TestGenerated.cpp b/nn/runtime/test/TestGenerated.cpp index f62dd904d..5a19fe827 100644 --- a/nn/runtime/test/TestGenerated.cpp +++ b/nn/runtime/test/TestGenerated.cpp @@ -15,12 +15,12 @@ */ #include "TestGenerated.h" -#include "TestHarness.h" - -#include <gtest/gtest.h> #include <ftw.h> +#include <gtest/gtest.h> #include <unistd.h> + +#include <algorithm> #include <cassert> #include <cmath> #include <fstream> @@ -29,6 +29,8 @@ #include <thread> #include <vector> +#include "TestHarness.h" + // Systrace is not available from CTS tests due to platform layering // constraints. We reuse the NNTEST_ONLY_PUBLIC_API flag, as that should also be // the case for CTS (public APIs only). @@ -44,20 +46,24 @@ namespace generated_tests { using namespace android::nn::test_wrapper; using namespace test_helper; -static OperandType getOperandType(const TestOperand& op) { +static OperandType getOperandType(const TestOperand& op, bool testDynamicOutputShape) { + auto dims = op.dimensions; + if (testDynamicOutputShape && op.lifetime == TestOperandLifeTime::MODEL_OUTPUT) { + dims.assign(dims.size(), 0); + } if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) { return OperandType( - static_cast<Type>(op.type), op.dimensions, + static_cast<Type>(op.type), dims, SymmPerChannelQuantParams(op.channelQuant.scales, op.channelQuant.channelDim)); } else { - return OperandType(static_cast<Type>(op.type), op.dimensions, op.scale, op.zeroPoint); + return OperandType(static_cast<Type>(op.type), dims, op.scale, op.zeroPoint); } } -void createModel(const TestModel& testModel, Model* model) { +void createModel(const TestModel& testModel, bool testDynamicOutputShape, Model* model) { // Operands. for (const auto& operand : testModel.operands) { - auto type = getOperandType(operand); + auto type = getOperandType(operand, testDynamicOutputShape); auto index = model->addOperand(&type); switch (operand.lifetime) { @@ -103,9 +109,18 @@ static void createRequest(const TestModel& testModel, Execution* execution, // Model outputs. for (uint32_t i = 0; i < testModel.outputIndexes.size(); i++) { const auto& operand = testModel.operands[testModel.outputIndexes[i]]; - outputs->emplace_back(operand.data.size()); + + // In the case of zero-sized output, we should at least provide a one-byte buffer. + // This is because zero-sized tensors are only supported internally to the runtime, or + // reported in output shapes. It is illegal for the client to pre-specify a zero-sized + // tensor as model output. Otherwise, we will have two semantic conflicts: + // - "Zero dimension" conflicts with "unspecified dimension". + // - "Omitted operand buffer" conflicts with "zero-sized operand buffer". + const size_t bufferSize = std::max<size_t>(operand.data.size(), 1); + + outputs->emplace_back(bufferSize); ASSERT_EQ(Result::NO_ERROR, - execution->setOutput(i, outputs->back().getMutable<void>(), operand.data.size())); + execution->setOutput(i, outputs->back().getMutable<void>(), bufferSize)); } } @@ -211,7 +226,7 @@ void GeneratedTests::executeMultithreadedSharedCompilation(const Model* model, void GeneratedTests::execute(const TestModel& testModel) { NNTRACE_APP(NNTRACE_PHASE_OVERALL, "execute"); Model model; - createModel(testModel, &model); + createModel(testModel, mTestDynamicOutputShape, &model); model.finish(); auto executeInternal = [&testModel, &model, this]() { SCOPED_TRACE("TestCompilationCaching = " + std::to_string(mTestCompilationCaching)); |