mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-21 05:34:18 +08:00
Rewrite Python built-in class `super()` calls. Only non-semantic changes should be applied. - #94587 - #94588 - #94592 Also, methods with only a `super()` call are removed: ```diff class MyModule(nn.Module): - def __init__(self): - super().__init__() - def forward(self, ...): ... ``` Some cases that change the semantics should be kept unchanged. E.g.:f152a79be9/caffe2/python/net_printer.py (L184-L190)
f152a79be9/test/test_jit_fuser_te.py (L2628-L2635)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/94592 Approved by: https://github.com/ezyang, https://github.com/seemethere
29 lines
674 B
Python
29 lines
674 B
Python
# Usage: python create_dummy_model.py <name_of_the_file>
|
|
import sys
|
|
import torch
|
|
from torch import nn
|
|
|
|
|
|
class NeuralNetwork(nn.Module):
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.flatten = nn.Flatten()
|
|
self.linear_relu_stack = nn.Sequential(
|
|
nn.Linear(28 * 28, 512),
|
|
nn.ReLU(),
|
|
nn.Linear(512, 512),
|
|
nn.ReLU(),
|
|
nn.Linear(512, 10),
|
|
)
|
|
|
|
def forward(self, x):
|
|
x = self.flatten(x)
|
|
logits = self.linear_relu_stack(x)
|
|
return logits
|
|
|
|
|
|
if __name__ == '__main__':
|
|
jit_module = torch.jit.script(NeuralNetwork())
|
|
torch.jit.save(jit_module, sys.argv[1])
|