summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLev Proleev <levp@google.com>2020-05-27 11:31:25 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2020-05-27 11:31:25 +0000
commitc3c428e678e09530bcc90fae796b0ce8a701059e (patch)
tree1bde5b262d77dde9e70c3a5f4a1bc2d777cb82df
parent66e5923200afc965bf19b880737e9180e9f5c909 (diff)
parentd517e96dba4d6528382a770d38882e4610e2ff1b (diff)
downloadml-c3c428e678e09530bcc90fae796b0ce8a701059e.tar.gz
Merge "Add shape check to CAST validation" into rvc-dev
-rw-r--r--nn/common/Utils.cpp22
-rw-r--r--nn/runtime/test/generated/spec_V1_3_cts_only/cast_mismatching_shapes.example.cpp53
-rw-r--r--nn/runtime/test/specs/V1_3_cts_only/cast_mismatching_shapes.mod.py25
3 files changed, 98 insertions, 2 deletions
diff --git a/nn/common/Utils.cpp b/nn/common/Utils.cpp
index fedc8cb30..4e4d0e06a 100644
--- a/nn/common/Utils.cpp
+++ b/nn/common/Utils.cpp
@@ -26,7 +26,10 @@
#include <sys/system_properties.h>
#include <algorithm>
+#include <functional>
+#include <iostream>
#include <limits>
+#include <numeric>
#include <set>
#include <string>
#include <tuple>
@@ -1509,8 +1512,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<OperandType> inExpectedTypes;
std::vector<OperandType> outExpectedTypes;
if ((inputType == OperandType::TENSOR_FLOAT16 ||
@@ -1536,6 +1541,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<uint32_t>& 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<int32_t>({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<int32_t>({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()