Commit f10944c9 by SWu Committed by Wuwei Lin

Fix bias_add gradient (#4516)

* Fix bias_add gradient

A change caused collapse_sum_like to reject implicit dimension
broadcasting for bias_add gradient, so switch to explicit sum reduction
on the non-bias axis dimensions.

* Lint fix
parent 6e085b40
...@@ -379,9 +379,9 @@ def log_softmax_grad(orig, grad): ...@@ -379,9 +379,9 @@ def log_softmax_grad(orig, grad):
@register_gradient("nn.bias_add") @register_gradient("nn.bias_add")
def bias_add_grad(orig, grad): def bias_add_grad(orig, grad):
"""Returns gradient of bias_add""" """Returns gradient of bias_add"""
data, bias = orig.args data = orig.args[0]
return [collapse_sum_like(grad, data), return [collapse_sum_like(grad, data),
collapse_sum_like(grad, bias)] _sum(grad, orig.attrs.axis, keepdims=False, exclude=True)]
@register_gradient("nn.dense") @register_gradient("nn.dense")
......
...@@ -110,12 +110,19 @@ def test_log_softmax_grad(): ...@@ -110,12 +110,19 @@ def test_log_softmax_grad():
check_grad(fwd_func, scale=1) check_grad(fwd_func, scale=1)
def test_bias_add_grad(): def verify_bias_add(d_shape, b_shape, axis=1):
data = relay.var("data", relay.TensorType((1, 16), "float32")) data = relay.var("data", relay.TensorType(d_shape, "float32"))
bias = relay.var("bias", relay.TensorType((16,), "float32")) bias = relay.var("bias", relay.TensorType(b_shape, "float32"))
fwd_func = relay.Function([data, bias], relay.nn.bias_add(data, bias)) fwd_func = relay.Function([data, bias], relay.nn.bias_add(data, bias, axis=axis))
check_grad(fwd_func) check_grad(fwd_func)
def test_bias_add_grad():
verify_bias_add((1, 16), (16,))
verify_bias_add((1, 8, 2, 2), (8,))
verify_bias_add((1, 2, 2, 8), (8,), 3)
verify_bias_add((4, 8), (8,))
if __name__ == "__main__": if __name__ == "__main__":
pytest.main([__file__]) pytest.main([__file__])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment