-
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b54a6b
Add NNAPI EP option to disable CPU
guoyu-wang 97f90ee
update comments
guoyu-wang 85e2a15
Merge remote-tracking branch 'origin/master' into gwang-msft/nnapi_de…
guoyu-wang 017f3a3
Address CR comment
guoyu-wang 7b930d4
Address CR comments, update code comments
guoyu-wang f6888e7
Address CR comments
guoyu-wang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,35 @@ 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()) { | ||
| std::unique_ptr<bool[]> supported_ops_holder(new bool[num_nnapi_ops_]); | ||
| auto* supported_ops = supported_ops_holder.get(); | ||
| 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(), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.