blob: 5728ff32031a8cf36054e97487f969905a419524 [file] [log] [blame]
Cathal Corbett9c9d5b92022-08-17 17:30:16 +01001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <Layer.hpp>
9
10#include <tosa_serialization_handler.h>
11#include "operatorMappings/AdditionOperator.hpp"
12
13using namespace armnn;
14using namespace tosa;
15
16// From the input armnn::Layer, set the corresponding data field in the
17// tosa::TosaSerializationTensor where constant tensor data exists in the armnn::Layer.
18void SetBasicBlockConstantTensorData(Layer* layer, TosaSerializationBasicBlock* /*basicBlock*/)
19{
20 switch (layer->GetType())
21 {
22 case LayerType::Convolution2d:
23 {
24 // ToDo: using Convolution2d as an example as it has constant tensors for weights and bias.
25 // ToDo: manually set TosaOperator data of basicBlock where constant tensors exist.
26 }
27 default:
28 // If no switch statement for layer, no constant tensors exist in that layer, return
29 return;
30 }
31}
32
33// Populates a tosa::TosaSerializationBasicBlock from constructing
34// tosa::TosaSerializationOperator(s) and tosa::TosaSerializationTensor(s)
35// based on the input armnn::LayerType and associated armnn::TensorInfos and armnn::Descriptor.
36//
37// If an armnn::LayerType does not have a tosa mapping or the mapping is not implemented in ArmNN,
38// an empty tosa::TosaSerializationBasicBlock() is returned with operator tosa::Op_UNKNOWN.
39TosaSerializationBasicBlock* GetTosaMapping(const LayerType type,
40 const std::vector<const TensorInfo*>& inputs,
41 const std::vector<const TensorInfo*>& outputs,
42 const BaseDescriptor& /*descriptor*/)
43{
44 switch (type)
45 {
46 case LayerType::Addition:
47 {
48 return ConvertAdditionToTosaOperator(inputs, outputs);
49 }
50 default:
51 {
52 // empty basic block when no tosa mapping implemented/exists
53 TosaSerializationOperator* op = new TosaSerializationOperator(Op_UNKNOWN, Attribute_NONE, nullptr, {}, {});
54 return new TosaSerializationBasicBlock("", {op}, {}, {}, {});
55 }
56 }
57}
58
59// Function called in armnn::OptimizeSubgraphView() when access to armnn::Layer is available
60// and there is an option to set tosa basic block data from constant layer tenors available from the input layer.
61TosaSerializationBasicBlock* GetTosaMappingFromLayer(Layer* layer)
62{
63 std::vector<const TensorInfo*> inputs;
64 for (auto inputSlot : layer->GetInputSlots())
65 {
66 inputs.push_back(&inputSlot.GetConnection()->GetTensorInfo());
67 }
68
69 std::vector<const TensorInfo*> outputs;
70 for (auto& outputSlot : layer->GetOutputSlots())
71 {
72 outputs.push_back(&outputSlot.GetTensorInfo());
73 }
74
75 TosaSerializationBasicBlock* basicBlock = GetTosaMapping(layer->GetType(),
76 inputs,
77 outputs,
78 layer->GetParameters());
79 SetBasicBlockConstantTensorData(layer, basicBlock);
80 return basicBlock;
81}