sxjscience commented on issue #17989: [Gradient Addto] Very serious bug of grad_req='add' URL: https://github.com/apache/incubator-mxnet/issues/17989#issuecomment-610219553 Just verified that there is no problem when `ctx=mx.cpu()` . Also, I've found a simpler script to reproduce the problem: ```python import mxnet as mx import numpy as np mx.npx.set_np() for ctx in [mx.cpu(), mx.gpu()]: for nrepeat in range(1, 10): stored_grad = dict() for grad_req in ['write', 'add']: a = mx.np.array(1, ctx=ctx) b = mx.np.array(2, ctx=ctx) if grad_req == 'write': a.attach_grad(grad_req='write') elif grad_req == 'add': a.attach_grad(grad_req='add') a.grad[()] = 0 with mx.autograd.record(): for _ in range(nrepeat): b = b * a b.backward() stored_grad[grad_req] = a.grad.asnumpy() print('ctx={}, nrepeat={}, write={}, add={}'.format(ctx, nrepeat, stored_grad['write'], stored_grad['add'])) ``` Result: ``` ctx=cpu(0), nrepeat=1, write=2.0, add=2.0 ctx=cpu(0), nrepeat=2, write=4.0, add=4.0 ctx=cpu(0), nrepeat=3, write=6.0, add=6.0 ctx=cpu(0), nrepeat=4, write=8.0, add=8.0 ctx=cpu(0), nrepeat=5, write=10.0, add=10.0 ctx=cpu(0), nrepeat=6, write=12.0, add=12.0 ctx=cpu(0), nrepeat=7, write=14.0, add=14.0 ctx=cpu(0), nrepeat=8, write=16.0, add=16.0 ctx=cpu(0), nrepeat=9, write=18.0, add=18.0 ctx=gpu(0), nrepeat=1, write=2.0, add=2.0 ctx=gpu(0), nrepeat=2, write=4.0, add=4.0 ctx=gpu(0), nrepeat=3, write=6.0, add=6.0 ctx=gpu(0), nrepeat=4, write=8.0, add=8.0 ctx=gpu(0), nrepeat=5, write=10.0, add=62.0 ctx=gpu(0), nrepeat=6, write=12.0, add=126.0 ctx=gpu(0), nrepeat=7, write=14.0, add=254.0 ctx=gpu(0), nrepeat=8, write=16.0, add=16.0 ctx=gpu(0), nrepeat=9, write=18.0, add=18.0 ```
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to 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