This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[MKLDNN] Independent gradients requests check with respect to weights and bias of convolution #15497
Merged
Merged
[MKLDNN] Independent gradients requests check with respect to weights and bias of convolution #15497
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
80fde0a
Independent req[kBias] and req[kWeight] check
zixuanweeei 9041993
Add UT for independent conv gradient requests
zixuanweeei a21f065
Update conv independent grad UT with no_bias enabled
zixuanweeei cde8ab8
Merge master and re-trigger CI
zixuanweeei 8b2cee4
Check req[kWeight] for avoiding unnecessary prim registration
zixuanweeei 9ca0428
Check `OpReqTpye` in CommitOutput automatically
zixuanweeei 3fda51f
Lock cudnn autotune for accurate conv output
zixuanweeei 8eedac6
Ignore independent gradients test on GPU
zixuanweeei 1f98778
Merge branch 'master' and re-trigger CI
zixuanweeei b2301ba
Trigger CI
zixuanweeei 952a2ba
Merge branch 'master' and re-trigger CI
zixuanweeei 9767772
Sets a low bar for autotuned cudnn convolution
zixuanweeei 4f41250
Merge branch 'master' and re-trigger CI
zixuanweeei 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 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 |
---|---|---|
|
@@ -1907,6 +1907,85 @@ def test_depthwise_convolution(): | |
for arr1, arr2 in zip(exe1.outputs + exe1.grad_arrays, exe2.outputs + exe2.grad_arrays): | ||
np.testing.assert_allclose(arr1.asnumpy(), arr2.asnumpy(), rtol=1e-3, atol=1e-3) | ||
|
||
|
||
@with_seed() | ||
def test_convolution_independent_gradients(): | ||
reqs = ["null", "write", "add"] | ||
var_names = ["x", "w", "b"] | ||
dims = [1, 2] | ||
num_bases = [1, 16, 64] | ||
kernel_xs = [3, 5] | ||
stride_xs = [1, 2] | ||
pad_xs = [0, 1] | ||
in_sizes = [7, 32] | ||
no_biases = [True, False] | ||
for dim, num_base, kernel_x, stride_x, pad_x , in_size, no_bias in \ | ||
itertools.product(dims, num_bases, kernel_xs, stride_xs, pad_xs, in_sizes, no_biases): | ||
# Prepare params shape | ||
kernel = (kernel_x,) * dim | ||
stride = (stride_x,) * dim | ||
pad = (pad_x,) * dim | ||
num_filter = num_base | ||
x_shape = (2, num_base) + (in_size,) * dim | ||
w_shape = (num_filter, num_base) + kernel | ||
|
||
# Symbols definition | ||
x = mx.sym.Variable('x') | ||
w = mx.sym.Variable('w') | ||
b = mx.sym.Variable('b') if not no_bias else None | ||
conv = mx.sym.Convolution(x, w, b, num_filter=num_filter, | ||
kernel=kernel, stride=stride, pad=pad, no_bias=no_bias) | ||
|
||
for req_kind in reqs: | ||
# Binding args for conv with possible dependent gradients | ||
base_args = { | ||
'x': mx.nd.random.normal(shape=x_shape), | ||
'w': mx.nd.random.normal(shape=w_shape), | ||
'b': mx.nd.random.normal(shape=(num_filter, )) if not no_bias else None} | ||
args1 = copy.deepcopy(base_args) | ||
grad1 = { | ||
'x': mx.nd.zeros(shape=x_shape), | ||
'w': mx.nd.zeros(shape=w_shape), | ||
'b': mx.nd.zeros(shape=(num_filter, )) if not no_bias else None} | ||
|
||
grad_req1 = [req_kind] * 3 | ||
grad_req1 = dict(zip(var_names, grad_req1)) | ||
|
||
ctx = default_context() | ||
exe1 = conv.bind(ctx, args1, args_grad=grad1, grad_req=grad_req1) | ||
exe1.forward(is_train=True) | ||
exe1.backward(exe1.outputs[0]) | ||
|
||
for x_req, w_req, b_req in itertools.product(reqs, repeat=3): | ||
# Binding args for conv with independent gradients | ||
args2 = copy.deepcopy(base_args) # Deepcopy the same params of `exe1` | ||
grad2 = { | ||
'x': mx.nd.zeros(shape=x_shape), | ||
'w': mx.nd.zeros(shape=w_shape), | ||
'b': mx.nd.zeros(shape=(num_filter, )) if not no_bias else None} | ||
grad_req2 = {"x": x_req, "w": w_req, "b": b_req} | ||
exe2 = conv.bind(ctx, args2, args_grad=grad2, grad_req=grad_req2) | ||
|
||
exe2.forward(is_train=True) | ||
np.testing.assert_allclose(exe1.outputs[0].asnumpy(), | ||
exe2.outputs[0].asnumpy(), rtol=1e-3, atol=1e-3) | ||
|
||
exe2.backward(exe2.outputs[0]) | ||
for var_name in var_names: | ||
if var_name == "b" and no_bias: | ||
continue | ||
if grad_req2[var_name] == "null": | ||
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. We don't have such case? 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. Yup. It is a very corner use of only requesting the gradient with respect to bias. |
||
exe2_var_grad = grad2[var_name].asnumpy() | ||
np.testing.assert_allclose(exe2_var_grad, | ||
np.zeros_like(exe2_var_grad), rtol=1e-3, atol=1e-3) | ||
if grad_req2[var_name] != grad_req1[var_name]: | ||
continue | ||
np.testing.assert_allclose(args1[var_name].asnumpy(), | ||
args2[var_name].asnumpy(), rtol=1e-3, atol=1e-3) | ||
np.testing.assert_allclose(grad1[var_name].asnumpy(), | ||
grad2[var_name].asnumpy(), rtol=1e-3, atol=1e-3) | ||
|
||
|
||
def gen_broadcast_data(idx): | ||
# Manually set test cases | ||
binary_op_data_shape = np.array( | ||
|
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.
what's the behavior of req[conv::bias]?
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.
It has the same behavior as req[kWeight]. Both of them return the operation request type (
OpReqType
) to Forward and Backward. We can use it to control the behavior of handling memory of result, like add/copy the result back to the source memory or just do nothing with them.