diff options
author | Georgios Pinitas <georgios.pinitas@arm.com> | 2021-08-03 08:24:00 +0100 |
---|---|---|
committer | Sheri Zhang <sheri.zhang@arm.com> | 2021-08-04 08:56:33 +0000 |
commit | 4164814a099773c0a512889473c980bc148e590f (patch) | |
tree | 7ee27bbd40724635f5bf22bc9ec28b3a1a4d1ac9 /src/common | |
parent | 6e90c12e3067c482524a08bf322f42de6d9d27b9 (diff) | |
download | ComputeLibrary-4164814a099773c0a512889473c980bc148e590f.tar.gz |
Implement Operator API
Resolves: COMPMID-4512
Signed-off-by: Georgios Pinitas <georgios.pinitas@arm.com>
Change-Id: Id12130365fa3fe2261160931dcc7affb6b467186
Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/6031
Tested-by: Arm Jenkins <bsgcomp@arm.com>
Reviewed-by: Michele Di Giorgio <michele.digiorgio@arm.com>
Comments-Addressed: Arm Jenkins <bsgcomp@arm.com>
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/IContext.h | 13 | ||||
-rw-r--r-- | src/common/IOperator.cpp | 73 | ||||
-rw-r--r-- | src/common/IOperator.h | 72 | ||||
-rw-r--r-- | src/common/Types.h | 1 | ||||
-rw-r--r-- | src/common/utils/LegacySupport.cpp | 54 | ||||
-rw-r--r-- | src/common/utils/LegacySupport.h | 8 |
6 files changed, 170 insertions, 51 deletions
diff --git a/src/common/IContext.h b/src/common/IContext.h index 31f39da06d..1ae46c57de 100644 --- a/src/common/IContext.h +++ b/src/common/IContext.h @@ -44,6 +44,7 @@ namespace arm_compute // Forward declarations class ITensorV2; class IQueue; +class IOperator; /**< Context interface */ class IContext : public AclContext_ @@ -53,13 +54,11 @@ public: : AclContext_(), _target(target), _refcount(0) { } - /** Virtual Destructor */ virtual ~IContext() { header.type = detail::ObjectType::Invalid; }; - /** Target type accessor * * @return Target that the context is associated with @@ -68,19 +67,16 @@ public: { return _target; } - /** Increment context refcount */ void inc_ref() const { ++_refcount; } - /** Decrement context refcount */ void dec_ref() const { --_refcount; } - /** Reference counter accessor * * @return The number of references pointing to this object @@ -89,7 +85,6 @@ public: { return _refcount; } - /** Checks if an object is valid * * @return True if sucessful otherwise false @@ -98,7 +93,6 @@ public: { return header.type == detail::ObjectType::Context; } - /** Create a tensor object * * @param[in] desc Descriptor to use @@ -107,7 +101,6 @@ public: * @return A pointer to the created tensor object */ virtual ITensorV2 *create_tensor(const AclTensorDescriptor &desc, bool allocate) = 0; - /** Create a queue object * * @param[in] options Queue options to be used @@ -115,6 +108,10 @@ public: * @return A pointer to the created queue object */ virtual IQueue *create_queue(const AclQueueOptions *options) = 0; + virtual std::tuple<IOperator *, StatusCode> create_activation(const AclTensorDescriptor &src, + const AclTensorDescriptor &dst, + const AclActivationDescriptor &act, + bool is_validate) = 0; private: Target _target; /**< Target type of context */ diff --git a/src/common/IOperator.cpp b/src/common/IOperator.cpp new file mode 100644 index 0000000000..b56f0e97fb --- /dev/null +++ b/src/common/IOperator.cpp @@ -0,0 +1,73 @@ +/* + * 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/IOperator.h" +#include "src/common/utils/Validate.h" + +namespace arm_compute +{ +#ifndef DOXYGEN_SKIP_THIS +IOperator::IOperator(IContext *ctx) + : AclOperator_() +{ + ARM_COMPUTE_ASSERT_NOT_NULLPTR(ctx); + this->header.ctx = ctx; + this->header.ctx->inc_ref(); +} + +IOperator::~IOperator() +{ + this->header.ctx->dec_ref(); + this->header.type = detail::ObjectType::Invalid; +} + +bool IOperator::is_valid() const +{ + return this->header.type == detail::ObjectType::Operator; +} + +StatusCode IOperator::run(ITensorPack &tensors) +{ + _op->run(tensors); + return StatusCode::Success; +} + +StatusCode IOperator::run(IQueue &queue, ITensorPack &tensors) +{ + ARM_COMPUTE_UNUSED(queue); + _op->run(tensors); + return StatusCode::Success; +} + +StatusCode IOperator::prepare(ITensorPack &tensors) +{ + _op->prepare(tensors); + return StatusCode::Success; +} + +MemoryRequirements IOperator::workspace() const +{ + return _op->workspace(); +} +#endif /* DOXYGEN_SKIP_THIS */ +} // namespace arm_compute diff --git a/src/common/IOperator.h b/src/common/IOperator.h index 7fdb443acb..1b65a09e0d 100644 --- a/src/common/IOperator.h +++ b/src/common/IOperator.h @@ -27,6 +27,11 @@ #include "src/common/IContext.h" #include "src/common/IQueue.h" +// TODO: Remove when all functions have been ported +#include "arm_compute/core/experimental/Types.h" +#include "arm_compute/runtime/IOperator.h" +#include "src/common/utils/Validate.h" + #include <vector> struct AclOperator_ @@ -42,21 +47,12 @@ namespace arm_compute { // Forward declarations class ITensorPack; - -/** Structure to capture internally memory requirements */ -struct MemoryInfo +namespace experimental { - MemoryInfo(AclTensorSlot slot_id, size_t size, size_t alignment) noexcept - : slot_id(slot_id), - size(size), - alignment(alignment) - { - } - AclTensorSlot slot_id; - size_t size; - size_t alignment; -}; -using MemoryRequirements = std::vector<MemoryInfo>; +class IOperator; +} // namespace experimental + +using MemoryRequirements = experimental::MemoryRequirements; /** Base class specifying the operator interface */ class IOperator : public AclOperator_ @@ -66,55 +62,45 @@ public: * * @param[in] ctx Context to be used by the operator */ - explicit IOperator(IContext *ctx) - { - this->header.ctx = ctx; - this->header.ctx->inc_ref(); - } - + explicit IOperator(IContext *ctx); /** Destructor */ - virtual ~IOperator() - { - this->header.ctx->dec_ref(); - this->header.type = detail::ObjectType::Invalid; - }; - + virtual ~IOperator(); /** Checks if an operator is valid * * @return True if successful otherwise false */ - bool is_valid() const - { - return this->header.type == detail::ObjectType::Operator; - }; - + bool is_valid() const; /** Run the kernels contained in the function * - * @param[in] queue Queue to run a kernel on + * @param[in] queue Queue to use * @param[in] tensors Vector that contains the tensors to operate on */ - virtual StatusCode run(IQueue &queue, ITensorPack &tensors) = 0; - + virtual StatusCode run(IQueue &queue, ITensorPack &tensors); + /** Run the kernels contained in the function + * + * @param[in] tensors Vector that contains the tensors to operate on + */ + virtual StatusCode run(ITensorPack &tensors); /** Prepare the operator for execution * * Any one off pre-processing step required by the function is handled here * - * @param[in] constants Vector that contains the constants tensors. + * @param[in] tensors Vector that contains the preparation tensors. * * @note Prepare stage might not need all the function's buffers' backing memory to be available in order to execute */ - virtual StatusCode prepare(ITensorPack &constants) - { - ARM_COMPUTE_UNUSED(constants); - return StatusCode::Success; - } - + virtual StatusCode prepare(ITensorPack &tensors); /** Return the memory requirements required by the workspace */ - virtual MemoryRequirements workspace() const + virtual MemoryRequirements workspace() const; + + void set_internal_operator(std::unique_ptr<experimental::IOperator> op) { - return {}; + _op = std::move(op); } + +private: + std::unique_ptr<experimental::IOperator> _op{ nullptr }; }; /** Extract internal representation of an Operator diff --git a/src/common/Types.h b/src/common/Types.h index ba07b51d55..25ee32353a 100644 --- a/src/common/Types.h +++ b/src/common/Types.h @@ -24,6 +24,7 @@ #ifndef SRC_COMMON_TYPES_H_ #define SRC_COMMON_TYPES_H_ +#include "arm_compute/AclDescriptors.h" #include "arm_compute/AclTypes.h" namespace arm_compute diff --git a/src/common/utils/LegacySupport.cpp b/src/common/utils/LegacySupport.cpp index 569b2abd89..6623825124 100644 --- a/src/common/utils/LegacySupport.cpp +++ b/src/common/utils/LegacySupport.cpp @@ -107,5 +107,59 @@ AclTensorDescriptor convert_to_descriptor(const TensorInfo &info) }; return desc; } + +ActivationLayerInfo convert_to_activation_info(const AclActivationDescriptor &desc) +{ + ActivationLayerInfo::ActivationFunction act; + switch(desc.type) + { + case AclActivationType::AclIdentity: + act = ActivationLayerInfo::ActivationFunction::IDENTITY; + break; + case AclActivationType::AclLogistic: + act = ActivationLayerInfo::ActivationFunction::LOGISTIC; + break; + case AclActivationType::AclTanh: + act = ActivationLayerInfo::ActivationFunction::TANH; + break; + case AclActivationType::AclRelu: + act = ActivationLayerInfo::ActivationFunction::RELU; + break; + case AclActivationType::AclBoundedRelu: + act = ActivationLayerInfo::ActivationFunction::BOUNDED_RELU; + break; + case AclActivationType::AclLuBoundedRelu: + act = ActivationLayerInfo::ActivationFunction::LU_BOUNDED_RELU; + break; + case AclActivationType::AclLeakyRelu: + act = ActivationLayerInfo::ActivationFunction::LEAKY_RELU; + break; + case AclActivationType::AclSoftRelu: + act = ActivationLayerInfo::ActivationFunction::SOFT_RELU; + break; + case AclActivationType::AclElu: + act = ActivationLayerInfo::ActivationFunction::ELU; + break; + case AclActivationType::AclAbs: + act = ActivationLayerInfo::ActivationFunction::ABS; + break; + case AclActivationType::AclSquare: + act = ActivationLayerInfo::ActivationFunction::SQUARE; + break; + case AclActivationType::AclSqrt: + act = ActivationLayerInfo::ActivationFunction::SQRT; + break; + case AclActivationType::AclLinear: + act = ActivationLayerInfo::ActivationFunction::LINEAR; + break; + case AclActivationType::AclHardSwish: + act = ActivationLayerInfo::ActivationFunction::HARD_SWISH; + break; + default: + return ActivationLayerInfo(); + } + + return ActivationLayerInfo(act, desc.a, desc.b); +} } // namespace detail } // namespace arm_compute diff --git a/src/common/utils/LegacySupport.h b/src/common/utils/LegacySupport.h index c2cc1bc182..3c3b09fce6 100644 --- a/src/common/utils/LegacySupport.h +++ b/src/common/utils/LegacySupport.h @@ -26,6 +26,7 @@ #include "arm_compute/Acl.h" #include "arm_compute/core/TensorInfo.h" +#include "arm_compute/core/Types.h" namespace arm_compute { @@ -45,6 +46,13 @@ TensorInfo convert_to_legacy_tensor_info(const AclTensorDescriptor &desc); * @return A converted descriptor */ AclTensorDescriptor convert_to_descriptor(const TensorInfo &info); +/** Convert an AclActivation descriptor to an internal one + * + * @param[in] desc Descriptor to convert + * + * @return Legacy tensor meta-data + */ +ActivationLayerInfo convert_to_activation_info(const AclActivationDescriptor &desc); } // namespace detail } // namespace arm_compute |