Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the value of a `tf.constant()` stored multiple times in memory in TensorFlow?

I read that (in TensorFlow):

the value of a tf.constant() is stored multiple times in memory.

Why is the value of a tf.constant() stored multiple times in memory?

like image 236
Franck Dernoncourt Avatar asked Feb 24 '17 23:02

Franck Dernoncourt


People also ask

What is TF constant in TensorFlow?

tf. constant is useful for asserting that the value can be embedded that way. If the argument dtype is not specified, then the type is inferred from the type of value . # Constant 1-D Tensor from a python list.

What is the difference between TF constant and TF variable?

In TensorFlow the differences between constants and variables are that when you declare some constant, its value can't be changed in the future (also the initialization should be with a value, not with operation). Nevertheless, when you declare a Variable, you can change its value in the future with tf.

Is TF constant immutable?

On the other hand, tf. constant is immutable, meaning that once you define it you can't change its value.

Which one of the following would you use to initialize a constant in TensorFlow?

In TensorFlow, constants are created using the function constant, which has the signature constant(value, dtype=None, shape=None, name='Const', verify_shape=False) , where value is an actual constant value which will be used in further computation, dtype is the data type parameter (e.g., float32/64, int8/16, etc.), ...


2 Answers

Because data for a constant tensor is embedded into graph definition. This means this data is stored both in the client, which maintains the graph definition, and in the runtime, which allocates it's own memory for all tensors.

IE, try

a = tf.constant([1,2])
tf.get_default_graph().as_graph_def()

You'll see

    dtype: DT_INT32
    tensor_shape {
      dim {
        size: 2
      }
    }
    tensor_content: "\001\000\000\000\002\000\000\000"
  }

The tensor_content field is the raw content, same as np.array([1,2], dtype=np.int32).tobytes().

Now, to see the runtime allocation, you can run with export TF_CPP_MIN_LOG_LEVEL=1.

If you evaluate anything using a you'll see something like this

2017-02-24 16:13:58: I tensorflow/core/framework/log_memory.cc:35] __LOG_MEMORY__ MemoryLogTensorOutput { step_id: 1 kernel_name: "Const_1/_1" tensor { dtype: DT_INT32 shape { dim { size: 2 } } allocation_description { requested_bytes: 8 allocated_bytes: 256 allocator_name: "cuda_host_bfc" allocation_id: 1 ptr: 8605532160 } } }

This means the runtime asked to allocate 8 bytes, and TF actually allocated 256 bytes. (the choices on how much data to actually allocate are somewhat arbitrary at the moment - bfc_allocator.cc )

Having constants embedded in the graph makes it easier to do some graph-based optimizations like constant folding . But this also means that large constants are inefficient. Also, using large constants is a common cause of exceeding 2GB limit for size of graph.

like image 147
Yaroslav Bulatov Avatar answered Nov 14 '22 22:11

Yaroslav Bulatov


They are referring to the fact that when initializing the constant one copy of the constant is stored as a numpy array and another copy is stored in tensorflow. The two copies exist while it is initializing the constant.

like image 29
Aaron Avatar answered Nov 14 '22 22:11

Aaron