diff options
Diffstat (limited to 'compute_kernel_writer')
32 files changed, 795 insertions, 280 deletions
diff --git a/compute_kernel_writer/CMakeLists.txt b/compute_kernel_writer/CMakeLists.txt index 2c770e4efb..186ab328f5 100644 --- a/compute_kernel_writer/CMakeLists.txt +++ b/compute_kernel_writer/CMakeLists.txt @@ -43,7 +43,6 @@ message(STATUS "${CMAKE_PROJECT_NAME} ${CMAKE_PROJECT_VERSION}") option(CKW_ENABLE_OPENCL "Enable OpenCL code generation" OFF) option(CKW_ENABLE_ASSERTS "Enable assertions. Always enabled in Debug builds" OFF) option(CKW_BUILD_TESTING "Build the Compute Kernel Writer validation test suite" OFF) -option(CKW_BUILD_EXAMPLES "Build the Compute Kernel Writer examples" OFF) option(CKW_CCACHE "Use compiler cache for faster recompilation" OFF) #--------------------------------------------------------------------- @@ -122,17 +121,6 @@ target_sources(ckw PRIVATE src/TensorInfo.cpp src/TensorUtils.cpp src/TileInfo.cpp - - src/Kernel.cpp - src/KernelWriter.cpp - src/OperandBase.cpp - src/TileOperand.cpp - src/TensorOperand.cpp - src/TensorTileSampler.cpp - - src/acl/AclKernelWriter.cpp - src/acl/AclScopedKernelWriter.cpp - src/acl/AclComponentArgument.cpp ) if(CKW_ENABLE_OPENCL) @@ -147,7 +135,7 @@ endif() target_include_directories(ckw PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include PRIVATE ${CMAKE_CURRENT_LIST_DIR} - ) +) #--------------------------------------------------------------------- # Validation tests @@ -172,16 +160,10 @@ if(CKW_BUILD_TESTING) endif() #--------------------------------------------------------------------- -# Examples - -function(add_ckw_example name) - add_executable(${name} ${ARGN}) - target_link_libraries(${name} PUBLIC ckw) -endfunction(add_ckw_example) +# Prototype -if(CKW_BUILD_EXAMPLES) - add_ckw_example(ckw_example_add_exp_store - examples/add_exp_store.cpp) +if(CKW_BUILD_PROTOTYPE) + add_subdirectory(prototype) endif() #--------------------------------------------------------------------- diff --git a/compute_kernel_writer/README.md b/compute_kernel_writer/README.md index 650eae2b48..8a24fe20ec 100644 --- a/compute_kernel_writer/README.md +++ b/compute_kernel_writer/README.md @@ -82,6 +82,5 @@ This project can be configured with the following build options. Enable options | CKW_ENABLE_OPENCL | Enable OpenCL code generation. | | CKW_ENABLE_ASSERTS | Enable assertions. Always enabled for Debug builds. | | CKW_BUILD_TESTING | Build the validation test suite. | -| CKW_BUILD_EXAMPLES | Build the examples. | | CKW_CCACHE | Use compiler cache for faster recompilation. | | CMAKE_TOOLCHAIN_FILE | When cross-compiling, set this variable to the path of the CMake toolchain file. | diff --git a/compute_kernel_writer/include/ckw/Error.h b/compute_kernel_writer/include/ckw/Error.h index 8c4853722b..996893823e 100644 --- a/compute_kernel_writer/include/ckw/Error.h +++ b/compute_kernel_writer/include/ckw/Error.h @@ -21,12 +21,11 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +#ifndef COMPUTE_KERNEL_WRITER_INCLUDE_CKW_ERROR_H +#define COMPUTE_KERNEL_WRITER_INCLUDE_CKW_ERROR_H -#ifndef CKW_INCLUDE_CKW_ERROR_H -#define CKW_INCLUDE_CKW_ERROR_H - -#include <stdexcept> #include <string> +#include <stdexcept> namespace ckw { @@ -45,59 +44,16 @@ std::string create_error_msg(const std::string &file, const std::string &func, c * * @param[in] msg Message to display. */ -#define COMPUTE_KERNEL_WRITER_ERROR_ON_MSG(msg) \ - do \ - { \ - const std::string arg0(__FILE__); \ - const std::string arg1(__func__); \ - const std::string arg2(std::to_string(__LINE__)); \ - const std::string arg3(msg); \ +#define COMPUTE_KERNEL_WRITER_ERROR_ON_MSG(msg) \ + do \ + { \ + const std::string arg0(__FILE__); \ + const std::string arg1(__func__); \ + const std::string arg2(std::to_string(__LINE__)); \ + const std::string arg3(msg); \ std::runtime_error(create_error_msg(arg0, arg1, arg2, arg3)); \ } while(false) -/** If the condition is not met, throw an std::runtime_error with the specified message. - * - * @param[in] cond The condition that is expected to be true. - * @param[in] msg The error message when the condition is not met. - */ -#define CKW_ASSERT_MSG(cond, msg) \ - do \ - { \ - if(!(cond)) \ - { \ - throw ::std::runtime_error(msg); \ - } \ - } while(false) - -/** If the condition is not met, throw an std::runtime_error. - * - * @param[in] cond The condition that is expected to be true. - */ -#define CKW_ASSERT(cond) CKW_ASSERT_MSG(cond, #cond) - -/** If the precondition is met but the consequence is not met, throw an std::runtime_error. - * - * @param[in] precond The condition if is met requires the consequence must also be met. - * @param[in] cond The condition that is expected to be true if the precondition is true. - */ -#define CKW_ASSERT_IF(precond, cond) \ - CKW_ASSERT_MSG(!(precond) || ((precond) && (cond)), #precond " |-> " #cond) - -/** Mark the variables as unused. - * - * @param[in] ... Variables which are unused. - */ -#define CKW_UNUSED(...) ::ckw::ignore_unused(__VA_ARGS__) // NOLINT - -/** Mark the variables as unused. - * - * @param[in] ... Variables which are unused. - */ -template <typename... T> -inline void ignore_unused(T &&...) -{ -} - } // namespace ckw -#endif // CKW_INCLUDE_CKW_ERROR_H +#endif /* COMPUTE_KERNEL_WRITER_INCLUDE_CKW_ERROR_H */ diff --git a/compute_kernel_writer/include/ckw/Types.h b/compute_kernel_writer/include/ckw/Types.h index 5516718e54..c9f80b65e0 100644 --- a/compute_kernel_writer/include/ckw/Types.h +++ b/compute_kernel_writer/include/ckw/Types.h @@ -21,120 +21,25 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ - -#ifndef CKW_INCLUDE_CKW_TYPES_H -#define CKW_INCLUDE_CKW_TYPES_H - -#include <array> -#include <cstdint> +#ifndef COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TYPES_H +#define COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TYPES_H namespace ckw { - /** Compute Kernel Writer data types. This data type is used by the code variables and tensor arguments. */ enum class DataType { - Unknown = 0x00, - Fp32 = 0x11, - Fp16 = 0x12, - Int32 = 0x21, - Int16 = 0x22, - Int8 = 0x24, - Uint32 = 0x31, - Uint16 = 0x32, - Uint8 = 0x34, - Bool = 0x41 -}; - -enum class GpuTargetLanguage -{ Unknown, - OpenCL -}; - -/* Binary operations -*/ -enum class BinaryOp : int32_t -{ - // Elementwise - Add = 0x0000, // + - Sub = 0x0001, // - - Mul = 0x0002, // * - Div = 0x0003, // / - Mod = 0x0004, // % - // Relational - Equal = 0x1000, // == - Less = 0x1001, // < - LessEqual = 0x1002, // <= - Greater = 0x1003, // > - GreaterEqual = 0x1004, // >= - // Algebra - MatMul_Nt_Nt = 0x2000, // X - MatMul_Nt_T = 0x2001, // X - MatMul_T_Nt = 0x2002, // X - MatMul_T_T = 0x2003, // X - Dot = 0x2004, // . - // Logical - LogicalAnd = 0x3000, // && - LogicalOr = 0x3001, // || - LogicalNot = 0x3002 // ! -}; - -enum class AssignmentOp : int32_t -{ - // Unary - Increment = 0x0000, // += - Decrement = 0x0001, // -= -}; - -enum class ScalarUnaryFunction : int32_t -{ - Exp, -}; - -enum class TensorSamplerFormat : int32_t -{ - Unknown = 0, - C_WH_1 = 1, - C_W_H = 2 -}; - -enum class TensorSamplerAddressModeX : int32_t -{ - Unknown = 0, - None = 1, // The user guarantees that the X 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. + Fp32, + Fp16, + Int32, + Int16, + Int8, + Uint32, + Uint16, + Uint8, + Bool }; - -enum class TensorSamplerAddressModeY : int32_t -{ - Unknown = 0, - None = 1, // The user guarantees that the Y 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 - Skip = 3, // Skip the read/write - SkipMinEdgeOnly = 4, // Skip greater than or equal to max only. The user guarantees that the Y coordinate is always >= 0 - SkipMaxEdgeOnly = 5, // Skip less than 0 only - ClampToNearest = 6, // Clamp the coordinate to nearest edge (0 or max value allowed on Y) - ClampToMinEdgeOnly = 7, // Clamp the negative coordinate to 0 only. Therefore, we expect Y to be always < MAX - ClampToMaxEdgeOnly = 8, // Clamp the coordinate to the max value allowed on Y only. We expect Y to be always >= 0 - ClampToBorder = 9, // Clamp to border which always has 0 value - ClampToBorderMinEdgeOnly = 10, - ClampToBorderMaxEdgeOnly = 11 -}; - -enum class TensorSamplerAddressModeZ : int32_t -{ - Unknown = 0, - None = 1, // The user guarantees that the Y coordinate is always in-bound - Skip = 3, // Skip the read/write - SkipMinEdgeOnly = 4, // Skip greater than or equal to max only. The user guarantees that the Y coordinate is always >= 0 - SkipMaxEdgeOnly = 5, // Skip less than 0 only - ClampToNearest = 6, // Clamp the coordinate to nearest edge (0 or max value allowed on Y) - ClampToMinEdgeOnly = 7, // Clamp the negative coordinate to 0 only. Therefore, we expect Y to be always < MAX - ClampToMaxEdgeOnly = 8, // Clamp the coordinate to the max value allowed on Y only. We expect Y to be always >= 0 -}; - } // namespace ckw -#endif // CKW_INCLUDE_CKW_TYPES_H +#endif /* COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TYPES_H */ diff --git a/compute_kernel_writer/prototype/CMakeLists.txt b/compute_kernel_writer/prototype/CMakeLists.txt new file mode 100644 index 0000000000..84436a95d6 --- /dev/null +++ b/compute_kernel_writer/prototype/CMakeLists.txt @@ -0,0 +1,72 @@ +# Copyright (c) 2023 Arm Limited. +# +# SPDX-License-Identifier: MIT +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +cmake_minimum_required(VERSION 3.14 FATAL_ERROR) + +#--------------------------------------------------------------------- +# Prototype + +add_library(ckw_prototype + src/TileInfo.cpp + src/TensorInfo.cpp + src/Kernel.cpp + src/KernelWriter.cpp + src/OperandBase.cpp + src/TileOperand.cpp + src/TensorOperand.cpp + src/TensorTileSampler.cpp +) + +target_compile_options(ckw_prototype + PUBLIC + ${CKW_CXX_FLAGS} + "$<$<CXX_COMPILER_ID:GNU>:${GNU_WARNINGS}>" + "$<$<CONFIG:Debug>:${CKW_ASSERTS_OPTS}>" + "$<$<BOOL:${CKW_ASSERTS}>:${CKW_ASSERTS_OPTS}>" + ${CMAKE_CXX_FLAGS} +) + +target_compile_definitions(ckw_prototype PUBLIC + $<$<CONFIG:Debug>:COMPUTE_KERNEL_WRITER_DEBUG_ENABLED> + $<$<CONFIG:Debug>:COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED> + $<$<BOOL:${CKW_ASSERTS}>:COMPUTE_KERNEL_WRITER_ASSERTS_ENABLED> + $<$<BOOL:${CKW_ENABLE_OPENCL}>:COMPUTE_KERNEL_WRITER_OPENCL_ENABLED> +) + +target_include_directories(ckw_prototype + PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include + PRIVATE ${CMAKE_CURRENT_LIST_DIR} +) + +#--------------------------------------------------------------------- +# Examples + +add_library(ckw_prototype_examples_common + examples/common/ExampleKernelWriter.cpp + examples/common/ExampleScopedKernelWriter.cpp + examples/common/ExampleComponentArgument.cpp +) + +target_link_libraries(ckw_prototype_examples_common PUBLIC ckw_prototype) + +add_executable(ckw_prototype_examples_add_exp_store examples/add_exp_store.cpp) +target_link_libraries(ckw_prototype_examples_add_exp_store PUBLIC ckw_prototype_examples_common) diff --git a/compute_kernel_writer/examples/add_exp_store.cpp b/compute_kernel_writer/prototype/examples/add_exp_store.cpp index ab2f3e15fb..9ee21957f1 100644 --- a/compute_kernel_writer/examples/add_exp_store.cpp +++ b/compute_kernel_writer/prototype/examples/add_exp_store.cpp @@ -29,16 +29,16 @@ #include "ckw/TileOperand.h" #include "ckw/Types.h" -#include "acl/AclComponentArgument.h" -#include "acl/AclKernelWriter.h" -#include "acl/AclScopedKernelWriter.h" +#include "common/ExampleComponentArgument.h" +#include "common/ExampleKernelWriter.h" +#include "common/ExampleScopedKernelWriter.h" #include <iostream> #include <vector> using namespace ckw; -TensorTileSampler create_simple_sampler(AclScopedKernelWriter writer) +TensorTileSampler create_simple_sampler(ExampleScopedKernelWriter writer) { TensorTileSampler sampler; @@ -57,8 +57,8 @@ TensorTileSampler create_simple_sampler(AclScopedKernelWriter writer) sampler.x(gid_0); sampler.y(gid_1); - sampler.z(gid_2); - sampler.b(const_0); + sampler.z(const_0); + sampler.b(gid_2); sampler.width(n0); sampler.height(m0); @@ -71,7 +71,7 @@ TensorTileSampler create_simple_sampler(AclScopedKernelWriter writer) return sampler; } -void op_binary_elementwise(AclScopedKernelWriter writer, std::vector<AclComponentArgument *> operands) +void op_binary_elementwise(ExampleScopedKernelWriter writer, std::vector<ExampleComponentArgument *> operands) { auto lhs = operands.at(0); auto rhs = operands.at(1); @@ -113,7 +113,7 @@ void op_binary_elementwise(AclScopedKernelWriter writer, std::vector<AclComponen writer->op_binary_expression(dst_tile, lhs_tile, rhs_tile, BinaryOp::Add); } -void op_exp(AclScopedKernelWriter writer, std::vector<AclComponentArgument *> operands) +void op_exp(ExampleScopedKernelWriter writer, std::vector<ExampleComponentArgument *> operands) { auto src = operands.at(0); auto dst = operands.at(1); @@ -141,7 +141,7 @@ void op_exp(AclScopedKernelWriter writer, std::vector<AclComponentArgument *> op writer->op_scalar_function(dst_tile, src_tile, ScalarUnaryFunction::Exp); } -void op_store(AclScopedKernelWriter writer, std::vector<AclComponentArgument *> operands) +void op_store(ExampleScopedKernelWriter writer, std::vector<ExampleComponentArgument *> operands) { auto src = operands.at(0); auto dst = operands.at(1); @@ -156,19 +156,19 @@ void op_store(AclScopedKernelWriter writer, std::vector<AclComponentArgument *> int main() { Kernel kernel("example", GpuTargetLanguage::OpenCL); - AclKernelWriter root_writer(kernel); + ExampleKernelWriter root_writer(kernel); - AclScopedKernelWriter writer(&root_writer); + ExampleScopedKernelWriter writer(&root_writer); const TensorInfo src0_info(DataType::Fp32, TensorShape({ 3, 10, 20, 1, 1 }), TensorDataLayout::Nhwc, 0); const TensorInfo src1_info(DataType::Fp32, TensorShape({ 3, 10, 20, 1, 1 }), TensorDataLayout::Nhwc, 1); const TensorInfo dst_info(DataType::Fp32, TensorShape({ 3, 10, 20, 1, 1 }), TensorDataLayout::Nhwc, 2); - AclComponentArgument src0(writer->create_tensor_argument("src0", src0_info)); - AclComponentArgument src1(writer->create_tensor_argument("src1", src1_info)); - AclComponentArgument dst(writer->create_tensor_argument("dst", dst_info)); + ExampleComponentArgument src0(writer->create_tensor_argument("src0", src0_info)); + ExampleComponentArgument src1(writer->create_tensor_argument("src1", src1_info)); + ExampleComponentArgument dst(writer->create_tensor_argument("dst", dst_info)); - AclComponentArgument ans; + ExampleComponentArgument ans; op_binary_elementwise(writer, { &src0, &src1, &ans }); op_exp(writer, { &ans, &ans }); diff --git a/compute_kernel_writer/src/acl/AclComponentArgument.cpp b/compute_kernel_writer/prototype/examples/common/ExampleComponentArgument.cpp index 5cb909021e..da5156ca8c 100644 --- a/compute_kernel_writer/src/acl/AclComponentArgument.cpp +++ b/compute_kernel_writer/prototype/examples/common/ExampleComponentArgument.cpp @@ -22,19 +22,19 @@ * SOFTWARE. */ -#include "acl/AclComponentArgument.h" +#include "ExampleComponentArgument.h" #include "ckw/Error.h" -AclComponentArgument::AclComponentArgument() +ExampleComponentArgument::ExampleComponentArgument() { } -AclComponentArgument::AclComponentArgument(ckw::TensorOperand &tensor) +ExampleComponentArgument::ExampleComponentArgument(ckw::TensorOperand &tensor) : _tensor(&tensor) { } -AclComponentArgument &AclComponentArgument::init_virtual_tensor(ckw::TileOperand &tile, const ckw::TensorTileSampler &tile_sampler) +ExampleComponentArgument &ExampleComponentArgument::init_virtual_tensor(ckw::TileOperand &tile, const ckw::TensorTileSampler &tile_sampler) { CKW_ASSERT(_tile == nullptr); @@ -44,52 +44,52 @@ AclComponentArgument &AclComponentArgument::init_virtual_tensor(ckw::TileOperand return *this; } -bool AclComponentArgument::has_tensor() const +bool ExampleComponentArgument::has_tensor() const { return _tensor != nullptr; } -ckw::TensorOperand &AclComponentArgument::tensor() +ckw::TensorOperand &ExampleComponentArgument::tensor() { CKW_ASSERT(_tensor != nullptr); return *_tensor; } -const ckw::TensorOperand &AclComponentArgument::tensor() const +const ckw::TensorOperand &ExampleComponentArgument::tensor() const { CKW_ASSERT(_tensor != nullptr); return *_tensor; } -bool AclComponentArgument::has_tile() const +bool ExampleComponentArgument::has_tile() const { return _tile != nullptr; } -ckw::TileOperand &AclComponentArgument::tile() +ckw::TileOperand &ExampleComponentArgument::tile() { CKW_ASSERT(_tile != nullptr); return *_tile; } -const ckw::TileOperand &AclComponentArgument::tile() const +const ckw::TileOperand &ExampleComponentArgument::tile() const { CKW_ASSERT(_tile != nullptr); return *_tile; } -ckw::TensorTileSampler &AclComponentArgument::tile_sampler() +ckw::TensorTileSampler &ExampleComponentArgument::tile_sampler() { CKW_ASSERT(_tile != nullptr); return _tile_sampler; } -const ckw::TensorTileSampler &AclComponentArgument::tile_sampler() const +const ckw::TensorTileSampler &ExampleComponentArgument::tile_sampler() const { CKW_ASSERT(_tile != nullptr); diff --git a/compute_kernel_writer/include/acl/AclComponentArgument.h b/compute_kernel_writer/prototype/examples/common/ExampleComponentArgument.h index 485b7a30bc..2de9042691 100644 --- a/compute_kernel_writer/include/acl/AclComponentArgument.h +++ b/compute_kernel_writer/prototype/examples/common/ExampleComponentArgument.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_ACL_ACLCOMPONENTARGUMENT_H -#define CKW_INCLUDE_ACL_ACLCOMPONENTARGUMENT_H +#ifndef CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLECOMPONENTARGUMENT_H +#define CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLECOMPONENTARGUMENT_H #include "ckw/TensorTileSampler.h" @@ -34,27 +34,27 @@ class TileOperand; } // namespace ckw /** The argument of a dynamic fusion component which can be either user tensor or virtual tensor. */ -class AclComponentArgument +class ExampleComponentArgument { public: - /** Initialize a new instance of @ref AclComponentArgument class for empty virtual tensor. */ - AclComponentArgument(); + /** Initialize a new instance of @ref ExampleComponentArgument class for empty virtual tensor. */ + ExampleComponentArgument(); - /** Initialize a new instance of @ref AclComponentArgument class for user tensor. + /** Initialize a new instance of @ref ExampleComponentArgument class for user tensor. * * @param[in] tensor The user tensor. */ - explicit AclComponentArgument(ckw::TensorOperand &tensor); + explicit ExampleComponentArgument(ckw::TensorOperand &tensor); /** Set virtual tensor information (tile, sampler) for the argument. * * If the component is a user tensor, it can be treated as virtual tensor as well - * and won't be loaded again using @ref AclKernelWriter::op_load_once method. + * and won't be loaded again using @ref ExampleKernelWriter::op_load_once method. * * @param[in] tile The tile that has been loaded. * @param[in] sampler The tensor sampling information that has been used to load the tile. */ - AclComponentArgument &init_virtual_tensor(ckw::TileOperand &tile, const ckw::TensorTileSampler &sampler); + ExampleComponentArgument &init_virtual_tensor(ckw::TileOperand &tile, const ckw::TensorTileSampler &sampler); /** Get whether the argument is a user tensor. */ bool has_tensor() const; @@ -108,4 +108,4 @@ private: ckw::TensorTileSampler _tile_sampler{}; }; -#endif // CKW_INCLUDE_ACL_ACLCOMPONENTARGUMENT_H +#endif // CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLECOMPONENTARGUMENT_H diff --git a/compute_kernel_writer/src/acl/AclKernelWriter.cpp b/compute_kernel_writer/prototype/examples/common/ExampleKernelWriter.cpp index a44e798c61..2c11ae36e5 100644 --- a/compute_kernel_writer/src/acl/AclKernelWriter.cpp +++ b/compute_kernel_writer/prototype/examples/common/ExampleKernelWriter.cpp @@ -22,17 +22,17 @@ * SOFTWARE. */ -#include "acl/AclKernelWriter.h" -#include "acl/AclComponentArgument.h" +#include "ExampleKernelWriter.h" +#include "ExampleComponentArgument.h" #include "ckw/Error.h" #include "ckw/TileInfo.h" -AclKernelWriter::AclKernelWriter(ckw::Kernel &kernel) +ExampleKernelWriter::ExampleKernelWriter(ckw::Kernel &kernel) : KernelWriter(kernel) { } -void AclKernelWriter::op_load_once(AclComponentArgument *tensor_or_tile, const ckw::TensorTileSampler &sampler) +void ExampleKernelWriter::op_load_once(ExampleComponentArgument *tensor_or_tile, const ckw::TensorTileSampler &sampler) { if(!tensor_or_tile->has_tile()) { diff --git a/compute_kernel_writer/include/acl/AclKernelWriter.h b/compute_kernel_writer/prototype/examples/common/ExampleKernelWriter.h index 27b7add6ef..1528c3d933 100644 --- a/compute_kernel_writer/include/acl/AclKernelWriter.h +++ b/compute_kernel_writer/prototype/examples/common/ExampleKernelWriter.h @@ -22,13 +22,13 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_ACL_ACLKERNELWRITER_H -#define CKW_INCLUDE_ACL_ACLKERNELWRITER_H +#ifndef CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLEKERNELWRITER_H +#define CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLEKERNELWRITER_H #include "ckw/KernelWriter.h" #include "ckw/TensorTileSampler.h" -class AclComponentArgument; +class ExampleComponentArgument; namespace ckw { @@ -36,21 +36,21 @@ class Kernel; } // namespace ckw /** Extended implementation of kernel writer for dynamic fusion. */ -class AclKernelWriter : public ckw::KernelWriter +class ExampleKernelWriter : public ckw::KernelWriter { public: - /** Initialize a new instance of @ref AclKernelWriter class. + /** Initialize a new instance of @ref ExampleKernelWriter class. * * @param[in] kernel The kernel to be generated. */ - explicit AclKernelWriter(ckw::Kernel &kernel); + explicit ExampleKernelWriter(ckw::Kernel &kernel); /** Load the user tensor to the tile in the same component argument if it hasn't been loaded. * * @param[in] tensor_or_tile The component argument that is either a user tensor or a virtual tensor. * @param[in] sampler The tensor sampling information to load the tile. */ - void op_load_once(AclComponentArgument *tensor_or_tile, const ckw::TensorTileSampler &sampler); + void op_load_once(ExampleComponentArgument *tensor_or_tile, const ckw::TensorTileSampler &sampler); }; -#endif // CKW_INCLUDE_ACL_ACLKERNELWRITER_H +#endif // CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLEKERNELWRITER_H diff --git a/compute_kernel_writer/src/acl/AclScopedKernelWriter.cpp b/compute_kernel_writer/prototype/examples/common/ExampleScopedKernelWriter.cpp index 2a73d47592..7c44fa8749 100644 --- a/compute_kernel_writer/src/acl/AclScopedKernelWriter.cpp +++ b/compute_kernel_writer/prototype/examples/common/ExampleScopedKernelWriter.cpp @@ -22,37 +22,37 @@ * SOFTWARE. */ -#include "acl/AclScopedKernelWriter.h" -#include "acl/AclKernelWriter.h" +#include "ExampleScopedKernelWriter.h" +#include "ExampleKernelWriter.h" -AclScopedKernelWriter::AclScopedKernelWriter(AclKernelWriter *writer) +ExampleScopedKernelWriter::ExampleScopedKernelWriter(ExampleKernelWriter *writer) : _writer(writer), _parent_id_space(writer->id_space()) { _writer->next_id_space(); } -AclScopedKernelWriter::AclScopedKernelWriter(const AclScopedKernelWriter &other) +ExampleScopedKernelWriter::ExampleScopedKernelWriter(const ExampleScopedKernelWriter &other) : _writer(other._writer), _parent_id_space(other._writer->id_space()) { _writer->next_id_space(); } -AclKernelWriter *AclScopedKernelWriter::operator->() +ExampleKernelWriter *ExampleScopedKernelWriter::operator->() { return _writer; } -const AclKernelWriter *AclScopedKernelWriter::operator->() const +const ExampleKernelWriter *ExampleScopedKernelWriter::operator->() const { return _writer; } -AclKernelWriter *AclScopedKernelWriter::writer() +ExampleKernelWriter *ExampleScopedKernelWriter::writer() { return _writer; } -const AclKernelWriter *AclScopedKernelWriter::writer() const +const ExampleKernelWriter *ExampleScopedKernelWriter::writer() const { return _writer; } diff --git a/compute_kernel_writer/include/acl/AclScopedKernelWriter.h b/compute_kernel_writer/prototype/examples/common/ExampleScopedKernelWriter.h index 6cb957bfb5..1aa0242c51 100644 --- a/compute_kernel_writer/include/acl/AclScopedKernelWriter.h +++ b/compute_kernel_writer/prototype/examples/common/ExampleScopedKernelWriter.h @@ -22,41 +22,41 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_ACL_ACLSCOPEDKERNELWRITER_H -#define CKW_INCLUDE_ACL_ACLSCOPEDKERNELWRITER_H +#ifndef CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLESCOPEDKERNELWRITER_H +#define CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLESCOPEDKERNELWRITER_H #include <cstdint> -class AclKernelWriter; +class ExampleKernelWriter; /** Helper to automatically manage kernel writer ID space. */ -class AclScopedKernelWriter +class ExampleScopedKernelWriter { public: - /** Initialize a new instance of @ref AclScopedKernelWriter class. */ - explicit AclScopedKernelWriter(AclKernelWriter *writer); + /** Initialize a new instance of @ref ExampleScopedKernelWriter class. */ + explicit ExampleScopedKernelWriter(ExampleKernelWriter *writer); /** Create a new scope from the specified scoped kernel writer. */ - AclScopedKernelWriter(const AclScopedKernelWriter &other); + ExampleScopedKernelWriter(const ExampleScopedKernelWriter &other); /** Assignment is disallowed. */ - AclScopedKernelWriter &operator=(const AclScopedKernelWriter &) = delete; + ExampleScopedKernelWriter &operator=(const ExampleScopedKernelWriter &) = delete; /** Access the underlying kernel writer. */ - AclKernelWriter *operator->(); + ExampleKernelWriter *operator->(); /** Access the underlying kernel writer. */ - const AclKernelWriter *operator->() const; + const ExampleKernelWriter *operator->() const; /** Get the kernel writer. */ - AclKernelWriter *writer(); + ExampleKernelWriter *writer(); /** Get the kernel writer. */ - const AclKernelWriter *writer() const; + const ExampleKernelWriter *writer() const; private: - AclKernelWriter *_writer; + ExampleKernelWriter *_writer; int32_t _parent_id_space; }; -#endif // CKW_INCLUDE_ACL_ACLSCOPEDKERNELWRITER_H +#endif // CKW_PROTOTYPE_EXAMPLES_COMMON_EXAMPLESCOPEDKERNELWRITER_H diff --git a/compute_kernel_writer/prototype/include/ckw/Error.h b/compute_kernel_writer/prototype/include/ckw/Error.h new file mode 100644 index 0000000000..b18944eac5 --- /dev/null +++ b/compute_kernel_writer/prototype/include/ckw/Error.h @@ -0,0 +1,79 @@ +/* + * 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_PROTOTYPE_INCLUDE_CKW_ERROR_H +#define CKW_PROTOTYPE_INCLUDE_CKW_ERROR_H + +#include <stdexcept> +#include <string> + +namespace ckw +{ + +/** If the condition is not met, throw an std::runtime_error with the specified message. + * + * @param[in] cond The condition that is expected to be true. + * @param[in] msg The error message when the condition is not met. + */ +#define CKW_ASSERT_MSG(cond, msg) \ + do \ + { \ + if(!(cond)) \ + { \ + throw ::std::runtime_error(msg); \ + } \ + } while(false) + +/** If the condition is not met, throw an std::runtime_error. + * + * @param[in] cond The condition that is expected to be true. + */ +#define CKW_ASSERT(cond) CKW_ASSERT_MSG(cond, #cond) + +/** If the precondition is met but the consequence is not met, throw an std::runtime_error. + * + * @param[in] precond The condition if is met requires the consequence must also be met. + * @param[in] cond The condition that is expected to be true if the precondition is true. + */ +#define CKW_ASSERT_IF(precond, cond) \ + CKW_ASSERT_MSG(!(precond) || ((precond) && (cond)), #precond " |-> " #cond) + +/** Mark the variables as unused. + * + * @param[in] ... Variables which are unused. + */ +#define CKW_UNUSED(...) ::ckw::ignore_unused(__VA_ARGS__) // NOLINT + +/** Mark the variables as unused. + * + * @param[in] ... Variables which are unused. + */ +template <typename... T> +inline void ignore_unused(T &&...) +{ +} + +} // namespace ckw + +#endif // CKW_INCLUDE_CKW_ERROR_H diff --git a/compute_kernel_writer/include/ckw/Kernel.h b/compute_kernel_writer/prototype/include/ckw/Kernel.h index cbc7700c22..57a8a40341 100644 --- a/compute_kernel_writer/include/ckw/Kernel.h +++ b/compute_kernel_writer/prototype/include/ckw/Kernel.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_KERNEL_H -#define CKW_INCLUDE_CKW_KERNEL_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_KERNEL_H +#define CKW_PROTOTYPE_INCLUDE_CKW_KERNEL_H #include "ckw/OperandBase.h" #include "ckw/Types.h" @@ -74,4 +74,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_KERNEL_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_KERNEL_H diff --git a/compute_kernel_writer/include/ckw/KernelWriter.h b/compute_kernel_writer/prototype/include/ckw/KernelWriter.h index 5dce62a14c..a2778a9485 100644 --- a/compute_kernel_writer/include/ckw/KernelWriter.h +++ b/compute_kernel_writer/prototype/include/ckw/KernelWriter.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_KERNELWRITER_H -#define CKW_INCLUDE_CKW_KERNELWRITER_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_KERNELWRITER_H +#define CKW_PROTOTYPE_INCLUDE_CKW_KERNELWRITER_H #include "ckw/Kernel.h" #include "ckw/TensorInfo.h" @@ -214,4 +214,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_KERNELWRITER_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_KERNELWRITER_H diff --git a/compute_kernel_writer/include/ckw/OperandBase.h b/compute_kernel_writer/prototype/include/ckw/OperandBase.h index 0ea5030968..f4825e16a7 100644 --- a/compute_kernel_writer/include/ckw/OperandBase.h +++ b/compute_kernel_writer/prototype/include/ckw/OperandBase.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_OPERANDBASE_H -#define CKW_INCLUDE_CKW_OPERANDBASE_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_OPERANDBASE_H +#define CKW_PROTOTYPE_INCLUDE_CKW_OPERANDBASE_H #include "ckw/Types.h" #include <string> @@ -73,4 +73,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_OPERANDBASE_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_OPERANDBASE_H diff --git a/compute_kernel_writer/include/ckw/ScalarValue.h b/compute_kernel_writer/prototype/include/ckw/ScalarValue.h index cf017d435f..16c3f6d441 100644 --- a/compute_kernel_writer/include/ckw/ScalarValue.h +++ b/compute_kernel_writer/prototype/include/ckw/ScalarValue.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_SCALARVALUE_H -#define CKW_INCLUDE_CKW_SCALARVALUE_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_SCALARVALUE_H +#define CKW_PROTOTYPE_INCLUDE_CKW_SCALARVALUE_H #include "ckw/Error.h" @@ -134,4 +134,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_SCALARVALUE_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_SCALARVALUE_H diff --git a/compute_kernel_writer/prototype/include/ckw/TensorInfo.h b/compute_kernel_writer/prototype/include/ckw/TensorInfo.h new file mode 100644 index 0000000000..00bb60a444 --- /dev/null +++ b/compute_kernel_writer/prototype/include/ckw/TensorInfo.h @@ -0,0 +1,145 @@ +/* + * 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_PROTOTYPE_INCLUDE_CKW_TENSORINFO_H +#define CKW_PROTOTYPE_INCLUDE_CKW_TENSORINFO_H + +#include "ckw/Types.h" + +#include <array> +#include <cstdint> + +namespace ckw +{ +/** Compute Kernel Writer tensor data layout (or memory format) */ +enum class TensorDataLayout +{ + Unknown, + Nhwc, + Ndhwc +}; + +/** Compute Kernel Writer tensor data layout component */ +enum class TensorDataLayoutComponent +{ + Unknown, + N, + D, + H, + W, + C, +}; + +/** 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 @ref TensorComponent + Stride = 0x02000000, // For example, stride0 in @ref TensorComponent + Dimension = 0x04000000, // For example, Dim0 in @ref TensorComponent + FoldedDimensions = 0x08000000, // For example, Dim0xDim1 in @ref TensorComponent +}; + +/** Compute Kernel Writer tensor component. The tensor components are used to access specific backend-agnostic tensor arguments, + * such as the tensor dimensions and tensor strides. + * The data type is represented as an integer. The value of the integer value + * is assigned to retrieve the information through the @ref TensorComponentBitmask. + */ +enum class TensorComponent : uint32_t +{ + Unknown = 0x00000000, + OffsetFirstElement = 0x01000000, + Stride0 = 0x02000001, + Stride1 = 0x02000010, + Stride2 = 0x02000100, + Stride3 = 0x02001000, + Stride4 = 0x02010000, + Dim0 = 0x04000001, + Dim1 = 0x04000010, + Dim2 = 0x04000100, + Dim3 = 0x04001000, + Dim4 = 0x04010000, + Dim1xDim2 = 0x08000110, + Dim2xDim3 = 0x08001100, + Dim1xDim2xDim3 = 0x08001110 +}; + +/** Compute Kernel Writer tensor storage. The tensor storage represents the type of tensor memory object. + */ +enum class TensorStorage : uint32_t +{ + Unknown = 0x00000000, + BufferUint8Ptr = 0x01000000, + Texture2dReadOnly = 0x02000001, + Texture2dWriteOnly = 0x02000010, +}; + +/** Compute Kernel Writer tensor shape + * Negative dimensions can be interpreted as dynamic dimensions by the Compute Kernel Writer + */ +using TensorShape = std::array<int32_t, 5>; + +/** Compute Kernel Writer tensor info */ +class TensorInfo +{ +public: + /** Constructor + * + * @param[in] dt Tensor data type + * @param[in] shape Tensor shape + * @param[in] dl Tensor data layout + * @param[in] id Tensor id. The id is used to keep track of the bound user tensor. Through the id, + * the user can know what tensor has been used by the Compute Kernel Writer. + * Possible id values: + * - greater than or equal to 0: bind a user specific tensors + * - less than 0: bind a virtual tensor (tile) + */ + TensorInfo(DataType dt, const TensorShape &shape, TensorDataLayout dl, int32_t id); + /** Set shape */ + TensorInfo &shape(const TensorShape &shape); + /** Get shape */ + TensorShape shape() const; + /** Set data type */ + TensorInfo &data_type(DataType dt); + /** Get data type */ + DataType data_type() const; + /** Set data layout */ + TensorInfo &data_layout(TensorDataLayout dl); + /** Get data layout */ + TensorDataLayout data_layout() const; + /** Set id */ + TensorInfo &id(int32_t id); + /** Get layout */ + int32_t id() const; + +private: + TensorShape _shape{ { 0 } }; + DataType _dt{ DataType::Unknown }; + TensorDataLayout _dl{ TensorDataLayout::Unknown }; + int32_t _id{ -1 }; +}; +} // namespace kw + +#endif /* CKW_PROTOTYPE_INCLUDE_CKW_TENSORINFO_H */ diff --git a/compute_kernel_writer/include/ckw/TensorOperand.h b/compute_kernel_writer/prototype/include/ckw/TensorOperand.h index 130ab596fb..2fc5044d1c 100644 --- a/compute_kernel_writer/include/ckw/TensorOperand.h +++ b/compute_kernel_writer/prototype/include/ckw/TensorOperand.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_TENSOROPERAND_H -#define CKW_INCLUDE_CKW_TENSOROPERAND_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H +#define CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H #include "ckw/OperandBase.h" #include "ckw/TensorInfo.h" @@ -178,4 +178,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_TENSOROPERAND_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_TENSOROPERAND_H diff --git a/compute_kernel_writer/include/ckw/TensorTileSampler.h b/compute_kernel_writer/prototype/include/ckw/TensorTileSampler.h index 5ef7bca647..2ea65bce9e 100644 --- a/compute_kernel_writer/include/ckw/TensorTileSampler.h +++ b/compute_kernel_writer/prototype/include/ckw/TensorTileSampler.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_TENSORTILESAMPLER_H -#define CKW_INCLUDE_CKW_TENSORTILESAMPLER_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_TENSORTILESAMPLER_H +#define CKW_PROTOTYPE_INCLUDE_CKW_TENSORTILESAMPLER_H #include "ckw/Types.h" #include <functional> @@ -160,4 +160,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_TENSORTILESAMPLER_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_TENSORTILESAMPLER_H diff --git a/compute_kernel_writer/prototype/include/ckw/TileInfo.h b/compute_kernel_writer/prototype/include/ckw/TileInfo.h new file mode 100644 index 0000000000..8fba8bb827 --- /dev/null +++ b/compute_kernel_writer/prototype/include/ckw/TileInfo.h @@ -0,0 +1,84 @@ +/* + * 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_PROTOTYPE_INCLUDE_CKW_TILEINFO_H +#define CKW_PROTOTYPE_INCLUDE_CKW_TILEINFO_H + +#include "ckw/Types.h" + +#include <array> +#include <cstdint> + +namespace ckw +{ +// Constants to access the tile width and height in the TileShape +constexpr int32_t kTileWidthIdx = 0; +constexpr int32_t kTileHeightIdx = 1; + +/** Compute Kernel Writer tile shape. It is used to define the shape of the tile */ +using TileShape = std::array<int32_t, 2>; + +/** Compute Kernel Writer tile info */ +class TileInfo +{ +public: + /** Constructor used to initialize a scalar variable with a given data type + * + * @param[in] dt Tile data type + */ + TileInfo(DataType dt); + /** Constructor used to initialize a vector with a given data type and vector length. + * + * @param[in] dt Tile data type + * @param[in] w Tile width (or vector length) + */ + TileInfo(DataType dt, int32_t w); + /** Constructor used to initialize a tile with a given data type and tile sizes. + * + * @param[in] dt Tile data type + * @param[in] h Tile height + * @param[in] w Tile width + */ + TileInfo(DataType dt, int32_t h, int32_t w); + /** Set width */ + TileInfo &width(int32_t w); + /** Get width */ + int32_t width() const; + /** Set height */ + TileInfo &height(int32_t h); + /** Get height */ + int32_t height() const; + /** Set data type */ + TileInfo &data_type(DataType dt); + /** Get data type */ + DataType data_type() const; + +private: + DataType _dt{ DataType::Unknown }; + TileShape _shape{}; +}; + +} // namespace ckw + +#endif /* COMPUTE_KERNEL_WRITER_INCLUDE_CKW_TILEINFO_H */ diff --git a/compute_kernel_writer/include/ckw/TileOperand.h b/compute_kernel_writer/prototype/include/ckw/TileOperand.h index 1eee18589f..c071707a45 100644 --- a/compute_kernel_writer/include/ckw/TileOperand.h +++ b/compute_kernel_writer/prototype/include/ckw/TileOperand.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_INCLUDE_CKW_TILEOPERAND_H -#define CKW_INCLUDE_CKW_TILEOPERAND_H +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_TILEOPERAND_H +#define CKW_PROTOTYPE_INCLUDE_CKW_TILEOPERAND_H #include "ckw/Error.h" #include "ckw/OperandBase.h" @@ -107,4 +107,4 @@ private: } // namespace ckw -#endif // CKW_INCLUDE_CKW_TILEOPERAND_H +#endif // CKW_PROTOTYPE_INCLUDE_CKW_TILEOPERAND_H diff --git a/compute_kernel_writer/prototype/include/ckw/Types.h b/compute_kernel_writer/prototype/include/ckw/Types.h new file mode 100644 index 0000000000..bb5d7ce077 --- /dev/null +++ b/compute_kernel_writer/prototype/include/ckw/Types.h @@ -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. + */ + +#ifndef CKW_PROTOTYPE_INCLUDE_CKW_TYPES_H +#define CKW_PROTOTYPE_INCLUDE_CKW_TYPES_H + +#include <array> +#include <cstdint> + +namespace ckw +{ + +/** Compute Kernel Writer data types. This data type is used by the code variables and tensor arguments. */ +enum class DataType +{ + Unknown = 0x00, + Fp32 = 0x11, + Fp16 = 0x12, + Int32 = 0x21, + Int16 = 0x22, + Int8 = 0x24, + Uint32 = 0x31, + Uint16 = 0x32, + Uint8 = 0x34, + Bool = 0x41 +}; + +enum class GpuTargetLanguage +{ + Unknown, + OpenCL +}; + +/* Binary operations +*/ +enum class BinaryOp : int32_t +{ + // Elementwise + Add = 0x0000, // + + Sub = 0x0001, // - + Mul = 0x0002, // * + Div = 0x0003, // / + Mod = 0x0004, // % + // Relational + Equal = 0x1000, // == + Less = 0x1001, // < + LessEqual = 0x1002, // <= + Greater = 0x1003, // > + GreaterEqual = 0x1004, // >= + // Algebra + MatMul_Nt_Nt = 0x2000, // X + MatMul_Nt_T = 0x2001, // X + MatMul_T_Nt = 0x2002, // X + MatMul_T_T = 0x2003, // X + Dot = 0x2004, // . + // Logical + LogicalAnd = 0x3000, // && + LogicalOr = 0x3001, // || + LogicalNot = 0x3002 // ! +}; + +enum class AssignmentOp : int32_t +{ + // Unary + Increment = 0x0000, // += + Decrement = 0x0001, // -= +}; + +enum class ScalarUnaryFunction : int32_t +{ + Exp, +}; + +enum class TensorSamplerFormat : int32_t +{ + Unknown = 0, + C_WH_1 = 1, + C_W_H = 2 +}; + +enum class TensorSamplerAddressModeX : int32_t +{ + Unknown = 0, + None = 1, // The user guarantees that the X 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. +}; + +enum class TensorSamplerAddressModeY : int32_t +{ + Unknown = 0, + None = 1, // The user guarantees that the Y 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 + Skip = 3, // Skip the read/write + SkipMinEdgeOnly = 4, // Skip greater than or equal to max only. The user guarantees that the Y coordinate is always >= 0 + SkipMaxEdgeOnly = 5, // Skip less than 0 only + ClampToNearest = 6, // Clamp the coordinate to nearest edge (0 or max value allowed on Y) + ClampToMinEdgeOnly = 7, // Clamp the negative coordinate to 0 only. Therefore, we expect Y to be always < MAX + ClampToMaxEdgeOnly = 8, // Clamp the coordinate to the max value allowed on Y only. We expect Y to be always >= 0 + ClampToBorder = 9, // Clamp to border which always has 0 value + ClampToBorderMinEdgeOnly = 10, + ClampToBorderMaxEdgeOnly = 11 +}; + +enum class TensorSamplerAddressModeZ : int32_t +{ + Unknown = 0, + None = 1, // The user guarantees that the Y coordinate is always in-bound + Skip = 3, // Skip the read/write + SkipMinEdgeOnly = 4, // Skip greater than or equal to max only. The user guarantees that the Y coordinate is always >= 0 + SkipMaxEdgeOnly = 5, // Skip less than 0 only + ClampToNearest = 6, // Clamp the coordinate to nearest edge (0 or max value allowed on Y) + ClampToMinEdgeOnly = 7, // Clamp the negative coordinate to 0 only. Therefore, we expect Y to be always < MAX + ClampToMaxEdgeOnly = 8, // Clamp the coordinate to the max value allowed on Y only. We expect Y to be always >= 0 +}; + +} // namespace ckw + +#endif // CKW_PROTOTYPE_INCLUDE_CKW_TYPES_H diff --git a/compute_kernel_writer/src/Kernel.cpp b/compute_kernel_writer/prototype/src/Kernel.cpp index bbf5c440a7..bbf5c440a7 100644 --- a/compute_kernel_writer/src/Kernel.cpp +++ b/compute_kernel_writer/prototype/src/Kernel.cpp diff --git a/compute_kernel_writer/src/KernelWriter.cpp b/compute_kernel_writer/prototype/src/KernelWriter.cpp index 5d79985e87..5d79985e87 100644 --- a/compute_kernel_writer/src/KernelWriter.cpp +++ b/compute_kernel_writer/prototype/src/KernelWriter.cpp diff --git a/compute_kernel_writer/src/OperandBase.cpp b/compute_kernel_writer/prototype/src/OperandBase.cpp index 59cf846cc7..59cf846cc7 100644 --- a/compute_kernel_writer/src/OperandBase.cpp +++ b/compute_kernel_writer/prototype/src/OperandBase.cpp diff --git a/compute_kernel_writer/src/Prototype.h b/compute_kernel_writer/prototype/src/Prototype.h index 45f1b3d464..b17481537f 100644 --- a/compute_kernel_writer/src/Prototype.h +++ b/compute_kernel_writer/prototype/src/Prototype.h @@ -22,8 +22,8 @@ * SOFTWARE. */ -#ifndef CKW_SRC_PROTOTYPE_H -#define CKW_SRC_PROTOTYPE_H +#ifndef CKW_PROTOTYPE_SRC_PROTOTYPE_H +#define CKW_PROTOTYPE_SRC_PROTOTYPE_H #include <vector> #include <map> @@ -3764,4 +3764,4 @@ inline int32_t adjust_step(TensorSamplerFormat tensor_format, int32_t step, cons } // namespace prototype } // namespace ckw -#endif // CKW_SRC_PROTOTYPE_H +#endif // CKW_PROTOTYPE_SRC_PROTOTYPE_H diff --git a/compute_kernel_writer/prototype/src/TensorInfo.cpp b/compute_kernel_writer/prototype/src/TensorInfo.cpp new file mode 100644 index 0000000000..561c126469 --- /dev/null +++ b/compute_kernel_writer/prototype/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/TensorOperand.cpp b/compute_kernel_writer/prototype/src/TensorOperand.cpp index 00ecc3824e..00ecc3824e 100644 --- a/compute_kernel_writer/src/TensorOperand.cpp +++ b/compute_kernel_writer/prototype/src/TensorOperand.cpp diff --git a/compute_kernel_writer/src/TensorTileSampler.cpp b/compute_kernel_writer/prototype/src/TensorTileSampler.cpp index 143d550dec..143d550dec 100644 --- a/compute_kernel_writer/src/TensorTileSampler.cpp +++ b/compute_kernel_writer/prototype/src/TensorTileSampler.cpp diff --git a/compute_kernel_writer/prototype/src/TileInfo.cpp b/compute_kernel_writer/prototype/src/TileInfo.cpp new file mode 100644 index 0000000000..7d8b2654ef --- /dev/null +++ b/compute_kernel_writer/prototype/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 h, int32_t w) + : _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/TileOperand.cpp b/compute_kernel_writer/prototype/src/TileOperand.cpp index 091947628d..091947628d 100644 --- a/compute_kernel_writer/src/TileOperand.cpp +++ b/compute_kernel_writer/prototype/src/TileOperand.cpp |