Sadik Armagan | 8f397a1 | 2022-06-17 15:38:22 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2022 Arm Ltd and Contributors. All rights reserved. |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #define LOG_TAG "arm-armnn-sl" |
| 7 | |
| 8 | #include "ArmnnDevice.hpp" |
| 9 | |
| 10 | #include <LegacyUtils.h> |
| 11 | #include <OperationsUtils.h> |
| 12 | |
| 13 | #include <log/log.h> |
| 14 | |
| 15 | #include <memory> |
| 16 | #include <string> |
| 17 | |
| 18 | #ifdef __ANDROID__ |
| 19 | #include <android/log.h> |
| 20 | #endif |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | std::string GetBackendString(const armnn_driver::DriverOptions& options) |
| 26 | { |
| 27 | std::stringstream backends; |
| 28 | for (auto&& b : options.GetBackends()) |
| 29 | { |
| 30 | backends << b << " "; |
| 31 | } |
| 32 | return backends.str(); |
| 33 | } |
| 34 | |
| 35 | } // anonymous namespace |
| 36 | |
| 37 | namespace armnn_driver |
| 38 | { |
| 39 | |
| 40 | using namespace android::nn; |
| 41 | |
| 42 | ArmnnDevice::ArmnnDevice(DriverOptions options) |
| 43 | : m_Runtime(nullptr, nullptr) |
| 44 | , m_ClTunedParameters(nullptr) |
| 45 | , m_Options(std::move(options)) |
| 46 | { |
| 47 | // First check if the DriverOptions is happy. |
| 48 | if (options.ShouldExit()) |
| 49 | { |
| 50 | // Is this a good or bad exit? |
| 51 | if (options.GetExitCode() != EXIT_SUCCESS) |
| 52 | { |
| 53 | throw armnn::InvalidArgumentException("ArmnnDevice: Insufficient or illegal options specified."); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | throw armnn::InvalidArgumentException("ArmnnDevice: Nothing to do."); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | initVLogMask(); |
| 62 | VLOG(DRIVER) << "ArmnnDevice::ArmnnDevice()"; |
| 63 | |
| 64 | #ifdef __ANDROID__ |
| 65 | __android_log_print(ANDROID_LOG_DEBUG, "ARMNN_SL", "ArmnnDevice::ArmnnDevice()"); |
| 66 | #endif |
| 67 | |
| 68 | armnn::ConfigureLogging(false, m_Options.IsVerboseLoggingEnabled(), armnn::LogSeverity::Trace); |
| 69 | if (m_Options.IsVerboseLoggingEnabled()) |
| 70 | { |
| 71 | SetMinimumLogSeverity(android::base::VERBOSE); |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | SetMinimumLogSeverity(android::base::INFO); |
| 76 | } |
| 77 | |
| 78 | armnn::IRuntime::CreationOptions runtimeOptions; |
| 79 | |
| 80 | #if defined(ARMCOMPUTECL_ENABLED) |
| 81 | try |
| 82 | { |
| 83 | if (!m_Options.GetClTunedParametersFile().empty()) |
| 84 | { |
| 85 | m_ClTunedParameters = armnn::IGpuAccTunedParameters::Create(m_Options.GetClTunedParametersMode(), |
| 86 | m_Options.GetClTuningLevel()); |
| 87 | try |
| 88 | { |
| 89 | m_ClTunedParameters->Load(m_Options.GetClTunedParametersFile().c_str()); |
| 90 | } |
| 91 | catch (std::exception& error) |
| 92 | { |
| 93 | // This is only a warning because the file won't exist the first time you are generating it. |
| 94 | VLOG(DRIVER) << "ArmnnDevice: Failed to load CL tuned parameters file " |
| 95 | << m_Options.GetClTunedParametersFile().c_str() << " : " << error.what()); |
| 96 | } |
| 97 | runtimeOptions.m_GpuAccTunedParameters = m_ClTunedParameters; |
| 98 | } |
| 99 | } |
| 100 | catch (const armnn::ClRuntimeUnavailableException& error) |
| 101 | { |
| 102 | VLOG(DRIVER) << "ArmnnDevice: Failed to setup CL runtime: %s. Device will be unavailable." << error.what(); |
| 103 | } |
| 104 | catch (std::exception& error) |
| 105 | { |
| 106 | VLOG(DRIVER) << "ArmnnDevice: Unknown exception: %s. Device will be unavailable." << error.what(); |
| 107 | } |
| 108 | #endif |
| 109 | runtimeOptions.m_EnableGpuProfiling = m_Options.IsGpuProfilingEnabled(); |
| 110 | m_Runtime = armnn::IRuntime::Create(runtimeOptions); |
| 111 | |
| 112 | std::vector<armnn::BackendId> backends; |
| 113 | |
| 114 | if (m_Runtime) |
| 115 | { |
| 116 | const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends(); |
| 117 | for (auto &backend : m_Options.GetBackends()) |
| 118 | { |
| 119 | if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend()) |
| 120 | { |
| 121 | VLOG(DRIVER) << "ArmnnDevice: Requested unknown backend " << backend.Get().c_str(); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | backends.push_back(backend); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if (backends.empty()) |
| 131 | { |
| 132 | // No known backend specified |
| 133 | throw armnn::InvalidArgumentException("ArmnnDevice: No known backend specified."); |
| 134 | } |
| 135 | |
| 136 | m_Options.SetBackends(backends); |
| 137 | VLOG(DRIVER) << "ArmnnDevice: Created device with the following backends: " << GetBackendString(m_Options).c_str(); |
| 138 | |
| 139 | #ifdef __ANDROID__ |
| 140 | __android_log_print(ANDROID_LOG_DEBUG, |
| 141 | "ARMNN_SL", |
| 142 | "ArmnnDevice: Created device with the following backends: %s", |
| 143 | GetBackendString(m_Options).c_str()); |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | } // namespace armnn_driver |