tensorflow-cpu/gpu速度 cpu(3分钟)vs gpu(4秒)还是gpu快
测试代码如下:
import sys
import numpy as np
import tensorflow as tf
from datetime import datetime
device_name=sys.argv[1] # Choose device from cmd line. Options: gpu or cpu
shape=(int(sys.argv[2]), int(sys.argv[2]))
if device_name=="gpu":
device_name="/gpu:0"
else:
device_name="/cpu:0"
with tf.device(device_name):
random_matrix=tf.random_uniform(shape=shape, minval=0, maxval=1)
dot_operation=tf.matmul(random_matrix, tf.transpose(random_matrix))
sum_operation=tfuce_sum(dot_operation)
startTime=datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
result=session(sum_operation)
print(result)
# It can be hard to see the results on the terminal with lots of output -- add some newlines to improve readability.
print("
" * 5)
print("Shape:", shape, "Device:", device_name)
print("Time taken:", datetime.now() - startTime)
print("
" * 5)
详细日志:
python .\demo01.py gpu 1500
2022-07-29 23:45:53.521707: I T:\src\github ensorflow ensorflow\core\platform\cpu_feature_guard:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2022-07-29 23:45:54.093709: I T:\src\github ensorflow ensorflow\core\common_runtime\gpu\gpu_device:1392] Found device 0 with properties:
name: GeForce 940MX major: 5 minor: 0 memoryClockRate(GHz): 1.189
pciBusID: 0000:02:00.0
totalMemory: 2.00GiB freeMemory: 1.66GiB
2022-07-29 23:45:54.109284: I T:\src\github ensorflow ensorflow\core\common_runtime\gpu\gpu_device:1471] Adding visible gpu devices: 0
2022-07-29 23:45:55.044900: I T:\src\github ensorflow ensorflow\www.tisheng8.com\core\common_runtime\gpu\gpu_device:952] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-07-29 23:45:55.055221: I T:\src\github ensorflow ensorflow\core\common_runtime\gpu\gpu_device:958] 0
2022-07-29 23:45:55.061725: I T:\src\github ensorflow ensorflow\core\common_runtime\gpu\gpu_device:971] 0: N
2022-07-29 23:45:55.070524: I T:\src\github ensorflow ensorflow\core\common_runtime\gpu\gpu_device:1084] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 1422 MB memory) -> physical GPU (device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0)
Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0
2022-07-29 23:45:55.217599: I T:\src\github ensorflow ensorflow\core\common_runtime\direct_session:288] Device mapping:
/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: GeForce 940MX, pci bus id: 0000:02:00.0, compute capability: 5.0
random_uniform/RandomUniform: (RandomUniform): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.240213: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform/RandomUniform: (RandomUniform)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform/sub: (Sub): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.253903: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform/sub: (Sub)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform/mul: (Mul): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.263435: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform/mul: (Mul)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform: (Add): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.277615: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform: (Add)/job:localhost/replica:0/task:0/device:GPU:0
transpose/Rank: (Rank): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.290588: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/Rank: (Rank)/job:localhost/replica:0/task:0/device:GPU:0
transpose/sub: (Sub): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.302476: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/sub: (Sub)/job:localhost/replica:0/task:0/device:GPU:0
transpose/Range: (Range): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.312525: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/Range: (Range)/job:localhost/replica:0/task:0/device:GPU:0
transpose/sub_1: (Sub): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.325154: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/sub_1: (Sub)/job:localhost/replica:0/task:0/device:GPU:0
transpose: (Transpose): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.338301: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose: (Transpose)/job:localhost/replica:0/task:0/device:GPU:0
MatMul: (MatMul): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.349577: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] MatMul: (MatMul)/job:localhost/replica:0/task:0/device:GPU:0
Sum: (Sum): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.359293: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] Sum: (Sum)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform/shape: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.372087: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform/shape: (Const)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform/min: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.386133: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform/min: (Const)/job:localhost/replica:0/task:0/device:GPU:0
random_uniform/max: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.396248: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] random_uniform/max: (Const)/job:localhost/replica:0/task:0/device:GPU:0
transpose/sub/y: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.409642: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/sub/y: (Const)/job:localhost/replica:0/task:0/device:GPU:0
transpose/Range/start: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.423110: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/Range/start: (Const)/job:localhost/replica:0/task:0/device:GPU:0
transpose/Range/delta: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.435466: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] transpose/Range/delta: (Const)/job:localhost/replica:0/task:0/device:GPU:0
Const: (Const): /job:localhost/replica:0/task:0/device:GPU:0
2022-07-29 23:45:55.447107: I T:\src\github ensorflow ensorflow\core\common_runtime\placer:886] Const: (Const)/job:localhost/replica:0/task:0/device:GPU:0
842822460.0
cpu 花了3分钟:
Shape: (1500, 1500) Device: /cpu:0
Time taken: 0:03:09.855242
gup 花了4秒钟:
Shape: (1500, 1500) Device: /gpu:0
Time taken: 0:00:04.823442
有个警告:
Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
这个是对cpu的优化,这边用gpu 进行计算,可以忽略了。
使用gpu还是不错的。
速度超级快,4 秒就行,要是cpu的还要跑个3 分钟。
同样的安装cuda,cudnn 在 linux上面也是类似的。