COMPMID-864 Window::collapse_if_possible() is misused in several CL kernels

Removed unnecessary collapse_if_possible() calls.

Change-Id: I6f3434bc4a26470c4de5bac4e3d90b4b019c2c9c
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/117993
Reviewed-by: Michalis Spyrou <michalis.spyrou@arm.com>
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
diff --git a/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h b/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h
index 1309240..a24ddb1 100644
--- a/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h
+++ b/arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -51,8 +51,8 @@
     ~CLQuantizationLayerKernel() = default;
     /** Set the input, output, min and max.
      *
-     * @param[in]  input   Source tensor. Data types supported: F32.
-     * @param[out] output  Destination tensor. Data types supported: U8.
+     * @param[in]  input   Source tensor with at least 3 dimensions. The dimensions over the third will be interpreted as batches. Data types supported: F32.
+     * @param[out] output  Destination tensor with the same dimensions of input. Output data type must be U8.
      * @param[in]  min_max Pointer to the tensor with shape [2, batches] which stores the minimum and maximum value for each 3D input tensor.
      *                     The dimensions over the second must match the batched dimensions of the input tensor. Data type supported: F32.
      */
diff --git a/arm_compute/core/Window.h b/arm_compute/core/Window.h
index cca12c9..5ca210a 100644
--- a/arm_compute/core/Window.h
+++ b/arm_compute/core/Window.h
@@ -177,6 +177,16 @@
      */
     void shift(size_t dimension, int shift_value);
 
