ThomasDelteil commented on a change in pull request #11320: Static alloc for hybridblock URL: https://github.com/apache/incubator-mxnet/pull/11320#discussion_r195918147
########## File path: tests/python/unittest/test_gluon.py ########## @@ -1137,6 +1137,60 @@ def test_zero_grad(): grad = net.collect_params()['test_zero_grad_weight'].grad() assert_almost_equal(grad.asnumpy(), grad.asnumpy() * 0) +def check_hybrid_static_memory(**kwargs): + x = mx.nd.random.uniform(shape=(2, 3, 32, 32)) + x.attach_grad() + + net1 = gluon.model_zoo.vision.get_resnet( + 1, 18, pretrained=True, prefix='net_', ctx=mx.context.current_context()) + net2 = gluon.model_zoo.vision.get_resnet( + 1, 18, pretrained=True, prefix='net_', ctx=mx.context.current_context()) + net2.hybridize(**kwargs) + net1(x) + net2(x) + + def test(net, x): + with mx.autograd.record(): + y = net(x) + net(x) + y.backward() + + grads = {k: v.grad() for k, v in net.collect_params().items() if v.grad_req != 'null'} + + return y, grads + + y1, grads1 = test(net1, x) + y2, grads2 = test(net2, x) + + assert_almost_equal(y1.asnumpy(), y2.asnumpy(), rtol=1e-3, atol=1e-5) + for key in grads1: + assert_almost_equal(grads1[key].asnumpy(), grads2[key].asnumpy(), rtol=1e-3, atol=1e-5) + +def test_hybrid_static_memory(): + check_hybrid_static_memory() + check_hybrid_static_memory(static_alloc=True) + check_hybrid_static_memory(static_alloc=True, static_shape=True) + +def check_hybrid_static_memory_switching(**kwargs): + net = gluon.model_zoo.vision.get_resnet( + 1, 18, pretrained=True, ctx=mx.context.current_context()) + net.hybridize(**kwargs) + + x = mx.nd.random.uniform(shape=(4, 3, 32, 32)) + net(x) + with mx.autograd.record(): + y = net(x) + y.backward() + x = mx.nd.random.uniform(shape=(2, 3, 32, 32)) + net(x) + with mx.autograd.record(): + y = net(x) + y.backward() + mx.nd.waitall() Review comment: @anirudh2290 do you think this could have been the cause for the flakiness of `test_hybrid_static_memory_switching` as reported here https://github.com/apache/incubator-mxnet/issues/11171? ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services