diff options
author | Gian Marco Iodice <gianmarco.iodice@arm.com> | 2023-07-07 11:25:57 +0100 |
---|---|---|
committer | Viet-Hoa Do <viet-hoa.do@arm.com> | 2023-07-20 08:48:15 +0000 |
commit | ebfdb5a1ea73c2269eec5af492970c2174ab7d0f (patch) | |
tree | 5c60d083100118f0a40e629dfa69b7a7373dd7fd /compute_kernel_writer/src | |
parent | 29dc9fc1d3d6e90746ba1173e3318b774dcf7bed (diff) | |
download | ComputeLibrary-ebfdb5a1ea73c2269eec5af492970c2174ab7d0f.tar.gz |
Integrate CLTensorArgument
- Add CLTensorArgument to query the components and storages as OpenCL
variables (or by values when possible)
- Add caching mechanism in CLTensorArgument to keep track of the components and storages used
- Add unit tests
Resolves COMPMID-5787
Signed-off-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
Signed-off-by: Viet-Hoa Do <viet-hoa.do@arm.com>
Change-Id: Ib39e1f77b097e5b907a296fe6b0d41bb4bcd4ffc
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/9908
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Benchmark: Arm Jenkins <bsgcomp@arm.com>
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Gunes Bayir <gunes.bayir@arm.com>
Reviewed-by: Jakub Sujak <jakub.sujak@arm.com>
Diffstat (limited to 'compute_kernel_writer/src')
-rw-r--r-- | compute_kernel_writer/src/ITensorArgument.h | 121 | ||||
-rw-r--r-- | compute_kernel_writer/src/TensorUtils.cpp | 56 | ||||
-rw-r--r-- | compute_kernel_writer/src/TensorUtils.h | 13 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLHelpers.cpp | 26 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLHelpers.h | 10 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTensorArgument.cpp | 247 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTensorArgument.h | 70 | ||||
-rw-r--r-- | compute_kernel_writer/src/types/TensorComponentType.h | 78 |
8 files changed, 584 insertions, 37 deletions
diff --git a/compute_kernel_writer/src/ITensorArgument.h b/compute_kernel_writer/src/ITensorArgument.h new file mode 100644 index 0000000000..40ad69fdc0 --- /dev/null +++ b/compute_kernel_writer/src/ITensorArgument.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef CKW_SRC_ITENSORARGUMENT_H +#define CKW_SRC_ITENSORARGUMENT_H + +#include "ckw/TensorInfo.h" +#include "ckw/types/TensorComponentType.h" +#include "ckw/types/TensorStorageType.h" +#include "src/ITile.h" + +#include <string> +#include <vector> + +namespace ckw +{ +/** Tensor storage variable */ +struct TensorStorageVariable +{ + std::string val{ "" }; /** Tensor storage as a string */ + std::string type{ "" }; /** Tensor storage type as a string */ +}; + +/** Tensor argument base class. + * A tensor is a multidimensional array used to store data. To access an element (or multiple elements) from a tensor, + * the following information are required: + * -# The data memory object. For example, the pointer to the array + * -# The tensor components, such as the size of each tensor dimension, or the number of elements in bytes contained in each dimension (also known as the "stride") + */ +class ITensorArgument +{ +public: + virtual ~ITensorArgument() = default; + /** Method to get the name of the tensor argument. + * + * @return the name of the tensor argument + */ + std::string name() const + { + return _basename; + } + /** Method to get the tensor info + * + * @return the @ref TensorInfo + */ + TensorInfo info() const + { + return _info; + } + +protected: + TensorInfo _info{}; // Tensor info + std::string _basename{ "" }; // Tensor name +}; + +/** Tensor component argument base class */ +class ITensorComponentArgument +{ +public: + virtual ~ITensorComponentArgument() = default; + /** Method to get the tensor component variable as a string + * + * @param[in] x The tensor component to query + * + * @return the tensor component variable as a @ref TileVariable + */ + virtual TileVariable component(TensorComponentType x) = 0; + /** Method to get all tensor components needed to access the data in the tensor + * + * The tensor components returned by this method must be all passed as kernel argument + * + * @return a vector containing all the tensor components as @ref TileVariable objects + */ + virtual std::vector<TileVariable> components() const = 0; +}; + +/** Tensor storage argument base class */ +class ITensorStorageArgument +{ +public: + virtual ~ITensorStorageArgument() = default; + /** Method to get the tensor storage as a string + * + * @param[in] x The tensor storage to query + * + * @return the tensor storage as a @ref TensorStorageVariable + */ + virtual TensorStorageVariable storage(TensorStorageType x) = 0; + /** Method to get all tensor storages needed to access the data in the tensor + * + * The tensor storages returned by this method must be all passed as kernel argument + * + * @return a vector containing all the tensor storages as @ref TensorStorageVariable objects + */ + virtual std::vector<TensorStorageVariable> storages() const = 0; +}; + +} // namespace ckw + +#endif // CKW_SRC_ITENSORARGUMENT_H diff --git a/compute_kernel_writer/src/TensorUtils.cpp b/compute_kernel_writer/src/TensorUtils.cpp index 4970de75a6..24836092d4 100644 --- a/compute_kernel_writer/src/TensorUtils.cpp +++ b/compute_kernel_writer/src/TensorUtils.cpp @@ -22,14 +22,14 @@ * SOFTWARE. */ +#include "src/TensorUtils.h" #include "ckw/Error.h" #include "ckw/TensorInfo.h" - -#include "src/TensorUtils.h" +#include "ckw/types/TensorComponentType.h" namespace ckw { -TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component) +TensorComponentType get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component) { switch(layout) { @@ -37,41 +37,41 @@ TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutCo switch(component) { case TensorDataLayoutComponent::C: - return TensorComponent::Dim0; + return TensorComponentType::Dim0; case TensorDataLayoutComponent::W: - return TensorComponent::Dim1; + return TensorComponentType::Dim1; case TensorDataLayoutComponent::H: - return TensorComponent::Dim2; + return TensorComponentType::Dim2; case TensorDataLayoutComponent::N: - return TensorComponent::Dim3; + return TensorComponentType::Dim3; default: COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC"); - return TensorComponent::Unknown; + return TensorComponentType::Unknown; } case TensorDataLayout::Ndhwc: switch(component) { case TensorDataLayoutComponent::C: - return TensorComponent::Dim0; + return TensorComponentType::Dim0; case TensorDataLayoutComponent::W: - return TensorComponent::Dim1; + return TensorComponentType::Dim1; case TensorDataLayoutComponent::H: - return TensorComponent::Dim2; + return TensorComponentType::Dim2; case TensorDataLayoutComponent::D: - return TensorComponent::Dim3; + return TensorComponentType::Dim3; case TensorDataLayoutComponent::N: - return TensorComponent::Dim4; + return TensorComponentType::Dim4; default: COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC"); - return TensorComponent::Unknown; + return TensorComponentType::Unknown; } default: COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout"); - return TensorComponent::Unknown; + return TensorComponentType::Unknown; } } -TensorComponent get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component) +TensorComponentType get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component) { switch(layout) { @@ -79,37 +79,37 @@ TensorComponent get_tensor_stride(TensorDataLayout layout, TensorDataLayoutCompo switch(component) { case TensorDataLayoutComponent::C: - return TensorComponent::Stride0; + return TensorComponentType::Stride0; case TensorDataLayoutComponent::W: - return TensorComponent::Stride1; + return TensorComponentType::Stride1; case TensorDataLayoutComponent::H: - return TensorComponent::Stride2; + return TensorComponentType::Stride2; case TensorDataLayoutComponent::N: - return TensorComponent::Stride3; + return TensorComponentType::Stride3; default: COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NHWC"); - return TensorComponent::Unknown; + return TensorComponentType::Unknown; } case TensorDataLayout::Ndhwc: switch(component) { case TensorDataLayoutComponent::C: - return TensorComponent::Stride0; + return TensorComponentType::Stride0; case TensorDataLayoutComponent::W: - return TensorComponent::Stride1; + return TensorComponentType::Stride1; case TensorDataLayoutComponent::H: - return TensorComponent::Stride2; + return TensorComponentType::Stride2; case TensorDataLayoutComponent::D: - return TensorComponent::Stride3; + return TensorComponentType::Stride3; case TensorDataLayoutComponent::N: - return TensorComponent::Stride4; + return TensorComponentType::Stride4; default: COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component for NDHWC"); - return TensorComponent::Unknown; + return TensorComponentType::Unknown; } default: COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor data layout"); - return TensorComponent::Unknown; + return TensorComponentType::Unknown; } } } // namespace ckw diff --git a/compute_kernel_writer/src/TensorUtils.h b/compute_kernel_writer/src/TensorUtils.h index 84eca084bb..bb0af5c0b9 100644 --- a/compute_kernel_writer/src/TensorUtils.h +++ b/compute_kernel_writer/src/TensorUtils.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef COMPUTE_KERNEL_WRITER_SRC_TENSORUTILS_H -#define COMPUTE_KERNEL_WRITER_SRC_TENSORUTILS_H +#ifndef CKW_SRC_TENSORUTILS_H +#define CKW_SRC_TENSORUTILS_H #include <cstdint> @@ -33,7 +33,7 @@ namespace ckw // Forward declarations enum class TensorDataLayout; enum class TensorDataLayoutComponent; -enum class TensorComponent : uint32_t; +enum class TensorComponentType : uint32_t; /** Get tensor dimension from a given data layout and data layout component * @@ -42,7 +42,7 @@ enum class TensorComponent : uint32_t; * * @return the @ref TensorComponent */ -TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component); +TensorComponentType get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutComponent component); /** Get tensor stride from a given data layout and data layout component * @@ -51,6 +51,7 @@ TensorComponent get_tensor_dimension(TensorDataLayout layout, TensorDataLayoutCo * * @return the @ref TensorComponent */ -TensorComponent get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component); +TensorComponentType get_tensor_stride(TensorDataLayout layout, TensorDataLayoutComponent component); } // namespace ckw -#endif /* COMPUTE_KERNEL_WRITER_SRC_TENSORUTILS_H */ + +#endif // CKW_SRC_TENSORUTILS_H diff --git a/compute_kernel_writer/src/cl/CLHelpers.cpp b/compute_kernel_writer/src/cl/CLHelpers.cpp index 5a3d0fab81..af8a8a07ac 100644 --- a/compute_kernel_writer/src/cl/CLHelpers.cpp +++ b/compute_kernel_writer/src/cl/CLHelpers.cpp @@ -21,10 +21,10 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#include "src/cl/CLHelpers.h" #include "ckw/Error.h" #include "ckw/types/DataType.h" - -#include "src/cl/CLHelpers.h" +#include "ckw/types/TensorStorageType.h" namespace ckw { @@ -120,4 +120,26 @@ int32_t width_to_cl_vector_size(int32_t width) return 0; } } + +std::string cl_get_variable_storagetype_as_string(TensorStorageType storage) +{ + std::string res; + switch(storage) + { + case TensorStorageType::BufferUint8Ptr: + res += "__global uchar*"; + break; + case TensorStorageType::Texture2dReadOnly: + res += "__read_only image2d_t"; + break; + case TensorStorageType::Texture2dWriteOnly: + res += "__write_only image2d_t"; + break; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported storage type"); + } + + return res; +} + } // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLHelpers.h b/compute_kernel_writer/src/cl/CLHelpers.h index a9a84e2187..d0ca488617 100644 --- a/compute_kernel_writer/src/cl/CLHelpers.h +++ b/compute_kernel_writer/src/cl/CLHelpers.h @@ -24,14 +24,15 @@ #ifndef CKW_SRC_CL_CLHELPERS_H #define CKW_SRC_CL_CLHELPERS_H -#include <string> #include <cstdint> +#include <string> /** OpenCL specific helper functions */ namespace ckw { // Forward declarations enum class DataType; +enum class TensorStorageType : uint32_t; /** Helper function to validate the vector length of OpenCL vector data types * @@ -58,6 +59,13 @@ std::string cl_get_variable_datatype_as_string(DataType dt, int32_t len); */ int32_t width_to_cl_vector_size(int32_t width); +/** Helper function to return the OpenCL storage type as a string from a @ref TensorStorage + * + * @param[in] storage Storage type + * + * @return the OpenCL storage type as a string + */ +std::string cl_get_variable_storagetype_as_string(TensorStorageType storage); } // namespace ckw #endif /* COMPUTE_KERNEL_WRITER_SRC_CL_CLHELPERS_H */ diff --git a/compute_kernel_writer/src/cl/CLTensorArgument.cpp b/compute_kernel_writer/src/cl/CLTensorArgument.cpp new file mode 100644 index 0000000000..ed1c5bd687 --- /dev/null +++ b/compute_kernel_writer/src/cl/CLTensorArgument.cpp @@ -0,0 +1,247 @@ +/* + * 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 "src/cl/CLTensorArgument.h" +#include "ckw/Error.h" +#include "src/cl/CLHelpers.h" +#include "src/types/TensorComponentType.h" + +#include <algorithm> +#include <vector> + +namespace ckw +{ +CLTensorArgument::CLTensorArgument(const std::string &name, const TensorInfo &info, bool return_dims_by_value) +{ + _return_dims_by_value = return_dims_by_value; + _basename = name; + _info = info; +} + +TileVariable CLTensorArgument::component(TensorComponentType x) +{ + if(_return_dims_by_value) + { + uint32_t component_type = static_cast<uint32_t>(x); + + const bool is_dimension = (component_type & static_cast<uint32_t>(TensorComponentBitmask::Dimension)) != 0; + const bool is_folded_dimensions = (component_type & static_cast<uint32_t>(TensorComponentBitmask::FoldedDimensions)) != 0; + + constexpr auto bitmask_all = static_cast<uint32_t>(TensorComponentIndexBitmask::All); + constexpr auto bitmask_index_0 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index0); +#ifdef COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED + constexpr auto bitmask_index_1 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index1); + constexpr auto bitmask_index_2 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index2); + constexpr auto bitmask_index_3 = static_cast<uint32_t>(TensorComponentIndexBitmask::Index3); +#endif // COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED + + // Make sure that the encoding of component type hasn't changed and each nibble is 4 bits apart. + CKW_ASSERT(bitmask_all == (bitmask_index_0 | bitmask_index_1 | bitmask_index_2 | bitmask_index_3)); + CKW_ASSERT(bitmask_index_0 == bitmask_index_1 >> 4); + CKW_ASSERT(bitmask_index_1 == bitmask_index_2 >> 4); + CKW_ASSERT(bitmask_index_2 == bitmask_index_3 >> 4); + + // If we have a dimension or folded dimensions, we can return the corresponding value if it is not dynamic (not equal to -1) + if(is_dimension == true || is_folded_dimensions == true) + { + component_type = component_type & bitmask_all; + + int32_t idx = 1; + for(int32_t i = 0; i < tensor_component_index_max_count; ++i) + { + uint32_t dim_idx = component_type & bitmask_index_0; + + if(dim_idx == 0) + { + // Stop at the first nibble containing 0 + break; + } + + // Subtract - 1. Please refer to the TensorComponentIndexBitmask documentation + dim_idx -= 1; + + // Get the dimension value + const int32_t dim_val = _info.shape()[dim_idx]; + + if(dim_val == kDynamicTensorDimensionValue) + { + // We cannot return the dimension by value if it is dynamic. + // Therefore, force the idx variable to kDynamicTensorDimensionValue and break the loop. + idx = kDynamicTensorDimensionValue; + break; + } + + idx *= dim_val; + + // Go to the next nibble + component_type >>= 4; + } + + if(idx != kDynamicTensorDimensionValue) + { + TileVariable t; + t.str = std::to_string(idx); + t.desc.dt = DataType::Uint32; + t.desc.len = 1; + return t; + } + } + } + + auto it = std::find(_components_used.begin(), _components_used.end(), x); + + // Add to the list of used components if not present yet + if(it == _components_used.end()) + { + _components_used.push_back(x); + } + + TileVariable t; + t.str = create_component_name(x); + t.desc.dt = DataType::Int32; + t.desc.len = 1; + return t; +} + +TensorStorageVariable CLTensorArgument::storage(TensorStorageType x) +{ + if(std::find(_storages_used.begin(), _storages_used.end(), x) == _storages_used.end()) + { + _storages_used.push_back(x); + } + + TensorStorageVariable t; + t.val = create_storage_name(x); + t.type = cl_get_variable_storagetype_as_string(x); + + return t; +} + +std::string CLTensorArgument::create_storage_name(TensorStorageType x) const +{ + std::string var_name = _basename; + + switch(x) + { + case TensorStorageType::BufferUint8Ptr: + var_name += "_ptr"; + break; + case TensorStorageType::Texture2dReadOnly: + case TensorStorageType::Texture2dWriteOnly: + var_name += "_img2d"; + break; + default: + CKW_ASSERT_FAILED_MSG("Unsupported tensor storage"); + return ""; + } + + return var_name; +} + +std::string CLTensorArgument::create_component_name(TensorComponentType x) const +{ + std::string var_name = _basename; + + switch(x) + { + case TensorComponentType::OffsetFirstElement: + var_name += "_offset_first_element"; + break; + case TensorComponentType::Stride0: + var_name += "_stride0"; + break; + case TensorComponentType::Stride1: + var_name += "_stride1"; + break; + case TensorComponentType::Stride2: + var_name += "_stride2"; + break; + case TensorComponentType::Stride3: + var_name += "_stride3"; + break; + case TensorComponentType::Stride4: + var_name += "_stride4"; + break; + case TensorComponentType::Dim0: + var_name += "_dim0"; + break; + case TensorComponentType::Dim1: + var_name += "_dim1"; + break; + case TensorComponentType::Dim2: + var_name += "_dim2"; + break; + case TensorComponentType::Dim3: + var_name += "_dim3"; + break; + case TensorComponentType::Dim4: + var_name += "_dim4"; + break; + case TensorComponentType::Dim1xDim2: + var_name += "_dim1xdim2"; + break; + case TensorComponentType::Dim2xDim3: + var_name += "_dim2xdim3"; + break; + case TensorComponentType::Dim1xDim2xDim3: + var_name += "_dim1xdim2xdim3"; + break; + default: + COMPUTE_KERNEL_WRITER_ERROR_ON_MSG("Unsupported tensor component"); + return ""; + } + + return var_name; +} + +std::vector<TensorStorageVariable> CLTensorArgument::storages() const +{ + std::vector<TensorStorageVariable> storages; + for(auto &val : _storages_used) + { + TensorStorageVariable t; + t.val = create_storage_name(val); + t.type = cl_get_variable_storagetype_as_string(val); + storages.push_back(t); + } + + return storages; +} + +std::vector<TileVariable> CLTensorArgument::components() const +{ + std::vector<TileVariable> components; + + for(auto &val : _components_used) + { + TileVariable t; + t.str = create_component_name(val); + t.desc.dt = DataType::Int32; + t.desc.len = 1; + components.push_back(t); + } + + return components; +} +} // namespace ckw diff --git a/compute_kernel_writer/src/cl/CLTensorArgument.h b/compute_kernel_writer/src/cl/CLTensorArgument.h new file mode 100644 index 0000000000..cd924846c5 --- /dev/null +++ b/compute_kernel_writer/src/cl/CLTensorArgument.h @@ -0,0 +1,70 @@ +/* + * 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_SRC_CL_CLTENSORARGUMENT_H +#define CKW_SRC_CL_CLTENSORARGUMENT_H + +#include "src/ITensorArgument.h" + +#include <string> +#include <vector> + +namespace ckw +{ +// Forward declarations +class TensorInfo; + +/** OpenCL specific tensor argument + * Internally, the object keeps track of the components and storages used to minimize the number + * of kernel arguments required. Therefore, if we create this object but we do not access any components + * or storages, the storages() and components() method will return an empty list. +*/ +class CLTensorArgument : public ITensorArgument, ITensorStorageArgument, ITensorComponentArgument +{ +public: + /** Constructor + * + * @param[in] name Tensor name + * @param[in] info Tensor info + * @param[in] return_dims_by_value Flag to return the dimensions by value whenever it is possible. + * True, if the dimensions should be returned as value instead as variable. + */ + CLTensorArgument(const std::string &name, const TensorInfo &info, bool return_dims_by_value); + + // Inherited method overridden + TensorStorageVariable storage(TensorStorageType x); + TileVariable component(TensorComponentType x); + std::vector<TensorStorageVariable> storages() const; + std::vector<TileVariable> components() const; + +private: + std::string create_storage_name(TensorStorageType x) const; + std::string create_component_name(TensorComponentType x) const; + + bool _return_dims_by_value{ false }; + std::vector<TensorStorageType> _storages_used{}; + std::vector<TensorComponentType> _components_used{}; +}; +} // namespace ckw + +#endif // CKW_SRC_CL_CLTENSORARGUMENT_H diff --git a/compute_kernel_writer/src/types/TensorComponentType.h b/compute_kernel_writer/src/types/TensorComponentType.h new file mode 100644 index 0000000000..03f4f4f5c8 --- /dev/null +++ b/compute_kernel_writer/src/types/TensorComponentType.h @@ -0,0 +1,78 @@ +/* + * 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_SRC_TYPES_TENSORCOMPONENTTYPE_H +#define CKW_SRC_TYPES_TENSORCOMPONENTTYPE_H + +#include <cstdint> + +namespace ckw +{ + +/** Compute Kernel Writer tensor component bitmask. + * + * The bitmask can be used to retrieve the info from @ref TensorComponent. + */ +enum class TensorComponentBitmask : uint32_t +{ + OffsetFirstElement = 0x01000000, // For example, OffsetFirstElement in TensorComponent + Stride = 0x02000000, // For example, stride0 in TensorComponent + Dimension = 0x04000000, // For example, Dim0 in TensorComponent + FoldedDimensions = 0x08000000, // For example, Dim0xDim1 in TensorComponent +}; + +/** Mask to retrieve the component index (for example, 1 for stride1, 2 for stride2, or 1 and 2 for Dim1xDim2). + * + * The 4 least significant half-bytes (nibbles) of the @ref TensorComponent are used to retrieve the specific component index. + * TensorComponent = | i7 | i6 | i5 | i4 | i3 | i2 | i1 | i0 |, where i7,...i0 are the nibbles + * of the TensorComponent hexadecimal number. i0, i1, i2 and i3 are reserved to the component index. + * + * In particular: + * + * -# i0: reserved to the first folded dimension component index + * -# i1: reserved to the second folded dimension component index + * -# i2: reserved to the third folded dimension component index + * -# i3: reserved to the fourth folded dimension component index + * + * Therefore, if there are no folded dimensions (dimensions and strides), only i0 is used. + * Instead, if there are two folded dimensions, only i0 and i1 are used. + * + * The component index is stored with the corresponding hexadecimal number + 1, + * hence the component index 0 is represented as 1, while the component index 3 is represented as 4. + */ +enum class TensorComponentIndexBitmask : uint32_t +{ + All = 0x0000ffff, // All nibbles reserved to the tensor component index + Index0 = 0x0000000f, // Folded dimension 0 + Index1 = 0x000000f0, // Folded dimension 1 + Index2 = 0x00000f00, // Folded dimension 2 + Index3 = 0x0000f000 // Folded dimension 3 +}; + +/** The maximum number of folded dimensions. */ +constexpr int tensor_component_index_max_count = 4; + +} // namespace ckw + +#endif // CKW_SRC_TYPES_TENSORCOMPONENTTYPE_H |