sony / nnabla

Neural Network Libraries
https://nnabla.org/
Apache License 2.0
2.73k stars 334 forks source link

Fix the timing of fill and zero evaluations in narrowed array #1125

Closed TakuyaNarihira closed 2 years ago

TakuyaNarihira commented 2 years ago

This PR fixes a bug where SyncedArray::cast with write_only == true returns array before fill and zero evaluations for a narrowed array.

import numpy as np
import nnabla as nn
import nnabla.functions as F

size = 4
a = nn.NdArray((size,))
a.fill(3)
b = nn.NdArray((size,))

c = b.narrow(0, 0, size)
# F.identity uses `write_only == true` casting for the destination array
F.identity(a, outputs=[c])

# Before fix: c == [0, 0, 0, 0]
# After fix:  c == [3, 3, 3, 3]
np.testing.assert_equal(c.get_data(mode='r'), a.get_data())