Release 18.08
[platform/upstream/armnn.git] / src / armnn / layers / MultiplicationLayer.cpp
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // See LICENSE file in the project root for full license information.
4 //
5 #include "MultiplicationLayer.hpp"
6
7 #include "LayerCloneBase.hpp"
8
9 #include <armnn/TypesUtils.hpp>
10 #include <backends/WorkloadData.hpp>
11 #include <backends/WorkloadFactory.hpp>
12
13 namespace armnn
14 {
15
16 MultiplicationLayer::MultiplicationLayer(const char* name)
17     : Layer(2, 1, LayerType::Multiplication, name)
18 {
19 }
20
21 std::unique_ptr<IWorkload> MultiplicationLayer::CreateWorkload(const Graph&            graph,
22                                                                const IWorkloadFactory& factory) const
23 {
24     MultiplicationQueueDescriptor descriptor;
25
26     return factory.CreateMultiplication(descriptor, PrepInfoAndDesc(descriptor, graph));
27 }
28
29 MultiplicationLayer* MultiplicationLayer::Clone(Graph& graph) const
30 {
31     return CloneBase<MultiplicationLayer>(graph, GetName());
32 }
33
34 std::vector<TensorShape> MultiplicationLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const
35 {
36     BOOST_ASSERT(inputShapes.size() == 2);
37     auto& input0 = inputShapes[0];
38     auto& input1 = inputShapes[1];
39
40     // Get the max of the inputs.
41     BOOST_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions());
42     unsigned int numDims = input0.GetNumDimensions();
43     std::vector<unsigned int> dims(numDims);
44
45     for (unsigned int i = 0; i < numDims; i++)
46     {
47         unsigned int dim0 = input0[i];
48         unsigned int dim1 = input1[i];
49
50     // Validates inputs are broadcast compatible.
51 #if !NDEBUG
52         if (dim0 != dim1)
53         {
54             BOOST_ASSERT_MSG(dim0 == 1 || dim1 == 1, "Dimensions should either match or one should be of size 1.");
55         }
56 #endif
57
58         dims[i] = std::max(dim0, dim1);
59     }
60
61     return std::vector<TensorShape>({ TensorShape(numDims, dims.data()) });
62 }
63
64 void MultiplicationLayer::ValidateTensorShapesFromInputs()
65 {
66     VerifyLayerConnections(2, CHECK_LOCATION());
67
68     auto inferredShapes = InferOutputShapes({
69         GetInputSlot(0).GetConnection()->GetTensorInfo().GetShape(),
70         GetInputSlot(1).GetConnection()->GetTensorInfo().GetShape()
71     });
72
73     BOOST_ASSERT(inferredShapes.size() == 1);
74
75     ConditionalThrowIfNotEqual<LayerValidationException>(
76         "MultiplicationLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.",
77         GetOutputSlot(0).GetTensorInfo().GetShape(),
78         inferredShapes[0]);
79 }
80
81 } // namespace armnn