Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CherryPick]:Prevent division by 0. #49772

Merged
merged 1 commit into from
May 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions tensorflow/lite/kernels/conv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,7 @@ TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,
// Only one scale factor per batch is typically necessary. See optimized
// implementation for why we need to allocate for the height of the inputs
// flattened to 2D.
TF_LITE_ENSURE(context, channels_in != 0);
const int height = NumElements(input) / channels_in;
int scaling_dims[1] = {height};
if (!TfLiteIntArrayEqualsArray(scaling_factors->dims, 1, scaling_dims)) {
Expand Down Expand Up @@ -539,6 +540,7 @@ TfLiteStatus Prepare(KernelType kernel_type, TfLiteContext* context,
input_offsets->type = kTfLiteInt32;
input_offsets->allocation_type = kTfLiteArenaRw;
// See above comment for the need to allocate for height of inputs.
TF_LITE_ENSURE(context, channels_in != 0);
const int height = NumElements(input) / channels_in;
const int input_offset_dims[1] = {height};
if (!TfLiteIntArrayEqualsArray(input_offsets->dims, 1,
Expand Down Expand Up @@ -800,8 +802,9 @@ void EvalHybridPerChannel(TfLiteContext* context, TfLiteNode* node,
CalculateActivationRange(params->activation, &output_activation_min,
&output_activation_max);

const int input_size = NumElements(input) / SizeOfDimension(input, 0);
const int batch_size = SizeOfDimension(input, 0);
TF_LITE_ENSURE(context, batch_size != 0);
const int input_size = NumElements(input) / batch_size;
int8_t* quantized_input_ptr_batch = GetTensorData<int8_t>(
GetTemporary(context, node, data->input_quantized_index));
float* scaling_factors_ptr = GetTensorData<float>(
Expand Down Expand Up @@ -878,8 +881,9 @@ void EvalHybrid(TfLiteContext* context, TfLiteNode* node,
CalculateActivationRange(params->activation, &output_activation_min,
&output_activation_max);

const int input_size = NumElements(input) / SizeOfDimension(input, 0);
const int batch_size = SizeOfDimension(input, 0);
TF_LITE_ENSURE(context, batch_size != 0);
const int input_size = NumElements(input) / batch_size;

const float* input_ptr = GetTensorData<float>(input);
int8_t* quantized_input_ptr_batch = GetTensorData<int8_t>(
Expand Down