From 806b8e856911e6691ede6725c7e2a0e7e0dd6e95 Mon Sep 17 00:00:00 2001 From: Gunes Bayir Date: Wed, 23 Aug 2023 23:28:31 +0100 Subject: Add declare_constant_tile API function in CKW Resolves: COMPMID-6535 Change-Id: I07d8aca96a0fcbd624f828b24513ee0500a14a74 Signed-off-by: Gunes Bayir Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/10200 Reviewed-by: Viet-Hoa Do Comments-Addressed: Arm Jenkins Tested-by: Arm Jenkins Benchmark: Arm Jenkins --- compute_kernel_writer/CMakeLists.txt | 1 + compute_kernel_writer/include/ckw/KernelWriter.h | 22 +++- .../include/ckw/types/ConstantData.h | 89 +++++++++++++++ compute_kernel_writer/src/KernelWriter.cpp | 10 ++ compute_kernel_writer/src/cl/CLKernelWriter.cpp | 25 ++++- compute_kernel_writer/src/cl/CLKernelWriter.h | 15 ++- compute_kernel_writer/src/types/ConstantData.cpp | 120 +++++++++++++++++++++ compute_kernel_writer/validation/Validation.cpp | 3 + .../tests/CLKernelWriterDeclareConstantTileTest.h | 106 ++++++++++++++++++ 9 files changed, 383 insertions(+), 8 deletions(-) create mode 100644 compute_kernel_writer/include/ckw/types/ConstantData.h create mode 100644 compute_kernel_writer/src/types/ConstantData.cpp create mode 100644 compute_kernel_writer/validation/tests/CLKernelWriterDeclareConstantTileTest.h (limited to 'compute_kernel_writer') diff --git a/compute_kernel_writer/CMakeLists.txt b/compute_kernel_writer/CMakeLists.txt index a539ef7186..8ec8ba24be 100644 --- a/compute_kernel_writer/CMakeLists.txt +++ b/compute_kernel_writer/CMakeLists.txt @@ -118,6 +118,7 @@ target_compile_definitions(ckw PUBLIC ) target_sources(ckw PRIVATE + src/types/ConstantData.cpp src/types/DataTypeHelpers.cpp src/Error.cpp src/Helpers.cpp diff --git a/compute_kernel_writer/include/ckw/KernelWriter.h b/compute_kernel_writer/include/ckw/KernelWriter.h index d59867fa6f..022ae83999 100644 --- a/compute_kernel_writer/include/ckw/KernelWriter.h +++ b/compute_kernel_writer/include/ckw/KernelWriter.h @@ -27,6 +27,7 @@ #include "ckw/TensorOperand.h" #include "ckw/TileOperand.h" +#include "ckw/types/ConstantData.h" #include "ckw/types/ConvertPolicy.h" #include "ckw/types/Operators.h" @@ -37,13 +38,13 @@ namespace ckw { -class Kernel; - /** Forward Declerations */ +class Kernel; class TensorInfo; class TensorSampler; class TileInfo; +enum class DataType; enum class TargetArchitecture; enum class TargetLanguage; @@ -175,10 +176,19 @@ public: * @param[in] name Name of the tile * @param[in] tile_info Shape and data type of the tile * - * @returns The created tile operand + * @return The created tile operand */ virtual TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) = 0; + /** Declare a constant tile given a @ref:ConstantData object + * + * @param[in] data a @ref ckw::ConstantData object that has the values and the + * underlying data type of the constant tile + * + * @return The created constant tile operand + */ + virtual TileOperand declare_constant_tile(const ConstantData &data) = 0; + /** Load the data from the tensor memory to the tile using the sampling information. * * @param[in] tile_op The tile to be loaded. @@ -240,6 +250,12 @@ protected: /** Get the reference to tensor object from the tensor operand. */ static ITensor &get_tensor(const TensorOperand &operand); + /** Get the values of a constant data object. */ + static const std::vector> &get_values(const ConstantData &data); + + /** Get the data type of a constant data object. */ + static DataType get_data_type(const ConstantData &data); + private: int32_t _id_space{ 0 }; }; diff --git a/compute_kernel_writer/include/ckw/types/ConstantData.h b/compute_kernel_writer/include/ckw/types/ConstantData.h new file mode 100644 index 0000000000..95425b2c65 --- /dev/null +++ b/compute_kernel_writer/include/ckw/types/ConstantData.h @@ -0,0 +1,89 @@ +/* + * 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_INCLUDE_CKW_TYPES_CONSTANTDATA_H +#define CKW_INCLUDE_CKW_TYPES_CONSTANTDATA_H + +#include "ckw/Error.h" +#include "ckw/types/DataType.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace ckw +{ +// Forward Declarations +class KernelWriter; + +class ConstantData +{ + using String = std::string; + using StringVector = std::vector; + +public: + /** Templated constructor */ + template + ConstantData(std::initializer_list> values, DataType data_type); + +private: + /** Validate the given data type and the template type + * + * @param[in] data_type data type + * + * @return true if user provided data type and the template type are conformant + */ + template + bool validate(DataType data_type); + + /** Get the constant data as a 2d vector of string values + * + * @return a 2d vector of strings that has the string-converted values + */ + const std::vector& values() const; + + /** Get the underlying data type of the constant values + * + * @return a @ref ckw::DataType object that represents the underlying data type + */ + DataType data_type() const; + + // Friends + friend class KernelWriter; + +private: + // Data members + std::vector _values{}; + DataType _data_type{}; +}; + +} // namespace ckw + +#endif // CKW_INCLUDE_CKW_TYPES_CONSTANTDATA_H diff --git a/compute_kernel_writer/src/KernelWriter.cpp b/compute_kernel_writer/src/KernelWriter.cpp index ce34a1c2d6..21a61d73bf 100644 --- a/compute_kernel_writer/src/KernelWriter.cpp +++ b/compute_kernel_writer/src/KernelWriter.cpp @@ -81,4 +81,14 @@ ITensor &KernelWriter::get_tensor(const TensorOperand &operand) return operand._tensor; } +const std::vector> &KernelWriter::get_values(const ConstantData &data) +{ + return data.values(); +} + +DataType KernelWriter::get_data_type(const ConstantData &data) +{ + return data.data_type(); +} + } // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLKernelWriter.cpp b/compute_kernel_writer/src/cl/CLKernelWriter.cpp index 312162f498..79d0f985d0 100644 --- a/compute_kernel_writer/src/cl/CLKernelWriter.cpp +++ b/compute_kernel_writer/src/cl/CLKernelWriter.cpp @@ -28,6 +28,7 @@ #include "ckw/Kernel.h" #include "ckw/TensorSampler.h" #include "ckw/TileOperand.h" +#include "ckw/types/DataType.h" #include "ckw/types/MemoryOperation.h" #include "ckw/types/TargetLanguage.h" #include "src/ITensorComponent.h" @@ -37,7 +38,6 @@ #include "src/cl/helpers/CLMemoryOpBufferHelper.h" #include "src/cl/helpers/CLMemoryOpImage2dHelper.h" #include "src/cl/helpers/ICLMemoryOpHelper.h" - #include "src/types/DataTypeHelpers.h" #include @@ -364,7 +364,7 @@ TileOperand CLKernelWriter::declare_tile(const std::string &name, const TileInfo return e->name() == fullname; }) == _tiles.end(), - "Tile name must be unique."); + "There is already a tile with name: " + fullname); auto tile = std::make_unique(fullname, tile_info); @@ -381,17 +381,27 @@ TileOperand CLKernelWriter::declare_tile(const std::string &name, const TileInfo return operand; } +TileOperand CLKernelWriter::declare_constant_tile(const ConstantData &data) +{ + auto tile = std::make_unique(get_values(data), get_data_type(data)); + const TileOperand operand = create_tile_operand(*tile); + _constant_tiles.insert(std::move(tile)); + + return operand; +} + void CLKernelWriter::op_write_raw_code(const std::string &raw_code) { append_code(raw_code); } -const CLTile &CLKernelWriter::to_cl_tile(const TileOperand &operand) +const CLTile &CLKernelWriter::to_cl_tile(const TileOperand &operand) const { const auto &tile = get_tile(operand); #ifdef COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED // Check if the tile is a CLTile created by this kernel writer. + { bool found = false; @@ -404,6 +414,15 @@ const CLTile &CLKernelWriter::to_cl_tile(const TileOperand &operand) } } + for(const auto &t : _constant_tiles) + { + if(&tile == t.get()) + { + found = true; + break; + } + } + if(!found) { for(const auto &t : _tensors) diff --git a/compute_kernel_writer/src/cl/CLKernelWriter.h b/compute_kernel_writer/src/cl/CLKernelWriter.h index 2a6b79c691..d2c84f192e 100644 --- a/compute_kernel_writer/src/cl/CLKernelWriter.h +++ b/compute_kernel_writer/src/cl/CLKernelWriter.h @@ -29,17 +29,21 @@ #include #include +#include #include namespace ckw { +// Forward Declarations class CLTile; class CLTensorArgument; +class ConstantData; +class TensorOperand; class TensorSampler; class TileOperand; -class TensorOperand; +enum class DataType; enum class MemoryOperation; /** OpenCL kernel writer. */ @@ -96,6 +100,12 @@ public: */ TileOperand declare_tile(const std::string &name, const TileInfo &tile_info) override; + /** Declare a constant tile given a @ref:ConstantData object + * + * Similar to @ref KernelWriter::declare_constant_tile() + */ + TileOperand declare_constant_tile(const ConstantData &data) override; + // ============================================================================================= // Memory Operations // ============================================================================================= @@ -139,7 +149,7 @@ protected: * * This function performs appropriate check before doing type casting. */ - const CLTile &to_cl_tile(const TileOperand &operand); + const CLTile &to_cl_tile(const TileOperand &operand) const; /** Append the specified code to the kernel body source code. */ template @@ -179,6 +189,7 @@ private: std::set> _tensors{}; std::set> _tiles{}; + std::set> _constant_tiles{}; }; } // namespace ckw diff --git a/compute_kernel_writer/src/types/ConstantData.cpp b/compute_kernel_writer/src/types/ConstantData.cpp new file mode 100644 index 0000000000..d2155cf55a --- /dev/null +++ b/compute_kernel_writer/src/types/ConstantData.cpp @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "ckw/types/ConstantData.h" + +#include + +namespace ckw +{ +namespace +{ + template + inline typename std::enable_if::value, std::string>::type to_str(T value) + { + std::stringstream ss; + ss << std::scientific << std::setprecision(std::numeric_limits::max_digits10) << value; + return ss.str(); + } + + template + inline typename std::enable_if::value && !std::is_same::value, std::string>::type to_str(T value) + { + return std::to_string(value); + } + + template + inline typename std::enable_if::value, std::string>::type to_str(T value) + { + return std::to_string((int) value); + } +} + +template +ConstantData::ConstantData(std::initializer_list> values, DataType data_type) + : _data_type(data_type) +{ + CKW_ASSERT(validate(data_type)); + CKW_ASSERT(values.size() > 0); + + for(auto value_arr: values) + { + // Each row must have the same number of elements + CKW_ASSERT(value_arr.size() == (*values.begin()).size()); + + StringVector vec; + std::transform(value_arr.begin(), value_arr.end(), + std::back_inserter(vec), + [](T val) { return to_str(val); }); + + _values.push_back(std::move(vec)); + } +} + +template +bool ConstantData::validate(DataType data_type) +{ + switch(data_type) + { + case DataType::Fp32: + case DataType::Fp16: + return std::is_same::value; + case DataType::Bool: + return std::is_same::value; + case DataType::Int32: + case DataType::Int16: + case DataType::Int8: + return std::is_same::value; + case DataType::Uint32: + case DataType::Uint16: + case DataType::Uint8: + return std::is_same::value; + default: + CKW_THROW_MSG("Unknown data type!"); + break; + } +} + +// Necessary instantiations for compiler to recognize +template ConstantData::ConstantData(std::initializer_list>, DataType); +template ConstantData::ConstantData(std::initializer_list>, DataType); +template ConstantData::ConstantData(std::initializer_list>, DataType); +template ConstantData::ConstantData(std::initializer_list>, DataType); + +template bool ConstantData::validate(DataType); +template bool ConstantData::validate(DataType); +template bool ConstantData::validate(DataType); +template bool ConstantData::validate(DataType); + +const std::vector>& ConstantData::values() const +{ + return _values; +} + +DataType ConstantData::data_type() const +{ + return _data_type; +} + +} // namespace ckw diff --git a/compute_kernel_writer/validation/Validation.cpp b/compute_kernel_writer/validation/Validation.cpp index 20957d90a5..3d73900c90 100644 --- a/compute_kernel_writer/validation/Validation.cpp +++ b/compute_kernel_writer/validation/Validation.cpp @@ -27,6 +27,7 @@ #include "validation/tests/CLKernelWriterBinaryOpTest.h" #include "validation/tests/CLKernelWriterCastTest.h" #include "validation/tests/CLKernelWriterCommentTest.h" +#include "validation/tests/CLKernelWriterDeclareConstantTileTest.h" #include "validation/tests/CLKernelWriterDeclareTensorTest.h" #include "validation/tests/CLKernelWriterDeclareTileTest.h" #include "validation/tests/CLKernelWriterOpLoadStoreTest.h" @@ -87,6 +88,7 @@ int32_t main() const auto test28 = std::make_unique(); const auto test29 = std::make_unique(); const auto test30 = std::make_unique(); + const auto test31 = std::make_unique(); tests.push_back(test3.get()); tests.push_back(test4.get()); @@ -118,6 +120,7 @@ int32_t main() tests.push_back(test28.get()); tests.push_back(test29.get()); tests.push_back(test30.get()); + tests.push_back(test31.get()); #endif /* COMPUTE_KERNEL_WRITER_OPENCL_ENABLED */ bool all_test_passed = true; 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 +#include +#include + +namespace ckw +{ +class CLKernelWriterDeclareConstantTileTest : public ITest +{ + using TestConfig = std::tuple; +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 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 _configs {}; +}; + +} // namespace ckw + +#endif // CKW_VALIDATION_TESTS_CLKERNELWRITERDECLARECONSTANTTILETEST_H -- cgit v1.2.1