aboutsummaryrefslogtreecommitdiff
path: root/compute_kernel_writer/src/cl
diff options
context:
space:
mode:
authorGian Marco Iodice <gianmarco.iodice@arm.com>2023-01-19 17:14:26 +0000
committerGian Marco Iodice <gianmarco.iodice@arm.com>2023-05-24 08:30:23 +0000
commit6c113ed1a95a08d17c2d556bd7b03c901512a34f (patch)
treec616a085e1eb3b4b9ac01a1f261182d4d10c481a /compute_kernel_writer/src/cl
parent1355ec4797cd77060af51c8b27d99ea1d25c08da (diff)
downloadComputeLibrary-6c113ed1a95a08d17c2d556bd7b03c901512a34f.tar.gz
Prepare the basic types for the compute kernel writer (CKW)
- Add TensorInfo - Add TileInfo - Add CLTile - Add basic utility methods to get tensor components - Add unit tests Resolves COMPMID-5782, COMPMID-5785 Signed-off-by: Gian Marco Iodice <gianmarco.iodice@arm.com> Change-Id: I5e590bddd240d2f1fc876cac7129947558d7d53b Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/c/VisualCompute/ComputeLibrary/+/486221 Tested-by: bsgcomp <bsgcomp@arm.com> Reviewed-by: Jakub Sujak <jakub.sujak@arm.com> Reviewed-by: Pablo Tello <pablo.tello@arm.com> Comments-Addressed: bsgcomp <bsgcomp@arm.com> Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9687 Reviewed-by: Viet-Hoa Do <viet-hoa.do@arm.com> Reviewed-by: Pablo Marquez Tello <pablo.tello@arm.com> Comments-Addressed: Arm Jenkins <bsgcomp@arm.com> Tested-by: Arm Jenkins <bsgcomp@arm.com> Benchmark: Arm Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'compute_kernel_writer/src/cl')
-rw-r--r--compute_kernel_writer/src/cl/CLHelpers.cpp91
-rw-r--r--compute_kernel_writer/src/cl/CLHelpers.h53
-rw-r--r--compute_kernel_writer/src/cl/CLTile.cpp156
-rw-r--r--compute_kernel_writer/src/cl/CLTile.h61
4 files changed, 361 insertions, 0 deletions
diff --git a/compute_kernel_writer/src/cl/CLHelpers.cpp b/compute_kernel_writer/src/cl/CLHelpers.cpp
new file mode 100644
index 0000000000..68d7db252b
--- /dev/null
+++ b/compute_kernel_writer/src/cl/CLHelpers.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2023 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 "ckw/Error.h"
+#include "ckw/Types.h"
+
+#include "src/cl/CLHelpers.h"
+
+namespace ckw
+{
+bool cl_validate_vector_length(int32_t len)
+{
+ bool valid_vector_length = true;
+ if(len < 1 || len > 16 || (len > 4 && len < 8) || (len > 8 && len < 16))
+ {
+ valid_vector_length = false;
+ }
+ return valid_vector_length;
+}
+
+std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len)
+{
+ if(cl_validate_vector_length(len) == false)
+ {
+ COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported vector length");
+ return "";
+ }
+
+ std::string res;
+ switch(dt)
+ {
+ case DataType::Fp32:
+ res += "float";
+ break;
+ case DataType::Fp16:
+ res += "half";
+ break;
+ case DataType::Int8:
+ res += "char";
+ break;
+ case DataType::Uint8:
+ res += "uchar";
+ break;
+ case DataType::Uint16:
+ res += "ushort";
+ break;
+ case DataType::Int16:
+ res += "short";
+ break;
+ case DataType::Uint32:
+ res += "uint";
+ break;
+ case DataType::Int32:
+ res += "int";
+ break;
+ case DataType::Bool:
+ res += "bool";
+ break;
+ default:
+ COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported datatype");
+ return "";
+ }
+
+ if(len > 1)
+ {
+ res += std::to_string(len);
+ }
+
+ return res;
+}
+} // namespace ckw \ No newline at end of file
diff --git a/compute_kernel_writer/src/cl/CLHelpers.h b/compute_kernel_writer/src/cl/CLHelpers.h
new file mode 100644
index 0000000000..915d59f458
--- /dev/null
+++ b/compute_kernel_writer/src/cl/CLHelpers.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2023 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 COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H
+#define COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H
+
+#include <string>
+
+/** OpenCL specific helper functions */
+namespace ckw
+{
+// Forward declarations
+enum class DataType;
+
+/** Helper function to validate the vector length of OpenCL vector data types
+ *
+ * @param[in] len Vector length
+ *
+ * @return true if the vector lenght is valid. It returns false, otherwise.
+ */
+bool cl_validate_vector_length(int32_t len);
+
+/** Helper function to return the OpenCL datatype as a string from a @ref DataType and vector length as int32_t variable
+ *
+ * @param[in] dt Datatype
+ * @param[in] len Vector length
+ *
+ * @return the OpenCL datatype as a string
+ */
+std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len);
+} // namespace ckw
+
+#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H */
diff --git a/compute_kernel_writer/src/cl/CLTile.cpp b/compute_kernel_writer/src/cl/CLTile.cpp
new file mode 100644
index 0000000000..a46f692a5c
--- /dev/null
+++ b/compute_kernel_writer/src/cl/CLTile.cpp
@@ -0,0 +1,156 @@
+/*
+ * Copyright (c) 2023 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 "ckw/Error.h"
+#include "ckw/TileInfo.h"
+
+#include "src/Helpers.h"
+#include "src/cl/CLHelpers.h"
+#include "src/cl/CLTile.h"
+
+#include <algorithm>
+#include <vector>
+
+namespace ckw
+{
+CLTile::CLTile(const std::string &name, const TileInfo &info)
+{
+ validate_tile_info(info);
+
+ _basename = name;
+ _info = info;
+}
+
+TileVariable CLTile::scalar(int32_t col, int32_t row) const
+{
+ // Clamp to nearest valid edge
+ col = clamp(col, static_cast<int32_t>(0), _info.width() - 1);
+ row = clamp(row, static_cast<int32_t>(0), _info.height() - 1);
+
+ TileVariable t;
+ t.str = create_var_name(row);
+ t.desc.dt = _info.data_type();
+ t.desc.len = 1;
+
+ // This check is required because if the width has only one element, we cannot use .s0
+ if(_info.width() != 1)
+ {
+ // Automatic broadcasting
+ t.str += ".s" + dec_to_hex_as_string(col);
+ }
+
+ return t;
+}
+
+TileVariable CLTile::vector(int32_t row) const
+{
+ // Clamp to nearest valid edge
+ row = clamp(row, static_cast<int32_t>(0), _info.height() - 1);
+
+ TileVariable t;
+ t.str = create_var_name(row);
+ t.desc.dt = _info.data_type();
+ t.desc.len = _info.width();
+ return t;
+}
+
+TileVariable CLTile::vector(int32_t col_start, int32_t width, int32_t row) const
+{
+ // Validate the new vector length
+ cl_validate_vector_length(width);
+
+ // Clamp to nearest valid edge
+ row = clamp(row, static_cast<int32_t>(0), _info.height() - 1);
+
+ TileVariable t;
+ t.str = create_var_name(row);
+ t.desc.dt = _info.data_type();
+ t.desc.len = width;
+
+ if(_info.width() != 1)
+ {
+ t.str += ".s";
+ for(int i = 0; i < width; ++i)
+ {
+ t.str += dec_to_hex_as_string(col_start + i);
+ }
+ }
+ return t;
+}
+
+std::vector<TileVariable> CLTile::all() const
+{
+ std::vector<TileVariable> vars;
+ for(int32_t y = 0; y < _info.height(); ++y)
+ {
+ TileVariable t;
+ t.str = create_var_name(y);
+ t.desc.dt = _info.data_type();
+ t.desc.len = _info.width();
+ vars.push_back(t);
+ }
+ return vars;
+}
+
+std::vector<int32_t> CLTile::supported_vector_lengths() const
+{
+ return std::vector<int32_t> {1, 2, 3, 4, 8, 16};
+}
+
+bool CLTile::is_assignable() const
+{
+ return true;
+}
+
+std::string CLTile::create_var_name(int32_t row) const
+{
+ std::string var_name = _basename;
+
+ // If a scalar variable, we do not append the row index
+ if(_info.height() == 1)
+ {
+ return var_name;
+
+ }
+ else
+ {
+ var_name += "_";
+ var_name += std::to_string(row);
+ }
+
+ return var_name;
+}
+
+void CLTile::validate_tile_info(const TileInfo &info) const
+{
+ if(cl_validate_vector_length(info.width()))
+ {
+ COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported TileInfo width");
+ }
+
+ if(info.data_type() == DataType::Unknown)
+ {
+ COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("DataType::Unknown is not supported");
+ }
+}
+} // namespace ckw \ No newline at end of file
diff --git a/compute_kernel_writer/src/cl/CLTile.h b/compute_kernel_writer/src/cl/CLTile.h
new file mode 100644
index 0000000000..50801675a7
--- /dev/null
+++ b/compute_kernel_writer/src/cl/CLTile.h
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2023 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 COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H
+#define COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H
+
+#include "src/ITile.h"
+
+#include <string>
+
+namespace ckw
+{
+// Forward declarations
+class TileInfo;
+
+/** OpenCL specific tile */
+class CLTile : protected IVectorTile
+{
+public:
+ /** Constructor
+ *
+ * @param[in] name Tile name
+ * @param[in] info Tile info
+ */
+ CLTile(const std::string& name, const TileInfo &info);
+
+ // Inherited method overridden
+ TileVariable scalar(int32_t col, int32_t row) const override;
+ TileVariable vector(int32_t row) const override;
+ TileVariable vector(int32_t col_start, int32_t width, int32_t row) const override;
+ std::vector<TileVariable> all() const override;
+ std::vector<int32_t> supported_vector_lengths() const override;
+ bool is_assignable() const override;
+
+private:
+ std::string create_var_name(int32_t row) const;
+ void validate_tile_info(const TileInfo &info) const;
+};
+} // namespace ckw
+
+#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H */