surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 1 | // |
Finn Williams | 87d0bda | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 4 | // |
| 5 | #include "ConstantLayer.hpp" |
| 6 | #include "LayerCloneBase.hpp" |
| 7 | |
| 8 | #include <armnn/TypesUtils.hpp> |
Colm Donelan | 0c47974 | 2021-12-10 12:43:54 +0000 | [diff] [blame] | 9 | #include <armnn/backends/TensorHandle.hpp> |
| 10 | #include <armnn/backends/WorkloadData.hpp> |
| 11 | #include <armnn/backends/WorkloadFactory.hpp> |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 12 | |
| 13 | namespace armnn |
| 14 | { |
| 15 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | ConstantLayer::ConstantLayer(const char* name) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 17 | : Layer(0, 1, LayerType::Constant, name) |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 18 | { |
| 19 | } |
| 20 | |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 21 | std::unique_ptr<IWorkload> ConstantLayer::CreateWorkload(const IWorkloadFactory& factory) const |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 22 | { |
| 23 | ConstantQueueDescriptor descriptor; |
| 24 | descriptor.m_LayerOutput = m_LayerOutput.get(); |
Keith Davis | df04d23 | 2020-10-23 17:20:05 +0100 | [diff] [blame] | 25 | SetAdditionalInfo(descriptor); |
| 26 | |
Teresa Charlin | 611c7fb | 2022-01-07 09:47:29 +0000 | [diff] [blame] | 27 | return factory.CreateWorkload(LayerType::Constant, descriptor, PrepInfoAndDesc(descriptor)); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | ConstantLayer* ConstantLayer::Clone(Graph& graph) const |
| 31 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | // Cloned layers share the same layer output object. |
| 33 | auto layer = CloneBase<ConstantLayer>(graph, GetName()); |
| 34 | |
Finn Williams | 4422cec | 2021-03-22 17:51:06 +0000 | [diff] [blame] | 35 | layer->m_LayerOutput = m_LayerOutput ? m_LayerOutput : nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 36 | |
| 37 | return std::move(layer); |
| 38 | } |
| 39 | |
| 40 | std::vector<TensorShape> ConstantLayer::InferOutputShapes(const std::vector<TensorShape>& inputShapes) const |
| 41 | { |
Derek Lamberti | 94a88d2 | 2019-12-10 21:12:59 +0000 | [diff] [blame] | 42 | return std::vector<TensorShape>({ inputShapes[0] }); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 43 | } |
| 44 | |
Finn Williams | f24effa | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 45 | void ConstantLayer::ValidateTensorShapesFromInputs() |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 46 | { |
Teresa Charlin | cdc0149 | 2020-06-09 18:00:20 +0100 | [diff] [blame] | 47 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | // Get the output shape from the value of the constant layer. |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 49 | TensorShape const& outShape = m_LayerOutput->GetTensorInfo().GetShape(); |
Finn Williams | 87d0bda | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 50 | |
| 51 | ConditionalThrow<LayerValidationException>( |
| 52 | outShape.GetDimensionality() != Dimensionality::NotSpecified, |
| 53 | "Constant layer m_LayerOutput output shape can not be Dimensionality::NotSpecified"); |
| 54 | |
| 55 | ConditionalThrow<LayerValidationException>( |
| 56 | outShape.AreAllDimensionsSpecified(), |
| 57 | "Constant layer m_LayerOutput output shape can not have an unspecified dimension"); |
| 58 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 59 | ConditionalThrowIfNotEqual<LayerValidationException>( |
Finn Williams | 87d0bda | 2020-07-03 10:12:03 +0100 | [diff] [blame] | 60 | "ConstantLayer: TensorShape set on OutputSlot[0] does not match the inferred shape.", |
| 61 | GetOutputSlot(0).GetTensorInfo().GetShape(), |
| 62 | outShape); |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 63 | } |
| 64 | |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 65 | void ConstantLayer::ExecuteStrategy(IStrategy& strategy) const |
| 66 | { |
Francis Murtagh | 4af5616 | 2021-04-20 16:37:55 +0100 | [diff] [blame] | 67 | ManagedConstTensorHandle managedLayerOutput(m_LayerOutput); |
| 68 | ConstTensor layerOutputTensor(managedLayerOutput.GetTensorInfo(), managedLayerOutput.Map()); |
| 69 | strategy.ExecuteStrategy(this, BaseDescriptor(), { layerOutputTensor }, GetName()); |
Finn Williams | b454c5c | 2021-02-09 15:56:23 +0000 | [diff] [blame] | 70 | } |
| 71 | |
surmeh01 | 3537c2c | 2018-05-18 16:31:43 +0100 | [diff] [blame] | 72 | } // namespace armnn |