Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
efea540c
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
efea540c
编写于
12月 15, 2020
作者:
A
AshburnLee
提交者:
GitHub
12月 15, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add tf32 support for A100 tensor core acceleration for cuBLAS (#28732)
上级
7779768b
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
94 addition
and
3 deletion
+94
-3
paddle/fluid/platform/cuda_helper.h
paddle/fluid/platform/cuda_helper.h
+6
-1
paddle/fluid/platform/device_context.cc
paddle/fluid/platform/device_context.cc
+6
-0
paddle/fluid/platform/device_context.h
paddle/fluid/platform/device_context.h
+19
-2
paddle/fluid/pybind/pybind.cc
paddle/fluid/pybind/pybind.cc
+6
-0
python/paddle/fluid/tests/unittests/test_tf32_cublas.py
python/paddle/fluid/tests/unittests/test_tf32_cublas.py
+57
-0
未找到文件。
paddle/fluid/platform/cuda_helper.h
浏览文件 @
efea540c
...
...
@@ -84,8 +84,13 @@ class CublasHandleHolder {
if
(
math_type
==
CUBLAS_TENSOR_OP_MATH
)
{
PADDLE_ENFORCE_CUDA_SUCCESS
(
dynload
::
cublasSetMathMode
(
handle_
,
CUBLAS_TENSOR_OP_MATH
));
#if CUDA_VERSION >= 11000
}
else
if
(
math_type
==
CUBLAS_TF32_TENSOR_OP_MATH
)
{
PADDLE_ENFORCE_CUDA_SUCCESS
(
dynload
::
cublasSetMathMode
(
handle_
,
CUBLAS_TF32_TENSOR_OP_MATH
));
#endif // CUDA_VERSION >= 11000
}
#endif
#endif
// CUDA_VERSION >= 9000
}
~
CublasHandleHolder
()
PADDLE_MAY_THROW
{
...
...
paddle/fluid/platform/device_context.cc
浏览文件 @
efea540c
...
...
@@ -54,6 +54,12 @@ AllocationPtr Alloc(const platform::DeviceContext& dev_ctx, size_t size) {
namespace
paddle
{
namespace
platform
{
#ifdef PADDLE_WITH_CUDA
bool
allow_tf32_cublas
=
true
;
void
SetAllowTF32Cublas
(
bool
active
)
{
allow_tf32_cublas
=
active
;
}
bool
AllowTF32Cublas
()
{
return
allow_tf32_cublas
;
}
#endif // PADDLE_WITH_CUDA
DeviceContextPool
*
DeviceContextPool
::
pool
=
nullptr
;
platform
::
DeviceContext
*
DeviceContextPool
::
Get
(
const
platform
::
Place
&
place
)
{
...
...
paddle/fluid/platform/device_context.h
浏览文件 @
efea540c
...
...
@@ -57,6 +57,13 @@ struct GpuDevice;
namespace
paddle
{
namespace
platform
{
#ifdef PADDLE_WITH_CUDA
/*Set the value of the global variable allow_tf32_cublas*/
void
SetAllowTF32Cublas
(
bool
active
);
/*Get the global variable allow_tf32_cublas value*/
bool
AllowTF32Cublas
();
#endif // PADDLE_WITH_CUDA
class
DeviceContext
{
public:
virtual
~
DeviceContext
()
PADDLE_MAY_THROW
{}
...
...
@@ -161,7 +168,11 @@ class CUDAContext {
/*! \brief Call cublas function safely. */
template
<
typename
Callback
>
inline
void
CublasCall
(
Callback
&&
callback
)
const
{
cublas_handle_
->
Call
(
std
::
forward
<
Callback
>
(
callback
));
if
(
cublas_tf32_tensor_core_handle_
)
{
cublas_tf32_tensor_core_handle_
->
Call
(
std
::
forward
<
Callback
>
(
callback
));
}
else
{
cublas_handle_
->
Call
(
std
::
forward
<
Callback
>
(
callback
));
}
}
/*! \brief Check whether tensor core is supported */
...
...
@@ -188,7 +199,11 @@ class CUDAContext {
#if CUDA_VERSION >= 9000
cublas_tensor_core_handle_
.
reset
(
new
CublasHandleHolder
(
RawStream
(),
CUBLAS_TENSOR_OP_MATH
));
#endif
#if CUDA_VERSION >= 11000
cublas_tf32_tensor_core_handle_
.
reset
(
new
CublasHandleHolder
(
RawStream
(),
CUBLAS_TF32_TENSOR_OP_MATH
));
#endif // CUDA_VERSION >= 11000
#endif // CUDA_VERSION >= 9000
}
}
...
...
@@ -231,6 +246,7 @@ class CUDAContext {
void
DestoryCuBlasContext
()
{
cublas_handle_
.
reset
();
cublas_tensor_core_handle_
.
reset
();
cublas_tf32_tensor_core_handle_
.
reset
();
}
void
DestoryCuSolverContext
()
{
...
...
@@ -247,6 +263,7 @@ class CUDAContext {
cudnnHandle_t
cudnn_handle_
;
std
::
unique_ptr
<
CublasHandleHolder
>
cublas_handle_
;
std
::
unique_ptr
<
CublasHandleHolder
>
cublas_tensor_core_handle_
;
std
::
unique_ptr
<
CublasHandleHolder
>
cublas_tf32_tensor_core_handle_
;
cusolverDnHandle_t
cusolver_dn_handle_
;
DISABLE_COPY_AND_ASSIGN
(
CUDAContext
);
};
...
...
paddle/fluid/pybind/pybind.cc
浏览文件 @
efea540c
...
...
@@ -58,6 +58,7 @@ limitations under the License. */
#include "paddle/fluid/operators/py_func_op.h"
#include "paddle/fluid/platform/cpu_helper.h"
#include "paddle/fluid/platform/cpu_info.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/init.h"
...
...
@@ -1980,6 +1981,11 @@ All parameter, weight, gradient are variables in Paddle.
m
.
def
(
"size_of_dtype"
,
framework
::
SizeOfType
);
#ifdef PADDLE_WITH_CUDA
m
.
def
(
"set_cublas_switch"
,
platform
::
SetAllowTF32Cublas
);
m
.
def
(
"get_cublas_switch"
,
platform
::
AllowTF32Cublas
);
#endif // PADDLE_WITH_CUDA
using
VarQuantScale
=
std
::
unordered_map
<
std
::
string
,
std
::
pair
<
bool
,
LoDTensor
>>
;
...
...
python/paddle/fluid/tests/unittests/test_tf32_cublas.py
0 → 100644
浏览文件 @
efea540c
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import
unittest
import
six
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
class
TestTF32Switch
(
unittest
.
TestCase
):
def
test_on_off
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
fluid
.
CUDAPlace
(
0
)
self
.
assertTrue
(
core
.
get_cublas_switch
())
# default
core
.
set_cublas_switch
(
False
)
self
.
assertFalse
(
core
.
get_cublas_switch
())
# turn off
core
.
set_cublas_switch
(
True
)
self
.
assertTrue
(
core
.
get_cublas_switch
())
# turn on
core
.
set_cublas_switch
(
True
)
# restore the switch
else
:
pass
class
TestTF32OnMatmul
(
unittest
.
TestCase
):
def
test_dygraph_without_out
(
self
):
if
core
.
is_compiled_with_cuda
():
place
=
fluid
.
CUDAPlace
(
0
)
core
.
set_cublas_switch
(
False
)
# turn off
with
fluid
.
dygraph
.
guard
(
place
):
input_array1
=
np
.
random
.
rand
(
4
,
12
,
64
,
88
).
astype
(
"float32"
)
input_array2
=
np
.
random
.
rand
(
4
,
12
,
88
,
512
).
astype
(
"float32"
)
data1
=
paddle
.
to_tensor
(
input_array1
)
data2
=
paddle
.
to_tensor
(
input_array2
)
out
=
paddle
.
matmul
(
data1
,
data2
)
expected_result
=
np
.
matmul
(
input_array1
,
input_array2
)
self
.
assertTrue
(
np
.
allclose
(
expected_result
,
out
.
numpy
(),
1e-03
))
core
.
set_cublas_switch
(
True
)
# restore the switch
else
:
pass
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录