Skip to content

Commit fd9d6fd

Browse files
authored
[cherry-pick]Add tf32 support for A100 tensor core acceleration for cuBLAS (#28732) (#30612)
* Add tf32 support for A100 tensor core acceleration for cuBLAS (#28732) * Fixed an error * Fixed an error
1 parent 228c1d7 commit fd9d6fd

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

paddle/fluid/platform/cuda_helper.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,13 @@ class CublasHandleHolder {
8484
if (math_type == CUBLAS_TENSOR_OP_MATH) {
8585
PADDLE_RETRY_CUDA_SUCCESS(
8686
dynload::cublasSetMathMode(handle_, CUBLAS_TENSOR_OP_MATH));
87+
#if CUDA_VERSION >= 11000
88+
} else if (math_type == CUBLAS_TF32_TENSOR_OP_MATH) {
89+
PADDLE_ENFORCE_CUDA_SUCCESS(
90+
dynload::cublasSetMathMode(handle_, CUBLAS_TF32_TENSOR_OP_MATH));
91+
#endif // CUDA_VERSION >= 11000
8792
}
88-
#endif
93+
#endif // CUDA_VERSION >= 9000
8994
}
9095

9196
~CublasHandleHolder() PADDLE_MAY_THROW {

paddle/fluid/platform/device_context.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ class CUDAContext {
198198
/*! \brief Call cublas function safely. */
199199
template <typename Callback>
200200
inline void CublasCall(Callback&& callback) const {
201-
cublas_handle_->Call(std::forward<Callback>(callback));
201+
if (cublas_tf32_tensor_core_handle_) {
202+
cublas_tf32_tensor_core_handle_->Call(std::forward<Callback>(callback));
203+
} else {
204+
cublas_handle_->Call(std::forward<Callback>(callback));
205+
}
202206
}
203207

204208
/*! \brief Check whether tensor core is supported */
@@ -225,7 +229,11 @@ class CUDAContext {
225229
#if CUDA_VERSION >= 9000
226230
cublas_tensor_core_handle_.reset(
227231
new CublasHandleHolder(RawStream(), CUBLAS_TENSOR_OP_MATH));
228-
#endif
232+
#if CUDA_VERSION >= 11000
233+
cublas_tf32_tensor_core_handle_.reset(
234+
new CublasHandleHolder(RawStream(), CUBLAS_TF32_TENSOR_OP_MATH));
235+
#endif // CUDA_VERSION >= 11000
236+
#endif // CUDA_VERSION >= 9000
229237
}
230238
}
231239

@@ -268,6 +276,7 @@ class CUDAContext {
268276
void DestoryCuBlasContext() {
269277
cublas_handle_.reset();
270278
cublas_tensor_core_handle_.reset();
279+
cublas_tf32_tensor_core_handle_.reset();
271280
}
272281

273282
void DestoryCuSolverContext() {
@@ -284,6 +293,7 @@ class CUDAContext {
284293
cudnnHandle_t cudnn_handle_;
285294
std::unique_ptr<CublasHandleHolder> cublas_handle_;
286295
std::unique_ptr<CublasHandleHolder> cublas_tensor_core_handle_;
296+
std::unique_ptr<CublasHandleHolder> cublas_tf32_tensor_core_handle_;
287297
cusolverDnHandle_t cusolver_dn_handle_;
288298
DISABLE_COPY_AND_ASSIGN(CUDAContext);
289299
};

paddle/fluid/pybind/pybind.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ limitations under the License. */
5858
#include "paddle/fluid/operators/py_func_op.h"
5959
#include "paddle/fluid/platform/cpu_helper.h"
6060
#include "paddle/fluid/platform/cpu_info.h"
61+
#include "paddle/fluid/platform/device_context.h"
6162
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
6263
#include "paddle/fluid/platform/enforce.h"
6364
#include "paddle/fluid/platform/init.h"
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import unittest
16+
import six
17+
import numpy as np
18+
import paddle
19+
import paddle.fluid as fluid
20+
import paddle.fluid.core as core
21+
22+
23+
class TestTF32Switch(unittest.TestCase):
24+
def test_on_off(self):
25+
if core.is_compiled_with_cuda():
26+
place = fluid.CUDAPlace(0)
27+
self.assertTrue(core.get_cublas_switch()) # default
28+
core.set_cublas_switch(False)
29+
self.assertFalse(core.get_cublas_switch()) # turn off
30+
core.set_cublas_switch(True)
31+
self.assertTrue(core.get_cublas_switch()) # turn on
32+
33+
core.set_cublas_switch(True) # restore the switch
34+
else:
35+
pass
36+
37+
38+
class TestTF32OnMatmul(unittest.TestCase):
39+
def test_dygraph_without_out(self):
40+
if core.is_compiled_with_cuda():
41+
place = fluid.CUDAPlace(0)
42+
core.set_cublas_switch(False) # turn off
43+
with fluid.dygraph.guard(place):
44+
input_array1 = np.random.rand(4, 12, 64, 88).astype("float32")
45+
input_array2 = np.random.rand(4, 12, 88, 512).astype("float32")
46+
data1 = paddle.to_tensor(input_array1)
47+
data2 = paddle.to_tensor(input_array2)
48+
out = paddle.matmul(data1, data2)
49+
expected_result = np.matmul(input_array1, input_array2)
50+
self.assertTrue(np.allclose(expected_result, out.numpy(), 1e-03))
51+
core.set_cublas_switch(True) # restore the switch
52+
else:
53+
pass
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)