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 --- .../validation/tests/CLConstantTileTest.hpp | 369 +++++++++++++++++++++ .../validation/tests/CLTileTest.hpp | 226 +++++++++++-- 2 files changed, 560 insertions(+), 35 deletions(-) create mode 100644 compute_kernel_writer/validation/tests/CLConstantTileTest.hpp (limited to 'compute_kernel_writer/validation/tests') 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