summaryrefslogtreecommitdiff
path: root/compiler/luci-interpreter/src/kernels/Shape.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/luci-interpreter/src/kernels/Shape.cpp')
-rw-r--r--compiler/luci-interpreter/src/kernels/Shape.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/compiler/luci-interpreter/src/kernels/Shape.cpp b/compiler/luci-interpreter/src/kernels/Shape.cpp
new file mode 100644
index 000000000..0429fe1e5
--- /dev/null
+++ b/compiler/luci-interpreter/src/kernels/Shape.cpp
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "kernels/Shape.h"
+#include "kernels/Utils.h"
+
+namespace luci_interpreter
+{
+namespace kernels
+{
+
+ShapeKernel::ShapeKernel(const Tensor *input, Tensor *output, const ShapeParams &params)
+ : KernelWithParams<ShapeParams>({input}, {output}, params)
+{
+}
+
+void ShapeKernel::configure()
+{
+ LUCI_INTERPRETER_CHECK(output()->element_type() == DataType::S32 or
+ output()->element_type() == DataType::S64);
+ const auto input_shape = input()->shape();
+
+ Shape output_shape(1);
+ output_shape.dim(0) = input_shape.num_dims();
+
+ output()->resize(output_shape);
+}
+
+void ShapeKernel::execute() const
+{
+ switch (params().out_type)
+ {
+ case DataType::S32:
+ evalInt<int32_t>();
+ break;
+ case DataType::S64:
+ evalInt<int64_t>();
+ break;
+ default:
+ throw std::runtime_error("Unsupported type.");
+ }
+}
+
+template <typename T> void ShapeKernel::evalInt() const
+{
+ const auto input_shape = input()->shape();
+
+ auto output_data = getTensorData<T>(output());
+
+ for (int i = 0; i < input_shape.num_dims(); ++i)
+ {
+ output_data[i] = input_shape.dim(i);
+ }
+}
+
+} // namespace kernels
+} // namespace luci_interpreter