diff options
Diffstat (limited to 'compute_kernel_writer/src')
-rw-r--r-- | compute_kernel_writer/src/Error.cpp | 40 | ||||
-rw-r--r-- | compute_kernel_writer/src/Helpers.cpp | 63 | ||||
-rw-r--r-- | compute_kernel_writer/src/Helpers.h | 56 | ||||
-rw-r--r-- | compute_kernel_writer/src/ITile.h | 134 | ||||
-rw-r--r-- | compute_kernel_writer/src/TensorInfo.cpp | 77 | ||||
-rw-r--r-- | compute_kernel_writer/src/TensorUtils.cpp | 116 | ||||
-rw-r--r-- | compute_kernel_writer/src/TensorUtils.h | 56 | ||||
-rw-r--r-- | compute_kernel_writer/src/TileInfo.cpp | 76 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLHelpers.cpp | 91 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLHelpers.h | 53 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTile.cpp | 156 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTile.h | 61 |
12 files changed, 979 insertions, 0 deletions
diff --git a/compute_kernel_writer/src/Error.cpp b/compute_kernel_writer/src/Error.cpp new file mode 100644 index 0000000000..7f2fb41187 --- /dev/null +++ b/compute_kernel_writer/src/Error.cpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "ckw/Error.h" + +#include <string> + +namespace ckw +{ +std::string create_error_msg(const std::string &file, const std::string &func, const std::string &line, const std::string &msg) +{ + std::string err; + err += "[COMPUTE_KERNEL_WRITER][ERROR]:"; + err += " " + file + ":" + line; + err += " " + func; + err += " " + msg; + return err; +} +} // namespace ckw
\ No newline at end of file diff --git a/compute_kernel_writer/src/Helpers.cpp b/compute_kernel_writer/src/Helpers.cpp new file mode 100644 index 0000000000..799f79a187 --- /dev/null +++ b/compute_kernel_writer/src/Helpers.cpp @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "ckw/Error.h" + +#include "src/Helpers.h" + +namespace ckw +{ +std::string dec_to_hex_as_string(int32_t dec) +{ + switch(dec) + { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + return std::to_string(dec); + case 10: + return "A"; + case 11: + return "B"; + case 12: + return "C"; + case 13: + return "D"; + case 14: + return "E"; + case 15: + return "F"; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported decimal number"); + return ""; + } +} +} // namespace ckw diff --git a/compute_kernel_writer/src/Helpers.h b/compute_kernel_writer/src/Helpers.h new file mode 100644 index 0000000000..f7ba7cec1c --- /dev/null +++ b/compute_kernel_writer/src/Helpers.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef COMPUTE_KERNEL_WRITER_SRC_HELPERS_H +#define COMPUTE_KERNEL_WRITER_SRC_HELPERS_H + +#include <cstdint> +#include <string> + +/** Generic helper functions */ +namespace ckw +{ +/** Helper function to convert a decimal number passed as int32_t variable to hexadecimal number as string + * + * @param[in] dec Decimal number. It must be >= 0 and < 16 + * + * @return the OpenCL datatype as a string + */ +std::string dec_to_hex_as_string(int32_t dec); + +/** Helper function to clamp a value between min_val and max_val + * + * @param[in] val Value to clamp + * @param[in] min_val Lower value + * @param[in] max_val Upper value + * + * @return the clamped value + */ +template <typename T> +T clamp(const T& val, const T& min_val, const T& max_val) +{ + return std::max(min_val, std::min(val, max_val)); +} +} +#endif /* COMPUTE_KERNEL_WRITER_SRC_HELPERS_H */ diff --git a/compute_kernel_writer/src/ITile.h b/compute_kernel_writer/src/ITile.h new file mode 100644 index 0000000000..283e6fa236 --- /dev/null +++ b/compute_kernel_writer/src/ITile.h @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef COMPUTE_KERNEL_WRITER_SRC_ITILE_H +#define COMPUTE_KERNEL_WRITER_SRC_ITILE_H + +#include "ckw/TileInfo.h" + +#include <string> +#include <vector> + +namespace ckw +{ +/** Tile descriptor which reports the underlying datatype and vector length */ +struct TileVariableDescriptor +{ + DataType dt { DataType::Unknown }; /** Data type */ + int32_t len { 1 }; /** Number of elements in a single variable. For example, 1 for scalar */ +}; + +/** Tile variable */ +struct TileVariable +{ + std::string str {""}; /** Tile variable as a string */ + TileVariableDescriptor desc {}; /** Tile value descriptor which reports the datatype and vector length */ +}; + +/** Tile base class. + * A Tile is a collection of variables (either program variables or constants) used to express a 2D data. + */ +class ITile +{ +public: + virtual ~ITile() = default; + /** Method to get all TileVariable objects + * + * @return a vector containing all @ref TileVariable objects + */ + virtual std::vector<TileVariable> all() const = 0; + /** Method to get the name of the tile. + * + * @return the name of the tile + */ + std::string name() const + { + return _basename; + } + /** Method to get the tile info + * + * @return the @ref TileInfo + */ + TileInfo info() const + { + return _info; + } + /** Method to know whether the tile is assignable or not. + * For example, a constant tile is not assignable. + * + * @return true if the tile is assignable + */ + virtual bool is_assignable() const = 0; + +protected: + TileInfo _info { DataType::Unknown }; // Tile info + std::string _basename { "" }; // Tile name +}; + +/** Tile base class to store scalar variables. + */ +class IScalarTile : public ITile +{ +public: + virtual ~IScalarTile() = default; + /** Method to get the scalar variable from a tile as a string + * @param[in] col Tile column. If out-of-bound, the column is clamped to the nearest valid edge + * @param[in] row Tile row. If out-of-bound, the row is clamped to the nearest valid edge + * + * @return the @ref TileVariable + */ + virtual TileVariable scalar(int32_t col, int32_t row) const = 0; +}; + +/** Tile base class to store vector variables. It derives from IScalarTile since we can still access the scalar variable + */ +class IVectorTile : public IScalarTile +{ +public: + virtual ~IVectorTile() = default; + /** Method to get the vector variable from a tile. + * The user can query the list of supported vector lengths through the supported_vector_lengths() method. + * + * @param[in] row Tile row. If out-of-bound, the row is clamped to the nearest valid edge + * + * @return the vector variable as a @ref TileVariable + */ + virtual TileVariable vector(int32_t row) const = 0; + /** Method to get a sub-vector variable. The length of the sub-vector must be supported by the derived IVectorTile class + * + * @param[in] col_start Tile starting column to get the sub-vector. If out-of-bound, the derived IVectorTile class may throw an assert. + * @param[in] width The width of the sub-vector. The width must be supported by the derived IVectorTile class and the last element must be in-bound. + * @param[in] row Tile row. If out-of-bound, the row is clamped to the nearest valid edge + * + * @return the vector variable as a @ref TileVariable + */ + virtual TileVariable vector(int32_t col_start, int32_t width, int32_t row) const = 0; + /** Method to get the supported vector length. + * + * @return a vector containing the supported vector lengths + */ + virtual std::vector<int32_t> supported_vector_lengths() const = 0; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_SRC_ITILE_H */ diff --git a/compute_kernel_writer/src/TensorInfo.cpp b/compute_kernel_writer/src/TensorInfo.cpp new file mode 100644 index 0000000000..561c126469 --- /dev/null +++ b/compute_kernel_writer/src/TensorInfo.cpp @@ -0,0 +1,77 @@ +/* + * 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/TensorInfo.h" + +namespace ckw +{ +TensorInfo::TensorInfo(DataType dt, const TensorShape &shape, TensorDataLayout dl, int32_t id) + : _shape(shape), _dt(dt), _dl(dl), _id(id) +{ +} + +TensorInfo &TensorInfo::shape(const TensorShape &shape) +{ + _shape = shape; + return *this; +} + +TensorShape TensorInfo::shape() const +{ + return _shape; +} + +TensorInfo &TensorInfo::data_type(DataType dt) +{ + _dt = dt; + return *this; +} + +DataType TensorInfo::data_type() const +{ + return _dt; +} + +TensorInfo &TensorInfo::data_layout(TensorDataLayout dl) +{ + _dl = dl; + return *this; +} + +TensorDataLayout TensorInfo::data_layout() const +{ + return _dl; +} + +TensorInfo &TensorInfo::id(int32_t id) +{ + _id = id; + return *this; +} + +int32_t TensorInfo::id() const +{ + return _id; +} +} // namespace ckw diff --git a/compute_kernel_writer/src/TensorUtils.cpp b/compute_kernel_writer/src/TensorUtils.cpp new file mode 100644 index 0000000000..cc179b4b51 --- /dev/null +++ b/compute_kernel_writer/src/TensorUtils.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "ckw/Error.h" +#include "ckw/TensorInfo.h" +#include "ckw/Types.h" + +#include "src/TensorUtils.h" + +namespace ckw +{ +TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component) +{ + switch(layout) + { + case TensorDataLayout::Nhwc: + switch(component) + { + case TensorDataLayoutComponent::C: + return TensorComponent::Dim0; + case TensorDataLayoutComponent::W: + return TensorComponent::Dim1; + case TensorDataLayoutComponent::H: + return TensorComponent::Dim2; + case TensorDataLayoutComponent::N: + return TensorComponent::Dim3; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC"); + return TensorComponent::Unknown; + } + case TensorDataLayout::Ndhwc: + switch(component) + { + case TensorDataLayoutComponent::C: + return TensorComponent::Dim0; + case TensorDataLayoutComponent::W: + return TensorComponent::Dim1; + case TensorDataLayoutComponent::H: + return TensorComponent::Dim2; + case TensorDataLayoutComponent::D: + return TensorComponent::Dim3; + case TensorDataLayoutComponent::N: + return TensorComponent::Dim4; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC"); + return TensorComponent::Unknown; + } + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout"); + return TensorComponent::Unknown; + } +} + +TensorComponent get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component) +{ + switch(layout) + { + case TensorDataLayout::Nhwc: + switch(component) + { + case TensorDataLayoutComponent::C: + return TensorComponent::Stride0; + case TensorDataLayoutComponent::W: + return TensorComponent::Stride1; + case TensorDataLayoutComponent::H: + return TensorComponent::Stride2; + case TensorDataLayoutComponent::N: + return TensorComponent::Stride3; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC"); + return TensorComponent::Unknown; + } + case TensorDataLayout::Ndhwc: + switch(component) + { + case TensorDataLayoutComponent::C: + return TensorComponent::Stride0; + case TensorDataLayoutComponent::W: + return TensorComponent::Stride1; + case TensorDataLayoutComponent::H: + return TensorComponent::Stride2; + case TensorDataLayoutComponent::D: + return TensorComponent::Stride3; + case TensorDataLayoutComponent::N: + return TensorComponent::Stride4; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC"); + return TensorComponent::Unknown; + } + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout"); + return TensorComponent::Unknown; + } +} +} // namespace ckw diff --git a/compute_kernel_writer/src/TensorUtils.h b/compute_kernel_writer/src/TensorUtils.h new file mode 100644 index 0000000000..4be0395435 --- /dev/null +++ b/compute_kernel_writer/src/TensorUtils.h @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef COMPUTE_KERNEL_WRITER_SRC_TENSORUTILS_H +#define COMPUTE_KERNEL_WRITER_SRC_TENSORUTILS_H + +#include <cstdint> + +/** Tensor specific utility functions */ +namespace ckw +{ +// Forward declarations +enum class TensorDataLayout; +enum class TensorDataLayoutComponent; +enum class TensorComponent : uint32_t; + +/** Get tensor dimension from a given data layout and data layout component + * + * @param[in] layout Layout of the tensor + * @param[in] component Data layout component + * + * @return the @ref TensorComponent + */ +TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component); + +/** Get tensor stride from a given data layout and data layout component + * + * @param[in] layout Layout of the tensor + * @param[in] component Data layout component + * + * @return the @ref TensorComponent + */ +TensorComponent get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component); +} +#endif /* COMPUTE_KERNEL_WRITER_SRC_TENSORUTILS_H */ diff --git a/compute_kernel_writer/src/TileInfo.cpp b/compute_kernel_writer/src/TileInfo.cpp new file mode 100644 index 0000000000..6dd1957a7a --- /dev/null +++ b/compute_kernel_writer/src/TileInfo.cpp @@ -0,0 +1,76 @@ +/* + * 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/TileInfo.h" + +namespace ckw +{ +TileInfo::TileInfo(DataType dt) + : _dt(dt), _shape({{1, 1}}) +{ +} + +TileInfo::TileInfo(DataType dt, int32_t w) + : _dt(dt), _shape({{w, 1}}) +{ +} + +TileInfo::TileInfo(DataType dt, int32_t w, int32_t h) + : _dt(dt), _shape({{w, h}}) +{ +} + +TileInfo &TileInfo::width(int32_t w) +{ + _shape[kTileWidthIdx] = w; + return *this; +} + +int32_t TileInfo::width() const +{ + return _shape[kTileWidthIdx]; +} + +TileInfo &TileInfo::height(int32_t h) +{ + _shape[kTileHeightIdx] = h; + return *this; +} + +int32_t TileInfo::height() const +{ + return _shape[kTileHeightIdx]; +} + +TileInfo &TileInfo::data_type(DataType dt) +{ + _dt = dt; + return *this; +} + +DataType TileInfo::data_type() const +{ + return _dt; +} +} // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLHelpers.cpp b/compute_kernel_writer/src/cl/CLHelpers.cpp new file mode 100644 index 0000000000..68d7db252b --- /dev/null +++ b/compute_kernel_writer/src/cl/CLHelpers.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "ckw/Error.h" +#include "ckw/Types.h" + +#include "src/cl/CLHelpers.h" + +namespace ckw +{ +bool cl_validate_vector_length(int32_t len) +{ + bool valid_vector_length = true; + if(len < 1 || len > 16 || (len > 4 && len < 8) || (len > 8 && len < 16)) + { + valid_vector_length = false; + } + return valid_vector_length; +} + +std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len) +{ + if(cl_validate_vector_length(len) == false) + { + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported vector length"); + return ""; + } + + std::string res; + switch(dt) + { + case DataType::Fp32: + res += "float"; + break; + case DataType::Fp16: + res += "half"; + break; + case DataType::Int8: + res += "char"; + break; + case DataType::Uint8: + res += "uchar"; + break; + case DataType::Uint16: + res += "ushort"; + break; + case DataType::Int16: + res += "short"; + break; + case DataType::Uint32: + res += "uint"; + break; + case DataType::Int32: + res += "int"; + break; + case DataType::Bool: + res += "bool"; + break; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported datatype"); + return ""; + } + + if(len > 1) + { + res += std::to_string(len); + } + + return res; +} +} // namespace ckw
\ No newline at end of file diff --git a/compute_kernel_writer/src/cl/CLHelpers.h b/compute_kernel_writer/src/cl/CLHelpers.h new file mode 100644 index 0000000000..915d59f458 --- /dev/null +++ b/compute_kernel_writer/src/cl/CLHelpers.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H +#define COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H + +#include <string> + +/** OpenCL specific helper functions */ +namespace ckw +{ +// Forward declarations +enum class DataType; + +/** Helper function to validate the vector length of OpenCL vector data types + * + * @param[in] len Vector length + * + * @return true if the vector lenght is valid. It returns false, otherwise. + */ +bool cl_validate_vector_length(int32_t len); + +/** Helper function to return the OpenCL datatype as a string from a @ref DataType and vector length as int32_t variable + * + * @param[in] dt Datatype + * @param[in] len Vector length + * + * @return the OpenCL datatype as a string + */ +std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len); +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H */ diff --git a/compute_kernel_writer/src/cl/CLTile.cpp b/compute_kernel_writer/src/cl/CLTile.cpp new file mode 100644 index 0000000000..a46f692a5c --- /dev/null +++ b/compute_kernel_writer/src/cl/CLTile.cpp @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "ckw/Error.h" +#include "ckw/TileInfo.h" + +#include "src/Helpers.h" +#include "src/cl/CLHelpers.h" +#include "src/cl/CLTile.h" + +#include <algorithm> +#include <vector> + +namespace ckw +{ +CLTile::CLTile(const std::string &name, const TileInfo &info) +{ + validate_tile_info(info); + + _basename = name; + _info = info; +} + +TileVariable CLTile::scalar(int32_t col, int32_t row) const +{ + // Clamp to nearest valid edge + col = clamp(col, static_cast<int32_t>(0), _info.width() - 1); + row = clamp(row, static_cast<int32_t>(0), _info.height() - 1); + + TileVariable t; + t.str = create_var_name(row); + t.desc.dt = _info.data_type(); + t.desc.len = 1; + + // This check is required because if the width has only one element, we cannot use .s0 + if(_info.width() != 1) + { + // Automatic broadcasting + t.str += ".s" + dec_to_hex_as_string(col); + } + + return t; +} + +TileVariable CLTile::vector(int32_t row) const +{ + // Clamp to nearest valid edge + row = clamp(row, static_cast<int32_t>(0), _info.height() - 1); + + TileVariable t; + t.str = create_var_name(row); + t.desc.dt = _info.data_type(); + t.desc.len = _info.width(); + return t; +} + +TileVariable CLTile::vector(int32_t col_start, int32_t width, int32_t row) const +{ + // Validate the new vector length + cl_validate_vector_length(width); + + // Clamp to nearest valid edge + row = clamp(row, static_cast<int32_t>(0), _info.height() - 1); + + TileVariable t; + t.str = create_var_name(row); + t.desc.dt = _info.data_type(); + t.desc.len = width; + + if(_info.width() != 1) + { + t.str += ".s"; + for(int i = 0; i < width; ++i) + { + t.str += dec_to_hex_as_string(col_start + i); + } + } + return t; +} + +std::vector<TileVariable> CLTile::all() const +{ + std::vector<TileVariable> vars; + for(int32_t y = 0; y < _info.height(); ++y) + { + TileVariable t; + t.str = create_var_name(y); + t.desc.dt = _info.data_type(); + t.desc.len = _info.width(); + vars.push_back(t); + } + return vars; +} + +std::vector<int32_t> CLTile::supported_vector_lengths() const +{ + return std::vector<int32_t> {1, 2, 3, 4, 8, 16}; +} + +bool CLTile::is_assignable() const +{ + return true; +} + +std::string CLTile::create_var_name(int32_t row) const +{ + std::string var_name = _basename; + + // If a scalar variable, we do not append the row index + if(_info.height() == 1) + { + return var_name; + + } + else + { + var_name += "_"; + var_name += std::to_string(row); + } + + return var_name; +} + +void CLTile::validate_tile_info(const TileInfo &info) const +{ + if(cl_validate_vector_length(info.width())) + { + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported TileInfo width"); + } + + if(info.data_type() == DataType::Unknown) + { + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("DataType::Unknown is not supported"); + } +} +} // namespace ckw
\ No newline at end of file diff --git a/compute_kernel_writer/src/cl/CLTile.h b/compute_kernel_writer/src/cl/CLTile.h new file mode 100644 index 0000000000..50801675a7 --- /dev/null +++ b/compute_kernel_writer/src/cl/CLTile.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H +#define COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H + +#include "src/ITile.h" + +#include <string> + +namespace ckw +{ +// Forward declarations +class TileInfo; + +/** OpenCL specific tile */ +class CLTile : protected IVectorTile +{ +public: + /** Constructor + * + * @param[in] name Tile name + * @param[in] info Tile info + */ + CLTile(const std::string& name, const TileInfo &info); + + // Inherited method overridden + TileVariable scalar(int32_t col, int32_t row) const override; + TileVariable vector(int32_t row) const override; + TileVariable vector(int32_t col_start, int32_t width, int32_t row) const override; + std::vector<TileVariable> all() const override; + std::vector<int32_t> supported_vector_lengths() const override; + bool is_assignable() const override; + +private: + std::string create_var_name(int32_t row) const; + void validate_tile_info(const TileInfo &info) const; +}; +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_SRC_CL_CLTILE_H */ |