Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
DeepSpeech
提交
75997d82
D
DeepSpeech
项目概览
PaddlePaddle
/
DeepSpeech
大约 2 年 前同步成功
通知
210
Star
8425
Fork
1598
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
245
列表
看板
标记
里程碑
合并请求
3
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DeepSpeech
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
245
Issue
245
列表
看板
标记
里程碑
合并请求
3
合并请求
3
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
75997d82
编写于
7月 15, 2022
作者:
H
huangyuxin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
merge
上级
30ba16e1
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
10 addition
and
210 deletion
+10
-210
paddlespeech/s2t/__init__.py
paddlespeech/s2t/__init__.py
+0
-63
paddlespeech/s2t/models/u2/u2.py
paddlespeech/s2t/models/u2/u2.py
+1
-1
paddlespeech/s2t/modules/align.py
paddlespeech/s2t/modules/align.py
+7
-8
paddlespeech/s2t/modules/attention.py
paddlespeech/s2t/modules/attention.py
+2
-2
paddlespeech/s2t/modules/initializer.py
paddlespeech/s2t/modules/initializer.py
+0
-136
未找到文件。
paddlespeech/s2t/__init__.py
浏览文件 @
75997d82
...
...
@@ -18,7 +18,6 @@ from typing import Union
import
paddle
from
paddle
import
nn
from
paddle.fluid
import
core
from
paddle.nn
import
functional
as
F
from
paddlespeech.s2t.utils.log
import
Log
...
...
@@ -39,46 +38,6 @@ paddle.long = 'int64'
paddle
.
uint16
=
'uint16'
paddle
.
cdouble
=
'complex128'
def
convert_dtype_to_string
(
tensor_dtype
):
"""
Convert the data type in numpy to the data type in Paddle
Args:
tensor_dtype(core.VarDesc.VarType): the data type in numpy.
Returns:
core.VarDesc.VarType: the data type in Paddle.
"""
dtype
=
tensor_dtype
if
dtype
==
core
.
VarDesc
.
VarType
.
FP32
:
return
paddle
.
float32
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP64
:
return
paddle
.
float64
elif
dtype
==
core
.
VarDesc
.
VarType
.
FP16
:
return
paddle
.
float16
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT32
:
return
paddle
.
int32
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT16
:
return
paddle
.
int16
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT64
:
return
paddle
.
int64
elif
dtype
==
core
.
VarDesc
.
VarType
.
BOOL
:
return
paddle
.
bool
elif
dtype
==
core
.
VarDesc
.
VarType
.
BF16
:
# since there is still no support for bfloat16 in NumPy,
# uint16 is used for casting bfloat16
return
paddle
.
uint16
elif
dtype
==
core
.
VarDesc
.
VarType
.
UINT8
:
return
paddle
.
uint8
elif
dtype
==
core
.
VarDesc
.
VarType
.
INT8
:
return
paddle
.
int8
elif
dtype
==
core
.
VarDesc
.
VarType
.
COMPLEX64
:
return
paddle
.
complex64
elif
dtype
==
core
.
VarDesc
.
VarType
.
COMPLEX128
:
return
paddle
.
complex128
else
:
raise
ValueError
(
"Not supported tensor dtype %s"
%
dtype
)
if
not
hasattr
(
paddle
,
'softmax'
):
logger
.
debug
(
"register user softmax to paddle, remove this when fixed!"
)
setattr
(
paddle
,
'softmax'
,
paddle
.
nn
.
functional
.
softmax
)
...
...
@@ -155,28 +114,6 @@ if not hasattr(paddle.Tensor, 'new_full'):
paddle
.
Tensor
.
new_full
=
new_full
paddle
.
static
.
Variable
.
new_full
=
new_full
def
eq
(
xs
:
paddle
.
Tensor
,
ys
:
Union
[
paddle
.
Tensor
,
float
])
->
paddle
.
Tensor
:
if
convert_dtype_to_string
(
xs
.
dtype
)
==
paddle
.
bool
:
xs
=
xs
.
astype
(
paddle
.
int
)
return
xs
.
equal
(
paddle
.
to_tensor
(
ys
,
dtype
=
convert_dtype_to_string
(
xs
.
dtype
),
place
=
xs
.
place
))
if
not
hasattr
(
paddle
.
Tensor
,
'eq'
):
logger
.
debug
(
"override eq of paddle.Tensor if exists or register, remove this when fixed!"
)
paddle
.
Tensor
.
eq
=
eq
paddle
.
static
.
Variable
.
eq
=
eq
if
not
hasattr
(
paddle
,
'eq'
):
logger
.
debug
(
"override eq of paddle if exists or register, remove this when fixed!"
)
paddle
.
eq
=
eq
def
contiguous
(
xs
:
paddle
.
Tensor
)
->
paddle
.
Tensor
:
return
xs
...
...
paddlespeech/s2t/models/u2/u2.py
浏览文件 @
75997d82
...
...
@@ -318,7 +318,7 @@ class U2BaseModel(ASRInterface, nn.Layer):
dim
=
1
)
# (B*N, i+1)
# 2.6 Update end flag
end_flag
=
paddle
.
eq
(
hyps
[:,
-
1
],
self
.
eos
).
view
(
-
1
,
1
)
end_flag
=
paddle
.
eq
ual
(
hyps
[:,
-
1
],
self
.
eos
).
view
(
-
1
,
1
)
# 3. Select best of best
scores
=
scores
.
view
(
batch_size
,
beam_size
)
...
...
paddlespeech/s2t/modules/align.py
浏览文件 @
75997d82
...
...
@@ -13,8 +13,7 @@
# limitations under the License.
import
paddle
from
paddle
import
nn
from
paddlespeech.s2t.modules.initializer
import
KaimingUniform
import
math
"""
To align the initializer between paddle and torch,
the API below are set defalut initializer with priority higger than global initializer.
...
...
@@ -82,10 +81,10 @@ class Linear(nn.Linear):
name
=
None
):
if
weight_attr
is
None
:
if
global_init_type
==
"kaiming_uniform"
:
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
KaimingUniform
(
))
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
nn
.
initializer
.
KaimingUniform
(
fan_in
=
None
,
negative_slope
=
math
.
sqrt
(
5
),
nonlinearity
=
'leaky_relu'
))
if
bias_attr
is
None
:
if
global_init_type
==
"kaiming_uniform"
:
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
KaimingUniform
(
))
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
nn
.
initializer
.
KaimingUniform
(
fan_in
=
None
,
negative_slope
=
math
.
sqrt
(
5
),
nonlinearity
=
'leaky_relu'
))
super
(
Linear
,
self
).
__init__
(
in_features
,
out_features
,
weight_attr
,
bias_attr
,
name
)
...
...
@@ -105,10 +104,10 @@ class Conv1D(nn.Conv1D):
data_format
=
'NCL'
):
if
weight_attr
is
None
:
if
global_init_type
==
"kaiming_uniform"
:
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
KaimingUniform
(
))
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
nn
.
initializer
.
KaimingUniform
(
fan_in
=
None
,
negative_slope
=
math
.
sqrt
(
5
),
nonlinearity
=
'leaky_relu'
))
if
bias_attr
is
None
:
if
global_init_type
==
"kaiming_uniform"
:
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
KaimingUniform
(
))
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
nn
.
initializer
.
KaimingUniform
(
fan_in
=
None
,
negative_slope
=
math
.
sqrt
(
5
),
nonlinearity
=
'leaky_relu'
))
super
(
Conv1D
,
self
).
__init__
(
in_channels
,
out_channels
,
kernel_size
,
stride
,
padding
,
dilation
,
groups
,
padding_mode
,
weight_attr
,
bias_attr
,
data_format
)
...
...
@@ -129,10 +128,10 @@ class Conv2D(nn.Conv2D):
data_format
=
'NCHW'
):
if
weight_attr
is
None
:
if
global_init_type
==
"kaiming_uniform"
:
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
KaimingUniform
(
))
weight_attr
=
paddle
.
ParamAttr
(
initializer
=
nn
.
initializer
.
KaimingUniform
(
fan_in
=
None
,
negative_slope
=
math
.
sqrt
(
5
),
nonlinearity
=
'leaky_relu'
))
if
bias_attr
is
None
:
if
global_init_type
==
"kaiming_uniform"
:
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
KaimingUniform
(
))
bias_attr
=
paddle
.
ParamAttr
(
initializer
=
nn
.
initializer
.
KaimingUniform
(
fan_in
=
None
,
negative_slope
=
math
.
sqrt
(
5
),
nonlinearity
=
'leaky_relu'
))
super
(
Conv2D
,
self
).
__init__
(
in_channels
,
out_channels
,
kernel_size
,
stride
,
padding
,
dilation
,
groups
,
padding_mode
,
weight_attr
,
bias_attr
,
data_format
)
paddlespeech/s2t/modules/attention.py
浏览文件 @
75997d82
...
...
@@ -109,7 +109,7 @@ class MultiHeadedAttention(nn.Layer):
# 1. onnx(16/-1, -1/-1, 16/0)
# 2. jit (16/-1, -1/-1, 16/0, 16/4)
if
paddle
.
shape
(
mask
)[
2
]
>
0
:
# time2 > 0
mask
=
mask
.
unsqueeze
(
1
).
eq
(
0
)
# (batch, 1, *, time2)
mask
=
mask
.
unsqueeze
(
1
).
eq
ual
(
0
)
# (batch, 1, *, time2)
# for last chunk, time2 might be larger than scores.size(-1)
mask
=
mask
[:,
:,
:,
:
paddle
.
shape
(
scores
)[
-
1
]]
scores
=
scores
.
masked_fill
(
mask
,
-
float
(
'inf'
))
...
...
@@ -321,4 +321,4 @@ class RelPositionMultiHeadedAttention(MultiHeadedAttention):
scores
=
(
matrix_ac
+
matrix_bd
)
/
math
.
sqrt
(
self
.
d_k
)
# (batch, head, time1, time2)
return
self
.
forward_attention
(
v
,
scores
,
mask
),
new_cache
\ No newline at end of file
return
self
.
forward_attention
(
v
,
scores
,
mask
),
new_cache
paddlespeech/s2t/modules/initializer.py
浏览文件 @
75997d82
...
...
@@ -12,142 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import
numpy
as
np
from
paddle.fluid
import
framework
from
paddle.fluid
import
unique_name
from
paddle.fluid.core
import
VarDesc
from
paddle.fluid.initializer
import
MSRAInitializer
__all__
=
[
'KaimingUniform'
]
class
KaimingUniform
(
MSRAInitializer
):
r
"""Implements the Kaiming Uniform initializer
This class implements the weight initialization from the paper
`Delving Deep into Rectifiers: Surpassing Human-Level Performance on
ImageNet Classification <https://arxiv.org/abs/1502.01852>`_
by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
robust initialization method that particularly considers the rectifier
nonlinearities.
In case of Uniform distribution, the range is [-x, x], where
.. math::
x = \sqrt{\frac{1.0}{fan\_in}}
In case of Normal distribution, the mean is 0 and the standard deviation
is
.. math::
\sqrt{\\frac{2.0}{fan\_in}}
Args:
fan_in (float32|None): fan_in for Kaiming uniform Initializer. If None, it is\
inferred from the variable. default is None.
Note:
It is recommended to set fan_in to None for most cases.
Examples:
.. code-block:: python
import paddle
import paddle.nn as nn
linear = nn.Linear(2,
4,
weight_attr=nn.initializer.KaimingUniform())
data = paddle.rand([30, 10, 2], dtype='float32')
res = linear(data)
"""
def
__init__
(
self
,
fan_in
=
None
):
super
(
KaimingUniform
,
self
).
__init__
(
uniform
=
True
,
fan_in
=
fan_in
,
seed
=
0
)
def
__call__
(
self
,
var
,
block
=
None
):
"""Initialize the input tensor with MSRA initialization.
Args:
var(Tensor): Tensor that needs to be initialized.
block(Block, optional): The block in which initialization ops
should be added. Used in static graph only, default None.
Returns:
The initialization op
"""
block
=
self
.
_check_block
(
block
)
assert
isinstance
(
var
,
framework
.
Variable
)
assert
isinstance
(
block
,
framework
.
Block
)
f_in
,
f_out
=
self
.
_compute_fans
(
var
)
# If fan_in is passed, use it
fan_in
=
f_in
if
self
.
_fan_in
is
None
else
self
.
_fan_in
if
self
.
_seed
==
0
:
self
.
_seed
=
block
.
program
.
random_seed
# to be compatible of fp16 initalizers
if
var
.
dtype
==
VarDesc
.
VarType
.
FP16
or
(
var
.
dtype
==
VarDesc
.
VarType
.
BF16
and
not
self
.
_uniform
):
out_dtype
=
VarDesc
.
VarType
.
FP32
out_var
=
block
.
create_var
(
name
=
unique_name
.
generate
(
"."
.
join
([
'masra_init'
,
var
.
name
,
'tmp'
])),
shape
=
var
.
shape
,
dtype
=
out_dtype
,
type
=
VarDesc
.
VarType
.
LOD_TENSOR
,
persistable
=
False
)
else
:
out_dtype
=
var
.
dtype
out_var
=
var
if
self
.
_uniform
:
limit
=
np
.
sqrt
(
1.0
/
float
(
fan_in
))
op
=
block
.
append_op
(
type
=
"uniform_random"
,
inputs
=
{},
outputs
=
{
"Out"
:
out_var
},
attrs
=
{
"shape"
:
out_var
.
shape
,
"dtype"
:
int
(
out_dtype
),
"min"
:
-
limit
,
"max"
:
limit
,
"seed"
:
self
.
_seed
},
stop_gradient
=
True
)
else
:
std
=
np
.
sqrt
(
2.0
/
float
(
fan_in
))
op
=
block
.
append_op
(
type
=
"gaussian_random"
,
outputs
=
{
"Out"
:
out_var
},
attrs
=
{
"shape"
:
out_var
.
shape
,
"dtype"
:
int
(
out_dtype
),
"mean"
:
0.0
,
"std"
:
std
,
"seed"
:
self
.
_seed
},
stop_gradient
=
True
)
if
var
.
dtype
==
VarDesc
.
VarType
.
FP16
or
(
var
.
dtype
==
VarDesc
.
VarType
.
BF16
and
not
self
.
_uniform
):
block
.
append_op
(
type
=
"cast"
,
inputs
=
{
"X"
:
out_var
},
outputs
=
{
"Out"
:
var
},
attrs
=
{
"in_dtype"
:
out_var
.
dtype
,
"out_dtype"
:
var
.
dtype
})
if
not
framework
.
in_dygraph_mode
():
var
.
op
=
op
return
op
class
DefaultInitializerContext
(
object
):
"""
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录