Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Use multi-tensor zeroing for resetting grads #19894

Merged
merged 1 commit into from
Feb 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions python/mxnet/gluon/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,18 +742,16 @@ def zero_grad(self):
if g.stype == 'row_sparse':
ndarray.zeros_like(g, out=g)
else:
arrays[g.ctx].append(g)
if is_np_array():
arrays[g.ctx].append(g.as_nd_ndarray())
else:
arrays[g.ctx].append(g)

if len(arrays) == 0:
return

if is_np_array():
for arr in arrays.values():
for ele in arr:
ele[()] = 0
else:
for arr in arrays.values():
ndarray.reset_arrays(*arr, num_arrays=len(arr))
for arr in arrays.values():
ndarray.reset_arrays(*arr, num_arrays=len(arr))

def reset_ctx(self, ctx):
"""Re-assign all Parameters to other contexts.
Expand Down