From d517e96dba4d6528382a770d38882e4610e2ff1b Mon Sep 17 00:00:00 2001 From: Lev Proleev Date: Wed, 20 May 2020 21:16:27 +0100 Subject: Add shape check to CAST validation The generated test is added only to CTS since VTS would fail on some 1.2 drivers. Fix: 156284111 Test: NeuralNetworksTest_static Change-Id: I4f3c6cdb9f546501e4ca375d7900431c384d6885 --- nn/common/Utils.cpp | 22 ++++++++- .../cast_mismatching_shapes.example.cpp | 53 ++++++++++++++++++++++ .../V1_3_cts_only/cast_mismatching_shapes.mod.py | 25 ++++++++++ 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 nn/runtime/test/generated/spec_V1_3_cts_only/cast_mismatching_shapes.example.cpp create mode 100644 nn/runtime/test/specs/V1_3_cts_only/cast_mismatching_shapes.mod.py (limited to 'nn') diff --git a/nn/common/Utils.cpp b/nn/common/Utils.cpp index 81e5cf1e1..7a66b68ef 100644 --- a/nn/common/Utils.cpp +++ b/nn/common/Utils.cpp @@ -26,7 +26,10 @@ #include #include +#include +#include #include +#include #include #include #include @@ -1508,8 +1511,10 @@ int validateOperation(ANeuralNetworksOperationType opType, uint32_t inputCount, logInvalidInOutNumber(1, 1); return ANEURALNETWORKS_BAD_DATA; } - auto inputType = operands[inputIndexes[0]].type; - auto outputType = operands[outputIndexes[0]].type; + auto inputOperand = operands[inputIndexes[0]]; + auto outputOperand = operands[outputIndexes[0]]; + auto inputType = inputOperand.type; + auto outputType = outputOperand.type; std::vector inExpectedTypes; std::vector outExpectedTypes; if ((inputType == OperandType::TENSOR_FLOAT16 || @@ -1535,6 +1540,19 @@ int validateOperation(ANeuralNetworksOperationType opType, uint32_t inputCount, LOG(ERROR) << "Unsupported data type for operation " << getOperationName(opType); return ANEURALNETWORKS_BAD_DATA; } + // Validate that output shape is equal to input shape if dimensions + // are already known. + auto getNumberOfElements = [](const hardware::hidl_vec& dims) { + if (dims.size() == 0) { + return 0; + } + return std::accumulate(dims.begin(), dims.end(), 1, std::multiplies<>()); + }; + if (inputOperand.dimensions.size() != 0 && outputOperand.dimensions.size() != 0 && + getNumberOfElements(outputOperand.dimensions) != 0 && + inputOperand.dimensions != outputOperand.dimensions) { + return ANEURALNETWORKS_BAD_DATA; + } return validateOperationOperandTypes(operands, inputCount, inputIndexes, inExpectedTypes, outputCount, outputIndexes, outExpectedTypes); diff --git a/nn/runtime/test/generated/spec_V1_3_cts_only/cast_mismatching_shapes.example.cpp b/nn/runtime/test/generated/spec_V1_3_cts_only/cast_mismatching_shapes.example.cpp new file mode 100644 index 000000000..24ba83438 --- /dev/null +++ b/nn/runtime/test/generated/spec_V1_3_cts_only/cast_mismatching_shapes.example.cpp @@ -0,0 +1,53 @@ +// Generated from cast_mismatching_shapes.mod.py +// DO NOT EDIT +// clang-format off +#include "TestHarness.h" +using namespace test_helper; + +namespace generated_tests::cast_mismatching_shapes { + +const TestModel& get_test_model() { + static TestModel model = { + .expectFailure = true, + .expectedMultinomialDistributionTolerance = 0, + .isRelaxed = false, + .main = { + .inputIndexes = {0}, + .operands = {{ // input0 + .channelQuant = {}, + .data = TestBuffer::createFromVector({1, 2, 3, 4, 5, 6}), + .dimensions = {2, 3}, + .isIgnored = false, + .lifetime = TestOperandLifeTime::SUBGRAPH_INPUT, + .numberOfConsumers = 1, + .scale = 0.0f, + .type = TestOperandType::TENSOR_INT32, + .zeroPoint = 0 + }, { // output0 + .channelQuant = {}, + .data = TestBuffer::createFromVector({1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}), + .dimensions = {100}, + .isIgnored = false, + .lifetime = TestOperandLifeTime::SUBGRAPH_OUTPUT, + .numberOfConsumers = 0, + .scale = 0.0f, + .type = TestOperandType::TENSOR_INT32, + .zeroPoint = 0 + }}, + .operations = {{ + .inputs = {0}, + .outputs = {1}, + .type = TestOperationType::CAST + }}, + .outputIndexes = {1} + }, + .minSupportedVersion = TestHalVersion::UNKNOWN, + .referenced = {} + }; + return model; +} + +const auto dummy_test_model = TestModelManager::get().add("cast_mismatching_shapes", get_test_model()); + +} // namespace generated_tests::cast_mismatching_shapes + diff --git a/nn/runtime/test/specs/V1_3_cts_only/cast_mismatching_shapes.mod.py b/nn/runtime/test/specs/V1_3_cts_only/cast_mismatching_shapes.mod.py new file mode 100644 index 000000000..c718e5cc2 --- /dev/null +++ b/nn/runtime/test/specs/V1_3_cts_only/cast_mismatching_shapes.mod.py @@ -0,0 +1,25 @@ +# +# Copyright (C) 2020 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +input0 = Input("input0", "TENSOR_INT32", "{2, 3}") +output0 = Output("output0", "TENSOR_INT32", "{100}") + +model = Model().Operation("CAST", input0).To(output0) + +example = Example({ + input0: [1, 2, 3, 4, 5, 6], + output0: [1, 2, 3, 4, 5, 6] + [0] * 94, +}, model=model).ExpectFailure() -- cgit v1.2.3