Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cli/paraformer] ali-paraformer inference #2067

Merged
merged 21 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
merge main
  • Loading branch information
Mddct committed Oct 24, 2023
commit 30b5677652a119230ce87bed68a34e140ba77c14
7 changes: 1 addition & 6 deletions wenet/paraformer/ali_paraformer/assets/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ encoder_conf:
input_dim: 80
output_dim: 8404
paraformer: true
is_json_cmvn: True
# decoder related
decoder: SanmDecoder
decoder_conf:
Expand All @@ -43,9 +44,3 @@ cif_predictor_conf:
tail_threshold: 0.45
cnn_groups: 1
residual: false

# smooth_factor2: 0.25
# noise_threshold2: 0.01
# upsample_times: 3
# use_cif1_cnn: false # TODO: support in the future, has no effect on model wer (timestamp related)
# upsample_type: cnn_blstm # TODO: support in the future, has no effect on model wer (timestamp related)
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def get_args():
required=True,
help='cmvn file of paraformer in wenet style')
parser.add_argument('--dict', required=True, help='dict file')
parser.add_argument('--wav', required=True, help='wav file')
parser.add_argument('--output_file', default=None, help='output file')
args = parser.parse_args()
return args
Expand All @@ -41,37 +40,10 @@ def main():
load_checkpoint(model, args.ali_paraformer)
model.eval()

waveform, sample_rate = torchaudio.load(args.wav)
assert sample_rate == 16000
waveform = waveform * (1 << 15)
waveform = waveform.to(torch.float)
feats = kaldi.fbank(waveform,
num_mel_bins=80,
frame_length=25,
frame_shift=10,
energy_floor=0.0,
sample_frequency=sample_rate)
feats = feats.unsqueeze(0)
feats_lens = torch.tensor([feats.size(1)], dtype=torch.int64)

decode_results = model.decode(['paraformer_greedy_search'],
feats,
feats_lens,
beam_size=10)
print("".join([
char_dict[id]
for id in decode_results['paraformer_greedy_search'][0].tokens
]))

if args.output_file:
script_model = torch.jit.script(model)
script_model.save(args.output_file)

model = torch.jit.load(args.output_file)
out, token_nums = model.forward_paraformer(feats, feats_lens)
print("".join([char_dict[id] for id in out.argmax(-1)[0].numpy()]))
print(token_nums)


if __name__ == "__main__":

Expand Down
19 changes: 8 additions & 11 deletions wenet/paraformer/ali_paraformer/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,21 +461,15 @@ def forward(

class AliParaformer(torch.nn.Module):

def __init__(self,
encoder: SanmEncoder,
decoder: SanmDecoer,
predictor: Predictor,
sos: int = -1,
eos: int = -1):
def __init__(self, encoder: SanmEncoder, decoder: SanmDecoer,
predictor: Predictor):
super().__init__()
self.encoder = encoder
self.decoder = decoder
self.predictor = predictor
self.lfr = LFR()
if eos != -1:
self.eos = eos
if sos != -1:
self.sos = sos
self.sos = 1
self.eos = 2

@torch.jit.ignore(drop=True)
def forward(
Expand Down Expand Up @@ -530,7 +524,10 @@ def decode(self,
assert decoder_out is not None
assert decoder_out_lens is not None
paraformer_beam_result = paraformer_beam_search(
decoder_out, decoder_out_lens, beam_size=beam_size)
decoder_out,
decoder_out_lens,
beam_size=beam_size,
eos=self.eos)
results['paraformer_beam_search'] = paraformer_beam_result

return results
5 changes: 4 additions & 1 deletion wenet/paraformer/paraformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ def decode(self,
assert decoder_out is not None
assert decoder_out_lens is not None
paraformer_beam_result = paraformer_beam_search(
decoder_out, decoder_out_lens, beam_size=beam_size)
decoder_out,
decoder_out_lens,
beam_size=beam_size,
eos=self.eos)
results['paraformer_beam_search'] = paraformer_beam_result

return results
8 changes: 6 additions & 2 deletions wenet/paraformer/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ def paraformer_greedy_search(

def paraformer_beam_search(decoder_out: torch.Tensor,
decoder_out_lens: torch.Tensor,
beam_size: int = 10) -> List[DecodeResult]:
beam_size: int = 10,
eos: int = -1) -> List[DecodeResult]:
mask = make_non_pad_mask(decoder_out_lens)
indices, _ = _batch_beam_search(decoder_out, mask, beam_size=beam_size)
indices, _ = _batch_beam_search(decoder_out,
mask,
beam_size=beam_size,
eos=eos)

best_hyps = indices[:, 0, :]
results = []
Expand Down
8 changes: 5 additions & 3 deletions wenet/utils/init_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ def init_model(configs):
if isinstance(encoder, SanmEncoder):
assert isinstance(decoder, SanmDecoer)
# NOTE(Mddct): only support inference for now
model = AliParaformer(encoder=encoder,
decoder=decoder,
predictor=predictor)
model = AliParaformer(
encoder=encoder,
decoder=decoder,
predictor=predictor,
)
else:
model = Paraformer(vocab_size=vocab_size,
encoder=encoder,
Expand Down
Loading