13{
14 if (unaryDescriptor->
m_Operation != UnaryOperation::Rsqrt)
15 {
16 throw armnn::Exception(
"ConvertRsqrtOperator: Unsupported elementwise unary operation in descriptor.");
17 }
18
20 "ConvertRsqrtOperator: Rsqrt must have only one input");
21
23 "ConvertRsqrtOperator: Rsqrt must have only one output");
24
25
26 std::string inputName = std::string("input_");
27 std::string outputName = std::string("output0_");
29 std::string supportedTypes = std::string(" Supported Types: FLOAT32, FLOAT16 & INT8.");
30
31
32
33 if (layer != nullptr)
34 {
37 }
38
39 std::vector<TosaSerializationTensor*> tensors;
40 std::vector<TosaSerializationOperator *> operators;
41
42 DataType inputDType = inputs[0]->GetDataType();
43
44 if (inputDType == DataType::QAsymmS8 || inputDType == DataType::QSymmS8)
45 {
46 float input_scale = inputs[0]->GetQuantizationScale();
47 float output_scale = outputs[0]->GetQuantizationScale();
48 int32_t input_zp = inputs[0]->GetQuantizationOffset();
49 int32_t output_zp = outputs[0]->GetQuantizationOffset();
50
51 const float output_max = static_cast<float>(127 - output_zp) * output_scale;
52
53 auto rsqrt_func = [&](float x) -> float
54 {
55 if (x <= 0.0f)
56 {
57 return output_max;
58 }
59
60 return 1.0f / std::sqrt(x);
61 };
62
63 TosaTableAttribute attribute(
65
66 operators.push_back(new TosaSerializationOperator(tosa::Op_TABLE,
67 Attribute_TableAttribute,
68 &attribute,
69 {inputName},
70 {outputName}));
71 }
72 else if (inputDType == DataType::Float32 || inputDType == DataType::Float16)
73 {
74 operators.push_back(new TosaSerializationOperator(tosa::Op_RSQRT,
75 Attribute_NONE,
76 nullptr,
77 {inputName},
78 {outputName}));
79 }
80 else if (inputDType == DataType::QSymmS16)
81 {
82 throw Exception(
"ConvertRsqrtOperator(): unsupported datatype INT16 is not implemented yet." + supportedTypes);
83 }
84 else if (inputDType == DataType::Signed32 || inputDType == DataType::Signed64)
85 {
86 throw Exception(
"ConvertRsqrtOperator(): unsupported datatype INT32 or INT64." + supportedTypes);
87 }
88 else
89 {
90 throw Exception(
"ConvertRsqrtOperator(): TOSA specification does not support this datatype." + supportedTypes);
91 }
92
93
94
95
96 if (inputName.find("input_") != std::string::npos)
97 {
100 tensors.push_back(new TosaSerializationTensor(inputName, inputShape0, inputDType0, {}));
101 }
102
104 DType outputDType0 =
ArmNNToDType(outputs[0]->GetDataType());
105
106 tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
107
108
109
110 return new TosaSerializationBasicBlock(blockName,
112 operators,
113 tensors,
114 {inputName},
115 {outputName});
116}
#define ARMNN_THROW_INVALIDARG_MSG_IF_FALSE(_cond, _str)
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
const std::string mainName
DType ArmNNToDType(const DataType &type)
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
std::string GetUniqueTosaMappingID()
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
std::vector< int16_t > getTosaConst8bitTable(float input_scale, int32_t input_zp, float output_scale, int32_t output_zp, std::function< float(float)> func)
Base class for all ArmNN exceptions so that users can filter to just those.
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.