-
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.
Merge pull request #16 from kengz/compact
feat: add compact spec
- Loading branch information
Showing
14 changed files
with
636 additions
and
11 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import pytest | ||
import torch | ||
|
||
import torcharc | ||
|
||
B = 4 # batch size | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"spec_file, input_shape, output_shape", | ||
[ | ||
("mlp.yaml", (B, 128), (B, 16)), | ||
("mlp_classifier.yaml", (B, 128), (B, 10)), | ||
("conv.yaml", (B, 3, 32, 32), (B, 64, 26, 26)), | ||
("conv_classifier.yaml", (B, 3, 32, 32), (B, 10)), | ||
], | ||
) | ||
def test_model(spec_file, input_shape, output_shape): | ||
# Build the model using torcharc | ||
model = torcharc.build(torcharc.SPEC_DIR / "compact" / spec_file) | ||
assert isinstance(model, torch.nn.Module) | ||
|
||
# Run the model and check the output shape | ||
x = torch.randn(*input_shape) | ||
y = model(x) | ||
assert y.shape == output_shape | ||
|
||
# Test compatibility with compile, script and trace | ||
compiled_model = torch.compile(model) | ||
assert compiled_model(x).shape == y.shape | ||
scripted_model = torch.jit.script(model) | ||
assert scripted_model(x).shape == y.shape | ||
traced_model = torch.jit.trace(model, (x)) | ||
assert traced_model(x).shape == y.shape |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# modules: | ||
# conv: | ||
# Sequential: | ||
# - LazyBatchNorm2d: | ||
# - LazyConv2d: | ||
# out_channels: 16 | ||
# kernel_size: 2 | ||
# - ReLU: | ||
# - Dropout: | ||
# p: 0.1 | ||
# - LazyBatchNorm2d: | ||
# - LazyConv2d: | ||
# out_channels: 32 | ||
# kernel_size: 3 | ||
# - ReLU: | ||
# - Dropout: | ||
# p: 0.1 | ||
# - LazyBatchNorm2d: | ||
# - LazyConv2d: | ||
# out_channels: 64 | ||
# kernel_size: 4 | ||
# - ReLU: | ||
# - Dropout: | ||
# p: 0.1 | ||
|
||
# the above is equivalent to the compact spec below | ||
|
||
modules: | ||
conv: | ||
compact: | ||
prelayer: | ||
- LazyBatchNorm2d: | ||
layer: | ||
type: LazyConv2d | ||
keys: [out_channels, kernel_size] | ||
args: [[16, 2], [32, 3], [64, 4]] | ||
postlayer: | ||
- ReLU: | ||
- Dropout: | ||
p: 0.1 | ||
|
||
graph: | ||
input: image | ||
modules: | ||
conv: [image] | ||
output: conv |
Oops, something went wrong.