You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/04/23 19:17:45 UTC
[GitHub] piiswrong closed pull request #10590: [MXNET-330] fix cached op req
piiswrong closed pull request #10590: [MXNET-330] fix cached op req
URL: https://github.com/apache/incubator-mxnet/pull/10590
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/src/imperative/cached_op.cc b/src/imperative/cached_op.cc
index 93a8bc6c54b..ce23735d1e2 100644
--- a/src/imperative/cached_op.cc
+++ b/src/imperative/cached_op.cc
@@ -474,6 +474,12 @@ void Imperative::CachedOp::Backward(
}
std::vector<OpReqType> array_reqs(arrays.size(), kWriteTo);
+ // set output reqs
+ for (size_t i = 0, j = num_forward_outputs; i < reqs.size(); ++i) {
+ if (reqs[i] == kNullOp) continue;
+ array_reqs[idx.entry_id(idx.outputs()[j++])] = reqs[i];
+ }
+ // set null reqs based on ref counts
for (size_t i = num_forward_entries; i < idx.num_node_entries(); ++i) {
if (ref_count[i] == 0) array_reqs[i] = kNullOp;
}
diff --git a/tests/python/unittest/test_gluon.py b/tests/python/unittest/test_gluon.py
index 854e6fe07f1..57e5087d424 100644
--- a/tests/python/unittest/test_gluon.py
+++ b/tests/python/unittest/test_gluon.py
@@ -902,6 +902,42 @@ def check_dropout_axes(ratio, shape, axes):
check_dropout_axes(0.25, nshape, axes = (0, 2, 3))
check_dropout_axes(0.25, nshape, axes = (1, 2, 3))
+@with_seed()
+def test_req():
+ data = mx.nd.random.uniform(shape=(1,3,224,224))
+ label = mx.nd.random.uniform(shape=(1))
+ label[:] = 1
+ loss = gluon.loss.SoftmaxCrossEntropyLoss()
+
+ net = nn.HybridSequential()
+ net1 = nn.HybridSequential()
+ net1.add(nn.Dense(4))
+ net2 = nn.HybridSequential()
+ net2.add(nn.Dense(3))
+ net2.add(nn.Dense(2))
+ net.add(net1)
+ net.add(net2)
+ net.initialize()
+
+ net.hybridize()
+
+ for v in net.collect_params().values():
+ v.grad_req = 'add'
+
+ net.collect_params().zero_grad()
+ with mx.autograd.record():
+ pred = net(data)
+ l = loss(pred, label)
+ l.backward()
+ grad = net[0][0].weight.grad().mean().asnumpy()
+ # run twice to check req = add
+ pred = net(data)
+ l = loss(pred, label)
+ l.backward()
+
+ grad_double = net[0][0].weight.grad().mean().asnumpy()
+ assert_almost_equal(grad * 2, grad_double)
+
def test_save_load():
net = mx.gluon.model_zoo.vision.get_resnet(1, 18, pretrained=True)
@@ -913,6 +949,7 @@ def test_save_load():
net.load_params('test.params')
+
if __name__ == '__main__':
import nose
nose.runmodule()
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services