From 87d0bda9b49d9df4455f1887027e5ead2527c27e Mon Sep 17 00:00:00 2001 From: Finn Williams Date: Fri, 3 Jul 2020 10:12:03 +0100 Subject: IVGCVSW-4929 Implement ShapeInferenceMethod in all Layers Signed-off-by: Finn Williams Change-Id: I2c2d99f97cf89814140b057a9f93f41b364197f5 --- src/armnn/Layer.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'src/armnn/Layer.cpp') diff --git a/src/armnn/Layer.cpp b/src/armnn/Layer.cpp index 12ab035079..692ee32acd 100644 --- a/src/armnn/Layer.cpp +++ b/src/armnn/Layer.cpp @@ -396,6 +396,69 @@ std::vector Layer::InferOutputShapes(const std::vector return inputShapes; } +void Layer::ValidateAndCopyShape(const TensorShape& outputShape, + const TensorShape& inferredShape, + const ShapeInferenceMethod shapeInferenceMethod, + const std::string& layerName, + const unsigned int outputSlotIndex) +{ + if (shapeInferenceMethod == ShapeInferenceMethod::ValidateOnly) + { + ConditionalThrowIfNotEqual( + layerName + ": TensorShape set on OutputSlot[0] does not match the inferred shape.", + outputShape, + inferredShape); + return; + } + + if (outputShape.GetDimensionality() == Dimensionality::Specified) + { + for (unsigned int i = 0; i < outputShape.GetNumDimensions(); ++i) + { + if (outputShape.GetDimensionSpecificity(i) && outputShape[i] != inferredShape[i]) + { + std::stringstream ss; + ss << layerName << ": TensorShape set on OutputSlot[" << outputSlotIndex << + "] does not match the inferred shape at dimension index ["; + ss << i << "] " << outputShape << " != " << inferredShape; + throw LayerValidationException(ss.str()); + } + } + } + + TensorInfo info = GetOutputSlot(outputSlotIndex).GetTensorInfo(); + + armnn::TensorInfo inferredTensorInfo(inferredShape, + info.GetDataType(), + info.GetQuantizationScale(), + info.GetQuantizationOffset()); + + GetOutputSlot(outputSlotIndex).SetTensorInfo(inferredTensorInfo); +} + +void Layer::VerifyShapeInferenceType(const TensorShape& outputShape, ShapeInferenceMethod shapeInferenceMethod) +{ + if (shapeInferenceMethod == ShapeInferenceMethod::ValidateOnly) + { + ConditionalThrow( + outputShape.GetDimensionality() != Dimensionality::NotSpecified, + "Dimensionality can not be NotSpecified while using ShapeInferenceMethod::ValidateOnly"); + + ConditionalThrow( + outputShape.AreAllDimensionsSpecified(), + "Unspecified dimension while using ShapeInferenceMethod::ValidateOnly"); + } + else + { + if (outputShape.GetDimensionality() == Dimensionality::Specified) + { + ConditionalThrow( + !outputShape.AreAllDimensionsSpecified(), + "No unspecified dimension while using ShapeInferenceMethod::InferAndValidate"); + } + } +} + void Layer::SerializeLayerParameters(ParameterStringifyFunction& fn) const { std::string layerType = GetLayerTypeAsCString(m_Type); -- cgit v1.2.1