diff options
author | David Beck <david.beck@arm.com> | 2018-10-10 15:11:44 +0100 |
---|---|---|
committer | Matthew Bentham <matthew.bentham@arm.com> | 2018-10-22 16:57:53 +0100 |
commit | 9df2d951616e3d76b67a9852d5324de96633f0f9 (patch) | |
tree | 37a57a98ef8aafba748d332bfc686e2b022398d6 | |
parent | 6b779f0e437127bfa71a529e9b848b5e41683ab8 (diff) | |
download | armnn-9df2d951616e3d76b67a9852d5324de96633f0f9.tar.gz |
IVGCVSW-1952 : add BackendId class to prepare for the replacement of Compute enum
!armnn:152674
Change-Id: I1bcdfdfbfb73e502d58f35717e2558e24651013c
-rw-r--r-- | CMakeLists.txt | 10 | ||||
-rw-r--r-- | include/armnn/BackendId.hpp | 132 | ||||
-rw-r--r-- | include/armnn/Types.hpp | 16 | ||||
-rw-r--r-- | include/armnn/TypesUtils.hpp | 36 | ||||
-rw-r--r-- | src/backends/BackendRegistry.cpp | 24 | ||||
-rw-r--r-- | src/backends/BackendRegistry.hpp | 13 | ||||
-rw-r--r-- | src/backends/cl/ClBackend.cpp | 4 | ||||
-rw-r--r-- | src/backends/cl/ClBackend.hpp | 2 | ||||
-rw-r--r-- | src/backends/neon/NeonBackend.cpp | 4 | ||||
-rw-r--r-- | src/backends/neon/NeonBackend.hpp | 2 | ||||
-rw-r--r-- | src/backends/reference/RefBackend.cpp | 4 | ||||
-rw-r--r-- | src/backends/reference/RefBackend.hpp | 2 | ||||
-rw-r--r-- | src/backends/test/BackendIdTests.cpp | 27 |
13 files changed, 205 insertions, 71 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 501da806ad..dfda4d1d93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -147,18 +147,23 @@ endif() list(APPEND armnn_sources include/armnn/ArmNN.hpp + include/armnn/BackendId.hpp include/armnn/Descriptors.hpp include/armnn/DescriptorsFwd.hpp - include/armnn/IRuntime.hpp + include/armnn/Exceptions.hpp include/armnn/ILayerSupport.hpp include/armnn/INetwork.hpp + include/armnn/IProfiler.hpp + include/armnn/IRuntime.hpp + include/armnn/LayerSupport.hpp + include/armnn/LstmParams.hpp + include/armnn/NetworkFwd.hpp include/armnn/Optional.hpp include/armnn/Tensor.hpp include/armnn/TensorFwd.hpp include/armnn/Types.hpp include/armnn/TypesUtils.hpp include/armnn/Utils.hpp - include/armnn/LayerSupport.hpp include/armnn/Version.hpp src/armnn/layers/LayerCloneBase.hpp src/armnn/layers/LayerWithParameters.hpp @@ -378,6 +383,7 @@ if(BUILD_UNIT_TESTS) src/armnn/test/InstrumentTests.cpp src/armnn/test/ObservableTest.cpp src/armnn/test/OptionalTest.cpp + src/backends/test/BackendIdTests.cpp src/backends/test/BackendRegistryTests.cpp src/backends/test/IsLayerSupportedTestImpl.hpp src/backends/test/WorkloadDataValidation.cpp diff --git a/include/armnn/BackendId.hpp b/include/armnn/BackendId.hpp new file mode 100644 index 0000000000..d1f06beba7 --- /dev/null +++ b/include/armnn/BackendId.hpp @@ -0,0 +1,132 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// +#pragma once + +#include <set> +#include <unordered_set> +#include <string> +#include <memory> + +namespace armnn +{ + +// +// The Compute enum is now deprecated and it is now +// replaced by BackendId +// +enum class Compute +{ + /// CPU Execution: Reference C++ kernels + CpuRef = 0, + /// CPU Execution: NEON: ArmCompute + CpuAcc = 1, + /// GPU Execution: OpenCL: ArmCompute + GpuAcc = 2, + Undefined = 5 +}; + +constexpr char const* GetComputeDeviceAsCString(Compute compute) +{ + switch (compute) + { + case armnn::Compute::CpuRef: return "CpuRef"; + case armnn::Compute::CpuAcc: return "CpuAcc"; + case armnn::Compute::GpuAcc: return "GpuAcc"; + default: return "Unknown"; + } +} + +inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute) +{ + for (const Compute& comp : compute) { + os << GetComputeDeviceAsCString(comp) << " "; + } + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute) +{ + for (const Compute& comp : compute) { + os << GetComputeDeviceAsCString(comp) << " "; + } + return os; +} + +inline std::ostream& operator<<(std::ostream& os, const Compute& compute) +{ + os << GetComputeDeviceAsCString(compute); + return os; +} + +class BackendId final +{ +public: + BackendId(const std::string& id) : m_Id{id} {} + BackendId(const char* id) : m_Id{id} {} + BackendId(Compute compute) : m_Id{GetComputeDeviceAsCString(compute)} {} + + operator std::string() const { return m_Id; } + + BackendId& operator=(const std::string& other) + { + m_Id = other; + return *this; + } + + BackendId& operator=(Compute compute) + { + BackendId temp{compute}; + std::swap(temp.m_Id, m_Id); + return *this; + } + + bool operator==(const BackendId& other) const + { + return m_Id == other.m_Id; + } + + bool operator<(const BackendId& other) const + { + return m_Id < other.m_Id; + } + + const std::string& Get() const { return m_Id; } + +private: + // backend Id mustn't be empty: + BackendId() = delete; + std::string m_Id; +}; + +template <template <class...> class TContainer> +inline std::ostream& operator<<(std::ostream& os, + const TContainer<BackendId>& ids) +{ + os << '['; + for (const auto& id : ids) { os << id.Get() << " "; } + os << ']'; + return os; +} + +using BackendIdSet = std::unordered_set<BackendId>; + +} // namespace armnn + +namespace std +{ + +// make BackendId compatible with std hashtables by reusing the hash +// function for strings +template <> +struct hash<armnn::BackendId> +{ + std::size_t operator()(const armnn::BackendId& id) const + { + std::hash<std::string> hasher; + return hasher(id.Get()); + } +}; + +} // namespace std diff --git a/include/armnn/Types.hpp b/include/armnn/Types.hpp index b7ee9472a3..4afc50b1b1 100644 --- a/include/armnn/Types.hpp +++ b/include/armnn/Types.hpp @@ -7,6 +7,7 @@ #include <array> #include <memory> #include "ILayerSupport.hpp" +#include "BackendId.hpp" namespace armnn { @@ -94,17 +95,6 @@ enum class OutputShapeRounding Ceiling = 1 }; -enum class Compute -{ - /// CPU Execution: Reference C++ kernels - CpuRef = 0, - /// CPU Execution: NEON: ArmCompute - CpuAcc = 1, - /// GPU Execution: OpenCL: ArmCompute - GpuAcc = 2, - Undefined = 5 -}; - /// Each backend should implement an IBackend. class IBackend { @@ -113,7 +103,7 @@ protected: virtual ~IBackend() {} public: - virtual const std::string& GetId() const = 0; + virtual const BackendId& GetId() const = 0; virtual const ILayerSupport& GetLayerSupport() const = 0; }; @@ -189,4 +179,4 @@ private: /// Define LayerGuid type. using LayerGuid = unsigned int; -} +} // namespace armnn diff --git a/include/armnn/TypesUtils.hpp b/include/armnn/TypesUtils.hpp index c6db905bb9..a9755bd112 100644 --- a/include/armnn/TypesUtils.hpp +++ b/include/armnn/TypesUtils.hpp @@ -15,9 +15,9 @@ namespace armnn { -constexpr char const* GetStatusAsCString(Status compute) +constexpr char const* GetStatusAsCString(Status status) { - switch (compute) + switch (status) { case armnn::Status::Success: return "Status::Success"; case armnn::Status::Failure: return "Status::Failure"; @@ -25,17 +25,6 @@ constexpr char const* GetStatusAsCString(Status compute) } } -constexpr char const* GetComputeDeviceAsCString(Compute compute) -{ - switch (compute) - { - case armnn::Compute::CpuRef: return "CpuRef"; - case armnn::Compute::CpuAcc: return "CpuAcc"; - case armnn::Compute::GpuAcc: return "GpuAcc"; - default: return "Unknown"; - } -} - constexpr char const* GetActivationFunctionAsCString(ActivationFunction activation) { switch (activation) @@ -194,27 +183,6 @@ inline std::ostream& operator<<(std::ostream& os, Status stat) return os; } -inline std::ostream& operator<<(std::ostream& os, const std::vector<Compute>& compute) -{ - for (const Compute& comp : compute) { - os << GetComputeDeviceAsCString(comp) << " "; - } - return os; -} - -inline std::ostream& operator<<(std::ostream& os, const std::set<Compute>& compute) -{ - for (const Compute& comp : compute) { - os << GetComputeDeviceAsCString(comp) << " "; - } - return os; -} - -inline std::ostream& operator<<(std::ostream& os, const Compute& compute) -{ - os << GetComputeDeviceAsCString(compute); - return os; -} inline std::ostream & operator<<(std::ostream & os, const armnn::TensorShape & shape) { diff --git a/src/backends/BackendRegistry.cpp b/src/backends/BackendRegistry.cpp index 68336c45b9..a5e9f0e1d9 100644 --- a/src/backends/BackendRegistry.cpp +++ b/src/backends/BackendRegistry.cpp @@ -15,22 +15,22 @@ BackendRegistry& BackendRegistry::Instance() return instance; } -void BackendRegistry::Register(const std::string& name, FactoryFunction factory) +void BackendRegistry::Register(const BackendId& id, FactoryFunction factory) { - if (m_BackendFactories.count(name) > 0) + if (m_BackendFactories.count(id) > 0) { - throw InvalidArgumentException(name + " already registered as backend"); + throw InvalidArgumentException(std::string(id) + " already registered as backend"); } - m_BackendFactories[name] = factory; + m_BackendFactories[id] = factory; } -BackendRegistry::FactoryFunction BackendRegistry::GetFactory(const std::string& name) const +BackendRegistry::FactoryFunction BackendRegistry::GetFactory(const BackendId& id) const { - auto it = m_BackendFactories.find(name); + auto it = m_BackendFactories.find(id); if (it == m_BackendFactories.end()) { - throw InvalidArgumentException(name + " has no backend factory registered"); + throw InvalidArgumentException(std::string(id) + " has no backend factory registered"); } return it->second; @@ -42,4 +42,14 @@ void BackendRegistry::Swap(BackendRegistry::FactoryStorage& other) std::swap(instance.m_BackendFactories, other); } +BackendIdSet BackendRegistry::GetBackendIds() const +{ + BackendIdSet result; + for (const auto& it : m_BackendFactories) + { + result.insert(it.first); + } + return result; } + +} // namespace armnn diff --git a/src/backends/BackendRegistry.hpp b/src/backends/BackendRegistry.hpp index ff01d21715..e2c526d293 100644 --- a/src/backends/BackendRegistry.hpp +++ b/src/backends/BackendRegistry.hpp @@ -5,7 +5,6 @@ #pragma once #include <armnn/Types.hpp> -#include <string> #include <functional> #include <memory> #include <unordered_map> @@ -21,21 +20,23 @@ public: using FactoryFunction = std::function<IBackendUniquePtr()>; static BackendRegistry& Instance(); - void Register(const std::string& name, FactoryFunction factory); - FactoryFunction GetFactory(const std::string& name) const; + + void Register(const BackendId& id, FactoryFunction factory); + FactoryFunction GetFactory(const BackendId& id) const; struct Helper { - Helper(const std::string& name, FactoryFunction factory) + Helper(const BackendId& id, FactoryFunction factory) { - BackendRegistry::Instance().Register(name, factory); + BackendRegistry::Instance().Register(id, factory); } }; size_t Size() const { return m_BackendFactories.size(); } + BackendIdSet GetBackendIds() const; protected: - using FactoryStorage = std::unordered_map<std::string, FactoryFunction>; + using FactoryStorage = std::unordered_map<BackendId, FactoryFunction>; // For testing only static void Swap(FactoryStorage& other); diff --git a/src/backends/cl/ClBackend.cpp b/src/backends/cl/ClBackend.cpp index d185c15b72..840da8bda3 100644 --- a/src/backends/cl/ClBackend.cpp +++ b/src/backends/cl/ClBackend.cpp @@ -12,7 +12,7 @@ namespace armnn namespace { -static const std::string s_Id = "GpuAcc"; +static const BackendId s_Id{"GpuAcc"}; static BackendRegistry::Helper g_RegisterHelper{ s_Id, @@ -24,7 +24,7 @@ static BackendRegistry::Helper g_RegisterHelper{ } -const std::string& ClBackend::GetId() const +const BackendId& ClBackend::GetId() const { return s_Id; } diff --git a/src/backends/cl/ClBackend.hpp b/src/backends/cl/ClBackend.hpp index c43b6a6ce0..b927db4b25 100644 --- a/src/backends/cl/ClBackend.hpp +++ b/src/backends/cl/ClBackend.hpp @@ -16,7 +16,7 @@ public: ClBackend() = default; ~ClBackend() = default; - const std::string& GetId() const override; + const BackendId& GetId() const override; const ILayerSupport& GetLayerSupport() const override; diff --git a/src/backends/neon/NeonBackend.cpp b/src/backends/neon/NeonBackend.cpp index b4f1897704..e35bf7ea45 100644 --- a/src/backends/neon/NeonBackend.cpp +++ b/src/backends/neon/NeonBackend.cpp @@ -13,7 +13,7 @@ namespace armnn namespace { -static const std::string s_Id = "CpuAcc"; +static const BackendId s_Id{"CpuAcc"}; static BackendRegistry::Helper g_RegisterHelper{ s_Id, @@ -25,7 +25,7 @@ static BackendRegistry::Helper g_RegisterHelper{ } -const std::string& NeonBackend::GetId() const +const BackendId& NeonBackend::GetId() const { return s_Id; } diff --git a/src/backends/neon/NeonBackend.hpp b/src/backends/neon/NeonBackend.hpp index 5d4bd5dfcc..fa2cad13ee 100644 --- a/src/backends/neon/NeonBackend.hpp +++ b/src/backends/neon/NeonBackend.hpp @@ -16,7 +16,7 @@ public: NeonBackend() = default; ~NeonBackend() = default; - const std::string& GetId() const override; + const BackendId& GetId() const override; const ILayerSupport& GetLayerSupport() const override; diff --git a/src/backends/reference/RefBackend.cpp b/src/backends/reference/RefBackend.cpp index b671e8bca8..63eff52b75 100644 --- a/src/backends/reference/RefBackend.cpp +++ b/src/backends/reference/RefBackend.cpp @@ -12,7 +12,7 @@ namespace armnn namespace { -const std::string s_Id = "CpuRef"; +const BackendId s_Id{"CpuRef"}; static BackendRegistry::Helper s_RegisterHelper{ s_Id, @@ -24,7 +24,7 @@ static BackendRegistry::Helper s_RegisterHelper{ } -const std::string& RefBackend::GetId() const +const BackendId& RefBackend::GetId() const { return s_Id; } diff --git a/src/backends/reference/RefBackend.hpp b/src/backends/reference/RefBackend.hpp index e4a11f10c9..dcc974167d 100644 --- a/src/backends/reference/RefBackend.hpp +++ b/src/backends/reference/RefBackend.hpp @@ -16,7 +16,7 @@ public: RefBackend() = default; ~RefBackend() = default; - const std::string& GetId() const override; + const BackendId& GetId() const override; const ILayerSupport& GetLayerSupport() const override; diff --git a/src/backends/test/BackendIdTests.cpp b/src/backends/test/BackendIdTests.cpp new file mode 100644 index 0000000000..0ef0a20d7f --- /dev/null +++ b/src/backends/test/BackendIdTests.cpp @@ -0,0 +1,27 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// SPDX-License-Identifier: MIT +// +#include <boost/test/unit_test.hpp> + +#include <armnn/BackendId.hpp> +#include <armnn/Types.hpp> + +using namespace armnn; + +BOOST_AUTO_TEST_SUITE(BackendIdTests) + +BOOST_AUTO_TEST_CASE(CreateBackendIdFromCompute) +{ + BackendId fromCompute{Compute::GpuAcc}; + BOOST_TEST(fromCompute.Get() == GetComputeDeviceAsCString(Compute::GpuAcc)); +} + +BOOST_AUTO_TEST_CASE(CreateBackendIdVectorFromCompute) +{ + std::vector<BackendId> fromComputes = {Compute::GpuAcc, Compute::CpuRef}; + BOOST_TEST(fromComputes[0].Get() == GetComputeDeviceAsCString(Compute::GpuAcc)); + BOOST_TEST(fromComputes[1].Get() == GetComputeDeviceAsCString(Compute::CpuRef)); +} + +BOOST_AUTO_TEST_SUITE_END() |