pull/2427/head
Hui Zhang 2 years ago
parent 322301a6db
commit f95edc382c

@ -1,16 +1,27 @@
# Copyright (c) 2021 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 unittest
import paddle import paddle
import numpy as np
import unittest
# from paddlespeech.audio.utils.tensor_utils import reverse_pad_list import paddlespeech.s2t # noqa: F401
import paddlespeech.s2t
from paddlespeech.audio.utils.tensor_utils import add_sos_eos from paddlespeech.audio.utils.tensor_utils import add_sos_eos
from paddlespeech.audio.utils.tensor_utils import pad_sequence from paddlespeech.audio.utils.tensor_utils import pad_sequence
# from paddlespeech.audio.utils.tensor_utils import reverse_pad_list
def reverse_pad_list(ys_pad: paddle.Tensor, def reverse_pad_list(ys_pad: paddle.Tensor,
ys_lens: paddle.Tensor, ys_lens: paddle.Tensor,
pad_value: float=-1.0) -> paddle.Tensor: pad_value: float=-1.0) -> paddle.Tensor:
@ -33,95 +44,94 @@ def reverse_pad_list(ys_pad: paddle.Tensor,
for y, i in zip(ys_pad, ys_lens)], True, pad_value) for y, i in zip(ys_pad, ys_lens)], True, pad_value)
return r_ys_pad return r_ys_pad
def naive_reverse_pad_list_with_sos_eos(r_hyps, r_hyps_lens, sos=5000, eos=5000, ignore_id=-1):
def naive_reverse_pad_list_with_sos_eos(r_hyps,
r_hyps_lens,
sos=5000,
eos=5000,
ignore_id=-1):
r_hyps = reverse_pad_list(r_hyps, r_hyps_lens, float(ignore_id)) r_hyps = reverse_pad_list(r_hyps, r_hyps_lens, float(ignore_id))
r_hyps, _ = add_sos_eos(r_hyps, sos, eos, ignore_id) r_hyps, _ = add_sos_eos(r_hyps, sos, eos, ignore_id)
return r_hyps return r_hyps
def reverse_pad_list_with_sos_eos(r_hyps, r_hyps_lens, sos=5000, eos=5000, ignore_id=-1):
# >>> r_hyps = reverse_pad_list(r_hyps, r_hyps_lens, float(self.ignore_id)) def reverse_pad_list_with_sos_eos(r_hyps,
# >>> r_hyps, _ = add_sos_eos(r_hyps, self.sos, self.eos, self.ignore_id) r_hyps_lens,
max_len = paddle.max(r_hyps_lens) sos=5000,
index_range = paddle.arange(0, max_len, 1) eos=5000,
seq_len_expand = r_hyps_lens.unsqueeze(1) ignore_id=-1):
seq_mask = seq_len_expand > index_range # (beam, max_len) # >>> r_hyps = reverse_pad_list(r_hyps, r_hyps_lens, float(self.ignore_id))
# >>> r_hyps, _ = add_sos_eos(r_hyps, self.sos, self.eos, self.ignore_id)
index = (seq_len_expand - 1) - index_range # (beam, max_len) max_len = paddle.max(r_hyps_lens)
# >>> index index_range = paddle.arange(0, max_len, 1)
# >>> tensor([[ 2, 1, 0], seq_len_expand = r_hyps_lens.unsqueeze(1)
# >>> [ 2, 1, 0], seq_mask = seq_len_expand > index_range # (beam, max_len)
# >>> [ 0, -1, -2]])
index = index * seq_mask index = (seq_len_expand - 1) - index_range # (beam, max_len)
# >>> index
# >>> index # >>> tensor([[ 2, 1, 0],
# >>> tensor([[2, 1, 0], # >>> [ 2, 1, 0],
# >>> [2, 1, 0], # >>> [ 0, -1, -2]])
# >>> [0, 0, 0]]) index = index * seq_mask
def paddle_gather(x, dim, index):
index_shape = index.shape # >>> index
index_flatten = index.flatten() # >>> tensor([[2, 1, 0],
if dim < 0: # >>> [2, 1, 0],
dim = len(x.shape) + dim # >>> [0, 0, 0]])
nd_index = [] def paddle_gather(x, dim, index):
for k in range(len(x.shape)): index_shape = index.shape
if k == dim: index_flatten = index.flatten()
nd_index.append(index_flatten) if dim < 0:
else: dim = len(x.shape) + dim
reshape_shape = [1] * len(x.shape) nd_index = []
reshape_shape[k] = x.shape[k] for k in range(len(x.shape)):
x_arange = paddle.arange(x.shape[k], dtype=index.dtype) if k == dim:
x_arange = x_arange.reshape(reshape_shape) nd_index.append(index_flatten)
dim_index = paddle.expand(x_arange, index_shape).flatten() else:
nd_index.append(dim_index) reshape_shape = [1] * len(x.shape)
ind2 = paddle.transpose(paddle.stack(nd_index), reshape_shape[k] = x.shape[k]
[1, 0]).astype("int64") x_arange = paddle.arange(x.shape[k], dtype=index.dtype)
paddle_out = paddle.gather_nd(x, ind2).reshape(index_shape) x_arange = x_arange.reshape(reshape_shape)
return paddle_out dim_index = paddle.expand(x_arange, index_shape).flatten()
nd_index.append(dim_index)
r_hyps = paddle_gather(r_hyps, 1, index) ind2 = paddle.transpose(paddle.stack(nd_index), [1, 0]).astype("int64")
# >>> r_hyps paddle_out = paddle.gather_nd(x, ind2).reshape(index_shape)
# >>> tensor([[3, 2, 1], return paddle_out
# >>> [4, 8, 9],
# >>> [2, 2, 2]]) r_hyps = paddle_gather(r_hyps, 1, index)
r_hyps = paddle.where(seq_mask, r_hyps, eos) # >>> r_hyps
# >>> r_hyps # >>> tensor([[3, 2, 1],
# >>> tensor([[3, 2, 1], # >>> [4, 8, 9],
# >>> [4, 8, 9], # >>> [2, 2, 2]])
# >>> [2, eos, eos]]) r_hyps = paddle.where(seq_mask, r_hyps, eos)
B = r_hyps.shape[0] # >>> r_hyps
_sos = paddle.ones([B, 1], dtype=r_hyps.dtype) * sos # >>> tensor([[3, 2, 1],
# r_hyps = paddle.concat([hyps[:, 0:1], r_hyps], axis=1) # >>> [4, 8, 9],
r_hyps = paddle.concat([_sos, r_hyps], axis=1) # >>> [2, eos, eos]])
# >>> r_hyps B = r_hyps.shape[0]
# >>> tensor([[sos, 3, 2, 1], _sos = paddle.ones([B, 1], dtype=r_hyps.dtype) * sos
# >>> [sos, 4, 8, 9], # r_hyps = paddle.concat([hyps[:, 0:1], r_hyps], axis=1)
# >>> [sos, 2, eos, eos]]) r_hyps = paddle.concat([_sos, r_hyps], axis=1)
return r_hyps # >>> r_hyps
# >>> tensor([[sos, 3, 2, 1],
# >>> [sos, 4, 8, 9],
# >>> [sos, 2, eos, eos]])
return r_hyps
class TestU2Model(unittest.TestCase): class TestU2Model(unittest.TestCase):
def setUp(self): def setUp(self):
paddle.set_device('cpu') paddle.set_device('cpu')
self.sos=5000 self.sos = 5000
self.eos=5000 self.eos = 5000
self.ignore_id=-1 self.ignore_id = -1
self.reverse_hyps = paddle.to_tensor( self.reverse_hyps = paddle.to_tensor([[4, 3, 2, 1, -1],
[[ 4, 3, 2, 1, -1], [5, 4, 3, 2, 1]])
[ 5, 4, 3, 2, 1]]
)
self.reverse_hyps_sos_eos = paddle.to_tensor( self.reverse_hyps_sos_eos = paddle.to_tensor(
[[self.sos, 4 , 3 , 2 , 1 , self.eos], [[self.sos, 4, 3, 2, 1, self.eos], [self.sos, 5, 4, 3, 2, 1]])
[self.sos, 5 , 4 , 3 , 2 , 1 ]]
)
self.hyps = paddle.to_tensor(
[
[1, 2, 3, 4, -1],
[1, 2, 3, 4, 5]
]
)
self.hyps = paddle.to_tensor([[1, 2, 3, 4, -1], [1, 2, 3, 4, 5]])
self.hyps_lens = paddle.to_tensor([4, 5], paddle.int32) self.hyps_lens = paddle.to_tensor([4, 5], paddle.int32)
@ -130,16 +140,17 @@ class TestU2Model(unittest.TestCase):
self.assertSequenceEqual(r_hyps.tolist(), self.reverse_hyps.tolist()) self.assertSequenceEqual(r_hyps.tolist(), self.reverse_hyps.tolist())
def test_naive_reverse_pad_list_with_sos_eos(self): def test_naive_reverse_pad_list_with_sos_eos(self):
r_hyps_sos_eos = naive_reverse_pad_list_with_sos_eos(self.hyps, self.hyps_lens) r_hyps_sos_eos = naive_reverse_pad_list_with_sos_eos(self.hyps,
self.assertSequenceEqual(r_hyps_sos_eos.tolist(), self.reverse_hyps_sos_eos.tolist()) self.hyps_lens)
self.assertSequenceEqual(r_hyps_sos_eos.tolist(),
self.reverse_hyps_sos_eos.tolist())
def test_static_reverse_pad_list_with_sos_eos(self): def test_static_reverse_pad_list_with_sos_eos(self):
r_hyps_sos_eos_static = reverse_pad_list_with_sos_eos(self.hyps, self.hyps_lens) r_hyps_sos_eos_static = reverse_pad_list_with_sos_eos(self.hyps,
self.assertSequenceEqual(r_hyps_sos_eos_static.tolist(), self.reverse_hyps_sos_eos.tolist()) self.hyps_lens)
self.assertSequenceEqual(r_hyps_sos_eos_static.tolist(),
self.reverse_hyps_sos_eos.tolist())
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

Loading…
Cancel
Save