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

warning: 'resolved_axis' may be used uninitialized in this function (reduce.cc) #449

Closed
tcal-x opened this issue Aug 20, 2021 · 3 comments
Closed

Comments

@tcal-x
Copy link

tcal-x commented Aug 20, 2021

This was seen when we attempted to bump the tflite-micro submodule in our project up to current main, from about a month ago (google/CFU-Playground#230 (comment)).

Since we compile TFL-micro with -Werror (using riscv gcc v8.3), this warning becomes an error for us:

In file included from /home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/kernels/internal/common.h:29,
                 from /home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/kernels/internal/reference/reduce.h:19,
                 from /home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/micro/kernels/reduce.cc:16:
/home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/kernels/internal/types.h: In function 'TfLiteStatus tflite::ops::micro::reduce::EvalMean(TfLiteContext*, TfLiteNode*)':
/home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/kernels/internal/types.h:185:33: warning: 'resolved_axis' may be used uninitialized in this function [-Wmaybe-uninitialized]
         if (idx == axis[axis_idx]) {
                    ~~~~~~~~~~~~~^
/home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/kernels/internal/types.h:185:33: warning: 'resolved_axis' may be used uninitialized in this function [-Wmaybe-uninitialized]
         if (idx == axis[axis_idx]) {
                    ~~~~~~~~~~~~~^
/home/tim/tcal-x/CFU-Playground/proj/proj_template_v/build/src/tensorflow/lite/kernels/internal/types.h:185:33: warning: 'resolved_axis' may be used uninitialized in this function [-Wmaybe-uninitialized]
         if (idx == axis[axis_idx]) {
                    ~~~~~~~~~~~~~^

I think it's referencing this: https://github.com/tensorflow/tflite-micro/blob/main/tensorflow/lite/micro/kernels/reduce.cc#L149 , and I can't see where/how it gets initialized.

If you don't see these warnings, then there must be something wrong in our usage of tfl-micro.

@tcal-x
Copy link
Author

tcal-x commented Aug 20, 2021

When I compiled without -Werror, there were no additional warnings and compilation finished successfully.

@advaitjain
Copy link
Member

As an aside, if you'd like to avoid pulling in a whole lot of unrelated code into your repo, you could use TFLM's new support for external integration:

To address this specific issue, while the code in question is certainly complex, AFAICT this is a false positive.

The variable is passed as a pre-allocated buffer and gives the impression of being an input parameter. However, if we trace the call stack it is initialized.

For example,

reference_ops::Mean(
tflite::micro::GetTensorData<float>(input), input->dims->data,
input->dims->size, tflite::micro::GetTensorData<float>(output),
output->dims->data, output->dims->size,
tflite::micro::GetTensorData<int>(axis), num_axis,
params->keep_dims, temp_index, resolved_axis,
tflite::micro::GetTensorData<float>(output)));

calls into

inline bool Mean(const T* input_data, const int* input_dims,
const int input_num_dims, T* output_data,
const int* output_dims, const int output_num_dims,
const int* axis, const int num_axis_dimensions, bool keep_dims,
int* temp_index, int* resolved_axis, U* temp_sum) {
ruy::profiler::ScopeLabel label("Mean");
// Reset output data.
size_t num_outputs = 1;
for (int idx = 0; idx < output_num_dims; ++idx) {
size_t current = static_cast<size_t>(output_dims[idx]);
// Overflow prevention.
if (num_outputs > std::numeric_limits<size_t>::max() / current) {
return false;
}
num_outputs *= current;
}
for (size_t idx = 0; idx < num_outputs; ++idx) {
output_data[idx] = T();
temp_sum[idx] = U();
}
// Resolve axis.
int num_resolved_axis = 0;
if (!ResolveAxis(input_num_dims, axis, num_axis_dimensions, resolved_axis,
&num_resolved_axis)) {
return false;

which then initializes the variable in

inline bool ResolveAxis(const int num_dims, const int* axis,
const int64_t num_axis, int* out_axis,
int* out_num_axis) {

It may be worth adding -Wno-maybe-uninitialized to the flags for TFLM in your repo.

For completeness, here are the flags that the TFLM repo uses:

gcc on x86:

CC_WARNINGS := \
-Wsign-compare \
-Wdouble-promotion \
-Wshadow \
-Wunused-variable \
-Wmissing-field-initializers \
-Wunused-function \
-Wswitch \
-Wvla \
-Wall \
-Wextra \
-Wstrict-aliasing \
-Wno-unused-parameter
COMMON_FLAGS := \
-Werror \

clang on x86:

def micro_copts():
return [
"-Wall",
"-Werror",
"-DFLATBUFFERS_LOCALE_INDEPENDENT=0",
]

@tcal-x
Copy link
Author

tcal-x commented Aug 20, 2021

Thank you for the detailed answer, @advaitjain . We are currently manually copying the directories and files that we need from TFLM; we should switch over to the new support.

FYI @alanvgreen

@tcal-x tcal-x closed this as completed Aug 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants