COMPMID-765: Sanitize permutation vector for Permute.

If permutation vector is bigger than the tensorshape to permute then
infer dimensions of size one for the extra dimensions.

Change-Id: I5addb292f770d925f47f756902e16073039e8f71
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/120473
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Stefana Simion <stefana.simion@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/arm_compute/core/Helpers.h b/arm_compute/core/Helpers.h
index c6a7db4..63fad1d 100644
--- a/arm_compute/core/Helpers.h
+++ b/arm_compute/core/Helpers.h
@@ -508,10 +508,28 @@
 template <typename T>
 inline void permute(Dimensions<T> &dimensions, const PermutationVector &perm)
 {
-    auto copy_dimensions = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
+    auto dimensions_copy = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
     for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
     {
-        dimensions[i] = copy_dimensions[perm[i]];
+        T dimension_val = (perm[i] < dimensions.num_dimensions()) ? dimensions_copy[perm[i]] : 0;
+        dimensions.set(i, dimension_val);
+    }
+}
+
+/** Permutes given TensorShape according to a permutation vector
+ *
+ * @warning Validity of permutation is not checked
+ *
+ * @param[in, out] shape Shape to permute
+ * @param[in]      perm  Permutation vector
+ */
+inline void permute(TensorShape &shape, const PermutationVector &perm)
+{
+    auto shape_copy = utility::make_array<TensorShape::num_max_dimensions>(shape.begin(), shape.end());
+    for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
+    {
+        size_t dimension_val = (perm[i] < shape.num_dimensions()) ? shape_copy[perm[i]] : 1;
+        shape.set(i, dimension_val);
     }
 }
 
diff --git a/src/core/CPP/kernels/CPPPermuteKernel.cpp b/src/core/CPP/kernels/CPPPermuteKernel.cpp
index 298c700..6b3e855 100644
--- a/src/core/CPP/kernels/CPPPermuteKernel.cpp
+++ b/src/core/CPP/kernels/CPPPermuteKernel.cpp
@@ -44,12 +44,7 @@
                                                          DataType::U16, DataType::S16, DataType::QS16,
                                                          DataType::U32, DataType::S32,
                                                          DataType::F16, DataType::F32);
-    ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() < 3, "Invalid input size!");
-    ARM_COMPUTE_RETURN_ERROR_ON_MSG(
-        (perm.num_dimensions() != 3 && ((perm[0] != 2 && perm[1] != 0 && perm[2] != 1) || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))) && (perm.num_dimensions() != 4 && ((perm[0] != 2 && perm[1] != 0
-                && perm[2] != 1)
-                || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))),
-        "Only [2, 0, 1],[1, 2, 0] and [3, 2, 0, 1] permutation is supported");
+    ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() > 4, "Only up to 4D permutation vectors are supported");
 
     const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
 
@@ -70,7 +65,8 @@
     const auto old_dim = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end());
     for(unsigned int i = 0; i < perm.num_dimensions(); ++i)
     {
-        dimensions[perm[i]] = old_dim[i];
+        T dimension_val = (perm[i] < dimensions.num_dimensions()) ? old_dim[i] : 0;
+        dimensions.set(perm[i], dimension_val);
     }
 }
 
@@ -79,20 +75,23 @@
 template <typename T>
 void CPPPermuteKernel::run_permute(const Window &window)
 {
+    // Permute strides
     Strides strides      = _output->info()->strides_in_bytes();
     Strides perm_strides = strides;
     permute_strides(perm_strides, _perm);
-    const int               output_stride_w = strides[3];
+
+    // Create output window
     Window                  window_out(window);
     const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
     for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
     {
         window_out.set(d, zero_window);
     }
+
     // Create iterators
     Iterator in(_input, window);
     Iterator out(_output, window_out);
-    ARM_COMPUTE_ERROR_ON(_perm.num_dimensions() > _input->info()->num_dimensions());
+
     if(_input->info()->num_dimensions() <= 3)
     {
         execute_window_loop(window, [&](const Coordinates & id)
@@ -104,26 +103,12 @@
     }
     else if(_input->info()->num_dimensions() >= 4)
     {
-        if(_perm.num_dimensions() < _input->info()->num_dimensions())
+        execute_window_loop(window, [&](const Coordinates & id)
         {
-            // special case: perm.size = 3 and tensor size > 3, _perm[3] would be invalid so we handle this with id[3] * output_stride_w instead of id[_perm[3]]
-            ARM_COMPUTE_ERROR_ON(_perm.num_dimensions() < 3);
-            execute_window_loop(window, [&](const Coordinates & id)
-            {
-                const int idx                             = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * output_stride_w;
-                *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
-            },
-            in, out);
-        }
-        else
-        {
-            execute_window_loop(window, [&](const Coordinates & id)
-            {
-                const int idx                             = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_strides[3];
-                *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
-            },
-            in, out);
-        }
+            const int idx                             = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_strides[3];
+            *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
+        },
+        in, out);
     }
 }
 
