ArmNN
 24.08
LogOperator.hpp File Reference
Include dependency graph for LogOperator.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

TosaSerializationBasicBlock * ConvertLogOperator (const Layer *layer, const std::vector< const TensorInfo * > &inputs, const std::vector< const TensorInfo * > &outputs, const ElementwiseUnaryDescriptor *unaryDescriptor)
 

Function Documentation

◆ ConvertLogOperator()

TosaSerializationBasicBlock* ConvertLogOperator ( const Layer layer,
const std::vector< const TensorInfo * > &  inputs,
const std::vector< const TensorInfo * > &  outputs,
const ElementwiseUnaryDescriptor unaryDescriptor 
)

Definition at line 13 of file LogOperator.cpp.

17 {
18  if (unaryDescriptor->m_Operation != UnaryOperation::Log)
19  {
20  throw armnn::Exception("ConvertLogOperator: Unsupported elementwise unary operation in descriptor.");
21  }
22 
23  std::string inputName = std::string("input_");
24  std::string outputName = std::string("output0_");
25  std::string blockName = std::string("Op_LOG_block_") + GetUniqueTosaMappingID();
26 
27  // If a layer is present then the block will be used for execution, so input and output names need to be determined
28  // using the previous and following layers so the graph is connected correctly. For validation this doesn't matter.
29  if(layer != nullptr)
30  {
31  inputName = GenerateUniqueInputName(layer->GetInputSlot(0));
32  outputName = GenerateUniqueOutputName(*layer);
33  }
34 
35  std::vector<TosaSerializationTensor*> tensors;
36  std::vector<TosaSerializationOperator*> operators;
37 
38  float input_scale = inputs[0]->GetQuantizationScale();
39  float output_scale = outputs[0]->GetQuantizationScale();
40  int32_t input_zp = inputs[0]->GetQuantizationOffset();
41  int32_t output_zp = outputs[0]->GetQuantizationOffset();
42  DataType inputDType = inputs[0]->GetDataType();
43 
44  if (inputDType == DataType::QAsymmS8 ||
45  inputDType == DataType::QSymmS8)
46  {
47  const float output_min = static_cast<float>(-128 - output_zp) * output_scale;
48 
49  auto log_func = [&](float x) -> float
50  {
51  if (x <= 0.0f)
52  {
53  return output_min;
54  }
55  return std::log(x);
56  };
57 
58  TosaTableAttribute attribute(
59  getTosaConst8bitTable(input_scale, input_zp, output_scale, output_zp, log_func));
60  operators.push_back(new TosaSerializationOperator(tosa::Op_TABLE,
61  Attribute_TableAttribute,
62  &attribute,
63  {inputName},
64  {outputName}));
65  }
66  else if (inputDType == DataType::QSymmS16)
67  {
68  throw Exception("ConvertLogOperator() unsupported int 16 not implemented yet.");
69  // The following generates the table, tosa attribute and operator for int16 log.
70  // However, running the int16 LOG EndToEnd test causes incorrect output values.
71  // At the time of writing the LOG operator there is no requirment for int16 support.
72  // Points to enable int16 in the future:
73  // - TOSA specifies LOG int16 input must have int32 output
74  // - We potentially need a rescale after the int32 LOG output to convert back to int16.
75  /*
76  const float output_min = (-32768 - output_zp) * static_cast<float>(output_scale);
77 
78  auto log_func = [&](float x) -> float {
79  if (x <= 0.0f) {
80  return output_min;
81  }
82  return std::log(x);
83  };
84 
85  TosaTableAttribute attribute(
86  getTosaConst16bitTable<float>(input_scale, input_zp, output_scale, output_zp, log_func));
87  operators.push_back(new TosaSerializationOperator(tosa::Op_TABLE,
88  Attribute_TableAttribute,
89  &attribute,
90  {inputName},
91  {outputName}));
92  */
93  }
94  else if (inputDType == DataType::Signed32 ||
95  inputDType == DataType::Signed64)
96  {
97  throw Exception(
98  "ConvertLogOperator() unsupported int 32. Only int 8 and int 16 quantized types are supported.");
99  }
100  // Floating point LOG operator
101  else
102  {
103  operators.push_back(new TosaSerializationOperator(tosa::Op_LOG,
104  Attribute_NONE,
105  nullptr,
106  {inputName},
107  {outputName}));
108  }
109 
110  // Only add input tensor if connected layer is an input layer.
111  // As intermediate or constant tensors will be created separately.
112  // There also can't be duplicate tensor.
113  if(inputName.find("input_") != std::string::npos)
114  {
115  std::vector<int32_t> inputShape0 = GetTosaTensorShape(inputs[0]->GetShape());
116  DType inputDType0 = ArmNNToDType(inputDType);
117  tensors.push_back(new TosaSerializationTensor(inputName, inputShape0, inputDType0, {}));
118  }
119 
120  std::vector<int32_t> outputShape0 = GetTosaTensorShape(outputs[0]->GetShape());
121 
122  // Re-enable below line for int16 LOG support which requires int32 output in TOSA and remove second line.
123  // DType outputDType0 =
124  // (inputDType == DataType::QSymmS16) ? DType::DType_INT32 : ArmNNToDType(outputs[0]->GetDataType());
125  DType outputDType0 = ArmNNToDType(outputs[0]->GetDataType());
126 
127  tensors.push_back(new TosaSerializationTensor(outputName, outputShape0, outputDType0, {}));
128 
129  // operatorInputNames/operatorOutputNames ends up being the same as
130  // blockInputNames/blockOutputNames for one-to-one ArmNN to Tosa mappings
131  return new TosaSerializationBasicBlock(blockName, // name
132  mainName, // region name
133  operators, // operators
134  tensors, // tensors
135  {inputName}, // inputs
136  {outputName}); // outputs
137 }

References GenerateUniqueInputName(), GenerateUniqueOutputName(), Layer::GetInputSlot(), getTosaConst8bitTable(), GetTosaTensorShape(), GetUniqueTosaMappingID(), and ElementwiseUnaryDescriptor::m_Operation.

Referenced by GetTosaMapping().

getTosaConst8bitTable
std::vector< int16_t > getTosaConst8bitTable(float input_scale, int32_t input_zp, float output_scale, int32_t output_zp, std::function< float(float)> func)
Definition: TosaTableUtils.hpp:19
GenerateUniqueOutputName
std::string GenerateUniqueOutputName(const Layer &layer, uint32_t layerSlot=0)
Definition: TosaOperatorUtils.hpp:120
armnn::Layer::GetInputSlot
const InputSlot & GetInputSlot(unsigned int index) const override
Get a const input slot handle by slot index.
Definition: Layer.hpp:337
mainName
const std::string mainName
Definition: TosaOperatorUtils.hpp:19
ArmNNToDType
DType ArmNNToDType(const DataType &type)
Definition: TosaOperatorUtils.hpp:22
armnn::DataType
DataType
Definition: Types.hpp:48
armnn::Exception
Base class for all ArmNN exceptions so that users can filter to just those.
Definition: Exceptions.hpp:46
armnn::ElementwiseUnaryDescriptor::m_Operation
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.
Definition: Descriptors.hpp:145
GetTosaTensorShape
std::vector< int32_t > GetTosaTensorShape(const TensorShape &shape)
Definition: TosaOperatorUtils.hpp:79
GenerateUniqueInputName
std::string GenerateUniqueInputName(const armnn::InputSlot &slot)
Definition: TosaOperatorUtils.hpp:109
GetUniqueTosaMappingID
std::string GetUniqueTosaMappingID()
Definition: TosaOperatorUtils.hpp:138