-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Relay] Add conv2d_backward_weight op (without topi)
#9954
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
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
efdf5a6
python plumbing
masahi 74dbb51
add cpp def
masahi 9f4242f
legalize worked
masahi 4c0a96a
clean up
masahi 75d6718
layout conversion doesnt work
masahi 7d1c839
extract wgrad body
masahi f939c27
fix convert layout
masahi c349e2a
black
masahi ed5a8dd
fix kernel size
masahi 816529c
revert irrelevant change
masahi b297d33
add doc, clarify the meanings of parameters
masahi 123f982
update layout convert
masahi f8b9273
test passed
masahi e911786
fixed layout conversion
masahi 3eb1228
update convert layout
masahi 3778c55
remove print
masahi 7096a4d
remove layout convert for now
masahi eacf283
minor fix
masahi 3f41633
removed unused import
masahi eeae52a
add wgrad python reference
masahi 339f07d
add test stub
masahi c4a6b59
add doc
masahi c77ce29
test other stride and pad
masahi 41119cd
tweak
masahi 3af609a
more pylint filter
masahi 1aad114
fix typo in doc
masahi f91e9d9
swap arg order (data, grad) to be consistent with conv2d_transpose(dg…
masahi 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
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 |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you under the Apache License, Version 2.0 (the | ||
| # "License"); you may not use this file except in compliance | ||
| # with the License. You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, | ||
| # software distributed under the License is distributed on an | ||
| # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| # KIND, either express or implied. See the License for the | ||
| # specific language governing permissions and limitations | ||
| # under the License. | ||
| # pylint: disable=invalid-name, too-many-nested-blocks | ||
| """Gradient of conv2d with respect to weight in python""" | ||
| import numpy as np | ||
|
|
||
|
|
||
| # Reference: cutlass/tools/util/include/cutlass/util/reference/host/convolution.h | ||
| def conv2d_backward_weight_nchw_python(dy_np, x_np, kernel_size, stride, padding): | ||
| """Gradient of the conv2d op with respect to weight, in NCHW layout. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| dy_np : numpy.ndarray | ||
| 4-D with shape [batch, in_channel, out_height, out_width] | ||
|
|
||
| x_np : numpy.ndarray | ||
| 4-D with shape [batch, in_channel, in_height, in_width] | ||
|
|
||
| kernel_size : tuple of two ints | ||
| Height and width of the weight | ||
|
|
||
| stride : tuple of two ints | ||
| Stride size, or [stride_height, stride_width] | ||
|
|
||
| padding : tuple of two ints | ||
| Spatial padding, or [pad_h, pad_w] | ||
|
|
||
| Returns | ||
| ------- | ||
| b_np : np.ndarray | ||
| 4-D with shape [num_filter, in_channel, filter_height, filter_width] | ||
|
|
||
| """ | ||
| N, C, H, W = x_np.shape | ||
| _, K, P, Q = dy_np.shape | ||
| R, S = kernel_size | ||
| pad_h, pad_w = padding | ||
| stride_h, stride_w = stride | ||
| dw = np.zeros((K, C, R, S)).astype(dy_np.dtype) | ||
|
|
||
| for k in range(K): | ||
| for r in range(R): | ||
| for s in range(S): | ||
| for c in range(C): | ||
| acc = 0 | ||
| for n in range(N): | ||
| for p in range(P): | ||
| for q in range(Q): | ||
| coord = (n, c, p * stride_h - pad_h + r, q * stride_w - pad_w + s) | ||
|
|
||
| if ( | ||
| coord[2] < H | ||
| and coord[2] >= 0 | ||
| and coord[3] < W | ||
| and coord[3] >= 0 | ||
| ): | ||
| acc += dy_np[n, k, p, q] * x_np[coord] | ||
|
|
||
| dw[k, c, r, s] = acc | ||
|
|
||
| return dw |
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.
Uh oh!
There was an error while loading. Please reload this page.