dan*_*451 16 python vram tensorflow cudnn
TensorFlow总是(预)在我的显卡上分配所有空闲内存(VRAM),这是好的,因为我希望我的模拟在我的工作站上尽可能快地运行.
但是,我想记录TensorFlow真正使用的内存(总之).另外,如果我还可以记录单个张量器使用的内存量,那将是非常好的.
此信息对于衡量和比较不同ML/AI架构所需的内存大小非常重要.
有小费吗?
Yar*_*tov 18
更新,可以使用TensorFlow操作来查询分配器:
# maximum across all sessions and .run calls so far
sess.run(tf.contrib.memory_stats.MaxBytesInUse())
# current usage
sess.run(tf.contrib.memory_stats.BytesInUse())
Run Code Online (Sandbox Code Playgroud)
您还可以通过查看获得有关session.run
呼叫的详细信息,包括run
呼叫期间所有内存分配RunMetadata
.IE就是这样的
run_metadata = tf.RunMetadata()
sess.run(c, options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE, output_partition_graphs=True), run_metadata=run_metadata)
Run Code Online (Sandbox Code Playgroud)
这是一个端到端的例子 - 采用列向量,行向量并添加它们以获得添加矩阵:
import tensorflow as tf
no_opt = tf.OptimizerOptions(opt_level=tf.OptimizerOptions.L0,
do_common_subexpression_elimination=False,
do_function_inlining=False,
do_constant_folding=False)
config = tf.ConfigProto(graph_options=tf.GraphOptions(optimizer_options=no_opt),
log_device_placement=True, allow_soft_placement=False,
device_count={"CPU": 3},
inter_op_parallelism_threads=3,
intra_op_parallelism_threads=1)
sess = tf.Session(config=config)
with tf.device("cpu:0"):
a = tf.ones((13, 1))
with tf.device("cpu:1"):
b = tf.ones((1, 13))
with tf.device("cpu:2"):
c = a+b
sess = tf.Session(config=config)
run_metadata = tf.RunMetadata()
sess.run(c, options=tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE, output_partition_graphs=True), run_metadata=run_metadata)
with open("/tmp/run2.txt", "w") as out:
out.write(str(run_metadata))
Run Code Online (Sandbox Code Playgroud)
如果你打开run.txt
你会看到这样的消息:
node_name: "ones"
allocation_description {
requested_bytes: 52
allocator_name: "cpu"
ptr: 4322108320
}
....
node_name: "ones_1"
allocation_description {
requested_bytes: 52
allocator_name: "cpu"
ptr: 4322092992
}
...
node_name: "add"
allocation_description {
requested_bytes: 676
allocator_name: "cpu"
ptr: 4492163840
Run Code Online (Sandbox Code Playgroud)
所以在这里你可以看到a
并b
分配了52个字节(13*4),结果分配了676个字节.
归档时间: |
|
查看次数: |
7510 次 |
最近记录: |