如何解决Tensorflow tf.matmul不利用Nvidia Tensor Cores
有人在Nvidia RTX 2080 Ti中使用Tensor Core运行tf.matmul()
吗?如果是这样,请告诉我如何。我正在使用以下版本:
tensorboard 2.2.2
tensorboard-plugin-wit 1.7.0
tensorflow 2.2.0
tensorflow-estimator 2.2.0
得到以下Python脚本:
import tensorflow as tf
import argparse
import logging
from time import perf_counter
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--number',dest='number',action='store',type=int,default=1024,help='Optional,integer')
parser.add_argument('--dtype',dest='dtype',type=str,default='float32',choices=['float64','float32','float16','bfloat16','int64','int32','int16','int8','uint64','uint32','uint16','uint8'],dtype')
args = parser.parse_args()
size = args.number;
print("Num GPUs Available: ",len(tf.config.experimental.list_physical_devices('GPU')))
tf.debugging.set_log_device_placement(True)
logging.info("Generating a %dx%d randomUniform matrix,please standby",size,size);
t1_start = perf_counter()
matrix = tf.random_uniform_initializer(minval=0,maxval=1,seed=2)(shape=[size,size],dtype=args.dtype);
t1_stop = perf_counter()
print("Generating a ","x"," matrix took: ",(t1_stop - t1_start),'s')
# Initialize cuBLAS....
temp = tf.constant([0.0,1.0,2.0,3.0],shape=[2,2],dtype="float32");
temp2 = tf.matmul(temp,temp,transpose_a=False,transpose_b=True);
t1_start = perf_counter()
result = tf.matmul(matrix,matrix,transpose_b=True);
t1_stop = perf_counter()
print("Multiplying a ",'s')
我运行了两次,每次运行都为Tensor Core设置了不同的环境变量。
编辑: 我以前使用数据类型 float32 进行了此测试。但是,tensorflow 2.3.0只能使用 float16 ,更具体地说是 CUDA_R_16F 的Tensor Core。
从tensorflow 2.3.0开始,已删除对环境变量TF_DISABLE_CUBLAS_TENSOR_OP_MATH
的使用(请参见commit)。
所以我在Tensorflow 2.2.0上使用数据类型float16重新运行了测试
张量芯已禁用:
TF_DISABLE_CUBLAS_TENSOR_OP_MATH=1
TF_ENABLE_CUBLAS_TENSOR_OP_MATH_FP32=0
TF_ENABLE_CUDNN_TENSOR_OP_MATH_FP32=0
TF_ENABLE_CUDNN_RNN_TENSOR_OP_MATH_FP32=0
NUMBER=16384
DTYPE=float16
2020-08-21 12:44:28.189777: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-08-21 12:44:32.243417: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:5a:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.545GHz coreCount: 68 deviceMemorySize: 10.76GiB deviceMemoryBandwidth: 573.69GiB/s
2020-08-21 12:44:32.243729: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-08-21 12:44:32.246376: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-08-21 12:44:32.249129: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-08-21 12:44:32.249554: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-08-21 12:44:32.252491: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-08-21 12:44:32.254002: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-08-21 12:44:32.259926: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-08-21 12:44:32.281312: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-08-21 12:44:32.282146: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA
2020-08-21 12:44:32.291868: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2600000000 Hz
2020-08-21 12:44:32.292063: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x3547ca0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-21 12:44:32.292081: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host,Default Version
2020-08-21 12:44:32.694055: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x35ba180 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-08-21 12:44:32.694078: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce RTX 2080 Ti,Compute Capability 7.5
2020-08-21 12:44:32.696876: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:5a:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.545GHz coreCount: 68 deviceMemorySize: 10.76GiB deviceMemoryBandwidth: 573.69GiB/s
2020-08-21 12:44:32.696914: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-08-21 12:44:32.696931: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-08-21 12:44:32.696947: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-08-21 12:44:32.696957: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-08-21 12:44:32.696967: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-08-21 12:44:32.696976: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-08-21 12:44:32.696985: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-08-21 12:44:32.715821: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-08-21 12:44:32.715858: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-08-21 12:44:32.718333: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-08-21 12:44:32.718348: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
2020-08-21 12:44:32.718356: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
2020-08-21 12:44:32.723419: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10204 MB memory) -> physical GPU (device: 0,name: GeForce RTX 2080 Ti,pci bus id: 0000:5a:00.0,compute capability: 7.5)
2020-08-21 12:44:32.725151: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op StatelessRandomUniform in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:41.983001: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op Sub in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:41.984023: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op Mul in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:42.230340: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op Add in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:42.231872: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:42.232362: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
Num GPUs Available: 1
Generating a 16384 x 16384 matrix took: 0.0007753130048513412 s
Multiplying a 16384 x 16384 matrix took: 0.00017198268324136734 s
已启用张量核心:
TF_DISABLE_CUBLAS_TENSOR_OP_MATH=0
TF_ENABLE_CUBLAS_TENSOR_OP_MATH_FP32=1
TF_ENABLE_CUDNN_TENSOR_OP_MATH_FP32=1
TF_ENABLE_CUDNN_RNN_TENSOR_OP_MATH_FP32=1
NUMBER=16384
DTYPE=float16
2020-08-21 12:44:28.189996: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcuda.so.1
2020-08-21 12:44:32.153926: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:62:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.545GHz coreCount: 68 deviceMemorySize: 10.76GiB deviceMemoryBandwidth: 573.69GiB/s
2020-08-21 12:44:32.155894: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-08-21 12:44:32.173061: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-08-21 12:44:32.177611: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-08-21 12:44:32.179239: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-08-21 12:44:32.184143: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-08-21 12:44:32.186975: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-08-21 12:44:32.195284: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-08-21 12:44:32.211230: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-08-21 12:44:32.212211: I tensorflow/core/platform/cpu_feature_guard.cc:143] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 AVX512F FMA
2020-08-21 12:44:32.223121: I tensorflow/core/platform/profile_utils/cpu_utils.cc:102] CPU Frequency: 2600000000 Hz
2020-08-21 12:44:32.223348: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x3547d60 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-08-21 12:44:32.223367: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): Host,Default Version
2020-08-21 12:44:32.658935: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x35ba260 initialized for platform CUDA (this does not guarantee that XLA will be used). Devices:
2020-08-21 12:44:32.658973: I tensorflow/compiler/xla/service/service.cc:176] StreamExecutor device (0): GeForce RTX 2080 Ti,Compute Capability 7.5
2020-08-21 12:44:32.660135: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1561] Found device 0 with properties:
pciBusID: 0000:62:00.0 name: GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.545GHz coreCount: 68 deviceMemorySize: 10.76GiB deviceMemoryBandwidth: 573.69GiB/s
2020-08-21 12:44:32.660175: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-08-21 12:44:32.660187: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
2020-08-21 12:44:32.660197: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcufft.so.10
2020-08-21 12:44:32.660206: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcurand.so.10
2020-08-21 12:44:32.660215: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusolver.so.10
2020-08-21 12:44:32.660225: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcusparse.so.10
2020-08-21 12:44:32.660235: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudnn.so.7
2020-08-21 12:44:32.662855: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1703] Adding visible gpu devices: 0
2020-08-21 12:44:32.662896: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcudart.so.10.1
2020-08-21 12:44:32.664538: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-08-21 12:44:32.664554: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108] 0
2020-08-21 12:44:32.664564: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1121] 0: N
2020-08-21 12:44:32.694148: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 10204 MB memory) -> physical GPU (device: 0,pci bus id: 0000:62:00.0,compute capability: 7.5)
2020-08-21 12:44:32.696836: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op StatelessRandomUniform in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:41.882224: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op Sub in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:41.883265: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op Mul in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:42.230553: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op Add in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:42.233699: I tensorflow/core/common_runtime/eager/execute.cc:501] Executing op MatMul in device /job:localhost/replica:0/task:0/device:GPU:0
2020-08-21 12:44:42.234656: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library libcublas.so.10
Num GPUs Available: 1
Generating a 16384 x 16384 matrix took: 0.0015629231929779053 s
Multiplying a 16384 x 16384 matrix took: 0.00017150864005088806 s
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。