tensorflow: tf.repeat fails

Please make sure that this is a bug. As per our GitHub Policy, we only address code/doc bugs, performance issues, feature requests and build/installation issues on GitHub. tag:bug_template

System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow): Yes
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): MacOS Mojave 10.14.6
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: No
  • TensorFlow installed from (source or binary): pip
  • TensorFlow version (use command below): v2.4.0-rc4-71-g582c8d236cb 2.4.0
  • Python version: 3.*
  • Bazel version (if compiling from source): n/a
  • GCC/Compiler version (if compiling from source): n/a
  • CUDA/cuDNN version: n/a
  • GPU model and memory: no GPU

You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with:

  1. TF 1.0: python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"
  2. TF 2.0: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"

Describe the current behavior

n = 50000
many_ones = tf.cast(tf.concat([tf.ones(n), [n,]], axis = 0), 'int32')
array = tf.concat([tf.range(n), [n, ]], axis = 0)
tf.repeat(array, many_ones)

returns

InvalidArgumentError: Size 0 must be non-negative, not -1794917296 [Op:Reshape]

Describe the expected behavior np.repeat returns

array([ 0, 1, 2, …, 50000, 50000, 50000], dtype=int32)

Standalone code to reproduce the issue Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook.

the following code would work for small ns, but fail/be very slow for big ns

n = 50000
many_ones = tf.cast(tf.concat([tf.ones(n), [n,]], axis = 0), 'int32')
array = tf.concat([tf.range(n), [n, ]], axis = 0)
tf.repeat(array, many_ones)

Other info / logs Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 15 (3 by maintainers)

Most upvoted comments

For not-very-large tensors(e.g. 5M elements), tf.repeat can also have some problems.

a = tf.range(5000000)
b = tf.concat([tf.zeros(5000000-1, dtype=tf.int32),tf.constant([5000000], dtype=tf.int32)], axis=0)
c = tf.repeat(a,b)

The above code results in an OOM error:

2021-07-08 14:22:05.656061: W tensorflow/core/framework/cpu_allocator_impl.cc:81] Allocation of 25000000000000 exceeds 10% of system memory. 2021-07-08 14:22:05.656150: W tensorflow/core/framework/op_kernel.cc:1651] OP_REQUIRES failed at cwise_ops_common.cc:82 : Resource exhausted: OOM when allocating tensor with shape[5000000,5000000] and type bool on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/home/tangfangshuang/.pyenv/versions/py3_venv/lib/python3.5/site-packages/tensorflow_core/python/ops/array_ops.py”, line 4927, in repeat return repeat_with_axis(input, repeats, axis, name) File “/home/tangfangshuang/.pyenv/versions/py3_venv/lib/python3.5/site-packages/tensorflow_core/python/ops/array_ops.py”, line 4839, in repeat_with_axis mask = sequence_mask(repeats, max_repeat) File “/home/tangfangshuang/.pyenv/versions/py3_venv/lib/python3.5/site-packages/tensorflow_core/python/ops/array_ops.py”, line 3591, in sequence_mask result = row_vector < matrix File “/home/tangfangshuang/.pyenv/versions/py3_venv/lib/python3.5/site-packages/tensorflow_core/python/ops/gen_math_ops.py”, line 5376, in less _six.raise_from(_core._status_to_exception(e.code, message), None) File “<string>”, line 3, in raise_from tensorflow.python.framework.errors_impl.ResourceExhaustedError: OOM when allocating tensor with shape[5000000,5000000] and type bool on /job:localhost/replica:0/task:0/device:CPU:0 by allocator cpu [Op:Less]