Merge pull request #31279 from ish1416/fix-flatten-axis-stale-4x

FlattenLayerImpl::finalize previously overwrote _startAxis and _endAxis in place with normalize_axis(..., numAxes) for the rank of the first input. Because end_axis defaults to -1 (and axis can also be negative), this permanently replaced relative negative axes with positive indices bound to the first input rank.

Reusing the same Flatten layer with an input of a different rank then failed in two ways:

Increasing rank (e.g., default axis=1, end_axis=-1: 3D [2, 3, 4] followed by 4D [2, 3, 4, 5]): silently flattened only axes [1, 2] and returned shape [2, 12, 5] instead of [2, 60].
Decreasing rank (e.g., 4D [2, 3, 4, 5] followed by 3D [2, 3, 4]): threw an assertion error in normalize_axis because the stale _endAxis = 3 was out of range for a 3D input.
Fix
Keep _startAxis and _endAxis unmodified in FlattenLayerImpl::finalize, while still validating the normalized axis range (0 <= startAxis <= endAxis < numAxes).
All backends already handle raw axis attributes cleanly: getMemoryShapes, initCann, and initNgraph normalize _startAxis and _endAxis into local variables, and forward, forward_ocl, and initCUDA operate directly on the output shapes.
This commit is contained in:
Abhishek Gola
2026-12-01 19:21:27 +05:30
committed by GitHub
2 changed files with 125 additions and 2 deletions

View File

@@ -123,10 +123,12 @@ public:
{
std::vector<Mat> inputs;
inputs_arr.getMatVector(inputs);
CV_Assert(!inputs.empty());
int numAxes = inputs[0].dims;
_startAxis = normalize_axis(_startAxis, numAxes);
_endAxis = normalize_axis(_endAxis, numAxes);
int startAxis = normalize_axis(_startAxis, numAxes);
int endAxis = normalize_axis(_endAxis, numAxes);
CV_Assert(startAxis >= 0 && endAxis >= startAxis && endAxis < numAxes);
}
#ifdef HAVE_OPENCL

View File

@@ -3551,4 +3551,125 @@ TEST(Layer_Test_TopK_Axis, keepsNegativeAxisDecreasingRank)
normAssert(outputs[1], Mat(2, 1, CV_32F, expected_idx));
}
TEST(Layer_Test_Flatten_Axis, keepsNegativeEndAxisIncreasingRank)
{
LayerParams lp;
lp.type = "Flatten";
lp.name = "test_flatten_end_inc";
lp.set("axis", 1);
lp.set("end_axis", -1);
Ptr<Layer> layer = LayerFactory::createLayerInstance(lp.type, lp);
ASSERT_TRUE(layer);
const int sz3[] = {2, 3, 4};
Mat inp3(3, sz3, CV_32F);
float* p3 = inp3.ptr<float>();
for (size_t i = 0; i < inp3.total(); ++i)
p3[i] = static_cast<float>(i);
// First run: 3D input [2, 3, 4], end_axis -1 is axis 2 -> [2, 12]
std::vector<Mat> inputs{inp3.clone()}, outputs;
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 12));
normAssert(outputs[0], inp3.reshape(1, {2, 12}));
const int sz4[] = {2, 3, 4, 5};
Mat inp4(4, sz4, CV_32F);
float* p4 = inp4.ptr<float>();
for (size_t i = 0; i < inp4.total(); ++i)
p4[i] = static_cast<float>(i);
// Second run on the same layer: 4D input [2, 3, 4, 5], end_axis -1 must now be axis 3 -> [2, 60]
inputs = {inp4.clone()};
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 60));
normAssert(outputs[0], inp4.reshape(1, {2, 60}));
}
TEST(Layer_Test_Flatten_Axis, keepsNegativeEndAxisDecreasingRank)
{
LayerParams lp;
lp.type = "Flatten";
lp.name = "test_flatten_end_dec";
lp.set("axis", 1);
lp.set("end_axis", -1);
Ptr<Layer> layer = LayerFactory::createLayerInstance(lp.type, lp);
ASSERT_TRUE(layer);
const int sz4[] = {2, 3, 4, 5};
Mat inp4(4, sz4, CV_32F);
float* p4 = inp4.ptr<float>();
for (size_t i = 0; i < inp4.total(); ++i)
p4[i] = static_cast<float>(i);
// First run: 4D input [2, 3, 4, 5], end_axis -1 is axis 3 -> [2, 60]
std::vector<Mat> inputs{inp4.clone()}, outputs;
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 60));
normAssert(outputs[0], inp4.reshape(1, {2, 60}));
const int sz3[] = {2, 3, 4};
Mat inp3(3, sz3, CV_32F);
float* p3 = inp3.ptr<float>();
for (size_t i = 0; i < inp3.total(); ++i)
p3[i] = static_cast<float>(i);
// Second run on the same layer: 3D input [2, 3, 4], end_axis -1 must now be axis 2 -> [2, 12]
inputs = {inp3.clone()};
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 12));
normAssert(outputs[0], inp3.reshape(1, {2, 12}));
}
TEST(Layer_Test_Flatten_Axis, keepsNegativeStartAndEndAxisAcrossRanks)
{
LayerParams lp;
lp.type = "Flatten";
lp.name = "test_flatten_start_end";
lp.set("axis", -2);
lp.set("end_axis", -1);
Ptr<Layer> layer = LayerFactory::createLayerInstance(lp.type, lp);
ASSERT_TRUE(layer);
const int sz3[] = {2, 3, 4};
Mat inp3(3, sz3, CV_32F);
float* p3 = inp3.ptr<float>();
for (size_t i = 0; i < inp3.total(); ++i)
p3[i] = static_cast<float>(i);
// First run: 3D input [2, 3, 4], axes [-2, -1] -> [1, 2] -> [2, 12]
std::vector<Mat> inputs{inp3.clone()}, outputs;
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 12));
normAssert(outputs[0], inp3.reshape(1, {2, 12}));
const int sz4[] = {2, 3, 4, 5};
Mat inp4(4, sz4, CV_32F);
float* p4 = inp4.ptr<float>();
for (size_t i = 0; i < inp4.total(); ++i)
p4[i] = static_cast<float>(i);
// Second run: 4D input [2, 3, 4, 5], axes [-2, -1] -> [2, 3] -> [2, 3, 20]
inputs = {inp4.clone()};
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 3, 20));
normAssert(outputs[0], inp4.reshape(1, {2, 3, 20}));
// Third run: back to 3D input [2, 3, 4], axes [-2, -1] -> [1, 2] -> [2, 12]
inputs = {inp3.clone()};
runLayer(layer, inputs, outputs);
ASSERT_EQ(outputs.size(), (size_t)1);
EXPECT_EQ(shape(outputs[0]), shape(2, 12));
normAssert(outputs[0], inp3.reshape(1, {2, 12}));
}
}} // namespace