Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f876320a
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2297
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
f876320a
编写于
3月 08, 2022
作者:
Z
zyfncg
提交者:
GitHub
3月 08, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support code auto-gene for sparse backward api (#40196)
上级
d4b007af
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
235 addition
and
9 deletion
+235
-9
.gitignore
.gitignore
+2
-0
paddle/phi/api/lib/CMakeLists.txt
paddle/phi/api/lib/CMakeLists.txt
+24
-2
python/paddle/utils/code_gen/backward_api_gen.py
python/paddle/utils/code_gen/backward_api_gen.py
+1
-0
python/paddle/utils/code_gen/sparse_api_gen.py
python/paddle/utils/code_gen/sparse_api_gen.py
+2
-7
python/paddle/utils/code_gen/sparse_bw_api.yaml
python/paddle/utils/code_gen/sparse_bw_api.yaml
+6
-0
python/paddle/utils/code_gen/sparse_bw_api_gen.py
python/paddle/utils/code_gen/sparse_bw_api_gen.py
+200
-0
未找到文件。
.gitignore
浏览文件 @
f876320a
...
...
@@ -6,12 +6,14 @@ paddle/fluid/eager/api/generated/*
paddle/fluid/op_use_default_grad_maker_DEV.spec
paddle/fluid/op_use_default_grad_maker_PR.spec
paddle/phi/api/backward/backward_api.h
paddle/phi/api/backward/sparse_bw_api.h
paddle/phi/api/include/api.h
paddle/phi/api/include/sparse_api.h
paddle/phi/api/lib/api.cc
paddle/phi/api/lib/dygraph_api.*
paddle/phi/api/lib/backward_api.cc
paddle/phi/api/lib/sparse_api.cc
paddle/phi/api/lib/sparse_bw_api.cc
paddle/phi/extension.h
paddle/phi/include/*
paddle/phi/infermeta/generated.*
...
...
paddle/phi/api/lib/CMakeLists.txt
浏览文件 @
f876320a
...
...
@@ -40,6 +40,14 @@ set(sparse_api_source_file ${CMAKE_SOURCE_DIR}/paddle/phi/api/lib/sparse_api.cc)
set
(
sparse_api_header_file_tmp
${
api_header_file
}
.tmp
)
set
(
sparse_api_source_file_tmp
${
api_source_file
}
.tmp
)
# sparse bw api file
set
(
sparse_bw_api_gen_file
${
CMAKE_SOURCE_DIR
}
/python/paddle/utils/code_gen/sparse_bw_api_gen.py
)
set
(
sparse_bw_api_yaml_file
${
CMAKE_SOURCE_DIR
}
/python/paddle/utils/code_gen/sparse_bw_api.yaml
)
set
(
sparse_bw_api_header_file
${
CMAKE_SOURCE_DIR
}
/paddle/phi/api/backward/sparse_bw_api.h
)
set
(
sparse_bw_api_source_file
${
CMAKE_SOURCE_DIR
}
/paddle/phi/api/lib/sparse_bw_api.cc
)
set
(
sparse_bw_api_header_file_tmp
${
sparse_bw_api_header_file
}
.tmp
)
set
(
sparse_bw_api_source_file_tmp
${
sparse_bw_api_source_file
}
.tmp
)
# wrapped infermeta file
set
(
wrapped_infermeta_gen_file
${
CMAKE_SOURCE_DIR
}
/python/paddle/utils/code_gen/wrapped_infermeta_gen.py
)
set
(
api_yaml_file
${
CMAKE_SOURCE_DIR
}
/python/paddle/utils/code_gen/api.yaml
)
...
...
@@ -91,7 +99,20 @@ add_custom_command(
COMMAND
${
CMAKE_COMMAND
}
-E copy_if_different
${
sparse_api_header_file_tmp
}
${
sparse_api_header_file
}
COMMAND
${
CMAKE_COMMAND
}
-E copy_if_different
${
sparse_api_source_file_tmp
}
${
sparse_api_source_file
}
COMMENT
"copy_if_different
${
sparse_api_header_file
}
${
sparse_sparse_api_source_file
}
"
DEPENDS
${
sparse_api_yaml_file
}
${
sparse_api_gen_file
}
${
api_gen_base
}
DEPENDS
${
sparse_api_yaml_file
}
${
sparse_api_gen_file
}
${
api_gen_base
}
${
api_gen_file
}
VERBATIM
)
# generate backward sparse api
add_custom_command
(
OUTPUT
${
sparse_bw_api_header_file
}
${
sparse_bw_api_source_file
}
COMMAND
${
PYTHON_EXECUTABLE
}
${
sparse_bw_api_gen_file
}
--api_yaml_path
${
sparse_bw_api_yaml_file
}
--api_header_path
${
sparse_bw_api_header_file_tmp
}
--api_source_path
${
sparse_bw_api_source_file_tmp
}
COMMAND
${
CMAKE_COMMAND
}
-E copy_if_different
${
sparse_bw_api_header_file_tmp
}
${
sparse_bw_api_header_file
}
COMMAND
${
CMAKE_COMMAND
}
-E copy_if_different
${
sparse_bw_api_source_file_tmp
}
${
sparse_bw_api_source_file
}
COMMENT
"copy_if_different
${
sparse_bw_api_header_file
}
${
sparse_bw_sparse_api_source_file
}
"
DEPENDS
${
sparse_bw_api_yaml_file
}
${
sparse_bw_api_gen_file
}
${
api_gen_base
}
${
api_gen_file
}
${
sparse_api_gen_file
}
${
bw_api_gen_file
}
VERBATIM
)
# generate wrapped infermeta
...
...
@@ -113,9 +134,10 @@ cc_library(phi_data_transform SRCS data_transform.cc DEPS phi_tensor_raw transfe
cc_library
(
api_custom_impl SRCS api_custom_impl.cc DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform
)
cc_library
(
sparse_api_custom_impl SRCS sparse_api_custom_impl.cc DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform
)
cc_library
(
sparse_api SRCS sparse_api.cc DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl
)
cc_library
(
phi_function_api SRCS
${
api_source_file
}
DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform api_custom_impl
)
cc_library
(
phi_dygraph_api SRCS
${
dygraph_api_source_file
}
DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils phi_data_transform
)
cc_library
(
phi_bw_function_api SRCS
${
bw_api_source_file
}
DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils backward_infermeta phi_data_transform phi_function_api api_custom_impl
)
cc_library
(
sparse_api SRCS
${
sparse_api_source_file
}
DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api_custom_impl
)
cc_library
(
sparse_bw_api SRCS
${
sparse_bw_api_source_file
}
DEPS phi_tensor_raw phi kernel_dispatch api_gen_utils sparse_api sparse_api_custom_impl
)
cc_library
(
phi_tensor SRCS tensor_method.cc DEPS phi_tensor_raw phi_function_api
)
python/paddle/utils/code_gen/backward_api_gen.py
浏览文件 @
f876320a
...
...
@@ -35,6 +35,7 @@ class BackwardAPI(BaseAPI):
forward_config
)
api
=
result
.
group
(
'api'
)
_
,
outputs
,
_
=
self
.
parse_output
(
self
.
api
,
result
.
group
(
'outputs'
))
outputs
=
[
item
.
split
(
'@'
)[
0
]
for
item
in
outputs
]
fw_inputs
,
fw_attrs
,
_
,
=
self
.
parse_input_and_attr
(
api
,
result
.
group
(
'args'
))
...
...
python/paddle/utils/code_gen/sparse_api_gen.py
浏览文件 @
f876320a
...
...
@@ -17,10 +17,10 @@ import yaml
import
argparse
import
re
from
api_
base
import
Base
API
from
api_
gen
import
Forward
API
class
SparseAPI
(
Base
API
):
class
SparseAPI
(
Forward
API
):
def
__init__
(
self
,
api_item_yaml
):
super
(
SparseAPI
,
self
).
__init__
(
api_item_yaml
)
...
...
@@ -30,11 +30,6 @@ class SparseAPI(BaseAPI):
def
get_api_func_name
(
self
):
return
self
.
api
def
get_return_type
(
self
,
out_type_list
):
return
out_type_list
[
0
]
if
len
(
out_type_list
)
==
1
else
"std::tuple<"
+
","
.
join
(
out_type_list
)
+
">"
def
gene_api_declaration
(
self
):
return
f
"""
//
{
", "
.
join
(
self
.
outputs
[
'names'
])
}
...
...
python/paddle/utils/code_gen/sparse_bw_api.yaml
0 → 100644
浏览文件 @
f876320a
-
sparse_bw_api
:
conv3d_grad
forward
:
conv3d (Tensor x, Tensor kernel, int[] paddings, int[] dilations, int[] strides, int groups) -> Tensor(out@SparseCooTensor), Tensor(rulebook@DenseTensor)
args
:
(Tensor x, Tensor kernel, Tensor rulebook, Tensor out_grad, int[] paddings, int[] dilations, int[] strides, int groups)
output
:
Tensor(x_grad@DenseTensor), Tensor(kernel_grad@DenseTensor)
kernel
:
func
:
sparse_conv_grad
python/paddle/utils/code_gen/sparse_bw_api_gen.py
0 → 100644
浏览文件 @
f876320a
# Copyright (c) 2022 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
os
import
yaml
import
argparse
import
re
from
sparse_api_gen
import
SparseAPI
from
backward_api_gen
import
BackwardAPI
class
SparseBackwardAPI
(
SparseAPI
,
BackwardAPI
):
def
__init__
(
self
,
bw_api_item_yaml
):
BackwardAPI
.
__init__
(
self
,
bw_api_item_yaml
)
def
get_api_name
(
self
,
api_item_yaml
):
return
api_item_yaml
[
'sparse_bw_api'
]
def
get_api_func_name
(
self
):
return
self
.
api
def
get_return_type
(
self
,
out_type_list
):
return
BackwardAPI
.
get_return_type
(
self
,
out_type_list
)
def
gene_api_declaration
(
self
):
return
SparseAPI
.
gene_api_declaration
(
self
)
def
gene_output
(
self
,
output_type_list
,
set_out_func
,
code_indent
,
inplace_flag
=
False
):
kernel_output
=
""
output_names
=
[]
output_create
=
""
if
len
(
output_type_list
)
==
1
:
kernel_output
=
'kernel_out'
output_names
.
append
(
'kernel_out'
)
inplace_assign
=
" = "
+
self
.
inplace_map
[
self
.
outputs
[
'names'
][
0
]]
if
inplace_flag
and
self
.
inplace_map
is
not
None
and
self
.
outputs
[
'names'
][
0
]
in
self
.
inplace_map
else
""
output_create
=
f
"""
{
self
.
outputs
[
'return_type'
]
}
out
{
inplace_assign
}
;
auto kernel_out =
{
set_out_func
}
(&out,
{
self
.
get_kernel_tensor_out_type
(
self
.
outputs
[
'names'
][
0
])
}
);"""
elif
len
(
output_type_list
)
>
1
:
output_create
=
f
"""
{
self
.
outputs
[
'return_type'
]
}
out(
{
len
(
output_type_list
)
}
);"""
for
i
,
out_type_item
in
enumerate
(
output_type_list
):
kernel_output
=
kernel_output
+
f
'kernel_out_
{
i
}
, '
output_names
.
append
(
f
'kernel_out_
{
i
}
'
)
if
out_type_item
==
'Tensor'
:
get_out_code
=
f
'&out[
{
i
}
][0]'
if
inplace_flag
and
self
.
inplace_map
is
not
None
and
self
.
outputs
[
'names'
][
i
]
in
self
.
inplace_map
:
output_create
=
output_create
+
f
"""
out[
{
i
}
].emplace_back(
{
self
.
inplace_map
[
self
.
outputs
[
'names'
][
i
]]
}
);"""
else
:
output_create
=
output_create
+
f
"""
out[
{
i
}
].emplace_back();"""
else
:
get_out_code
=
f
'&out[
{
i
}
]'
if
inplace_flag
and
self
.
inplace_map
is
not
None
and
self
.
outputs
[
'names'
][
i
]
in
self
.
inplace_map
:
output_create
=
output_create
+
f
"""
out[
{
i
}
] =
{
self
.
inplace_map
[
self
.
outputs
[
'names'
][
i
]]
}
;"""
output_create
=
output_create
+
f
"""
auto kernel_out_
{
i
}
=
{
set_out_func
}
(
{
get_out_code
}
,
{
self
.
get_kernel_tensor_out_type
(
self
.
outputs
[
'names'
][
i
])
}
);"""
kernel_output
=
kernel_output
[:
-
2
]
else
:
raise
ValueError
(
"{} : Output error: the output should not be empty."
.
format
(
self
.
api
))
return
kernel_output
,
output_names
,
output_create
def
header_include
():
return
"""
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/common/scalar_array.h"
#include "paddle/utils/optional.h"
"""
def
source_include
(
header_file_path
):
return
f
"""
#include "
{
header_file_path
}
"
#include <memory>
#include "glog/logging.h"
#include "paddle/phi/api/lib/api_registry.h"
#include "paddle/phi/api/lib/api_gen_utils.h"
#include "paddle/phi/api/lib/kernel_dispatch.h"
#include "paddle/phi/api/lib/sparse_api_custom_impl.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/declarations.h"
"""
def
api_register
():
return
"""
PD_REGISTER_API(Test);
"""
def
api_namespace
():
return
(
"""
namespace paddle {
namespace experimental {
namespace sparse {
"""
,
"""
} // namespace sparse
} // namespace experimental
} // namespace paddle
"""
)
def
generate_api
(
api_yaml_path
,
header_file_path
,
source_file_path
):
with
open
(
api_yaml_path
,
'r'
)
as
f
:
apis
=
yaml
.
load
(
f
,
Loader
=
yaml
.
FullLoader
)
header_file
=
open
(
header_file_path
,
'w'
)
source_file
=
open
(
source_file_path
,
'w'
)
namespace
=
api_namespace
()
header_file
.
write
(
"#pragma once
\n
"
)
header_file
.
write
(
header_include
())
header_file
.
write
(
namespace
[
0
])
include_header_file
=
"paddle/phi/api/backward/sparse_bw_api.h"
source_file
.
write
(
source_include
(
include_header_file
))
source_file
.
write
(
namespace
[
0
])
for
api
in
apis
:
sparse_bw_api
=
SparseBackwardAPI
(
api
)
header_file
.
write
(
sparse_bw_api
.
gene_api_declaration
())
source_file
.
write
(
sparse_bw_api
.
gene_api_code
())
header_file
.
write
(
namespace
[
1
])
source_file
.
write
(
namespace
[
1
])
source_file
.
write
(
api_register
())
header_file
.
close
()
source_file
.
close
()
def
main
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Generate PaddlePaddle C++ Sparse API files'
)
parser
.
add_argument
(
'--api_yaml_path'
,
help
=
'path to sparse api yaml file'
,
default
=
'python/paddle/utils/code_gen/sparse_bw_api.yaml'
)
parser
.
add_argument
(
'--api_header_path'
,
help
=
'output of generated api header code file'
,
default
=
'paddle/phi/api/backward/sparse_bw_api.h'
)
parser
.
add_argument
(
'--api_source_path'
,
help
=
'output of generated api source code file'
,
default
=
'paddle/phi/api/lib/sparse_bw_api.cc'
)
options
=
parser
.
parse_args
()
api_yaml_path
=
options
.
api_yaml_path
header_file_path
=
options
.
api_header_path
source_file_path
=
options
.
api_source_path
generate_api
(
api_yaml_path
,
header_file_path
,
source_file_path
)
if
__name__
==
'__main__'
:
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录