mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

Wrong output from operator _npi_repeats?

Open matteosal opened this issue 2 years ago • 1 comments

import mxnet as mx

data = mx.np.array([[5, 8, 7, 3, 4]])

print("**************************** repeat with spec 3")
print(mx.np.repeat(data, repeats=3, axis=1))

json = '{\"nodes\":[{\"op\":\"null\",\"name\":\"in\",\"inputs\":[]},{\"op\":\"_npi_repeats\",\"name\":\"_npi_repeats9\",\"attrs\":{\"axis\":\"1\",\"repeats\":\"(3,3,3,3,3)\"},\"inputs\":[[0,0,0]]}],\"arg_nodes\":[0],\"heads\":[[1,0,0]]}'
sym = mx.sym.fromjson(json)
cached_op = mx.ndarray.CachedOp(sym)
print("**************************** repeats with spec [3, 3, 3, 3, 3]")
print(cached_op(data))

json = '{\"nodes\":[{\"op\":\"null\",\"name\":\"in\",\"inputs\":[]},{\"op\":\"_npi_repeats\",\"name\":\"_npi_repeats9\",\"attrs\":{\"axis\":\"1\",\"repeats\":\"(2,4,3,4,2)\"},\"inputs\":[[0,0,0]]}],\"arg_nodes\":[0],\"heads\":[[1,0,0]]}'
sym = mx.sym.fromjson(json)
cached_op = mx.ndarray.CachedOp(sym)
print("**************************** repeats with spec, [2, 4, 3, 4, 2]")
print(cached_op(data))

The output of the above script is:

[17:47:50] /home/matteo/Git/mxnet-build/Build/Linux-x86-64/MKL/mxnet/src/storage/storage.cc:202: Using Pooled (Naive) StorageManager for CPU
**************************** repeat with spec 3
[[5. 5. 5. 8. 8. 8. 7. 7. 7. 3. 3. 3. 4. 4. 4.]]
[17:47:50] /home/matteo/Git/mxnet-build/Build/Linux-x86-64/MKL/mxnet/src/nnvm/legacy_json_util.cc:211: Loading symbol saved by previous version v0.8.0. Attempting to upgrade...
[17:47:50] /home/matteo/Git/mxnet-build/Build/Linux-x86-64/MKL/mxnet/src/nnvm/legacy_json_util.cc:220: Symbol successfully upgraded!
**************************** repeats with spec [3, 3, 3, 3, 3]

[[5. 8. 7. 3. 4. 5. 8. 7. 3. 4. 5. 8. 7. 3. 4.]]
<NDArray 1x15 @cpu(0)>
[17:47:50] /home/matteo/Git/mxnet-build/Build/Linux-x86-64/MKL/mxnet/src/nnvm/legacy_json_util.cc:211: Loading symbol saved by previous version v0.8.0. Attempting to upgrade...
[17:47:50] /home/matteo/Git/mxnet-build/Build/Linux-x86-64/MKL/mxnet/src/nnvm/legacy_json_util.cc:220: Symbol successfully upgraded!
**************************** repeats with spec, [2, 4, 3, 4, 2]

[[5.0000000e+00 8.0000000e+00 7.0000000e+00 3.0000000e+00 4.0000000e+00
  5.0000000e+00 8.0000000e+00 7.0000000e+00 3.0000000e+00 4.0000000e+00
  0.0000000e+00 1.9618179e-44 0.0000000e+00 7.5563101e+31 7.0366695e+22]]
<NDArray 1x15 @cpu(0)>

I would expect the output of "repeats with spec [3, 3, 3, 3, 3]" to match "repeat with spec 3", and the output of repeats with spec, [2, 4, 3, 4, 2] to be [[5, 5, 8, 8, 8, 8, 7, 7, 7, 3, 3, 3, 3, 4, 4]]. Is _npi_repeats buggy?

Side question: is there a way to access _npi_repeats without using mx.sym.fromjson?

matteosal avatar Jul 15 '22 15:07 matteosal

Hi @matteosal, it seems like calling directly npi_repeats with specific parameters is buggy, but calling it through API "np.repeat" work as expected - https://github.com/apache/incubator-mxnet/blob/cf15e0a478ea45166b2f397ff440e30601ea2ec4/python/mxnet/ndarray/numpy/_op.py#L4253

bgawrych avatar Jul 18 '22 07:07 bgawrych

Fix is here: https://github.com/apache/incubator-mxnet/pull/21112

Kacper-Pietkun avatar Aug 12 '22 08:08 Kacper-Pietkun