diff options
Diffstat (limited to 'compute_kernel_writer/src/types')
-rw-r--r-- | compute_kernel_writer/src/types/ConstantData.cpp | 141 | ||||
-rw-r--r-- | compute_kernel_writer/src/types/DataTypeHelpers.cpp | 35 | ||||
-rw-r--r-- | compute_kernel_writer/src/types/DataTypeHelpers.h | 43 | ||||
-rw-r--r-- | compute_kernel_writer/src/types/TensorComponentType.h | 78 |
4 files changed, 297 insertions, 0 deletions
diff --git a/compute_kernel_writer/src/types/ConstantData.cpp b/compute_kernel_writer/src/types/ConstantData.cpp new file mode 100644 index 0000000000..6d15eab407 --- /dev/null +++ b/compute_kernel_writer/src/types/ConstantData.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2023 Arm Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include "ckw/types/ConstantData.h" + +#include <limits> + +namespace ckw +{ +namespace +{ +template <typename T> +typename std::enable_if<std::is_same<T, float>::value, std::string>::type to_str(T value) +{ + std::stringstream ss; + ss << std::scientific << std::setprecision(std::numeric_limits<T>::max_digits10) << value; + return ss.str(); +} + +template <typename T> +typename std::enable_if<!std::is_same<T, float>::value && !std::is_same<T, bool>::value, std::string>::type +to_str(T value) +{ + return std::to_string(value); +} + +template <typename T> +typename std::enable_if<std::is_same<T, bool>::value, std::string>::type to_str(T value) +{ + return std::to_string((int)value); +} +} // namespace + +template <typename T> +ConstantData::ConstantData(std::initializer_list<std::initializer_list<T>> values, DataType data_type) + : _data_type(data_type) +{ + CKW_ASSERT(validate<T>(data_type)); + CKW_ASSERT(values.size() > 0); + + for (auto value_arr : values) + { + // Each row must have the same number of elements + CKW_ASSERT(value_arr.size() == (*values.begin()).size()); + + StringVector vec; + std::transform(value_arr.begin(), value_arr.end(), std::back_inserter(vec), [](T val) { return to_str(val); }); + + _values.push_back(std::move(vec)); + } +} + +template <typename T> +ConstantData::ConstantData(const std::vector<std::vector<T>> &values, DataType data_type) : _data_type(data_type) +{ + CKW_ASSERT(validate<T>(data_type)); + CKW_ASSERT(values.size() > 0); + + for (auto value_arr : values) + { + // Each row must have the same number of elements + CKW_ASSERT(value_arr.size() == (*values.begin()).size()); + + StringVector vec; + std::transform(value_arr.begin(), value_arr.end(), std::back_inserter(vec), [](T val) { return to_str(val); }); + + _values.push_back(std::move(vec)); + } +} + +template <typename T> +bool ConstantData::validate(DataType data_type) +{ + switch (data_type) + { + case DataType::Fp32: + case DataType::Fp16: + return std::is_same<T, float>::value; + case DataType::Bool: + return std::is_same<T, bool>::value; + case DataType::Int32: + case DataType::Int16: + case DataType::Int8: + return std::is_same<T, int32_t>::value; + case DataType::Uint32: + case DataType::Uint16: + case DataType::Uint8: + return std::is_same<T, uint32_t>::value; + default: + CKW_THROW_MSG("Unknown data type!"); + break; + } +} + +// Necessary instantiations for compiler to recognize +template ConstantData::ConstantData(std::initializer_list<std::initializer_list<int32_t>>, DataType); +template ConstantData::ConstantData(std::initializer_list<std::initializer_list<uint32_t>>, DataType); +template ConstantData::ConstantData(std::initializer_list<std::initializer_list<bool>>, DataType); +template ConstantData::ConstantData(std::initializer_list<std::initializer_list<float>>, DataType); +template ConstantData::ConstantData(const std::vector<std::vector<int32_t>> &, DataType); +template ConstantData::ConstantData(const std::vector<std::vector<uint32_t>> &, DataType); +template ConstantData::ConstantData(const std::vector<std::vector<bool>> &, DataType); +template ConstantData::ConstantData(const std::vector<std::vector<float>> &, DataType); + +template bool ConstantData::validate<int32_t>(DataType); +template bool ConstantData::validate<uint32_t>(DataType); +template bool ConstantData::validate<bool>(DataType); +template bool ConstantData::validate<float>(DataType); + +const std::vector<std::vector<std::string>> &ConstantData::values() const +{ + return _values; +} + +DataType ConstantData::data_type() const +{ + return _data_type; +} + +} // namespace ckw diff --git a/compute_kernel_writer/src/types/DataTypeHelpers.cpp b/compute_kernel_writer/src/types/DataTypeHelpers.cpp new file mode 100644 index 0000000000..7f0c33fb72 --- /dev/null +++ b/compute_kernel_writer/src/types/DataTypeHelpers.cpp @@ -0,0 +1,35 @@ +/* +* 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/types/DataTypeHelpers.h" + +namespace ckw +{ + +bool is_data_type_float(DataType data_type) +{ + return (data_type == DataType::Fp32 || data_type == DataType::Fp16); +} + +} // namespace ckw diff --git a/compute_kernel_writer/src/types/DataTypeHelpers.h b/compute_kernel_writer/src/types/DataTypeHelpers.h new file mode 100644 index 0000000000..b6ec6ccd19 --- /dev/null +++ b/compute_kernel_writer/src/types/DataTypeHelpers.h @@ -0,0 +1,43 @@ +/* +* 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_DATATYPEHELPERS_H +#define CKW_SRC_TYPES_DATATYPEHELPERS_H + +#include "ckw/types/DataType.h" + +namespace ckw +{ + +/** Return a value indicating whether the data type is floating-point. + * + * @param[in] data_type The data type to check. + * + * @return Whether the data type is floating-point. + */ +bool is_data_type_float(DataType data_type); + +} // namespace ckw + +#endif // CKW_SRC_TYPES_DATATYPEHELPERS_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 |