-
Notifications
You must be signed in to change notification settings - Fork 178
[tools/onnx-subgraph] add functions for json config process #15152
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 1 commit
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 |
|---|---|---|
|
|
@@ -15,11 +15,134 @@ | |
| */ | ||
| #include "device.h" | ||
|
|
||
| void Device::GetDeviceJson(const std::string &json_path) { return; } | ||
| void Device::GetDeviceJson(const std::string &json_path) | ||
| { | ||
| Json::Reader reader; | ||
| Json::Value root; | ||
|
|
||
| // Open the JSON file in binary mode | ||
| std::ifstream in(json_path, std::ios::binary); | ||
| if (!in.is_open()) | ||
| { | ||
| std::cout << "Error opening file\n"; | ||
| exit(-1); | ||
| } | ||
|
|
||
| if (reader.parse(in, root)) | ||
| { | ||
| // Extract and set the maximum subgraph size from hardware limits | ||
| float max_subgraph_size_json = root["hardware_limits"]["max_subgraph_size"].asFloat(); | ||
|
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. Q) what if file has wrong content and there is no such
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. yes, it is a problem that I don't consider such exception cases in this PR, current I suppose there are such contents in the config.json, I will add it in next PR, thank you!
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. I have tested, if there was no such ["hardware_limits"]["max_subgraph_size"], max_subgraph_size_json =0, and no exception happened, thank you
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. OK, thanks for checking.
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. for safety using, need to check the existing by using if(root.contains("hardware_limits")), I will add it in future code |
||
| _max_subgraph_size = max_subgraph_size_json; | ||
|
|
||
| // Iterate through performance data to identify operations where NPU outperforms CPU | ||
| for (unsigned int i = 0; i < root["performance_data"].size(); i++) | ||
| { | ||
| if (root["performance_data"][i]["CPU_time"].asFloat() > | ||
| root["performance_data"][i]["NPU_time"].asFloat()) | ||
| { | ||
| _NPUPreferOp.push_back(root["performance_data"][i]["name"].asString()); | ||
| std::cout << "Performance Op: " << root["performance_data"][i]["name"].asString() | ||
| << std::endl; | ||
| } | ||
| } | ||
|
|
||
| // Iterate through and store supported NPU operations | ||
| for (int i = 0; i < int(root["NPU_supported_ops"].size()); i++) | ||
| { | ||
| if (std::find(_NPUSupportOp.begin(), _NPUSupportOp.end(), | ||
| root["NPU_supported_ops"][i].asString()) == _NPUSupportOp.end()) | ||
| { | ||
| _NPUSupportOp.push_back(root["NPU_supported_ops"][i].asString()); | ||
| std::cout << "NPU Supported: " << root["NPU_supported_ops"][i].asString() << std::endl; | ||
| } | ||
| } | ||
|
|
||
| // Iterate through and store supported CPU operations | ||
| for (int i = 0; i < int(root["CPU_supported_ops"].size()); i++) | ||
| { | ||
| if (std::find(_CPUSupportOp.begin(), _CPUSupportOp.end(), | ||
| root["CPU_supported_ops"][i].asString()) == _CPUSupportOp.end()) | ||
| { | ||
| _CPUSupportOp.push_back(root["CPU_supported_ops"][i].asString()); | ||
| std::cout << "CPU Supported: " << root["CPU_supported_ops"][i].asString() << std::endl; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void Device::GenerateCutInstruction(std::vector<onnx::GraphProto> &Subgraphs, std::string device, | ||
| std::vector<std::unordered_set<NodeTensor>> &subgraphs_inputs, | ||
| std::vector<std::unordered_set<NodeTensor>> &subgraphs_outputs) | ||
| { | ||
| return; | ||
| std::cout << "Generate Cut Instruction for Target_NPU" << std::endl; | ||
| // open file | ||
| std::string file_name = device + "CutInstruction.txt"; | ||
| std::ofstream outFile(file_name); | ||
|
|
||
| if (!outFile.is_open()) | ||
| { | ||
| std::cerr << "Error opening file." << std::endl; | ||
| exit(-1); | ||
| } | ||
|
|
||
| for (size_t i = 0; i < Subgraphs.size(); i++) | ||
| { | ||
| // default parameters | ||
| std::string modelFile = _onnxFile; | ||
|
|
||
| std::unordered_set<NodeTensor> graphInputs = subgraphs_inputs[i]; | ||
| std::unordered_set<NodeTensor> graphOutputs = subgraphs_outputs[i]; | ||
|
|
||
| std::string inputName = "\""; | ||
| for (const auto &input : graphInputs) | ||
| { | ||
| inputName = inputName + input.name + ";"; | ||
| } | ||
|
|
||
| // delete last semicolon | ||
| if (!inputName.empty() && inputName.back() == ';') | ||
| { | ||
| inputName.pop_back(); | ||
| } | ||
|
|
||
| inputName = inputName + "\""; | ||
| std::string outputName = "\""; | ||
|
|
||
| for (const auto &output : graphOutputs) | ||
| { | ||
| outputName = outputName + output.name + ";"; | ||
| } | ||
|
|
||
| // delete last semicolon | ||
| if (!outputName.empty() && outputName.back() == ';') | ||
| { | ||
| outputName.pop_back(); | ||
| } | ||
| outputName = outputName + "\""; | ||
|
|
||
| std::string inputShape = "\""; | ||
| for (const auto &input : graphInputs) | ||
| { | ||
| for (const auto &dim : input.shape) | ||
| { | ||
| inputShape = inputShape + std::to_string(dim) + " "; | ||
| } | ||
|
|
||
| // delete last space | ||
| if (!inputShape.empty() && inputShape.back() == ' ') | ||
| { | ||
| inputShape.pop_back(); | ||
| } | ||
| inputShape = inputShape + ";"; | ||
| } | ||
|
|
||
| // delete last semicolon | ||
| if (!inputShape.empty() && inputShape.back() == ';') | ||
| { | ||
| inputShape.pop_back(); | ||
| } | ||
| inputShape = inputShape + "\""; | ||
| } | ||
|
|
||
| outFile.close(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
below code used this
maybe use single convention