Skip to content

ReduceMean: loop variable typo in kFirst reduction path produces wrong results #17

Description

@harz05

I was going through ROperator_Reduce.hxx and noticed a typo on line 215 in the kFirst reduction path for ReduceMean.

The division loop after the sum uses i in the condition instead of j:

for (size_t j = 0; i < outputLength; j++) {
    tensor_Y[j] /= static_cast<float>(reducedLength);
}

At this point i is left over from the outer loop (line 202) which already finished at reducedLength. So , if reducedLength < outputLength, the condition i < outputLength is always true(inifinte loop). If reducedLength >= outputLength, the loop never runs and the output is a sum instead of a mean

Should be:

for (size_t j = 0; j < outputLength; j++)

The existing ReduceMean test doesn't catch this because it reduces axis 1 on shape [1, 2, 3], which takes the kMiddle path. This bug only shows up when reducing leading axes (e.g. axes=[0] on shape [3, 4]), which takes the kFirst path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions