Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
93097f9c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
提交
93097f9c
编写于
8月 05, 2019
作者:
G
gavin1332
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add comments
test=develop
上级
4eac955c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
115 addition
and
59 deletion
+115
-59
python/paddle/fluid/layers/collective.py
python/paddle/fluid/layers/collective.py
+113
-59
python/paddle/fluid/transpiler/collective.py
python/paddle/fluid/transpiler/collective.py
+2
-0
未找到文件。
python/paddle/fluid/layers/collective.py
浏览文件 @
93097f9c
...
@@ -298,12 +298,12 @@ class DistributedClassifier(object):
...
@@ -298,12 +298,12 @@ class DistributedClassifier(object):
return
avg_loss
return
avg_loss
def
arc
margin
_classify
(
self
,
def
arc
face
_classify
(
self
,
x
,
x
,
label
,
label
,
margin
=
0.5
,
margin
=
0.5
,
logit_scale
=
64
,
logit_scale
=
64
,
param_attr
=
None
):
param_attr
=
None
):
'''
'''
reference: ArcFace. https://arxiv.org/abs/1801.07698
reference: ArcFace. https://arxiv.org/abs/1801.07698
'''
'''
...
@@ -362,72 +362,126 @@ class DistributedClassifier(object):
...
@@ -362,72 +362,126 @@ class DistributedClassifier(object):
return
avg_loss
return
avg_loss
def
distributed_fc_classify
(
x
,
def
_
distributed_fc_classify
(
x
,
label
,
label
,
class_num
,
class_num
,
nranks
,
nranks
,
rank_id
,
rank_id
,
param_attr
=
None
,
param_attr
=
None
,
use_bias
=
True
,
use_bias
=
True
,
name
=
'dist_fc'
):
name
=
None
):
'''
'''
Classification layer with FC, softmax and cross entropy calculation of
distibuted version in case of too large number of classes.
Args:
x (Variable): The feature representation of the input samples. This
feature will be flattened into 2-D tensor from dimension index
1. E.g. [32, 1024, 1, 1] will be flattened to [32, 1024].
label (Variable): The label corresponding to the input samples.
class_num (integer): The number of classes of the classification problem.
nranks (integer): The number of ranks of distributed trainers.
rank_id (integer): The rank index of the current trainer.
param_attr (ParamAttr, default None): The parameter attribute for
learnable distributed parameters/weights of this layer.
use_bias (float, default 64.0): The scale factor for logit value
of cosine range.
name (str, default None): The name of this layer.
Returns:
Variable: The ArcFace loss.
Examples:
.. code-block:: python
import paddle.fluid as fluid
input = fluid.layers.data(name="input",
shape=[32, 1024],
dtype='float32',
append_batch_size=False)
label = fluid.layers.data(name="label",
shape=[32, 1],
dtype='int64',
append_batch_size=False)
y = fluid.layers.collective.distributed_fc_classify(x=input,
label=label,
class_num=1000,
nranks=8,
rank_id=0)
'''
'''
if
name
is
None
:
name
=
'dist_fc'
helper
=
LayerHelper
(
name
,
**
locals
())
helper
=
LayerHelper
(
name
,
**
locals
())
classifier
=
DistributedClassifier
(
class_num
,
nranks
,
rank_id
,
helper
)
classifier
=
DistributedClassifier
(
class_num
,
nranks
,
rank_id
,
helper
)
return
classifier
.
fc_classify
(
x
,
label
,
param_attr
,
use_bias
)
return
classifier
.
fc_classify
(
x
,
label
,
param_attr
,
use_bias
)
def
distributed_arcmargin
_classify
(
x
,
def
_distributed_arcface
_classify
(
x
,
label
,
label
,
class_num
,
class_num
,
nranks
,
nranks
,
rank_id
,
rank_id
,
margin
=
0.5
,
margin
=
0.5
,
logit_scale
=
64
,
logit_scale
=
64.0
,
param_attr
=
None
,
param_attr
=
None
,
name
=
'dist_fc'
):
name
=
None
):
'''
'''
Classification layer with ArcFace loss of distibuted version in case of
too large number of classes. the equation is
.. math::
L=-
\f
rac{1}{N}\sum^N_{i=1}\log
\f
rac{e^{s(cos(
\t
heta_{y_i}+m))}}{e^{s(cos(
\t
heta_{y_i}+m))}+\sum^n_{j=1,j
\n
eq y_i} e^{scos
\t
heta_{y_i}}}
where the :math: `
\t
heta_{y_i}` is the angle between the feature :math: `x` and
the representation of class :math: `i`. The details of ArcFace loss
could be referred to https://arxiv.org/abs/1801.07698.
Args:
x (Variable): The feature representation of the input samples. This
feature will be flattened into 2-D tensor from dimension index
1. E.g. [32, 1024, 1, 1] will be flattened to [32, 1024].
label (Variable): The label corresponding to the input samples.
class_num (integer): The number of classes of the classification problem.
nranks (integer): The number of ranks of distributed trainers.
rank_id (integer): The rank index of the current trainer.
margin (float, default 0.5): The angular margin penalty to enhance
the intra-class compactness and inter-class discrepancy.
logit_scale (float, default 64.0): The scale factor for logit value
of cosine range.
param_attr (ParamAttr, default None): The parameter attribute for
learnable distributed parameters/weights of this layer.
name (str, default None): The name of this layer.
Returns:
Variable: The ArcFace loss.
Examples:
.. code-block:: python
import paddle.fluid as fluid
input = fluid.layers.data(name="input",
shape=[32, 1024],
dtype='float32',
append_batch_size=False)
label = fluid.layers.data(name="label",
shape=[32, 1],
dtype='int64',
append_batch_size=False)
y = fluid.layers.collective.distributed_arcface_classify(x=input,
label=label,
class_num=1000,
nranks=8,
rank_id=0)
'''
'''
if
name
is
None
:
name
=
'dist_fc'
helper
=
LayerHelper
(
name
,
**
locals
())
helper
=
LayerHelper
(
name
,
**
locals
())
classifier
=
DistributedClassifier
(
class_num
,
nranks
,
rank_id
,
helper
)
classifier
=
DistributedClassifier
(
class_num
,
nranks
,
rank_id
,
helper
)
return
classifier
.
arc
margin
_classify
(
return
classifier
.
arc
face
_classify
(
x
=
x
,
x
=
x
,
label
=
label
,
label
=
label
,
margin
=
margin
,
margin
=
margin
,
logit_scale
=
logit_scale
,
logit_scale
=
logit_scale
,
param_attr
=
param_attr
)
param_attr
=
param_attr
)
def
distributed_fc
(
x
,
out_dim
,
nranks
,
rank_id
,
param_attr
=
None
,
use_bias
=
True
,
name
=
'dist_fc'
):
'''
'''
helper
=
LayerHelper
(
name
,
**
locals
())
classifier
=
DistributedClassifier
(
out_dim
,
nranks
,
rank_id
,
helper
)
weight
,
bias
=
classifier
.
create_parameter
(
dtype
=
x
.
dtype
,
in_dim
=
x
.
shape
[
-
1
],
param_attr
=
param_attr
,
use_bias
=
use_bias
)
x_all
=
_c_allgather
(
x
,
nranks
=
self
.
nranks
,
use_calc_stream
=
True
)
label_all
=
_c_allgather
(
label
,
nranks
=
self
.
nranks
,
use_calc_stream
=
True
)
shard_fc
=
nn
.
mul
(
x_all
,
weight
)
if
use_bias
:
shard_fc
=
nn
.
elementwise_add
(
shard_fc
,
bias
)
# sample code
#if not classifier.is_equal_division:
# shard_fc = nn.pad(shard_fc)
#fc = _c_slice_allgather(shard_fc,
# nranks=nranks,
# rank_id=rank_id)
#if not classifier.is_equal_division:
# fc = nn.depad(fc)
#return fc
raise
NotImplementedError
(
'distributed_fc'
)
python/paddle/fluid/transpiler/collective.py
浏览文件 @
93097f9c
...
@@ -376,6 +376,8 @@ class LocalSGD(Collective):
...
@@ -376,6 +376,8 @@ class LocalSGD(Collective):
class
DistributedClassificationOptimizer
(
object
):
class
DistributedClassificationOptimizer
(
object
):
'''
'''
A optimizer wrapper to generate backward network for distributed
classification training of model parallelism.
'''
'''
def
__init__
(
self
,
optimizer
,
batch_size
):
def
__init__
(
self
,
optimizer
,
batch_size
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录