diff --git a/src/core/NEON/kernels/NEPermuteKernel.cpp b/src/core/NEON/kernels/NEPermuteKernel.cpp
index f5f276f..87e5e74 100644
--- a/src/core/NEON/kernels/NEPermuteKernel.cpp
+++ b/src/core/NEON/kernels/NEPermuteKernel.cpp
@@ -49,7 +49,6 @@
                                                          DataType::U16, DataType::S16, DataType::QS16,
                                                          DataType::U32, DataType::S32,
                                                          DataType::F16, DataType::F32);
-    ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() < 3, "Invalid input size!");
     ARM_COMPUTE_RETURN_ERROR_ON_MSG(
         (perm.num_dimensions() != 3 && ((perm[0] != 2 && perm[1] != 0 && perm[2] != 1) || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))),
         "Only [2, 0, 1] and [1, 2, 0] permutation is supported");
diff --git a/tests/datasets/DepthwiseConvolutionLayerDataset.h b/tests/datasets/DepthwiseConvolutionLayerDataset.h
index f1dfb98..629217a 100644
--- a/tests/datasets/DepthwiseConvolutionLayerDataset.h
+++ b/tests/datasets/DepthwiseConvolutionLayerDataset.h
@@ -154,6 +154,7 @@
 public:
     SmallDepthwiseConvolutionLayerDataset3x3()
     {
+        add_config(TensorShape(3U, 3U, 2U), TensorShape(3U, 3U, 2U), TensorShape(1U, 1U, 2U), PadStrideInfo(1, 1, 0, 0));
         add_config(TensorShape(7U, 7U, 3U, 2U), TensorShape(3U, 3U, 3U), TensorShape(5U, 5U, 3U, 2U), PadStrideInfo(1, 1, 0, 0));
         add_config(TensorShape(33U, 27U, 11U), TensorShape(3U, 3U, 11U), TensorShape(11U, 14U, 11U), PadStrideInfo(3, 2, 1, 1));
         add_config(TensorShape(21U, 31U, 9U, 4U), TensorShape(3U, 3U, 9U), TensorShape(21U, 15U, 9U, 4U), PadStrideInfo(1, 2, 1, 0));
diff --git a/tests/validation/CPP/Permute.cpp b/tests/validation/CPP/Permute.cpp
index 3341da3..0a97041 100644
--- a/tests/validation/CPP/Permute.cpp
+++ b/tests/validation/CPP/Permute.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 ARM Limited.
+ * Copyright (c) 2017-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -42,7 +42,7 @@
 {
 namespace
 {
-const auto PermuteParametersSmall = combine(datasets::Small4DShapes(),
+const auto PermuteParametersSmall = combine(concat(concat(datasets::Small2DShapes(), datasets::Small3DShapes()), datasets::Small4DShapes()),
                                             framework::dataset::make("PermutationVector", { PermutationVector(2U, 0U, 1U), PermutationVector(1U, 2U, 0U), PermutationVector(3U, 2U, 0U, 1U) }));
 const auto PermuteParametersLarge = combine(datasets::Large4DShapes(),
                                             framework::dataset::make("PermutationVector", { PermutationVector(2U, 0U, 1U), PermutationVector(1U, 2U, 0U), PermutationVector(3U, 2U, 0U, 1U) }));
diff --git a/tests/validation/NEON/DepthwiseConvolutionLayer.cpp b/tests/validation/NEON/DepthwiseConvolutionLayer.cpp
index 47e8896..0cdd4c0 100644
--- a/tests/validation/NEON/DepthwiseConvolutionLayer.cpp
+++ b/tests/validation/NEON/DepthwiseConvolutionLayer.cpp
@@ -82,8 +82,11 @@
     validate(bias.info()->valid_region(), bias_valid_region);
 
     // Validate padding
-    const int         step    = 16 >> info.stride().first;
-    const PaddingSize padding = PaddingCalculator(output_shape.x(), step).required_padding();
+    bool              is_optimized_run = NEDepthwiseConvolutionLayer3x3Kernel::is_optimized_execution_possible(input_shape, info, data_type, DataLayout::NCHW);
+    const int         step_non_opt_dwc = 16 >> info.stride().first;
+    const int         step_bias_add    = 16 / src.info()->element_size();
+    const int         step             = is_optimized_run ? step_bias_add : std::max(step_non_opt_dwc, step_bias_add);
+    const PaddingSize padding          = PaddingCalculator(output_shape.x(), step).required_padding();
     validate(dst.info()->padding(), padding);
 }
 
diff --git a/tests/validation/NEON/Permute.cpp b/tests/validation/NEON/Permute.cpp
index 004aa82..7451d9e 100644
--- a/tests/validation/NEON/Permute.cpp
+++ b/tests/validation/NEON/Permute.cpp
@@ -42,7 +42,7 @@
 {
 namespace
 {
-const auto PermuteParametersSmall = combine(datasets::Small4DShapes(),
+const auto PermuteParametersSmall = combine(concat(concat(datasets::Small2DShapes(), datasets::Small3DShapes()), datasets::Small4DShapes()),
                                             framework::dataset::make("PermutationVector", { PermutationVector(2U, 0U, 1U), PermutationVector(1U, 2U, 0U) }));
 const auto PermuteParametersLarge = combine(datasets::Large4DShapes(),
                                             framework::dataset::make("PermutationVector", { PermutationVector(2U, 0U, 1U), PermutationVector(1U, 2U, 0U) }));