16#include "common/include/ProfilingGuid.hpp"
18#include <tosa_serialization_handler.h>
85 std::vector<int32_t> returnShape;
88 returnShape.push_back(
static_cast<int32_t
>(shape[i]));
94static std::string GenerateUniqueName(
const Layer& layer, uint32_t layerSlot)
96 std::string guid = std::to_string(layer.
GetGuid());
97 std::string slotAndGuid = std::to_string(layerSlot) +
"_" + guid;
102 return "input_" + guid;
104 return "output" + slotAndGuid;
106 return "constant_" + guid;
108 return "intermediate" + slotAndGuid;
120 return GenerateUniqueName(connectedLayer, connectedInputSlotIdx);
126 bool multipleParents =
false;
129 multipleParents = layer.
GetInputSlots()[0].GetConnectedOutputSlot()->GetOwningLayerGuid() !=
130 layer.
GetInputSlots()[1].GetConnectedOutputSlot()->GetOwningLayerGuid();
133 return multipleParents;
145 return GenerateUniqueName(connectedLayer, layerSlot);
149 return GenerateUniqueName(layer, layerSlot);
166 return "DType_UNKNOWN";
170 return "DType_UINT8";
176 return "DType_INT16";
178 return "DType_INT32";
180 return "DType_INT48";
184 return "DType_UINT16";
190 return "DType_SHAPE";
203 return "Op_AVG_POOL2D";
205 return "Op_MAX_POOL2D";
216 case Op_DEPTHWISE_CONV2D:
217 return "Op_DEPTHWISE_CONV2D";
218 case Op_FULLY_CONNECTED:
219 return "Op_FULLY_CONNECTED";
222 case Op_TRANSPOSE_CONV2D:
223 return "Op_TRANSPOSE_CONV2D";
227 return "Op_RESERVED";
232 case Op_ARITHMETIC_RIGHT_SHIFT:
233 return "Op_ARITHMETIC_RIGHT_SHIFT";
235 return "Op_BITWISE_AND";
237 return "Op_BITWISE_OR";
239 return "Op_BITWISE_XOR";
243 return "Op_LOGICAL_AND";
244 case Op_LOGICAL_LEFT_SHIFT:
245 return "Op_LOGICAL_LEFT_SHIFT";
246 case Op_LOGICAL_RIGHT_SHIFT:
247 return "Op_LOGICAL_RIGHT_SHIFT";
249 return "Op_LOGICAL_OR";
251 return "Op_LOGICAL_XOR";
267 return "Op_BITWISE_NOT";
279 return "Op_LOGICAL_NOT";
283 return "Op_RECIPROCAL";
292 case Op_GREATER_EQUAL:
293 return "Op_GREATER_EQUAL";
295 return "Op_REDUCE_ANY";
297 return "Op_REDUCE_ALL";
299 return "Op_REDUCE_MAX";
301 return "Op_REDUCE_MIN";
302 case Op_REDUCE_PRODUCT:
303 return "Op_REDUCE_PRODUCT";
305 return "Op_REDUCE_SUM";
317 return "Op_TRANSPOSE";
331 return "Op_IDENTITY";
337 return "Op_WHILE_LOOP";
352 tosa_err_t
error = tosa_err_t::TOSA_OK;
353 std::vector<uint8_t> uint8Data;
354 auto tensorInfo = tensorHandle->GetTensorInfo();
356 switch (tensorInfo.GetDataType())
360 std::vector<float> data(tensorInfo.GetNumElements());
361 memcpy(data.data(), tensorHandle->Map(
true), tensorInfo.GetNumBytes());
363 error = TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
368 std::vector<float> data(tensorInfo.GetNumElements());
369 memcpy(data.data(), tensorHandle->Map(
true), tensorInfo.GetNumBytes());
371 error = TosaSerializationHandler::ConvertF16toU8(data, uint8Data);
377 std::vector<int8_t> data(tensorInfo.GetNumElements());
378 memcpy(data.data(), tensorHandle->Map(
true), tensorInfo.GetNumBytes());
380 error = TosaSerializationHandler::ConvertI8toU8(data, uint8Data);
385 memcpy(uint8Data.data(), tensorHandle->Map(
true), tensorInfo.GetNumBytes());
390 std::vector<int16_t> data(tensorInfo.GetNumElements());
391 memcpy(data.data(), tensorHandle->Map(
true), tensorInfo.GetNumBytes());
393 error = TosaSerializationHandler::ConvertI16toU8(data, uint8Data);
398 std::vector<int32_t> data(tensorInfo.GetNumElements());
399 memcpy(data.data(), tensorHandle->Map(
true), tensorInfo.GetNumBytes());
401 error = TosaSerializationHandler::ConvertI32toU8(data, uint8Data);
406 throw armnn::Exception(
"SetConstantTensorData: An unsupported data type was encountered.");
410 if(
error != tosa_err_t::TOSA_OK)
412 throw armnn::Exception(
"SetConstantTensorData: An error occurred when converting constant data");
415 tensorHandle->Unmap();
421 const std::vector<int32_t>& shape)
423 std::vector<uint8_t> uint8Data;
424 tosa_err_t
error = tosa_err_t::TOSA_OK;
426 unsigned int numElements = 1;
431 throw armnn::Exception(
"CreateConstTosaData: negative shape elements unhandled.");
433 numElements = numElements *
static_cast<unsigned int>(s);
438 case DType::DType_FP32:
440 std::vector<float> data(numElements, *
static_cast<const float*
>(value));
441 error = TosaSerializationHandler::ConvertF32toU8(data, uint8Data);
444 case DType::DType_FP16:
446 std::vector<float> data(numElements, *
static_cast<const float*
>(value));
447 error = TosaSerializationHandler::ConvertF16toU8(data, uint8Data);
450 case DType::DType_INT48:
452 std::vector<int64_t> data(numElements, *
static_cast<const int64_t*
>(value));
453 error = TosaSerializationHandler::ConvertI48toU8(data, uint8Data);
456 case DType::DType_INT32:
458 std::vector<int32_t> data(numElements, *
static_cast<const int32_t*
>(value));
459 error = TosaSerializationHandler::ConvertI32toU8(data, uint8Data);
462 case DType::DType_INT16:
464 std::vector<int16_t> data(numElements, *
static_cast<const int16_t*
>(value));
465 error = TosaSerializationHandler::ConvertI16toU8(data, uint8Data);
468 case DType::DType_INT8:
470 std::vector<int8_t> data(numElements, *
static_cast<const int8_t*
>(value));
471 error = TosaSerializationHandler::ConvertI8toU8(data, uint8Data);
474 case DType::DType_UINT8:
476 const int8_t* copy_data =
static_cast<const int8_t*
>(value);
477 uint8Data.assign(copy_data, copy_data + numElements);
480 case DType::DType_INT4:
482 std::vector<int8_t> data(numElements, *
static_cast<const int8_t*
>(value));
483 error = TosaSerializationHandler::ConvertI4toU8(data, uint8Data);
486 case DType::DType_BOOL:
488 std::vector<bool> data(numElements, *
static_cast<const bool*
>(value));
489 error = TosaSerializationHandler::ConvertBooltoU8(data, uint8Data);
494 throw armnn::Exception(
"CreateConstTosaData: An unsupported data type was encountered.");
498 if(
error != tosa_err_t::TOSA_OK)
500 throw armnn::Exception(
"CreateConstTosaData: An error occurred when converting constant data");
510 const std::vector<int32_t>& shape,
511 TosaSerializationOperator*& op,
512 TosaSerializationTensor*& tensor)
514 if (outputName.find(
"constant") == std::string::npos)
516 throw armnn::Exception(std::string(
"CreateConstTosaOperator: outputName must contain the string 'constant'"));
519 std::vector<uint8_t> uint8Data =
CreateConstTosaData(
static_cast<const void *
>(&value), dtype, shape);
521 op =
new TosaSerializationOperator(Op_CONST, Attribute_NONE,
nullptr, {}, {outputName});
524 tensor =
new TosaSerializationTensor(outputName, shape, dtype, uint8Data);
530 bool type_unsigned =
false;
535 type_unsigned =
true;
538 type_unsigned =
false;
541 return type_unsigned;
572 return spatialDim + 1;
575 return spatialDim + 2;
577 return spatialDim + 3;
579 throw Exception(
"GetTensorSpatialDimIndex Unknown format");
596 throw Exception(
"GetTensorFeatureDimIndex Unknown format");
603 const std::string& inputName,
605 const DType datatype,
606 const std::vector<int32_t>& kernel,
607 const std::vector<int32_t>& pad,
608 const std::vector<int32_t>& stride,
609 const std::vector<int32_t>& dilations,
610 std::vector<TosaSerializationTensor*>& tensors,
611 std::vector<TosaSerializationOperator*>& operators,
612 const bool isPoolingOp =
false)
616 std::vector<int32_t> outputSizeRemainder;
617 for (
int spatialDim = 0; spatialDim < spatialDims; spatialDim++)
619 const size_t spatialDimSize_t =
static_cast<size_t>(spatialDim);
621 const int32_t kernelVal = isPoolingOp ? kernel[spatialDimSize_t] : kernel[spatialDimIndexSize_t];
623 const int32_t inSize = inputShape[spatialDimIndexSize_t];
624 const int32_t fullPad = pad[2 * spatialDimSize_t + 0] + pad[2 * spatialDimSize_t + 1];
625 const int32_t fullSize = inSize - 1 + fullPad - (kernelVal - 1) * dilations[spatialDimSize_t];
626 outputSizeRemainder.push_back(fullSize % stride[spatialDimSize_t]);
629 const bool needSlicing = std::any_of(
630 outputSizeRemainder.begin(), outputSizeRemainder.end(), [](int64_t v) { return v > 0; });
631 const bool zeroPads = std::all_of(pad.begin(), pad.end(), [](
int v) { return v == 0; });
633 std::string sliceOutputName = inputName;
634 if (needSlicing && zeroPads)
637 std::vector<int32_t> start(inputShape.size(), 0);
638 std::vector<int32_t> size = inputShape;
639 for (
int spatialDim = 0; spatialDim < spatialDims; spatialDim++)
642 size[
static_cast<size_t>(index)] -= outputSizeRemainder[
static_cast<size_t>(spatialDim)];
645 TosaSliceAttribute attribute(start, size);
647 operators.push_back(
new TosaSerializationOperator(Op_SLICE,
648 Attribute_SliceAttribute,
652 tensors.push_back(
new TosaSerializationTensor(sliceOutputName, size, datatype, {}));
654 return sliceOutputName;
#define ARMNN_THROW_MSG_IF_FALSE(_cond, _except, _str)
int GetTensorSpatialDims(int numDims, DataLayout format)
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
const std::string mainName
std::vector< uint8_t > ConvertConstantTensorDataToBuffer(const std::shared_ptr< ConstTensorHandle > &tensorHandle)
std::string TosaOpToString(Op tosaOp)
bool IsUnsignedDataType(DType type)
DType ArmNNToDType(const DataType &type)
std::vector< uint8_t > CreateConstTosaData(const void *value, DType dtype, const std::vector< int32_t > &shape)
bool WeightFromDifferentLayer(const Layer &layer)
int GetTensorSpatialDimIndex(DataLayout format, int spatialDim)
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
std::string TosaDTypeToString(DType tosaDType)
std::string GetInputSlicedToItsUsedSize(const std::vector< int32_t > &inputShape, const std::string &inputName, const DataLayout layout, const DType datatype, const std::vector< int32_t > &kernel, const std::vector< int32_t > &pad, const std::vector< int32_t > &stride, const std::vector< int32_t > &dilations, std::vector< TosaSerializationTensor * > &tensors, std::vector< TosaSerializationOperator * > &operators, const bool isPoolingOp=false)
std::string GetUniqueTosaMappingID()
void FlipSignage(DType &type)
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
void CreateConstTosaOperator(const std::string &outputName, const T value, DType dtype, const std::vector< int32_t > &shape, TosaSerializationOperator *&op, TosaSerializationTensor *&tensor)
DataType DtypeToArmNN(const DType type)
Base class for all ArmNN exceptions so that users can filter to just those.
const std::vector< InputSlot > & GetInputSlots() const
const OutputSlot & GetOutputSlot(unsigned int index=0) const override
Get the const output slot handle by slot index.
LayerGuid GetGuid() const final
Returns the unique id of the layer.
unsigned int GetNumInputSlots() const override
Returns the number of connectable input slots.
LayerType GetType() const override
Returns the armnn::LayerType of this layer.
const InputSlot * GetConnection(unsigned int index) const override
unsigned int CalculateIndexOnOwner() const override
Layer & GetOwningLayer() const
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Copyright (c) 2021 ARM Limited and Contributors.