diff options
author | Michael Butler <butlermichael@google.com> | 2018-01-23 11:05:43 -0800 |
---|---|---|
committer | Michael Butler <butlermichael@google.com> | 2018-01-30 16:23:47 -0800 |
commit | 75886e77f9ca074173a49283b5c0a8c182d98977 (patch) | |
tree | 1532a1bcdeb2b3ebbbaa63243160ac24d092410b /nn/runtime/Manager.cpp | |
parent | 79b08d60b29db0a6729e7f30737fd7cf9a4b9c82 (diff) | |
download | ml-75886e77f9ca074173a49283b5c0a8c182d98977.tar.gz |
Upgrade NeuralNetworks v1.0 to v1.1.
For Android P, the NNAPI is getting new operations. To support this
change, the NN runtime needs to handle both v1.0 and v1.1. This change
brings the NN runtime up to v1.1 and adds a VersionedIDevice class which
manages both v1.0 and v1.1 calls.
Test: mm
Test: cts and vts test binaries
Change-Id: Iaec1dcfe00c1712a0ac89954ae142d4ea126989d
Diffstat (limited to 'nn/runtime/Manager.cpp')
-rw-r--r-- | nn/runtime/Manager.cpp | 84 |
1 files changed, 37 insertions, 47 deletions
diff --git a/nn/runtime/Manager.cpp b/nn/runtime/Manager.cpp index 0820f61dc..8b629eb17 100644 --- a/nn/runtime/Manager.cpp +++ b/nn/runtime/Manager.cpp @@ -30,6 +30,9 @@ namespace android { namespace nn { +Device::Device(std::string name, const sp<V1_0::IDevice>& device) : + mName(std::move(name)), mInterface(device) {} + // TODO: handle errors from initialize correctly bool Device::initialize() { #ifdef NN_DEBUGGABLE @@ -39,61 +42,49 @@ bool Device::initialize() { (mName.substr(0, sizeof(samplePrefix) - 1) == samplePrefix) ? getProp("debug.nn.sample.supported") : 0; #endif // NN_DEBUGGABLE - bool success = false; - auto ret = mInterface->getCapabilities([&](ErrorStatus status, - const Capabilities& capabilities) { - if (status != ErrorStatus::NONE) { - LOG(ERROR) << "IDevice::getCapabilities returned the error " << toString(status); - } else { - VLOG(MANAGER) << "Capab " << capabilities.float32Performance.execTime; - VLOG(MANAGER) << "Capab " << capabilities.quantized8Performance.execTime; - mFloat32Performance = capabilities.float32Performance; - mQuantized8Performance = capabilities.quantized8Performance; - success = true; - } - }); - if (!ret.isOk()) { - LOG(ERROR) << "IDevice::getCapabilities failed for " << getName() - << ": " << ret.description(); + + ErrorStatus status = ErrorStatus::GENERAL_FAILURE; + Capabilities capabilities; + std::tie(status, capabilities) = mInterface.getCapabilities(); + + if (status != ErrorStatus::NONE) { + LOG(ERROR) << "IDevice::getCapabilities returned the error " << toString(status); + } else { + VLOG(MANAGER) << "Capab " << capabilities.float32Performance.execTime; + VLOG(MANAGER) << "Capab " << capabilities.quantized8Performance.execTime; + mFloat32Performance = capabilities.float32Performance; + mQuantized8Performance = capabilities.quantized8Performance; } - return success; + + return status == ErrorStatus::NONE; } void Device::getSupportedOperations(const Model& hidlModel, - hidl_vec<bool>* outSupportedOperations) const { - bool success = false; - auto queryDriver = [outSupportedOperations, &success, - &hidlModel](ErrorStatus status, const hidl_vec<bool>& supportedOperations) { - if (status != ErrorStatus::NONE) { - LOG(ERROR) << "IDevice::getSupportedOperations returned the error " << toString(status); - return; - } - if (supportedOperations.size() != hidlModel.operations.size()) { - LOG(ERROR) << "IDevice::getSupportedOperations returned a vector of length " - << supportedOperations.size() << " when expecting " - << hidlModel.operations.size(); - return; - } - success = true; - *outSupportedOperations = supportedOperations; - }; - + hidl_vec<bool>* outSupportedOperations) { // Query the driver for what it can do. - auto ret = mInterface->getSupportedOperations(hidlModel, queryDriver); + ErrorStatus status = ErrorStatus::GENERAL_FAILURE; + hidl_vec<bool> supportedOperations; + std::tie(status, supportedOperations) = mInterface.getSupportedOperations(hidlModel); - if (!success) { + if (status != ErrorStatus::NONE) { + LOG(ERROR) << "IDevice::getSupportedOperations returned the error " << toString(status); + // Set the supported operation vectors to all false, so we won't use this driver. + outSupportedOperations->resize(hidlModel.operations.size()); + std::fill(outSupportedOperations->begin(), outSupportedOperations->end(), false); + return; + } + if (supportedOperations.size() != hidlModel.operations.size()) { + LOG(ERROR) << "IDevice::getSupportedOperations returned a vector of length " + << supportedOperations.size() << " when expecting " + << hidlModel.operations.size(); // Set the supported operation vectors to all false, so we won't use this driver. outSupportedOperations->resize(hidlModel.operations.size()); std::fill(outSupportedOperations->begin(), outSupportedOperations->end(), false); - - // If the failure is in the call, log the error. - if (!ret.isOk()) { - LOG(ERROR) << "IDevice::getSupportedOperations failed for " << getName() << ": " - << ret.description(); - } return; } + *outSupportedOperations = supportedOperations; + #ifdef NN_DEBUGGABLE if (mSupported != 1) { return; @@ -138,7 +129,6 @@ DeviceManager* DeviceManager::get() { } void DeviceManager::findAvailableDevices() { - using ::android::hardware::neuralnetworks::V1_0::IDevice; using ::android::hidl::manager::V1_0::IServiceManager; VLOG(MANAGER) << "findAvailableDevices"; @@ -148,10 +138,10 @@ void DeviceManager::findAvailableDevices() { return; } - manager->listByInterface(IDevice::descriptor, [this](const hidl_vec<hidl_string>& names) { + manager->listByInterface(V1_0::IDevice::descriptor, [this](const hidl_vec<hidl_string>& names) { for (const auto& name : names) { VLOG(MANAGER) << "Found interface " << name.c_str(); - sp<IDevice> device = IDevice::getService(name); + sp<V1_0::IDevice> device = V1_0::IDevice::getService(name); if (device == nullptr) { LOG(ERROR) << "Got a null IDEVICE for " << name.c_str(); continue; @@ -161,7 +151,7 @@ void DeviceManager::findAvailableDevices() { }); } -void DeviceManager::registerDevice(const char* name, const sp<IDevice>& device) { +void DeviceManager::registerDevice(const char* name, const sp<V1_0::IDevice>& device) { auto d = std::make_shared<Device>(name, device); if (d->initialize()) { mDevices.push_back(d); |