Bug fix for named nn.Sequential in pytorch parser
Created by: JanFSchulte
Parsing of nn.Sequentials that are named members of a model class results in a naming convention for the tensors in the state_dict
of the model different from what the parser expects, since it was so far tested only on unnamed nn.Sequentials. This PR catches this and adjusts the name of the tensors we are importing from the state_dict
accordingly. A test is added to ensure that we keep parsing both cases successfully.
Type of change
For a new feature or function, please create an issue first to discuss it with us before submitting a pull request.
Note: Please delete options that are not relevant.
-
Bug fix (non-breaking change that fixes an issue)
Tests
To reproduce, this will fail with this PR:
import torch.nn as nn
from hls4ml.converters import convert_from_pytorch_model
from hls4ml.utils.config import config_from_pytorch_model
#simple model with namend sequential
class SeqModel(nn.Module):
def __init__(self):
super().__init__()
self.layer = nn.Sequential(
nn.Conv2d(1,20,5),
nn.ReLU(),
nn.Conv2d(20,64,5),
nn.ReLU()
)
def forward(self, x):
output = self.layer(x)
return output
model = SeqModel()
config = config_from_pytorch_model(model)
output_dir = 'test_pytorch'
convert_from_pytorch_model(
model, (None, 1, 5, 5), hls_config=config, output_dir=output_dir)
pytests have been added to verify that this keeps working.
Checklist
-
I have read the guidelines for contributing. -
I have commented my code, particularly in hard-to-understand areas. -
My changes generate no new warnings. -
I have installed and run pre-commit
on the files I edited or added. -
I have added tests that prove my fix is effective or that my feature works.