diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/IContext.h | 11 | ||||
-rw-r--r-- | src/common/ITensor.h | 128 | ||||
-rw-r--r-- | src/common/TensorPack.cpp | 74 | ||||
-rw-r--r-- | src/common/TensorPack.h | 130 | ||||
-rw-r--r-- | src/common/Types.h | 5 | ||||
-rw-r--r-- | src/common/utils/LegacySupport.cpp | 66 | ||||
-rw-r--r-- | src/common/utils/LegacySupport.h | 44 | ||||
-rw-r--r-- | src/common/utils/Log.h | 11 | ||||
-rw-r--r-- | src/common/utils/Utils.h | 16 |
9 files changed, 485 insertions, 0 deletions
diff --git a/src/common/IContext.h b/src/common/IContext.h index 0d23abd2be..ee234795cf 100644 --- a/src/common/IContext.h +++ b/src/common/IContext.h @@ -41,6 +41,9 @@ protected: namespace arm_compute { +// Forward declarations +class ITensorV2; + /**< Context interface */ class IContext : public AclContext_ { @@ -88,6 +91,14 @@ public: { return header.type == detail::ObjectType::Context; } + /** Create a tensor object + * + * @param[in] desc Descriptor to use + * @param[in] allocate Flag to allocate tensor + * + * @return A pointer to the created tensor object + */ + virtual ITensorV2 *create_tensor(const AclTensorDescriptor &desc, bool allocate) = 0; private: Target _target; /**< Target type of context */ diff --git a/src/common/ITensor.h b/src/common/ITensor.h new file mode 100644 index 0000000000..ee7eac7688 --- /dev/null +++ b/src/common/ITensor.h @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 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 SRC_COMMON_ITENSOR_H_ +#define SRC_COMMON_ITENSOR_H_ + +#include "src/common/IContext.h" +#include "src/common/utils/Validate.h" + +struct AclTensor_ +{ + arm_compute::detail::Header header{ arm_compute::detail::ObjectType::Tensor, nullptr }; + +protected: + AclTensor_() = default; + ~AclTensor_() = default; +}; + +namespace arm_compute +{ +// Forward declaration +class ITensor; + +/** Base class specifying the tensor interface */ +class ITensorV2 : public AclTensor_ +{ +public: + /** Explict Operator Constructor + * + * @param[in] ctx Context to be used by the operator + */ + explicit ITensorV2(IContext *ctx) + : AclTensor_() + { + ARM_COMPUTE_ASSERT_NOT_NULLPTR(ctx); + this->header.ctx = ctx; + this->header.ctx->inc_ref(); + } + /** Destructor */ + virtual ~ITensorV2() + { + this->header.ctx->dec_ref(); + this->header.type = detail::ObjectType::Invalid; + }; + /** Checks if a queue is valid + * + * @return True if successful otherwise false + */ + bool is_valid() const + { + return this->header.type == detail::ObjectType::Tensor; + }; + /** Map tensor to a host pointer + * + * @return A pointer to the underlying backing memory if successful else nullptr + */ + virtual void *map() = 0; + /** Unmap tensor + * + * @return AclStatus A status cod + */ + virtual StatusCode unmap() = 0; + /** Import external memory handle + * + * @param[in] handle Memory to import + * @param[in] type Type of imported memory + * + * @return Status code + */ + virtual StatusCode import(void *handle, ImportMemoryType type) = 0; + /** Get the legacy tensor object + * + * @return The legacy underlying tensor object + */ + virtual arm_compute::ITensor *tensor() = 0; +}; + +/** Extract internal representation of a Tensor + * + * @param[in] tensor Opaque tensor pointer + * + * @return The internal representation as an ITensor + */ +inline ITensorV2 *get_internal(AclTensor tensor) +{ + return static_cast<ITensorV2 *>(tensor); +} + +namespace detail +{ +/** Check if an internal tensor is valid + * + * @param[in] tensor Internal tensor to check + * + * @return A status code + */ +inline StatusCode validate_internal_tensor(const ITensorV2 *tensor) +{ + if(tensor == nullptr || !tensor->is_valid()) + { + ARM_COMPUTE_LOG_ERROR_ACL("[ITensorV2]: Invalid tensor object"); + return StatusCode::InvalidArgument; + } + return StatusCode::Success; +} +} // namespace detail +} // namespace arm_compute +#endif /* SRC_COMMON_ITENSOR_H_ */ diff --git a/src/common/TensorPack.cpp b/src/common/TensorPack.cpp new file mode 100644 index 0000000000..c582c7b106 --- /dev/null +++ b/src/common/TensorPack.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2021 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/common/TensorPack.h" +#include "src/common/ITensor.h" +#include "src/common/utils/Validate.h" + +namespace arm_compute +{ +TensorPack::TensorPack(IContext *ctx) + : AclTensorPack_(), _pack() +{ + ARM_COMPUTE_ASSERT_NOT_NULLPTR(ctx); + this->header.ctx = ctx; + this->header.ctx->inc_ref(); +} + +TensorPack::~TensorPack() +{ + this->header.ctx->dec_ref(); + this->header.type = detail::ObjectType::Invalid; +} + +AclStatus TensorPack::add_tensor(ITensorV2 *tensor, int32_t slot_id) +{ + _pack.add_tensor(slot_id, tensor->tensor()); + return AclStatus::AclSuccess; +} + +size_t TensorPack::size() const +{ + return _pack.size(); +} + +bool TensorPack::empty() const +{ + return _pack.empty(); +} + +bool TensorPack::is_valid() const +{ + return this->header.type == detail::ObjectType::TensorPack; +} + +arm_compute::ITensor *TensorPack::get_tensor(int32_t slot_id) +{ + return _pack.get_tensor(slot_id); +} + +arm_compute::ITensorPack &TensorPack::get_tensor_pack() +{ + return _pack; +} +} // namespace arm_compute diff --git a/src/common/TensorPack.h b/src/common/TensorPack.h new file mode 100644 index 0000000000..f330eee740 --- /dev/null +++ b/src/common/TensorPack.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2021 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 SRC_COMMON_ITENSORPACK_H_ +#define SRC_COMMON_ITENSORPACK_H_ + +#include "arm_compute/core/ITensorPack.h" +#include "src/common/IContext.h" + +struct AclTensorPack_ +{ + arm_compute::detail::Header header{ arm_compute::detail::ObjectType::TensorPack, nullptr }; + +protected: + AclTensorPack_() = default; + ~AclTensorPack_() = default; +}; + +namespace arm_compute +{ +// Forward declaration +class ITensor; +class ITensorV2; + +/** Tensor packing service + * + * Class is responsible for creating and managing a collection of tensors. + * Tensor packs can be passed to operators to be part of the mutable data of the execution. + */ +class TensorPack : public AclTensorPack_ +{ +public: + /** Constructor + * + * @param[in] ctx Context to be used + */ + explicit TensorPack(IContext *ctx); + /** Destructor */ + ~TensorPack(); + /** Add tensor to the pack + * + * @param[in] tensor Tensor to add + * @param[in] slot_id Slot identification in respect to the operator of the tensor to add + * + * @return Status code + */ + AclStatus add_tensor(ITensorV2 *tensor, int32_t slot_id); + /** Pack size accessor + * + * @return Number of tensors registered to the pack + */ + size_t size() const; + /** Checks if pack is empty + * + * @return True if empty else false + */ + bool empty() const; + /** Checks if an object is valid + * + * @return True if valid else false + */ + bool is_valid() const; + /** Get tensor of a given id from the pac + * + * @param[in] slot_id Slot identification of tensor to extract + * + * @return The pointer to the tensor if exist and is non-const else nullptr + */ + arm_compute::ITensor *get_tensor(int32_t slot_id); + /** Get legacy tensor pack + * + * @return Legacy tensor pack + */ + arm_compute::ITensorPack &get_tensor_pack(); + +private: + arm_compute::ITensorPack _pack; /**< Pack that currently redirects to the existing TensorPack */ +}; + +/** Extract internal representation of a TensoPack + * + * @param[in] pack Opaque tensor pack pointer + * + * @return The internal representation as an TensorPack + */ +inline TensorPack *get_internal(AclTensorPack pack) +{ + return static_cast<TensorPack *>(pack); +} + +namespace detail +{ +/** Check if an internal TensorPack is valid + * + * @param[in] pack Internal tensor pack to check + * + * @return A status code + */ +inline StatusCode validate_internal_pack(const TensorPack *pack) +{ + if(pack == nullptr || !pack->is_valid()) + { + ARM_COMPUTE_LOG_ERROR_ACL("[TensorPack]: Invalid tensor pack object"); + return StatusCode::InvalidArgument; + } + return StatusCode::Success; +} +} // namespace detail +} // namespace arm_compute +#endif /* SRC_COMMON_ITENSORPACK_H_ */ diff --git a/src/common/Types.h b/src/common/Types.h index 60a11b04ec..ba07b51d55 100644 --- a/src/common/Types.h +++ b/src/common/Types.h @@ -52,5 +52,10 @@ enum class ExecutionMode FastRerun = AclPreferFastRerun, FastStart = AclPreferFastStart, }; + +enum class ImportMemoryType +{ + HostPtr = AclImportMemoryType::AclHostPtr +}; } // namespace arm_compute #endif /* SRC_COMMON_TYPES_H_ */ diff --git a/src/common/utils/LegacySupport.cpp b/src/common/utils/LegacySupport.cpp new file mode 100644 index 0000000000..5981c657bd --- /dev/null +++ b/src/common/utils/LegacySupport.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2021 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/common/utils/LegacySupport.h" + +namespace arm_compute +{ +namespace detail +{ +namespace +{ +DataType data_type_mapper(AclDataType data_type) +{ + switch(data_type) + { + case AclDataType::AclFloat32: + return DataType::F32; + case AclDataType::AclFloat16: + return DataType::F16; + case AclDataType::AclBFloat16: + return DataType::BFLOAT16; + default: + return DataType::UNKNOWN; + ; + } +} + +TensorShape tensor_shape_mapper(int32_t ndims, int32_t *shape) +{ + TensorShape legacy_shape{}; + for(int32_t d = 0; d < ndims; ++d) + { + legacy_shape.set(d, shape[d], false); + } + return legacy_shape; +} +} // namespace + +TensorInfo convert_to_legacy_tensor_info(const AclTensorDescriptor &desc) +{ + TensorInfo legacy_desc; + legacy_desc.init(tensor_shape_mapper(desc.ndims, desc.shape), 1, data_type_mapper(desc.data_type)); + return legacy_desc; +} +} // namespace detail +} // namespace arm_compute diff --git a/src/common/utils/LegacySupport.h b/src/common/utils/LegacySupport.h new file mode 100644 index 0000000000..37329b747c --- /dev/null +++ b/src/common/utils/LegacySupport.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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 SRC_COMMON_LEGACY_SUPPORT_H +#define SRC_COMMON_LEGACY_SUPPORT_H + +#include "arm_compute/Acl.h" +#include "arm_compute/core/TensorInfo.h" + +namespace arm_compute +{ +namespace detail +{ +/** Convert a descriptor to a legacy format one + * + * @param[in] desc Descriptor to convert + * + * @return Legacy tensor meta-data + */ +TensorInfo convert_to_legacy_tensor_info(const AclTensorDescriptor &desc); +} // namespace detail +} // namespace arm_compute + +#endif /* SRC_COMMON_LEGACY_SUPPORT_H */ diff --git a/src/common/utils/Log.h b/src/common/utils/Log.h index 0d6a50da92..496ee74a16 100644 --- a/src/common/utils/Log.h +++ b/src/common/utils/Log.h @@ -77,4 +77,15 @@ ARM_COMPUTE_LOG_MSG("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \ } while(false) +/** Log an error message to the logger with function name before the message + * + * @param[in] msg Message to log + */ +#define ARM_COMPUTE_LOG_ERROR_WITH_FUNCNAME_ACL(msg) \ + do \ + { \ + ARM_COMPUTE_CREATE_ACL_LOGGER(); \ + ARM_COMPUTE_LOG_MSG_WITH_FUNCNAME("ComputeLibrary", arm_compute::logging::LogLevel::ERROR, msg); \ + } while(false) + #endif /* SRC_COMMON_LOG_H */ diff --git a/src/common/utils/Utils.h b/src/common/utils/Utils.h index 9602c32f62..87be9df509 100644 --- a/src/common/utils/Utils.h +++ b/src/common/utils/Utils.h @@ -44,6 +44,22 @@ constexpr E as_cenum(SE v) noexcept { return static_cast<E>(static_cast<std::underlying_type_t<SE>>(v)); } + +/** Convert plain old enumeration to a strongly typed enum + * + * @tparam SE Strongly typed resulting enum + * @tparam E Plain old C enum + * + * @param[in] val Value to convert + * + * @return A corresponding strongly typed enumeration + */ +template <typename SE, typename E> +constexpr SE as_enum(E val) noexcept +{ + return static_cast<SE>(val); +} + /** Check if the given value is in the given enum value list * * @tparam E The type of the enum |