| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- ==============================================================================*/
- #include "tensorflow/lite/kernels/internal/reference/transpose_conv.h"
- #include "tensorflow/lite/c/builtin_op_data.h"
- #include "tensorflow/lite/c/common.h"
- #include "tensorflow/lite/kernels/internal/common.h"
- #include "tensorflow/lite/kernels/internal/quantization_util.h"
- #include "tensorflow/lite/kernels/internal/reference/integer_ops/transpose_conv.h"
- #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
- #include "tensorflow/lite/kernels/kernel_util.h"
- #include "tensorflow/lite/kernels/padding.h"
- #include "tensorflow/lite/micro/kernels/kernel_util.h"
- namespace tflite {
- namespace {
- // For the TfLite transpose_conv implementation, input tensor 0 corresponds to
- // the OutputShapeTensor. However, since TFLM does not support dynamic tensors,
- // the TFLM implementation ignores input tensor 0 and the only inputs we care
- // about are kFilterTensor, kInputTensor and kBiasTensor.
- constexpr int kFilterTensor = 1;
- constexpr int kInputTensor = 2;
- constexpr int kBiasTensor = 3;
- constexpr int kOutputTensor = 0;
- // Conv is quantized along dimension 0:
- // https://www.tensorflow.org/lite/performance/quantization_spec
- constexpr int kConvQuantizedDimension = 0;
- struct OpData {
- ConvParams params;
- // A scratch buffer is required for quantized implementations.
- int scratch_buffer_index;
- // Multiplier and shift arrays are required for the int8 implementation.
- int32_t* per_channel_output_multiplier;
- int32_t* per_channel_output_shift;
- };
- inline PaddingType RuntimePaddingType(TfLitePadding padding) {
- switch (padding) {
- case TfLitePadding::kTfLitePaddingSame:
- return PaddingType::kSame;
- case TfLitePadding::kTfLitePaddingValid:
- return PaddingType::kValid;
- case TfLitePadding::kTfLitePaddingUnknown:
- default:
- return PaddingType::kNone;
- }
- }
- TfLiteStatus CalculateOpData(TfLiteContext* context, TfLiteNode* node,
- const TfLiteConvParams* params, int width,
- int height, int filter_width, int filter_height,
- int out_width, int out_height,
- const TfLiteType data_type, OpData* data) {
- bool has_bias = node->inputs->size == 4;
- // Check number of inputs/outputs
- TF_LITE_ENSURE(context, has_bias || node->inputs->size == 3);
- TF_LITE_ENSURE_EQ(context, node->outputs->size, 1);
- // Matching GetWindowedOutputSize in TensorFlow.
- auto padding = params->padding;
- TfLitePaddingValues padding_values = ComputePaddingHeightWidth(
- params->stride_height, params->stride_width,
- params->dilation_height_factor, params->dilation_width_factor, height,
- width, filter_height, filter_width, padding, &out_height, &out_width);
- data->params.padding_type = RuntimePaddingType(padding);
- data->params.padding_values.width = padding_values.width;
- data->params.padding_values.height = padding_values.height;
- // Note that quantized inference requires that all tensors have their
- // parameters set. This is usually done during quantized training.
- if (data_type != kTfLiteFloat32) {
- const TfLiteTensor* input = GetInput(context, node, kInputTensor);
- TF_LITE_ENSURE(context, input != nullptr);
- const TfLiteTensor* filter = GetInput(context, node, kFilterTensor);
- TF_LITE_ENSURE(context, filter != nullptr);
- const TfLiteTensor* bias =
- GetOptionalInputTensor(context, node, kBiasTensor);
- TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
- TF_LITE_ENSURE(context, output != nullptr);
- int output_channels = filter->dims->data[kConvQuantizedDimension];
- TF_LITE_ENSURE_STATUS(tflite::PopulateConvolutionQuantizationParams(
- context, input, filter, bias, output, params->activation,
- &data->params.output_multiplier, &data->params.output_shift,
- &data->params.quantized_activation_min,
- &data->params.quantized_activation_max,
- data->per_channel_output_multiplier,
- reinterpret_cast<int*>(data->per_channel_output_shift),
- output_channels));
- }
- return kTfLiteOk;
- }
- void* Init(TfLiteContext* context, const char* buffer, size_t length) {
- TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
- return context->AllocatePersistentBuffer(context, sizeof(OpData));
- }
- TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
- TFLITE_DCHECK(node->user_data != nullptr);
- TFLITE_DCHECK(node->builtin_data != nullptr);
- OpData* data = static_cast<OpData*>(node->user_data);
- const auto params = static_cast<const TfLiteConvParams*>(node->builtin_data);
- TfLiteTensor* output = GetOutput(context, node, kOutputTensor);
- TF_LITE_ENSURE(context, output != nullptr);
- const TfLiteTensor* input = GetInput(context, node, kInputTensor);
- TF_LITE_ENSURE(context, input != nullptr);
- const TfLiteTensor* filter = GetInput(context, node, kFilterTensor);
- TF_LITE_ENSURE(context, filter != nullptr);
- int input_width = input->dims->data[2];
- int input_height = input->dims->data[1];
- int filter_width = filter->dims->data[2];
- int filter_height = filter->dims->data[1];
- int output_width = output->dims->data[2];
- int output_height = output->dims->data[1];
- // Dynamically allocate per-channel quantization parameters.
- const int num_channels = filter->dims->data[kConvQuantizedDimension];
- data->per_channel_output_multiplier =
- static_cast<int32_t*>(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t)));
- data->per_channel_output_shift =
- static_cast<int32_t*>(context->AllocatePersistentBuffer(
- context, num_channels * sizeof(int32_t)));
- // Quantized kernels use an int32 scratch buffer.
- if (input->type == kTfLiteUInt8 || input->type == kTfLiteInt8) {
- TFLITE_DCHECK(context->RequestScratchBufferInArena != nullptr);
- TFLITE_DCHECK(context->RequestScratchBufferInArena(
- context,
- GetTensorShape(output).FlatSize() * sizeof(int32_t),
- &(data->scratch_buffer_index)) == kTfLiteOk);
- }
- // All per-channel quantized tensors need valid zero point and scale arrays.
- if (input->type == kTfLiteInt8) {
- TF_LITE_ENSURE_EQ(context, filter->quantization.type,
- kTfLiteAffineQuantization);
- const auto* affine_quantization =
- static_cast<TfLiteAffineQuantization*>(filter->quantization.params);
- TF_LITE_ENSURE(context, affine_quantization);
- TF_LITE_ENSURE(context, affine_quantization->scale);
- TF_LITE_ENSURE(context, affine_quantization->zero_point);
- TF_LITE_ENSURE(context,
- affine_quantization->scale->size == 1 ||
- affine_quantization->scale->size ==
- filter->dims->data[kConvQuantizedDimension]);
- TF_LITE_ENSURE_EQ(context, affine_quantization->scale->size,
- affine_quantization->zero_point->size);
- }
- TF_LITE_ENSURE_STATUS(CalculateOpData(
- context, node, params, input_width, input_height, filter_width,
- filter_height, output_width, output_height, input->type, data));
- // Offsets (zero points)
- data->params.input_offset = -input->params.zero_point;
- data->params.weights_offset = -filter->params.zero_point;
- data->params.output_offset = output->params.zero_point;
- // Stride + dilation
- data->params.stride_width = params->stride_width;
- data->params.stride_height = params->stride_height;
- data->params.dilation_width_factor = params->dilation_width_factor;
- data->params.dilation_height_factor = params->dilation_height_factor;
- float output_activation_min, output_activation_max;
- CalculateActivationRange(params->activation, &output_activation_min,
- &output_activation_max);
- data->params.float_activation_min = output_activation_min;
- data->params.float_activation_max = output_activation_max;
- return kTfLiteOk;
- } // namespace conv
- TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
- const TfLiteEvalTensor* input =
- tflite::micro::GetEvalInput(context, node, kInputTensor);
- const TfLiteEvalTensor* filter =
- tflite::micro::GetEvalInput(context, node, kFilterTensor);
- const TfLiteEvalTensor* bias =
- (NumInputs(node) == 4)
- ? tflite::micro::GetEvalInput(context, node, kBiasTensor)
- : nullptr;
- TfLiteEvalTensor* output =
- tflite::micro::GetEvalOutput(context, node, kOutputTensor);
- TFLITE_DCHECK(node->user_data != nullptr);
- const OpData& data = *(static_cast<const OpData*>(node->user_data));
- TF_LITE_ENSURE_EQ(context, input->type, output->type);
- TF_LITE_ENSURE_MSG(context, input->type == filter->type,
- "Hybrid models are not supported on TFLite Micro.");
- switch (input->type) { // Already know in/out types are same.
- case kTfLiteFloat32: {
- reference_ops::TransposeConv(
- data.params, tflite::micro::GetTensorShape(input),
- tflite::micro::GetTensorData<float>(input),
- tflite::micro::GetTensorShape(filter),
- tflite::micro::GetTensorData<float>(filter),
- tflite::micro::GetTensorShape(bias),
- tflite::micro::GetTensorData<float>(bias),
- tflite::micro::GetTensorShape(output),
- tflite::micro::GetTensorData<float>(output),
- tflite::micro::GetTensorShape(nullptr), nullptr);
- break;
- }
- case kTfLiteInt8: {
- int32_t* scratch_buffer = static_cast<int32_t*>(
- context->GetScratchBuffer(context, data.scratch_buffer_index));
- reference_integer_ops::TransposeConv(
- data.params, data.per_channel_output_multiplier,
- data.per_channel_output_shift, tflite::micro::GetTensorShape(input),
- tflite::micro::GetTensorData<int8_t>(input),
- tflite::micro::GetTensorShape(filter),
- tflite::micro::GetTensorData<int8_t>(filter),
- tflite::micro::GetTensorShape(bias),
- tflite::micro::GetTensorData<int32_t>(bias),
- tflite::micro::GetTensorShape(output),
- tflite::micro::GetTensorData<int8_t>(output),
- tflite::micro::GetTensorShape(nullptr), nullptr, scratch_buffer);
- break;
- }
- default:
- TF_LITE_KERNEL_LOG(context, "Type %s (%d) not supported.",
- TfLiteTypeGetName(input->type), input->type);
- return kTfLiteError;
- }
- return kTfLiteOk;
- }
- } // namespace
- TfLiteRegistration Register_TRANSPOSE_CONV() {
- return {/*init=*/Init,
- /*free=*/nullptr,
- /*prepare=*/Prepare,
- /*invoke=*/Eval,
- /*profiling_string=*/nullptr,
- /*builtin_code=*/0,
- /*custom_name=*/nullptr,
- /*version=*/0};
- }
- } // namespace tflite
|