+    /** Shift down all the dimensions of a window
+     *
+     * i.e new_dims[n] = old_dims[n+shift_value].
+     *
+     * @param[in] shift_value Number of dimensions to shift the window by.
+     *
+     * @return The window with the shifted dimensions.
+     */
+    Window shift_dimensions(unsigned int shift_value) const;
+
     /** Adjust the start or end of a given dimension by the given value
      *
      * @param[in] dimension    The dimension to adjust
diff --git a/arm_compute/core/Window.inl b/arm_compute/core/Window.inl
index 23b2a8e..18d454a 100644
--- a/arm_compute/core/Window.inl
+++ b/arm_compute/core/Window.inl
@@ -77,6 +77,16 @@
     return collapsed;
 }
 
+inline Window Window::shift_dimensions(unsigned int shift_value) const
+{
+    Window shifted_window;
+    for(size_t n = 0; n < (Coordinates::num_max_dimensions - shift_value); n++)
+    {
+        shifted_window.set(n, _dims[n + shift_value]);
+    }
+    return shifted_window;
+}
+
 inline Window Window::collapse(const Window &full_window, const size_t first, const size_t last) const
 {
     bool   has_collapsed = false;
diff --git a/src/core/CL/kernels/CLDequantizationLayerKernel.cpp b/src/core/CL/kernels/CLDequantizationLayerKernel.cpp
index 216fa27..4efdb76 100644
--- a/src/core/CL/kernels/CLDequantizationLayerKernel.cpp
+++ b/src/core/CL/kernels/CLDequantizationLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -97,5 +97,5 @@
         add_1D_tensor_argument(idx, _min_max, min_max_slice);
         enqueue(queue, *this, slice);
     }
-    while(window.slide_window_slice_3D(slice) && min_max_window.slide_window_slice_1D(min_max_slice));
+    while(window_collapsed.slide_window_slice_3D(slice) && min_max_window.slide_window_slice_1D(min_max_slice));
 }
diff --git a/src/core/CL/kernels/CLMinMaxLayerKernel.cpp b/src/core/CL/kernels/CLMinMaxLayerKernel.cpp
index 9b4533b..8ba1f77 100644
--- a/src/core/CL/kernels/CLMinMaxLayerKernel.cpp
+++ b/src/core/CL/kernels/CLMinMaxLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -89,7 +89,6 @@
     Window window_output;
     window_output.use_tensor_dimensions(_output->info()->tensor_shape());
     window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
-    window_output.collapse_if_possible(ICLKernel::window(), 1);
 
     Iterator output(_output, window_output);
 
@@ -110,27 +109,21 @@
     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
 
-    // Collapse min/max batches
     Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
     Window slice            = window_collapsed.first_slice_window_3D();
     slice.set(Window::DimX, Window::Dimension(0, 1, 1));
     slice.set(Window::DimY, Window::Dimension(0, 1, 1));
     slice.set(Window::DimZ, Window::Dimension(0, 1, 1));
 
-    Window window_output;
-    window_output.use_tensor_dimensions(_output->info()->tensor_shape());
-    window_output.set(Window::DimX, Window::Dimension(0, 1, 1));
-    window_output.collapse_if_possible(ICLKernel::window(), 1);
-
-    Window output_slice = window_output.first_slice_window_1D();
-
     do
     {
+        Window output_slice = slice.shift_dimensions(2);
+
         unsigned int idx = 0;
         // Set inputs
         add_3D_tensor_argument(idx, _input, slice);
         add_1D_tensor_argument(idx, _output, output_slice);
         enqueue(queue, *this, slice);
     }
-    while(window.slide_window_slice_3D(slice) && window_output.slide_window_slice_1D(output_slice));
+    while(window_collapsed.slide_window_slice_3D(slice));
 }
diff --git a/src/core/CL/kernels/CLQuantizationLayerKernel.cpp b/src/core/CL/kernels/CLQuantizationLayerKernel.cpp
index 4756443..8b082a8 100644
--- a/src/core/CL/kernels/CLQuantizationLayerKernel.cpp
+++ b/src/core/CL/kernels/CLQuantizationLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -82,20 +82,16 @@
     Window window_collapsed = window.collapse_if_possible(ICLKernel::window(), 3);
     Window slice            = window_collapsed.first_slice_window_3D();
 
-    Window window_min_max;
-    window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape());
-    window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1));
-    window_min_max.collapse_if_possible(ICLKernel::window(), 1);
-
-    Window slice_min_max = window_min_max.first_slice_window_1D();
-
     do
     {
+        Window slice_min_max = slice.shift_dimensions(2);
+        slice_min_max.set(Window::DimX, Window::Dimension(0, 1, 1));
+
         unsigned int idx = 0;
         add_3D_tensor_argument(idx, _input, slice);
         add_3D_tensor_argument(idx, _output, slice);
         add_1D_tensor_argument(idx, _min_max, slice_min_max);
         enqueue(queue, *this, slice);
     }
-    while(window.slide_window_slice_3D(slice) && window_min_max.slide_window_slice_1D(slice_min_max));
+    while(window_collapsed.slide_window_slice_3D(slice));
 }
diff --git a/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp b/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp
index 70984f0..be211b2 100644
--- a/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp
+++ b/src/core/NEON/kernels/NEDequantizationLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -78,13 +78,11 @@
     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
 
     Window window_input_output(window);
-    window_input_output.collapse_if_possible(INEKernel::window(), 3);
     window_input_output.set(3, Window::Dimension(0, 1, 1));
 
     Window window_min_max;
     window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape());
     window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1));
-    window_min_max.collapse_if_possible(INEKernel::window(), 1);
 
     Iterator input(_input, window_input_output);
     Iterator output(_output, window_input_output);
diff --git a/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp b/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp
index a81725f..01be36b 100644
--- a/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp
+++ b/src/core/NEON/kernels/NEMinMaxLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -96,7 +96,6 @@
     // First one will use vector operations, second one processes the left over pixels
     Window window_input(window);
     window_input.set(Window::DimX, Window::Dimension(0, 1, 1));
-    window_input.collapse_if_possible(INEKernel::window(), 3);
     window_input.set(3, Window::Dimension(0, 1, 1));
 
     Iterator input(_input, window_input);
diff --git a/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp b/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp
index bff79f0..767af08 100644
--- a/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp
+++ b/src/core/NEON/kernels/NEQuantizationLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -77,13 +77,11 @@
     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
 
     Window window_input_output(window);
-    window_input_output.collapse_if_possible(INEKernel::window(), 3);
     window_input_output.set(3, Window::Dimension(0, 1, 1));
 
     Window window_min_max;
     window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape());
     window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1));
-    window_min_max.collapse_if_possible(INEKernel::window(), 1);
 
     Iterator input(_input, window_input_output);
     Iterator output(_output, window_input_output);
diff --git a/src/core/NEON/kernels/NEReshapeLayerKernel.cpp b/src/core/NEON/kernels/NEReshapeLayerKernel.cpp
index a0f324e..45ba68d 100644
--- a/src/core/NEON/kernels/NEReshapeLayerKernel.cpp
+++ b/src/core/NEON/kernels/NEReshapeLayerKernel.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -46,7 +46,6 @@
     const TensorShape &output_shape = output->info()->tensor_shape();
     Coordinates        output_coord{};
 
-    window.collapse_if_possible(window, 3);
     Iterator in(input, window);
 
     execute_window_loop(window, [&](const Coordinates & id)