COMPMID-1726: Implement CLUnstack.

Change-Id: I94b0707d19757c5f5d7ca66d9c47e378867126a3
Reviewed-on: https://review.mlplatform.org/325
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Georgios Pinitas <georgios.pinitas@arm.com>
diff --git a/tests/validation/CL/Unstack.cpp b/tests/validation/CL/Unstack.cpp
new file mode 100644
index 0000000..13b8872
--- /dev/null
+++ b/tests/validation/CL/Unstack.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/CL/CLTensor.h"
+#include "arm_compute/runtime/CL/CLTensorAllocator.h"
+#include "arm_compute/runtime/CL/functions/CLUnstack.h"
+
+#include "tests/CL/CLAccessor.h"
+#include "tests/datasets/ShapeDatasets.h"
+#include "tests/framework/Asserts.h"
+#include "tests/framework/Macros.h"
+#include "tests/framework/datasets/Datasets.h"
+#include "tests/validation/Validation.h"
+#include "tests/validation/fixtures/UnstackFixture.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace validation
+{
+namespace
+{
+const auto unstack_axis_dataset  = framework::dataset::make("Axis", -3, 3);
+const auto unstack_num_dataset   = framework::dataset::make("Num", 1, 3); // The length of the dimension axis
+const auto unstack_dataset_small = datasets::Small3DShapes() * unstack_axis_dataset * unstack_num_dataset;
+} //namespace
+
+TEST_SUITE(CL)
+TEST_SUITE(Unstack)
+
+DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip(
+                                                                      framework::dataset::make("InputInfo",
+{
+    TensorInfo(TensorShape(1U, 9U, 8U), 1, DataType::U8),   // Passes, 1 slice on x axis
+    TensorInfo(TensorShape(1U, 2U, 3U), 1, DataType::U8),   // fails because axis > input's rank
+    TensorInfo(TensorShape(1U, 2U, 3U), 1, DataType::S32),  // fails axis <  (- input's rank)
+    TensorInfo(TensorShape(3U, 7U, 5U), 1, DataType::S32),  // passes, 3 slices along X
+    TensorInfo(TensorShape(13U, 7U, 5U), 1, DataType::S16), // fails, too few output slices
+    TensorInfo(TensorShape(1U, 2U, 3U), 1, DataType::U8),   // fails mismatching data types
+}),
+framework::dataset::make("OutputInfo",
+{
+    std::vector<TensorInfo>{ TensorInfo(TensorShape(9U, 8U), 1, DataType::U8) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(2U, 3U), 1, DataType::U8) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(2U, 3U), 1, DataType::S32) },
+
+    std::vector<TensorInfo>{ TensorInfo(TensorShape(7U, 5U), 1, DataType::S32), TensorInfo(TensorShape(7U, 5U), 1, DataType::S32), TensorInfo(TensorShape(7U, 5U), 1, DataType::S32) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(7U, 5U), 1, DataType::S16) }, std::vector<TensorInfo>{ TensorInfo(TensorShape(9U, 8U), 1, DataType::S32) },
+})),
+framework::dataset::make("Axis", { -3, 3, -4, -3, 1, 1 })),
+framework::dataset::make("Num", { 1, 1, 1, 1, 0, 1 })),
+framework::dataset::make("Expected", { true, false, false, true, false, false })),
+input_info, output_info, axis, num, expected)
+{
+    std::vector<TensorInfo>    ti(output_info);
+    std::vector<ITensorInfo *> vec(num);
+    for(size_t j = 0; j < vec.size(); ++j)
+    {
+        vec[j] = &ti[j];
+    }
+    ARM_COMPUTE_EXPECT(bool(CLUnstack::validate(&input_info.clone()->set_is_resizable(false), vec, axis)) == expected, framework::LogLevel::ERRORS);
+}
+
+template <typename T>
+using CLUnstackFixture = UnstackValidationFixture<CLTensor, ICLTensor, CLAccessor, CLUnstack, T>;
+
+TEST_SUITE(F32)
+FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture<float>, framework::DatasetMode::PRECOMMIT, unstack_dataset_small * framework::dataset::make("DataType", { DataType::F32 }))
+{
+    ARM_COMPUTE_ERROR_ON(_target.size() != _reference.size());
+    // Validate output
+    for(size_t k = 0; k < _target.size(); ++k)
+    {
+        validate(CLAccessor(_target[k]), _reference[k]);
+    }
+}
+TEST_SUITE_END() // F32
+
+TEST_SUITE(F16)
+FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture<half>, framework::DatasetMode::PRECOMMIT, unstack_dataset_small * framework::dataset::make("DataType", { DataType::F16 }))
+{
+    ARM_COMPUTE_ERROR_ON(_target.size() != _reference.size());
+    // Validate output
+    for(size_t k = 0; k < _target.size(); ++k)
+    {
+        validate(CLAccessor(_target[k]), _reference[k]);
+    }
+}
+TEST_SUITE_END() // F16
+
+TEST_SUITE(Quantized)
+FIXTURE_DATA_TEST_CASE(RunSmall, CLUnstackFixture<uint8_t>, framework::DatasetMode::PRECOMMIT, unstack_dataset_small * framework::dataset::make("DataType", { DataType::QASYMM8 }))
+{
+    ARM_COMPUTE_ERROR_ON(_target.size() != _reference.size());
+    // Validate output
+    for(size_t k = 0; k < _target.size(); ++k)
+    {
+        validate(CLAccessor(_target[k]), _reference[k]);
+    }
+}
+TEST_SUITE_END() // QASYMM8
+
+TEST_SUITE_END() // Unstack
+TEST_SUITE_END() // CL
+} // namespace validation
+} // namespace test
+} // namespace arm_compute
diff --git a/tests/validation/fixtures/UnstackFixture.h b/tests/validation/fixtures/UnstackFixture.h
new file mode 100644
index 0000000..f20a128
--- /dev/null
+++ b/tests/validation/fixtures/UnstackFixture.h
@@ -0,0 +1,118 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef ARM_COMPUTE_TEST_UNSTACK_FIXTURE
+#define ARM_COMPUTE_TEST_UNSTACK_FIXTURE
+
+#include "arm_compute/core/TensorShape.h"
+#include "arm_compute/core/Types.h"
+#include "arm_compute/core/utils/misc/ShapeCalculator.h"
+#include "tests/AssetsLibrary.h"
+#include "tests/Globals.h"
+#include "tests/IAccessor.h"
+#include "tests/framework/Asserts.h"
+#include "tests/framework/Fixture.h"
+#include "tests/validation/Helpers.h"
+#include "tests/validation/reference/Unstack.h"
+
+#include <random>
+
+namespace arm_compute
+{
+namespace test
+{
+namespace validation
+{
+template <typename TensorType, typename ITensorType, typename AccessorType, typename FunctionType, typename T>
+class UnstackValidationFixture : public framework::Fixture
+{
+public:
+    template <typename...>
+    void setup(TensorShape input_shape, int axis, int num, DataType data_type)
+    {
+        _target    = compute_target(input_shape, axis, num, data_type);
+        _reference = compute_reference(input_shape, axis, num, data_type);
+    }
+
+protected:
+    template <typename U>
+    void fill(U &&tensor, int i)
+    {
+        library->fill_tensor_uniform(tensor, i);
+    }
+
+    std::vector<TensorType> compute_target(TensorShape input_shape, int axis, unsigned int num, DataType data_type)
+    {
+        TensorType                 input_tensor = create_tensor<TensorType>(input_shape, data_type);
+        const unsigned int         axis_u       = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
+        const unsigned int         axis_size    = input_shape[axis_u];
+        const unsigned int         num_slices   = std::min(num, axis_size);
+        std::vector<TensorType>    output_slices(num_slices);
+        std::vector<ITensorType *> output_ptrs(num_slices);
+        for(size_t k = 0; k < num_slices; ++k)
+        {
+            output_ptrs[k] = &output_slices[k];
+        }
+        // Create and configure function
+        FunctionType unstack;
+        unstack.configure(&input_tensor, output_ptrs, axis);
+        // Allocate tensors
+        for(auto &out : output_slices)
+        {
+            out.allocator()->allocate();
+            ARM_COMPUTE_EXPECT(!out.info()->is_resizable(), framework::LogLevel::ERRORS);
+        }
+        input_tensor.allocator()->allocate();
+        ARM_COMPUTE_EXPECT(!input_tensor.info()->is_resizable(), framework::LogLevel::ERRORS);
+        fill(AccessorType(input_tensor), 0);
+        // Compute function
+        unstack.run();
+        return output_slices;
+    }
+
+    std::vector<SimpleTensor<T>> compute_reference(TensorShape input_shape, int axis, unsigned int num, DataType data_type)
+    {
+        const unsigned int axis_u             = wrap_around(axis, static_cast<int>(input_shape.num_dimensions()));
+        const unsigned int axis_size          = input_shape[axis_u];
+        const unsigned int num_output_tensors = (num == 0) ? axis_size : std::min(axis_size, num);
+        // create and fill input tensor
+        SimpleTensor<T> input_tensor{ input_shape, data_type };
+        fill(input_tensor, 0);
+        // create output tensors
+        const TensorShape            slice_shape = arm_compute::misc::shape_calculator::calculate_unstack_shape(input_shape, axis_u);
+        std::vector<SimpleTensor<T>> output_tensors(num_output_tensors);
+        for(size_t k = 0; k < num_output_tensors; ++k)
+        {
+            output_tensors[k] = SimpleTensor<T>(slice_shape, data_type);
+        }
+
+        return reference::unstack<T>(input_tensor, output_tensors, axis);
+    }
+
+    std::vector<TensorType>      _target{};
+    std::vector<SimpleTensor<T>> _reference{};
+};
+} // namespace validation
+} // namespace test
+} // namespace arm_compute
+#endif /* ARM_COMPUTE_TEST_UNSTACK_FIXTURE */
diff --git a/tests/validation/reference/Unstack.cpp b/tests/validation/reference/Unstack.cpp
new file mode 100644
index 0000000..3474c15
--- /dev/null
+++ b/tests/validation/reference/Unstack.cpp
@@ -0,0 +1,108 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "Unstack.h"
+
+#include "tests/validation/Helpers.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace validation
+{
+namespace reference
+{
+namespace
+{
+inline Coordinates expand_coordinates(Coordinates in_coord, size_t axis, size_t slice, size_t num_dimensions)
+{
+    /*
+        Reconstruct input_coord to read the corresponding value from the correct slice. This is done by adding an extra dimension
+        to the coordinates and shuffling around the values based on the info below.
+
+        For example, if input tensor shape is (X, Y, Z, W);
+
+        If axis == 0, each slice will have the shape (Y, Z, W) and there will be X slices
+
+        If axis == 1, each slice will have the shape (X, Z, W) and there will be Y slices.
+    */
+    Coordinates expanded_coord;
+    expanded_coord.set_num_dimensions(num_dimensions);
+    expanded_coord.set(axis, slice);
+    for(size_t k = 0; k < axis; ++k)
+    {
+        expanded_coord.set(k, in_coord[k]);
+    }
+    for(size_t k = axis + 1; k < num_dimensions; ++k)
+    {
+        expanded_coord.set(k, in_coord[k - 1]);
+    }
+    return expanded_coord;
+}
+
+template <typename T>
+SimpleTensor<T> get_slice(const SimpleTensor<T> &input_tensor, size_t axis, size_t slice)
+{
+    TensorShape out_shape = input_tensor.shape();
+    out_shape.remove_dimension(axis);
+
+    const size_t unpacked_num_dimensions(input_tensor.shape().num_dimensions());
+
+    SimpleTensor<T> output{ out_shape, input_tensor.data_type() };
+
+    Window win;
+    win.use_tensor_dimensions(out_shape);
+    execute_window_loop(win, [&](const Coordinates & id)
+    {
+        const Coordinates input_coords     = expand_coordinates(id, axis, slice, unpacked_num_dimensions);
+        *reinterpret_cast<T *>(output(id)) = *reinterpret_cast<const T *>(input_tensor(input_coords));
+    });
+
+    return output;
+}
+} // namespace
+
+template <typename T>
+std::vector<SimpleTensor<T>> unstack(const SimpleTensor<T> &input_tensor, std::vector<SimpleTensor<T>> &output_tensors, int axis)
+{
+    // Wrap around negative values
+    const unsigned int axis_u = wrap_around(axis, static_cast<int>(input_tensor.shape().num_dimensions()));
+    ARM_COMPUTE_ERROR_ON(axis_u >= input_tensor.shape().num_dimensions());
+    for(size_t k = 0; k < output_tensors.size(); ++k)
+    {
+        SimpleTensor<T>      &output    = output_tensors[k];
+        const SimpleTensor<T> kth_slice = get_slice(input_tensor, axis_u, k);
+        output                          = copy_tensor<T>(kth_slice);
+    }
+    return output_tensors;
+}
+
+template std::vector<SimpleTensor<float>> unstack(const SimpleTensor<float> &input_tensor, std::vector<SimpleTensor<float>> &output_tensors, int axis);
+template std::vector<SimpleTensor<half>> unstack(const SimpleTensor<half> &input_tensor, std::vector<SimpleTensor<half>> &output_tensors, int axis);
+template std::vector<SimpleTensor<uint8_t>> unstack(const SimpleTensor<uint8_t> &input_tensor, std::vector<SimpleTensor<uint8_t>> &output_tensors, int axis);
+
+} // namespace reference
+} // namespace validation
+} // namespace test
+} // namespace arm_compute
diff --git a/tests/validation/reference/Unstack.h b/tests/validation/reference/Unstack.h
new file mode 100644
index 0000000..56e3778
--- /dev/null
+++ b/tests/validation/reference/Unstack.h
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef __ARM_COMPUTE_TEST_UNSTACK_H__
+#define __ARM_COMPUTE_TEST_UNSTACK_H__
+
+#include "tests/SimpleTensor.h"
+
+#include <vector>
+
+namespace arm_compute
+{
+namespace test
+{
+namespace validation
+{
+namespace reference
+{
+template <typename T>
+std::vector<SimpleTensor<T>> unstack(const SimpleTensor<T> &input_tensor, std::vector<SimpleTensor<T>> &output_tensors, int axis);
+
+} // namespace reference
+} // namespace validation
+} // namespace test
+} // namespace arm_compute
+#endif /* __ARM_COMPUTE_TEST_UNSTACK_H__ */