Cadence: HiFi4 Neural Network (NN) source download and Fix issue with Softmax

HiFi4 Neural Network (NN) source download
Fixed compilation issue with softmax.cc

Signed-off-by: Prasad Nikam pnikam@cadence.com
Signed-off-by: Niranjan Yadla nyadla@cadence.com
This commit is contained in:
Niranjan Yadla 2020-03-27 14:18:28 -07:00
parent 22325a5c7d
commit 2722af9700
4 changed files with 92 additions and 176 deletions

View File

@ -19,7 +19,7 @@
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/ ******************************************************************************/
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -40,39 +40,34 @@ limitations under the License.
#include "tensorflow/lite/c/common.h" #include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/kernels/internal/common.h" #include "tensorflow/lite/kernels/internal/common.h"
#include "tensorflow/lite/kernels/internal/quantization_util.h" #include "tensorflow/lite/kernels/internal/quantization_util.h"
#include "tensorflow/lite/kernels/internal/reference/integer_ops/softmax.h"
#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
#include "tensorflow/lite/kernels/kernel_util.h" #include "tensorflow/lite/kernels/kernel_util.h"
#include "tensorflow/lite/kernels/op_macros.h" #include "tensorflow/lite/kernels/op_macros.h"
#include "xtensa_tf_micro_common.h"
#include "xtensa_tf_micro_common.h"
namespace tflite { namespace tflite {
namespace ops { namespace ops {
namespace micro { namespace micro {
namespace activations { namespace activations {
namespace { namespace {
struct OpData { TfLiteStatus CalculateSoftmaxParams(TfLiteContext* context,
int32_t input_multiplier = 0;
int input_left_shift = 0;
int32_t input_range_radius = 0;
int diff_min = 0;
};
TfLiteStatus CalculateSoftmaxOpData(TfLiteContext* context,
const TfLiteTensor* input, const TfLiteTensor* input,
TfLiteTensor* output, TfLiteTensor* output,
const TfLiteSoftmaxParams* params, const TfLiteSoftmaxParams* params,
OpData* data) { SoftmaxParams* op_data) {
if (input->type == kTfLiteUInt8 || input->type == kTfLiteInt8) { if (input->type == kTfLiteUInt8 || input->type == kTfLiteInt8) {
if (input->type == kTfLiteUInt8) { if (input->type == kTfLiteUInt8) {
TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt8);
TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0); TF_LITE_ENSURE_EQ(context, output->params.zero_point, 0);
} else { } else {
TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt8);
if (output->type == kTfLiteInt16) { if (output->type == kTfLiteInt16) {
TF_LITE_ENSURE_EQ(context, output->params.zero_point, -32768); TF_LITE_ENSURE_EQ(context, output->params.zero_point, -32768);
// NOTE: Current int16 softmax output does not require symmetric scaling // NOTE: Current int16 softmax output does not require symmetric scaling
// - so no need to verify scale here. // - so no need to verify scale here.
} else { } else {
TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8);
TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128); TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128);
TF_LITE_ENSURE(context, output->params.scale == 1.f / 256); TF_LITE_ENSURE(context, output->params.scale == 1.f / 256);
} }
@ -80,12 +75,19 @@ TfLiteStatus CalculateSoftmaxOpData(TfLiteContext* context,
static const int kScaledDiffIntegerBits = 5; static const int kScaledDiffIntegerBits = 5;
int input_left_shift;
tflite::PreprocessSoftmaxScaling( tflite::PreprocessSoftmaxScaling(
static_cast<double>(params->beta), static_cast<double>(params->beta),
static_cast<double>(input->params.scale), kScaledDiffIntegerBits, static_cast<double>(input->params.scale), kScaledDiffIntegerBits,
&data->input_multiplier, &data->input_left_shift); &op_data->input_multiplier, &input_left_shift);
data->diff_min = -1.0 * tflite::CalculateInputRadius( op_data->input_left_shift = input_left_shift;
kScaledDiffIntegerBits, data->input_left_shift); op_data->diff_min =
-1.0 * tflite::CalculateInputRadius(kScaledDiffIntegerBits,
op_data->input_left_shift);
} else {
TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteFloat32);
TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteFloat32);
op_data->beta = static_cast<double>(params->beta);
} }
return kTfLiteOk; return kTfLiteOk;
} }
@ -99,207 +101,118 @@ void* Init(TfLiteContext* context, const char* buffer, size_t length) {
void Free(TfLiteContext* context, void* buffer) {} void Free(TfLiteContext* context, void* buffer) {}
TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) { TfLiteStatus SoftmaxPrepare(TfLiteContext* context, TfLiteNode* node) {
TF_LITE_ENSURE_EQ(context, NumInputs(node), 1);
TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1);
const TfLiteTensor* input = GetInput(context, node, 0);
TF_LITE_ENSURE(context, NumDimensions(input) >= 1);
return kTfLiteOk; return kTfLiteOk;
} }
// Takes a 1D tensor and performs softmax along it. // Takes a tensor and performs softmax along the last dimension.
void Softmax1DFloat(const TfLiteTensor* input, TfLiteTensor* output, TfLiteStatus SoftmaxFloat(TfLiteContext *context, const TfLiteTensor* input, TfLiteTensor* output,
TfLiteSoftmaxParams* params) { const SoftmaxParams& op_data) {
const int input_size = input->dims->data[0]; const RuntimeShape& input_shape = GetTensorShape(input);
tflite::reference_ops::Softmax(input->data.f, input_size, 1, params->beta, const float *input_data = GetTensorData<float>(input);
output->data.f); const RuntimeShape& output_shape = GetTensorShape(output);
} float *output_data = GetTensorData<float>(output);
const int trailing_dim = input_shape.DimensionsCount() - 1;
// Takes a 2D tensor and perform softmax along the last dimension. const int outer_size =
TfLiteStatus Softmax2DFloat(TfLiteContext* context, const TfLiteTensor* input, MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
TfLiteTensor* output, TfLiteSoftmaxParams* params) { const int depth =
const int batch_size = input->dims->data[0]; MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
const int input_size = input->dims->data[1];
ALLOCATE_XTENSA_NNLIB_SCRATCH_MEM; ALLOCATE_XTENSA_NNLIB_SCRATCH_MEM;
float* p_scratch = (float*)xtensa_nnlib_scratch_buf; float *p_scratch = (float *)xtensa_nnlib_scratch_buf;
if (input->dims->data[1] * sizeof(float) > XTENSA_NNLIB_MAX_SCRATCH_SIZE) { if(depth * sizeof(float) > XTENSA_NNLIB_MAX_SCRATCH_SIZE)
{
TF_LITE_KERNEL_LOG(context, "Softmax: insufficient scratch memory"); TF_LITE_KERNEL_LOG(context, "Softmax: insufficient scratch memory");
return kTfLiteError; return kTfLiteError;
} }
for (int i = 0; i < batch_size * input_size; ++i) { for (int i = 0; i < outer_size; ++i) {
p_scratch[i] = input->data.f[i] * params->beta; for (int c = 0; c < depth; ++c) {
} p_scratch[c] = input_data[i * depth + c] * static_cast<float>(op_data.beta);
}
for (int i = 0; i < batch_size; ++i) { int err = xa_nn_vec_softmax_f32_f32(&output_data[i * depth],
int err = xa_nn_vec_softmax_f32_f32(&output->data.f[i * input_size], p_scratch,
&p_scratch[i * input_size], input_size); depth);
CHECK_ERR_HIFI_NNLIB_KER(err, "xa_nn_vec_softmax_f32_f32 failed"); CHECK_ERR_HIFI_NNLIB_KER(err, "xa_nn_vec_softmax_f32_f32 failed"); \
} }
return kTfLiteOk; return kTfLiteOk;
} }
void Softmax1DQuantized(const TfLiteTensor* input, TfLiteTensor* output, TfLiteStatus SoftmaxQuantized(TfLiteContext* context, const TfLiteTensor* input, TfLiteTensor* output,
TfLiteSoftmaxParams* params, OpData* data) { const SoftmaxParams& op_data) {
// (ahentz): this is arguably a dirty trick. Since the implementation
// always traverses the last dimension of a 4D tensor, we will pretend our 1D
// tensor is 4D in a special way. We will convert a (Y) shape into a (1,
// 1, 1, Y) shape.
const int input_size = input->dims->data[0];
const int32_t shape_data[4] = {1, 1, 1, input_size};
RuntimeShape shape(4, shape_data);
SoftmaxParams op_params;
op_params.input_multiplier = data->input_multiplier;
op_params.input_left_shift = data->input_left_shift;
op_params.diff_min = data->diff_min;
if (input->type == kTfLiteUInt8) { if (input->type == kTfLiteUInt8) {
tflite::reference_ops::Softmax(op_params, shape, const RuntimeShape& input_shape = GetTensorShape(input);
GetTensorData<uint8_t>(input), shape, const uint8_t *input_data = GetTensorData<uint8_t>(input);
GetTensorData<uint8_t>(output)); const RuntimeShape& output_shape = GetTensorShape(output);
} else { uint8_t *output_data = GetTensorData<uint8_t>(output);
if (output->type == kTfLiteInt16) { const int trailing_dim = input_shape.DimensionsCount() - 1;
tflite::reference_integer_ops::Softmax( const int outer_size =
op_params, shape, GetTensorData<int8_t>(input), shape, MatchingFlatSizeSkipDim(input_shape, trailing_dim, output_shape);
GetTensorData<int16_t>(output)); const int depth =
} else { MatchingDim(input_shape, trailing_dim, output_shape, trailing_dim);
tflite::reference_integer_ops::Softmax(
op_params, shape, GetTensorData<int8_t>(input), shape,
GetTensorData<int8_t>(output));
}
}
}
TfLiteStatus Softmax2DQuantized(TfLiteContext* context,
const TfLiteTensor* input, TfLiteTensor* output,
TfLiteSoftmaxParams* params, OpData* data) {
// (ahentz): this is arguably a dirty trick. Since the implementation
// always traverses the last dimension of a 4D tensor, we will pretend our 2D
// tensor is 4D in a special way. We will convert a (X, Y) shape into a (X,
// 1, 1, Y) shape.
const int batch_size = input->dims->data[0];
const int input_size = input->dims->data[1];
const int32_t shape_data[4] = {batch_size, 1, 1, input_size};
RuntimeShape shape(4, shape_data);
SoftmaxParams op_params;
op_params.input_multiplier = data->input_multiplier;
op_params.input_left_shift = data->input_left_shift;
op_params.diff_min = data->diff_min;
if (input->type == kTfLiteUInt8) {
ALLOCATE_XTENSA_NNLIB_SCRATCH_MEM; ALLOCATE_XTENSA_NNLIB_SCRATCH_MEM;
void* p_scratch = (void*)xtensa_nnlib_scratch_buf; void *p_scratch = (void *)xtensa_nnlib_scratch_buf;
if (get_softmax_scratch_size(PREC_ASYM8, PREC_ASYM8, input_size) > if(get_softmax_scratch_size(PREC_ASYM8, PREC_ASYM8, depth) > XTENSA_NNLIB_MAX_SCRATCH_SIZE)
XTENSA_NNLIB_MAX_SCRATCH_SIZE) { {
TF_LITE_KERNEL_LOG(context, "Softmax: insufficient scratch memory"); TF_LITE_KERNEL_LOG(context, "Softmax: insufficient scratch memory");
return kTfLiteError; return kTfLiteError;
} }
for (int i = 0; i < batch_size; ++i) { for (int i = 0; i < outer_size; ++i) {
int err = xa_nn_vec_softmax_asym8_asym8( int err = xa_nn_vec_softmax_asym8_asym8(&output_data[i * depth],
&output->data.uint8[i * input_size], &input_data[i * depth],
&input->data.uint8[i * input_size], op_params.diff_min, op_data.diff_min,
op_params.input_left_shift, op_params.input_multiplier, input_size, op_data.input_left_shift,
p_scratch); op_data.input_multiplier,
CHECK_ERR_HIFI_NNLIB_KER(err, "xa_nn_vec_softmax_asym8_asym8 failed"); depth,
p_scratch
);
CHECK_ERR_HIFI_NNLIB_KER(err, "xa_nn_vec_softmax_asym8_asym8 failed"); \
} }
} else { } else {
if (output->type == kTfLiteInt16) { if (output->type == kTfLiteInt16) {
tflite::reference_integer_ops::Softmax( tflite::reference_ops::Softmax(
op_params, shape, GetTensorData<int8_t>(input), shape, op_data, GetTensorShape(input), GetTensorData<int8_t>(input),
GetTensorData<int16_t>(output)); GetTensorShape(output), GetTensorData<int16_t>(output));
} else { } else {
tflite::reference_integer_ops::Softmax( tflite::reference_ops::Softmax(
op_params, shape, GetTensorData<int8_t>(input), shape, op_data, GetTensorShape(input), GetTensorData<int8_t>(input),
GetTensorData<int8_t>(output)); GetTensorShape(output), GetTensorData<int8_t>(output));
} }
} }
return kTfLiteOk; return kTfLiteOk;
} }
// Takes a 4D tensor and perform softmax along the forth dimension.
void Softmax4DFloat(const TfLiteTensor* input, TfLiteTensor* output,
TfLiteSoftmaxParams* params) {
SoftmaxParams op_params;
op_params.beta = static_cast<double>(params->beta);
tflite::reference_ops::Softmax(
op_params, GetTensorShape(input), GetTensorData<float>(input),
GetTensorShape(output), GetTensorData<float>(output));
}
void Softmax4DQuantized(const TfLiteTensor* input, TfLiteTensor* output,
TfLiteSoftmaxParams* params, OpData* data) {
SoftmaxParams op_params;
op_params.input_multiplier = data->input_multiplier;
op_params.input_left_shift = data->input_left_shift;
op_params.diff_min = data->diff_min;
if (input->type == kTfLiteUInt8) {
tflite::reference_ops::Softmax(
op_params, GetTensorShape(input), GetTensorData<uint8_t>(input),
GetTensorShape(output), GetTensorData<uint8_t>(output));
} else {
if (output->type == kTfLiteInt16) {
tflite::reference_integer_ops::Softmax(
op_params, GetTensorShape(input), GetTensorData<int8_t>(input),
GetTensorShape(output), GetTensorData<int16_t>(output));
} else {
tflite::reference_integer_ops::Softmax(
op_params, GetTensorShape(input), GetTensorData<int8_t>(input),
GetTensorShape(output), GetTensorData<int8_t>(output));
}
}
}
TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) { TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) {
auto* params = reinterpret_cast<TfLiteSoftmaxParams*>(node->builtin_data); auto* params = static_cast<TfLiteSoftmaxParams*>(node->builtin_data);
const TfLiteTensor* input = GetInput(context, node, 0); const TfLiteTensor* input = GetInput(context, node, 0);
TfLiteTensor* output = GetOutput(context, node, 0); TfLiteTensor* output = GetOutput(context, node, 0);
OpData local_data_object; SoftmaxParams op_data;
OpData* data = &local_data_object;
TF_LITE_ENSURE_STATUS( TF_LITE_ENSURE_STATUS(
CalculateSoftmaxOpData(context, input, output, params, data)); CalculateSoftmaxParams(context, input, output, params, &op_data));
// (ahentz): consider an implementation that works for many (all?)
// dimensions.
switch (input->type) { switch (input->type) {
case kTfLiteFloat32: { case kTfLiteFloat32: {
if (NumDimensions(input) == 1) { return SoftmaxFloat(context, input, output, op_data);
Softmax1DFloat(input, output, params);
return kTfLiteOk;
}
if (NumDimensions(input) == 2) {
return Softmax2DFloat(context, input, output, params);
}
if (NumDimensions(input) == 4) {
Softmax4DFloat(input, output, params);
return kTfLiteOk;
}
TF_LITE_KERNEL_LOG(
context, "Only 1D, 2D and 4D tensors supported currently, got %dD.",
NumDimensions(input));
return kTfLiteError;
} }
case kTfLiteInt8: case kTfLiteInt8:
case kTfLiteUInt8: { case kTfLiteUInt8: {
if (NumDimensions(input) == 1) { return SoftmaxQuantized(context, input, output, op_data);
Softmax1DQuantized(input, output, params, data);
return kTfLiteOk;
}
if (NumDimensions(input) == 2) {
return Softmax2DQuantized(context, input, output, params, data);
}
if (NumDimensions(input) == 4) {
Softmax4DQuantized(input, output, params, data);
return kTfLiteOk;
}
TF_LITE_KERNEL_LOG(context,
"Only 2D and 4D tensors supported currently, got %dD.",
NumDimensions(input));
return kTfLiteError;
} }
default: default:
TF_LITE_KERNEL_LOG( TF_LITE_KERNEL_LOG(
context, context,
"Only float32, uint8_t and int8_t supported currently, got %d.", "Only float32, uint8_t and int8_t input supported currently, got %d.",
input->type); input->type);
return kTfLiteError; return kTfLiteError;
} }
@ -307,11 +220,14 @@ TfLiteStatus SoftmaxEval(TfLiteContext* context, TfLiteNode* node) {
} // namespace activations } // namespace activations
TfLiteRegistration* Register_SOFTMAX() { TfLiteRegistration* Register_SOFTMAX() {
static TfLiteRegistration r = {}; static TfLiteRegistration r = {activations::Init,
r.init = activations::Init; activations::Free,
r.free = activations::Free; activations::SoftmaxPrepare,
r.prepare = activations::SoftmaxPrepare; activations::SoftmaxEval,
r.invoke = activations::SoftmaxEval; nullptr,
0,
nullptr,
0};
return &r; return &r;
} }

View File

@ -1,6 +1,6 @@
ifneq ($(filter xtensa_hifi, $(ALL_TAGS)),) ifneq ($(filter xtensa_hifi, $(ALL_TAGS)),)
XTENSA_PATH = $(MAKEFILE_DIR)/../../kernels/xtensa_hifi XTENSA_PATH = $(MAKEFILE_DIR)/downloads
ifneq (,$(filter hifi4%, $(TARGET_ARCH))) ifneq (,$(filter hifi4%, $(TARGET_ARCH)))

View File

@ -5,6 +5,8 @@
ifeq ($(TARGET), xtensa_hifi) ifeq ($(TARGET), xtensa_hifi)
TARGET_ARCH := hifi3_bd5 TARGET_ARCH := hifi3_bd5
$(eval $(call add_third_party_download,$(XTENSA_HIFI4_URL),$(XTENSA_HIFI4_MD5),xa_nnlib,))
PLATFORM_ARGS = \ PLATFORM_ARGS = \
-mno-mul16 \ -mno-mul16 \
-mno-mul32 \ -mno-mul32 \

View File

@ -7,8 +7,6 @@
ifeq ($(TARGET), xtensa-xpg) ifeq ($(TARGET), xtensa-xpg)
TARGET_ARCH := xtensa-xpg TARGET_ARCH := xtensa-xpg
$(eval $(call add_third_party_download,$(XTENSA_HIFI4_URL),$(XTENSA_HIFI4_MD5),xa_nnlib,))
PLATFORM_ARGS = \ PLATFORM_ARGS = \
-DTF_LITE_MCU_DEBUG_LOG \ -DTF_LITE_MCU_DEBUG_LOG \
--xtensa-core=$(XTENSA_CORE) \ --xtensa-core=$(XTENSA_CORE) \