Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 125 additions & 2 deletions tools/onnx_subgraph/src/lib/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::cerr << "Error opening file." << std::endl;
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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q) what if file has wrong content and there is no such ["hardware_limits"]["max_subgraph_size"] ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thanks for checking.
Without such nodes, having max_subgraph_size_json = 0, can this be correct interpretation of the json file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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();
}