tensorflow: AttributeError: 'TensorArray' object has no attribute 'mark_used' with tf.function

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): colab
  • TensorFlow version (use command below): v2.3.0-54-gfcc4b966f1 2.3.1

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

Describe the current behavior

import tensorflow as tf
import tensorflow_probability as tfp
tfd = tfp.distributions 

class Test(tf.Module):
  def __init__(self):
    self.log_likes_list = None
    self.i = tf.constant(0)

  @tf.function
  def __call__(self, samples):

    @tf.function
    def rnd():
        return tfd.Normal(0,1).sample()+ tfd.Normal(3,1).sample()
      
    if self.log_likes_list is None:
        self.log_likes_list = tf.TensorArray(tf.float32, size=samples) 

    def cond(x,i):
        return tf.less(i, samples) 

    def body(x,i):
        #x=x.write(i,tfm.reduce_sum(tfd.Normal(rnd(), 1).log_prob(0.4)))
        # AttributeError: 'TensorArray' object has no attribute 'mark_used'
        x.write(i,tfm.reduce_sum(tfd.Normal(rnd(), 1).log_prob(0.4))).mark_used()
        return x, i+1 

    self.log_likes_list, i = tf.while_loop(cond, body, [self.log_likes_list, self.i])

    self.log_likes = self.log_likes_list.stack()

    self.log_like = tfm.reduce_mean(self.log_likes)

    loss = self.log_like

    return loss


T= Test()
t= T(5)

T.log_likes, T.log_like, 
# the code in below is not running 
T.log_likes_list.stack()

Describe the expected behavior How can get values in t.log_likes, and t.log_like, and t.log_likes_list?

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. https://colab.research.google.com/drive/1ddlIXUBOHd5umTaCW-xghWk3bTOIKhVf#scrollTo=-yj19j0eC_cj

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 6 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

@cheshire FYI

Sorry for the delay. I think the warning about mark_used is quite misleading, we’ll need to fix it. You almost never want to use it. The warning itself was meant to caution that you should always assign the result of x.write back to x. It’s similar to Pandas, if you’re familiar with it.

There is another limitation of tf.function here: when you want to pass values outside the tf.function, you must either return them, or use tf.Variable. Assigning to self.property won’t work properly. This is something we intend to fix, and should have a warning message, but in the mean time, a workaround is to use Variables.

I rewrote your code to get it to run without error, see below. A few more notes:

  • Since tf.Variable cannot change shape, I used padding for self.log_likes
  • I also took the liberty to write the tf.while_loop in a shorter form since the function is autographed
  • I recommend calling the function with a Tensor (that is, tf.constant(5) instead of 5) to avoid unwanted retracing
  • I turned jit_compile to False since it seems to reveal a bug; I think that will be fixed soon, and I do recommend turning it on when possible because it helps performance
  • rnd doesn’t need a separate tf.function - as long as it’s called inside one it will be compiled
class Test(tf.Module):
  def __init__(self, max_samples=100):
    # Variables to store things across tf.function boundaries (that are not returns/args)
    self.i = tf.Variable(0)
    self.max_samples = max_samples
    self.log_likes = tf.Variable(tf.zeros([max_samples]))
    self.log_like = tf.Variable(0.0)

  @tf.function  # Should be able to use jit_compile=True soon, once a shape bug is fixed
  def __call__(self, samples):

    def rnd():
        return tfd.Normal(0,1).sample()+ tfd.Normal(3,1).sample()

    # Read things from outside tf.function
    x = tf.TensorArray(tf.float32, size=samples)
    i = self.i.read_value()

    x = x.unstack(self.log_likes.read_value())
    while i < samples:
      # Always assign back to `x` after `x.write`
      x = x.write(i, tfm.reduce_sum(tfd.Normal(rnd(), 1).log_prob(0.4)))
      i += 1

    log_likes = x.stack()
    log_like = tfm.reduce_mean(log_likes)
    loss = tfm.reduce_mean(log_like)

    # Put things back
    self.i.assign(i)
    self.log_likes.assign(tf.pad(log_likes, [[0, self.max_samples - samples]]))
    self.log_like.assign(log_like)

    return loss

T= Test()
# Use a Tensor for samples, otherwise you might get many retraces.
t= T(tf.constant(5))