blob: 742ba88d73c8b6efce84bad08554359eff0e8840 [file] [log] [blame]
Cathal Corbett3b9acd52022-12-09 12:17:27 +00001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include "SliceOperator.hpp"
7
8TosaSerializationBasicBlock* ConvertSliceToTosaOperator(const Layer* layer,
9 const std::vector<const TensorInfo*>& inputs,
10 const std::vector<const TensorInfo*>& outputs,
11 const SliceDescriptor* sliceDescriptor)
12{
13 std::string inputName = std::string("input0_");
14 std::string outputName = std::string("output0_");
15 std::string blockName = std::string("Op_SLICE_block_") + GetUniqueTosaMappingID();
16
17 // If a layer is present then the block will be used for execution, so input and output names need to be determined
18 // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
19 if(layer != nullptr)
20 {
21 // Get the layers connected to the input slots and determine unique layer names.
22 Layer& connectedLayer = layer->GetInputSlot(0).GetConnectedOutputSlot()->GetOwningLayer();
23 inputName = GenerateUniqueName(connectedLayer, 0);
24
25 // Get the layer connected to the output slot and determine unique layer name.
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000026 outputName = GenerateUniqueOutputName(*layer, 0);
Cathal Corbett3b9acd52022-12-09 12:17:27 +000027 }
28
29 std::vector<int32_t> begin(sliceDescriptor->m_Begin.begin(), sliceDescriptor->m_Begin.end());
30 std::vector<int32_t> size(sliceDescriptor->m_Size.begin(), sliceDescriptor->m_Size.end());
31
32 TosaSliceAttribute attribute(begin, size);
33
34 auto* op = new TosaSerializationOperator(Op_SLICE,
35 Attribute_SliceAttribute,
36 &attribute,
37 {inputName},
38 {outputName});
39
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000040 std::vector<TosaSerializationTensor*> tensors;
41
42 // Only add input tensors if connected layer is an input layer.
43 // As intermediate or constant tensors will be created separately.
44 // There also can't be duplicate tensor.
45 if(inputName.find("input0_") != std::string::npos)
46 {
47 std::vector<int32_t> inputShape = GetTosaTensorShape(inputs[0]->GetShape());
48 DType inputDType = ArmNNToDType(inputs[0]->GetDataType());
49
50 tensors.push_back(new TosaSerializationTensor(inputName, inputShape, inputDType, {}));
51 }
Cathal Corbett3b9acd52022-12-09 12:17:27 +000052
53 std::vector<int32_t> outputShape = GetTosaTensorShape(outputs[0]->GetShape());
54 DType outputDType = ArmNNToDType(outputs[0]->GetDataType());
55
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000056 tensors.push_back(new TosaSerializationTensor(outputName, outputShape, outputDType, {}));
Cathal Corbett3b9acd52022-12-09 12:17:27 +000057
58 // operatorInputNames/operatorOutputNames ends up being the same as
59 // blockInputNames/blockOutputNames for one-to-one ArmNN to TOSA mappings
60 return new TosaSerializationBasicBlock(blockName, // name
61 {op}, // operators
Matthew Sloyanda6bf9e2022-12-14 10:16:27 +000062 tensors, // tensors
Cathal Corbett3b9acd52022-12-09 12:17:27 +000063 {inputName}, // inputs
64 {outputName}); // outputs
65}