-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[NNAPI EP] Add EP option to disable CPU #6593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
8b54a6b
97f90ee
85e2a15
017f3a3
7b930d4
f6888e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,17 +87,23 @@ Status ModelBuilder::GetTargetDevices() { | |
| for (uint32_t i = 0; i < num_devices; i++) { | ||
| ANeuralNetworksDevice* device = nullptr; | ||
| const char* device_name = nullptr; | ||
| int32_t device_type; | ||
| RETURN_STATUS_ON_ERROR_WITH_NOTE( | ||
| nnapi_->ANeuralNetworks_getDevice(i, &device), "Getting " + std::to_string(i) + "th device"); | ||
|
|
||
| RETURN_STATUS_ON_ERROR_WITH_NOTE(nnapi_->ANeuralNetworksDevice_getName(device, &device_name), | ||
| "Getting " + std::to_string(i) + "th device's name"); | ||
|
|
||
| RETURN_STATUS_ON_ERROR_WITH_NOTE(nnapi_->ANeuralNetworksDevice_getType(device, &device_type), | ||
| "Getting " + std::to_string(i) + "th device's type"); | ||
|
|
||
| bool device_is_cpu = nnapi_cpu == device_name; | ||
| if ((target_device_option_ == TargetDeviceOption::CPU_DISABLED && !device_is_cpu) || | ||
| (target_device_option_ == TargetDeviceOption::CPU_ONLY && device_is_cpu)) { | ||
| nnapi_target_devices_.push_back(device); | ||
| LOGS_DEFAULT(VERBOSE) << "Target device [" << device_name << "] added"; | ||
| const auto device_detail = MakeString("[Name: [", device_name, "], Type [", device_type, "]], "); | ||
| nnapi_target_devices_detail_ += device_detail; | ||
| LOGS_DEFAULT(VERBOSE) << "Target device " << device_detail << " is added"; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -489,6 +495,7 @@ Status ModelBuilder::AddOperation(int op, const std::vector<uint32_t>& input_ind | |
| output_indices.size(), &output_indices[0]), | ||
| "op = " + std::to_string(op)); | ||
|
|
||
| num_nnapi_ops_++; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
|
|
@@ -515,7 +522,34 @@ Status ModelBuilder::Compile(std::unique_ptr<Model>& model) { | |
| nnapi_->ANeuralNetworksModel_finish(nnapi_model_->model_), | ||
| "on model finish"); | ||
|
|
||
| // We have a list of target devices, try to see if the model can be run entirely | ||
| // using the list of target devices | ||
| bool use_create_for_devices = false; | ||
| if (!nnapi_target_devices_.empty()) { | ||
| bool supported_ops[num_nnapi_ops_]; | ||
|
||
| RETURN_STATUS_ON_ERROR_WITH_NOTE( | ||
| nnapi_->ANeuralNetworksModel_getSupportedOperationsForDevices( | ||
| nnapi_model_->model_, nnapi_target_devices_.data(), | ||
| nnapi_target_devices_.size(), supported_ops), | ||
| "on getSupportedOperationsForDevices"); | ||
|
|
||
| auto* it = std::find(supported_ops, supported_ops + num_nnapi_ops_, false); | ||
| if (it != supported_ops + num_nnapi_ops_) { | ||
| // There are some ops not supported by the list of the target devices | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: std::any_of may be slightly clearer
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to std::all_of |
||
| // Fail the Compile | ||
| // | ||
| // TODO, add some logic to not fail for some cases | ||
| // Such as, if there are some acceptable fall back to cpu (nnapi-reference) | ||
| // and cpu is not in the target devices list | ||
| return ORT_MAKE_STATUS(ONNXRUNTIME, EP_FAIL, | ||
| "The model cannot run using current set of target devices, ", | ||
| nnapi_target_devices_detail_); | ||
| } else { | ||
| use_create_for_devices = true; | ||
| } | ||
| } | ||
|
|
||
| if (use_create_for_devices) { | ||
| RETURN_STATUS_ON_ERROR_WITH_NOTE( | ||
| nnapi_->ANeuralNetworksCompilation_createForDevices( | ||
| nnapi_model_->model_, nnapi_target_devices_.data(), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.