diff options
author | Gian Marco Iodice <gianmarco.iodice@arm.com> | 2023-06-15 17:40:28 +0100 |
---|---|---|
committer | Gian Marco Iodice <gianmarco.iodice@arm.com> | 2023-06-20 14:03:01 +0000 |
commit | 68e9c4dfd3c02b3f2e25290f9ad9c3b0bc001d25 (patch) | |
tree | a846a18407fb47c9e56c3f617dc4f02793aa413e /compute_kernel_writer/src | |
parent | 59b9ff0adac8b7d7044b4a74c23b710fe1062d10 (diff) | |
download | ComputeLibrary-68e9c4dfd3c02b3f2e25290f9ad9c3b0bc001d25.tar.gz |
Implement CLConstantTile
- Create tile object to store constant variables
- Add unit tests for the CLConstantTile
- Extend unit test for CLTile
Resolves COMPMID-5786
Signed-off-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
Change-Id: I351d8b88327e071791b781a04a6ab4990c89c04d
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9794
Benchmark: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Diffstat (limited to 'compute_kernel_writer/src')
-rw-r--r-- | compute_kernel_writer/src/ITile.h | 11 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLConstantTile.cpp | 121 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLConstantTile.h | 61 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTile.cpp | 22 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTile.h | 9 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/ICLTile.cpp | 51 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/ICLTile.h | 46 |
7 files changed, 292 insertions, 29 deletions
diff --git a/compute_kernel_writer/src/ITile.h b/compute_kernel_writer/src/ITile.h index 283e6fa236..15f4a3e623 100644 --- a/compute_kernel_writer/src/ITile.h +++ b/compute_kernel_writer/src/ITile.h @@ -31,6 +31,9 @@ namespace ckw { +/** Compute Kernel Writer tile container. It contains the variables stored in the tile as a string */ +using TileContainer = std::vector<std::vector<std::string>>; + /** Tile descriptor which reports the underlying datatype and vector length */ struct TileVariableDescriptor { @@ -92,12 +95,12 @@ class IScalarTile : public ITile public: virtual ~IScalarTile() = default; /** Method to get the scalar variable from a tile as a string - * @param[in] col Tile column. If out-of-bound, the column is clamped to the nearest valid edge * @param[in] row Tile row. If out-of-bound, the row is clamped to the nearest valid edge + * @param[in] col Tile column. If out-of-bound, the column is clamped to the nearest valid edge * * @return the @ref TileVariable */ - virtual TileVariable scalar(int32_t col, int32_t row) const = 0; + virtual TileVariable scalar(int32_t row, int32_t col) const = 0; }; /** Tile base class to store vector variables. It derives from IScalarTile since we can still access the scalar variable @@ -116,13 +119,13 @@ public: virtual TileVariable vector(int32_t row) const = 0; /** Method to get a sub-vector variable. The length of the sub-vector must be supported by the derived IVectorTile class * + * @param[in] row Tile row. If out-of-bound, the row is clamped to the nearest valid edge * @param[in] col_start Tile starting column to get the sub-vector. If out-of-bound, the derived IVectorTile class may throw an assert. * @param[in] width The width of the sub-vector. The width must be supported by the derived IVectorTile class and the last element must be in-bound. - * @param[in] row Tile row. If out-of-bound, the row is clamped to the nearest valid edge * * @return the vector variable as a @ref TileVariable */ - virtual TileVariable vector(int32_t col_start, int32_t width, int32_t row) const = 0; + virtual TileVariable vector(int32_t row, int32_t col_start, int32_t width) const = 0; /** Method to get the supported vector length. * * @return a vector containing the supported vector lengths diff --git a/compute_kernel_writer/src/cl/CLConstantTile.cpp b/compute_kernel_writer/src/cl/CLConstantTile.cpp new file mode 100644 index 0000000000..1477a683e6 --- /dev/null +++ b/compute_kernel_writer/src/cl/CLConstantTile.cpp @@ -0,0 +1,121 @@ +/* + * 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/CLConstantTile.h" + +namespace ckw +{ +CLConstantTile::CLConstantTile(const TileContainer &vals, DataType dt) +{ + const int32_t w = vals[0].size(); + const int32_t h = vals.size(); + + _info.width(w); + _info.height(h); + _info.data_type(dt); + + validate_tile_info(_info); + + _vals = TileContainer(h, std::vector<std::string>(w)); + + for(int32_t y = 0; y < h; ++y) + { + for(int32_t x = 0; x < w; ++x) + { + _vals[y][x] = vals[y][x]; + } + } +} + +TileVariable CLConstantTile::scalar(int32_t row, int32_t col) 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); + + // We can use the vector method to retrieve the scalar variable stored in the constant tile + return vector(row, col, 1); +} + +TileVariable CLConstantTile::vector(int32_t row) const +{ + // Clamp to nearest valid edge + row = clamp(row, static_cast<int32_t>(0), _info.height() - 1); + + return vector(row, 0, _info.width()); +} + +TileVariable CLConstantTile::vector(int32_t row, int32_t col_start, int32_t width) 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.desc.dt = _info.data_type(); + t.desc.len = width; + + // The vector has the following form: ((data_typeN)(val0, val1,..., ValN-1)) + t.str = "((" + cl_get_variable_datatype_as_string(t.desc.dt, width) + ")"; + t.str += "("; + + int32_t col = col_start; + for(; col < width - 1; ++col) + { + t.str += _vals[row][col]; + t.str += ", "; + } + t.str += _vals[row][col]; + t.str += "))"; + + return t; +} + +std::vector<TileVariable> CLConstantTile::all() const +{ + std::vector<TileVariable> vars; + + for(int32_t y = 0; y < _info.height(); ++y) + { + for(int32_t x = 0; x < _info.width(); ++x) + { + // We can use the vector method to retrieve all the scalar variables stored in the constant tile + TileVariable t = vector(y, x, 1); + vars.push_back(t); + } + } + return vars; +} + +bool CLConstantTile::is_assignable() const +{ + return false; +} +} // namespace ckw
\ No newline at end of file diff --git a/compute_kernel_writer/src/cl/CLConstantTile.h b/compute_kernel_writer/src/cl/CLConstantTile.h new file mode 100644 index 0000000000..ebd0f04659 --- /dev/null +++ b/compute_kernel_writer/src/cl/CLConstantTile.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_CLCONSTANTTILE_H +#define COMPUTE_KERNEL_WRITER_SRC_CL_CLCONSTANTTILE_H + +#include "src/ITile.h" +#include "src/cl/ICLTile.h" + +namespace ckw +{ +// Forward declarations +class TileInfo; + +/** OpenCL specific constant tile */ +class CLConstantTile : public ICLTile +{ +public: + /** Constructor + * + * @note A constant tile does not need a name since this object does not return variable's name but rather + * values stored as string type + * + * @param[in] vals The tile container with the constant values as std::string + * @param[in] dt Datatype of the values stored in the tile container + */ + CLConstantTile(const TileContainer &vals, DataType dt); + + // Inherited method overridden + TileVariable scalar(int32_t row, int32_t col) const override; + TileVariable vector(int32_t row) const override; + TileVariable vector(int32_t row, int32_t col_start, int32_t width) const override; + std::vector<TileVariable> all() const override; + bool is_assignable() const override; + +private: + TileContainer _vals {}; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_CLCONSTANTTILE_H */ diff --git a/compute_kernel_writer/src/cl/CLTile.cpp b/compute_kernel_writer/src/cl/CLTile.cpp index a46f692a5c..bc544ecedf 100644 --- a/compute_kernel_writer/src/cl/CLTile.cpp +++ b/compute_kernel_writer/src/cl/CLTile.cpp @@ -41,7 +41,7 @@ CLTile::CLTile(const std::string &name, const TileInfo &info) _info = info; } -TileVariable CLTile::scalar(int32_t col, int32_t row) const +TileVariable CLTile::scalar(int32_t row, int32_t col) const { // Clamp to nearest valid edge col = clamp(col, static_cast<int32_t>(0), _info.width() - 1); @@ -74,7 +74,7 @@ TileVariable CLTile::vector(int32_t row) const return t; } -TileVariable CLTile::vector(int32_t col_start, int32_t width, int32_t row) const +TileVariable CLTile::vector(int32_t row, int32_t col_start, int32_t width) const { // Validate the new vector length cl_validate_vector_length(width); @@ -112,11 +112,6 @@ std::vector<TileVariable> CLTile::all() const 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; @@ -140,17 +135,4 @@ std::string CLTile::create_var_name(int32_t row) const 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 index 50801675a7..83d49b7030 100644 --- a/compute_kernel_writer/src/cl/CLTile.h +++ b/compute_kernel_writer/src/cl/CLTile.h @@ -25,6 +25,7 @@ #define COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H #include "src/ITile.h" +#include "src/cl/ICLTile.h" #include <string> @@ -34,7 +35,7 @@ namespace ckw class TileInfo; /** OpenCL specific tile */ -class CLTile : protected IVectorTile +class CLTile : public ICLTile { public: /** Constructor @@ -45,16 +46,14 @@ public: CLTile(const std::string& name, const TileInfo &info); // Inherited method overridden - TileVariable scalar(int32_t col, int32_t row) const override; + TileVariable scalar(int32_t row, int32_t col) const override; TileVariable vector(int32_t row) const override; - TileVariable vector(int32_t col_start, int32_t width, int32_t row) const override; + TileVariable vector(int32_t row, int32_t col_start, int32_t width) 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 diff --git a/compute_kernel_writer/src/cl/ICLTile.cpp b/compute_kernel_writer/src/cl/ICLTile.cpp new file mode 100644 index 0000000000..f9c8827ef4 --- /dev/null +++ b/compute_kernel_writer/src/cl/ICLTile.cpp @@ -0,0 +1,51 @@ +/* + * 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/cl/CLHelpers.h" +#include "src/cl/ICLTile.h" + +#include <vector> + +namespace ckw +{ +std::vector<int32_t> ICLTile::supported_vector_lengths() const +{ + return std::vector<int32_t> {1, 2, 3, 4, 8, 16}; +} + +void ICLTile::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/ICLTile.h b/compute_kernel_writer/src/cl/ICLTile.h new file mode 100644 index 0000000000..b02bc037af --- /dev/null +++ b/compute_kernel_writer/src/cl/ICLTile.h @@ -0,0 +1,46 @@ +/* + * 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_ICLTILE_H +#define COMPUTE_KERNEL_WRITER_SRC_CL_ICLTILE_H + +#include "src/ITile.h" + +namespace ckw +{ +// Forward declarations +class TileInfo; + +/** Interface for the OpenCL specific tile */ +class ICLTile : public IVectorTile +{ +public: + // Inherited method overridden + std::vector<int32_t> supported_vector_lengths() const override; + +protected: + void validate_tile_info(const TileInfo &info) const; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_ICLTILE_H */ |