-
Notifications
You must be signed in to change notification settings - Fork 14
Fixes for fvdb.nn.SimpleUNet
#336
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
Open
swahtz
wants to merge
4
commits into
openvdb:main
Choose a base branch
from
swahtz:simpleunet_relu
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3806340
Using torch.nn.ReLU instead of fvdb.nn.ReLU; some import cleanups
swahtz 66c2373
Switch to fvdb.relu_ for functional inplace ReLU
swahtz c3299a0
Remove 1x1 conv kmap building error
swahtz aea3572
Transposed conv fixes. Per reading PyTorch's transposed conv documen…
swahtz 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
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 |
|---|---|---|
|
|
@@ -56,41 +56,23 @@ SparseConvolutionKernelMap::forward(AutogradContext *ctx, | |
| 3, | ||
| }, | ||
| opt); | ||
| if (!transposed) { | ||
| TORCH_CHECK_VALUE(inFeatures.size(0) == sizes[0], | ||
| "The number of input features must match the number of voxels"); | ||
| TORCH_CHECK_VALUE( | ||
| kernels.dim() == 5, | ||
| std::string( | ||
| "Expected kernels to have 5 dimensions (shape (out_ch, in_ch, d, h, w)) but got ") + | ||
| std::to_string(kernels.dim()) + " dimensions"); | ||
| TORCH_CHECK_VALUE( | ||
| kernels.size(1) == inFeatures.size(1), | ||
| "Expected input channels of kernels (" + std::to_string(kernels.size(1)) + | ||
| ") to equal input channels of features: " + std::to_string(inFeatures.size(1))); | ||
| const int outC = kernels.size(0), inC = kernels.size(1); | ||
| kWidth[0] = kernels.size(2); | ||
| kWidth[1] = kernels.size(3); | ||
| kWidth[2] = kernels.size(4); | ||
| kernels = kernels.permute({2, 3, 4, 1, 0}).reshape({-1, inC, outC}).contiguous(); | ||
| } else { | ||
| TORCH_CHECK_VALUE(inFeatures.size(0) == sizes[1], | ||
| "The number of input features must match the number of voxels"); | ||
| TORCH_CHECK_VALUE( | ||
| kernels.dim() == 5, | ||
| std::string( | ||
| "Expected kernels to have 5 dimensions (shape (in_ch, out_ch, d, h, w)) but got ") + | ||
| std::to_string(kernels.dim()) + " dimensions"); | ||
| TORCH_CHECK_VALUE( | ||
| kernels.size(0) == inFeatures.size(1), | ||
| "Expected input channels of kernels (" + std::to_string(kernels.size(0)) + | ||
| ") to equal input channels of features: " + std::to_string(inFeatures.size(1))); | ||
| const int inC = kernels.size(0), outC = kernels.size(1); | ||
| kWidth[0] = kernels.size(2); | ||
| kWidth[1] = kernels.size(3); | ||
| kWidth[2] = kernels.size(4); | ||
| kernels = kernels.permute({2, 3, 4, 0, 1}).reshape({-1, inC, outC}).contiguous(); | ||
| } | ||
|
|
||
| TORCH_CHECK_VALUE(!transposed ? inFeatures.size(0) == sizes[0] : inFeatures.size(0) == sizes[1], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks backwards to me. I need to confirm what is expected in torch before agreeing that this is how it should behave. It may be that we need to switch how our transposed convolution works. |
||
| "The number of input features must match the number of voxels"); | ||
| TORCH_CHECK_VALUE( | ||
| kernels.dim() == 5, | ||
| std::string( | ||
| "Expected kernels to have 5 dimensions (shape (out_ch, in_ch, d, h, w)) but got ") + | ||
| std::to_string(kernels.dim()) + " dimensions"); | ||
| TORCH_CHECK_VALUE( | ||
| kernels.size(1) == inFeatures.size(1), | ||
| "Expected input channels of kernels (" + std::to_string(kernels.size(1)) + | ||
| ") to equal input channels of features: " + std::to_string(inFeatures.size(1))); | ||
| const int outC = kernels.size(0), inC = kernels.size(1); | ||
| kWidth[0] = kernels.size(2); | ||
| kWidth[1] = kernels.size(3); | ||
| kWidth[2] = kernels.size(4); | ||
| kernels = kernels.permute({2, 3, 4, 1, 0}).reshape({-1, inC, outC}).contiguous(); | ||
|
|
||
| // Save for backward | ||
| ctx->save_for_backward({inFeatures, kernels, nbmaps, nbsizes}); | ||
|
|
@@ -169,23 +151,13 @@ SparseConvolutionKernelMap::backward(AutogradContext *ctx, variable_list grad_ou | |
| } | ||
|
|
||
| const int outC = gradWeight.size(-1), inC = gradWeight.size(-2); | ||
| if (!transposed) { | ||
| gradWeight = gradWeight | ||
| .reshape({kWidth[2].item<int32_t>(), | ||
| kWidth[1].item<int32_t>(), | ||
| kWidth[0].item<int32_t>(), | ||
| inC, | ||
| outC}) | ||
| .permute({4, 3, 2, 1, 0}); | ||
| } else { | ||
| gradWeight = gradWeight | ||
| .reshape({kWidth[2].item<int32_t>(), | ||
| kWidth[1].item<int32_t>(), | ||
| kWidth[0].item<int32_t>(), | ||
| inC, | ||
| outC}) | ||
| .permute({3, 4, 2, 1, 0}); | ||
| } | ||
| gradWeight = gradWeight | ||
| .reshape({kWidth[2].item<int32_t>(), | ||
| kWidth[1].item<int32_t>(), | ||
| kWidth[0].item<int32_t>(), | ||
| inC, | ||
| outC}) | ||
| .permute({4, 3, 2, 1, 0}); | ||
| return { | ||
| gradInput, gradWeight, torch::Tensor(), torch::Tensor(), torch::Tensor(), torch::Tensor()}; | ||
| } | ||
|
|
||
Oops, something went wrong.
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 looks backwards to me, and counter to the intention. I need to look at the torch.convtranspose3d code more carefully, and create a testing framework that confirms the ordering. Generally speaking, we should not be reordering arguments like this, it indicates a semantic mismatch. If the transpose plans are wrong compared to pytorch, then we should fix it in the plan, not in the unet.
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.
Currently, with
source_grid=paded_grid, target_grid=grid, an exception will be thrown as if the order is incorrect. Investigating whether that's a mistake in the transposed convolution code or the ordering of the arguments, is what I was explaining in the PR description above, as to which direction to take this in. If you look at the 'topological' arguments to a PyTorch transposed conv (like stride), a stride of 2 will upsample (insert zeros in the input) in the inverse way a stride of 2 in a conv operator will downsample. So does that imply that we should order our source and target arguments to the transposed conv as if they were the source and target of the conv operator (since we'd also use stride=2, etc. to have the same meaning and not expect stride=1/2)?I'm fine with not doing this and source/target take the natural meanings, I'm just trying to determine what is the intent both of PyTorch and the state of the transposed conv kmap code. You could also read the PyTorch docs for transposed conv as redefining what 'stride' means in transposed conv as basically 'inverse stride'.