diff options
-rw-r--r-- | compute_kernel_writer/CMakeLists.txt | 1 | ||||
-rw-r--r-- | compute_kernel_writer/include/ckw/types/TensorSamplerTypes.h | 11 | ||||
-rw-r--r-- | compute_kernel_writer/src/ITile.h | 74 | ||||
-rw-r--r-- | compute_kernel_writer/src/Tensor3dMapper.cpp | 140 | ||||
-rw-r--r-- | compute_kernel_writer/src/Tensor3dMapper.h | 81 | ||||
-rw-r--r-- | compute_kernel_writer/src/cl/CLTile.h | 2 |
6 files changed, 265 insertions, 44 deletions
diff --git a/compute_kernel_writer/CMakeLists.txt b/compute_kernel_writer/CMakeLists.txt index 9a97563025..8f896ef35f 100644 --- a/compute_kernel_writer/CMakeLists.txt +++ b/compute_kernel_writer/CMakeLists.txt @@ -122,6 +122,7 @@ target_sources(ckw PRIVATE src/Helpers.cpp src/Kernel.cpp src/KernelWriter.cpp + src/Tensor3dMapper.cpp src/TensorInfo.cpp src/TensorOperand.cpp src/TensorSampler.cpp diff --git a/compute_kernel_writer/include/ckw/types/TensorSamplerTypes.h b/compute_kernel_writer/include/ckw/types/TensorSamplerTypes.h index 347536512e..3a9f4f5722 100644 --- a/compute_kernel_writer/include/ckw/types/TensorSamplerTypes.h +++ b/compute_kernel_writer/include/ckw/types/TensorSamplerTypes.h @@ -33,9 +33,9 @@ namespace ckw // This enum class defines how the dimensions of a 3d tensor is mapped into x,y and z coordianates. enum class TensorSamplerFormat : int32_t { - Unknown = 0, - D0_D1xD2_1 = 1, // Original dimensions 1 and 2 are collapsed onto y-axis - D0_D1_D2 = 2 // Original dimensions stays as they're defined. No collapsing. + Unknown = 0, + Dim0_Dim1xDim2_1 = 1, // Original dimensions 1 and 2 are collapsed onto y-axis + Dim0_Dim1_Dim2 = 2 // Original dimensions stays as they're defined. No collapsing. }; /** Tensor sampler address mode enum class for X dimension @@ -53,9 +53,8 @@ enum class TensorSamplerFormat : int32_t enum class TensorSamplerAddressModeX : int32_t { Unknown = 0, - None = 1, // The user guarantees that the coordinate is always in-bound - OverlappingMin = 2 // (FIXED shapes only) Reduce the load/store length when x == 0 (MIN). The load length will be width % original length - // Leftover elements can be handled using overlapping. This involves processing some of the elements in the array twice. + None = 1, + OverlappingMin = 2 }; /** diff --git a/compute_kernel_writer/src/ITile.h b/compute_kernel_writer/src/ITile.h index a54fd9b794..b5585abcdf 100644 --- a/compute_kernel_writer/src/ITile.h +++ b/compute_kernel_writer/src/ITile.h @@ -21,8 +21,8 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -#ifndef COMPUTE_KERNEL_WRITER_SRC_ITILE -#define COMPUTE_KERNEL_WRITER_SRC_ITILE +#ifndef CKW_SRC_ITILE +#define CKW_SRC_ITILE #include "ckw/TileInfo.h" @@ -48,40 +48,6 @@ struct TileVariable 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 - */ - virtual const std::string &name() const = 0; - - /** Method to get the tile info - * - * @return the @ref TileInfo - */ - virtual const TileInfo &info() const = 0; - - /** 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; -}; - /** Interface to provide support for scalar access for a Tile. */ class IScalarAccess @@ -130,6 +96,40 @@ public: */ virtual std::vector<int32_t> supported_vector_lengths() const = 0; }; + +/** Tile base class. + * A Tile is a collection of variables (either program variables or constants) used to express a 2D data. + */ +class ITile: public IScalarAccess +{ +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 + */ + virtual const std::string &name() const = 0; + + /** Method to get the tile info + * + * @return the @ref TileInfo + */ + virtual const TileInfo &info() const = 0; + + /** 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; +}; } // namespace ckw -#endif /* COMPUTE_KERNEL_WRITER_SRC_ITILE */ +#endif /* CKW_SRC_ITILE */ diff --git a/compute_kernel_writer/src/Tensor3dMapper.cpp b/compute_kernel_writer/src/Tensor3dMapper.cpp new file mode 100644 index 0000000000..60f4f57466 --- /dev/null +++ b/compute_kernel_writer/src/Tensor3dMapper.cpp @@ -0,0 +1,140 @@ +/* + * 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 "Tensor3dMapper.h" + +#include "ckw/Error.h" +#include "ckw/types/TensorSamplerTypes.h" +#include "src/ITensor.h" + + +namespace ckw +{ +Tensor3dMapper::Tensor3dMapper(ITensor *tensor, TensorSampler sampler) + : _tensor(tensor), _sampler(sampler) +{ +} + +std::string Tensor3dMapper::tensor_component_x() const +{ + const TensorSamplerFormat format = _sampler.format(); + switch(format) + { + case TensorSamplerFormat::Dim0_Dim1xDim2_1: + case TensorSamplerFormat::Dim0_Dim1_Dim2: + return _tensor->component(TensorComponentType::Dim0).scalar(0,0).str; + default: + CKW_THROW_MSG("Unsupported tensor format"); + return ""; + } +} + +std::string Tensor3dMapper::tensor_component_y() const +{ + const TensorSamplerFormat format = _sampler.format(); + switch(format) + { + case TensorSamplerFormat::Dim0_Dim1xDim2_1: + return _tensor->component(TensorComponentType::Dim1xDim2).scalar(0,0).str; + case TensorSamplerFormat::Dim0_Dim1_Dim2: + return _tensor->component(TensorComponentType::Dim1).scalar(0,0).str; + default: + CKW_THROW_MSG("Unsupported tensor format"); + return ""; + } +} + +std::string Tensor3dMapper::tensor_component_z() const +{ + const TensorSamplerFormat format = _sampler.format(); + switch(format) + { + case TensorSamplerFormat::Dim0_Dim1xDim2_1: + return "1"; + case TensorSamplerFormat::Dim0_Dim1_Dim2: + return _tensor->component(TensorComponentType::Dim2).scalar(0,0).str; + default: + CKW_THROW_MSG("Unsupported tensor format"); + return ""; + } +} + +std::string Tensor3dMapper::tensor_component_stride_x() const +{ + const TensorSamplerFormat format = _sampler.format(); + switch(format) + { + case TensorSamplerFormat::Dim0_Dim1xDim2_1: + case TensorSamplerFormat::Dim0_Dim1_Dim2: + return _tensor->component(TensorComponentType::Stride0).scalar(0,0).str; + default: + CKW_THROW_MSG("Unsupported tensor format"); + return ""; + } +} + +std::string Tensor3dMapper::tensor_component_stride_y() const +{ + const TensorSamplerFormat format = _sampler.format(); + switch(format) + { + case TensorSamplerFormat::Dim0_Dim1xDim2_1: + case TensorSamplerFormat::Dim0_Dim1_Dim2: + return _tensor->component(TensorComponentType::Stride1).scalar(0,0).str; + default: + CKW_THROW_MSG("Unsupported tensor format"); + return ""; + } +} + +std::string Tensor3dMapper::tensor_component_stride_z() const +{ + const TensorSamplerFormat format = _sampler.format(); + switch(format) + { + case TensorSamplerFormat::Dim0_Dim1xDim2_1: + return "0"; + case TensorSamplerFormat::Dim0_Dim1_Dim2: + return _tensor->component(TensorComponentType::Stride2).scalar(0,0).str; + default: + CKW_THROW_MSG("Unsupported tensor format"); + return ""; + } +} + +std::string Tensor3dMapper::tensor_component_stride_batch() const +{ + return _tensor->component(TensorComponentType::Stride3).scalar(0,0).str; +} + +TensorSampler Tensor3dMapper::sampler() const +{ + return _sampler; +} + +ITensor *Tensor3dMapper::tensor() const +{ + return _tensor; +} +} // namespace ckw
\ No newline at end of file diff --git a/compute_kernel_writer/src/Tensor3dMapper.h b/compute_kernel_writer/src/Tensor3dMapper.h new file mode 100644 index 0000000000..1c3856addd --- /dev/null +++ b/compute_kernel_writer/src/Tensor3dMapper.h @@ -0,0 +1,81 @@ +/* + * 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_TENSOR3DMAPPER_H +#define CKW_SRC_TENSOR3DMAPPER_H + +#include "ckw/TensorSampler.h" + +#include <string> +namespace ckw +{ + +class ITensor; +class TensorSampler; + +/** This internal-only class is responsible to map an Nd tensor to a 3d tensor with the + * help of TensorSampler object. The aim of the dimensionality reduction is to reduce + * the address calculation to: + * x + y * stride_y + z * stride_z + offset, where offset is determined by the batch. + */ +class Tensor3dMapper +{ +public: + /** Constructor */ + Tensor3dMapper(ITensor *tensor, TensorSampler sampler); + + /** Get dimension x as string */ + std::string tensor_component_x() const; + + /** Get dimension y as string */ + std::string tensor_component_y() const; + + /** Get dimension z as string */ + std::string tensor_component_z() const; + + /** Get stride for dimension x as string */ + std::string tensor_component_stride_x() const; + + /** Get stride for dimension y as string */ + std::string tensor_component_stride_y() const; + + /** Get stride for dimension z as string */ + std::string tensor_component_stride_z() const; + + /** Get stride for batch dimension as string */ + std::string tensor_component_stride_batch() const; + + /** Get the tensor sampler */ + TensorSampler sampler() const; + + /** Get the associated tensor */ + ITensor *tensor() const; + +private: + ITensor *_tensor; + TensorSampler _sampler; +}; +} // namespace ckw + +#endif /* CKW_SRC_TENSOR3DMAPPER_H */ diff --git a/compute_kernel_writer/src/cl/CLTile.h b/compute_kernel_writer/src/cl/CLTile.h index 46af4de364..1fb0fc9dbe 100644 --- a/compute_kernel_writer/src/cl/CLTile.h +++ b/compute_kernel_writer/src/cl/CLTile.h @@ -33,7 +33,7 @@ namespace ckw class TileInfo; /** OpenCL specific tile */ -class CLTile : public ITile, public IVectorAccess, public IScalarAccess +class CLTile : public ITile, public IVectorAccess { public: /** Initialize a new instance of @ref CLTile class for variable tile. |