given test_leaf=[1,2,3] and mask [1,1,0], that code does
test_tensor = [1,2,0] + [0,0,3] = [1,2,3]
because the second summand was detached, test_leaf’s gradient will be zero at positions where mask is zero
note that usual adaptive optimizers like Adam won’t calculate gradient moments correctly for such parameters. There is optim.SparseAdam, but I’m not sure how to use it.