From 68e9c4dfd3c02b3f2e25290f9ad9c3b0bc001d25 Mon Sep 17 00:00:00 2001 From: Gian Marco Iodice Date: Thu, 15 Jun 2023 17:40:28 +0100 Subject: 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 Change-Id: I351d8b88327e071791b781a04a6ab4990c89c04d Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9794 Benchmark: Arm Jenkins Tested-by: Arm Jenkins Reviewed-by: Viet-Hoa Do --- compute_kernel_writer/CMakeLists.txt | 6 +- compute_kernel_writer/include/ckw/TileInfo.h | 2 +- compute_kernel_writer/src/ITile.h | 11 +- compute_kernel_writer/src/cl/CLConstantTile.cpp | 121 +++++++ compute_kernel_writer/src/cl/CLConstantTile.h | 61 ++++ compute_kernel_writer/src/cl/CLTile.cpp | 22 +- compute_kernel_writer/src/cl/CLTile.h | 9 +- compute_kernel_writer/src/cl/ICLTile.cpp | 51 +++ compute_kernel_writer/src/cl/ICLTile.h | 46 +++ compute_kernel_writer/validation/Validation.cpp | 18 +- .../validation/tests/CLConstantTileTest.hpp | 369 +++++++++++++++++++++ .../validation/tests/CLTileTest.hpp | 226 +++++++++++-- 12 files changed, 875 insertions(+), 67 deletions(-) create mode 100644 compute_kernel_writer/src/cl/CLConstantTile.cpp create mode 100644 compute_kernel_writer/src/cl/CLConstantTile.h create mode 100644 compute_kernel_writer/src/cl/ICLTile.cpp create mode 100644 compute_kernel_writer/src/cl/ICLTile.h create mode 100644 compute_kernel_writer/validation/tests/CLConstantTileTest.hpp diff --git a/compute_kernel_writer/CMakeLists.txt b/compute_kernel_writer/CMakeLists.txt index 4338f7bbb4..f203a18f56 100644 --- a/compute_kernel_writer/CMakeLists.txt +++ b/compute_kernel_writer/CMakeLists.txt @@ -107,8 +107,10 @@ target_sources(ckw PRIVATE ) if(CKW_ENABLE_OPENCL) target_sources(ckw PRIVATE + src/cl/CLConstantTile.cpp src/cl/CLHelpers.cpp src/cl/CLTile.cpp + src/cl/ICLTile.cpp ) endif() @@ -135,7 +137,9 @@ if(CKW_BUILD_TESTING) validation/Validation.cpp ) if(CKW_ENABLE_OPENCL) - target_sources(ckw_validation PRIVATE validation/tests/CLTileTest.hpp) + target_sources(ckw_validation PRIVATE + validation/tests/CLConstantTileTest.hpp + validation/tests/CLTileTest.hpp) endif() target_link_libraries(ckw_validation PRIVATE ckw) diff --git a/compute_kernel_writer/include/ckw/TileInfo.h b/compute_kernel_writer/include/ckw/TileInfo.h index 4f801513b0..5f9d037a66 100644 --- a/compute_kernel_writer/include/ckw/TileInfo.h +++ b/compute_kernel_writer/include/ckw/TileInfo.h @@ -36,7 +36,7 @@ namespace ckw constexpr int32_t kTileWidthIdx = 0; constexpr int32_t kTileHeightIdx = 1; -/** Compute Kernel Writer tile shape */ +/** Compute Kernel Writer tile shape. It is used to define the shape of the tile */ using TileShape = std::array; /** Compute Kernel Writer tile info */ 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>; + /** 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(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(0), _info.width() - 1); + row = clamp(row, static_cast(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(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(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 CLConstantTile::all() const +{ + std::vector 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 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(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 CLTile::all() const return vars; } -std::vector CLTile::supported_vector_lengths() const -{ - return std::vector {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 @@ -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 all() const override; - std::vector 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 + +namespace ckw +{ +std::vector ICLTile::supported_vector_lengths() const +{ + return std::vector {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 supported_vector_lengths() const override; + +protected: + void validate_tile_info(const TileInfo &info) const; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_ICLTILE_H */ diff --git a/compute_kernel_writer/validation/Validation.cpp b/compute_kernel_writer/validation/Validation.cpp index 9bdf0dfdd2..da0ce8ad15 100644 --- a/compute_kernel_writer/validation/Validation.cpp +++ b/compute_kernel_writer/validation/Validation.cpp @@ -22,6 +22,7 @@ * SOFTWARE. */ +#include "tests/CLConstantTileTest.hpp" #include "tests/CLTileTest.hpp" #include "tests/TensorBitMaskTest.hpp" #include "tests/UtilsTest.hpp" @@ -51,11 +52,26 @@ int32_t main() const auto test5 = std::make_unique(); const auto test6 = std::make_unique(); const auto test7 = std::make_unique(); + const auto test8 = std::make_unique(); + const auto test9 = std::make_unique(); + const auto test10 = std::make_unique(); + const auto test11 = std::make_unique(); + const auto test12 = std::make_unique(); + const auto test13 = std::make_unique(); + const auto test14 = std::make_unique(); + tests.push_back(test3.get()); tests.push_back(test4.get()); tests.push_back(test5.get()); tests.push_back(test6.get()); tests.push_back(test7.get()); + tests.push_back(test8.get()); + tests.push_back(test9.get()); + tests.push_back(test10.get()); + tests.push_back(test11.get()); + tests.push_back(test12.get()); + tests.push_back(test13.get()); + tests.push_back(test14.get()); #endif /* COMPUTE_KERNEL_WRITER_OPENCL_ENABLED */ bool all_test_passed = true; @@ -72,7 +88,7 @@ int32_t main() } else { - std::cout << "One or more tests failed" << std::endl; + std::runtime_error("One or more tests failed"); } return 0; diff --git a/compute_kernel_writer/validation/tests/CLConstantTileTest.hpp b/compute_kernel_writer/validation/tests/CLConstantTileTest.hpp new file mode 100644 index 0000000000..33942c707d --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLConstantTileTest.hpp @@ -0,0 +1,369 @@ +/* + * 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_TESTS_CLCONSTANTTILETEST_HPP +#define COMPUTE_KERNEL_WRITER_TESTS_CLCONSTANTTILETEST_HPP + +#include "src/Helpers.h" +#include "src/cl/CLConstantTile.h" +#include "src/cl/CLHelpers.h" +#include "common/Common.h" + +#include +#include +#include + +namespace ckw +{ +class CLConstantTileInternalValuesTest : public ITest +{ +public: + CLConstantTileInternalValuesTest() + { + _values.push_back({{"1.2", "3.5"}, {"4.2", "1.3"}}); + _values.push_back({{"1.2"}}); + _values.push_back({{"1.2", "6.9"}}); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + int32_t test_idx = 0; + for(const auto &test : _values) + { + const CLConstantTile tile(test, DataType::Fp16); + const auto vars = tile.all(); + const int32_t num_vars = vars.size(); + const int32_t width = tile.info().width(); + + for(int32_t y = 0; y < num_vars; ++y) + { + const int32_t col = y % width; + const int32_t row = y / width; + const std::string expected_var_name = "((half)(" + test[row][col] + "))"; + const std::string actual_var_name = vars[y].str; + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + + } + return all_tests_passed; + } + + std::string name() override + { + return "CLConstantTileInternalValuesTest"; + } + +private: + std::vector _values {}; +}; + +class CLConstantTileAccessScalarVariableBroadcastXTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t height = 8; + const DataType dt = DataType::Fp16; + + CLConstantTileAccessScalarVariableBroadcastXTest() + { + _width.push_back(1); + _width.push_back(2); + _width.push_back(3); + + _x_coord.push_back(4); + _x_coord.push_back(5); + _x_coord.push_back(6); + + _y_coord.push_back(1); + _y_coord.push_back(3); + _y_coord.push_back(2); + } + + bool run() override + { + VALIDATE_ON_MSG(_width.size() == _y_coord.size(), "The number of widths and y-coords does not match"); + VALIDATE_ON_MSG(_x_coord.size() == _y_coord.size(), "The number of x-coords and y-coords does not match"); + + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const size_t num_coords = _x_coord.size(); + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dist(-1, 1); + + int32_t test_idx = 0; + for(size_t i = 0; i < num_coords; ++i) + { + const int32_t width = _width[i]; + const int32_t x_coord = _x_coord[i]; + const int32_t y_coord = _y_coord[i]; + + const int32_t x_coord_clamped = clamp(x_coord, static_cast(0), width - 1); + + TileContainer container = TileContainer(height, std::vector(width)); + + for(int32_t row = 0; row < height; ++row) + { + for(int32_t col = 0; col < width; ++col) + { + container[row][col] = std::to_string(dist(gen)); + } + } + + const CLConstantTile tile(container, dt); + + const TileVariable var = tile.scalar(y_coord, x_coord); + + const std::string actual_var_name = var.str; + const std::string expected_var_name = "((half)(" + container[y_coord][x_coord_clamped] + "))"; + + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLConstantTileAccessScalarVariableBroadcastXTest"; + } + +private: + std::vector _width {}; + std::vector _x_coord {}; + std::vector _y_coord {}; +}; + +class CLConstantTileAccessScalarVariableBroadcastYTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t width = 8; + const DataType dt = DataType::Fp16; + + CLConstantTileAccessScalarVariableBroadcastYTest() + { + _height.push_back(1); + _height.push_back(2); + _height.push_back(3); + + _x_coord.push_back(4); + _x_coord.push_back(5); + _x_coord.push_back(6); + + _y_coord.push_back(3); + _y_coord.push_back(4); + _y_coord.push_back(5); + } + + bool run() override + { + VALIDATE_ON_MSG(_height.size() == _y_coord.size(), "The number of widths and y-coords does not match"); + VALIDATE_ON_MSG(_x_coord.size() == _y_coord.size(), "The number of x-coords and y-coords does not match"); + + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_real_distribution<> dist(-1, 1); + + const size_t num_coords = _x_coord.size(); + + int32_t test_idx = 0; + for(size_t i = 0; i < num_coords; ++i) + { + const int32_t height = _height[i]; + const int32_t x_coord = _x_coord[i]; + const int32_t y_coord = _y_coord[i]; + + const int32_t y_coord_clamped = clamp(y_coord, static_cast(0), height - 1); + + TileContainer container = TileContainer(height, std::vector(width)); + + for(int32_t row = 0; row < height; ++row) + { + for(int32_t col = 0; col < width; ++col) + { + container[row][col] = std::to_string(dist(gen)); + } + } + + const CLConstantTile tile(container, dt); + + const TileVariable var = tile.scalar(y_coord, x_coord); + + const std::string actual_var_name = var.str; + const std::string expected_var_name = "((half)(" + container[y_coord_clamped][x_coord] + "))"; + + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLConstantTileAccessScalarVariableBroadcastYTest"; + } + +private: + std::vector _height {}; + std::vector _x_coord {}; + std::vector _y_coord {}; +}; + +class CLConstantTileAccessVectorVariablesTest : public ITest +{ +public: + const DataType dt = DataType::Fp16; + + CLConstantTileAccessVectorVariablesTest() + { + _values.push_back({{"1.2", "3.5"}, {"4.2", "1.3"}}); + _values.push_back({{"1.2"}}); + // Mix variable names and values + _values.push_back({{"1.2", "acc", "8.7", "9.3", "ratio", "2.9", "1.7", "0.3"}}); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + int32_t test_idx = 0; + + for(const auto &test : _values) + { + const CLConstantTile tile(test, dt); + const int32_t width = tile.info().width(); + const int32_t height = tile.info().height(); + + for(int32_t row = 0; row < height; ++row) + { + std::string expected_var_name = "(("; + expected_var_name += cl_get_variable_datatype_as_string(dt, width); + expected_var_name += ")("; + + int32_t col = 0; + for(; col < width - 1; ++col) + { + expected_var_name += test[row][col]; + expected_var_name += ", "; + } + + expected_var_name += test[row][col]; + expected_var_name += "))"; + + const std::string actual_var_name = tile.vector(row).str; + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + } + return all_tests_passed; + } + + std::string name() override + { + return "CLConstantTileAccessVectorVariablesTest"; + } + +private: + std::vector _values {}; +}; + +class CLConstantTileAccessSubVectorVariablesTest : public ITest +{ +public: + const DataType dt = DataType::Fp16; + + CLConstantTileAccessSubVectorVariablesTest() + { + _values.push_back({{"1.2", "acc", "8.7", "9.3", "ratio", "2.9", "1.7", "0.3"}}); + _subwidths.push_back(1); + _subwidths.push_back(2); + _subwidths.push_back(3); + _subwidths.push_back(4); + _offsets.push_back(1); + _offsets.push_back(3); + _offsets.push_back(4); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + size_t test_idx = 0; + + for(auto &test : _values) + { + for(auto &col_start : _offsets) + { + for(auto &subwidth : _subwidths) + { + const CLConstantTile tile(test, dt); + const int32_t height = tile.info().height(); + + for(int32_t row = 0; row < height; ++row) + { + std::string expected_var_name = "(("; + expected_var_name += cl_get_variable_datatype_as_string(dt, subwidth); + expected_var_name += ")("; + + int32_t col = col_start; + for(; col < subwidth - 1; ++col) + { + expected_var_name += test[row][col]; + expected_var_name += ", "; + } + + expected_var_name += test[row][col]; + expected_var_name += "))"; + + const std::string actual_var_name = tile.vector(row, col_start, subwidth).str; + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + } + } + } + return all_tests_passed; + } + + std::string name() override + { + return "CLConstantTileAccessSubVectorVariablesTest"; + } + +private: + std::vector _values {}; + std::vector _subwidths {}; + std::vector _offsets {}; +}; + +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_TESTS_CLCONSTANTTILETEST_HPP */ diff --git a/compute_kernel_writer/validation/tests/CLTileTest.hpp b/compute_kernel_writer/validation/tests/CLTileTest.hpp index 9fb47941f4..f3fdcd1304 100644 --- a/compute_kernel_writer/validation/tests/CLTileTest.hpp +++ b/compute_kernel_writer/validation/tests/CLTileTest.hpp @@ -1,5 +1,29 @@ -#ifndef COMPUTE_KERNEL_WRITER_TESTS_CLTENSOR_HPP -#define COMPUTE_KERNEL_WRITER_TESTS_CLTENSOR_HPP +/* + * 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_TESTS_CLTILETEST_HPP +#define COMPUTE_KERNEL_WRITER_TESTS_CLTILETEST_HPP #include "src/Helpers.h" #include "src/cl/CLTile.h" @@ -31,10 +55,9 @@ public: const TileInfo info(dt, width, height); - const size_t num_tests = _tile_name.size(); - for(size_t i = 0; i < num_tests; ++i) + int32_t test_idx = 0; + for(const auto &tile_name : _tile_name) { - const std::string tile_name = _tile_name[i]; const CLTile tile(tile_name, info); const auto vars = tile.all(); @@ -42,7 +65,7 @@ public: { const std::string expected_var_name = tile_name + "_" + std::to_string(y); const std::string actual_var_name = vars[y].str; - VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, i); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); } } return all_tests_passed; @@ -78,9 +101,10 @@ public: // The status of this variable can change in VALIDATE_TEST() bool all_tests_passed = true; - const size_t num_tests = _width.size(); + const size_t num_dims = _width.size(); - for(size_t i = 0; i < num_tests; ++i) + int32_t test_idx = 0; + for(size_t i = 0; i < num_dims; ++i) { const int32_t width = _width[i]; const int32_t height = _height[i]; @@ -90,7 +114,7 @@ public: const int32_t num_vars = vars.size(); // We expect the number of variables to match the heigth of the tile - VALIDATE_TEST(num_vars == height, all_tests_passed, i); + VALIDATE_TEST(num_vars == height, all_tests_passed, test_idx++); } return all_tests_passed; } @@ -136,21 +160,22 @@ public: // The status of this variable can change in VALIDATE_TEST() bool all_tests_passed = true; - const size_t num_tests = _x_coord.size(); + const size_t num_coords = _x_coord.size(); - for(size_t i = 0; i < num_tests; ++i) + int32_t test_idx = 0; + for(size_t i = 0; i < num_coords; ++i) { const int32_t x_coord = _x_coord[i]; const int32_t y_coord = _y_coord[i]; - const TileVariable var = tile.scalar(x_coord, y_coord); + const TileVariable var = tile.scalar(y_coord, x_coord); - const std::string expected_var_name = var.str; - std::string actual_var_name = tile_name; - actual_var_name += "_" + std::to_string(y_coord); - actual_var_name += ".s" + dec_to_hex_as_string(x_coord); + const std::string actual_var_name = var.str; + std::string expected_var_name = tile_name; + expected_var_name += "_" + std::to_string(y_coord); + expected_var_name += ".s" + dec_to_hex_as_string(x_coord); - VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, i); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); } return all_tests_passed; } @@ -195,9 +220,10 @@ public: // The status of this variable can change in VALIDATE_TEST() bool all_tests_passed = true; - const size_t num_tests = _x_coord.size(); + const size_t num_coords = _x_coord.size(); - for(size_t i = 0; i < num_tests; ++i) + int32_t test_idx = 0; + for(size_t i = 0; i < num_coords; ++i) { const int32_t width = _width[i]; const int32_t x_coord = _x_coord[i]; @@ -208,17 +234,17 @@ public: const TileInfo info(dt, width, height); const CLTile tile(tile_name, info); - const TileVariable var = tile.scalar(x_coord, y_coord); + const TileVariable var = tile.scalar(y_coord, x_coord); - const std::string expected_var_name = var.str; - std::string actual_var_name = tile_name; - actual_var_name += "_" + std::to_string(y_coord); + const std::string actual_var_name = var.str; + std::string expected_var_name = tile_name; + expected_var_name += "_" + std::to_string(y_coord); if(width != 1) { - actual_var_name += ".s" + dec_to_hex_as_string(x_coord_clamped); + expected_var_name += ".s" + dec_to_hex_as_string(x_coord_clamped); } - VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, i); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); } return all_tests_passed; } @@ -264,9 +290,10 @@ public: // The status of this variable can change in VALIDATE_TEST() bool all_tests_passed = true; - const size_t num_tests = _x_coord.size(); + const size_t num_coords = _x_coord.size(); - for(size_t i = 0; i < num_tests; ++i) + int32_t test_idx = 0; + for(size_t i = 0; i < num_coords; ++i) { const int32_t height = _height[i]; const int32_t x_coord = _x_coord[i]; @@ -277,21 +304,21 @@ public: const TileInfo info(dt, width, height); const CLTile tile(tile_name, info); - const TileVariable var = tile.scalar(x_coord, y_coord); + const TileVariable var = tile.scalar(y_coord, x_coord); - const std::string expected_var_name = var.str; - std::string actual_var_name = tile_name; + const std::string actual_var_name = var.str; + std::string expected_var_name = tile_name; if(height != 1) { - actual_var_name += "_" + std::to_string(y_coord_clamped); + expected_var_name += "_" + std::to_string(y_coord_clamped); } if(width != 1) { - actual_var_name += ".s" + dec_to_hex_as_string(x_coord); + expected_var_name += ".s" + dec_to_hex_as_string(x_coord); } - VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, i); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); } return all_tests_passed; } @@ -306,6 +333,135 @@ private: std::vector _x_coord {}; std::vector _y_coord {}; }; -} -#endif /* COMPUTE_KERNEL_WRITER_TESTS_CLTENSOR_HPP */ +class CLTileAccessVectorVariablesTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t width = 8; + const DataType dt = DataType::Fp32; + + CLTileAccessVectorVariablesTest() + { + _heights.push_back(1); + _heights.push_back(2); + _heights.push_back(3); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + int32_t test_idx = 0; + for(const auto &height : _heights) + { + const TileInfo info(dt, width, height); + const CLTile tile(tile_name, info); + + for(int32_t row = 0; row < height; ++row) + { + const TileVariable var = tile.vector(row); + + const std::string actual_var_name = var.str; + std::string expected_var_name = tile_name; + if(height != 1) + { + expected_var_name += "_" + std::to_string(row); + } + + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTileAccessVectorVariablesTest"; + } + +private: + std::vector _heights {}; +}; + +class CLTileAccessSubVectorVariablesTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t width = 8; + const int32_t height = 3; + const DataType dt = DataType::Fp32; + + CLTileAccessSubVectorVariablesTest() + { + _subwidths.push_back(1); + _subwidths.push_back(2); + _subwidths.push_back(3); + _subwidths.push_back(4); + _offsets.push_back(1); + _offsets.push_back(3); + _offsets.push_back(4); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + size_t test_idx = 0; + + for(auto &col_start : _offsets) + { + for(const auto &subwidth : _subwidths) + { + const TileInfo info(dt, width, height); + const CLTile tile(tile_name, info); + + for(int32_t row = 0; row < height; ++row) + { + std::string expected_var_name = tile_name; + if(height != 1) + { + expected_var_name += "_" + std::to_string(row); + } + + if(width != 1) + { + expected_var_name += ".s"; + } + + int32_t col = col_start; + for(; col < col_start + subwidth - 1; ++col) + { + if(width != 1) + { + expected_var_name += dec_to_hex_as_string(col); + } + } + + if(width != 1) + { + expected_var_name += dec_to_hex_as_string(col); + } + + const std::string actual_var_name = tile.vector(row, col_start, subwidth).str; + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + } + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTileAccessSubVectorVariablesTest"; + } + +private: + std::vector _subwidths {}; + std::vector _offsets {}; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_TESTS_CLTILETEST_HPP */ -- cgit v1.2.1