Skip to content
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

TorchScript: Argument names to match runtime #865

Closed
daodaoawaker opened this issue Jan 12, 2022 · 11 comments
Closed

TorchScript: Argument names to match runtime #865

daodaoawaker opened this issue Jan 12, 2022 · 11 comments
Assignees

Comments

@daodaoawaker
Copy link

Hi, there is some questions about node's name which in pt model saved by TorchScript. I use netron to view my pt model exported by torch.jit.save(),but the node's name doesn't match with it's real name resolved by TorchScript interface. It looks like the names in pt are arranged numerically from smallest to largest,but this is clearly not the case when they are parsed from TorchScript's interface.
I wonder how this kind of situation can be solved, thanks a lot !! Looking forward to your reply.

@lutzroeder
Copy link
Owner

lutzroeder commented Jan 12, 2022

@daodaoawaker can you share a sample file and screenshot to reproduce the issue.

@daodaoawaker
Copy link
Author

It looks like the names in pt are arranged numerically from smallest to largest, don't you think there's something wrong with that ??

@lutzroeder
Copy link
Owner

@daodaoawaker which names? Can you share an example or screenshot?

@daodaoawaker
Copy link
Author

My files related about this situation all in company,can u wait for me to upload the files after I negotiate with IT colleagues tomorrow,thanks

@daodaoawaker
Copy link
Author

b8d610f3-18d1-438a-b04f-2c2035904ee4

8f5f898a-92f5-4f58-8b3e-86067efdbcf3

Hi, @lutzroeder

As shown above, the conv-op in pt model viewed by netron whose name is number 4, but the node's name parsed by torch.jit interface is input2.1 or number 373 which generated by different interface invoked.

some of code to parse the pt model is as follows:

std::string model_path = "model.pt";                                              
auto module = torch::jit::load(model_path);            
module = torch::jit::freeze_module(module);            
auto graph = module.get_methods()[0].graph();        
auto graph_inputs = graph->inputs();         
auto graph_outputs = graph->outputs();
auto graph_node_list = graph->nodes();

for(auto it = graph_node_list.begin(); it != graph_node_list.end(); ++it) {
    torch::jit::Node* curNode = *it;
    const char* qualifiedName = curNode->kind().toQualString();  
    const char* unqualifiedName = curNode->kind().toUnqualString();
    fprintf(fp, "==> Node kind\n");
    fprintf(fp, "         %s\n", qualifiedName);
    fprintf(fp, "         %s\n", unqualifiedName);

    c10::ArrayRef<torch::jit::Value*> inputs = curNode->inputs();
    c10::ArrayRef<torch::jit::Value*> outputs = curNode->outputs();
    std::vector<torch::jit::Value*> inp_vec = inputs.vec();
    std::vector<torch::jit::Value*> oup_vec = outputs.vec();

    torch::jit::Value* out = oup_vec[0];
    std::string output_name = std::to_string(out->unique());

    fprintf(fp, "==> Node inputs: ( %d )\n", (int)inp_vec.size());
    for (int i = 0; i < inp_vec.size(); i++) {
        torch::jit::Value* v = inp_vec[i];
        fprintf(fp, "         name: %s\n", std::to_string(v->unique()).c_str());
    }

    node_output_number = (int)oup_vec.size();
    fprintf(fp, "==> Node outputs: ( %d )\n", (int)oup_vec.size());
    fprintf(fp, "         OrgNodeName: %s | DigitalNodeName: %s\n", out->debugName().c_str(), output_name.c_str());
}

@lutzroeder lutzroeder changed the title Op's name which in pt model saved by TorchScript viewed by netron does not match it's real name resolved by TorchScript TorchScript: Argument names to match runtime Jan 14, 2022
@lutzroeder lutzroeder reopened this Jan 14, 2022
@lutzroeder
Copy link
Owner

@daodaoawaker there seem to be multiple options:

  1. Using the variable names from TorchScript code
  2. Using the identifiers the runtime allocates (is there a description available how this allocation is done)

Can you help investigate this and submit a fix?

@daodaoawaker
Copy link
Author

I think the first approach seems more appropriate, while the second approach is more complex and I don't yet understand the mechanics of TorchScript‘s runtime compilation.
For the first optins, The PT model is saved with the name of the first output variable as the node name. Although this has not been verified, the pt model should have done something unique to the node name during model's saving process, so it seems reasonable to use the variable names in the TorchScript generated code directly.
For the second optins, I'm not sure if the TorchScript runtime has made any changes to this node name, which is difficult to analyze

Repository owner deleted a comment Mar 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants
@lutzroeder @daodaoawaker and others