forked from apache/tvm
-
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.
[Relay] Add
conv2d_backward_weight
op (without topi) (apache#9954)
* python plumbing * add cpp def * legalize worked * clean up * layout conversion doesnt work * extract wgrad body * fix convert layout * black * fix kernel size * revert irrelevant change * add doc, clarify the meanings of parameters * update layout convert * test passed * fixed layout conversion * update convert layout * remove print * remove layout convert for now * minor fix * removed unused import * add wgrad python reference * add test stub * add doc * test other stride and pad * tweak * more pylint filter * fix typo in doc * swap arg order (data, grad) to be consistent with conv2d_transpose(dgrad)
- Loading branch information
Showing
8 changed files
with
344 additions
and
49 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
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
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,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.