aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/CL
diff options
context:
space:
mode:
authorPablo Tello <pablo.tello@arm.com>2018-11-22 16:14:36 +0000
committerGeorgios Pinitas <georgios.pinitas@arm.com>2018-12-21 18:28:31 +0000
commit543036904d5324c4738cdc8b586c301471046c8e (patch)
tree680efe9d7d02b23a0453495c13f4ece5d0a0edbd /src/runtime/CL
parent8b6b4a959a49127d64293f8b60265f0f5ed486d4 (diff)
downloadComputeLibrary-543036904d5324c4738cdc8b586c301471046c8e.tar.gz
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>
Diffstat (limited to 'src/runtime/CL')
-rw-r--r--src/runtime/CL/functions/CLUnstack.cpp120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/runtime/CL/functions/CLUnstack.cpp b/src/runtime/CL/functions/CLUnstack.cpp
new file mode 100644
index 0000000000..2c8a310043
--- /dev/null
+++ b/src/runtime/CL/functions/CLUnstack.cpp
@@ -0,0 +1,120 @@
+/*
+ * 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/runtime/CL/functions/CLUnstack.h"
+
+#include "arm_compute/core/CL/ICLTensor.h"
+#include "arm_compute/core/Error.h"
+#include "arm_compute/core/TensorInfo.h"
+#include "arm_compute/core/Types.h"
+#include "arm_compute/core/utils/misc/ShapeCalculator.h"
+
+namespace arm_compute
+{
+namespace
+{
+inline unsigned int wrap_axis(int axis, const ITensorInfo *const tensor)
+{
+ return wrap_around(axis, static_cast<int>(tensor->num_dimensions()));
+}
+
+inline void setup_slice_coordinates_and_mask(Coordinates &slice_start, int32_t &slice_end_mask, const unsigned int input_num_dimensions)
+{
+ // Setups up coordinates to slice the input tensor: start coordinates to all 0s and the unstacking axis of both Start/End to slice just one 2d tensor at a time.
+ Coordinates slice_end;
+ slice_start.set_num_dimensions(input_num_dimensions);
+ slice_end.set_num_dimensions(input_num_dimensions);
+ for(size_t k = 0; k < input_num_dimensions; ++k)
+ {
+ slice_start.set(k, 0);
+ slice_end.set(k, -1);
+ }
+ slice_end_mask = arm_compute::helpers::tensor_transform::construct_slice_end_mask(slice_end);
+}
+
+inline const std::vector<ITensorInfo *> get_tensor_info(const std::vector<ICLTensor *> &output_vector)
+{
+ // given a vector of tensors this function returns a vector to their ITensorInfo*
+ std::vector<ITensorInfo *> out_info(output_vector.size());
+ std::transform(output_vector.begin(), output_vector.end(), out_info.begin(), [](ICLTensor * t)
+ {
+ return t->info();
+ });
+ return out_info;
+}
+} // namespace
+
+CLUnstack::CLUnstack() // NOLINT
+ : _num_slices(0),
+ _strided_slice_vector()
+{
+}
+
+void CLUnstack::configure(const ICLTensor *input, const std::vector<ICLTensor *> &output_vector, int axis)
+{
+ ARM_COMPUTE_ERROR_ON_NULLPTR(input);
+ ARM_COMPUTE_ERROR_THROW_ON(CLUnstack::validate(input->info(), get_tensor_info(output_vector), axis));
+ // Wrap around negative values
+ const unsigned int axis_u = wrap_axis(axis, input->info());
+ _num_slices = std::min(output_vector.size(), input->info()->dimension(axis_u));
+ _strided_slice_vector = arm_compute::support::cpp14::make_unique<CLStridedSlice[]>(_num_slices);
+ Coordinates slice_start;
+ int32_t slice_end_mask;
+ setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->info()->tensor_shape().num_dimensions());
+ for(unsigned int slice = 0; slice < _num_slices; ++slice)
+ {
+ // Adjusts start and end coordinates to take a 2D slice at a time
+ slice_start.set(axis_u, slice);
+ _strided_slice_vector[slice].configure(input, output_vector[slice], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << axis_u));
+ }
+}
+
+Status CLUnstack::validate(const ITensorInfo *input, const std::vector<ITensorInfo *> &output_vector, int axis)
+{
+ ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
+ ARM_COMPUTE_RETURN_ERROR_ON(output_vector.empty());
+ ARM_COMPUTE_RETURN_ERROR_ON(axis < (-static_cast<int>(input->tensor_shape().num_dimensions())));
+ ARM_COMPUTE_RETURN_ERROR_ON(axis >= static_cast<int>(input->tensor_shape().num_dimensions()));
+ const unsigned int num_slices = std::min(output_vector.size(), input->dimension(wrap_axis(axis, input)));
+ ARM_COMPUTE_RETURN_ERROR_ON(num_slices > input->dimension(wrap_axis(axis, input)));
+ ARM_COMPUTE_RETURN_ERROR_ON(num_slices > output_vector.size());
+ Coordinates slice_start;
+ int32_t slice_end_mask;
+ for(size_t k = 0; k < num_slices; ++k)
+ {
+ slice_start.set(wrap_axis(axis, input), k);
+ setup_slice_coordinates_and_mask(slice_start, slice_end_mask, input->tensor_shape().num_dimensions());
+ ARM_COMPUTE_RETURN_ON_ERROR(CLStridedSlice::validate(input, output_vector[k], slice_start, Coordinates(), BiStrides(), 0, slice_end_mask, (1 << wrap_axis(axis, input))));
+ }
+ return Status{};
+}
+
+void CLUnstack::run()
+{
+ for(unsigned i = 0; i < _num_slices; ++i)
+ {
+ _strided_slice_vector[i].run();
+ }
+}
+
+} // namespace arm_compute