forked from tracel-ai/burn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Enum module support in PyTorchFileRecorder
Fixes tracel-ai#1431
- Loading branch information
Showing
7 changed files
with
445 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+1.72 KB
crates/burn-import/pytorch-tests/tests/enum_module/enum_depthwise_false.pt
Binary file not shown.
Binary file added
BIN
+2.23 KB
crates/burn-import/pytorch-tests/tests/enum_module/enum_depthwise_true.pt
Binary file not shown.
60 changes: 60 additions & 0 deletions
60
crates/burn-import/pytorch-tests/tests/enum_module/export_weights.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
import torch | ||
from torch import nn, Tensor | ||
|
||
class DwsConv(nn.Module): | ||
"""Depthwise separable convolution.""" | ||
|
||
def __init__(self, in_channels: int, out_channels: int, kernel_size: int) -> None: | ||
super().__init__() | ||
# Depthwise conv | ||
self.dconv = nn.Conv2d(in_channels, in_channels, kernel_size, groups=in_channels) | ||
# Pointwise conv | ||
self.pconv = nn.Conv2d(in_channels, out_channels, kernel_size=1, groups=1) | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
x = self.dconv(x) | ||
return self.pconv(x) | ||
|
||
|
||
class Model(nn.Module): | ||
def __init__(self, depthwise: bool = False) -> None: | ||
super().__init__() | ||
self.conv = DwsConv(2, 2, 3) if depthwise else nn.Conv2d(2, 2, 3) | ||
|
||
def forward(self, x: Tensor) -> Tensor: | ||
return self.conv(x) | ||
|
||
|
||
def main(): | ||
|
||
torch.set_printoptions(precision=8) | ||
torch.manual_seed(1) | ||
|
||
model = Model().to(torch.device("cpu")) | ||
|
||
torch.save(model.state_dict(), "enum_depthwise_false.pt") | ||
|
||
input = torch.rand(1, 2, 5, 5) | ||
|
||
print("Depthwise is False") | ||
print("Input shape: {}", input.shape) | ||
print("Input: {}", input) | ||
output = model(input) | ||
print("Output: {}", output) | ||
print("Output Shape: {}", output.shape) | ||
|
||
|
||
print("Depthwise is True") | ||
model = Model(depthwise=True).to(torch.device("cpu")) | ||
torch.save(model.state_dict(), "enum_depthwise_true.pt") | ||
|
||
print("Input shape: {}", input.shape) | ||
print("Input: {}", input) | ||
output = model(input) | ||
print("Output: {}", output) | ||
print("Output Shape: {}", output.shape) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.