Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
b6aa9f53
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看板
未验证
提交
b6aa9f53
编写于
12月 13, 2022
作者:
E
engineer1109
提交者:
GitHub
12月 13, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enable custom device save model on device memory && fix conflict (#48221)
上级
5eff6f01
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
97 addition
and
9 deletion
+97
-9
paddle/fluid/inference/analysis/argument.h
paddle/fluid/inference/analysis/argument.h
+5
-0
paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc
...ence/analysis/passes/ir_params_sync_among_devices_pass.cc
+76
-8
paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.h
...rence/analysis/passes/ir_params_sync_among_devices_pass.h
+7
-1
paddle/fluid/inference/api/analysis_predictor.cc
paddle/fluid/inference/api/analysis_predictor.cc
+9
-0
未找到文件。
paddle/fluid/inference/analysis/argument.h
浏览文件 @
b6aa9f53
...
...
@@ -371,6 +371,11 @@ struct Argument {
// cinn compiler related
DECL_ARGUMENT_FIELD
(
use_cinn_compiler
,
UseCinnCompiler
,
bool
);
// custom device
DECL_ARGUMENT_FIELD
(
use_custom_device
,
UseCustomDevice
,
bool
);
DECL_ARGUMENT_FIELD
(
custom_device_type
,
CustomDeviceType
,
std
::
string
);
DECL_ARGUMENT_FIELD
(
custom_device_id
,
CustomDeviceId
,
int
);
private:
std
::
unordered_set
<
std
::
string
>
valid_fields_
;
};
...
...
paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.cc
浏览文件 @
b6aa9f53
...
...
@@ -14,6 +14,7 @@
#include "paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.h"
#include <cstdlib>
#include <string>
#include <unordered_set>
...
...
@@ -26,6 +27,11 @@
#include "paddle/fluid/platform/enforce.h"
#include "paddle/phi/common/data_type.h"
DEFINE_bool
(
custom_model_save_cpu
,
false
,
"Keep old mode for developers, the model is saved on cpu not device."
);
namespace
paddle
{
namespace
inference
{
namespace
analysis
{
...
...
@@ -71,9 +77,9 @@ void IrParamsSyncAmongDevicesPass::CopyParamsToNpu(Argument *argument) {
}
}
}
#endif
#else
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
void
IrParamsSyncAmongDevicesPass
::
CopyParamsToGpu
(
Argument
*
argument
)
{
// The parameters are on the cpu, therefore, synchronization is not necessary.
if
(
!
argument
->
use_gpu
())
return
;
...
...
@@ -148,7 +154,62 @@ void IrParamsSyncAmongDevicesPass::CopyParamsToGpu(Argument *argument) {
}
}
}
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
void
IrParamsSyncAmongDevicesPass
::
CopyParamsToCustomDevice
(
Argument
*
argument
)
{
if
(
!
argument
->
use_custom_device
())
return
;
// On old mode, the model is saved on cpu not device.
if
(
argument
->
custom_device_type
()
==
"OpenCL"
)
{
PADDLE_ENFORCE_EQ
(
FLAGS_custom_model_save_cpu
,
false
,
phi
::
errors
::
InvalidArgument
(
"'FLAGS_custom_model_save_cpu = false' is only for the developers "
"who have not completed custom device memory settings. Setting to "
"true will make "
"model memory reserve on the cpu, and make inference slower."
));
}
if
(
FLAGS_custom_model_save_cpu
)
return
;
auto
&
graph
=
argument
->
main_graph
();
std
::
vector
<
std
::
string
>
repetitive_params
;
if
(
graph
.
Has
(
framework
::
ir
::
kRepetitiveParamAttr
))
repetitive_params
=
graph
.
Get
<
std
::
vector
<
std
::
string
>>
(
framework
::
ir
::
kRepetitiveParamAttr
);
LOG
(
INFO
)
<<
"Sync params from CPU to CustomDevice"
<<
argument
->
custom_device_type
()
<<
"/"
<<
argument
->
custom_device_id
();
platform
::
Place
place
=
platform
::
CustomPlace
(
argument
->
custom_device_type
(),
argument
->
custom_device_id
());
auto
*
scope
=
argument
->
scope_ptr
();
std
::
vector
<
std
::
string
>
all_vars
=
scope
->
LocalVarNames
();
for
(
auto
&
var_name
:
all_vars
)
{
auto
*
var
=
scope
->
FindLocalVar
(
var_name
);
PADDLE_ENFORCE_NOT_NULL
(
var
,
platform
::
errors
::
PreconditionNotMet
(
"The var should not be nullptr"
));
if
(
var
->
IsType
<
phi
::
DenseTensor
>
()
||
var
->
IsType
<
phi
::
DenseTensor
>
())
{
auto
*
t
=
var
->
GetMutable
<
phi
::
DenseTensor
>
();
platform
::
CPUPlace
cpu_place
;
phi
::
DenseTensor
temp_tensor
;
temp_tensor
.
Resize
(
t
->
dims
());
paddle
::
framework
::
TensorCopySync
(
*
t
,
cpu_place
,
&
temp_tensor
);
t
->
clear
();
paddle
::
framework
::
TensorCopySync
(
temp_tensor
,
place
,
t
);
}
}
}
#endif
void
IrParamsSyncAmongDevicesPass
::
RunImpl
(
Argument
*
argument
)
{
...
...
@@ -156,13 +217,20 @@ void IrParamsSyncAmongDevicesPass::RunImpl(Argument *argument) {
argument
->
scope_valid
(),
true
,
platform
::
errors
::
PreconditionNotMet
(
"The scope field should be valid"
));
#ifdef PADDLE_WITH_ASCEND_CL
if
(
!
argument
->
use_npu_valid
())
return
;
CopyParamsToNpu
(
argument
);
#else
if
(
!
argument
->
use_gpu_valid
())
return
;
CopyParamsToGpu
(
argument
);
if
(
argument
->
use_npu_valid
())
{
CopyParamsToNpu
(
argument
);
}
#endif
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
if
(
argument
->
use_gpu_valid
())
{
CopyParamsToGpu
(
argument
);
}
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
if
(
argument
->
use_custom_device_valid
())
{
CopyParamsToCustomDevice
(
argument
);
}
#endif
}
...
...
paddle/fluid/inference/analysis/passes/ir_params_sync_among_devices_pass.h
浏览文件 @
b6aa9f53
...
...
@@ -37,9 +37,15 @@ class IrParamsSyncAmongDevicesPass : public AnalysisPass {
private:
#ifdef PADDLE_WITH_ASCEND_CL
void
CopyParamsToNpu
(
Argument
*
argument
);
#else
#endif
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
void
CopyParamsToGpu
(
Argument
*
argument
);
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
void
CopyParamsToCustomDevice
(
Argument
*
argument
);
#endif
};
}
// namespace analysis
...
...
paddle/fluid/inference/api/analysis_predictor.cc
浏览文件 @
b6aa9f53
...
...
@@ -1242,6 +1242,15 @@ void AnalysisPredictor::PrepareArgument() {
}
#endif
#ifdef PADDLE_WITH_CUSTOM_DEVICE
argument_
.
SetUseCustomDevice
(
config_
.
use_custom_device
());
if
(
config_
.
use_custom_device
())
{
LOG
(
INFO
)
<<
"CustomDevice is enabled"
;
argument_
.
SetCustomDeviceType
(
config_
.
custom_device_type
());
argument_
.
SetCustomDeviceId
(
config_
.
custom_device_id
());
}
#endif
auto
*
pass_builder
=
config_
.
pass_builder
();
// TODO(inference): Need to reconstruct the pass_builder, pass should be
// processed in a single
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录