-
Notifications
You must be signed in to change notification settings - Fork 11
Add block sparse linear and locally connected layers #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
clane9
wants to merge
9
commits into
main
Choose a base branch
from
blocksparse
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d0ec304
Initial blocksparse linear outline
clane9 bc7e748
BlockSparseLinear complete
alismil 4489743
added MixtureLayerNorm
alismil d320747
matched MixtureLinear to main
alismil f38640d
completed BlockSparseLocallyConnected
alismil 6356947
updated comments in _create_connectivity_matrix
alismil f6581f4
Update blocksparse linear implementation
clane9 b32e1fa
Debug block sparse linear
clane9 b5b9a02
Add `in_shape="nd"` option for no rearranging
clane9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,54 @@ | ||
| import pytest | ||
|
|
||
| import logging | ||
| import torch | ||
| from torch import nn | ||
|
|
||
| import columnformers.models.layers as L | ||
|
|
||
|
|
||
| @pytest.mark.skipif(not torch.cuda.is_available(), reason="cuda not available") | ||
| def test_block_sparse_locally_connected(): | ||
| torch.manual_seed(42) | ||
| device = torch.device("cuda") | ||
|
|
||
| loc = L.BlockSparseLocallyConnected( | ||
| in_channels=8, | ||
| out_channels=16, | ||
| kernel_size=3, | ||
| height=16, | ||
| depthwise=False, | ||
| ) | ||
| logging.info("%s", loc) | ||
|
|
||
| conv = nn.Conv2d( | ||
| in_channels=8, | ||
| out_channels=16, | ||
| kernel_size=3, | ||
| stride=1, | ||
| padding="same", | ||
| ) | ||
|
|
||
| nn.init.ones_(loc.bsl.weight) | ||
| nn.init.zeros_(loc.bsl.bias) | ||
| nn.init.ones_(conv.weight) | ||
| nn.init.zeros_(conv.bias) | ||
|
|
||
| loc = loc.to(device) | ||
| conv = conv.to(device) | ||
|
|
||
| input = torch.randn((2, 8, 16, 16), device=device) | ||
| input_loc = input.clone().requires_grad_(True) | ||
| input_conv = input.clone().requires_grad_(True) | ||
|
|
||
| output_loc = loc(input_loc) | ||
| output_conv = conv(input_conv) | ||
| assert torch.allclose(output_loc, output_conv, rtol=1e-4) | ||
|
|
||
| loss_loc = (output_loc**2).mean() | ||
| loss_conv = (output_conv**2).mean() | ||
| loss_loc.backward() | ||
| loss_conv.backward() | ||
| grad_loc = input_loc.grad.data | ||
| grad_conv = input_conv.grad.data | ||
| assert torch.allclose(grad_loc, grad_conv, rtol=1e-4) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit restrictive, can we adapt this to also include even kernel size by doing something like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I think you're right, something like this would probably be better. Although it feels like it should be possible to make the code shorter.
More generally, it would probably be best to have exactly the same interface and behavior as Conv2d. What I have now takes a few shortcuts.