Skip to content

[Bug] relay.transform.AnnotateSpans does not work on modules imported from Pytorch #8994

Description

@gilles-reservoir

Expected behavior

AnnotateSpans should work on modules imported from Pytorch.

Actual behavior

It fails, because the PyTorch importer creates variables with "." in their names. AnnotateSpans works by pretty-printing and then re-parsing a module. Unfortunately, the parser (tokenizer?) chokes on variables with "." in the name.

This makes it very difficult to debug other transformations when working with PyTorch models.

Environment

Ubuntu LTS, latest TVM main, torch 1.8.0

Steps to reproduce

class Net(nn.Module):
    def __init__(self):
        super().__init__()
        self.dense = nn.Linear(4, 13)
        
    def forward(self, x):
        return self.dense(x)

net = Net()
data = torch.randn(17, 4)
traced = torch.jit.trace(net, data).eval()

mod, params = relay.frontend.from_pytorch(traced, [('x', tuple(data.shape))])
print(mod['main'].astext())
mod = relay.transform.AnnotateSpans()(mod)

Output:

#[version = "0.0.5"]
fn (%x: Tensor[(17, 4), float32], %dense.weight: Tensor[(13, 4), float32], %dense.bias: Tensor[(13), float32]) {
  %0 = transpose(%dense.weight, axes=[1, 0]);
  %1 = transpose(%0, axes=[1, 0]);
  %2 = nn.dense(%x, %1, units=13);
  add(%2, %dense.bias)
}
error: expected a local variable found `.`
 --> GeneratedSource:115:48
     |  
 115 |  def @main(%x: Tensor[(17, 4), float32], %dense.weight: Tensor[(13, 4), float32], %dense.bias: Tensor[(13), float32]) {
     |                                                 ^                                                                      

Unfortunately it's not totally trivial to fix this because tuples use "." for element access. Maybe the PyTorch importer / all variable creation should excise "."s from variable names, but I could see that breaking scripts that currently work.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions