diff options
Diffstat (limited to 'compute_kernel_writer/validation/tests')
24 files changed, 4085 insertions, 0 deletions
diff --git a/compute_kernel_writer/validation/tests/CLConstantTileTest.hpp b/compute_kernel_writer/validation/tests/CLConstantTileTest.hpp new file mode 100644 index 0000000000..f10ad10146 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLConstantTileTest.hpp @@ -0,0 +1,371 @@ +/* + * 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 CKW_TESTS_CLCONSTANTTILETEST_HPP +#define CKW_TESTS_CLCONSTANTTILETEST_HPP + +#include "common/Common.h" +#include "src/Helpers.h" +#include "src/cl/CLHelpers.h" +#include "src/cl/CLTile.h" + +#include <random> +#include <string> +#include <vector> + +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 CLTile 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<TileContainer> _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<int32_t>(0), width - 1); + + TileContainer container = TileContainer(height, std::vector<std::string>(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 CLTile 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<int32_t> _width{}; + std::vector<int32_t> _x_coord{}; + std::vector<int32_t> _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<int32_t>(0), height - 1); + + TileContainer container = TileContainer(height, std::vector<std::string>(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 CLTile 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<int32_t> _height{}; + std::vector<int32_t> _x_coord{}; + std::vector<int32_t> _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 CLTile 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<TileContainer> _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 CLTile 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<TileContainer> _values{}; + std::vector<int32_t> _subwidths{}; + std::vector<int32_t> _offsets{}; +}; + +} // namespace ckw + +#endif // CKW_TESTS_CLCONSTANTTILETEST_HPP diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterAssignTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterAssignTest.h new file mode 100644 index 0000000000..f32f797a01 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterAssignTest.h @@ -0,0 +1,101 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERASSIGNTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERASSIGNTEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> +#include <vector> + +namespace ckw +{ + +class CLKernelWriterAssignTest : public ITest +{ +public: + CLKernelWriterAssignTest() + { + _tests.push_back({ 1, 1, 1, 1, DataType::Fp32, "G0__dst = G0__src;\n" }); // Scalar. + + _tests.push_back({ 1, 3, 1, 3, DataType::Fp16, "G0__dst = G0__src;\n" }); // Whole vector. + + _tests.push_back({ 2, 4, 2, 4, DataType::Int8, "G0__dst__0 = G0__src__0;\nG0__dst__1 = G0__src__1;\n" }); // Whole tile. + + _tests.push_back({ 2, 3, 1, 3, DataType::Uint8, "G0__dst__0 = G0__src;\nG0__dst__1 = G0__src;\n" }); // Y-dimension broadcast. + + _tests.push_back({ 2, 4, 2, 1, DataType::Fp32, "G0__dst__0 = (float4)G0__src__0;\nG0__dst__1 = (float4)G0__src__1;\n" }); // X-dimension broadcast. + + _tests.push_back({ 2, 3, 1, 1, DataType::Fp16, "G0__dst__0 = (half3)G0__src;\nG0__dst__1 = (half3)G0__src;\n" }); // X and y dimension broadcast. + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + for(const auto &test : _tests) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + auto dst = writer.declare_tile("dst", TileInfo(test.data_type, test.dst_height, test.dst_width)); + auto src = writer.declare_tile("src", TileInfo(test.data_type, test.src_height, test.src_width)); + + writer.start_capture_code(); + + writer.op_assign(dst, src); + + VALIDATE_TEST(writer.check_added_code(test.expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterAssignTest"; + } + +private: + struct TestInfo + { + int32_t dst_height; + int32_t dst_width; + int32_t src_height; + int32_t src_width; + DataType data_type; + std::string expected_code; + }; + + std::vector<TestInfo> _tests{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERASSIGNTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterBinaryOpTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterBinaryOpTest.h new file mode 100644 index 0000000000..44a4df1ce1 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterBinaryOpTest.h @@ -0,0 +1,127 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERBINARYOPTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERBINARYOPTEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> +#include <vector> + +namespace ckw +{ + +class CLKernelWriterBinaryOpTest : public ITest +{ +public: + CLKernelWriterBinaryOpTest() + { + // dst_height, dst_width, dst_data_type, lhs_height, lhs_width, rhs_height, rhs_width, src_data_type, op, expected_code + _tests.push_back({ 1, 1, DataType::Fp32, 1, 1, 1, 1, DataType::Fp32, BinaryOp::Add, "G0__dst = G0__lhs + G0__rhs;\n" }); // Scalar. + + _tests.push_back({ 1, 3, DataType::Bool, 1, 3, 1, 3, DataType::Fp16, BinaryOp::Equal, "G0__dst = G0__lhs == G0__rhs;\n" }); // Whole vector. + + _tests.push_back({ 2, 4, DataType::Int8, 2, 4, 2, 4, DataType::Int8, BinaryOp::Min, "G0__dst__0 = min(G0__lhs__0, G0__rhs__0);\nG0__dst__1 = min(G0__lhs__1, G0__rhs__1);\n" }); // Whole tile. + + _tests.push_back({ 2, 3, DataType::Uint8, 1, 3, 2, 3, DataType::Uint8, BinaryOp::BitwiseXOR, "G0__dst__0 = G0__lhs ^ G0__rhs__0;\nG0__dst__1 = G0__lhs ^ G0__rhs__1;\n" }); // LHS y-dimension broadcast. + + _tests.push_back({ 2, 3, DataType::Bool, 2, 3, 1, 3, DataType::Fp32, BinaryOp::Less, "G0__dst__0 = G0__lhs__0 < G0__rhs;\nG0__dst__1 = G0__lhs__1 < G0__rhs;\n" }); // RHS y-dimension broadcast. + + _tests.push_back({ 2, 3, DataType::Fp16, 1, 3, 1, 3, DataType::Fp16, BinaryOp::Max, "G0__dst__0 = fmax(G0__lhs, G0__rhs);\nG0__dst__1 = fmax(G0__lhs, G0__rhs);\n" }); // LHS and RHS y-dimension broadcast. + + _tests.push_back({ 2, 4, DataType::Fp32, 2, 1, 2, 4, DataType::Fp32, BinaryOp::Div, "G0__dst__0 = (float4)G0__lhs__0 / G0__rhs__0;\nG0__dst__1 = (float4)G0__lhs__1 / G0__rhs__1;\n" }); // LHS x-dimension broadcast. + + _tests.push_back({ 2, 4, DataType::Fp16, 2, 4, 2, 1, DataType::Fp16, BinaryOp::Mod, "G0__dst__0 = G0__lhs__0 % (half4)G0__rhs__0;\nG0__dst__1 = G0__lhs__1 % (half4)G0__rhs__1;\n" }); // RHS x-dimension broadcast. + + _tests.push_back({ 2, 4, DataType::Bool, 2, 1, 2, 1, DataType::Fp32, BinaryOp::GreaterEqual, "G0__dst__0 = (float4)G0__lhs__0 >= (float4)G0__rhs__0;\nG0__dst__1 = (float4)G0__lhs__1 >= (float4)G0__rhs__1;\n" }); // LHS and RHS x-dimension broadcast. + + _tests.push_back({ 2, 2, DataType::Fp32, 2, 3, 2, 3, DataType::Fp32, BinaryOp::MatMul_Nt_T, + "G0__dst__0.s0 = fma(G0__lhs__0.s0, G0__rhs__0.s0, G0__dst__0.s0);\n" + "G0__dst__0.s0 = fma(G0__lhs__0.s1, G0__rhs__0.s1, G0__dst__0.s0);\n" + "G0__dst__0.s0 = fma(G0__lhs__0.s2, G0__rhs__0.s2, G0__dst__0.s0);\n" + "G0__dst__0.s1 = fma(G0__lhs__0.s0, G0__rhs__1.s0, G0__dst__0.s1);\n" + "G0__dst__0.s1 = fma(G0__lhs__0.s1, G0__rhs__1.s1, G0__dst__0.s1);\n" + "G0__dst__0.s1 = fma(G0__lhs__0.s2, G0__rhs__1.s2, G0__dst__0.s1);\n" + "G0__dst__1.s0 = fma(G0__lhs__1.s0, G0__rhs__0.s0, G0__dst__1.s0);\n" + "G0__dst__1.s0 = fma(G0__lhs__1.s1, G0__rhs__0.s1, G0__dst__1.s0);\n" + "G0__dst__1.s0 = fma(G0__lhs__1.s2, G0__rhs__0.s2, G0__dst__1.s0);\n" + "G0__dst__1.s1 = fma(G0__lhs__1.s0, G0__rhs__1.s0, G0__dst__1.s1);\n" + "G0__dst__1.s1 = fma(G0__lhs__1.s1, G0__rhs__1.s1, G0__dst__1.s1);\n" + "G0__dst__1.s1 = fma(G0__lhs__1.s2, G0__rhs__1.s2, G0__dst__1.s1);\n" }); + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + for(const auto &test : _tests) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + auto dst = writer.declare_tile("dst", TileInfo(test.dst_data_type, test.dst_height, test.dst_width)); + auto lhs = writer.declare_tile("lhs", TileInfo(test.src_data_type, test.lhs_height, test.lhs_width)); + auto rhs = writer.declare_tile("rhs", TileInfo(test.src_data_type, test.rhs_height, test.rhs_width)); + + writer.start_capture_code(); + + writer.op_binary(dst, test.op, lhs, rhs); + + VALIDATE_TEST(writer.check_added_code(test.expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterBinaryOpTest"; + } + +private: + struct TestInfo + { + int32_t dst_height; + int32_t dst_width; + DataType dst_data_type; + int32_t lhs_height; + int32_t lhs_width; + int32_t rhs_height; + int32_t rhs_width; + DataType src_data_type; + BinaryOp op; + std::string expected_code; + }; + + std::vector<TestInfo> _tests{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERBINARYOPTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterCastTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterCastTest.h new file mode 100644 index 0000000000..a185cce545 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterCastTest.h @@ -0,0 +1,104 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERCASTTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERCASTTEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/ConvertPolicy.h" +#include "ckw/types/DataType.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> +#include <vector> + +namespace ckw +{ + +class CLKernelWriterCastTest : public ITest +{ +public: + CLKernelWriterCastTest() + { + _tests.push_back({ 1, 1, DataType::Fp16, 1, 1, DataType::Fp32, ConvertPolicy::None, "G0__dst = convert_half(G0__src);\n" }); // Scalar. + + _tests.push_back({ 1, 3, DataType::Int32, 1, 3, DataType::Fp16, ConvertPolicy::Saturate, "G0__dst = convert_int3_sat(G0__src);\n" }); // Whole vector. + + _tests.push_back({ 2, 4, DataType::Uint16, 2, 4, DataType::Int8, ConvertPolicy::Saturate, "G0__dst__0 = convert_ushort4_sat(G0__src__0);\nG0__dst__1 = convert_ushort4_sat(G0__src__1);\n" }); // Whole tile. + + _tests.push_back({ 2, 3, DataType::Int8, 1, 3, DataType::Uint8, ConvertPolicy::None, "G0__dst__0 = convert_char3(G0__src);\nG0__dst__1 = convert_char3(G0__src);\n" }); // Y-dimension broadcast. + + _tests.push_back({ 2, 4, DataType::Fp16, 2, 1, DataType::Fp32, ConvertPolicy::None, "G0__dst__0 = (half4)convert_half(G0__src__0);\nG0__dst__1 = (half4)convert_half(G0__src__1);\n" }); // X-dimension broadcast. + + _tests.push_back({ 2, 3, DataType::Fp32, 1, 1, DataType::Fp16, ConvertPolicy::None, "G0__dst__0 = (float3)convert_float(G0__src);\nG0__dst__1 = (float3)convert_float(G0__src);\n" }); // X and y dimension broadcast. + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + for(const auto &test : _tests) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + auto dst = writer.declare_tile("dst", TileInfo(test.dst_data_type, test.dst_height, test.dst_width)); + auto src = writer.declare_tile("src", TileInfo(test.src_data_type, test.src_height, test.src_width)); + + writer.start_capture_code(); + + writer.op_cast(dst, src, test.policy); + + VALIDATE_TEST(writer.check_added_code(test.expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterCastTest"; + } + +private: + struct TestInfo + { + int32_t dst_height; + int32_t dst_width; + DataType dst_data_type; + int32_t src_height; + int32_t src_width; + DataType src_data_type; + ConvertPolicy policy; + std::string expected_code; + }; + + std::vector<TestInfo> _tests{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERCASTTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterCommentTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterCommentTest.h new file mode 100644 index 0000000000..b36c3905ec --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterCommentTest.h @@ -0,0 +1,74 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERCOMMENTTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERCOMMENTTEST_H + +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterCommentTest : public ITest +{ +public: + CLKernelWriterCommentTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor<CLKernelWriter> writer; + + writer.op_comment("previous code"); + + writer.start_capture_code(); + + writer.op_comment("code under test 0"); + writer.op_comment("code under test 1"); + +#ifdef COMPUTE_KERNEL_WRITER_DEBUG_ENABLED + constexpr auto expected_code = "// code under test 0\n// code under test 1\n"; +#else // COMPUTE_KERNEL_WRITER_DEBUG_ENABLED + constexpr auto expected_code = ""; +#endif // COMPUTE_KERNEL_WRITER_DEBUG_ENABLED + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterCommentTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERCOMMENTTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterDeclareConstantTileTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterDeclareConstantTileTest.h new file mode 100644 index 0000000000..661a8328e8 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterDeclareConstantTileTest.h @@ -0,0 +1,106 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARECONSTANTTILETEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARECONSTANTTILETEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/ConstantData.h" +#include "ckw/types/DataType.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/KernelWriterInterceptor.h" +#include "validation/tests/common/Common.h" + +#include <string> +#include <tuple> +#include <vector> + +namespace ckw +{ +class CLKernelWriterDeclareConstantTileTest : public ITest +{ + using TestConfig = std::tuple<ConstantData, DataType, int32_t, int32_t, std::string>; +public: + CLKernelWriterDeclareConstantTileTest() + { + _configs = { + // ConstantData, DataType, Height, Width + {ConstantData({{1}}, DataType::Int32), DataType::Int32, 1, 1, + "G0__tile = ((int)(1));\n"}, + {ConstantData({{1U}}, DataType::Uint32), DataType::Uint32, 1, 1, + "G0__tile = ((uint)(1));\n"}, + {ConstantData({{1, 2}}, DataType::Int8), DataType::Int8, 1, 2, + "G0__tile = ((char2)(1, 2));\n"}, + {ConstantData({{1, -2}, {-3, 4}}, DataType::Int32), DataType::Int32, 2, 2, + "G0__tile__0 = ((int2)(1, -2));\nG0__tile__1 = ((int2)(-3, 4));\n"}, + {ConstantData({{1.0f, -2.0f}}, DataType::Fp16), DataType::Fp16, 1, 2, + "G0__tile = ((half2)(1.000000000e+00, -2.000000000e+00));\n"}, + {ConstantData({{/* FLT_MAX */ 340282346638528859811704183484516925440.0f, -2.0f, 3.0f}}, DataType::Fp32), DataType::Fp32, 1, 3, + "G0__tile = ((float3)(3.402823466e+38, -2.000000000e+00, 3.000000000e+00));\n"}, + {ConstantData({{1.0f, -1e-20f, 2e-20f, /* FLT_EPS */ 1.1920928955078125e-7f}}, DataType::Fp32), DataType::Fp32, 1, 4, + "G0__tile = ((float4)(1.000000000e+00, -9.999999683e-21, 1.999999937e-20, 1.192092896e-07));\n"}, + {ConstantData({{0.5f, 2.1e-30f, /* FLT_MIN */ 1.175494350822287507969e-38f}}, DataType::Fp32), DataType::Fp32, 1, 3, + "G0__tile = ((float3)(5.000000000e-01, 2.099999969e-30, 1.175494351e-38));\n"}, + {ConstantData({{true}, {false}, {false}}, DataType::Bool), DataType::Bool, 3, 1, + "G0__tile__0 = ((bool)(1));\nG0__tile__1 = ((bool)(0));\nG0__tile__2 = ((bool)(0));\n"} + }; + } + + bool run() override + { + bool all_tests_passed = true; + int test_idx = 0; + + for(TestConfig _config: _configs) + { + KernelWriterInterceptor<CLKernelWriter> writer; + const ConstantData const_data = std::get<0>(_config); + const DataType data_type = std::get<1>(_config); + const size_t height = std::get<2>(_config); + const size_t width = std::get<3>(_config); + const std::string expected_code = std::get<4>(_config); + + TileOperand tile = writer.declare_tile("tile", TileInfo(data_type, height, width)); + writer.start_capture_code(); + TileOperand const_tile = writer.declare_constant_tile(const_data); + writer.op_assign(tile, const_tile); + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_idx++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterDeclareConstantTileTest"; + } + +private: + std::vector<TestConfig> _configs {}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARECONSTANTTILETEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterDeclareTensorTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterDeclareTensorTest.h new file mode 100644 index 0000000000..855c747f13 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterDeclareTensorTest.h @@ -0,0 +1,115 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARETENSORTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARETENSORTEST_H + +#include "ckw/Error.h" +#include "ckw/Kernel.h" +#include "ckw/KernelArgument.h" +#include "ckw/TensorInfo.h" +#include "ckw/types/TensorComponentType.h" +#include "ckw/types/TensorDataLayout.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" + +namespace ckw +{ + +class CLKernelWriterDeclareTensorTest : public ITest +{ +public: + CLKernelWriterDeclareTensorTest() + { + } + + std::string name() override + { + return "CLKernelWriterDeclareTensorTest"; + } + + bool run() override + { + auto all_tests_passed = true; + + CLKernelWriter writer; + + auto src = writer.declare_tensor_argument("src", TensorInfo(DataType::Fp32, TensorShape{ 2, 3, 4, 5 }, TensorDataLayout::Nhwc, 0)); + auto dst = writer.declare_tensor_argument("dst", TensorInfo(DataType::Fp32, TensorShape{ 6, 7, 8, 9 }, TensorDataLayout::Nhwc, 1)); + + auto src_dim0 = src.dim0(); + auto src_stride2 = src.stride2(); + auto src_offset_element = src.offset_first_element_in_bytes(); + + auto dst_dim1 = dst.dim0(); + + auto src_dim0_again = src.dim0(); + + CKW_UNUSED(src_dim0, src_stride2, src_offset_element, dst_dim1, src_dim0_again); + + const auto kernel = writer.emit_kernel("test_kernel"); + + const std::string expected_code = + "__kernel void test_kernel\n" + "(\n" + "int G0__src_dim0,\n" + "int G0__src_stride2,\n" + "int G0__src_offset_first_element,\n" + "int G0__dst_dim0\n" + ")\n" + "{\n" + "}\n"; + + std::string actual_code = kernel->source_code(); + + std::size_t pos = actual_code.find("__kernel"); + + if (pos != std::string::npos) + { + // Remove text before "__kernel" + actual_code = actual_code.substr(pos); + } + + int test_id = 0; + VALIDATE_TEST(kernel->arguments().size() == 4, all_tests_passed, test_id++); + test_tensor_component_argument(kernel->arguments()[0], 0, TensorComponentType::Dim0, all_tests_passed, test_id); + test_tensor_component_argument(kernel->arguments()[1], 0, TensorComponentType::Stride2, all_tests_passed, test_id); + test_tensor_component_argument(kernel->arguments()[2], 0, TensorComponentType::OffsetFirstElement, all_tests_passed, test_id); + test_tensor_component_argument(kernel->arguments()[3], 1, TensorComponentType::Dim0, all_tests_passed, test_id); + VALIDATE_TEST(actual_code == expected_code, all_tests_passed, test_id++); + + return all_tests_passed; + } + + void test_tensor_component_argument(const KernelArgument &arg, int32_t tensor_id, TensorComponentType component_type, bool &all_tests_passed, int &test_id) + { + VALIDATE_TEST(arg.type() == KernelArgument::Type::TensorComponent, all_tests_passed, test_id++); + VALIDATE_TEST(arg.id() == tensor_id, all_tests_passed, test_id++); + VALIDATE_TEST(arg.tensor_component_type() == component_type, all_tests_passed, test_id++); + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARETENSORTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterDeclareTileTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterDeclareTileTest.h new file mode 100644 index 0000000000..4f728bc1bf --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterDeclareTileTest.h @@ -0,0 +1,99 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITER_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITER_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/KernelWriterInterceptor.h" +#include "validation/tests/common/Common.h" + +#include <vector> + +namespace ckw +{ + +using CLKernelWriterDeclareTileConfig = std::tuple<DataType, int32_t, int32_t, std::string>; + +class CLKernelWriterDeclareTileTest : public ITest +{ +public: + CLKernelWriterDeclareTileTest() + { + _configs = { + {DataType::Fp32, 4, 4, "float4 G0__a_tile"}, + {DataType::Uint8, 4, 1, "uchar G0__a_tile"}, + {DataType::Int8, 4, 2, "char2 G0__a_tile"}, + {DataType::Bool, 9, 3, "bool3 G0__a_tile"}, + {DataType::Fp16, 4, 16, "half16 G0__a_tile"}, + {DataType::Uint32, 1, 8, "uint8 G0__a_tile"}, + {DataType::Uint16, 2, 3, "ushort3 G0__a_tile"}, + }; + } + + bool run() override + { + bool all_tests_passed = true; + int32_t test_idx = 0; + + for(auto _config: _configs) + { + KernelWriterInterceptor<CLKernelWriter> writer; + writer.start_capture_code(); + + const DataType data_type = std::get<0>(_config); + const int32_t height = std::get<1>(_config); + const int32_t width = std::get<2>(_config); + const std::string prefix = std::get<3>(_config); + + // expected output + std::string expected_code = ""; + for(int32_t row = 0; row < height; ++row) + { + expected_code += prefix + ((height > 1) ? std::string("__") + std::to_string(row) : "") + ";\n"; + } + + TileInfo tile_info(data_type, height, width); + writer.declare_tile("a_tile", tile_info); + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_idx++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterDeclareTileTest"; + } + +private: + std::vector<CLKernelWriterDeclareTileConfig> _configs {}; +}; + +} // namespace ckw + +#endif /* CKW_VALIDATION_TESTS_CLKERNELWRITER_H */ diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterForTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterForTest.h new file mode 100644 index 0000000000..beb39966b2 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterForTest.h @@ -0,0 +1,85 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERFORTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERFORTEST_H + +#include "ckw/Error.h" +#include "ckw/TileInfo.h" +#include "ckw/types/Operators.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterForTest : public ITest +{ +public: + CLKernelWriterForTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor<CLKernelWriter> writer; + + auto idx = writer.declare_tile("idx", TileInfo(DataType::Int32, 1, 1)); + auto len = writer.declare_tile("len", TileInfo(DataType::Int32, 1, 1)); + auto addr = writer.declare_tile("addr", TileInfo(DataType::Int32, 1, 1)); + auto esize = writer.declare_tile("esize", TileInfo(DataType::Int32, 1, 1)); + + writer.start_capture_code(); + + writer.op_for_loop( + idx, BinaryOp::Less, len, addr, AssignmentOp::Increment, esize, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp32, 1, 3)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "for (; G0__idx < G0__len; G0__addr += G0__esize)\n" + "{\n" + "float3 G1__tile;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterForTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERFORTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterGetGlobalIdTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterGetGlobalIdTest.h new file mode 100644 index 0000000000..fa34b3f5df --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterGetGlobalIdTest.h @@ -0,0 +1,72 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERGETGLOBALIDTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERGETGLOBALIDTEST_H + +#include "ckw/TileInfo.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterGetGlobalIdTest : public ITest +{ +public: + CLKernelWriterGetGlobalIdTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor<CLKernelWriter> writer; + + auto gid = writer.declare_tile("gid", TileInfo(DataType::Int32)); + + writer.start_capture_code(); + + writer.op_get_global_id(gid, 0); + writer.op_get_global_id(gid, 1); + writer.op_get_global_id(gid, 2); + + constexpr auto expected_code = "G0__gid = get_global_id(0);\nG0__gid = get_global_id(1);\nG0__gid = get_global_id(2);\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterGetGlobalIdTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERGETGLOBALIDTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h new file mode 100644 index 0000000000..3964bd76d4 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterIfTest.h @@ -0,0 +1,175 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERIFTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERIFTEST_H + +#include "ckw/Error.h" +#include "ckw/TileInfo.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> + +namespace ckw +{ + +class CLKernelWriterIfTest : public ITest +{ +public: + CLKernelWriterIfTest() + { + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + KernelWriterInterceptor<CLKernelWriter> writer; + + auto lhs = writer.declare_tile("lhs", TileInfo(DataType::Fp32, 1, 1)); + auto rhs = writer.declare_tile("rhs", TileInfo(DataType::Fp32, 1, 1)); + + // The first if block. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp16, 2, 3)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "half3 G1__tile__0;\n" + "half3 G1__tile__1;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + // The second if block - The ID space inside the if block should change. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp16, 2, 3)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "half3 G2__tile__0;\n" + "half3 G2__tile__1;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + // The if-else block - The ID space in each block should change. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp16, 2, 3)); + CKW_UNUSED(tile); + }); + writer.op_else( + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Uint8, 1, 4)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "half3 G3__tile__0;\n" + "half3 G3__tile__1;\n" + "}\n" + "else\n" + "{\n" + "uchar4 G4__tile;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + // If-else if block. + { + writer.start_capture_code(); + + writer.op_if( + lhs, BinaryOp::Equal, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Fp32, 1, 3)); + CKW_UNUSED(tile); + }); + writer.op_else_if( + lhs, BinaryOp::Less, rhs, + [&]() + { + auto tile = writer.declare_tile("tile", TileInfo(DataType::Int8, 1, 4)); + CKW_UNUSED(tile); + }); + + constexpr auto expected_code = + "if (G0__lhs == G0__rhs)\n" + "{\n" + "float3 G5__tile;\n" + "}\n" + "else if (G0__lhs < G0__rhs)\n" + "{\n" + "char4 G6__tile;\n" + "}\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterIfTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERIFTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterOpLoadIndirectTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterOpLoadIndirectTest.h new file mode 100644 index 0000000000..dacf3cd435 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterOpLoadIndirectTest.h @@ -0,0 +1,216 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITEROPLOADINDIRECTTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITEROPLOADINDIRECTTEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "ckw/TensorSampler.h" +#include "ckw/types/MemoryOperation.h" +#include "ckw/types/TensorSamplerTypes.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/KernelWriterInterceptor.h" +#include "validation/tests/common/Common.h" + +#include <vector> + +namespace ckw +{ + +class CLKernelWriterOpLoadIndirectTest : public ITest +{ +private: + using AddressModeX = TensorSamplerAddressModeX; + using AddressModeY = TensorSamplerAddressModeY; + using AddressModeZ = TensorSamplerAddressModeZ; + using Format = TensorSamplerFormat; + using Storage = TensorStorageType; + + struct Coordinates + { + Coordinates(std::string x, std::string y, std::string z, std::string batch) + : x(x), y(y), z(z), batch(batch) + { + } + + std::string x; + std::string y; + std::string z; + std::string batch; + }; + + struct SamplerData + { + SamplerData(Format format, AddressModeX mode_x, AddressModeY mode_y, AddressModeZ mode_z) + : format(format), mode_x(mode_x), mode_y(mode_y), mode_z(mode_z) + { + } + + Format format; + AddressModeX mode_x; + AddressModeY mode_y; + AddressModeZ mode_z; + }; + + using CLKernelWriterOpLoadIndirectConfig = std::tuple<TileInfo, TensorStorageType, SamplerData, Coordinates, std::string>; + +public: + CLKernelWriterOpLoadIndirectTest() + { + const std::string fp_2x3_tile = R"_( +G0__tile__0 = vload3(0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__indirect_addr__0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +G0__tile__1 = vload3(0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__indirect_addr__1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +)_"; + + const std::string half_2x4_yz_collapsed_y_clamped_to_border_max_only_image = R"_( +G0__tile__0 = read_imageh(G0__tensor_img2d, CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST, (int2)((G0__x) >> 2, (G0__indirect_addr__0 + (G0__b) * G0__tensor_dim1xdim2 * 1))); +G0__tile__1 = read_imageh(G0__tensor_img2d, CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST, (int2)((G0__x) >> 2, (G0__indirect_addr__1 + (G0__b) * G0__tensor_dim1xdim2 * 1))); +)_"; + + const std::string int_2x4_y_skip_less_than_zero = R"_( +if(G0__indirect_addr__0 >= 0) +{ +G0__tile__0 = vload4(0, (__global int*)(G0__tensor_ptr + (G0__x) * sizeof(int) + (G0__indirect_addr__0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +} +if(G0__indirect_addr__1 >= 0) +{ +G0__tile__1 = vload4(0, (__global int*)(G0__tensor_ptr + (G0__x) * sizeof(int) + (G0__indirect_addr__1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +} +)_"; + + // tensor shape in x-dim is 10 (thus the 8, 2 vloads in if, else blocks respectively) + const std::string uint16_3x8_yz_collapsed_b_eq_0_x_overlapping_min_y_skip_less_than_zero = R"_( +if(G0__x > 0) +{ +if(G0__indirect_addr__0 >= 0) +{ +G0__tile__0 = vload8(0, (__global ushort*)(G0__tensor_ptr + (G0__x) * sizeof(ushort) + (G0__indirect_addr__0) * G0__tensor_stride1 + (G0__0) * G0__tensor_stride3)); +} +if(G0__indirect_addr__1 >= 0) +{ +G0__tile__1 = vload8(0, (__global ushort*)(G0__tensor_ptr + (G0__x) * sizeof(ushort) + (G0__indirect_addr__1) * G0__tensor_stride1 + (G0__0) * G0__tensor_stride3)); +} +if(G0__indirect_addr__2 >= 0) +{ +G0__tile__2 = vload8(0, (__global ushort*)(G0__tensor_ptr + (G0__x) * sizeof(ushort) + (G0__indirect_addr__2) * G0__tensor_stride1 + (G0__0) * G0__tensor_stride3)); +} +} +else +{ +if(G0__indirect_addr__0 >= 0) +{ +G0__tile__0.s01 = vload2(0, (__global ushort*)(G0__tensor_ptr + (G0__x + 0) * sizeof(ushort) + (G0__indirect_addr__0) * G0__tensor_stride1 + (G0__0) * G0__tensor_stride3)); +} +if(G0__indirect_addr__1 >= 0) +{ +G0__tile__1.s01 = vload2(0, (__global ushort*)(G0__tensor_ptr + (G0__x + 0) * sizeof(ushort) + (G0__indirect_addr__1) * G0__tensor_stride1 + (G0__0) * G0__tensor_stride3)); +} +if(G0__indirect_addr__2 >= 0) +{ +G0__tile__2.s01 = vload2(0, (__global ushort*)(G0__tensor_ptr + (G0__x + 0) * sizeof(ushort) + (G0__indirect_addr__2) * G0__tensor_stride1 + (G0__0) * G0__tensor_stride3)); +} +} +)_"; + + // Configs Bundled + _configs = { + { + TileInfo(DataType::Fp32, 2, 3), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::None, AddressModeY::None, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + fp_2x3_tile + }, + { + TileInfo(DataType::Fp16, 2, 4), + TensorStorageType::Texture2dReadOnly, + SamplerData(Format::Dim0_Dim1xDim2_1, AddressModeX::None, AddressModeY::ClampToBorderMaxOnly, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + half_2x4_yz_collapsed_y_clamped_to_border_max_only_image + }, + { + TileInfo(DataType::Int32, 2, 4), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::None, AddressModeY::SkipLessThanZero, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + int_2x4_y_skip_less_than_zero + }, + { + TileInfo(DataType::Uint16, 3, 8), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1xDim2_1, AddressModeX::OverlappingMin, AddressModeY::SkipLessThanZero, AddressModeZ::None), + Coordinates("x", "y", "z", "0"), + uint16_3x8_yz_collapsed_b_eq_0_x_overlapping_min_y_skip_less_than_zero + } + }; + } + + bool run() override + { + bool all_tests_passed = true; + int32_t test_idx = 0; + + for(auto _config: _configs) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + const TileInfo tile_info = std::get<0>(_config); + const Storage storage = std::get<1>(_config); + const SamplerData sampler_data = std::get<2>(_config); + const Coordinates coord = std::get<3>(_config); + const std::string expected_code = std::get<4>(_config).substr(1); // ignore initial newline, which was added for convenience + + TileOperand tile_op = writer.declare_tile("tile", TileInfo(tile_info.data_type(), tile_info.height(), tile_info.width())); + TileOperand indirect_addr_op = writer.declare_tile("indirect_addr", TileInfo(DataType::Int32, tile_info.height(), 1)); // (M0, 1) + TileOperand x_op = writer.declare_tile(coord.x, TileInfo(DataType::Int32)); + TileOperand z_op = writer.declare_tile(coord.z, TileInfo(DataType::Int32)); + TileOperand batch_op = writer.declare_tile(coord.batch, TileInfo(DataType::Int32)); + + TensorShape tensor_shape {10, 10, 10, 10}; + TensorInfo tensor_info(tile_info.data_type(), tensor_shape, TensorDataLayout::Nhwc, 0 /* id */); + TensorOperand tensor_op = writer.declare_tensor_argument("tensor", tensor_info); + TensorSampler sampler(storage, sampler_data.format, sampler_data.mode_x, sampler_data.mode_y, sampler_data.mode_z); + + writer.start_capture_code(); + writer.op_load_indirect(tile_op, tensor_op, sampler, x_op, indirect_addr_op, z_op, batch_op); + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_idx++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterOpLoadIndirectTest"; + } + +private: + std::vector<CLKernelWriterOpLoadIndirectConfig> _configs {}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITEROPLOADINDIRECTTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterOpLoadStoreTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterOpLoadStoreTest.h new file mode 100644 index 0000000000..870e80ee9a --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterOpLoadStoreTest.h @@ -0,0 +1,325 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITEROPLOADSTORETEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITEROPLOADSTORETEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/KernelWriterInterceptor.h" +#include "validation/tests/common/Common.h" + +#include "ckw/TensorSampler.h" +#include "ckw/types/MemoryOperation.h" +#include "ckw/types/TensorSamplerTypes.h" + +#include <vector> + +namespace ckw +{ + +class CLKernelWriterOpLoadStoreTest : public ITest +{ +private: + using AddressModeX = TensorSamplerAddressModeX; + using AddressModeY = TensorSamplerAddressModeY; + using AddressModeZ = TensorSamplerAddressModeZ; + using Format = TensorSamplerFormat; + using Storage = TensorStorageType; + + struct Coordinates + { + Coordinates(std::string x, std::string y, std::string z, std::string batch) + : x(x), y(y), z(z), batch(batch) + { + } + + std::string x; + std::string y; + std::string z; + std::string batch; + }; + + struct SamplerData + { + SamplerData(Format format, AddressModeX mode_x, AddressModeY mode_y, AddressModeZ mode_z) + : format(format), mode_x(mode_x), mode_y(mode_y), mode_z(mode_z) + { + } + + Format format; + AddressModeX mode_x; + AddressModeY mode_y; + AddressModeZ mode_z; + }; + + struct Dilations + { + Dilations(std::string dilation_x, std::string dilation_y) + : dilation_x(dilation_x), dilation_y(dilation_y) + { + } + + std::string dilation_x; + std::string dilation_y; + }; + + using CLKernelWriterOpLoadStoreConfig = std::tuple<MemoryOperation, TileInfo, TensorStorageType, SamplerData, Coordinates, Dilations, std::string>; + +public: + CLKernelWriterOpLoadStoreTest() + { + // Cases + const std::string load_fp_2x3_tile = R"_( +G0__tile__0 = vload3(0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__y + 0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +G0__tile__1 = vload3(0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__y + 1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +)_"; + const std::string load_half_2x4_tile_image_clamp_y = R"_( +G0__tile__0 = read_imageh(G0__tensor_img2d, CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST, (int2)((G0__x) >> 2, (G0__y + 0 + (G0__z) * G0__tensor_dim1 + (G0__b) * G0__tensor_dim1 * G0__tensor_dim2))); +G0__tile__1 = read_imageh(G0__tensor_img2d, CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST, (int2)((G0__x) >> 2, (G0__y + 1 + (G0__z) * G0__tensor_dim1 + (G0__b) * G0__tensor_dim1 * G0__tensor_dim2))); +)_"; + const std::string store_fp_2x3_tile = R"_( +vstore3(G0__tile__0, 0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__y + 0) * G0__tensor_stride1 + (G0__b) * G0__tensor_stride3)); +vstore3(G0__tile__1, 0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__y + 1) * G0__tensor_stride1 + (G0__b) * G0__tensor_stride3)); +)_"; + const std::string store_int8_4x4_y_dilation_batch_eq_0 = R"_( +vstore4(G0__tile__0, 0, (__global char*)(G0__tensor_ptr + (((int)(1))) * sizeof(char) + (G0__y + 0 * G0__y_dilation) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(0))) * G0__tensor_stride3)); +vstore4(G0__tile__1, 0, (__global char*)(G0__tensor_ptr + (((int)(1))) * sizeof(char) + (G0__y + 1 * G0__y_dilation) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(0))) * G0__tensor_stride3)); +vstore4(G0__tile__2, 0, (__global char*)(G0__tensor_ptr + (((int)(1))) * sizeof(char) + (G0__y + 2 * G0__y_dilation) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(0))) * G0__tensor_stride3)); +vstore4(G0__tile__3, 0, (__global char*)(G0__tensor_ptr + (((int)(1))) * sizeof(char) + (G0__y + 3 * G0__y_dilation) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(0))) * G0__tensor_stride3)); +)_"; + // tensor dimension is 10 + const std::string load_fp_2x3_tile_x_overlapping_min_y_eq_0_batch_eq_1 = R"_( +if(G0__x > 0) +{ +G0__tile__0 = vload3(0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (((int)(0)) + 0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(1))) * G0__tensor_stride3)); +G0__tile__1 = vload3(0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (((int)(0)) + 1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(1))) * G0__tensor_stride3)); +} +else +{ +G0__tile__0.s0 = *((__global float*)(G0__tensor_ptr + (G0__x + 0) * sizeof(float) + (((int)(0)) + 0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(1))) * G0__tensor_stride3)); +G0__tile__1.s0 = *((__global float*)(G0__tensor_ptr + (G0__x + 0) * sizeof(float) + (((int)(0)) + 1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (((int)(1))) * G0__tensor_stride3)); +} +)_"; + const std::string store_fp_2x3_tile_x_overlapping_min_y_clamp_to_border_max_only = R"_( +if(G0__x > 0) +{ +if(G0__y + 0 < G0__tensor_dim1) +{ +vstore3(G0__tile__0, 0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__y + 0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +} +else +{ +G0__tile__0 = 0.0f; +} +if(G0__y + 1 < G0__tensor_dim1) +{ +vstore3(G0__tile__1, 0, (__global float*)(G0__tensor_ptr + (G0__x) * sizeof(float) + (G0__y + 1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)); +} +else +{ +G0__tile__1 = 0.0f; +} +} +else +{ +if(G0__y + 0 < G0__tensor_dim1) +{ +*((__global float*)(G0__tensor_ptr + (G0__x + 0) * sizeof(float) + (G0__y + 0) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)) = G0__tile__0.s0; +} +else +{ +G0__tile__0.s0 = 0.0f; +} +if(G0__y + 1 < G0__tensor_dim1) +{ +*((__global float*)(G0__tensor_ptr + (G0__x + 0) * sizeof(float) + (G0__y + 1) * G0__tensor_stride1 + (G0__z) * G0__tensor_stride2 + (G0__b) * G0__tensor_stride3)) = G0__tile__1.s0; +} +else +{ +G0__tile__1.s0 = 0.0f; +} +} +)_"; + const std::string store_half_2x4_tile_x_image_y_dilation = R"_( +write_imageh(G0__tensor_img2d, (int2)((G0__x) >> 2, (((int)(0)) + 0 * G0__y_dilation + (G0__z) * G0__tensor_dim1 + (((int)(1))) * G0__tensor_dim1 * G0__tensor_dim2)), G0__tile__0); +write_imageh(G0__tensor_img2d, (int2)((G0__x) >> 2, (((int)(0)) + 1 * G0__y_dilation + (G0__z) * G0__tensor_dim1 + (((int)(1))) * G0__tensor_dim1 * G0__tensor_dim2)), G0__tile__1); +)_"; + + // Configs Bundled + _configs = { + // op, tile, storage, sampler, coordinates, dilation, expected + { + MemoryOperation::Load, + TileInfo(DataType::Fp32, 2, 3), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::None, AddressModeY::None, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + Dilations("1", "1"), + load_fp_2x3_tile + }, + { + MemoryOperation::Load, + TileInfo(DataType::Fp16, 2, 4), + TensorStorageType::Texture2dReadOnly, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::None, AddressModeY::ClampToBorderMaxOnly, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + Dilations("1", "1"), + load_half_2x4_tile_image_clamp_y + }, + { + MemoryOperation::Store, + TileInfo(DataType::Fp32, 2, 3), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1xDim2_1,AddressModeX::None, AddressModeY::None, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + Dilations("1", "1"), + store_fp_2x3_tile + }, + { + MemoryOperation::Store, + TileInfo(DataType::Int8, 4, 4), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::None, AddressModeY::None, AddressModeZ::None), + Coordinates("1", "y", "z", "0"), + Dilations("1", "y_dilation"), + store_int8_4x4_y_dilation_batch_eq_0 + }, + { + MemoryOperation::Load, + TileInfo(DataType::Fp32, 2, 3), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::OverlappingMin, AddressModeY::None, AddressModeZ::None), + Coordinates("x", "0", "z", "1"), + Dilations("1", "1"), + load_fp_2x3_tile_x_overlapping_min_y_eq_0_batch_eq_1 + }, + { + MemoryOperation::Store, + TileInfo(DataType::Fp32, 2, 3), + TensorStorageType::BufferUint8Ptr, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::OverlappingMin, AddressModeY::ClampToBorderMaxOnly, AddressModeZ::None), + Coordinates("x", "y", "z", "b"), + Dilations("1", "1"), + store_fp_2x3_tile_x_overlapping_min_y_clamp_to_border_max_only + }, + { + MemoryOperation::Store, + TileInfo(DataType::Fp16, 2, 4), + TensorStorageType::Texture2dWriteOnly, + SamplerData(Format::Dim0_Dim1_Dim2, AddressModeX::None, AddressModeY::None, AddressModeZ::None), + Coordinates("x", "0", "z", "1"), + Dilations("1", "y_dilation"), + store_half_2x4_tile_x_image_y_dilation + } + }; + } + + TileOperand declare_tile_helper(KernelWriter &writer, std::string tile) + { + if(tile == "0" || tile == "1") + { + return writer.declare_constant_tile(ConstantData({{std::stoi(tile)}}, DataType::Int32)); + } + else + { + return writer.declare_tile(tile, TileInfo(DataType::Int32)); + } + } + + bool run() override + { + bool all_tests_passed = true; + int32_t test_idx = 0; + + for(auto _config: _configs) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + const MemoryOperation op = std::get<0>(_config); + const TileInfo tile_info = std::get<1>(_config); + const Storage storage = std::get<2>(_config); + const SamplerData sampler_data = std::get<3>(_config); + const Coordinates coord = std::get<4>(_config); + const Dilations dilations = std::get<5>(_config); + const std::string expected_code = std::get<6>(_config).substr(1); // ignore initial newline, which was added for convenience + + TileOperand tile_op = writer.declare_tile("tile", tile_info); + TileOperand x_op = declare_tile_helper(writer, coord.x); + TileOperand y_op = declare_tile_helper(writer, coord.y); + TileOperand z_op = declare_tile_helper(writer, coord.z); + TileOperand batch_op = declare_tile_helper(writer, coord.batch); + TileOperand dil_x_op = declare_tile_helper(writer, dilations.dilation_x); + TileOperand dil_y_op = declare_tile_helper(writer, dilations.dilation_y); + + TensorShape tensor_shape {10, 10, 10, 10}; + TensorInfo tensor_info(tile_info.data_type(), tensor_shape, TensorDataLayout::Nhwc, 0 /* id */); + TensorOperand tensor_op = writer.declare_tensor_argument("tensor", tensor_info); + TensorSampler sampler(storage, sampler_data.format, sampler_data.mode_x, sampler_data.mode_y, sampler_data.mode_z); + + const bool no_dilation = (dilations.dilation_x == "1" && dilations.dilation_y == "1"); + + writer.start_capture_code(); + if(op == MemoryOperation::Load) + { + if(no_dilation) + { + writer.op_load(tile_op, tensor_op, sampler, x_op, y_op, z_op, batch_op); + } + else + { + writer.op_load_dilated(tile_op, tensor_op, sampler, x_op, y_op, z_op, batch_op, dil_x_op, dil_y_op); + } + } + else + { + if(no_dilation) + { + writer.op_store(tensor_op, tile_op, sampler, x_op, y_op, z_op, batch_op); + } + else + { + writer.op_store_dilated(tensor_op, tile_op, sampler, x_op, y_op, z_op, batch_op, dil_x_op, dil_y_op); + } + } + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_idx++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterOpLoadStoreTest"; + } + +private: + std::vector<CLKernelWriterOpLoadStoreConfig> _configs {}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITEROPLOADSTORETEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterPrintTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterPrintTest.h new file mode 100644 index 0000000000..6229dfb8c0 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterPrintTest.h @@ -0,0 +1,75 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERPRINT_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERPRINT_H + +#include "ckw/TileInfo.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterPrintTest : public ITest +{ +public: + CLKernelWriterPrintTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor<CLKernelWriter> writer; + + const auto tile2x3f16 = writer.declare_tile("tile2x3f16", TileInfo(DataType::Fp16, 2, 3)); + const auto tile1x2i32 = writer.declare_tile("tile1x2i32", TileInfo(DataType::Int32, 1, 2)); + const auto tile2x1s32 = writer.declare_tile("tile2x1s32", TileInfo(DataType::Int32, 2, 1)); + const auto tile1x1u32 = writer.declare_tile("tile1x1u32", TileInfo(DataType::Uint32, 1, 1)); + + writer.start_capture_code(); + + writer.op_print("debug_log", { tile2x3f16, tile1x2i32, tile2x1s32, tile1x1u32 }); + + constexpr auto expected_code = + "printf(\"debug_log\\nG0__tile2x3f16 = [[%v3hg], [%v3hg]]\\nG0__tile1x2i32 = [%v2hli]\\nG0__tile2x1s32 = [%i, %i]\\nG0__tile1x1u32 = %u\\n\", " + "G0__tile2x3f16__0, G0__tile2x3f16__1, G0__tile1x2i32, G0__tile2x1s32__0, G0__tile2x1s32__1, G0__tile1x1u32);\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterPrintTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERPRINT_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h new file mode 100644 index 0000000000..fc3f2a639b --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterReturnTest.h @@ -0,0 +1,67 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERRETURNTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERRETURNTEST_H + +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +namespace ckw +{ + +class CLKernelWriterReturnTest : public ITest +{ +public: + CLKernelWriterReturnTest() + { + } + + bool run() override + { + bool all_tests_passed = true; + + KernelWriterInterceptor<CLKernelWriter> writer; + + writer.start_capture_code(); + + writer.op_return(); + + constexpr auto expected_code = "return;\n"; + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, 0); + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterReturnTest"; + } +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERRETURNTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterSubTileTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterSubTileTest.h new file mode 100644 index 0000000000..ea360b289e --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterSubTileTest.h @@ -0,0 +1,264 @@ +/* + * 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 CKW_VALIDATION_SRC_TESTS_CLKERNELWRITERSUBTILETEST_H +#define CKW_VALIDATION_SRC_TESTS_CLKERNELWRITERSUBTILETEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "ckw/types/Operators.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> +#include <vector> + +namespace ckw +{ + +class CLKernelWriterSubTileTest : public ITest +{ +public: + CLKernelWriterSubTileTest() + { + // These are the definitions of the tiles involving in the writing actions. + // + // Structure: + // * List of tiles: + // - Tile full height. + // - Tile full width. + // - Tile view access type (full tile, vector, scalar). + // - Tile view start row. + // - Tile view start column. + // - The tile name. + + // Vector access. + _tests.push_back( + { { { 1, 4, AccessType::Vector, 0, 0, "{tile_name}" }, // + { 4, 4, AccessType::Vector, 2, 0, "{tile_name}__2" }, + { 1, 4, AccessType::Full, 0, 0, "{tile_name}" }, + { 4, 4, AccessType::Vector, 3, 0, "{tile_name}__3" } } }); + + // Scalar access. + _tests.push_back( + { { { 1, 1, AccessType::Full, 0, 0, "{tile_name}" }, // + { 4, 8, AccessType::Scalar, 2, 4, "{tile_name}__2.s4" }, + { 1, 16, AccessType::ScalarOfVector, 0, 10, "{tile_name}.sA" }, + { 1, 1, AccessType::Scalar, 0, 0, "{tile_name}" } } }); + + // These are the definitions of the writing actions. + // + // Structure: + // * Writing function. + // * Whether this function only works with scalar value. + // * Expected code format. + + _actions.push_back( + { [](CLKernelWriter &writer, const std::vector<TileOperand> &args) + { + writer.op_assign(args.at(0), args.at(1)); + }, + false, + "{op0} = {op1};\n" }); + + _actions.push_back( + { [](CLKernelWriter &writer, const std::vector<TileOperand> &args) + { + writer.op_unary(args.at(0), UnaryOp::Sqrt, args.at(1)); + }, + false, + "{op0} = sqrt({op1});\n" }); + + _actions.push_back( + { [](CLKernelWriter &writer, const std::vector<TileOperand> &args) + { + writer.op_binary(args.at(0), BinaryOp::Add, args.at(1), args.at(2)); + }, + false, + "{op0} = {op1} + {op2};\n" }); + + _actions.push_back( + { [](CLKernelWriter &writer, const std::vector<TileOperand> &args) + { + writer.op_ternary(args.at(0), TernaryOp::Clamp, args.at(1), args.at(2), args.at(3)); + }, + false, + "{op0} = clamp({op1}, {op2}, {op3});\n" }); + + _actions.push_back( + { [](CLKernelWriter &writer, const std::vector<TileOperand> &args) + { + writer.op_if(args.at(0), BinaryOp::Greater, args.at(1), [] {}); + }, + true, + "if ({op0} > {op1})\n{\n}\n" }); + } + + bool run() override + { + bool all_tests_passed = true; + int32_t test_idx = 0; + + KernelWriterInterceptor<CLKernelWriter> writer; + + for(size_t test_no = 0; test_no < _tests.size(); ++test_no) + { + const TestInfo &test = _tests.at(test_no); + + // Declare all the tiles and get the full name of those tile operand. + std::vector<TileOperand> tiles; + std::vector<std::string> expected_tiles_name; + + for(size_t operand_no = 0; operand_no < test.operands.size(); ++operand_no) + { + const TestOperand &operand = test.operands.at(operand_no); + std::string name = "test" + std::to_string(test_no) + "_op" + std::to_string(operand_no); + + const TileOperand full_tile = writer.declare_tile(name, TileInfo(DataType::Fp32, operand.height, operand.width)); + + switch(operand.access_type) + { + case AccessType::Full: + tiles.emplace_back(full_tile); + break; + + case AccessType::Vector: + tiles.emplace_back(full_tile.row(operand.start_row)); + break; + + case AccessType::Scalar: + tiles.emplace_back(full_tile.scalar(operand.start_row, operand.start_col)); + break; + + case AccessType::ScalarOfVector: + tiles.emplace_back(full_tile.row(operand.start_row).scalar(0, operand.start_col)); + break; + + default: + CKW_THROW_MSG("Unsupported access type!"); + } + + expected_tiles_name.push_back("G0__" + name); + } + + // Try each writing action using the newly declared tiles. + for(const TestAction &action : _actions) + { + if(action.scalar_only && // + (test.operands.at(0).access_type != AccessType::Scalar && // + (test.operands.at(0).height != 1 || test.operands.at(0).width != 1))) + { + continue; + } + + writer.start_capture_code(); + + action.write(writer, tiles); + + // The expected code is constructed from the format strings. + std::string expected_code = action.expected_code; + + for(size_t operand_no = 0; operand_no < test.operands.size(); ++operand_no) + { + const TestOperand &operand = test.operands.at(operand_no); + + const std::string op_name = search_and_replace(operand.name, "{tile_name}", expected_tiles_name.at(operand_no)); + expected_code = search_and_replace(expected_code, "{op" + std::to_string(operand_no) + "}", op_name); + } + + VALIDATE_TEST(writer.check_added_code(expected_code), all_tests_passed, test_idx++); + } + } + + return all_tests_passed; + } + + std::string search_and_replace(const std::string &src, const std::string &search, const std::string &replace) + { + std::string result = src; + + size_t idx = 0; + + while(true) + { + idx = result.find(search, idx); + + if(idx == std::string::npos) + { + break; + } + + result = result.replace(idx, search.size(), replace); + } + + return result; + } + + std::string name() override + { + return "CLKernelWriterSubTileTest"; + } + +private: + enum class AccessType + { + Full, + Vector, + Scalar, + ScalarOfVector, + }; + + struct TestOperand + { + int32_t height; + int32_t width; + + AccessType access_type; + int32_t start_row; + int32_t start_col; + + std::string name; + }; + + struct TestInfo + { + std::vector<TestOperand> operands; + }; + + struct TestAction + { + std::function<void(CLKernelWriter &, const std::vector<TileOperand> &)> write; + + bool scalar_only; + std::string expected_code; + }; + + std::vector<TestInfo> _tests{}; + std::vector<TestAction> _actions{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_SRC_TESTS_CLKERNELWRITERSUBTILETEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterTernaryOpTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterTernaryOpTest.h new file mode 100644 index 0000000000..d25d3e2958 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterTernaryOpTest.h @@ -0,0 +1,111 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERTERNARYOPTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERTERNARYOPTEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "ckw/types/Operators.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> +#include <vector> + +namespace ckw +{ + +class CLKernelWriterTernaryOpTest : public ITest +{ +public: + CLKernelWriterTernaryOpTest() + { + // dst_height, dst_width, op0_height, op0_width, op1_height, op1_width, op2_height, op2_width, data_type, op, expected_code + + _tests.push_back({ 1, 1, 1, 1, 1, 1, 1, 1, DataType::Fp32, TernaryOp::Select, "G0__dst = select(G0__op0, G0__op1, G0__op2);\n" }); // Scalar. + + _tests.push_back({ 1, 3, 1, 3, 1, 3, 1, 3, DataType::Fp16, TernaryOp::Clamp, "G0__dst = clamp(G0__op0, G0__op1, G0__op2);\n" }); // Whole vector. + + _tests.push_back({ 2, 4, 2, 4, 2, 4, 2, 4, DataType::Int8, TernaryOp::Select, "G0__dst__0 = select(G0__op0__0, G0__op1__0, G0__op2__0);\nG0__dst__1 = select(G0__op0__1, G0__op1__1, G0__op2__1);\n" }); // Whole tile. + + _tests.push_back({ 2, 3, 1, 3, 2, 3, 2, 3, DataType::Uint8, TernaryOp::Clamp, "G0__dst__0 = clamp(G0__op0, G0__op1__0, G0__op2__0);\nG0__dst__1 = clamp(G0__op0, G0__op1__1, G0__op2__1);\n" }); // 1st operand y-dimension broadcast. + + _tests.push_back({ 2, 3, 2, 3, 2, 1, 2, 3, DataType::Fp32, TernaryOp::Select, "G0__dst__0 = select(G0__op0__0, (float3)G0__op1__0, G0__op2__0);\nG0__dst__1 = select(G0__op0__1, (float3)G0__op1__1, G0__op2__1);\n" }); // 2nd operand x-dimension broadcast. + + _tests.push_back({ 2, 3, 1, 3, 2, 1, 1, 1, DataType::Fp16, TernaryOp::Clamp, "G0__dst__0 = clamp(G0__op0, (half3)G0__op1__0, (half3)G0__op2);\nG0__dst__1 = clamp(G0__op0, (half3)G0__op1__1, (half3)G0__op2);\n" }); // 1st operand y-, 2nd operand x-, 3rd operand x- and y-dimension broadcast. + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + for(const auto &test : _tests) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + auto dst = writer.declare_tile("dst", TileInfo(test.data_type, test.dst_height, test.dst_width)); + auto op0 = writer.declare_tile("op0", TileInfo(DataType::Bool, test.op0_height, test.op0_width)); + auto op1 = writer.declare_tile("op1", TileInfo(test.data_type, test.op1_height, test.op1_width)); + auto op2 = writer.declare_tile("op2", TileInfo(test.data_type, test.op2_height, test.op2_width)); + + writer.start_capture_code(); + + writer.op_ternary(dst, test.op, op0, op1, op2); + + VALIDATE_TEST(writer.check_added_code(test.expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterTernaryOpTest"; + } + +private: + struct TestInfo + { + int32_t dst_height; + int32_t dst_width; + int32_t op0_height; + int32_t op0_width; + int32_t op1_height; + int32_t op1_width; + int32_t op2_height; + int32_t op2_width; + DataType data_type; + TernaryOp op; + std::string expected_code; + }; + + std::vector<TestInfo> _tests{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERTERNARYOPTEST_H diff --git a/compute_kernel_writer/validation/tests/CLKernelWriterUnaryExpressionTest.h b/compute_kernel_writer/validation/tests/CLKernelWriterUnaryExpressionTest.h new file mode 100644 index 0000000000..395a2fe817 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLKernelWriterUnaryExpressionTest.h @@ -0,0 +1,105 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLKERNELWRITERUNARYEXPRESSIONTEST_H +#define CKW_VALIDATION_TESTS_CLKERNELWRITERUNARYEXPRESSIONTEST_H + +#include "ckw/TileInfo.h" +#include "ckw/types/DataType.h" +#include "ckw/types/Operators.h" +#include "src/cl/CLKernelWriter.h" +#include "validation/tests/common/Common.h" +#include "validation/tests/common/KernelWriterInterceptor.h" + +#include <cstdint> +#include <vector> + +namespace ckw +{ + +class CLKernelWriterUnaryExpressionTest : public ITest +{ +public: + CLKernelWriterUnaryExpressionTest() + { + // dst_height, dst_width, src_height, src_width, data_type, op, expected_code + + _tests.push_back({ 1, 1, 1, 1, DataType::Uint32, UnaryOp::BitwiseNot, "G0__dst = ~G0__src;\n" }); // Scalar. + + _tests.push_back({ 1, 3, 1, 3, DataType::Int16, UnaryOp::LogicalNot, "G0__dst = !G0__src;\n" }); // Whole vector. + + _tests.push_back({ 2, 4, 2, 4, DataType::Int8, UnaryOp::Exp, "G0__dst__0 = exp(G0__src__0);\nG0__dst__1 = exp(G0__src__1);\n" }); // Whole tile. + + _tests.push_back({ 2, 3, 1, 3, DataType::Uint8, UnaryOp::Log, "G0__dst__0 = log(G0__src);\nG0__dst__1 = log(G0__src);\n" }); // Y-dimension broadcast. + + _tests.push_back({ 2, 4, 2, 1, DataType::Uint16, UnaryOp::Sqrt, "G0__dst__0 = (ushort4)sqrt(G0__src__0);\nG0__dst__1 = (ushort4)sqrt(G0__src__1);\n" }); // X-dimension broadcast. + + _tests.push_back({ 2, 3, 1, 1, DataType::Int32, UnaryOp::Round, "G0__dst__0 = (int3)round(G0__src);\nG0__dst__1 = (int3)round(G0__src);\n" }); // X and y dimension broadcast. + } + + bool run() override + { + int32_t test_no = 0; + bool all_tests_passed = true; + + for(const auto &test : _tests) + { + KernelWriterInterceptor<CLKernelWriter> writer; + + auto dst = writer.declare_tile("dst", TileInfo(test.data_type, test.dst_height, test.dst_width)); + auto src = writer.declare_tile("src", TileInfo(test.data_type, test.src_height, test.src_width)); + + writer.start_capture_code(); + + writer.op_unary(dst, test.op, src); + + VALIDATE_TEST(writer.check_added_code(test.expected_code), all_tests_passed, test_no++); + } + + return all_tests_passed; + } + + std::string name() override + { + return "CLKernelWriterUnaryExpressionTest"; + } + +private: + struct TestInfo + { + int32_t dst_height; + int32_t dst_width; + int32_t src_height; + int32_t src_width; + DataType data_type; + UnaryOp op; + std::string expected_code; + }; + + std::vector<TestInfo> _tests{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERUNARYEXPRESSIONTEST_H diff --git a/compute_kernel_writer/validation/tests/CLTensorArgumentTest.h b/compute_kernel_writer/validation/tests/CLTensorArgumentTest.h new file mode 100644 index 0000000000..d3e455cb83 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLTensorArgumentTest.h @@ -0,0 +1,540 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLTENSORARGUMENTTEST_H +#define CKW_VALIDATION_TESTS_CLTENSORARGUMENTTEST_H + +#include "common/Common.h" +#include "src/cl/CLHelpers.h" +#include "src/cl/CLTensorArgument.h" +#include "src/cl/CLTensorComponent.h" + +#include <string> +#include <vector> + +namespace ckw +{ +class CLTensorArgumentComponentNamesTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { 12, 14, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentComponentNamesTest() + { + _components.push_back(TensorComponentType::Dim0); + _components.push_back(TensorComponentType::Dim1); + _components.push_back(TensorComponentType::Dim2); + _components.push_back(TensorComponentType::Dim3); + _components.push_back(TensorComponentType::Dim4); + _components.push_back(TensorComponentType::Dim1xDim2); + _components.push_back(TensorComponentType::Dim2xDim3); + _components.push_back(TensorComponentType::OffsetFirstElement); + _components.push_back(TensorComponentType::Stride0); + _components.push_back(TensorComponentType::Stride1); + _components.push_back(TensorComponentType::Stride2); + _components.push_back(TensorComponentType::Stride3); + _components.push_back(TensorComponentType::Stride4); + + _expected_vars.push_back("src_dim0"); + _expected_vars.push_back("src_dim1"); + _expected_vars.push_back("src_dim2"); + _expected_vars.push_back("src_dim3"); + _expected_vars.push_back("src_dim4"); + _expected_vars.push_back("src_dim1xdim2"); + _expected_vars.push_back("src_dim2xdim3"); + _expected_vars.push_back("src_offset_first_element"); + _expected_vars.push_back("src_stride0"); + _expected_vars.push_back("src_stride1"); + _expected_vars.push_back("src_stride2"); + _expected_vars.push_back("src_stride3"); + _expected_vars.push_back("src_stride4"); + } + + bool run() override + { + VALIDATE_ON_MSG(_components.size() == _expected_vars.size(), "The number of components and variables does not match"); + + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_tests = _expected_vars.size(); + + int32_t test_idx = 0; + for(size_t i = 0; i < num_tests; ++i) + { + CLTensorArgument arg(tensor_name, info, false /* return_dims_by_value */); + + const std::string expected_var_name = _expected_vars[i]; + const std::string actual_var_name = arg.component(_components[i]).name(); + + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentVariableNamesTest"; + } + +private: + std::vector<TensorComponentType> _components{}; + std::vector<std::string> _expected_vars{}; +}; + +class CLTensorArgumentStorageNamesTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { 12, 14, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentStorageNamesTest() + { + _storages.push_back(TensorStorageType::BufferUint8Ptr); + _storages.push_back(TensorStorageType::Texture2dReadOnly); + _storages.push_back(TensorStorageType::Texture2dWriteOnly); + + _expected_vars.push_back("src_ptr"); + _expected_vars.push_back("src_img2d"); + _expected_vars.push_back("src_img2d"); + } + + bool run() override + { + VALIDATE_ON_MSG(_storages.size() == _expected_vars.size(), "The number of storages and variables does not match"); + + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_tests = _expected_vars.size(); + + int32_t test_idx = 0; + for(size_t i = 0; i < num_tests; ++i) + { + CLTensorArgument arg(tensor_name, info, false /* return_dims_by_value */); + + const std::string expected_var_name = _expected_vars[i]; + const std::string actual_var_name = arg.storage(_storages[i]).val; + + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentStorageNamesTest"; + } + +private: + std::vector<TensorStorageType> _storages{}; + std::vector<std::string> _expected_vars{}; +}; + +class CLTensorArgumentComponentValuesTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { 12, 14, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentComponentValuesTest() + { + _components.push_back(TensorComponentType::Dim0); + _components.push_back(TensorComponentType::Dim1); + _components.push_back(TensorComponentType::Dim2); + _components.push_back(TensorComponentType::Dim3); + _components.push_back(TensorComponentType::Dim4); + _components.push_back(TensorComponentType::Dim1xDim2); + _components.push_back(TensorComponentType::Dim2xDim3); + + _expected_vals.push_back(std::to_string(shape[0])); + _expected_vals.push_back(std::to_string(shape[1])); + _expected_vals.push_back(std::to_string(shape[2])); + _expected_vals.push_back(std::to_string(shape[3])); + _expected_vals.push_back(std::to_string(shape[4])); + _expected_vals.push_back(std::to_string(shape[1] * shape[2])); + _expected_vals.push_back(std::to_string(shape[2] * shape[3])); + } + + bool run() override + { + VALIDATE_ON_MSG(_components.size() == _expected_vals.size(), "The number of components and values does not match"); + + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_tests = _expected_vals.size(); + + int32_t test_idx = 0; + for(size_t i = 0; i < num_tests; ++i) + { + CLTensorArgument arg(tensor_name, info, true /* return_dims_by_value */); + + const std::string expected_var_val = std::string("((int)(") + _expected_vals[i] + "))"; + const std::string actual_var_val = arg.cl_component(_components[i]).scalar(0, 0).str; + + VALIDATE_TEST(actual_var_val.compare(expected_var_val) == 0, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentComponentValuesTest"; + } + +private: + std::vector<TensorComponentType> _components{}; + std::vector<std::string> _expected_vals{}; +}; + +class CLTensorArgumentComponentsUsedPassByValueFalseTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { 12, 14, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentComponentsUsedPassByValueFalseTest() + { + _components.push_back(TensorComponentType::Dim0); + _components.push_back(TensorComponentType::Dim2); + _components.push_back(TensorComponentType::Dim3); + _components.push_back(TensorComponentType::Dim1xDim2); + _components.push_back(TensorComponentType::OffsetFirstElement); + _components.push_back(TensorComponentType::Stride1); + _components.push_back(TensorComponentType::Stride2); + _components.push_back(TensorComponentType::Stride3); + _components.push_back(TensorComponentType::Dim0); // Repeat the query. The TensorArgument should not create a new variable + _components.push_back(TensorComponentType::Dim2); // Repeat the query. The TensorArgument should not create a new variable + _components.push_back(TensorComponentType::Dim3); // Repeat the query. The TensorArgument should not create a new variable + + _expected_vars.push_back("src_dim0"); + _expected_vars.push_back("src_dim2"); + _expected_vars.push_back("src_dim3"); + _expected_vars.push_back("src_dim1xdim2"); + _expected_vars.push_back("src_offset_first_element"); + _expected_vars.push_back("src_stride1"); + _expected_vars.push_back("src_stride2"); + _expected_vars.push_back("src_stride3"); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_components = _components.size(); + + int32_t test_idx = 0; + + CLTensorArgument arg(tensor_name, info, false /* return_dims_by_value */); + for(size_t i = 0; i < num_components; ++i) + { + arg.component(_components[i]); + } + + const auto actual_vars = arg.components(); + + const size_t num_vars = _expected_vars.size(); + + VALIDATE_ON_MSG(actual_vars.size() == num_vars, "The number of variables must match the number of expected variables"); + + for(size_t i = 0; i < num_vars; ++i) + { + // Validate variable name + const std::string expected_var_name = _expected_vars[i]; + const std::string actual_var_name = actual_vars[i]->tile().name(); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + + // Validate data type + const DataType expected_var_type = DataType::Int32; + const DataType actual_var_type = actual_vars[i]->tile().info().data_type(); + VALIDATE_TEST(actual_var_type == expected_var_type, all_tests_passed, test_idx++); + + // Validate tile shape + const int32_t actual_var_width = actual_vars[i]->tile().info().width(); + const int32_t actual_var_height = actual_vars[i]->tile().info().height(); + + VALIDATE_TEST(actual_var_height == 1, all_tests_passed, test_idx++); + VALIDATE_TEST(actual_var_width == 1, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentComponentsUsedPassByValueFalseTest"; + } + +private: + std::vector<TensorComponentType> _components{}; + std::vector<std::string> _expected_vars{}; +}; + +class CLTensorArgumentComponentsUsedPassByValueTrueTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { 12, 14, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentComponentsUsedPassByValueTrueTest() + { + _components.push_back(TensorComponentType::Dim0); + _components.push_back(TensorComponentType::Dim2); + _components.push_back(TensorComponentType::Dim3); + _components.push_back(TensorComponentType::Dim1xDim2); + _components.push_back(TensorComponentType::OffsetFirstElement); + _components.push_back(TensorComponentType::Stride1); + _components.push_back(TensorComponentType::Stride2); + _components.push_back(TensorComponentType::Stride3); + _components.push_back(TensorComponentType::OffsetFirstElement); // Repeat the query. The TensorArgument should not create a new variable + _components.push_back(TensorComponentType::Stride1); // Repeat the query. The TensorArgument should not create a new variable + + _expected_vars.push_back("src_offset_first_element"); + _expected_vars.push_back("src_stride1"); + _expected_vars.push_back("src_stride2"); + _expected_vars.push_back("src_stride3"); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_components = _components.size(); + + int32_t test_idx = 0; + + CLTensorArgument arg(tensor_name, info, true /* return_dims_by_value */); + for(size_t i = 0; i < num_components; ++i) + { + arg.component(_components[i]); + } + + const auto actual_vars = arg.components(); + + const size_t num_vars = _expected_vars.size(); + + VALIDATE_ON_MSG(actual_vars.size() == num_vars, "The number of variables must match the number of expected variables"); + + // Since the dimensions are passed by value, we expect only the variables for the strides + for(size_t i = 0; i < num_vars; ++i) + { + // Validate variable name + const std::string expected_var_name = _expected_vars[i]; + const std::string actual_var_name = actual_vars[i]->tile().name(); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + + // Validate data type + const DataType expected_var_type = DataType::Int32; + const DataType actual_var_type = actual_vars[i]->tile().info().data_type(); + VALIDATE_TEST(actual_var_type == expected_var_type, all_tests_passed, test_idx++); + + // Validate tile shape + const int32_t actual_var_width = actual_vars[i]->tile().info().width(); + const int32_t actual_var_height = actual_vars[i]->tile().info().height(); + + VALIDATE_TEST(actual_var_height == 1, all_tests_passed, test_idx++); + VALIDATE_TEST(actual_var_width == 1, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentComponentsUsedPassByValueTrueTest"; + } + +private: + std::vector<TensorComponentType> _components{}; + std::vector<std::string> _expected_vars{}; +}; + +class CLTensorArgumentStoragesUsedTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { 12, 14, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentStoragesUsedTest() + { + _storages.push_back(TensorStorageType::BufferUint8Ptr); + _storages.push_back(TensorStorageType::Texture2dReadOnly); + _storages.push_back(TensorStorageType::BufferUint8Ptr); // Repeat the query. The TensorArgument should not create a new variable + + _expected_vars.push_back("src_ptr"); + _expected_vars.push_back("src_img2d"); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_storages = _storages.size(); + + int32_t test_idx = 0; + + CLTensorArgument arg(tensor_name, info, true /* return_dims_by_value */); + for(size_t i = 0; i < num_storages; ++i) + { + arg.storage(_storages[i]); + } + + const auto actual_vars = arg.storages(); + + const size_t num_vars = _expected_vars.size(); + + VALIDATE_ON_MSG(actual_vars.size() == num_vars, "The number of variables must match the number of expected variables"); + + for(size_t i = 0; i < num_vars; ++i) + { + // Validate variable name + const std::string expected_var_name = _expected_vars[i]; + const std::string actual_var_name = actual_vars[i].val; + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + + // Validate storage type + const auto expected_var_type = _storages[i]; + const auto actual_var_type = actual_vars[i].type; + VALIDATE_TEST(actual_var_type == expected_var_type, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentStoragesUsedTest"; + } + +private: + std::vector<TensorStorageType> _storages{}; + std::vector<std::string> _expected_vars{}; +}; + +class CLTensorArgumentComponentsUsedPassByValueTrueDynamicDimTrueTest : public ITest +{ +public: + const DataType dt = DataType::Fp32; + const TensorShape shape = TensorShape({ { -1, -1, 3, 1, 2 } }); + const std::string tensor_name = "src"; + + CLTensorArgumentComponentsUsedPassByValueTrueDynamicDimTrueTest() + { + _components.push_back(TensorComponentType::Dim0); + _components.push_back(TensorComponentType::Dim2); + _components.push_back(TensorComponentType::Dim3); + _components.push_back(TensorComponentType::Dim1xDim2); + _components.push_back(TensorComponentType::OffsetFirstElement); + _components.push_back(TensorComponentType::Stride1); + _components.push_back(TensorComponentType::Stride2); + _components.push_back(TensorComponentType::Stride3); + _components.push_back(TensorComponentType::OffsetFirstElement); // Repeat the query. The TensorArgument should not create a new variable + _components.push_back(TensorComponentType::Stride1); // Repeat the query. The TensorArgument should not create a new variable + + _expected_vars.push_back("src_dim0"); + _expected_vars.push_back("src_dim1xdim2"); + _expected_vars.push_back("src_offset_first_element"); + _expected_vars.push_back("src_stride1"); + _expected_vars.push_back("src_stride2"); + _expected_vars.push_back("src_stride3"); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TensorInfo info(dt, shape, TensorDataLayout::Nhwc, 1); + + const size_t num_components = _components.size(); + + int32_t test_idx = 0; + + CLTensorArgument arg(tensor_name, info, true /* return_dims_by_value */); + for(size_t i = 0; i < num_components; ++i) + { + arg.component(_components[i]); + } + + const auto actual_vars = arg.components(); + + const size_t num_vars = _expected_vars.size(); + + VALIDATE_ON_MSG(actual_vars.size() == num_vars, "The number of variables must match the number of expected variables"); + + // Since the dimensions are passed by value, we expect only the variables for the strides + for(size_t i = 0; i < num_vars; ++i) + { + // Validate variable name + const std::string expected_var_name = _expected_vars[i]; + const std::string actual_var_name = actual_vars[i]->tile().name(); + VALIDATE_TEST(actual_var_name.compare(expected_var_name) == 0, all_tests_passed, test_idx++); + + // Validate data type + const DataType expected_var_type = DataType::Int32; + const DataType actual_var_type = actual_vars[i]->tile().info().data_type(); + VALIDATE_TEST(actual_var_type == expected_var_type, all_tests_passed, test_idx++); + + // Validate tile shape + const int32_t actual_var_width = actual_vars[i]->tile().info().width(); + const int32_t actual_var_height = actual_vars[i]->tile().info().height(); + + VALIDATE_TEST(actual_var_height == 1, all_tests_passed, test_idx++); + VALIDATE_TEST(actual_var_width == 1, all_tests_passed, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTensorArgumentComponentsUsedPassByValueTrueDynamicDimTrueTest"; + } + +private: + std::vector<TensorComponentType> _components{}; + std::vector<std::string> _expected_vars{}; +}; +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLTENSORARGUMENTTEST_H diff --git a/compute_kernel_writer/validation/tests/CLTileTest.hpp b/compute_kernel_writer/validation/tests/CLTileTest.hpp new file mode 100644 index 0000000000..a95a11ace7 --- /dev/null +++ b/compute_kernel_writer/validation/tests/CLTileTest.hpp @@ -0,0 +1,467 @@ +/* + * 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 CKW_VALIDATION_TESTS_CLTILETEST_HPP +#define CKW_VALIDATION_TESTS_CLTILETEST_HPP + +#include "common/Common.h" +#include "src/Helpers.h" +#include "src/cl/CLTile.h" + +#include <string> +#include <vector> + +namespace ckw +{ +class CLTileInternalVariableNamesTest : public ITest +{ +public: + const int32_t width = 4; + const int32_t height = 4; + const DataType dt = DataType::Fp32; + + CLTileInternalVariableNamesTest() + { + _tile_name.push_back("dst"); + _tile_name.push_back("_G0_dst"); + _tile_name.push_back("_SRC"); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const TileInfo info(dt, height, width); + + int32_t test_idx = 0; + for(const auto &tile_name : _tile_name) + { + const CLTile tile(tile_name, info); + const auto vars = tile.all(); + + for(int32_t y = 0; y < height; ++y) + { + 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, test_idx++); + } + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTileInternalVariableNamesTest"; + } + +private: + std::vector<std::string> _tile_name{}; +}; + +class CLTileInternalNumVariablesTest : public ITest +{ +public: + CLTileInternalNumVariablesTest() + { + _width.push_back(4); + _width.push_back(1); + _width.push_back(16); + + _height.push_back(1); + _height.push_back(5); + _height.push_back(3); + } + + bool run() override + { + VALIDATE_ON_MSG(_width.size() == _height.size(), "The number of widths and heights does not match"); + + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + const size_t num_dims = _width.size(); + + 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]; + const TileInfo info(DataType::Fp32, height, width); + const CLTile tile("src", info); + const auto vars = tile.all(); + 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, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTileInternalNumVariablesTest"; + } + +private: + std::vector<int32_t> _width{}; + std::vector<int32_t> _height{}; +}; + +class CLTileAccessScalarVariableTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t width = 16; + const int32_t height = 8; + const DataType dt = DataType::Fp32; + + CLTileAccessScalarVariableTest() + { + _x_coord.push_back(4); + _x_coord.push_back(1); + _x_coord.push_back(15); + _x_coord.push_back(10); + + _y_coord.push_back(1); + _y_coord.push_back(5); + _y_coord.push_back(3); + _y_coord.push_back(4); + } + + bool run() override + { + const TileInfo info(dt, height, width); + const CLTile tile(tile_name, info); + + 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(); + + 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(y_coord, 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, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTileAccessScalarVariableTest"; + } + +private: + std::vector<int32_t> _x_coord{}; + std::vector<int32_t> _y_coord{}; +}; + +class CLTileAccessScalarVariableBroadcastXTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t height = 8; + const DataType dt = DataType::Fp32; + + CLTileAccessScalarVariableBroadcastXTest() + { + _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(); + + 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<int32_t>(0), width - 1); + + const TileInfo info(dt, height, width); + const CLTile tile(tile_name, info); + + const TileVariable var = tile.scalar(y_coord, 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); + if(width != 1) + { + 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, test_idx++); + } + return all_tests_passed; + } + + std::string name() override + { + return "CLTileAccessScalarVariableBroadcastXTest"; + } + +private: + std::vector<int32_t> _width{}; + std::vector<int32_t> _x_coord{}; + std::vector<int32_t> _y_coord{}; +}; + +class CLTileAccessScalarVariableBroadcastYTest : public ITest +{ +public: + const std::string tile_name = "src"; + const int32_t width = 8; + const DataType dt = DataType::Fp32; + + CLTileAccessScalarVariableBroadcastYTest() + { + _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; + + 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<int32_t>(0), height - 1); + + const TileInfo info(dt, height, width); + const CLTile tile(tile_name, info); + + const TileVariable var = tile.scalar(y_coord, x_coord); + + const std::string actual_var_name = var.str; + std::string expected_var_name = tile_name; + if(height != 1) + { + expected_var_name += "__" + std::to_string(y_coord_clamped); + } + + if(width != 1) + { + expected_var_name += ".s" + dec_to_hex_as_string(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 "CLTileAccessScalarVariableBroadcastYTest"; + } + +private: + std::vector<int32_t> _height{}; + std::vector<int32_t> _x_coord{}; + std::vector<int32_t> _y_coord{}; +}; + +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, height, width); + 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<int32_t> _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, height, width); + 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<int32_t> _subwidths{}; + std::vector<int32_t> _offsets{}; +}; +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLTILETEST_HPP diff --git a/compute_kernel_writer/validation/tests/TensorBitMaskTest.h b/compute_kernel_writer/validation/tests/TensorBitMaskTest.h new file mode 100644 index 0000000000..759d926d18 --- /dev/null +++ b/compute_kernel_writer/validation/tests/TensorBitMaskTest.h @@ -0,0 +1,221 @@ +/* + * 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 CKW_TESTS_TENSORBITMASKTEST_H +#define CKW_TESTS_TENSORBITMASKTEST_H + +#include "ckw/TensorInfo.h" +#include "ckw/types/TensorComponentType.h" +#include "common/Common.h" +#include "src/types/TensorComponentType.h" + +#include <vector> + +namespace ckw +{ +class TensorBitMaskTrueTest : public ITest +{ +public: + TensorBitMaskTrueTest() + { + _component.push_back(TensorComponentType::Dim0); + _component.push_back(TensorComponentType::Dim1); + _component.push_back(TensorComponentType::Dim2); + _component.push_back(TensorComponentType::Dim3); + _component.push_back(TensorComponentType::Dim4); + _component.push_back(TensorComponentType::Stride0); + _component.push_back(TensorComponentType::Stride1); + _component.push_back(TensorComponentType::Stride2); + _component.push_back(TensorComponentType::Stride3); + _component.push_back(TensorComponentType::Stride4); + _component.push_back(TensorComponentType::Dim1xDim2); + _component.push_back(TensorComponentType::Dim1xDim2xDim3); + _component.push_back(TensorComponentType::Dim2xDim3); + _component.push_back(TensorComponentType::OffsetFirstElement); + + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + VALIDATE_ON_MSG(_component.size() == _bitmask.size(), + "The number of layouts and components does not match"); + const size_t num_tests = _component.size(); + for(size_t i = 0; i < num_tests; ++i) + { + const TensorComponentType component = _component[i]; + const TensorComponentBitmask bitmask = _bitmask[i]; + const bool out = static_cast<uint32_t>(component) & static_cast<uint32_t>(bitmask); + VALIDATE_TEST(out == true, all_tests_passed, i); + } + return all_tests_passed; + } + + std::string name() override + { + return "TensorBitMaskTrueTest"; + } + +private: + std::vector<TensorComponentType> _component{}; + std::vector<TensorComponentBitmask> _bitmask{}; +}; + +class TensorBitMaskFalseTest : public ITest +{ +public: + TensorBitMaskFalseTest() + { + _component.push_back(TensorComponentType::Dim0); + _component.push_back(TensorComponentType::Dim1); + _component.push_back(TensorComponentType::Dim2); + _component.push_back(TensorComponentType::Dim3); + _component.push_back(TensorComponentType::Dim4); + _component.push_back(TensorComponentType::Dim0); + _component.push_back(TensorComponentType::Dim1); + _component.push_back(TensorComponentType::Dim2); + _component.push_back(TensorComponentType::Dim3); + _component.push_back(TensorComponentType::Dim4); + _component.push_back(TensorComponentType::Dim0); + _component.push_back(TensorComponentType::Dim1); + _component.push_back(TensorComponentType::Dim2); + _component.push_back(TensorComponentType::Dim3); + _component.push_back(TensorComponentType::Dim4); + _component.push_back(TensorComponentType::Stride0); + _component.push_back(TensorComponentType::Stride1); + _component.push_back(TensorComponentType::Stride2); + _component.push_back(TensorComponentType::Stride3); + _component.push_back(TensorComponentType::Stride4); + _component.push_back(TensorComponentType::Stride0); + _component.push_back(TensorComponentType::Stride1); + _component.push_back(TensorComponentType::Stride2); + _component.push_back(TensorComponentType::Stride3); + _component.push_back(TensorComponentType::Stride4); + _component.push_back(TensorComponentType::Stride0); + _component.push_back(TensorComponentType::Stride1); + _component.push_back(TensorComponentType::Stride2); + _component.push_back(TensorComponentType::Stride3); + _component.push_back(TensorComponentType::Stride4); + _component.push_back(TensorComponentType::Dim1xDim2); + _component.push_back(TensorComponentType::Dim1xDim2xDim3); + _component.push_back(TensorComponentType::Dim2xDim3); + _component.push_back(TensorComponentType::Dim1xDim2); + _component.push_back(TensorComponentType::Dim1xDim2xDim3); + _component.push_back(TensorComponentType::Dim2xDim3); + _component.push_back(TensorComponentType::Dim1xDim2); + _component.push_back(TensorComponentType::Dim1xDim2xDim3); + _component.push_back(TensorComponentType::Dim2xDim3); + _component.push_back(TensorComponentType::OffsetFirstElement); + _component.push_back(TensorComponentType::OffsetFirstElement); + _component.push_back(TensorComponentType::OffsetFirstElement); + + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::OffsetFirstElement); + _bitmask.push_back(TensorComponentBitmask::Dimension); + _bitmask.push_back(TensorComponentBitmask::Stride); + _bitmask.push_back(TensorComponentBitmask::FoldedDimensions); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + VALIDATE_ON_MSG(_component.size() == _bitmask.size(), + "The number of layouts and components does not match"); + const size_t num_tests = _component.size(); + for(size_t i = 0; i < num_tests; ++i) + { + const TensorComponentType component = _component[i]; + const TensorComponentBitmask bitmask = _bitmask[i]; + const bool out = static_cast<uint32_t>(component) & static_cast<uint32_t>(bitmask); + VALIDATE_TEST(out == false, all_tests_passed, i); + } + return all_tests_passed; + } + + std::string name() override + { + return "TensorBitMaskFalseTest"; + } + +private: + std::vector<TensorComponentType> _component{}; + std::vector<TensorComponentBitmask> _bitmask{}; +}; +} // namespace ckw + +#endif // CKW_TESTS_TENSORBITMASKTEST_H diff --git a/compute_kernel_writer/validation/tests/UtilsTest.h b/compute_kernel_writer/validation/tests/UtilsTest.h new file mode 100644 index 0000000000..a335a48f81 --- /dev/null +++ b/compute_kernel_writer/validation/tests/UtilsTest.h @@ -0,0 +1,104 @@ +/* + * 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 CKW_TESTS_UTILSTEST_H +#define CKW_TESTS_UTILSTEST_H + +#include "ckw/TensorInfo.h" +#include "ckw/types/TensorDataLayout.h" +#include "common/Common.h" +#include "src/TensorUtils.h" + +#include <vector> + +namespace ckw +{ +class UtilsTest : public ITest +{ +public: + UtilsTest() + { + _layout.push_back(TensorDataLayout::Nhwc); + _layout.push_back(TensorDataLayout::Nhwc); + _layout.push_back(TensorDataLayout::Nhwc); + _layout.push_back(TensorDataLayout::Nhwc); + _layout.push_back(TensorDataLayout::Ndhwc); + _layout.push_back(TensorDataLayout::Ndhwc); + _layout.push_back(TensorDataLayout::Ndhwc); + _layout.push_back(TensorDataLayout::Ndhwc); + _layout.push_back(TensorDataLayout::Ndhwc); + + _component.push_back(TensorDataLayoutComponent::N); + _component.push_back(TensorDataLayoutComponent::H); + _component.push_back(TensorDataLayoutComponent::W); + _component.push_back(TensorDataLayoutComponent::C); + _component.push_back(TensorDataLayoutComponent::N); + _component.push_back(TensorDataLayoutComponent::D); + _component.push_back(TensorDataLayoutComponent::H); + _component.push_back(TensorDataLayoutComponent::W); + _component.push_back(TensorDataLayoutComponent::C); + + _expected.push_back(TensorComponentType::Dim3); + _expected.push_back(TensorComponentType::Dim2); + _expected.push_back(TensorComponentType::Dim1); + _expected.push_back(TensorComponentType::Dim0); + _expected.push_back(TensorComponentType::Dim4); + _expected.push_back(TensorComponentType::Dim3); + _expected.push_back(TensorComponentType::Dim2); + _expected.push_back(TensorComponentType::Dim1); + _expected.push_back(TensorComponentType::Dim0); + } + + bool run() override + { + // The status of this variable can change in VALIDATE_TEST() + bool all_tests_passed = true; + + VALIDATE_ON_MSG(_layout.size() == _component.size(), "The number of layouts and components does not match"); + VALIDATE_ON_MSG(_layout.size() == _expected.size(), + "The number of layouts and expected outputs does not match"); + const size_t num_tests = _layout.size(); + for(size_t i = 0; i < num_tests; ++i) + { + const TensorDataLayout layout = _layout[i]; + const TensorDataLayoutComponent component = _component[i]; + const TensorComponentType expected = _expected[i]; + const TensorComponentType out = get_tensor_dimension(layout, component); + VALIDATE_TEST(out == expected, all_tests_passed, i); + } + return all_tests_passed; + } + + std::string name() override + { + return "UtilsTest"; + } + +private: + std::vector<TensorDataLayout> _layout{}; + std::vector<TensorDataLayoutComponent> _component{}; + std::vector<TensorComponentType> _expected{}; +}; +} // namespace ckw + +#endif // CKW_TESTS_UTILSTEST_H diff --git a/compute_kernel_writer/validation/tests/common/Common.h b/compute_kernel_writer/validation/tests/common/Common.h new file mode 100644 index 0000000000..8573c42b88 --- /dev/null +++ b/compute_kernel_writer/validation/tests/common/Common.h @@ -0,0 +1,71 @@ +/* + * 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_TEST_COMMON_COMMON_H +#define COMPUTE_KERNEL_WRITER_TEST_COMMON_COMMON_H + +#include <cassert> +#include <iostream> +#include <string> + +namespace ckw +{ +#define VALIDATE_ON_MSG(exp, msg) assert(((void)msg, exp)) + +#define VALIDATE_TEST(exp, all_tests_passed, id_test) \ + do \ + { \ + if((exp) == true) \ + { \ + all_tests_passed &= true; \ + const std::string msg = "TEST " + std::to_string((id_test)) + ": [PASSED]"; \ + std::cout << msg << std::endl; \ + } \ + else \ + { \ + all_tests_passed &= false; \ + const std::string msg = "TEST " + std::to_string((id_test)) + ": [FAILED]"; \ + std::cout << msg << std::endl; \ + } \ + } while(false) + +class ITest +{ +public: + virtual ~ITest() = default; + + /** Method to run the test + * + * @return it returns true if all tests passed + */ + virtual bool run() = 0; + + /** Name of the test + * + * @return it returns the name of the test + */ + virtual std::string name() = 0; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_TEST_COMMON_COMMON_H */ diff --git a/compute_kernel_writer/validation/tests/common/KernelWriterInterceptor.h b/compute_kernel_writer/validation/tests/common/KernelWriterInterceptor.h new file mode 100644 index 0000000000..89bb76e37f --- /dev/null +++ b/compute_kernel_writer/validation/tests/common/KernelWriterInterceptor.h @@ -0,0 +1,90 @@ +/* + * 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 CKW_VALIDATION_TESTS_COMMON_KERNELWRITERINTERCEPTOR_H +#define CKW_VALIDATION_TESTS_COMMON_KERNELWRITERINTERCEPTOR_H + +#include <string> +#include <utility> + +namespace ckw +{ + +/** This class provides the ability to capture only the code changed after a point + * and compares that part of code with the expected value. + * + * It is useful for testing purpose when a particular sequence of instructions is interested + * while the rest of the initialization code is out of scope. + */ +template <typename T> +class KernelWriterInterceptor : public T +{ +public: + template <typename... TArgs> + KernelWriterInterceptor(const TArgs &&...args) + : T(std::forward<TArgs>(args)...) + { + } + + /** Mark this point in the source code as the start position to capture. + * Only source code added after this function is considered when check_add_code is called. + */ + void start_capture_code() + { + _start_code = this->body_source_code(); + } + + /** Compare the source code added after start_capture_code is called the the specified expected code. */ + bool check_added_code(const std::string &expected_added_code) + { + const auto &end_code = this->body_source_code(); + + // Code can only grow over time. + if(end_code.length() < _start_code.length()) + { + return false; + } + + // New code must be added to the source code without changing the already existed code. + if(end_code.substr(0, _start_code.length()) != _start_code) + { + return false; + } + + // The newly added code must match the expected value. + if(end_code.substr(_start_code.length(), end_code.length() - _start_code.length()) != expected_added_code) + { + return false; + } + + return true; + } + +private: + std::string _start_code{}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_COMMON_KERNELWRITERINTERCEPTOR_H |