Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
f2968742
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
未验证
提交
f2968742
编写于
9月 08, 2023
作者:
Y
Yichen Zhang
提交者:
GitHub
9月 08, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add transpose backward rule (#56509)
上级
f839e821
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
214 addition
and
21 deletion
+214
-21
paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc
...stributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc
+90
-20
paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.h
...istributed/auto_parallel/spmd_rules/transpose_spmd_rule.h
+7
-1
test/auto_parallel/spmd_rules/test_transpose_rule.py
test/auto_parallel/spmd_rules/test_transpose_rule.py
+117
-0
未找到文件。
paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.cc
浏览文件 @
f2968742
...
@@ -23,7 +23,7 @@ std::pair<std::vector<TensorDistAttr>, std::vector<TensorDistAttr>>
...
@@ -23,7 +23,7 @@ std::pair<std::vector<TensorDistAttr>, std::vector<TensorDistAttr>>
TransposeSPMDRule
::
InferForward
(
const
std
::
vector
<
DistTensorSpec
>&
input_specs
,
TransposeSPMDRule
::
InferForward
(
const
std
::
vector
<
DistTensorSpec
>&
input_specs
,
const
paddle
::
framework
::
AttributeMap
&
attrs
)
{
const
paddle
::
framework
::
AttributeMap
&
attrs
)
{
// step0: Verify Input Args Based on Transpose Logic
// step0: Verify Input Args Based on Transpose Logic
int64_t
ninputs
=
static_cast
<
int64_t
>
(
input_specs
.
size
()
);
int64_t
ninputs
=
input_specs
.
size
(
);
PADDLE_ENFORCE_EQ
(
PADDLE_ENFORCE_EQ
(
ninputs
,
ninputs
,
1
,
1
,
...
@@ -33,27 +33,15 @@ TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
...
@@ -33,27 +33,15 @@ TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
VerifySpecs
(
input_specs
,
"transpose"
);
VerifySpecs
(
input_specs
,
"transpose"
);
// step1: Build Einsum Notation
// step1: Build Einsum Notation
std
::
vector
<
int64_t
>
perm_dims
=
ExtractAttr
<
std
::
vector
<
int64_t
>>
(
"perm"
,
attrs
);
std
::
string
alphabet
=
"abcdefghijklmnopqrstuvwxyz"
;
std
::
string
alphabet
=
"abcdefghijklmnopqrstuvwxyz"
;
// get einsum notation for input
// get einsum notation for input
int64_t
ndim
=
static_cast
<
int64_t
>
(
input_specs
[
0
].
shape
().
size
()
);
int64_t
ndim
=
input_specs
[
0
].
shape
().
size
(
);
std
::
vector
<
std
::
string
>
input_axes_vec
;
std
::
vector
<
std
::
string
>
input_axes_vec
;
std
::
string
input_axes
=
alphabet
.
substr
(
0
,
ndim
);
std
::
string
input_axes
=
alphabet
.
substr
(
0
,
ndim
);
input_axes_vec
.
emplace_back
(
input_axes
);
input_axes_vec
.
emplace_back
(
input_axes
);
// get einsum notation for output
// get einsum notation for output
for
(
int64_t
i
=
0
,
n
=
static_cast
<
int64_t
>
(
perm_dims
.
size
());
i
<
n
;
++
i
)
{
std
::
string
output_axes
=
GetOutputNotation
(
ndim
,
input_axes
,
attrs
);
// convert the negative dim value to normal dim value
if
(
perm_dims
[
i
]
<
0
)
{
perm_dims
[
i
]
=
ndim
+
perm_dims
[
i
];
}
}
std
::
string
output_axes
=
""
;
for
(
int64_t
i
=
0
;
i
<
ndim
;
i
++
)
{
output_axes
.
append
(
1
,
input_axes
[
perm_dims
[
i
]]);
}
// step2: Sharding Propogation
// step2: Sharding Propogation
// step2.1: merge input shardings
// step2.1: merge input shardings
...
@@ -72,17 +60,19 @@ TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
...
@@ -72,17 +60,19 @@ TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
CopyTensorDistAttrForOutput
(
input_specs
[
0
].
dist_attr
());
CopyTensorDistAttrForOutput
(
input_specs
[
0
].
dist_attr
());
output_dist_attr
.
set_dims_mapping
(
output_dims_mapping
);
output_dist_attr
.
set_dims_mapping
(
output_dims_mapping
);
// Step2.3 handle input tensor partial (TODO)
// step3 Handle partial (TODO)
VLOG
(
4
)
<<
"TransposeSPMDRule InferForward:"
;
VLOG
(
4
)
<<
"TransposeSPMDRule InferForward:"
;
for
(
int64_t
i
=
0
;
i
<
ninputs
;
i
++
)
{
for
(
int64_t
i
=
0
;
i
<
ninputs
;
i
++
)
{
VLOG
(
4
)
<<
"Input"
<<
std
::
to_string
(
i
)
<<
" shape: ["
VLOG
(
4
)
<<
"Input"
<<
std
::
to_string
(
i
)
<<
" shape: ["
<<
str_join
(
input_specs
[
i
].
shape
())
<<
"] "
<<
str_join
(
input_specs
[
i
].
shape
())
<<
"] "
<<
"src_dims_mapping: ["
<<
str_join
(
input_specs
[
i
].
dims_mapping
())
<<
"src_dims_mapping: ["
<<
str_join
(
input_specs
[
i
].
dims_mapping
())
<<
"] "
<<
"] "
<<
"perm: ["
<<
str_join
(
perm_dims
)
<<
"] "
<<
"dst_dims_mapping: ["
<<
str_join
(
input_specs
[
i
].
dims_mapping
())
<<
"dst_dims_mapping: ["
<<
str_join
(
input_specs
[
i
].
dims_mapping
())
<<
"]"
;
<<
"]"
;
}
}
VLOG
(
4
)
<<
"Perm: ["
<<
str_join
(
ExtractAttr
<
std
::
vector
<
int64_t
>>
(
"perm"
,
attrs
))
<<
"]"
;
VLOG
(
4
)
<<
"Output dims_mapping: ["
+
str_join
(
output_dims_mapping
)
+
"]
\n\n
"
;
VLOG
(
4
)
<<
"Output dims_mapping: ["
+
str_join
(
output_dims_mapping
)
+
"]
\n\n
"
;
return
{{
input_specs
[
0
].
dist_attr
()},
{
output_dist_attr
}};
return
{{
input_specs
[
0
].
dist_attr
()},
{
output_dist_attr
}};
...
@@ -90,12 +80,92 @@ TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
...
@@ -90,12 +80,92 @@ TransposeSPMDRule::InferForward(const std::vector<DistTensorSpec>& input_specs,
std
::
pair
<
std
::
vector
<
TensorDistAttr
>
,
std
::
vector
<
TensorDistAttr
>>
std
::
pair
<
std
::
vector
<
TensorDistAttr
>
,
std
::
vector
<
TensorDistAttr
>>
TransposeSPMDRule
::
InferBackward
(
TransposeSPMDRule
::
InferBackward
(
const
std
::
vector
<
DistTensorSpec
>&
input_specs
,
const
std
::
vector
<
DistTensorSpec
>&
output_specs
,
const
std
::
vector
<
DistTensorSpec
>&
output_specs
,
const
paddle
::
framework
::
AttributeMap
&
attrs
)
{
const
paddle
::
framework
::
AttributeMap
&
attrs
)
{
PADDLE_THROW
(
phi
::
errors
::
Unimplemented
(
// step0: Verify Input Args Based on Transpose Logic
"InferBackward of TransposeSPMDRule is NOT implemented yet."
));
int64_t
ninputs
=
input_specs
.
size
();
int64_t
noutputs
=
output_specs
.
size
();
PADDLE_ENFORCE_EQ
(
ninputs
,
1
,
phi
::
errors
::
InvalidArgument
(
"The size of InputSpec in transpose must "
"be equal to 1, but got [%d]."
,
ninputs
));
PADDLE_ENFORCE_EQ
(
noutputs
,
1
,
phi
::
errors
::
InvalidArgument
(
"The size of OutputSpec in transpose must "
"be equal to 1, but got [%d]."
,
noutputs
));
VerifySpecs
(
output_specs
,
"transpose_backward"
);
// step1: Build Einsum Notation
std
::
string
alphabet
=
"abcdefghijklmnopqrstuvwxyz"
;
// get einsum notation for input
int64_t
ndim
=
input_specs
[
0
].
shape
().
size
();
std
::
string
input_axes
=
alphabet
.
substr
(
0
,
ndim
);
// get einsum notation for output
std
::
string
output_axes
=
GetOutputNotation
(
ndim
,
input_axes
,
attrs
);
std
::
vector
<
std
::
string
>
output_axes_vec
;
output_axes_vec
.
emplace_back
(
output_axes
);
// step2: Sharding Propogation
// step2.1: merge input shardings
std
::
vector
<
std
::
pair
<
std
::
string
,
std
::
vector
<
int64_t
>>>
axes_sharding_info
;
axes_sharding_info
=
GetAxesDimsMappingPair
(
output_axes_vec
,
output_specs
);
std
::
unordered_map
<
std
::
string
,
int64_t
>
axis_to_dim_map
=
ShardingMergeForTensors
(
axes_sharding_info
);
// step2.2: infer output dimsmapping from merged input dimsmapping
std
::
vector
<
int64_t
>
input_dims_mapping
=
GetDimsMappingForAxes
(
input_axes
,
axis_to_dim_map
);
// initialize output dist_attr's process_mesh, batch_dim and dynamic dims with
// input dist_attr.
TensorDistAttr
input_dist_attr
=
CopyTensorDistAttrForOutput
(
input_specs
[
0
].
dist_attr
());
input_dist_attr
.
set_dims_mapping
(
input_dims_mapping
);
// Step3 Handle partial (TODO)
VLOG
(
4
)
<<
"TransposeSPMDRule InferBackward:"
;
VLOG
(
4
)
<<
"Output shape: ["
<<
str_join
(
output_specs
[
0
].
shape
())
<<
"] "
<<
"dims_mapping: ["
<<
str_join
(
output_specs
[
0
].
dims_mapping
())
<<
"]"
;
VLOG
(
4
)
<<
"Perm: ["
<<
str_join
(
ExtractAttr
<
std
::
vector
<
int64_t
>>
(
"perm"
,
attrs
))
<<
"]"
;
for
(
int64_t
i
=
0
;
i
<
ninputs
;
i
++
)
{
VLOG
(
4
)
<<
"Input"
<<
std
::
to_string
(
i
)
<<
" shape: ["
<<
str_join
(
input_specs
[
i
].
shape
())
<<
"] "
<<
"dims_mapping: ["
<<
str_join
(
input_dims_mapping
)
<<
"]"
;
}
VLOG
(
4
)
<<
std
::
endl
;
return
{{
input_dist_attr
},
{
output_specs
[
0
].
dist_attr
()}};
}
std
::
string
TransposeSPMDRule
::
GetOutputNotation
(
int64_t
input_ndim
,
const
std
::
string
&
input_axes
,
const
paddle
::
framework
::
AttributeMap
&
attrs
)
{
std
::
vector
<
int64_t
>
perm_dims
=
ExtractAttr
<
std
::
vector
<
int64_t
>>
(
"perm"
,
attrs
);
// convert the negative dim value to normal dim value
for
(
int64_t
i
=
0
,
n
=
perm_dims
.
size
();
i
<
n
;
++
i
)
{
if
(
perm_dims
[
i
]
<
0
)
{
perm_dims
[
i
]
=
input_ndim
+
perm_dims
[
i
];
}
}
std
::
string
output_axes
=
""
;
for
(
int64_t
i
=
0
;
i
<
input_ndim
;
i
++
)
{
output_axes
.
append
(
1
,
input_axes
[
perm_dims
[
i
]]);
}
return
{}
;
return
output_axes
;
}
}
}
// namespace auto_parallel
}
// namespace auto_parallel
...
...
paddle/fluid/distributed/auto_parallel/spmd_rules/transpose_spmd_rule.h
浏览文件 @
f2968742
...
@@ -32,8 +32,14 @@ class TransposeSPMDRule : public SPMDRuleBase {
...
@@ -32,8 +32,14 @@ class TransposeSPMDRule : public SPMDRuleBase {
const
paddle
::
framework
::
AttributeMap
&
attrs
)
override
;
const
paddle
::
framework
::
AttributeMap
&
attrs
)
override
;
std
::
pair
<
std
::
vector
<
TensorDistAttr
>
,
std
::
vector
<
TensorDistAttr
>>
std
::
pair
<
std
::
vector
<
TensorDistAttr
>
,
std
::
vector
<
TensorDistAttr
>>
InferBackward
(
const
std
::
vector
<
DistTensorSpec
>&
output_specs
,
InferBackward
(
const
std
::
vector
<
DistTensorSpec
>&
input_specs
,
const
std
::
vector
<
DistTensorSpec
>&
output_specs
,
const
paddle
::
framework
::
AttributeMap
&
attrs
)
override
;
const
paddle
::
framework
::
AttributeMap
&
attrs
)
override
;
private:
std
::
string
GetOutputNotation
(
int64_t
input_ndim
,
const
std
::
string
&
input_axes
,
const
paddle
::
framework
::
AttributeMap
&
attrs
);
};
};
}
// namespace auto_parallel
}
// namespace auto_parallel
}
// namespace distributed
}
// namespace distributed
...
...
test/auto_parallel/spmd_rules/test_transpose_rule.py
浏览文件 @
f2968742
...
@@ -38,6 +38,8 @@ class TestTransposeSPMDRule(unittest.TestCase):
...
@@ -38,6 +38,8 @@ class TestTransposeSPMDRule(unittest.TestCase):
x_tensor_dist_attr
.
process_mesh
=
process_mesh
x_tensor_dist_attr
.
process_mesh
=
process_mesh
self
.
x_dist_tensor_spec
=
DistTensorSpec
(
x_shape
,
x_tensor_dist_attr
)
self
.
x_dist_tensor_spec
=
DistTensorSpec
(
x_shape
,
x_tensor_dist_attr
)
self
.
out_dist_tensor_spec
=
DistTensorSpec
(
self
.
x_dist_tensor_spec
)
self
.
attrs
=
{
self
.
attrs
=
{
'perm'
:
[
0
,
1
,
2
,
3
],
'perm'
:
[
0
,
1
,
2
,
3
],
}
}
...
@@ -149,6 +151,121 @@ class TestTransposeSPMDRule(unittest.TestCase):
...
@@ -149,6 +151,121 @@ class TestTransposeSPMDRule(unittest.TestCase):
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
1
,
-
1
,
0
,
-
1
]
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
1
,
-
1
,
0
,
-
1
]
)
)
def
test_backward_single_mesh_dim
(
self
):
# perm = [1, 0]
# [-1, 0] --> [0, -1], [-1, 0] (output --> input, output)
self
.
attrs
[
'perm'
]
=
[
1
,
0
]
self
.
out_dist_tensor_spec
.
shape
=
[
36
,
64
]
self
.
out_dist_tensor_spec
.
set_dims_mapping
([
-
1
,
0
])
result_dist_attrs
=
self
.
rule
.
infer_backward
(
[
self
.
x_dist_tensor_spec
],
[
self
.
out_dist_tensor_spec
],
self
.
attrs
)
infered_input_dist_attrs
=
result_dist_attrs
[
0
]
infered_output_dist_attrs
=
result_dist_attrs
[
1
]
self
.
assertEqual
(
len
(
result_dist_attrs
),
2
)
self
.
assertEqual
(
len
(
infered_input_dist_attrs
),
1
)
self
.
assertEqual
(
len
(
infered_output_dist_attrs
),
1
)
self
.
assertEqual
(
infered_input_dist_attrs
[
0
].
dims_mapping
,
[
0
,
-
1
])
self
.
assertEqual
(
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
0
])
# perm = [0, 1]
# [0, -1] --> [0, -1], [0, -1] (output --> input, output)
self
.
attrs
[
'perm'
]
=
[
0
,
1
]
self
.
out_dist_tensor_spec
.
shape
=
[
64
,
36
]
self
.
out_dist_tensor_spec
.
set_dims_mapping
([
0
,
-
1
])
result_dist_attrs
=
self
.
rule
.
infer_backward
(
[
self
.
x_dist_tensor_spec
],
[
self
.
out_dist_tensor_spec
],
self
.
attrs
)
infered_input_dist_attrs
=
result_dist_attrs
[
0
]
infered_output_dist_attrs
=
result_dist_attrs
[
1
]
self
.
assertEqual
(
infered_input_dist_attrs
[
0
].
dims_mapping
,
[
0
,
-
1
])
self
.
assertEqual
(
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
0
,
-
1
])
# perm = [0, 2, 3, 1]
# [-1, 0, -1, -1] --> [-1, -1, 0, -1], [-1, 0, -1, -1] (output --> input, output)
self
.
x_dist_tensor_spec
.
shape
=
[
64
,
48
,
36
,
24
]
self
.
attrs
[
'perm'
]
=
[
0
,
2
,
3
,
1
]
self
.
out_dist_tensor_spec
.
shape
=
[
64
,
36
,
24
,
48
]
self
.
out_dist_tensor_spec
.
set_dims_mapping
([
-
1
,
0
,
-
1
,
-
1
])
result_dist_attrs
=
self
.
rule
.
infer_backward
(
[
self
.
x_dist_tensor_spec
],
[
self
.
out_dist_tensor_spec
],
self
.
attrs
)
infered_input_dist_attrs
=
result_dist_attrs
[
0
]
infered_output_dist_attrs
=
result_dist_attrs
[
1
]
self
.
assertEqual
(
infered_input_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
-
1
,
0
,
-
1
]
)
self
.
assertEqual
(
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
0
,
-
1
,
-
1
]
)
def
test_backward_multi_mesh_dim
(
self
):
process_mesh
=
auto
.
ProcessMesh
(
mesh
=
[[
0
,
1
,
2
],
[
3
,
4
,
5
]])
self
.
x_dist_tensor_spec
.
set_process_mesh
(
process_mesh
)
self
.
x_dist_tensor_spec
.
shape
=
[
64
,
48
,
36
,
24
]
self
.
out_dist_tensor_spec
.
set_process_mesh
(
process_mesh
)
# perm = [0, 2, 3, 1]
# [-1, 1, -1, 0] --> [-1, 0, 1, -1], [-1, 1, -1, 0] (output --> input, output)
self
.
attrs
[
'perm'
]
=
[
0
,
2
,
3
,
1
]
self
.
out_dist_tensor_spec
.
shape
=
[
64
,
36
,
24
,
48
]
self
.
out_dist_tensor_spec
.
set_dims_mapping
([
-
1
,
1
,
-
1
,
0
])
result_dist_attrs
=
self
.
rule
.
infer_backward
(
[
self
.
x_dist_tensor_spec
],
[
self
.
out_dist_tensor_spec
],
self
.
attrs
)
infered_input_dist_attrs
=
result_dist_attrs
[
0
]
infered_output_dist_attrs
=
result_dist_attrs
[
1
]
self
.
assertEqual
(
len
(
result_dist_attrs
),
2
)
self
.
assertEqual
(
len
(
infered_input_dist_attrs
),
1
)
self
.
assertEqual
(
len
(
infered_output_dist_attrs
),
1
)
self
.
assertEqual
(
infered_input_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
0
,
1
,
-
1
]
)
self
.
assertEqual
(
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
1
,
-
1
,
0
]
)
# perm = [0, 2, 3, 1]
# [-1, -1, -1, -1] --> [-1, -1, -1, -1], [-1, -1, -1, -1] (output --> input, output)
self
.
attrs
[
'perm'
]
=
[
0
,
2
,
3
,
1
]
self
.
out_dist_tensor_spec
.
set_dims_mapping
([
-
1
,
-
1
,
-
1
,
-
1
])
result_dist_attrs
=
self
.
rule
.
infer_backward
(
[
self
.
x_dist_tensor_spec
],
[
self
.
out_dist_tensor_spec
],
self
.
attrs
)
infered_input_dist_attrs
=
result_dist_attrs
[
0
]
infered_output_dist_attrs
=
result_dist_attrs
[
1
]
self
.
assertEqual
(
infered_input_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
-
1
,
-
1
,
-
1
]
)
self
.
assertEqual
(
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
-
1
,
-
1
,
-
1
]
)
# perm = [-1, 0, -2, 1]
# [1, -1, 0, -1] --> [-1, -1, 0, 1], [1, -1, 0, -1] (output --> input, output)
self
.
x_dist_tensor_spec
.
shape
=
[
64
,
48
,
36
,
24
]
self
.
attrs
[
'perm'
]
=
[
-
1
,
0
,
-
2
,
1
]
self
.
out_dist_tensor_spec
.
shape
=
[
24
,
64
,
36
,
48
]
self
.
out_dist_tensor_spec
.
set_dims_mapping
([
1
,
-
1
,
0
,
-
1
])
result_dist_attrs
=
self
.
rule
.
infer_backward
(
[
self
.
x_dist_tensor_spec
],
[
self
.
out_dist_tensor_spec
],
self
.
attrs
)
infered_input_dist_attrs
=
result_dist_attrs
[
0
]
infered_output_dist_attrs
=
result_dist_attrs
[
1
]
self
.
assertEqual
(
infered_input_dist_attrs
[
0
].
dims_mapping
,
[
-
1
,
-
1
,
0
,
1
]
)
self
.
assertEqual
(
infered_output_dist_attrs
[
0
].
dims_mapping
,
[
1
,
-
1
,
0
,
-
1
]
)
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
unittest
.
main
()
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录