Следующий код:
import time
import tensorflow as tf
tf.enable_eager_execution()
def time_matmul(x):
start = time.time()
for loop in range(10):
tf.matmul(x, x)
result = time.time() - start
print("10 loops: {:0.2f}ms".format(1000 * result))
# Force execution on CPU
print("On CPU:")
with tf.device("CPU:0"):
x = tf.random_uniform([1000, 1000])
assert x.device.endswith("CPU:0")
time_matmul(x)
# Force execution on GPU #0 if available
print("On GPU:")
if tf.test.is_gpu_available():
with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.
x = tf.random_uniform([1000, 1000])
assert x.device.endswith("GPU:0")
time_matmul(x)
дает следующий вывод:
On CPU:
2019-02-16 11:12:33.724828: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-02-16 11:12:34.056651: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce GTX 1070 major: 6 minor: 1 memoryClockRate(GHz): 1.7715
pciBusID: 0000:01:00.0
totalMemory: 8.00GiB freeMemory: 6.63GiB
2019-02-16 11:12:34.056984: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-16 11:12:34.501349: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-16 11:12:34.501515: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2019-02-16 11:12:34.501612: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2019-02-16 11:12:34.501855: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6384 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)
10 loops: 206.00ms
On GPU:
2019-02-16 11:12:34.718164: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-02-16 11:12:34.718377: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-02-16 11:12:34.718540: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0
2019-02-16 11:12:34.718641: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N
2019-02-16 11:12:34.718832: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/device:GPU:0 with 6384 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1070, pci bus id: 0000:01:00.0, compute capability: 6.1)
10 loops: 239.52ms
Процессор Core i7
, GPU GTX 1070
.
Что такоепроисходит?