mirror of
https://github.com/k2-fsa/icefall.git
synced 2025-08-09 01:52:41 +00:00
symlink copied files to librispeech recipe dir
This commit is contained in:
parent
0e868049a6
commit
6e81d9aa5b
File diff suppressed because it is too large
Load Diff
1
egs/mls_english/ASR/zipformer/beam_search.py
Symbolic link
1
egs/mls_english/ASR/zipformer/beam_search.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/beam_search.py
|
File diff suppressed because it is too large
Load Diff
1
egs/mls_english/ASR/zipformer/ctc_decode.py
Symbolic link
1
egs/mls_english/ASR/zipformer/ctc_decode.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/ctc_decode.py
|
@ -1,148 +0,0 @@
|
|||||||
# Copyright 2022 Xiaomi Corp. (authors: Wei Kang,
|
|
||||||
# Zengwei Yao)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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 math
|
|
||||||
from typing import List, Optional, Tuple
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import torch
|
|
||||||
from beam_search import Hypothesis, HypothesisList
|
|
||||||
|
|
||||||
from icefall.utils import AttributeDict
|
|
||||||
|
|
||||||
|
|
||||||
class DecodeStream(object):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
params: AttributeDict,
|
|
||||||
cut_id: str,
|
|
||||||
initial_states: List[torch.Tensor],
|
|
||||||
decoding_graph: Optional[k2.Fsa] = None,
|
|
||||||
device: torch.device = torch.device("cpu"),
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
initial_states:
|
|
||||||
Initial decode states of the model, e.g. the return value of
|
|
||||||
`get_init_state` in conformer.py
|
|
||||||
decoding_graph:
|
|
||||||
Decoding graph used for decoding, may be a TrivialGraph or a HLG.
|
|
||||||
Used only when decoding_method is fast_beam_search.
|
|
||||||
device:
|
|
||||||
The device to run this stream.
|
|
||||||
"""
|
|
||||||
if params.decoding_method == "fast_beam_search":
|
|
||||||
assert decoding_graph is not None
|
|
||||||
assert device == decoding_graph.device
|
|
||||||
|
|
||||||
self.params = params
|
|
||||||
self.cut_id = cut_id
|
|
||||||
self.LOG_EPS = math.log(1e-10)
|
|
||||||
|
|
||||||
self.states = initial_states
|
|
||||||
|
|
||||||
# It contains a 2-D tensors representing the feature frames.
|
|
||||||
self.features: torch.Tensor = None
|
|
||||||
|
|
||||||
self.num_frames: int = 0
|
|
||||||
# how many frames have been processed. (before subsampling).
|
|
||||||
# we only modify this value in `func:get_feature_frames`.
|
|
||||||
self.num_processed_frames: int = 0
|
|
||||||
|
|
||||||
self._done: bool = False
|
|
||||||
|
|
||||||
# The transcript of current utterance.
|
|
||||||
self.ground_truth: str = ""
|
|
||||||
|
|
||||||
# The decoding result (partial or final) of current utterance.
|
|
||||||
self.hyp: List = []
|
|
||||||
|
|
||||||
# how many frames have been processed, at encoder output
|
|
||||||
self.done_frames: int = 0
|
|
||||||
|
|
||||||
# The encoder_embed subsample features (T - 7) // 2
|
|
||||||
# The ConvNeXt module needs (7 - 1) // 2 = 3 frames of right padding after subsampling
|
|
||||||
self.pad_length = 7 + 2 * 3
|
|
||||||
|
|
||||||
if params.decoding_method == "greedy_search":
|
|
||||||
self.hyp = [-1] * (params.context_size - 1) + [params.blank_id]
|
|
||||||
elif params.decoding_method == "modified_beam_search":
|
|
||||||
self.hyps = HypothesisList()
|
|
||||||
self.hyps.add(
|
|
||||||
Hypothesis(
|
|
||||||
ys=[-1] * (params.context_size - 1) + [params.blank_id],
|
|
||||||
log_prob=torch.zeros(1, dtype=torch.float32, device=device),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
elif params.decoding_method == "fast_beam_search":
|
|
||||||
# The rnnt_decoding_stream for fast_beam_search.
|
|
||||||
self.rnnt_decoding_stream: k2.RnntDecodingStream = k2.RnntDecodingStream(
|
|
||||||
decoding_graph
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unsupported decoding method: {params.decoding_method}")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def done(self) -> bool:
|
|
||||||
"""Return True if all the features are processed."""
|
|
||||||
return self._done
|
|
||||||
|
|
||||||
@property
|
|
||||||
def id(self) -> str:
|
|
||||||
return self.cut_id
|
|
||||||
|
|
||||||
def set_features(
|
|
||||||
self,
|
|
||||||
features: torch.Tensor,
|
|
||||||
tail_pad_len: int = 0,
|
|
||||||
) -> None:
|
|
||||||
"""Set features tensor of current utterance."""
|
|
||||||
assert features.dim() == 2, features.dim()
|
|
||||||
self.features = torch.nn.functional.pad(
|
|
||||||
features,
|
|
||||||
(0, 0, 0, self.pad_length + tail_pad_len),
|
|
||||||
mode="constant",
|
|
||||||
value=self.LOG_EPS,
|
|
||||||
)
|
|
||||||
self.num_frames = self.features.size(0)
|
|
||||||
|
|
||||||
def get_feature_frames(self, chunk_size: int) -> Tuple[torch.Tensor, int]:
|
|
||||||
"""Consume chunk_size frames of features"""
|
|
||||||
chunk_length = chunk_size + self.pad_length
|
|
||||||
|
|
||||||
ret_length = min(self.num_frames - self.num_processed_frames, chunk_length)
|
|
||||||
|
|
||||||
ret_features = self.features[
|
|
||||||
self.num_processed_frames : self.num_processed_frames + ret_length # noqa
|
|
||||||
]
|
|
||||||
|
|
||||||
self.num_processed_frames += chunk_size
|
|
||||||
if self.num_processed_frames >= self.num_frames:
|
|
||||||
self._done = True
|
|
||||||
|
|
||||||
return ret_features, ret_length
|
|
||||||
|
|
||||||
def decoding_result(self) -> List[int]:
|
|
||||||
"""Obtain current decoding result."""
|
|
||||||
if self.params.decoding_method == "greedy_search":
|
|
||||||
return self.hyp[self.params.context_size :] # noqa
|
|
||||||
elif self.params.decoding_method == "modified_beam_search":
|
|
||||||
best_hyp = self.hyps.get_most_probable(length_norm=True)
|
|
||||||
return best_hyp.ys[self.params.context_size :] # noqa
|
|
||||||
else:
|
|
||||||
assert self.params.decoding_method == "fast_beam_search"
|
|
||||||
return self.hyp
|
|
1
egs/mls_english/ASR/zipformer/decode_stream.py
Symbolic link
1
egs/mls_english/ASR/zipformer/decode_stream.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/decode_stream.py
|
@ -1,134 +0,0 @@
|
|||||||
# Copyright 2021 Xiaomi Corp. (authors: Fangjun Kuang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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 torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
from scaling import Balancer
|
|
||||||
|
|
||||||
|
|
||||||
class Decoder(nn.Module):
|
|
||||||
"""This class modifies the stateless decoder from the following paper:
|
|
||||||
|
|
||||||
RNN-transducer with stateless prediction network
|
|
||||||
https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=9054419
|
|
||||||
|
|
||||||
It removes the recurrent connection from the decoder, i.e., the prediction
|
|
||||||
network. Different from the above paper, it adds an extra Conv1d
|
|
||||||
right after the embedding layer.
|
|
||||||
|
|
||||||
TODO: Implement https://arxiv.org/pdf/2109.07513.pdf
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
vocab_size: int,
|
|
||||||
decoder_dim: int,
|
|
||||||
blank_id: int,
|
|
||||||
context_size: int,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
vocab_size:
|
|
||||||
Number of tokens of the modeling unit including blank.
|
|
||||||
decoder_dim:
|
|
||||||
Dimension of the input embedding, and of the decoder output.
|
|
||||||
blank_id:
|
|
||||||
The ID of the blank symbol.
|
|
||||||
context_size:
|
|
||||||
Number of previous words to use to predict the next word.
|
|
||||||
1 means bigram; 2 means trigram. n means (n+1)-gram.
|
|
||||||
"""
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.embedding = nn.Embedding(
|
|
||||||
num_embeddings=vocab_size,
|
|
||||||
embedding_dim=decoder_dim,
|
|
||||||
)
|
|
||||||
# the balancers are to avoid any drift in the magnitude of the
|
|
||||||
# embeddings, which would interact badly with parameter averaging.
|
|
||||||
self.balancer = Balancer(
|
|
||||||
decoder_dim,
|
|
||||||
channel_dim=-1,
|
|
||||||
min_positive=0.0,
|
|
||||||
max_positive=1.0,
|
|
||||||
min_abs=0.5,
|
|
||||||
max_abs=1.0,
|
|
||||||
prob=0.05,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.blank_id = blank_id
|
|
||||||
|
|
||||||
assert context_size >= 1, context_size
|
|
||||||
self.context_size = context_size
|
|
||||||
self.vocab_size = vocab_size
|
|
||||||
|
|
||||||
if context_size > 1:
|
|
||||||
self.conv = nn.Conv1d(
|
|
||||||
in_channels=decoder_dim,
|
|
||||||
out_channels=decoder_dim,
|
|
||||||
kernel_size=context_size,
|
|
||||||
padding=0,
|
|
||||||
groups=decoder_dim // 4, # group size == 4
|
|
||||||
bias=False,
|
|
||||||
)
|
|
||||||
self.balancer2 = Balancer(
|
|
||||||
decoder_dim,
|
|
||||||
channel_dim=-1,
|
|
||||||
min_positive=0.0,
|
|
||||||
max_positive=1.0,
|
|
||||||
min_abs=0.5,
|
|
||||||
max_abs=1.0,
|
|
||||||
prob=0.05,
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
# To avoid `RuntimeError: Module 'Decoder' has no attribute 'conv'`
|
|
||||||
# when inference with torch.jit.script and context_size == 1
|
|
||||||
self.conv = nn.Identity()
|
|
||||||
self.balancer2 = nn.Identity()
|
|
||||||
|
|
||||||
def forward(self, y: torch.Tensor, need_pad: bool = True) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
y:
|
|
||||||
A 2-D tensor of shape (N, U).
|
|
||||||
need_pad:
|
|
||||||
True to left pad the input. Should be True during training.
|
|
||||||
False to not pad the input. Should be False during inference.
|
|
||||||
Returns:
|
|
||||||
Return a tensor of shape (N, U, decoder_dim).
|
|
||||||
"""
|
|
||||||
y = y.to(torch.int64)
|
|
||||||
# this stuff about clamp() is a temporary fix for a mismatch
|
|
||||||
# at utterance start, we use negative ids in beam_search.py
|
|
||||||
embedding_out = self.embedding(y.clamp(min=0)) * (y >= 0).unsqueeze(-1)
|
|
||||||
|
|
||||||
embedding_out = self.balancer(embedding_out)
|
|
||||||
|
|
||||||
if self.context_size > 1:
|
|
||||||
embedding_out = embedding_out.permute(0, 2, 1)
|
|
||||||
if need_pad is True:
|
|
||||||
embedding_out = F.pad(embedding_out, pad=(self.context_size - 1, 0))
|
|
||||||
else:
|
|
||||||
# During inference time, there is no need to do extra padding
|
|
||||||
# as we only need one output
|
|
||||||
assert embedding_out.size(-1) == self.context_size
|
|
||||||
embedding_out = self.conv(embedding_out)
|
|
||||||
embedding_out = embedding_out.permute(0, 2, 1)
|
|
||||||
embedding_out = F.relu(embedding_out)
|
|
||||||
embedding_out = self.balancer2(embedding_out)
|
|
||||||
|
|
||||||
return embedding_out
|
|
1
egs/mls_english/ASR/zipformer/decoder.py
Symbolic link
1
egs/mls_english/ASR/zipformer/decoder.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/decoder.py
|
@ -1,43 +0,0 @@
|
|||||||
# Copyright 2021 Xiaomi Corp. (authors: Fangjun Kuang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
from typing import Tuple
|
|
||||||
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
|
|
||||||
|
|
||||||
class EncoderInterface(nn.Module):
|
|
||||||
def forward(
|
|
||||||
self, x: torch.Tensor, x_lens: torch.Tensor
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
A tensor of shape (batch_size, input_seq_len, num_features)
|
|
||||||
containing the input features.
|
|
||||||
x_lens:
|
|
||||||
A tensor of shape (batch_size,) containing the number of frames
|
|
||||||
in `x` before padding.
|
|
||||||
Returns:
|
|
||||||
Return a tuple containing two tensors:
|
|
||||||
- encoder_out, a tensor of (batch_size, out_seq_len, output_dim)
|
|
||||||
containing unnormalized probabilities, i.e., the output of a
|
|
||||||
linear layer.
|
|
||||||
- encoder_out_lens, a tensor of shape (batch_size,) containing
|
|
||||||
the number of frames in `encoder_out` before padding.
|
|
||||||
"""
|
|
||||||
raise NotImplementedError("Please implement it in a subclass")
|
|
1
egs/mls_english/ASR/zipformer/encoder_interface.py
Symbolic link
1
egs/mls_english/ASR/zipformer/encoder_interface.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/encoder_interface.py
|
@ -1,646 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#
|
|
||||||
# Copyright 2023 Xiaomi Corporation (Author: Fangjun Kuang, Wei Kang)
|
|
||||||
# Copyright 2023 Danqing Fu (danqing.fu@gmail.com)
|
|
||||||
|
|
||||||
"""
|
|
||||||
This script exports a transducer model from PyTorch to ONNX.
|
|
||||||
|
|
||||||
We use the pre-trained model from
|
|
||||||
https://huggingface.co/Zengwei/icefall-asr-librispeech-zipformer-2023-05-15
|
|
||||||
as an example to show how to use this file.
|
|
||||||
|
|
||||||
1. Download the pre-trained model
|
|
||||||
|
|
||||||
cd egs/librispeech/ASR
|
|
||||||
|
|
||||||
repo_url=https://huggingface.co/Zengwei/icefall-asr-librispeech-zipformer-2023-05-15
|
|
||||||
GIT_LFS_SKIP_SMUDGE=1 git clone $repo_url
|
|
||||||
repo=$(basename $repo_url)
|
|
||||||
|
|
||||||
pushd $repo
|
|
||||||
git lfs pull --include "exp/pretrained.pt"
|
|
||||||
|
|
||||||
cd exp
|
|
||||||
ln -s pretrained.pt epoch-99.pt
|
|
||||||
popd
|
|
||||||
|
|
||||||
2. Export the model to ONNX
|
|
||||||
|
|
||||||
./zipformer/export-onnx.py \
|
|
||||||
--tokens $repo/data/lang_bpe_500/tokens.txt \
|
|
||||||
--use-averaged-model 0 \
|
|
||||||
--epoch 99 \
|
|
||||||
--avg 1 \
|
|
||||||
--exp-dir $repo/exp \
|
|
||||||
--num-encoder-layers "2,2,3,4,3,2" \
|
|
||||||
--downsampling-factor "1,2,4,8,4,2" \
|
|
||||||
--feedforward-dim "512,768,1024,1536,1024,768" \
|
|
||||||
--num-heads "4,4,4,8,4,4" \
|
|
||||||
--encoder-dim "192,256,384,512,384,256" \
|
|
||||||
--query-head-dim 32 \
|
|
||||||
--value-head-dim 12 \
|
|
||||||
--pos-head-dim 4 \
|
|
||||||
--pos-dim 48 \
|
|
||||||
--encoder-unmasked-dim "192,192,256,256,256,192" \
|
|
||||||
--cnn-module-kernel "31,31,15,15,15,31" \
|
|
||||||
--decoder-dim 512 \
|
|
||||||
--joiner-dim 512 \
|
|
||||||
--causal False \
|
|
||||||
--chunk-size "16,32,64,-1" \
|
|
||||||
--left-context-frames "64,128,256,-1" \
|
|
||||||
--fp16 True
|
|
||||||
It will generate the following 3 files inside $repo/exp:
|
|
||||||
|
|
||||||
- encoder-epoch-99-avg-1.onnx
|
|
||||||
- decoder-epoch-99-avg-1.onnx
|
|
||||||
- joiner-epoch-99-avg-1.onnx
|
|
||||||
|
|
||||||
See ./onnx_pretrained.py and ./onnx_check.py for how to
|
|
||||||
use the exported ONNX models.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import logging
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import Dict, Tuple
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import onnx
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from decoder import Decoder
|
|
||||||
from onnxconverter_common import float16
|
|
||||||
from onnxruntime.quantization import QuantType, quantize_dynamic
|
|
||||||
from scaling_converter import convert_scaled_to_non_scaled
|
|
||||||
from train import add_model_arguments, get_model, get_params
|
|
||||||
from zipformer import Zipformer2
|
|
||||||
|
|
||||||
from icefall.checkpoint import (
|
|
||||||
average_checkpoints,
|
|
||||||
average_checkpoints_with_averaged_model,
|
|
||||||
find_checkpoints,
|
|
||||||
load_checkpoint,
|
|
||||||
)
|
|
||||||
from icefall.utils import make_pad_mask, num_tokens, str2bool
|
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--epoch",
|
|
||||||
type=int,
|
|
||||||
default=28,
|
|
||||||
help="""It specifies the checkpoint to use for averaging.
|
|
||||||
Note: Epoch counts from 0.
|
|
||||||
You can specify --avg to use more checkpoints for model averaging.""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--iter",
|
|
||||||
type=int,
|
|
||||||
default=0,
|
|
||||||
help="""If positive, --epoch is ignored and it
|
|
||||||
will use the checkpoint exp_dir/checkpoint-iter.pt.
|
|
||||||
You can specify --avg to use more checkpoints for model averaging.
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--avg",
|
|
||||||
type=int,
|
|
||||||
default=15,
|
|
||||||
help="Number of checkpoints to average. Automatically select "
|
|
||||||
"consecutive checkpoints before the checkpoint specified by "
|
|
||||||
"'--epoch' and '--iter'",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--use-averaged-model",
|
|
||||||
type=str2bool,
|
|
||||||
default=True,
|
|
||||||
help="Whether to load averaged model. Currently it only supports "
|
|
||||||
"using --epoch. If True, it would decode with the averaged model "
|
|
||||||
"over the epoch range from `epoch-avg` (excluded) to `epoch`."
|
|
||||||
"Actually only the models with epoch number of `epoch-avg` and "
|
|
||||||
"`epoch` are loaded for averaging. ",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--exp-dir",
|
|
||||||
type=str,
|
|
||||||
default="zipformer/exp",
|
|
||||||
help="""It specifies the directory where all training related
|
|
||||||
files, e.g., checkpoints, log, etc, are saved
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--tokens",
|
|
||||||
type=str,
|
|
||||||
default="data/lang_bpe_500/tokens.txt",
|
|
||||||
help="Path to the tokens.txt",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--context-size",
|
|
||||||
type=int,
|
|
||||||
default=2,
|
|
||||||
help="The context size in the decoder. 1 means bigram; 2 means tri-gram",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--fp16",
|
|
||||||
type=str2bool,
|
|
||||||
default=False,
|
|
||||||
help="Whether to export models in fp16",
|
|
||||||
)
|
|
||||||
|
|
||||||
add_model_arguments(parser)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
def add_meta_data(filename: str, meta_data: Dict[str, str]):
|
|
||||||
"""Add meta data to an ONNX model. It is changed in-place.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
filename:
|
|
||||||
Filename of the ONNX model to be changed.
|
|
||||||
meta_data:
|
|
||||||
Key-value pairs.
|
|
||||||
"""
|
|
||||||
model = onnx.load(filename)
|
|
||||||
for key, value in meta_data.items():
|
|
||||||
meta = model.metadata_props.add()
|
|
||||||
meta.key = key
|
|
||||||
meta.value = value
|
|
||||||
|
|
||||||
onnx.save(model, filename)
|
|
||||||
|
|
||||||
|
|
||||||
class OnnxEncoder(nn.Module):
|
|
||||||
"""A wrapper for Zipformer and the encoder_proj from the joiner"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self, encoder: Zipformer2, encoder_embed: nn.Module, encoder_proj: nn.Linear
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
encoder:
|
|
||||||
A Zipformer encoder.
|
|
||||||
encoder_proj:
|
|
||||||
The projection layer for encoder from the joiner.
|
|
||||||
"""
|
|
||||||
super().__init__()
|
|
||||||
self.encoder = encoder
|
|
||||||
self.encoder_embed = encoder_embed
|
|
||||||
self.encoder_proj = encoder_proj
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self,
|
|
||||||
x: torch.Tensor,
|
|
||||||
x_lens: torch.Tensor,
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""Please see the help information of Zipformer.forward
|
|
||||||
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
A 3-D tensor of shape (N, T, C)
|
|
||||||
x_lens:
|
|
||||||
A 1-D tensor of shape (N,). Its dtype is torch.int64
|
|
||||||
Returns:
|
|
||||||
Return a tuple containing:
|
|
||||||
- encoder_out, A 3-D tensor of shape (N, T', joiner_dim)
|
|
||||||
- encoder_out_lens, A 1-D tensor of shape (N,)
|
|
||||||
"""
|
|
||||||
x, x_lens = self.encoder_embed(x, x_lens)
|
|
||||||
src_key_padding_mask = make_pad_mask(x_lens, x.shape[1])
|
|
||||||
x = x.permute(1, 0, 2)
|
|
||||||
encoder_out, encoder_out_lens = self.encoder(x, x_lens, src_key_padding_mask)
|
|
||||||
encoder_out = encoder_out.permute(1, 0, 2)
|
|
||||||
encoder_out = self.encoder_proj(encoder_out)
|
|
||||||
# Now encoder_out is of shape (N, T, joiner_dim)
|
|
||||||
|
|
||||||
return encoder_out, encoder_out_lens
|
|
||||||
|
|
||||||
|
|
||||||
class OnnxDecoder(nn.Module):
|
|
||||||
"""A wrapper for Decoder and the decoder_proj from the joiner"""
|
|
||||||
|
|
||||||
def __init__(self, decoder: Decoder, decoder_proj: nn.Linear):
|
|
||||||
super().__init__()
|
|
||||||
self.decoder = decoder
|
|
||||||
self.decoder_proj = decoder_proj
|
|
||||||
|
|
||||||
def forward(self, y: torch.Tensor) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
y:
|
|
||||||
A 2-D tensor of shape (N, context_size).
|
|
||||||
Returns
|
|
||||||
Return a 2-D tensor of shape (N, joiner_dim)
|
|
||||||
"""
|
|
||||||
need_pad = False
|
|
||||||
decoder_output = self.decoder(y, need_pad=need_pad)
|
|
||||||
decoder_output = decoder_output.squeeze(1)
|
|
||||||
output = self.decoder_proj(decoder_output)
|
|
||||||
|
|
||||||
return output
|
|
||||||
|
|
||||||
|
|
||||||
class OnnxJoiner(nn.Module):
|
|
||||||
"""A wrapper for the joiner"""
|
|
||||||
|
|
||||||
def __init__(self, output_linear: nn.Linear):
|
|
||||||
super().__init__()
|
|
||||||
self.output_linear = output_linear
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
decoder_out: torch.Tensor,
|
|
||||||
) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
encoder_out:
|
|
||||||
A 2-D tensor of shape (N, joiner_dim)
|
|
||||||
decoder_out:
|
|
||||||
A 2-D tensor of shape (N, joiner_dim)
|
|
||||||
Returns:
|
|
||||||
Return a 2-D tensor of shape (N, vocab_size)
|
|
||||||
"""
|
|
||||||
logit = encoder_out + decoder_out
|
|
||||||
logit = self.output_linear(torch.tanh(logit))
|
|
||||||
return logit
|
|
||||||
|
|
||||||
|
|
||||||
def export_encoder_model_onnx(
|
|
||||||
encoder_model: OnnxEncoder,
|
|
||||||
encoder_filename: str,
|
|
||||||
opset_version: int = 11,
|
|
||||||
) -> None:
|
|
||||||
"""Export the given encoder model to ONNX format.
|
|
||||||
The exported model has two inputs:
|
|
||||||
|
|
||||||
- x, a tensor of shape (N, T, C); dtype is torch.float32
|
|
||||||
- x_lens, a tensor of shape (N,); dtype is torch.int64
|
|
||||||
|
|
||||||
and it has two outputs:
|
|
||||||
|
|
||||||
- encoder_out, a tensor of shape (N, T', joiner_dim)
|
|
||||||
- encoder_out_lens, a tensor of shape (N,)
|
|
||||||
|
|
||||||
Args:
|
|
||||||
encoder_model:
|
|
||||||
The input encoder model
|
|
||||||
encoder_filename:
|
|
||||||
The filename to save the exported ONNX model.
|
|
||||||
opset_version:
|
|
||||||
The opset version to use.
|
|
||||||
"""
|
|
||||||
x = torch.zeros(1, 100, 80, dtype=torch.float32)
|
|
||||||
x_lens = torch.tensor([100], dtype=torch.int64)
|
|
||||||
|
|
||||||
encoder_model = torch.jit.trace(encoder_model, (x, x_lens))
|
|
||||||
|
|
||||||
torch.onnx.export(
|
|
||||||
encoder_model,
|
|
||||||
(x, x_lens),
|
|
||||||
encoder_filename,
|
|
||||||
verbose=False,
|
|
||||||
opset_version=opset_version,
|
|
||||||
input_names=["x", "x_lens"],
|
|
||||||
output_names=["encoder_out", "encoder_out_lens"],
|
|
||||||
dynamic_axes={
|
|
||||||
"x": {0: "N", 1: "T"},
|
|
||||||
"x_lens": {0: "N"},
|
|
||||||
"encoder_out": {0: "N", 1: "T"},
|
|
||||||
"encoder_out_lens": {0: "N"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
meta_data = {
|
|
||||||
"model_type": "zipformer2",
|
|
||||||
"version": "1",
|
|
||||||
"model_author": "k2-fsa",
|
|
||||||
"comment": "non-streaming zipformer2",
|
|
||||||
}
|
|
||||||
logging.info(f"meta_data: {meta_data}")
|
|
||||||
|
|
||||||
add_meta_data(filename=encoder_filename, meta_data=meta_data)
|
|
||||||
|
|
||||||
|
|
||||||
def export_decoder_model_onnx(
|
|
||||||
decoder_model: OnnxDecoder,
|
|
||||||
decoder_filename: str,
|
|
||||||
opset_version: int = 11,
|
|
||||||
) -> None:
|
|
||||||
"""Export the decoder model to ONNX format.
|
|
||||||
|
|
||||||
The exported model has one input:
|
|
||||||
|
|
||||||
- y: a torch.int64 tensor of shape (N, decoder_model.context_size)
|
|
||||||
|
|
||||||
and has one output:
|
|
||||||
|
|
||||||
- decoder_out: a torch.float32 tensor of shape (N, joiner_dim)
|
|
||||||
|
|
||||||
Args:
|
|
||||||
decoder_model:
|
|
||||||
The decoder model to be exported.
|
|
||||||
decoder_filename:
|
|
||||||
Filename to save the exported ONNX model.
|
|
||||||
opset_version:
|
|
||||||
The opset version to use.
|
|
||||||
"""
|
|
||||||
context_size = decoder_model.decoder.context_size
|
|
||||||
vocab_size = decoder_model.decoder.vocab_size
|
|
||||||
|
|
||||||
y = torch.zeros(10, context_size, dtype=torch.int64)
|
|
||||||
decoder_model = torch.jit.script(decoder_model)
|
|
||||||
torch.onnx.export(
|
|
||||||
decoder_model,
|
|
||||||
y,
|
|
||||||
decoder_filename,
|
|
||||||
verbose=False,
|
|
||||||
opset_version=opset_version,
|
|
||||||
input_names=["y"],
|
|
||||||
output_names=["decoder_out"],
|
|
||||||
dynamic_axes={
|
|
||||||
"y": {0: "N"},
|
|
||||||
"decoder_out": {0: "N"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
meta_data = {
|
|
||||||
"context_size": str(context_size),
|
|
||||||
"vocab_size": str(vocab_size),
|
|
||||||
}
|
|
||||||
add_meta_data(filename=decoder_filename, meta_data=meta_data)
|
|
||||||
|
|
||||||
|
|
||||||
def export_joiner_model_onnx(
|
|
||||||
joiner_model: nn.Module,
|
|
||||||
joiner_filename: str,
|
|
||||||
opset_version: int = 11,
|
|
||||||
) -> None:
|
|
||||||
"""Export the joiner model to ONNX format.
|
|
||||||
The exported joiner model has two inputs:
|
|
||||||
|
|
||||||
- encoder_out: a tensor of shape (N, joiner_dim)
|
|
||||||
- decoder_out: a tensor of shape (N, joiner_dim)
|
|
||||||
|
|
||||||
and produces one output:
|
|
||||||
|
|
||||||
- logit: a tensor of shape (N, vocab_size)
|
|
||||||
"""
|
|
||||||
joiner_dim = joiner_model.output_linear.weight.shape[1]
|
|
||||||
logging.info(f"joiner dim: {joiner_dim}")
|
|
||||||
|
|
||||||
projected_encoder_out = torch.rand(11, joiner_dim, dtype=torch.float32)
|
|
||||||
projected_decoder_out = torch.rand(11, joiner_dim, dtype=torch.float32)
|
|
||||||
|
|
||||||
torch.onnx.export(
|
|
||||||
joiner_model,
|
|
||||||
(projected_encoder_out, projected_decoder_out),
|
|
||||||
joiner_filename,
|
|
||||||
verbose=False,
|
|
||||||
opset_version=opset_version,
|
|
||||||
input_names=[
|
|
||||||
"encoder_out",
|
|
||||||
"decoder_out",
|
|
||||||
],
|
|
||||||
output_names=["logit"],
|
|
||||||
dynamic_axes={
|
|
||||||
"encoder_out": {0: "N"},
|
|
||||||
"decoder_out": {0: "N"},
|
|
||||||
"logit": {0: "N"},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
meta_data = {
|
|
||||||
"joiner_dim": str(joiner_dim),
|
|
||||||
}
|
|
||||||
add_meta_data(filename=joiner_filename, meta_data=meta_data)
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
|
||||||
def main():
|
|
||||||
args = get_parser().parse_args()
|
|
||||||
args.exp_dir = Path(args.exp_dir)
|
|
||||||
|
|
||||||
params = get_params()
|
|
||||||
params.update(vars(args))
|
|
||||||
|
|
||||||
device = torch.device("cpu")
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
device = torch.device("cuda", 0)
|
|
||||||
|
|
||||||
logging.info(f"device: {device}")
|
|
||||||
|
|
||||||
token_table = k2.SymbolTable.from_file(params.tokens)
|
|
||||||
params.blank_id = token_table["<blk>"]
|
|
||||||
params.vocab_size = num_tokens(token_table) + 1
|
|
||||||
|
|
||||||
logging.info(params)
|
|
||||||
|
|
||||||
logging.info("About to create model")
|
|
||||||
model = get_model(params)
|
|
||||||
|
|
||||||
model.to(device)
|
|
||||||
|
|
||||||
if not params.use_averaged_model:
|
|
||||||
if params.iter > 0:
|
|
||||||
filenames = find_checkpoints(params.exp_dir, iteration=-params.iter)[
|
|
||||||
: params.avg
|
|
||||||
]
|
|
||||||
if len(filenames) == 0:
|
|
||||||
raise ValueError(
|
|
||||||
f"No checkpoints found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
elif len(filenames) < params.avg:
|
|
||||||
raise ValueError(
|
|
||||||
f"Not enough checkpoints ({len(filenames)}) found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
logging.info(f"averaging {filenames}")
|
|
||||||
model.to(device)
|
|
||||||
model.load_state_dict(average_checkpoints(filenames, device=device))
|
|
||||||
elif params.avg == 1:
|
|
||||||
load_checkpoint(f"{params.exp_dir}/epoch-{params.epoch}.pt", model)
|
|
||||||
else:
|
|
||||||
start = params.epoch - params.avg + 1
|
|
||||||
filenames = []
|
|
||||||
for i in range(start, params.epoch + 1):
|
|
||||||
if i >= 1:
|
|
||||||
filenames.append(f"{params.exp_dir}/epoch-{i}.pt")
|
|
||||||
logging.info(f"averaging {filenames}")
|
|
||||||
model.to(device)
|
|
||||||
model.load_state_dict(average_checkpoints(filenames, device=device))
|
|
||||||
else:
|
|
||||||
if params.iter > 0:
|
|
||||||
filenames = find_checkpoints(params.exp_dir, iteration=-params.iter)[
|
|
||||||
: params.avg + 1
|
|
||||||
]
|
|
||||||
if len(filenames) == 0:
|
|
||||||
raise ValueError(
|
|
||||||
f"No checkpoints found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
elif len(filenames) < params.avg + 1:
|
|
||||||
raise ValueError(
|
|
||||||
f"Not enough checkpoints ({len(filenames)}) found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
filename_start = filenames[-1]
|
|
||||||
filename_end = filenames[0]
|
|
||||||
logging.info(
|
|
||||||
"Calculating the averaged model over iteration checkpoints"
|
|
||||||
f" from {filename_start} (excluded) to {filename_end}"
|
|
||||||
)
|
|
||||||
model.to(device)
|
|
||||||
model.load_state_dict(
|
|
||||||
average_checkpoints_with_averaged_model(
|
|
||||||
filename_start=filename_start,
|
|
||||||
filename_end=filename_end,
|
|
||||||
device=device,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
assert params.avg > 0, params.avg
|
|
||||||
start = params.epoch - params.avg
|
|
||||||
assert start >= 1, start
|
|
||||||
filename_start = f"{params.exp_dir}/epoch-{start}.pt"
|
|
||||||
filename_end = f"{params.exp_dir}/epoch-{params.epoch}.pt"
|
|
||||||
logging.info(
|
|
||||||
f"Calculating the averaged model over epoch range from "
|
|
||||||
f"{start} (excluded) to {params.epoch}"
|
|
||||||
)
|
|
||||||
model.to(device)
|
|
||||||
model.load_state_dict(
|
|
||||||
average_checkpoints_with_averaged_model(
|
|
||||||
filename_start=filename_start,
|
|
||||||
filename_end=filename_end,
|
|
||||||
device=device,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
model.to("cpu")
|
|
||||||
model.eval()
|
|
||||||
|
|
||||||
convert_scaled_to_non_scaled(model, inplace=True, is_onnx=True)
|
|
||||||
|
|
||||||
encoder = OnnxEncoder(
|
|
||||||
encoder=model.encoder,
|
|
||||||
encoder_embed=model.encoder_embed,
|
|
||||||
encoder_proj=model.joiner.encoder_proj,
|
|
||||||
)
|
|
||||||
|
|
||||||
decoder = OnnxDecoder(
|
|
||||||
decoder=model.decoder,
|
|
||||||
decoder_proj=model.joiner.decoder_proj,
|
|
||||||
)
|
|
||||||
|
|
||||||
joiner = OnnxJoiner(output_linear=model.joiner.output_linear)
|
|
||||||
|
|
||||||
encoder_num_param = sum([p.numel() for p in encoder.parameters()])
|
|
||||||
decoder_num_param = sum([p.numel() for p in decoder.parameters()])
|
|
||||||
joiner_num_param = sum([p.numel() for p in joiner.parameters()])
|
|
||||||
total_num_param = encoder_num_param + decoder_num_param + joiner_num_param
|
|
||||||
logging.info(f"encoder parameters: {encoder_num_param}")
|
|
||||||
logging.info(f"decoder parameters: {decoder_num_param}")
|
|
||||||
logging.info(f"joiner parameters: {joiner_num_param}")
|
|
||||||
logging.info(f"total parameters: {total_num_param}")
|
|
||||||
|
|
||||||
if params.iter > 0:
|
|
||||||
suffix = f"iter-{params.iter}"
|
|
||||||
else:
|
|
||||||
suffix = f"epoch-{params.epoch}"
|
|
||||||
|
|
||||||
suffix += f"-avg-{params.avg}"
|
|
||||||
|
|
||||||
opset_version = 13
|
|
||||||
|
|
||||||
logging.info("Exporting encoder")
|
|
||||||
encoder_filename = params.exp_dir / f"encoder-{suffix}.onnx"
|
|
||||||
export_encoder_model_onnx(
|
|
||||||
encoder,
|
|
||||||
encoder_filename,
|
|
||||||
opset_version=opset_version,
|
|
||||||
)
|
|
||||||
logging.info(f"Exported encoder to {encoder_filename}")
|
|
||||||
|
|
||||||
logging.info("Exporting decoder")
|
|
||||||
decoder_filename = params.exp_dir / f"decoder-{suffix}.onnx"
|
|
||||||
export_decoder_model_onnx(
|
|
||||||
decoder,
|
|
||||||
decoder_filename,
|
|
||||||
opset_version=opset_version,
|
|
||||||
)
|
|
||||||
logging.info(f"Exported decoder to {decoder_filename}")
|
|
||||||
|
|
||||||
logging.info("Exporting joiner")
|
|
||||||
joiner_filename = params.exp_dir / f"joiner-{suffix}.onnx"
|
|
||||||
export_joiner_model_onnx(
|
|
||||||
joiner,
|
|
||||||
joiner_filename,
|
|
||||||
opset_version=opset_version,
|
|
||||||
)
|
|
||||||
logging.info(f"Exported joiner to {joiner_filename}")
|
|
||||||
|
|
||||||
if params.fp16:
|
|
||||||
logging.info("Generate fp16 models")
|
|
||||||
|
|
||||||
encoder = onnx.load(encoder_filename)
|
|
||||||
encoder_fp16 = float16.convert_float_to_float16(encoder, keep_io_types=True)
|
|
||||||
encoder_filename_fp16 = params.exp_dir / f"encoder-{suffix}.fp16.onnx"
|
|
||||||
onnx.save(encoder_fp16, encoder_filename_fp16)
|
|
||||||
|
|
||||||
decoder = onnx.load(decoder_filename)
|
|
||||||
decoder_fp16 = float16.convert_float_to_float16(decoder, keep_io_types=True)
|
|
||||||
decoder_filename_fp16 = params.exp_dir / f"decoder-{suffix}.fp16.onnx"
|
|
||||||
onnx.save(decoder_fp16, decoder_filename_fp16)
|
|
||||||
|
|
||||||
joiner = onnx.load(joiner_filename)
|
|
||||||
joiner_fp16 = float16.convert_float_to_float16(joiner, keep_io_types=True)
|
|
||||||
joiner_filename_fp16 = params.exp_dir / f"joiner-{suffix}.fp16.onnx"
|
|
||||||
onnx.save(joiner_fp16, joiner_filename_fp16)
|
|
||||||
|
|
||||||
# Generate int8 quantization models
|
|
||||||
# See https://onnxruntime.ai/docs/performance/model-optimizations/quantization.html#data-type-selection
|
|
||||||
|
|
||||||
logging.info("Generate int8 quantization models")
|
|
||||||
|
|
||||||
encoder_filename_int8 = params.exp_dir / f"encoder-{suffix}.int8.onnx"
|
|
||||||
quantize_dynamic(
|
|
||||||
model_input=encoder_filename,
|
|
||||||
model_output=encoder_filename_int8,
|
|
||||||
op_types_to_quantize=["MatMul"],
|
|
||||||
weight_type=QuantType.QInt8,
|
|
||||||
)
|
|
||||||
|
|
||||||
decoder_filename_int8 = params.exp_dir / f"decoder-{suffix}.int8.onnx"
|
|
||||||
quantize_dynamic(
|
|
||||||
model_input=decoder_filename,
|
|
||||||
model_output=decoder_filename_int8,
|
|
||||||
op_types_to_quantize=["MatMul", "Gather"],
|
|
||||||
weight_type=QuantType.QInt8,
|
|
||||||
)
|
|
||||||
|
|
||||||
joiner_filename_int8 = params.exp_dir / f"joiner-{suffix}.int8.onnx"
|
|
||||||
quantize_dynamic(
|
|
||||||
model_input=joiner_filename,
|
|
||||||
model_output=joiner_filename_int8,
|
|
||||||
op_types_to_quantize=["MatMul"],
|
|
||||||
weight_type=QuantType.QInt8,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
|
||||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/export-onnx.py
Symbolic link
1
egs/mls_english/ASR/zipformer/export-onnx.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/export-onnx.py
|
@ -1,525 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#
|
|
||||||
# Copyright 2021-2023 Xiaomi Corporation (Author: Fangjun Kuang,
|
|
||||||
# Zengwei Yao,
|
|
||||||
# Wei Kang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
# This script converts several saved checkpoints
|
|
||||||
# to a single one using model averaging.
|
|
||||||
"""
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
|
|
||||||
Note: This is a example for librispeech dataset, if you are using different
|
|
||||||
dataset, you should change the argument values according to your dataset.
|
|
||||||
|
|
||||||
(1) Export to torchscript model using torch.jit.script()
|
|
||||||
|
|
||||||
- For non-streaming model:
|
|
||||||
|
|
||||||
./zipformer/export.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--epoch 30 \
|
|
||||||
--avg 9 \
|
|
||||||
--jit 1
|
|
||||||
|
|
||||||
It will generate a file `jit_script.pt` in the given `exp_dir`. You can later
|
|
||||||
load it by `torch.jit.load("jit_script.pt")`.
|
|
||||||
|
|
||||||
Check ./jit_pretrained.py for its usage.
|
|
||||||
|
|
||||||
Check https://github.com/k2-fsa/sherpa
|
|
||||||
for how to use the exported models outside of icefall.
|
|
||||||
|
|
||||||
- For streaming model:
|
|
||||||
|
|
||||||
./zipformer/export.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--causal 1 \
|
|
||||||
--chunk-size 16 \
|
|
||||||
--left-context-frames 128 \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--epoch 30 \
|
|
||||||
--avg 9 \
|
|
||||||
--jit 1
|
|
||||||
|
|
||||||
It will generate a file `jit_script_chunk_16_left_128.pt` in the given `exp_dir`.
|
|
||||||
You can later load it by `torch.jit.load("jit_script_chunk_16_left_128.pt")`.
|
|
||||||
|
|
||||||
Check ./jit_pretrained_streaming.py for its usage.
|
|
||||||
|
|
||||||
Check https://github.com/k2-fsa/sherpa
|
|
||||||
for how to use the exported models outside of icefall.
|
|
||||||
|
|
||||||
(2) Export `model.state_dict()`
|
|
||||||
|
|
||||||
- For non-streaming model:
|
|
||||||
|
|
||||||
./zipformer/export.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--epoch 30 \
|
|
||||||
--avg 9
|
|
||||||
|
|
||||||
- For streaming model:
|
|
||||||
|
|
||||||
./zipformer/export.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--causal 1 \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--epoch 30 \
|
|
||||||
--avg 9
|
|
||||||
|
|
||||||
It will generate a file `pretrained.pt` in the given `exp_dir`. You can later
|
|
||||||
load it by `icefall.checkpoint.load_checkpoint()`.
|
|
||||||
|
|
||||||
- For non-streaming model:
|
|
||||||
|
|
||||||
To use the generated file with `zipformer/decode.py`,
|
|
||||||
you can do:
|
|
||||||
|
|
||||||
cd /path/to/exp_dir
|
|
||||||
ln -s pretrained.pt epoch-9999.pt
|
|
||||||
|
|
||||||
cd /path/to/egs/librispeech/ASR
|
|
||||||
./zipformer/decode.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--epoch 9999 \
|
|
||||||
--avg 1 \
|
|
||||||
--max-duration 600 \
|
|
||||||
--decoding-method greedy_search \
|
|
||||||
--bpe-model data/lang_bpe_500/bpe.model
|
|
||||||
|
|
||||||
- For streaming model:
|
|
||||||
|
|
||||||
To use the generated file with `zipformer/decode.py` and `zipformer/streaming_decode.py`, you can do:
|
|
||||||
|
|
||||||
cd /path/to/exp_dir
|
|
||||||
ln -s pretrained.pt epoch-9999.pt
|
|
||||||
|
|
||||||
cd /path/to/egs/librispeech/ASR
|
|
||||||
|
|
||||||
# simulated streaming decoding
|
|
||||||
./zipformer/decode.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--epoch 9999 \
|
|
||||||
--avg 1 \
|
|
||||||
--max-duration 600 \
|
|
||||||
--causal 1 \
|
|
||||||
--chunk-size 16 \
|
|
||||||
--left-context-frames 128 \
|
|
||||||
--decoding-method greedy_search \
|
|
||||||
--bpe-model data/lang_bpe_500/bpe.model
|
|
||||||
|
|
||||||
# chunk-wise streaming decoding
|
|
||||||
./zipformer/streaming_decode.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--epoch 9999 \
|
|
||||||
--avg 1 \
|
|
||||||
--max-duration 600 \
|
|
||||||
--causal 1 \
|
|
||||||
--chunk-size 16 \
|
|
||||||
--left-context-frames 128 \
|
|
||||||
--decoding-method greedy_search \
|
|
||||||
--bpe-model data/lang_bpe_500/bpe.model
|
|
||||||
|
|
||||||
Check ./pretrained.py for its usage.
|
|
||||||
|
|
||||||
Note: If you don't want to train a model from scratch, we have
|
|
||||||
provided one for you. You can get it at
|
|
||||||
|
|
||||||
- non-streaming model:
|
|
||||||
https://huggingface.co/Zengwei/icefall-asr-librispeech-zipformer-2023-05-15
|
|
||||||
|
|
||||||
- streaming model:
|
|
||||||
https://huggingface.co/Zengwei/icefall-asr-librispeech-streaming-zipformer-2023-05-17
|
|
||||||
|
|
||||||
with the following commands:
|
|
||||||
|
|
||||||
sudo apt-get install git-lfs
|
|
||||||
git lfs install
|
|
||||||
git clone https://huggingface.co/Zengwei/icefall-asr-librispeech-zipformer-2023-05-15
|
|
||||||
git clone https://huggingface.co/Zengwei/icefall-asr-librispeech-streaming-zipformer-2023-05-17
|
|
||||||
# You will find the pre-trained models in exp dir
|
|
||||||
"""
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import logging
|
|
||||||
from pathlib import Path
|
|
||||||
from typing import List, Tuple
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import torch
|
|
||||||
from scaling_converter import convert_scaled_to_non_scaled
|
|
||||||
from torch import Tensor, nn
|
|
||||||
from train import add_model_arguments, get_model, get_params
|
|
||||||
|
|
||||||
from icefall.checkpoint import (
|
|
||||||
average_checkpoints,
|
|
||||||
average_checkpoints_with_averaged_model,
|
|
||||||
find_checkpoints,
|
|
||||||
load_checkpoint,
|
|
||||||
)
|
|
||||||
from icefall.utils import make_pad_mask, num_tokens, str2bool
|
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--epoch",
|
|
||||||
type=int,
|
|
||||||
default=30,
|
|
||||||
help="""It specifies the checkpoint to use for decoding.
|
|
||||||
Note: Epoch counts from 1.
|
|
||||||
You can specify --avg to use more checkpoints for model averaging.""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--iter",
|
|
||||||
type=int,
|
|
||||||
default=0,
|
|
||||||
help="""If positive, --epoch is ignored and it
|
|
||||||
will use the checkpoint exp_dir/checkpoint-iter.pt.
|
|
||||||
You can specify --avg to use more checkpoints for model averaging.
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--avg",
|
|
||||||
type=int,
|
|
||||||
default=9,
|
|
||||||
help="Number of checkpoints to average. Automatically select "
|
|
||||||
"consecutive checkpoints before the checkpoint specified by "
|
|
||||||
"'--epoch' and '--iter'",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--use-averaged-model",
|
|
||||||
type=str2bool,
|
|
||||||
default=True,
|
|
||||||
help="Whether to load averaged model. Currently it only supports "
|
|
||||||
"using --epoch. If True, it would decode with the averaged model "
|
|
||||||
"over the epoch range from `epoch-avg` (excluded) to `epoch`."
|
|
||||||
"Actually only the models with epoch number of `epoch-avg` and "
|
|
||||||
"`epoch` are loaded for averaging. ",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--exp-dir",
|
|
||||||
type=str,
|
|
||||||
default="zipformer/exp",
|
|
||||||
help="""It specifies the directory where all training related
|
|
||||||
files, e.g., checkpoints, log, etc, are saved
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--tokens",
|
|
||||||
type=str,
|
|
||||||
default="data/lang_bpe_500/tokens.txt",
|
|
||||||
help="Path to the tokens.txt",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--jit",
|
|
||||||
type=str2bool,
|
|
||||||
default=False,
|
|
||||||
help="""True to save a model after applying torch.jit.script.
|
|
||||||
It will generate a file named jit_script.pt.
|
|
||||||
Check ./jit_pretrained.py for how to use it.
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--context-size",
|
|
||||||
type=int,
|
|
||||||
default=2,
|
|
||||||
help="The context size in the decoder. 1 means bigram; 2 means tri-gram",
|
|
||||||
)
|
|
||||||
|
|
||||||
add_model_arguments(parser)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
class EncoderModel(nn.Module):
|
|
||||||
"""A wrapper for encoder and encoder_embed"""
|
|
||||||
|
|
||||||
def __init__(self, encoder: nn.Module, encoder_embed: nn.Module) -> None:
|
|
||||||
super().__init__()
|
|
||||||
self.encoder = encoder
|
|
||||||
self.encoder_embed = encoder_embed
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self, features: Tensor, feature_lengths: Tensor
|
|
||||||
) -> Tuple[Tensor, Tensor]:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
features: (N, T, C)
|
|
||||||
feature_lengths: (N,)
|
|
||||||
"""
|
|
||||||
x, x_lens = self.encoder_embed(features, feature_lengths)
|
|
||||||
|
|
||||||
src_key_padding_mask = make_pad_mask(x_lens)
|
|
||||||
x = x.permute(1, 0, 2) # (N, T, C) -> (T, N, C)
|
|
||||||
|
|
||||||
encoder_out, encoder_out_lens = self.encoder(x, x_lens, src_key_padding_mask)
|
|
||||||
encoder_out = encoder_out.permute(1, 0, 2) # (T, N, C) ->(N, T, C)
|
|
||||||
|
|
||||||
return encoder_out, encoder_out_lens
|
|
||||||
|
|
||||||
|
|
||||||
class StreamingEncoderModel(nn.Module):
|
|
||||||
"""A wrapper for encoder and encoder_embed"""
|
|
||||||
|
|
||||||
def __init__(self, encoder: nn.Module, encoder_embed: nn.Module) -> None:
|
|
||||||
super().__init__()
|
|
||||||
assert len(encoder.chunk_size) == 1, encoder.chunk_size
|
|
||||||
assert len(encoder.left_context_frames) == 1, encoder.left_context_frames
|
|
||||||
self.chunk_size = encoder.chunk_size[0]
|
|
||||||
self.left_context_len = encoder.left_context_frames[0]
|
|
||||||
|
|
||||||
# The encoder_embed subsample features (T - 7) // 2
|
|
||||||
# The ConvNeXt module needs (7 - 1) // 2 = 3 frames of right padding after subsampling
|
|
||||||
self.pad_length = 7 + 2 * 3
|
|
||||||
|
|
||||||
self.encoder = encoder
|
|
||||||
self.encoder_embed = encoder_embed
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self, features: Tensor, feature_lengths: Tensor, states: List[Tensor]
|
|
||||||
) -> Tuple[Tensor, Tensor, List[Tensor]]:
|
|
||||||
"""Streaming forward for encoder_embed and encoder.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
features: (N, T, C)
|
|
||||||
feature_lengths: (N,)
|
|
||||||
states: a list of Tensors
|
|
||||||
|
|
||||||
Returns encoder outputs, output lengths, and updated states.
|
|
||||||
"""
|
|
||||||
chunk_size = self.chunk_size
|
|
||||||
left_context_len = self.left_context_len
|
|
||||||
|
|
||||||
cached_embed_left_pad = states[-2]
|
|
||||||
x, x_lens, new_cached_embed_left_pad = self.encoder_embed.streaming_forward(
|
|
||||||
x=features,
|
|
||||||
x_lens=feature_lengths,
|
|
||||||
cached_left_pad=cached_embed_left_pad,
|
|
||||||
)
|
|
||||||
assert x.size(1) == chunk_size, (x.size(1), chunk_size)
|
|
||||||
|
|
||||||
src_key_padding_mask = make_pad_mask(x_lens)
|
|
||||||
|
|
||||||
# processed_mask is used to mask out initial states
|
|
||||||
processed_mask = torch.arange(left_context_len, device=x.device).expand(
|
|
||||||
x.size(0), left_context_len
|
|
||||||
)
|
|
||||||
processed_lens = states[-1] # (batch,)
|
|
||||||
# (batch, left_context_size)
|
|
||||||
processed_mask = (processed_lens.unsqueeze(1) <= processed_mask).flip(1)
|
|
||||||
# Update processed lengths
|
|
||||||
new_processed_lens = processed_lens + x_lens
|
|
||||||
|
|
||||||
# (batch, left_context_size + chunk_size)
|
|
||||||
src_key_padding_mask = torch.cat([processed_mask, src_key_padding_mask], dim=1)
|
|
||||||
|
|
||||||
x = x.permute(1, 0, 2) # (N, T, C) -> (T, N, C)
|
|
||||||
encoder_states = states[:-2]
|
|
||||||
|
|
||||||
(
|
|
||||||
encoder_out,
|
|
||||||
encoder_out_lens,
|
|
||||||
new_encoder_states,
|
|
||||||
) = self.encoder.streaming_forward(
|
|
||||||
x=x,
|
|
||||||
x_lens=x_lens,
|
|
||||||
states=encoder_states,
|
|
||||||
src_key_padding_mask=src_key_padding_mask,
|
|
||||||
)
|
|
||||||
encoder_out = encoder_out.permute(1, 0, 2) # (T, N, C) ->(N, T, C)
|
|
||||||
|
|
||||||
new_states = new_encoder_states + [
|
|
||||||
new_cached_embed_left_pad,
|
|
||||||
new_processed_lens,
|
|
||||||
]
|
|
||||||
return encoder_out, encoder_out_lens, new_states
|
|
||||||
|
|
||||||
@torch.jit.export
|
|
||||||
def get_init_states(
|
|
||||||
self,
|
|
||||||
batch_size: int = 1,
|
|
||||||
device: torch.device = torch.device("cpu"),
|
|
||||||
) -> List[torch.Tensor]:
|
|
||||||
"""
|
|
||||||
Returns a list of cached tensors of all encoder layers. For layer-i, states[i*6:(i+1)*6]
|
|
||||||
is (cached_key, cached_nonlin_attn, cached_val1, cached_val2, cached_conv1, cached_conv2).
|
|
||||||
states[-2] is the cached left padding for ConvNeXt module,
|
|
||||||
of shape (batch_size, num_channels, left_pad, num_freqs)
|
|
||||||
states[-1] is processed_lens of shape (batch,), which records the number
|
|
||||||
of processed frames (at 50hz frame rate, after encoder_embed) for each sample in batch.
|
|
||||||
"""
|
|
||||||
states = self.encoder.get_init_states(batch_size, device)
|
|
||||||
|
|
||||||
embed_states = self.encoder_embed.get_init_states(batch_size, device)
|
|
||||||
states.append(embed_states)
|
|
||||||
|
|
||||||
processed_lens = torch.zeros(batch_size, dtype=torch.int32, device=device)
|
|
||||||
states.append(processed_lens)
|
|
||||||
|
|
||||||
return states
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
|
||||||
def main():
|
|
||||||
args = get_parser().parse_args()
|
|
||||||
args.exp_dir = Path(args.exp_dir)
|
|
||||||
|
|
||||||
params = get_params()
|
|
||||||
params.update(vars(args))
|
|
||||||
|
|
||||||
device = torch.device("cpu")
|
|
||||||
# if torch.cuda.is_available():
|
|
||||||
# device = torch.device("cuda", 0)
|
|
||||||
|
|
||||||
logging.info(f"device: {device}")
|
|
||||||
|
|
||||||
token_table = k2.SymbolTable.from_file(params.tokens)
|
|
||||||
params.blank_id = token_table["<blk>"]
|
|
||||||
params.sos_id = params.eos_id = token_table["<sos/eos>"]
|
|
||||||
params.vocab_size = num_tokens(token_table) + 1
|
|
||||||
|
|
||||||
logging.info(params)
|
|
||||||
|
|
||||||
logging.info("About to create model")
|
|
||||||
model = get_model(params)
|
|
||||||
|
|
||||||
if not params.use_averaged_model:
|
|
||||||
if params.iter > 0:
|
|
||||||
filenames = find_checkpoints(params.exp_dir, iteration=-params.iter)[
|
|
||||||
: params.avg
|
|
||||||
]
|
|
||||||
if len(filenames) == 0:
|
|
||||||
raise ValueError(
|
|
||||||
f"No checkpoints found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
elif len(filenames) < params.avg:
|
|
||||||
raise ValueError(
|
|
||||||
f"Not enough checkpoints ({len(filenames)}) found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
logging.info(f"averaging {filenames}")
|
|
||||||
model.load_state_dict(average_checkpoints(filenames, device=device))
|
|
||||||
elif params.avg == 1:
|
|
||||||
load_checkpoint(f"{params.exp_dir}/epoch-{params.epoch}.pt", model)
|
|
||||||
else:
|
|
||||||
start = params.epoch - params.avg + 1
|
|
||||||
filenames = []
|
|
||||||
for i in range(start, params.epoch + 1):
|
|
||||||
if i >= 1:
|
|
||||||
filenames.append(f"{params.exp_dir}/epoch-{i}.pt")
|
|
||||||
logging.info(f"averaging {filenames}")
|
|
||||||
model.load_state_dict(average_checkpoints(filenames, device=device))
|
|
||||||
else:
|
|
||||||
if params.iter > 0:
|
|
||||||
filenames = find_checkpoints(params.exp_dir, iteration=-params.iter)[
|
|
||||||
: params.avg + 1
|
|
||||||
]
|
|
||||||
if len(filenames) == 0:
|
|
||||||
raise ValueError(
|
|
||||||
f"No checkpoints found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
elif len(filenames) < params.avg + 1:
|
|
||||||
raise ValueError(
|
|
||||||
f"Not enough checkpoints ({len(filenames)}) found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
filename_start = filenames[-1]
|
|
||||||
filename_end = filenames[0]
|
|
||||||
logging.info(
|
|
||||||
"Calculating the averaged model over iteration checkpoints"
|
|
||||||
f" from {filename_start} (excluded) to {filename_end}"
|
|
||||||
)
|
|
||||||
model.load_state_dict(
|
|
||||||
average_checkpoints_with_averaged_model(
|
|
||||||
filename_start=filename_start,
|
|
||||||
filename_end=filename_end,
|
|
||||||
device=device,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
assert params.avg > 0, params.avg
|
|
||||||
start = params.epoch - params.avg
|
|
||||||
assert start >= 1, start
|
|
||||||
filename_start = f"{params.exp_dir}/epoch-{start}.pt"
|
|
||||||
filename_end = f"{params.exp_dir}/epoch-{params.epoch}.pt"
|
|
||||||
logging.info(
|
|
||||||
f"Calculating the averaged model over epoch range from "
|
|
||||||
f"{start} (excluded) to {params.epoch}"
|
|
||||||
)
|
|
||||||
model.load_state_dict(
|
|
||||||
average_checkpoints_with_averaged_model(
|
|
||||||
filename_start=filename_start,
|
|
||||||
filename_end=filename_end,
|
|
||||||
device=device,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
model.eval()
|
|
||||||
|
|
||||||
if params.jit is True:
|
|
||||||
convert_scaled_to_non_scaled(model, inplace=True)
|
|
||||||
# We won't use the forward() method of the model in C++, so just ignore
|
|
||||||
# it here.
|
|
||||||
# Otherwise, one of its arguments is a ragged tensor and is not
|
|
||||||
# torch scriptabe.
|
|
||||||
model.__class__.forward = torch.jit.ignore(model.__class__.forward)
|
|
||||||
|
|
||||||
# Wrap encoder and encoder_embed as a module
|
|
||||||
if params.causal:
|
|
||||||
model.encoder = StreamingEncoderModel(model.encoder, model.encoder_embed)
|
|
||||||
chunk_size = model.encoder.chunk_size
|
|
||||||
left_context_len = model.encoder.left_context_len
|
|
||||||
filename = f"jit_script_chunk_{chunk_size}_left_{left_context_len}.pt"
|
|
||||||
else:
|
|
||||||
model.encoder = EncoderModel(model.encoder, model.encoder_embed)
|
|
||||||
filename = "jit_script.pt"
|
|
||||||
|
|
||||||
logging.info("Using torch.jit.script")
|
|
||||||
model = torch.jit.script(model)
|
|
||||||
model.save(str(params.exp_dir / filename))
|
|
||||||
logging.info(f"Saved to {filename}")
|
|
||||||
else:
|
|
||||||
logging.info("Not using torchscript. Export model.state_dict()")
|
|
||||||
# Save it using a format so that it can be loaded
|
|
||||||
# by :func:`load_checkpoint`
|
|
||||||
filename = params.exp_dir / "pretrained.pt"
|
|
||||||
torch.save({"model": model.state_dict()}, str(filename))
|
|
||||||
logging.info(f"Saved to {filename}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
|
||||||
|
|
||||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/export.py
Symbolic link
1
egs/mls_english/ASR/zipformer/export.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/export.py
|
@ -1,193 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#
|
|
||||||
# Copyright 2021-2022 Xiaomi Corporation (Author: Yifan Yang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
"""
|
|
||||||
Usage:
|
|
||||||
(1) use the checkpoint exp_dir/epoch-xxx.pt
|
|
||||||
./zipformer/generate_averaged_model.py \
|
|
||||||
--epoch 28 \
|
|
||||||
--avg 15 \
|
|
||||||
--exp-dir ./zipformer/exp
|
|
||||||
|
|
||||||
It will generate a file `epoch-28-avg-15.pt` in the given `exp_dir`.
|
|
||||||
You can later load it by `torch.load("epoch-28-avg-15.pt")`.
|
|
||||||
|
|
||||||
(2) use the checkpoint exp_dir/checkpoint-iter.pt
|
|
||||||
./zipformer/generate_averaged_model.py \
|
|
||||||
--iter 22000 \
|
|
||||||
--avg 5 \
|
|
||||||
--exp-dir ./zipformer/exp
|
|
||||||
|
|
||||||
It will generate a file `iter-22000-avg-5.pt` in the given `exp_dir`.
|
|
||||||
You can later load it by `torch.load("iter-22000-avg-5.pt")`.
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import torch
|
|
||||||
from train import add_model_arguments, get_model, get_params
|
|
||||||
|
|
||||||
from icefall.checkpoint import average_checkpoints_with_averaged_model, find_checkpoints
|
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--epoch",
|
|
||||||
type=int,
|
|
||||||
default=30,
|
|
||||||
help="""It specifies the checkpoint to use for decoding.
|
|
||||||
Note: Epoch counts from 1.
|
|
||||||
You can specify --avg to use more checkpoints for model averaging.""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--iter",
|
|
||||||
type=int,
|
|
||||||
default=0,
|
|
||||||
help="""If positive, --epoch is ignored and it
|
|
||||||
will use the checkpoint exp_dir/checkpoint-iter.pt.
|
|
||||||
You can specify --avg to use more checkpoints for model averaging.
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--avg",
|
|
||||||
type=int,
|
|
||||||
default=9,
|
|
||||||
help="Number of checkpoints to average. Automatically select "
|
|
||||||
"consecutive checkpoints before the checkpoint specified by "
|
|
||||||
"'--epoch' and '--iter'",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--exp-dir",
|
|
||||||
type=str,
|
|
||||||
default="zipformer/exp",
|
|
||||||
help="The experiment dir",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--tokens",
|
|
||||||
type=str,
|
|
||||||
default="data/lang_bpe_500/tokens.txt",
|
|
||||||
help="Path to the tokens.txt",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--context-size",
|
|
||||||
type=int,
|
|
||||||
default=2,
|
|
||||||
help="The context size in the decoder. 1 means bigram; 2 means tri-gram",
|
|
||||||
)
|
|
||||||
|
|
||||||
add_model_arguments(parser)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
|
||||||
def main():
|
|
||||||
parser = get_parser()
|
|
||||||
args = parser.parse_args()
|
|
||||||
args.exp_dir = Path(args.exp_dir)
|
|
||||||
|
|
||||||
params = get_params()
|
|
||||||
params.update(vars(args))
|
|
||||||
|
|
||||||
if params.iter > 0:
|
|
||||||
params.suffix = f"iter-{params.iter}-avg-{params.avg}"
|
|
||||||
else:
|
|
||||||
params.suffix = f"epoch-{params.epoch}-avg-{params.avg}"
|
|
||||||
|
|
||||||
print("Script started")
|
|
||||||
|
|
||||||
device = torch.device("cpu")
|
|
||||||
print(f"Device: {device}")
|
|
||||||
|
|
||||||
symbol_table = k2.SymbolTable.from_file(params.tokens)
|
|
||||||
params.blank_id = symbol_table["<blk>"]
|
|
||||||
params.unk_id = symbol_table["<unk>"]
|
|
||||||
params.vocab_size = len(symbol_table)
|
|
||||||
|
|
||||||
print("About to create model")
|
|
||||||
model = get_model(params)
|
|
||||||
|
|
||||||
if params.iter > 0:
|
|
||||||
filenames = find_checkpoints(params.exp_dir, iteration=-params.iter)[
|
|
||||||
: params.avg + 1
|
|
||||||
]
|
|
||||||
if len(filenames) == 0:
|
|
||||||
raise ValueError(
|
|
||||||
f"No checkpoints found for --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
elif len(filenames) < params.avg + 1:
|
|
||||||
raise ValueError(
|
|
||||||
f"Not enough checkpoints ({len(filenames)}) found for"
|
|
||||||
f" --iter {params.iter}, --avg {params.avg}"
|
|
||||||
)
|
|
||||||
filename_start = filenames[-1]
|
|
||||||
filename_end = filenames[0]
|
|
||||||
print(
|
|
||||||
"Calculating the averaged model over iteration checkpoints"
|
|
||||||
f" from {filename_start} (excluded) to {filename_end}"
|
|
||||||
)
|
|
||||||
model.to(device)
|
|
||||||
model.load_state_dict(
|
|
||||||
average_checkpoints_with_averaged_model(
|
|
||||||
filename_start=filename_start,
|
|
||||||
filename_end=filename_end,
|
|
||||||
device=device,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
filename = params.exp_dir / f"iter-{params.iter}-avg-{params.avg}.pt"
|
|
||||||
torch.save({"model": model.state_dict()}, filename)
|
|
||||||
else:
|
|
||||||
assert params.avg > 0, params.avg
|
|
||||||
start = params.epoch - params.avg
|
|
||||||
assert start >= 1, start
|
|
||||||
filename_start = f"{params.exp_dir}/epoch-{start}.pt"
|
|
||||||
filename_end = f"{params.exp_dir}/epoch-{params.epoch}.pt"
|
|
||||||
print(
|
|
||||||
f"Calculating the averaged model over epoch range from "
|
|
||||||
f"{start} (excluded) to {params.epoch}"
|
|
||||||
)
|
|
||||||
model.to(device)
|
|
||||||
model.load_state_dict(
|
|
||||||
average_checkpoints_with_averaged_model(
|
|
||||||
filename_start=filename_start,
|
|
||||||
filename_end=filename_end,
|
|
||||||
device=device,
|
|
||||||
)
|
|
||||||
)
|
|
||||||
filename = params.exp_dir / f"epoch-{params.epoch}-avg-{params.avg}.pt"
|
|
||||||
torch.save({"model": model.state_dict()}, filename)
|
|
||||||
|
|
||||||
num_param = sum([p.numel() for p in model.parameters()])
|
|
||||||
print(f"Number of model parameters: {num_param}")
|
|
||||||
|
|
||||||
print("Done!")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/generate_averaged_model.py
Symbolic link
1
egs/mls_english/ASR/zipformer/generate_averaged_model.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/generate_averaged_model.py
|
@ -1,67 +0,0 @@
|
|||||||
# Copyright 2021 Xiaomi Corp. (authors: Fangjun Kuang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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 torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from scaling import ScaledLinear
|
|
||||||
|
|
||||||
|
|
||||||
class Joiner(nn.Module):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
encoder_dim: int,
|
|
||||||
decoder_dim: int,
|
|
||||||
joiner_dim: int,
|
|
||||||
vocab_size: int,
|
|
||||||
):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.encoder_proj = ScaledLinear(encoder_dim, joiner_dim, initial_scale=0.25)
|
|
||||||
self.decoder_proj = ScaledLinear(decoder_dim, joiner_dim, initial_scale=0.25)
|
|
||||||
self.output_linear = nn.Linear(joiner_dim, vocab_size)
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
decoder_out: torch.Tensor,
|
|
||||||
project_input: bool = True,
|
|
||||||
) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
encoder_out:
|
|
||||||
Output from the encoder. Its shape is (N, T, s_range, C).
|
|
||||||
decoder_out:
|
|
||||||
Output from the decoder. Its shape is (N, T, s_range, C).
|
|
||||||
project_input:
|
|
||||||
If true, apply input projections encoder_proj and decoder_proj.
|
|
||||||
If this is false, it is the user's responsibility to do this
|
|
||||||
manually.
|
|
||||||
Returns:
|
|
||||||
Return a tensor of shape (N, T, s_range, C).
|
|
||||||
"""
|
|
||||||
assert encoder_out.ndim == decoder_out.ndim, (
|
|
||||||
encoder_out.shape,
|
|
||||||
decoder_out.shape,
|
|
||||||
)
|
|
||||||
|
|
||||||
if project_input:
|
|
||||||
logit = self.encoder_proj(encoder_out) + self.decoder_proj(decoder_out)
|
|
||||||
else:
|
|
||||||
logit = encoder_out + decoder_out
|
|
||||||
|
|
||||||
logit = self.output_linear(torch.tanh(logit))
|
|
||||||
|
|
||||||
return logit
|
|
1
egs/mls_english/ASR/zipformer/joiner.py
Symbolic link
1
egs/mls_english/ASR/zipformer/joiner.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/joiner.py
|
@ -1,481 +0,0 @@
|
|||||||
# Copyright 2021-2023 Xiaomi Corp. (authors: Fangjun Kuang,
|
|
||||||
# Wei Kang,
|
|
||||||
# Zengwei Yao)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
from typing import Optional, Tuple
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from encoder_interface import EncoderInterface
|
|
||||||
from lhotse.dataset import SpecAugment
|
|
||||||
from scaling import ScaledLinear
|
|
||||||
|
|
||||||
from icefall.utils import add_sos, make_pad_mask, time_warp
|
|
||||||
|
|
||||||
|
|
||||||
class AsrModel(nn.Module):
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
encoder_embed: nn.Module,
|
|
||||||
encoder: EncoderInterface,
|
|
||||||
decoder: Optional[nn.Module] = None,
|
|
||||||
joiner: Optional[nn.Module] = None,
|
|
||||||
attention_decoder: Optional[nn.Module] = None,
|
|
||||||
encoder_dim: int = 384,
|
|
||||||
decoder_dim: int = 512,
|
|
||||||
vocab_size: int = 500,
|
|
||||||
use_transducer: bool = True,
|
|
||||||
use_ctc: bool = False,
|
|
||||||
use_attention_decoder: bool = False,
|
|
||||||
):
|
|
||||||
"""A joint CTC & Transducer ASR model.
|
|
||||||
|
|
||||||
- Connectionist temporal classification: labelling unsegmented sequence data with recurrent neural networks (http://imagine.enpc.fr/~obozinsg/teaching/mva_gm/papers/ctc.pdf)
|
|
||||||
- Sequence Transduction with Recurrent Neural Networks (https://arxiv.org/pdf/1211.3711.pdf)
|
|
||||||
- Pruned RNN-T for fast, memory-efficient ASR training (https://arxiv.org/pdf/2206.13236.pdf)
|
|
||||||
|
|
||||||
Args:
|
|
||||||
encoder_embed:
|
|
||||||
It is a Convolutional 2D subsampling module. It converts
|
|
||||||
an input of shape (N, T, idim) to an output of of shape
|
|
||||||
(N, T', odim), where T' = (T-3)//2-2 = (T-7)//2.
|
|
||||||
encoder:
|
|
||||||
It is the transcription network in the paper. Its accepts
|
|
||||||
two inputs: `x` of (N, T, encoder_dim) and `x_lens` of shape (N,).
|
|
||||||
It returns two tensors: `logits` of shape (N, T, encoder_dim) and
|
|
||||||
`logit_lens` of shape (N,).
|
|
||||||
decoder:
|
|
||||||
It is the prediction network in the paper. Its input shape
|
|
||||||
is (N, U) and its output shape is (N, U, decoder_dim).
|
|
||||||
It should contain one attribute: `blank_id`.
|
|
||||||
It is used when use_transducer is True.
|
|
||||||
joiner:
|
|
||||||
It has two inputs with shapes: (N, T, encoder_dim) and (N, U, decoder_dim).
|
|
||||||
Its output shape is (N, T, U, vocab_size). Note that its output contains
|
|
||||||
unnormalized probs, i.e., not processed by log-softmax.
|
|
||||||
It is used when use_transducer is True.
|
|
||||||
use_transducer:
|
|
||||||
Whether use transducer head. Default: True.
|
|
||||||
use_ctc:
|
|
||||||
Whether use CTC head. Default: False.
|
|
||||||
use_attention_decoder:
|
|
||||||
Whether use attention-decoder head. Default: False.
|
|
||||||
"""
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
assert (
|
|
||||||
use_transducer or use_ctc
|
|
||||||
), f"At least one of them should be True, but got use_transducer={use_transducer}, use_ctc={use_ctc}"
|
|
||||||
|
|
||||||
assert isinstance(encoder, EncoderInterface), type(encoder)
|
|
||||||
|
|
||||||
self.encoder_embed = encoder_embed
|
|
||||||
self.encoder = encoder
|
|
||||||
|
|
||||||
self.use_transducer = use_transducer
|
|
||||||
if use_transducer:
|
|
||||||
# Modules for Transducer head
|
|
||||||
assert decoder is not None
|
|
||||||
assert hasattr(decoder, "blank_id")
|
|
||||||
assert joiner is not None
|
|
||||||
|
|
||||||
self.decoder = decoder
|
|
||||||
self.joiner = joiner
|
|
||||||
|
|
||||||
self.simple_am_proj = ScaledLinear(
|
|
||||||
encoder_dim, vocab_size, initial_scale=0.25
|
|
||||||
)
|
|
||||||
self.simple_lm_proj = ScaledLinear(
|
|
||||||
decoder_dim, vocab_size, initial_scale=0.25
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
assert decoder is None
|
|
||||||
assert joiner is None
|
|
||||||
|
|
||||||
self.use_ctc = use_ctc
|
|
||||||
if use_ctc:
|
|
||||||
# Modules for CTC head
|
|
||||||
self.ctc_output = nn.Sequential(
|
|
||||||
nn.Dropout(p=0.1),
|
|
||||||
nn.Linear(encoder_dim, vocab_size),
|
|
||||||
nn.LogSoftmax(dim=-1),
|
|
||||||
)
|
|
||||||
|
|
||||||
self.use_attention_decoder = use_attention_decoder
|
|
||||||
if use_attention_decoder:
|
|
||||||
self.attention_decoder = attention_decoder
|
|
||||||
else:
|
|
||||||
assert attention_decoder is None
|
|
||||||
|
|
||||||
def forward_encoder(
|
|
||||||
self, x: torch.Tensor, x_lens: torch.Tensor
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""Compute encoder outputs.
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
A 3-D tensor of shape (N, T, C).
|
|
||||||
x_lens:
|
|
||||||
A 1-D tensor of shape (N,). It contains the number of frames in `x`
|
|
||||||
before padding.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
encoder_out:
|
|
||||||
Encoder output, of shape (N, T, C).
|
|
||||||
encoder_out_lens:
|
|
||||||
Encoder output lengths, of shape (N,).
|
|
||||||
"""
|
|
||||||
# logging.info(f"Memory allocated at entry: {torch.cuda.memory_allocated() // 1000000}M")
|
|
||||||
x, x_lens = self.encoder_embed(x, x_lens)
|
|
||||||
# logging.info(f"Memory allocated after encoder_embed: {torch.cuda.memory_allocated() // 1000000}M")
|
|
||||||
|
|
||||||
src_key_padding_mask = make_pad_mask(x_lens)
|
|
||||||
x = x.permute(1, 0, 2) # (N, T, C) -> (T, N, C)
|
|
||||||
|
|
||||||
encoder_out, encoder_out_lens = self.encoder(x, x_lens, src_key_padding_mask)
|
|
||||||
|
|
||||||
encoder_out = encoder_out.permute(1, 0, 2) # (T, N, C) ->(N, T, C)
|
|
||||||
assert torch.all(encoder_out_lens > 0), (x_lens, encoder_out_lens)
|
|
||||||
|
|
||||||
return encoder_out, encoder_out_lens
|
|
||||||
|
|
||||||
def forward_ctc(
|
|
||||||
self,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
encoder_out_lens: torch.Tensor,
|
|
||||||
targets: torch.Tensor,
|
|
||||||
target_lengths: torch.Tensor,
|
|
||||||
) -> torch.Tensor:
|
|
||||||
"""Compute CTC loss.
|
|
||||||
Args:
|
|
||||||
encoder_out:
|
|
||||||
Encoder output, of shape (N, T, C).
|
|
||||||
encoder_out_lens:
|
|
||||||
Encoder output lengths, of shape (N,).
|
|
||||||
targets:
|
|
||||||
Target Tensor of shape (sum(target_lengths)). The targets are assumed
|
|
||||||
to be un-padded and concatenated within 1 dimension.
|
|
||||||
"""
|
|
||||||
# Compute CTC log-prob
|
|
||||||
ctc_output = self.ctc_output(encoder_out) # (N, T, C)
|
|
||||||
|
|
||||||
ctc_loss = torch.nn.functional.ctc_loss(
|
|
||||||
log_probs=ctc_output.permute(1, 0, 2), # (T, N, C)
|
|
||||||
targets=targets.cpu(),
|
|
||||||
input_lengths=encoder_out_lens.cpu(),
|
|
||||||
target_lengths=target_lengths.cpu(),
|
|
||||||
reduction="sum",
|
|
||||||
)
|
|
||||||
return ctc_loss
|
|
||||||
|
|
||||||
def forward_cr_ctc(
|
|
||||||
self,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
encoder_out_lens: torch.Tensor,
|
|
||||||
targets: torch.Tensor,
|
|
||||||
target_lengths: torch.Tensor,
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""Compute CTC loss with consistency regularization loss.
|
|
||||||
Args:
|
|
||||||
encoder_out:
|
|
||||||
Encoder output, of shape (2 * N, T, C).
|
|
||||||
encoder_out_lens:
|
|
||||||
Encoder output lengths, of shape (2 * N,).
|
|
||||||
targets:
|
|
||||||
Target Tensor of shape (2 * sum(target_lengths)). The targets are assumed
|
|
||||||
to be un-padded and concatenated within 1 dimension.
|
|
||||||
"""
|
|
||||||
# Compute CTC loss
|
|
||||||
ctc_output = self.ctc_output(encoder_out) # (2 * N, T, C)
|
|
||||||
ctc_loss = torch.nn.functional.ctc_loss(
|
|
||||||
log_probs=ctc_output.permute(1, 0, 2), # (T, 2 * N, C)
|
|
||||||
targets=targets.cpu(),
|
|
||||||
input_lengths=encoder_out_lens.cpu(),
|
|
||||||
target_lengths=target_lengths.cpu(),
|
|
||||||
reduction="sum",
|
|
||||||
)
|
|
||||||
|
|
||||||
# Compute consistency regularization loss
|
|
||||||
exchanged_targets = ctc_output.detach().chunk(2, dim=0)
|
|
||||||
exchanged_targets = torch.cat(
|
|
||||||
[exchanged_targets[1], exchanged_targets[0]], dim=0
|
|
||||||
) # exchange: [x1, x2] -> [x2, x1]
|
|
||||||
cr_loss = nn.functional.kl_div(
|
|
||||||
input=ctc_output,
|
|
||||||
target=exchanged_targets,
|
|
||||||
reduction="none",
|
|
||||||
log_target=True,
|
|
||||||
) # (2 * N, T, C)
|
|
||||||
length_mask = make_pad_mask(encoder_out_lens).unsqueeze(-1)
|
|
||||||
cr_loss = cr_loss.masked_fill(length_mask, 0.0).sum()
|
|
||||||
|
|
||||||
return ctc_loss, cr_loss
|
|
||||||
|
|
||||||
def forward_transducer(
|
|
||||||
self,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
encoder_out_lens: torch.Tensor,
|
|
||||||
y: k2.RaggedTensor,
|
|
||||||
y_lens: torch.Tensor,
|
|
||||||
prune_range: int = 5,
|
|
||||||
am_scale: float = 0.0,
|
|
||||||
lm_scale: float = 0.0,
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""Compute Transducer loss.
|
|
||||||
Args:
|
|
||||||
encoder_out:
|
|
||||||
Encoder output, of shape (N, T, C).
|
|
||||||
encoder_out_lens:
|
|
||||||
Encoder output lengths, of shape (N,).
|
|
||||||
y:
|
|
||||||
A ragged tensor with 2 axes [utt][label]. It contains labels of each
|
|
||||||
utterance.
|
|
||||||
prune_range:
|
|
||||||
The prune range for rnnt loss, it means how many symbols(context)
|
|
||||||
we are considering for each frame to compute the loss.
|
|
||||||
am_scale:
|
|
||||||
The scale to smooth the loss with am (output of encoder network)
|
|
||||||
part
|
|
||||||
lm_scale:
|
|
||||||
The scale to smooth the loss with lm (output of predictor network)
|
|
||||||
part
|
|
||||||
"""
|
|
||||||
# Now for the decoder, i.e., the prediction network
|
|
||||||
blank_id = self.decoder.blank_id
|
|
||||||
sos_y = add_sos(y, sos_id=blank_id)
|
|
||||||
|
|
||||||
# sos_y_padded: [B, S + 1], start with SOS.
|
|
||||||
sos_y_padded = sos_y.pad(mode="constant", padding_value=blank_id)
|
|
||||||
|
|
||||||
# decoder_out: [B, S + 1, decoder_dim]
|
|
||||||
decoder_out = self.decoder(sos_y_padded)
|
|
||||||
|
|
||||||
# Note: y does not start with SOS
|
|
||||||
# y_padded : [B, S]
|
|
||||||
y_padded = y.pad(mode="constant", padding_value=0)
|
|
||||||
|
|
||||||
y_padded = y_padded.to(torch.int64)
|
|
||||||
boundary = torch.zeros(
|
|
||||||
(encoder_out.size(0), 4),
|
|
||||||
dtype=torch.int64,
|
|
||||||
device=encoder_out.device,
|
|
||||||
)
|
|
||||||
boundary[:, 2] = y_lens
|
|
||||||
boundary[:, 3] = encoder_out_lens
|
|
||||||
|
|
||||||
lm = self.simple_lm_proj(decoder_out)
|
|
||||||
am = self.simple_am_proj(encoder_out)
|
|
||||||
|
|
||||||
# if self.training and random.random() < 0.25:
|
|
||||||
# lm = penalize_abs_values_gt(lm, 100.0, 1.0e-04)
|
|
||||||
# if self.training and random.random() < 0.25:
|
|
||||||
# am = penalize_abs_values_gt(am, 30.0, 1.0e-04)
|
|
||||||
|
|
||||||
with torch.cuda.amp.autocast(enabled=False):
|
|
||||||
simple_loss, (px_grad, py_grad) = k2.rnnt_loss_smoothed(
|
|
||||||
lm=lm.float(),
|
|
||||||
am=am.float(),
|
|
||||||
symbols=y_padded,
|
|
||||||
termination_symbol=blank_id,
|
|
||||||
lm_only_scale=lm_scale,
|
|
||||||
am_only_scale=am_scale,
|
|
||||||
boundary=boundary,
|
|
||||||
reduction="sum",
|
|
||||||
return_grad=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
# ranges : [B, T, prune_range]
|
|
||||||
ranges = k2.get_rnnt_prune_ranges(
|
|
||||||
px_grad=px_grad,
|
|
||||||
py_grad=py_grad,
|
|
||||||
boundary=boundary,
|
|
||||||
s_range=prune_range,
|
|
||||||
)
|
|
||||||
|
|
||||||
# am_pruned : [B, T, prune_range, encoder_dim]
|
|
||||||
# lm_pruned : [B, T, prune_range, decoder_dim]
|
|
||||||
am_pruned, lm_pruned = k2.do_rnnt_pruning(
|
|
||||||
am=self.joiner.encoder_proj(encoder_out),
|
|
||||||
lm=self.joiner.decoder_proj(decoder_out),
|
|
||||||
ranges=ranges,
|
|
||||||
)
|
|
||||||
|
|
||||||
# logits : [B, T, prune_range, vocab_size]
|
|
||||||
|
|
||||||
# project_input=False since we applied the decoder's input projections
|
|
||||||
# prior to do_rnnt_pruning (this is an optimization for speed).
|
|
||||||
logits = self.joiner(am_pruned, lm_pruned, project_input=False)
|
|
||||||
|
|
||||||
with torch.cuda.amp.autocast(enabled=False):
|
|
||||||
pruned_loss = k2.rnnt_loss_pruned(
|
|
||||||
logits=logits.float(),
|
|
||||||
symbols=y_padded,
|
|
||||||
ranges=ranges,
|
|
||||||
termination_symbol=blank_id,
|
|
||||||
boundary=boundary,
|
|
||||||
reduction="sum",
|
|
||||||
)
|
|
||||||
|
|
||||||
return simple_loss, pruned_loss
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self,
|
|
||||||
x: torch.Tensor,
|
|
||||||
x_lens: torch.Tensor,
|
|
||||||
y: k2.RaggedTensor,
|
|
||||||
prune_range: int = 5,
|
|
||||||
am_scale: float = 0.0,
|
|
||||||
lm_scale: float = 0.0,
|
|
||||||
use_cr_ctc: bool = False,
|
|
||||||
use_spec_aug: bool = False,
|
|
||||||
spec_augment: Optional[SpecAugment] = None,
|
|
||||||
supervision_segments: Optional[torch.Tensor] = None,
|
|
||||||
time_warp_factor: Optional[int] = 80,
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
A 3-D tensor of shape (N, T, C).
|
|
||||||
x_lens:
|
|
||||||
A 1-D tensor of shape (N,). It contains the number of frames in `x`
|
|
||||||
before padding.
|
|
||||||
y:
|
|
||||||
A ragged tensor with 2 axes [utt][label]. It contains labels of each
|
|
||||||
utterance.
|
|
||||||
prune_range:
|
|
||||||
The prune range for rnnt loss, it means how many symbols(context)
|
|
||||||
we are considering for each frame to compute the loss.
|
|
||||||
am_scale:
|
|
||||||
The scale to smooth the loss with am (output of encoder network)
|
|
||||||
part
|
|
||||||
lm_scale:
|
|
||||||
The scale to smooth the loss with lm (output of predictor network)
|
|
||||||
part
|
|
||||||
use_cr_ctc:
|
|
||||||
Whether use consistency-regularized CTC.
|
|
||||||
use_spec_aug:
|
|
||||||
Whether apply spec-augment manually, used only if use_cr_ctc is True.
|
|
||||||
spec_augment:
|
|
||||||
The SpecAugment instance that returns time masks,
|
|
||||||
used only if use_cr_ctc is True.
|
|
||||||
supervision_segments:
|
|
||||||
An int tensor of shape ``(S, 3)``. ``S`` is the number of
|
|
||||||
supervision segments that exist in ``features``.
|
|
||||||
Used only if use_cr_ctc is True.
|
|
||||||
time_warp_factor:
|
|
||||||
Parameter for the time warping; larger values mean more warping.
|
|
||||||
Set to ``None``, or less than ``1``, to disable.
|
|
||||||
Used only if use_cr_ctc is True.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Return the transducer losses, CTC loss, AED loss,
|
|
||||||
and consistency-regularization loss in form of
|
|
||||||
(simple_loss, pruned_loss, ctc_loss, attention_decoder_loss, cr_loss)
|
|
||||||
|
|
||||||
Note:
|
|
||||||
Regarding am_scale & lm_scale, it will make the loss-function one of
|
|
||||||
the form:
|
|
||||||
lm_scale * lm_probs + am_scale * am_probs +
|
|
||||||
(1-lm_scale-am_scale) * combined_probs
|
|
||||||
"""
|
|
||||||
assert x.ndim == 3, x.shape
|
|
||||||
assert x_lens.ndim == 1, x_lens.shape
|
|
||||||
assert y.num_axes == 2, y.num_axes
|
|
||||||
|
|
||||||
assert x.size(0) == x_lens.size(0) == y.dim0, (x.shape, x_lens.shape, y.dim0)
|
|
||||||
|
|
||||||
device = x.device
|
|
||||||
|
|
||||||
if use_cr_ctc:
|
|
||||||
assert self.use_ctc
|
|
||||||
if use_spec_aug:
|
|
||||||
assert spec_augment is not None and spec_augment.time_warp_factor < 1
|
|
||||||
# Apply time warping before input duplicating
|
|
||||||
assert supervision_segments is not None
|
|
||||||
x = time_warp(
|
|
||||||
x,
|
|
||||||
time_warp_factor=time_warp_factor,
|
|
||||||
supervision_segments=supervision_segments,
|
|
||||||
)
|
|
||||||
# Independently apply frequency masking and time masking to the two copies
|
|
||||||
x = spec_augment(x.repeat(2, 1, 1))
|
|
||||||
else:
|
|
||||||
x = x.repeat(2, 1, 1)
|
|
||||||
x_lens = x_lens.repeat(2)
|
|
||||||
y = k2.ragged.cat([y, y], axis=0)
|
|
||||||
|
|
||||||
# Compute encoder outputs
|
|
||||||
encoder_out, encoder_out_lens = self.forward_encoder(x, x_lens)
|
|
||||||
|
|
||||||
row_splits = y.shape.row_splits(1)
|
|
||||||
y_lens = row_splits[1:] - row_splits[:-1]
|
|
||||||
|
|
||||||
if self.use_transducer:
|
|
||||||
# Compute transducer loss
|
|
||||||
simple_loss, pruned_loss = self.forward_transducer(
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
y=y.to(device),
|
|
||||||
y_lens=y_lens,
|
|
||||||
prune_range=prune_range,
|
|
||||||
am_scale=am_scale,
|
|
||||||
lm_scale=lm_scale,
|
|
||||||
)
|
|
||||||
if use_cr_ctc:
|
|
||||||
simple_loss = simple_loss * 0.5
|
|
||||||
pruned_loss = pruned_loss * 0.5
|
|
||||||
else:
|
|
||||||
simple_loss = torch.empty(0)
|
|
||||||
pruned_loss = torch.empty(0)
|
|
||||||
|
|
||||||
if self.use_ctc:
|
|
||||||
# Compute CTC loss
|
|
||||||
targets = y.values
|
|
||||||
if not use_cr_ctc:
|
|
||||||
ctc_loss = self.forward_ctc(
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
targets=targets,
|
|
||||||
target_lengths=y_lens,
|
|
||||||
)
|
|
||||||
cr_loss = torch.empty(0)
|
|
||||||
else:
|
|
||||||
ctc_loss, cr_loss = self.forward_cr_ctc(
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
targets=targets,
|
|
||||||
target_lengths=y_lens,
|
|
||||||
)
|
|
||||||
ctc_loss = ctc_loss * 0.5
|
|
||||||
cr_loss = cr_loss * 0.5
|
|
||||||
else:
|
|
||||||
ctc_loss = torch.empty(0)
|
|
||||||
cr_loss = torch.empty(0)
|
|
||||||
|
|
||||||
if self.use_attention_decoder:
|
|
||||||
attention_decoder_loss = self.attention_decoder.calc_att_loss(
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
ys=y.to(device),
|
|
||||||
ys_lens=y_lens.to(device),
|
|
||||||
)
|
|
||||||
if use_cr_ctc:
|
|
||||||
attention_decoder_loss = attention_decoder_loss * 0.5
|
|
||||||
else:
|
|
||||||
attention_decoder_loss = torch.empty(0)
|
|
||||||
|
|
||||||
return simple_loss, pruned_loss, ctc_loss, attention_decoder_loss, cr_loss
|
|
1
egs/mls_english/ASR/zipformer/model.py
Symbolic link
1
egs/mls_english/ASR/zipformer/model.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/model.py
|
@ -1,170 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#
|
|
||||||
# Copyright 2023 Xiaomi Corporation (Author: Zengwei Yao)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: ./zipformer/my_profile.py
|
|
||||||
"""
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import logging
|
|
||||||
from typing import Tuple
|
|
||||||
|
|
||||||
import sentencepiece as spm
|
|
||||||
import torch
|
|
||||||
from scaling import BiasNorm
|
|
||||||
from torch import Tensor, nn
|
|
||||||
from train import (
|
|
||||||
add_model_arguments,
|
|
||||||
get_encoder_embed,
|
|
||||||
get_encoder_model,
|
|
||||||
get_joiner_model,
|
|
||||||
get_params,
|
|
||||||
)
|
|
||||||
from zipformer import BypassModule
|
|
||||||
|
|
||||||
from icefall.profiler import get_model_profile
|
|
||||||
from icefall.utils import make_pad_mask
|
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--bpe-model",
|
|
||||||
type=str,
|
|
||||||
default="data/lang_bpe_500/bpe.model",
|
|
||||||
help="Path to the BPE model",
|
|
||||||
)
|
|
||||||
|
|
||||||
add_model_arguments(parser)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
def _bias_norm_flops_compute(module, input, output):
|
|
||||||
assert len(input) == 1, len(input)
|
|
||||||
# estimate as layer_norm, see icefall/profiler.py
|
|
||||||
flops = input[0].numel() * 5
|
|
||||||
module.__flops__ += int(flops)
|
|
||||||
|
|
||||||
|
|
||||||
def _swoosh_module_flops_compute(module, input, output):
|
|
||||||
# For SwooshL and SwooshR modules
|
|
||||||
assert len(input) == 1, len(input)
|
|
||||||
# estimate as swish/silu, see icefall/profiler.py
|
|
||||||
flops = input[0].numel()
|
|
||||||
module.__flops__ += int(flops)
|
|
||||||
|
|
||||||
|
|
||||||
def _bypass_module_flops_compute(module, input, output):
|
|
||||||
# For Bypass module
|
|
||||||
assert len(input) == 2, len(input)
|
|
||||||
flops = input[0].numel() * 2
|
|
||||||
module.__flops__ += int(flops)
|
|
||||||
|
|
||||||
|
|
||||||
MODULE_HOOK_MAPPING = {
|
|
||||||
BiasNorm: _bias_norm_flops_compute,
|
|
||||||
BypassModule: _bypass_module_flops_compute,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class Model(nn.Module):
|
|
||||||
"""A Wrapper for encoder, encoder_embed, and encoder_proj"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
encoder: nn.Module,
|
|
||||||
encoder_embed: nn.Module,
|
|
||||||
encoder_proj: nn.Module,
|
|
||||||
) -> None:
|
|
||||||
super().__init__()
|
|
||||||
self.encoder = encoder
|
|
||||||
self.encoder_embed = encoder_embed
|
|
||||||
self.encoder_proj = encoder_proj
|
|
||||||
|
|
||||||
def forward(self, feature: Tensor, feature_lens: Tensor) -> Tuple[Tensor, Tensor]:
|
|
||||||
x, x_lens = self.encoder_embed(feature, feature_lens)
|
|
||||||
|
|
||||||
src_key_padding_mask = make_pad_mask(x_lens)
|
|
||||||
x = x.permute(1, 0, 2) # (N, T, C) -> (T, N, C)
|
|
||||||
|
|
||||||
encoder_out, encoder_out_lens = self.encoder(x, x_lens, src_key_padding_mask)
|
|
||||||
|
|
||||||
encoder_out = encoder_out.permute(1, 0, 2) # (N, T, C) -> (T, N, C)
|
|
||||||
logits = self.encoder_proj(encoder_out)
|
|
||||||
|
|
||||||
return logits, encoder_out_lens
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
|
||||||
def main():
|
|
||||||
parser = get_parser()
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
params = get_params()
|
|
||||||
params.update(vars(args))
|
|
||||||
|
|
||||||
device = torch.device("cpu")
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
device = torch.device("cuda", 0)
|
|
||||||
logging.info(f"Device: {device}")
|
|
||||||
|
|
||||||
sp = spm.SentencePieceProcessor()
|
|
||||||
sp.load(params.bpe_model)
|
|
||||||
|
|
||||||
# <blk> is defined in local/train_bpe_model.py
|
|
||||||
params.blank_id = sp.piece_to_id("<blk>")
|
|
||||||
params.vocab_size = sp.get_piece_size()
|
|
||||||
|
|
||||||
logging.info(params)
|
|
||||||
|
|
||||||
logging.info("About to create model")
|
|
||||||
|
|
||||||
# We only profile the encoder part
|
|
||||||
model = Model(
|
|
||||||
encoder=get_encoder_model(params),
|
|
||||||
encoder_embed=get_encoder_embed(params),
|
|
||||||
encoder_proj=get_joiner_model(params).encoder_proj,
|
|
||||||
)
|
|
||||||
model.eval()
|
|
||||||
model.to(device)
|
|
||||||
|
|
||||||
num_param = sum([p.numel() for p in model.parameters()])
|
|
||||||
logging.info(f"Number of model parameters: {num_param}")
|
|
||||||
|
|
||||||
# for 30-second input
|
|
||||||
B, T, D = 1, 3000, 80
|
|
||||||
feature = torch.ones(B, T, D, dtype=torch.float32).to(device)
|
|
||||||
feature_lens = torch.full((B,), T, dtype=torch.int64).to(device)
|
|
||||||
|
|
||||||
flops, params = get_model_profile(
|
|
||||||
model=model,
|
|
||||||
args=(feature, feature_lens),
|
|
||||||
module_hoop_mapping=MODULE_HOOK_MAPPING,
|
|
||||||
)
|
|
||||||
logging.info(f"For the encoder part, params: {params}, flops: {flops}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
|
||||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
|
||||||
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/my_profile.py
Symbolic link
1
egs/mls_english/ASR/zipformer/my_profile.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/my_profile.py
|
@ -1,422 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# Copyright 2022 Xiaomi Corp. (authors: Fangjun Kuang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
"""
|
|
||||||
This script loads ONNX models and uses them to decode waves.
|
|
||||||
You can use the following command to get the exported models:
|
|
||||||
|
|
||||||
We use the pre-trained model from
|
|
||||||
https://huggingface.co/Zengwei/icefall-asr-librispeech-zipformer-2023-05-15
|
|
||||||
as an example to show how to use this file.
|
|
||||||
|
|
||||||
1. Download the pre-trained model
|
|
||||||
|
|
||||||
cd egs/librispeech/ASR
|
|
||||||
|
|
||||||
repo_url=https://huggingface.co/Zengwei/icefall-asr-librispeech-zipformer-2023-05-15
|
|
||||||
GIT_LFS_SKIP_SMUDGE=1 git clone $repo_url
|
|
||||||
repo=$(basename $repo_url)
|
|
||||||
|
|
||||||
pushd $repo
|
|
||||||
git lfs pull --include "exp/pretrained.pt"
|
|
||||||
|
|
||||||
cd exp
|
|
||||||
ln -s pretrained.pt epoch-99.pt
|
|
||||||
popd
|
|
||||||
|
|
||||||
2. Export the model to ONNX
|
|
||||||
|
|
||||||
./zipformer/export-onnx.py \
|
|
||||||
--tokens $repo/data/lang_bpe_500/tokens.txt \
|
|
||||||
--use-averaged-model 0 \
|
|
||||||
--epoch 99 \
|
|
||||||
--avg 1 \
|
|
||||||
--exp-dir $repo/exp \
|
|
||||||
--causal False
|
|
||||||
|
|
||||||
It will generate the following 3 files inside $repo/exp:
|
|
||||||
|
|
||||||
- encoder-epoch-99-avg-1.onnx
|
|
||||||
- decoder-epoch-99-avg-1.onnx
|
|
||||||
- joiner-epoch-99-avg-1.onnx
|
|
||||||
|
|
||||||
3. Run this file
|
|
||||||
|
|
||||||
./zipformer/onnx_pretrained.py \
|
|
||||||
--encoder-model-filename $repo/exp/encoder-epoch-99-avg-1.onnx \
|
|
||||||
--decoder-model-filename $repo/exp/decoder-epoch-99-avg-1.onnx \
|
|
||||||
--joiner-model-filename $repo/exp/joiner-epoch-99-avg-1.onnx \
|
|
||||||
--tokens $repo/data/lang_bpe_500/tokens.txt \
|
|
||||||
$repo/test_wavs/1089-134686-0001.wav \
|
|
||||||
$repo/test_wavs/1221-135766-0001.wav \
|
|
||||||
$repo/test_wavs/1221-135766-0002.wav
|
|
||||||
"""
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import logging
|
|
||||||
import math
|
|
||||||
from typing import List, Tuple
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import kaldifeat
|
|
||||||
import onnxruntime as ort
|
|
||||||
import torch
|
|
||||||
import torchaudio
|
|
||||||
from torch.nn.utils.rnn import pad_sequence
|
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--encoder-model-filename",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
help="Path to the encoder onnx model. ",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--decoder-model-filename",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
help="Path to the decoder onnx model. ",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--joiner-model-filename",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
help="Path to the joiner onnx model. ",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--tokens",
|
|
||||||
type=str,
|
|
||||||
help="""Path to tokens.txt.""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"sound_files",
|
|
||||||
type=str,
|
|
||||||
nargs="+",
|
|
||||||
help="The input sound file(s) to transcribe. "
|
|
||||||
"Supported formats are those supported by torchaudio.load(). "
|
|
||||||
"For example, wav and flac are supported. "
|
|
||||||
"The sample rate has to be 16kHz.",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--sample-rate",
|
|
||||||
type=int,
|
|
||||||
default=16000,
|
|
||||||
help="The sample rate of the input sound file",
|
|
||||||
)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
class OnnxModel:
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
encoder_model_filename: str,
|
|
||||||
decoder_model_filename: str,
|
|
||||||
joiner_model_filename: str,
|
|
||||||
):
|
|
||||||
session_opts = ort.SessionOptions()
|
|
||||||
session_opts.inter_op_num_threads = 1
|
|
||||||
session_opts.intra_op_num_threads = 4
|
|
||||||
|
|
||||||
self.session_opts = session_opts
|
|
||||||
|
|
||||||
self.init_encoder(encoder_model_filename)
|
|
||||||
self.init_decoder(decoder_model_filename)
|
|
||||||
self.init_joiner(joiner_model_filename)
|
|
||||||
|
|
||||||
def init_encoder(self, encoder_model_filename: str):
|
|
||||||
self.encoder = ort.InferenceSession(
|
|
||||||
encoder_model_filename,
|
|
||||||
sess_options=self.session_opts,
|
|
||||||
providers=["CPUExecutionProvider"],
|
|
||||||
)
|
|
||||||
|
|
||||||
def init_decoder(self, decoder_model_filename: str):
|
|
||||||
self.decoder = ort.InferenceSession(
|
|
||||||
decoder_model_filename,
|
|
||||||
sess_options=self.session_opts,
|
|
||||||
providers=["CPUExecutionProvider"],
|
|
||||||
)
|
|
||||||
|
|
||||||
decoder_meta = self.decoder.get_modelmeta().custom_metadata_map
|
|
||||||
self.context_size = int(decoder_meta["context_size"])
|
|
||||||
self.vocab_size = int(decoder_meta["vocab_size"])
|
|
||||||
|
|
||||||
logging.info(f"context_size: {self.context_size}")
|
|
||||||
logging.info(f"vocab_size: {self.vocab_size}")
|
|
||||||
|
|
||||||
def init_joiner(self, joiner_model_filename: str):
|
|
||||||
self.joiner = ort.InferenceSession(
|
|
||||||
joiner_model_filename,
|
|
||||||
sess_options=self.session_opts,
|
|
||||||
providers=["CPUExecutionProvider"],
|
|
||||||
)
|
|
||||||
|
|
||||||
joiner_meta = self.joiner.get_modelmeta().custom_metadata_map
|
|
||||||
self.joiner_dim = int(joiner_meta["joiner_dim"])
|
|
||||||
|
|
||||||
logging.info(f"joiner_dim: {self.joiner_dim}")
|
|
||||||
|
|
||||||
def run_encoder(
|
|
||||||
self,
|
|
||||||
x: torch.Tensor,
|
|
||||||
x_lens: torch.Tensor,
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
A 3-D tensor of shape (N, T, C)
|
|
||||||
x_lens:
|
|
||||||
A 2-D tensor of shape (N,). Its dtype is torch.int64
|
|
||||||
Returns:
|
|
||||||
Return a tuple containing:
|
|
||||||
- encoder_out, its shape is (N, T', joiner_dim)
|
|
||||||
- encoder_out_lens, its shape is (N,)
|
|
||||||
"""
|
|
||||||
out = self.encoder.run(
|
|
||||||
[
|
|
||||||
self.encoder.get_outputs()[0].name,
|
|
||||||
self.encoder.get_outputs()[1].name,
|
|
||||||
],
|
|
||||||
{
|
|
||||||
self.encoder.get_inputs()[0].name: x.numpy(),
|
|
||||||
self.encoder.get_inputs()[1].name: x_lens.numpy(),
|
|
||||||
},
|
|
||||||
)
|
|
||||||
return torch.from_numpy(out[0]), torch.from_numpy(out[1])
|
|
||||||
|
|
||||||
def run_decoder(self, decoder_input: torch.Tensor) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
decoder_input:
|
|
||||||
A 2-D tensor of shape (N, context_size)
|
|
||||||
Returns:
|
|
||||||
Return a 2-D tensor of shape (N, joiner_dim)
|
|
||||||
"""
|
|
||||||
out = self.decoder.run(
|
|
||||||
[self.decoder.get_outputs()[0].name],
|
|
||||||
{self.decoder.get_inputs()[0].name: decoder_input.numpy()},
|
|
||||||
)[0]
|
|
||||||
|
|
||||||
return torch.from_numpy(out)
|
|
||||||
|
|
||||||
def run_joiner(
|
|
||||||
self, encoder_out: torch.Tensor, decoder_out: torch.Tensor
|
|
||||||
) -> torch.Tensor:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
encoder_out:
|
|
||||||
A 2-D tensor of shape (N, joiner_dim)
|
|
||||||
decoder_out:
|
|
||||||
A 2-D tensor of shape (N, joiner_dim)
|
|
||||||
Returns:
|
|
||||||
Return a 2-D tensor of shape (N, vocab_size)
|
|
||||||
"""
|
|
||||||
out = self.joiner.run(
|
|
||||||
[self.joiner.get_outputs()[0].name],
|
|
||||||
{
|
|
||||||
self.joiner.get_inputs()[0].name: encoder_out.numpy(),
|
|
||||||
self.joiner.get_inputs()[1].name: decoder_out.numpy(),
|
|
||||||
},
|
|
||||||
)[0]
|
|
||||||
|
|
||||||
return torch.from_numpy(out)
|
|
||||||
|
|
||||||
|
|
||||||
def read_sound_files(
|
|
||||||
filenames: List[str], expected_sample_rate: float
|
|
||||||
) -> List[torch.Tensor]:
|
|
||||||
"""Read a list of sound files into a list 1-D float32 torch tensors.
|
|
||||||
Args:
|
|
||||||
filenames:
|
|
||||||
A list of sound filenames.
|
|
||||||
expected_sample_rate:
|
|
||||||
The expected sample rate of the sound files.
|
|
||||||
Returns:
|
|
||||||
Return a list of 1-D float32 torch tensors.
|
|
||||||
"""
|
|
||||||
ans = []
|
|
||||||
for f in filenames:
|
|
||||||
wave, sample_rate = torchaudio.load(f)
|
|
||||||
assert (
|
|
||||||
sample_rate == expected_sample_rate
|
|
||||||
), f"expected sample rate: {expected_sample_rate}. Given: {sample_rate}"
|
|
||||||
# We use only the first channel
|
|
||||||
ans.append(wave[0])
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
def greedy_search(
|
|
||||||
model: OnnxModel,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
encoder_out_lens: torch.Tensor,
|
|
||||||
) -> List[List[int]]:
|
|
||||||
"""Greedy search in batch mode. It hardcodes --max-sym-per-frame=1.
|
|
||||||
Args:
|
|
||||||
model:
|
|
||||||
The transducer model.
|
|
||||||
encoder_out:
|
|
||||||
A 3-D tensor of shape (N, T, joiner_dim)
|
|
||||||
encoder_out_lens:
|
|
||||||
A 1-D tensor of shape (N,).
|
|
||||||
Returns:
|
|
||||||
Return the decoded results for each utterance.
|
|
||||||
"""
|
|
||||||
assert encoder_out.ndim == 3, encoder_out.shape
|
|
||||||
assert encoder_out.size(0) >= 1, encoder_out.size(0)
|
|
||||||
|
|
||||||
packed_encoder_out = torch.nn.utils.rnn.pack_padded_sequence(
|
|
||||||
input=encoder_out,
|
|
||||||
lengths=encoder_out_lens.cpu(),
|
|
||||||
batch_first=True,
|
|
||||||
enforce_sorted=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
blank_id = 0 # hard-code to 0
|
|
||||||
|
|
||||||
batch_size_list = packed_encoder_out.batch_sizes.tolist()
|
|
||||||
N = encoder_out.size(0)
|
|
||||||
|
|
||||||
assert torch.all(encoder_out_lens > 0), encoder_out_lens
|
|
||||||
assert N == batch_size_list[0], (N, batch_size_list)
|
|
||||||
|
|
||||||
context_size = model.context_size
|
|
||||||
hyps = [[blank_id] * context_size for _ in range(N)]
|
|
||||||
|
|
||||||
decoder_input = torch.tensor(
|
|
||||||
hyps,
|
|
||||||
dtype=torch.int64,
|
|
||||||
) # (N, context_size)
|
|
||||||
|
|
||||||
decoder_out = model.run_decoder(decoder_input)
|
|
||||||
|
|
||||||
offset = 0
|
|
||||||
for batch_size in batch_size_list:
|
|
||||||
start = offset
|
|
||||||
end = offset + batch_size
|
|
||||||
current_encoder_out = packed_encoder_out.data[start:end]
|
|
||||||
# current_encoder_out's shape: (batch_size, joiner_dim)
|
|
||||||
offset = end
|
|
||||||
|
|
||||||
decoder_out = decoder_out[:batch_size]
|
|
||||||
logits = model.run_joiner(current_encoder_out, decoder_out)
|
|
||||||
|
|
||||||
# logits'shape (batch_size, vocab_size)
|
|
||||||
|
|
||||||
assert logits.ndim == 2, logits.shape
|
|
||||||
y = logits.argmax(dim=1).tolist()
|
|
||||||
emitted = False
|
|
||||||
for i, v in enumerate(y):
|
|
||||||
if v != blank_id:
|
|
||||||
hyps[i].append(v)
|
|
||||||
emitted = True
|
|
||||||
if emitted:
|
|
||||||
# update decoder output
|
|
||||||
decoder_input = [h[-context_size:] for h in hyps[:batch_size]]
|
|
||||||
decoder_input = torch.tensor(
|
|
||||||
decoder_input,
|
|
||||||
dtype=torch.int64,
|
|
||||||
)
|
|
||||||
decoder_out = model.run_decoder(decoder_input)
|
|
||||||
|
|
||||||
sorted_ans = [h[context_size:] for h in hyps]
|
|
||||||
ans = []
|
|
||||||
unsorted_indices = packed_encoder_out.unsorted_indices.tolist()
|
|
||||||
for i in range(N):
|
|
||||||
ans.append(sorted_ans[unsorted_indices[i]])
|
|
||||||
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
|
||||||
def main():
|
|
||||||
parser = get_parser()
|
|
||||||
args = parser.parse_args()
|
|
||||||
logging.info(vars(args))
|
|
||||||
model = OnnxModel(
|
|
||||||
encoder_model_filename=args.encoder_model_filename,
|
|
||||||
decoder_model_filename=args.decoder_model_filename,
|
|
||||||
joiner_model_filename=args.joiner_model_filename,
|
|
||||||
)
|
|
||||||
|
|
||||||
logging.info("Constructing Fbank computer")
|
|
||||||
opts = kaldifeat.FbankOptions()
|
|
||||||
opts.device = "cpu"
|
|
||||||
opts.frame_opts.dither = 0
|
|
||||||
opts.frame_opts.snip_edges = False
|
|
||||||
opts.frame_opts.samp_freq = args.sample_rate
|
|
||||||
opts.mel_opts.num_bins = 80
|
|
||||||
opts.mel_opts.high_freq = -400
|
|
||||||
|
|
||||||
fbank = kaldifeat.Fbank(opts)
|
|
||||||
|
|
||||||
logging.info(f"Reading sound files: {args.sound_files}")
|
|
||||||
waves = read_sound_files(
|
|
||||||
filenames=args.sound_files,
|
|
||||||
expected_sample_rate=args.sample_rate,
|
|
||||||
)
|
|
||||||
|
|
||||||
logging.info("Decoding started")
|
|
||||||
features = fbank(waves)
|
|
||||||
feature_lengths = [f.size(0) for f in features]
|
|
||||||
|
|
||||||
features = pad_sequence(
|
|
||||||
features,
|
|
||||||
batch_first=True,
|
|
||||||
padding_value=math.log(1e-10),
|
|
||||||
)
|
|
||||||
|
|
||||||
feature_lengths = torch.tensor(feature_lengths, dtype=torch.int64)
|
|
||||||
encoder_out, encoder_out_lens = model.run_encoder(features, feature_lengths)
|
|
||||||
|
|
||||||
hyps = greedy_search(
|
|
||||||
model=model,
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
)
|
|
||||||
s = "\n"
|
|
||||||
|
|
||||||
token_table = k2.SymbolTable.from_file(args.tokens)
|
|
||||||
|
|
||||||
def token_ids_to_words(token_ids: List[int]) -> str:
|
|
||||||
text = ""
|
|
||||||
for i in token_ids:
|
|
||||||
text += token_table[i]
|
|
||||||
return text.replace("▁", " ").strip()
|
|
||||||
|
|
||||||
for filename, hyp in zip(args.sound_files, hyps):
|
|
||||||
words = token_ids_to_words(hyp)
|
|
||||||
s += f"{filename}:\n{words}\n"
|
|
||||||
logging.info(s)
|
|
||||||
|
|
||||||
logging.info("Decoding Done")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
|
||||||
|
|
||||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/onnx_pretrained.py
Symbolic link
1
egs/mls_english/ASR/zipformer/onnx_pretrained.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/onnx_pretrained.py
|
File diff suppressed because it is too large
Load Diff
1
egs/mls_english/ASR/zipformer/optim.py
Symbolic link
1
egs/mls_english/ASR/zipformer/optim.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/optim.py
|
@ -1,380 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# Copyright 2021-2023 Xiaomi Corp. (authors: Fangjun Kuang, Zengwei Yao)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
"""
|
|
||||||
This script loads a checkpoint and uses it to decode waves.
|
|
||||||
You can generate the checkpoint with the following command:
|
|
||||||
|
|
||||||
Note: This is a example for librispeech dataset, if you are using different
|
|
||||||
dataset, you should change the argument values according to your dataset.
|
|
||||||
|
|
||||||
- For non-streaming model:
|
|
||||||
|
|
||||||
./zipformer/export.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--epoch 30 \
|
|
||||||
--avg 9
|
|
||||||
|
|
||||||
- For streaming model:
|
|
||||||
|
|
||||||
./zipformer/export.py \
|
|
||||||
--exp-dir ./zipformer/exp \
|
|
||||||
--causal 1 \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--epoch 30 \
|
|
||||||
--avg 9
|
|
||||||
|
|
||||||
Usage of this script:
|
|
||||||
|
|
||||||
- For non-streaming model:
|
|
||||||
|
|
||||||
(1) greedy search
|
|
||||||
./zipformer/pretrained.py \
|
|
||||||
--checkpoint ./zipformer/exp/pretrained.pt \
|
|
||||||
--tokens data/lang_bpe_500/tokens.txt \
|
|
||||||
--method greedy_search \
|
|
||||||
/path/to/foo.wav \
|
|
||||||
/path/to/bar.wav
|
|
||||||
|
|
||||||
(2) modified beam search
|
|
||||||
./zipformer/pretrained.py \
|
|
||||||
--checkpoint ./zipformer/exp/pretrained.pt \
|
|
||||||
--tokens ./data/lang_bpe_500/tokens.txt \
|
|
||||||
--method modified_beam_search \
|
|
||||||
/path/to/foo.wav \
|
|
||||||
/path/to/bar.wav
|
|
||||||
|
|
||||||
(3) fast beam search
|
|
||||||
./zipformer/pretrained.py \
|
|
||||||
--checkpoint ./zipformer/exp/pretrained.pt \
|
|
||||||
--tokens ./data/lang_bpe_500/tokens.txt \
|
|
||||||
--method fast_beam_search \
|
|
||||||
/path/to/foo.wav \
|
|
||||||
/path/to/bar.wav
|
|
||||||
|
|
||||||
- For streaming model:
|
|
||||||
|
|
||||||
(1) greedy search
|
|
||||||
./zipformer/pretrained.py \
|
|
||||||
--checkpoint ./zipformer/exp/pretrained.pt \
|
|
||||||
--causal 1 \
|
|
||||||
--chunk-size 16 \
|
|
||||||
--left-context-frames 128 \
|
|
||||||
--tokens ./data/lang_bpe_500/tokens.txt \
|
|
||||||
--method greedy_search \
|
|
||||||
/path/to/foo.wav \
|
|
||||||
/path/to/bar.wav
|
|
||||||
|
|
||||||
(2) modified beam search
|
|
||||||
./zipformer/pretrained.py \
|
|
||||||
--checkpoint ./zipformer/exp/pretrained.pt \
|
|
||||||
--causal 1 \
|
|
||||||
--chunk-size 16 \
|
|
||||||
--left-context-frames 128 \
|
|
||||||
--tokens ./data/lang_bpe_500/tokens.txt \
|
|
||||||
--method modified_beam_search \
|
|
||||||
/path/to/foo.wav \
|
|
||||||
/path/to/bar.wav
|
|
||||||
|
|
||||||
(3) fast beam search
|
|
||||||
./zipformer/pretrained.py \
|
|
||||||
--checkpoint ./zipformer/exp/pretrained.pt \
|
|
||||||
--causal 1 \
|
|
||||||
--chunk-size 16 \
|
|
||||||
--left-context-frames 128 \
|
|
||||||
--tokens ./data/lang_bpe_500/tokens.txt \
|
|
||||||
--method fast_beam_search \
|
|
||||||
/path/to/foo.wav \
|
|
||||||
/path/to/bar.wav
|
|
||||||
|
|
||||||
|
|
||||||
You can also use `./zipformer/exp/epoch-xx.pt`.
|
|
||||||
|
|
||||||
Note: ./zipformer/exp/pretrained.pt is generated by ./zipformer/export.py
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import logging
|
|
||||||
import math
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import kaldifeat
|
|
||||||
import torch
|
|
||||||
import torchaudio
|
|
||||||
from beam_search import (
|
|
||||||
fast_beam_search_one_best,
|
|
||||||
greedy_search_batch,
|
|
||||||
modified_beam_search,
|
|
||||||
)
|
|
||||||
from export import num_tokens
|
|
||||||
from torch.nn.utils.rnn import pad_sequence
|
|
||||||
from train import add_model_arguments, get_model, get_params
|
|
||||||
|
|
||||||
|
|
||||||
def get_parser():
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--checkpoint",
|
|
||||||
type=str,
|
|
||||||
required=True,
|
|
||||||
help="Path to the checkpoint. "
|
|
||||||
"The checkpoint is assumed to be saved by "
|
|
||||||
"icefall.checkpoint.save_checkpoint().",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--tokens",
|
|
||||||
type=str,
|
|
||||||
help="""Path to tokens.txt.""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--method",
|
|
||||||
type=str,
|
|
||||||
default="greedy_search",
|
|
||||||
help="""Possible values are:
|
|
||||||
- greedy_search
|
|
||||||
- modified_beam_search
|
|
||||||
- fast_beam_search
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"sound_files",
|
|
||||||
type=str,
|
|
||||||
nargs="+",
|
|
||||||
help="The input sound file(s) to transcribe. "
|
|
||||||
"Supported formats are those supported by torchaudio.load(). "
|
|
||||||
"For example, wav and flac are supported. "
|
|
||||||
"The sample rate has to be 16kHz.",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--sample-rate",
|
|
||||||
type=int,
|
|
||||||
default=16000,
|
|
||||||
help="The sample rate of the input sound file",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--beam-size",
|
|
||||||
type=int,
|
|
||||||
default=4,
|
|
||||||
help="""An integer indicating how many candidates we will keep for each
|
|
||||||
frame. Used only when --method is beam_search or
|
|
||||||
modified_beam_search.""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--beam",
|
|
||||||
type=float,
|
|
||||||
default=4,
|
|
||||||
help="""A floating point value to calculate the cutoff score during beam
|
|
||||||
search (i.e., `cutoff = max-score - beam`), which is the same as the
|
|
||||||
`beam` in Kaldi.
|
|
||||||
Used only when --method is fast_beam_search""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--max-contexts",
|
|
||||||
type=int,
|
|
||||||
default=4,
|
|
||||||
help="""Used only when --method is fast_beam_search""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--max-states",
|
|
||||||
type=int,
|
|
||||||
default=8,
|
|
||||||
help="""Used only when --method is fast_beam_search""",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--context-size",
|
|
||||||
type=int,
|
|
||||||
default=2,
|
|
||||||
help="The context size in the decoder. 1 means bigram; 2 means tri-gram",
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument(
|
|
||||||
"--max-sym-per-frame",
|
|
||||||
type=int,
|
|
||||||
default=1,
|
|
||||||
help="""Maximum number of symbols per frame. Used only when
|
|
||||||
--method is greedy_search.
|
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
add_model_arguments(parser)
|
|
||||||
|
|
||||||
return parser
|
|
||||||
|
|
||||||
|
|
||||||
def read_sound_files(
|
|
||||||
filenames: List[str], expected_sample_rate: float
|
|
||||||
) -> List[torch.Tensor]:
|
|
||||||
"""Read a list of sound files into a list 1-D float32 torch tensors.
|
|
||||||
Args:
|
|
||||||
filenames:
|
|
||||||
A list of sound filenames.
|
|
||||||
expected_sample_rate:
|
|
||||||
The expected sample rate of the sound files.
|
|
||||||
Returns:
|
|
||||||
Return a list of 1-D float32 torch tensors.
|
|
||||||
"""
|
|
||||||
ans = []
|
|
||||||
for f in filenames:
|
|
||||||
wave, sample_rate = torchaudio.load(f)
|
|
||||||
assert (
|
|
||||||
sample_rate == expected_sample_rate
|
|
||||||
), f"expected sample rate: {expected_sample_rate}. Given: {sample_rate}"
|
|
||||||
# We use only the first channel
|
|
||||||
ans.append(wave[0].contiguous())
|
|
||||||
return ans
|
|
||||||
|
|
||||||
|
|
||||||
@torch.no_grad()
|
|
||||||
def main():
|
|
||||||
parser = get_parser()
|
|
||||||
args = parser.parse_args()
|
|
||||||
|
|
||||||
params = get_params()
|
|
||||||
|
|
||||||
params.update(vars(args))
|
|
||||||
|
|
||||||
token_table = k2.SymbolTable.from_file(params.tokens)
|
|
||||||
|
|
||||||
params.blank_id = token_table["<blk>"]
|
|
||||||
params.unk_id = token_table["<unk>"]
|
|
||||||
params.vocab_size = num_tokens(token_table) + 1
|
|
||||||
|
|
||||||
logging.info(f"{params}")
|
|
||||||
|
|
||||||
device = torch.device("cpu")
|
|
||||||
if torch.cuda.is_available():
|
|
||||||
device = torch.device("cuda", 0)
|
|
||||||
|
|
||||||
logging.info(f"device: {device}")
|
|
||||||
|
|
||||||
if params.causal:
|
|
||||||
assert (
|
|
||||||
"," not in params.chunk_size
|
|
||||||
), "chunk_size should be one value in decoding."
|
|
||||||
assert (
|
|
||||||
"," not in params.left_context_frames
|
|
||||||
), "left_context_frames should be one value in decoding."
|
|
||||||
|
|
||||||
logging.info("Creating model")
|
|
||||||
model = get_model(params)
|
|
||||||
|
|
||||||
num_param = sum([p.numel() for p in model.parameters()])
|
|
||||||
logging.info(f"Number of model parameters: {num_param}")
|
|
||||||
|
|
||||||
checkpoint = torch.load(args.checkpoint, map_location="cpu")
|
|
||||||
model.load_state_dict(checkpoint["model"], strict=False)
|
|
||||||
model.to(device)
|
|
||||||
model.eval()
|
|
||||||
|
|
||||||
logging.info("Constructing Fbank computer")
|
|
||||||
opts = kaldifeat.FbankOptions()
|
|
||||||
opts.device = device
|
|
||||||
opts.frame_opts.dither = 0
|
|
||||||
opts.frame_opts.snip_edges = False
|
|
||||||
opts.frame_opts.samp_freq = params.sample_rate
|
|
||||||
opts.mel_opts.num_bins = params.feature_dim
|
|
||||||
opts.mel_opts.high_freq = -400
|
|
||||||
|
|
||||||
fbank = kaldifeat.Fbank(opts)
|
|
||||||
|
|
||||||
logging.info(f"Reading sound files: {params.sound_files}")
|
|
||||||
waves = read_sound_files(
|
|
||||||
filenames=params.sound_files, expected_sample_rate=params.sample_rate
|
|
||||||
)
|
|
||||||
waves = [w.to(device) for w in waves]
|
|
||||||
|
|
||||||
logging.info("Decoding started")
|
|
||||||
features = fbank(waves)
|
|
||||||
feature_lengths = [f.size(0) for f in features]
|
|
||||||
|
|
||||||
features = pad_sequence(features, batch_first=True, padding_value=math.log(1e-10))
|
|
||||||
feature_lengths = torch.tensor(feature_lengths, device=device)
|
|
||||||
|
|
||||||
# model forward
|
|
||||||
encoder_out, encoder_out_lens = model.forward_encoder(features, feature_lengths)
|
|
||||||
|
|
||||||
hyps = []
|
|
||||||
msg = f"Using {params.method}"
|
|
||||||
logging.info(msg)
|
|
||||||
|
|
||||||
def token_ids_to_words(token_ids: List[int]) -> str:
|
|
||||||
text = ""
|
|
||||||
for i in token_ids:
|
|
||||||
text += token_table[i]
|
|
||||||
return text.replace("▁", " ").strip()
|
|
||||||
|
|
||||||
if params.method == "fast_beam_search":
|
|
||||||
decoding_graph = k2.trivial_graph(params.vocab_size - 1, device=device)
|
|
||||||
hyp_tokens = fast_beam_search_one_best(
|
|
||||||
model=model,
|
|
||||||
decoding_graph=decoding_graph,
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
beam=params.beam,
|
|
||||||
max_contexts=params.max_contexts,
|
|
||||||
max_states=params.max_states,
|
|
||||||
)
|
|
||||||
for hyp in hyp_tokens:
|
|
||||||
hyps.append(token_ids_to_words(hyp))
|
|
||||||
elif params.method == "modified_beam_search":
|
|
||||||
hyp_tokens = modified_beam_search(
|
|
||||||
model=model,
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
beam=params.beam_size,
|
|
||||||
)
|
|
||||||
|
|
||||||
for hyp in hyp_tokens:
|
|
||||||
hyps.append(token_ids_to_words(hyp))
|
|
||||||
elif params.method == "greedy_search" and params.max_sym_per_frame == 1:
|
|
||||||
hyp_tokens = greedy_search_batch(
|
|
||||||
model=model,
|
|
||||||
encoder_out=encoder_out,
|
|
||||||
encoder_out_lens=encoder_out_lens,
|
|
||||||
)
|
|
||||||
for hyp in hyp_tokens:
|
|
||||||
hyps.append(token_ids_to_words(hyp))
|
|
||||||
else:
|
|
||||||
raise ValueError(f"Unsupported method: {params.method}")
|
|
||||||
|
|
||||||
s = "\n"
|
|
||||||
for filename, hyp in zip(params.sound_files, hyps):
|
|
||||||
s += f"{filename}:\n{hyp}\n\n"
|
|
||||||
logging.info(s)
|
|
||||||
|
|
||||||
logging.info("Decoding Done")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
formatter = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
|
|
||||||
|
|
||||||
logging.basicConfig(format=formatter, level=logging.INFO)
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/pretrained.py
Symbolic link
1
egs/mls_english/ASR/zipformer/pretrained.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/pretrained.py
|
File diff suppressed because it is too large
Load Diff
1
egs/mls_english/ASR/zipformer/scaling.py
Symbolic link
1
egs/mls_english/ASR/zipformer/scaling.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/scaling.py
|
@ -1,105 +0,0 @@
|
|||||||
# Copyright 2022-2023 Xiaomi Corp. (authors: Fangjun Kuang,
|
|
||||||
# Zengwei Yao)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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.
|
|
||||||
|
|
||||||
"""
|
|
||||||
This file replaces various modules in a model.
|
|
||||||
Specifically, ActivationBalancer is replaced with an identity operator;
|
|
||||||
Whiten is also replaced with an identity operator;
|
|
||||||
BasicNorm is replaced by a module with `exp` removed.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import copy
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from scaling import (
|
|
||||||
Balancer,
|
|
||||||
Dropout3,
|
|
||||||
ScaleGrad,
|
|
||||||
SwooshL,
|
|
||||||
SwooshLOnnx,
|
|
||||||
SwooshR,
|
|
||||||
SwooshROnnx,
|
|
||||||
Whiten,
|
|
||||||
)
|
|
||||||
from zipformer import CompactRelPositionalEncoding
|
|
||||||
|
|
||||||
|
|
||||||
# Copied from https://pytorch.org/docs/1.9.0/_modules/torch/nn/modules/module.html#Module.get_submodule # noqa
|
|
||||||
# get_submodule was added to nn.Module at v1.9.0
|
|
||||||
def get_submodule(model, target):
|
|
||||||
if target == "":
|
|
||||||
return model
|
|
||||||
atoms: List[str] = target.split(".")
|
|
||||||
mod: torch.nn.Module = model
|
|
||||||
for item in atoms:
|
|
||||||
if not hasattr(mod, item):
|
|
||||||
raise AttributeError(
|
|
||||||
mod._get_name() + " has no " "attribute `" + item + "`"
|
|
||||||
)
|
|
||||||
mod = getattr(mod, item)
|
|
||||||
if not isinstance(mod, torch.nn.Module):
|
|
||||||
raise AttributeError("`" + item + "` is not " "an nn.Module")
|
|
||||||
return mod
|
|
||||||
|
|
||||||
|
|
||||||
def convert_scaled_to_non_scaled(
|
|
||||||
model: nn.Module,
|
|
||||||
inplace: bool = False,
|
|
||||||
is_pnnx: bool = False,
|
|
||||||
is_onnx: bool = False,
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
model:
|
|
||||||
The model to be converted.
|
|
||||||
inplace:
|
|
||||||
If True, the input model is modified inplace.
|
|
||||||
If False, the input model is copied and we modify the copied version.
|
|
||||||
is_pnnx:
|
|
||||||
True if we are going to export the model for PNNX.
|
|
||||||
is_onnx:
|
|
||||||
True if we are going to export the model for ONNX.
|
|
||||||
Return:
|
|
||||||
Return a model without scaled layers.
|
|
||||||
"""
|
|
||||||
if not inplace:
|
|
||||||
model = copy.deepcopy(model)
|
|
||||||
|
|
||||||
d = {}
|
|
||||||
for name, m in model.named_modules():
|
|
||||||
if isinstance(m, (Balancer, Dropout3, ScaleGrad, Whiten)):
|
|
||||||
d[name] = nn.Identity()
|
|
||||||
elif is_onnx and isinstance(m, SwooshR):
|
|
||||||
d[name] = SwooshROnnx()
|
|
||||||
elif is_onnx and isinstance(m, SwooshL):
|
|
||||||
d[name] = SwooshLOnnx()
|
|
||||||
elif is_onnx and isinstance(m, CompactRelPositionalEncoding):
|
|
||||||
# We want to recreate the positional encoding vector when
|
|
||||||
# the input changes, so we have to use torch.jit.script()
|
|
||||||
# to replace torch.jit.trace()
|
|
||||||
d[name] = torch.jit.script(m)
|
|
||||||
|
|
||||||
for k, v in d.items():
|
|
||||||
if "." in k:
|
|
||||||
parent, child = k.rsplit(".", maxsplit=1)
|
|
||||||
setattr(get_submodule(model, parent), child, v)
|
|
||||||
else:
|
|
||||||
setattr(model, k, v)
|
|
||||||
|
|
||||||
return model
|
|
1
egs/mls_english/ASR/zipformer/scaling_converter.py
Symbolic link
1
egs/mls_english/ASR/zipformer/scaling_converter.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/scaling_converter.py
|
@ -1,295 +0,0 @@
|
|||||||
# Copyright 2022 Xiaomi Corp. (authors: Wei Kang)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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 warnings
|
|
||||||
from typing import List
|
|
||||||
|
|
||||||
import k2
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from beam_search import Hypothesis, HypothesisList, get_hyps_shape
|
|
||||||
from decode_stream import DecodeStream
|
|
||||||
|
|
||||||
from icefall.decode import one_best_decoding
|
|
||||||
from icefall.utils import get_texts
|
|
||||||
|
|
||||||
|
|
||||||
def greedy_search(
|
|
||||||
model: nn.Module,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
streams: List[DecodeStream],
|
|
||||||
blank_penalty: float = 0.0,
|
|
||||||
) -> None:
|
|
||||||
"""Greedy search in batch mode. It hardcodes --max-sym-per-frame=1.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
model:
|
|
||||||
The transducer model.
|
|
||||||
encoder_out:
|
|
||||||
Output from the encoder. Its shape is (N, T, C), where N >= 1.
|
|
||||||
streams:
|
|
||||||
A list of Stream objects.
|
|
||||||
"""
|
|
||||||
assert len(streams) == encoder_out.size(0)
|
|
||||||
assert encoder_out.ndim == 3
|
|
||||||
|
|
||||||
blank_id = model.decoder.blank_id
|
|
||||||
context_size = model.decoder.context_size
|
|
||||||
device = model.device
|
|
||||||
T = encoder_out.size(1)
|
|
||||||
|
|
||||||
decoder_input = torch.tensor(
|
|
||||||
[stream.hyp[-context_size:] for stream in streams],
|
|
||||||
device=device,
|
|
||||||
dtype=torch.int64,
|
|
||||||
)
|
|
||||||
# decoder_out is of shape (N, 1, decoder_out_dim)
|
|
||||||
decoder_out = model.decoder(decoder_input, need_pad=False)
|
|
||||||
decoder_out = model.joiner.decoder_proj(decoder_out)
|
|
||||||
|
|
||||||
for t in range(T):
|
|
||||||
# current_encoder_out's shape: (batch_size, 1, encoder_out_dim)
|
|
||||||
current_encoder_out = encoder_out[:, t : t + 1, :] # noqa
|
|
||||||
|
|
||||||
logits = model.joiner(
|
|
||||||
current_encoder_out.unsqueeze(2),
|
|
||||||
decoder_out.unsqueeze(1),
|
|
||||||
project_input=False,
|
|
||||||
)
|
|
||||||
# logits'shape (batch_size, vocab_size)
|
|
||||||
logits = logits.squeeze(1).squeeze(1)
|
|
||||||
|
|
||||||
if blank_penalty != 0.0:
|
|
||||||
logits[:, 0] -= blank_penalty
|
|
||||||
|
|
||||||
assert logits.ndim == 2, logits.shape
|
|
||||||
y = logits.argmax(dim=1).tolist()
|
|
||||||
emitted = False
|
|
||||||
for i, v in enumerate(y):
|
|
||||||
if v != blank_id:
|
|
||||||
streams[i].hyp.append(v)
|
|
||||||
emitted = True
|
|
||||||
if emitted:
|
|
||||||
# update decoder output
|
|
||||||
decoder_input = torch.tensor(
|
|
||||||
[stream.hyp[-context_size:] for stream in streams],
|
|
||||||
device=device,
|
|
||||||
dtype=torch.int64,
|
|
||||||
)
|
|
||||||
decoder_out = model.decoder(
|
|
||||||
decoder_input,
|
|
||||||
need_pad=False,
|
|
||||||
)
|
|
||||||
decoder_out = model.joiner.decoder_proj(decoder_out)
|
|
||||||
|
|
||||||
|
|
||||||
def modified_beam_search(
|
|
||||||
model: nn.Module,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
streams: List[DecodeStream],
|
|
||||||
num_active_paths: int = 4,
|
|
||||||
blank_penalty: float = 0.0,
|
|
||||||
) -> None:
|
|
||||||
"""Beam search in batch mode with --max-sym-per-frame=1 being hardcoded.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
model:
|
|
||||||
The RNN-T model.
|
|
||||||
encoder_out:
|
|
||||||
A 3-D tensor of shape (N, T, encoder_out_dim) containing the output of
|
|
||||||
the encoder model.
|
|
||||||
streams:
|
|
||||||
A list of stream objects.
|
|
||||||
num_active_paths:
|
|
||||||
Number of active paths during the beam search.
|
|
||||||
"""
|
|
||||||
assert encoder_out.ndim == 3, encoder_out.shape
|
|
||||||
assert len(streams) == encoder_out.size(0)
|
|
||||||
|
|
||||||
blank_id = model.decoder.blank_id
|
|
||||||
context_size = model.decoder.context_size
|
|
||||||
device = next(model.parameters()).device
|
|
||||||
batch_size = len(streams)
|
|
||||||
T = encoder_out.size(1)
|
|
||||||
|
|
||||||
B = [stream.hyps for stream in streams]
|
|
||||||
|
|
||||||
for t in range(T):
|
|
||||||
current_encoder_out = encoder_out[:, t].unsqueeze(1).unsqueeze(1)
|
|
||||||
# current_encoder_out's shape: (batch_size, 1, 1, encoder_out_dim)
|
|
||||||
|
|
||||||
hyps_shape = get_hyps_shape(B).to(device)
|
|
||||||
|
|
||||||
A = [list(b) for b in B]
|
|
||||||
B = [HypothesisList() for _ in range(batch_size)]
|
|
||||||
|
|
||||||
ys_log_probs = torch.stack(
|
|
||||||
[hyp.log_prob.reshape(1) for hyps in A for hyp in hyps], dim=0
|
|
||||||
) # (num_hyps, 1)
|
|
||||||
|
|
||||||
decoder_input = torch.tensor(
|
|
||||||
[hyp.ys[-context_size:] for hyps in A for hyp in hyps],
|
|
||||||
device=device,
|
|
||||||
dtype=torch.int64,
|
|
||||||
) # (num_hyps, context_size)
|
|
||||||
|
|
||||||
decoder_out = model.decoder(decoder_input, need_pad=False).unsqueeze(1)
|
|
||||||
decoder_out = model.joiner.decoder_proj(decoder_out)
|
|
||||||
# decoder_out is of shape (num_hyps, 1, 1, decoder_output_dim)
|
|
||||||
|
|
||||||
# Note: For torch 1.7.1 and below, it requires a torch.int64 tensor
|
|
||||||
# as index, so we use `to(torch.int64)` below.
|
|
||||||
current_encoder_out = torch.index_select(
|
|
||||||
current_encoder_out,
|
|
||||||
dim=0,
|
|
||||||
index=hyps_shape.row_ids(1).to(torch.int64),
|
|
||||||
) # (num_hyps, encoder_out_dim)
|
|
||||||
|
|
||||||
logits = model.joiner(current_encoder_out, decoder_out, project_input=False)
|
|
||||||
# logits is of shape (num_hyps, 1, 1, vocab_size)
|
|
||||||
|
|
||||||
logits = logits.squeeze(1).squeeze(1)
|
|
||||||
|
|
||||||
if blank_penalty != 0.0:
|
|
||||||
logits[:, 0] -= blank_penalty
|
|
||||||
|
|
||||||
log_probs = logits.log_softmax(dim=-1) # (num_hyps, vocab_size)
|
|
||||||
|
|
||||||
log_probs.add_(ys_log_probs)
|
|
||||||
|
|
||||||
vocab_size = log_probs.size(-1)
|
|
||||||
|
|
||||||
log_probs = log_probs.reshape(-1)
|
|
||||||
|
|
||||||
row_splits = hyps_shape.row_splits(1) * vocab_size
|
|
||||||
log_probs_shape = k2.ragged.create_ragged_shape2(
|
|
||||||
row_splits=row_splits, cached_tot_size=log_probs.numel()
|
|
||||||
)
|
|
||||||
ragged_log_probs = k2.RaggedTensor(shape=log_probs_shape, value=log_probs)
|
|
||||||
|
|
||||||
for i in range(batch_size):
|
|
||||||
topk_log_probs, topk_indexes = ragged_log_probs[i].topk(num_active_paths)
|
|
||||||
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
warnings.simplefilter("ignore")
|
|
||||||
topk_hyp_indexes = (topk_indexes // vocab_size).tolist()
|
|
||||||
topk_token_indexes = (topk_indexes % vocab_size).tolist()
|
|
||||||
|
|
||||||
for k in range(len(topk_hyp_indexes)):
|
|
||||||
hyp_idx = topk_hyp_indexes[k]
|
|
||||||
hyp = A[i][hyp_idx]
|
|
||||||
|
|
||||||
new_ys = hyp.ys[:]
|
|
||||||
new_token = topk_token_indexes[k]
|
|
||||||
if new_token != blank_id:
|
|
||||||
new_ys.append(new_token)
|
|
||||||
|
|
||||||
new_log_prob = topk_log_probs[k]
|
|
||||||
new_hyp = Hypothesis(ys=new_ys, log_prob=new_log_prob)
|
|
||||||
B[i].add(new_hyp)
|
|
||||||
|
|
||||||
for i in range(batch_size):
|
|
||||||
streams[i].hyps = B[i]
|
|
||||||
|
|
||||||
|
|
||||||
def fast_beam_search_one_best(
|
|
||||||
model: nn.Module,
|
|
||||||
encoder_out: torch.Tensor,
|
|
||||||
processed_lens: torch.Tensor,
|
|
||||||
streams: List[DecodeStream],
|
|
||||||
beam: float,
|
|
||||||
max_states: int,
|
|
||||||
max_contexts: int,
|
|
||||||
blank_penalty: float = 0.0,
|
|
||||||
) -> None:
|
|
||||||
"""It limits the maximum number of symbols per frame to 1.
|
|
||||||
|
|
||||||
A lattice is first generated by Fsa-based beam search, then we get the
|
|
||||||
recognition by applying shortest path on the lattice.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
model:
|
|
||||||
An instance of `Transducer`.
|
|
||||||
encoder_out:
|
|
||||||
A tensor of shape (N, T, C) from the encoder.
|
|
||||||
processed_lens:
|
|
||||||
A tensor of shape (N,) containing the number of processed frames
|
|
||||||
in `encoder_out` before padding.
|
|
||||||
streams:
|
|
||||||
A list of stream objects.
|
|
||||||
beam:
|
|
||||||
Beam value, similar to the beam used in Kaldi..
|
|
||||||
max_states:
|
|
||||||
Max states per stream per frame.
|
|
||||||
max_contexts:
|
|
||||||
Max contexts pre stream per frame.
|
|
||||||
"""
|
|
||||||
assert encoder_out.ndim == 3
|
|
||||||
B, T, C = encoder_out.shape
|
|
||||||
assert B == len(streams)
|
|
||||||
|
|
||||||
context_size = model.decoder.context_size
|
|
||||||
vocab_size = model.decoder.vocab_size
|
|
||||||
|
|
||||||
config = k2.RnntDecodingConfig(
|
|
||||||
vocab_size=vocab_size,
|
|
||||||
decoder_history_len=context_size,
|
|
||||||
beam=beam,
|
|
||||||
max_contexts=max_contexts,
|
|
||||||
max_states=max_states,
|
|
||||||
)
|
|
||||||
individual_streams = []
|
|
||||||
for i in range(B):
|
|
||||||
individual_streams.append(streams[i].rnnt_decoding_stream)
|
|
||||||
decoding_streams = k2.RnntDecodingStreams(individual_streams, config)
|
|
||||||
|
|
||||||
for t in range(T):
|
|
||||||
# shape is a RaggedShape of shape (B, context)
|
|
||||||
# contexts is a Tensor of shape (shape.NumElements(), context_size)
|
|
||||||
shape, contexts = decoding_streams.get_contexts()
|
|
||||||
# `nn.Embedding()` in torch below v1.7.1 supports only torch.int64
|
|
||||||
contexts = contexts.to(torch.int64)
|
|
||||||
# decoder_out is of shape (shape.NumElements(), 1, decoder_out_dim)
|
|
||||||
decoder_out = model.decoder(contexts, need_pad=False)
|
|
||||||
decoder_out = model.joiner.decoder_proj(decoder_out)
|
|
||||||
# current_encoder_out is of shape
|
|
||||||
# (shape.NumElements(), 1, joiner_dim)
|
|
||||||
# fmt: off
|
|
||||||
current_encoder_out = torch.index_select(
|
|
||||||
encoder_out[:, t:t + 1, :], 0, shape.row_ids(1).to(torch.int64)
|
|
||||||
)
|
|
||||||
# fmt: on
|
|
||||||
logits = model.joiner(
|
|
||||||
current_encoder_out.unsqueeze(2),
|
|
||||||
decoder_out.unsqueeze(1),
|
|
||||||
project_input=False,
|
|
||||||
)
|
|
||||||
logits = logits.squeeze(1).squeeze(1)
|
|
||||||
|
|
||||||
if blank_penalty != 0.0:
|
|
||||||
logits[:, 0] -= blank_penalty
|
|
||||||
|
|
||||||
log_probs = logits.log_softmax(dim=-1)
|
|
||||||
decoding_streams.advance(log_probs)
|
|
||||||
|
|
||||||
decoding_streams.terminate_and_flush_to_streams()
|
|
||||||
|
|
||||||
lattice = decoding_streams.format_output(processed_lens.tolist())
|
|
||||||
best_path = one_best_decoding(lattice)
|
|
||||||
hyp_tokens = get_texts(best_path)
|
|
||||||
|
|
||||||
for i in range(B):
|
|
||||||
streams[i].hyp = hyp_tokens[i]
|
|
1
egs/mls_english/ASR/zipformer/streaming_beam_search.py
Symbolic link
1
egs/mls_english/ASR/zipformer/streaming_beam_search.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/streaming_beam_search.py
|
@ -1,406 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# Copyright 2023 Xiaomi Corp. (authors: Daniel Povey,
|
|
||||||
# Zengwei Yao)
|
|
||||||
#
|
|
||||||
# See ../../../../LICENSE for clarification regarding multiple authors
|
|
||||||
#
|
|
||||||
# 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 warnings
|
|
||||||
from typing import Tuple
|
|
||||||
|
|
||||||
import torch
|
|
||||||
from scaling import (
|
|
||||||
Balancer,
|
|
||||||
BiasNorm,
|
|
||||||
Dropout3,
|
|
||||||
FloatLike,
|
|
||||||
Optional,
|
|
||||||
ScaledConv2d,
|
|
||||||
ScaleGrad,
|
|
||||||
ScheduledFloat,
|
|
||||||
SwooshL,
|
|
||||||
SwooshR,
|
|
||||||
Whiten,
|
|
||||||
)
|
|
||||||
from torch import Tensor, nn
|
|
||||||
|
|
||||||
|
|
||||||
class ConvNeXt(nn.Module):
|
|
||||||
"""
|
|
||||||
Our interpretation of the ConvNeXt module as used in https://arxiv.org/pdf/2206.14747.pdf
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
channels: int,
|
|
||||||
hidden_ratio: int = 3,
|
|
||||||
kernel_size: Tuple[int, int] = (7, 7),
|
|
||||||
layerdrop_rate: FloatLike = None,
|
|
||||||
):
|
|
||||||
super().__init__()
|
|
||||||
self.padding = ((kernel_size[0] - 1) // 2, (kernel_size[1] - 1) // 2)
|
|
||||||
hidden_channels = channels * hidden_ratio
|
|
||||||
if layerdrop_rate is None:
|
|
||||||
layerdrop_rate = ScheduledFloat((0.0, 0.2), (20000.0, 0.015))
|
|
||||||
self.layerdrop_rate = layerdrop_rate
|
|
||||||
|
|
||||||
self.depthwise_conv = nn.Conv2d(
|
|
||||||
in_channels=channels,
|
|
||||||
out_channels=channels,
|
|
||||||
groups=channels,
|
|
||||||
kernel_size=kernel_size,
|
|
||||||
padding=self.padding,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.pointwise_conv1 = nn.Conv2d(
|
|
||||||
in_channels=channels, out_channels=hidden_channels, kernel_size=1
|
|
||||||
)
|
|
||||||
|
|
||||||
self.hidden_balancer = Balancer(
|
|
||||||
hidden_channels,
|
|
||||||
channel_dim=1,
|
|
||||||
min_positive=0.3,
|
|
||||||
max_positive=1.0,
|
|
||||||
min_abs=0.75,
|
|
||||||
max_abs=5.0,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.activation = SwooshL()
|
|
||||||
self.pointwise_conv2 = ScaledConv2d(
|
|
||||||
in_channels=hidden_channels,
|
|
||||||
out_channels=channels,
|
|
||||||
kernel_size=1,
|
|
||||||
initial_scale=0.01,
|
|
||||||
)
|
|
||||||
|
|
||||||
self.out_balancer = Balancer(
|
|
||||||
channels,
|
|
||||||
channel_dim=1,
|
|
||||||
min_positive=0.4,
|
|
||||||
max_positive=0.6,
|
|
||||||
min_abs=1.0,
|
|
||||||
max_abs=6.0,
|
|
||||||
)
|
|
||||||
self.out_whiten = Whiten(
|
|
||||||
num_groups=1,
|
|
||||||
whitening_limit=5.0,
|
|
||||||
prob=(0.025, 0.25),
|
|
||||||
grad_scale=0.01,
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x: Tensor) -> Tensor:
|
|
||||||
if torch.jit.is_scripting() or torch.jit.is_tracing() or not self.training:
|
|
||||||
return self.forward_internal(x)
|
|
||||||
layerdrop_rate = float(self.layerdrop_rate)
|
|
||||||
|
|
||||||
if layerdrop_rate != 0.0:
|
|
||||||
batch_size = x.shape[0]
|
|
||||||
mask = (
|
|
||||||
torch.rand((batch_size, 1, 1, 1), dtype=x.dtype, device=x.device)
|
|
||||||
> layerdrop_rate
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
mask = None
|
|
||||||
# turns out this caching idea does not work with --world-size > 1
|
|
||||||
# return caching_eval(self.forward_internal, x, mask)
|
|
||||||
return self.forward_internal(x, mask)
|
|
||||||
|
|
||||||
def forward_internal(
|
|
||||||
self, x: Tensor, layer_skip_mask: Optional[Tensor] = None
|
|
||||||
) -> Tensor:
|
|
||||||
"""
|
|
||||||
x layout: (N, C, H, W), i.e. (batch_size, num_channels, num_frames, num_freqs)
|
|
||||||
|
|
||||||
The returned value has the same shape as x.
|
|
||||||
"""
|
|
||||||
bypass = x
|
|
||||||
x = self.depthwise_conv(x)
|
|
||||||
x = self.pointwise_conv1(x)
|
|
||||||
x = self.hidden_balancer(x)
|
|
||||||
x = self.activation(x)
|
|
||||||
x = self.pointwise_conv2(x)
|
|
||||||
|
|
||||||
if layer_skip_mask is not None:
|
|
||||||
x = x * layer_skip_mask
|
|
||||||
|
|
||||||
x = bypass + x
|
|
||||||
x = self.out_balancer(x)
|
|
||||||
|
|
||||||
if x.requires_grad:
|
|
||||||
x = x.transpose(1, 3) # (N, W, H, C); need channel dim to be last
|
|
||||||
x = self.out_whiten(x)
|
|
||||||
x = x.transpose(1, 3) # (N, C, H, W)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
def streaming_forward(
|
|
||||||
self,
|
|
||||||
x: Tensor,
|
|
||||||
cached_left_pad: Tensor,
|
|
||||||
) -> Tuple[Tensor, Tensor]:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
x layout: (N, C, H, W), i.e. (batch_size, num_channels, num_frames, num_freqs)
|
|
||||||
cached_left_pad: (batch_size, num_channels, left_pad, num_freqs)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- The returned value has the same shape as x.
|
|
||||||
- Updated cached_left_pad.
|
|
||||||
"""
|
|
||||||
padding = self.padding
|
|
||||||
|
|
||||||
# The length without right padding for depth-wise conv
|
|
||||||
T = x.size(2) - padding[0]
|
|
||||||
|
|
||||||
bypass = x[:, :, :T, :]
|
|
||||||
|
|
||||||
# Pad left side
|
|
||||||
assert cached_left_pad.size(2) == padding[0], (
|
|
||||||
cached_left_pad.size(2),
|
|
||||||
padding[0],
|
|
||||||
)
|
|
||||||
x = torch.cat([cached_left_pad, x], dim=2)
|
|
||||||
# Update cached left padding
|
|
||||||
cached_left_pad = x[:, :, T : padding[0] + T, :]
|
|
||||||
|
|
||||||
# depthwise_conv
|
|
||||||
x = torch.nn.functional.conv2d(
|
|
||||||
x,
|
|
||||||
weight=self.depthwise_conv.weight,
|
|
||||||
bias=self.depthwise_conv.bias,
|
|
||||||
padding=(0, padding[1]),
|
|
||||||
groups=self.depthwise_conv.groups,
|
|
||||||
)
|
|
||||||
x = self.pointwise_conv1(x)
|
|
||||||
x = self.hidden_balancer(x)
|
|
||||||
x = self.activation(x)
|
|
||||||
x = self.pointwise_conv2(x)
|
|
||||||
|
|
||||||
x = bypass + x
|
|
||||||
return x, cached_left_pad
|
|
||||||
|
|
||||||
|
|
||||||
class Conv2dSubsampling(nn.Module):
|
|
||||||
"""Convolutional 2D subsampling (to 1/2 length).
|
|
||||||
|
|
||||||
Convert an input of shape (N, T, idim) to an output
|
|
||||||
with shape (N, T', odim), where
|
|
||||||
T' = (T-3)//2 - 2 == (T-7)//2
|
|
||||||
|
|
||||||
It is based on
|
|
||||||
https://github.com/espnet/espnet/blob/master/espnet/nets/pytorch_backend/transformer/subsampling.py # noqa
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
in_channels: int,
|
|
||||||
out_channels: int,
|
|
||||||
layer1_channels: int = 8,
|
|
||||||
layer2_channels: int = 32,
|
|
||||||
layer3_channels: int = 128,
|
|
||||||
dropout: FloatLike = 0.1,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
in_channels:
|
|
||||||
Number of channels in. The input shape is (N, T, in_channels).
|
|
||||||
Caution: It requires: T >=7, in_channels >=7
|
|
||||||
out_channels
|
|
||||||
Output dim. The output shape is (N, (T-3)//2, out_channels)
|
|
||||||
layer1_channels:
|
|
||||||
Number of channels in layer1
|
|
||||||
layer1_channels:
|
|
||||||
Number of channels in layer2
|
|
||||||
bottleneck:
|
|
||||||
bottleneck dimension for 1d squeeze-excite
|
|
||||||
"""
|
|
||||||
assert in_channels >= 7
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
# The ScaleGrad module is there to prevent the gradients
|
|
||||||
# w.r.t. the weight or bias of the first Conv2d module in self.conv from
|
|
||||||
# exceeding the range of fp16 when using automatic mixed precision (amp)
|
|
||||||
# training. (The second one is necessary to stop its bias from getting
|
|
||||||
# a too-large gradient).
|
|
||||||
|
|
||||||
self.conv = nn.Sequential(
|
|
||||||
nn.Conv2d(
|
|
||||||
in_channels=1,
|
|
||||||
out_channels=layer1_channels,
|
|
||||||
kernel_size=3,
|
|
||||||
padding=(0, 1), # (time, freq)
|
|
||||||
),
|
|
||||||
ScaleGrad(0.2),
|
|
||||||
Balancer(layer1_channels, channel_dim=1, max_abs=1.0),
|
|
||||||
SwooshR(),
|
|
||||||
nn.Conv2d(
|
|
||||||
in_channels=layer1_channels,
|
|
||||||
out_channels=layer2_channels,
|
|
||||||
kernel_size=3,
|
|
||||||
stride=2,
|
|
||||||
padding=0,
|
|
||||||
),
|
|
||||||
Balancer(layer2_channels, channel_dim=1, max_abs=4.0),
|
|
||||||
SwooshR(),
|
|
||||||
nn.Conv2d(
|
|
||||||
in_channels=layer2_channels,
|
|
||||||
out_channels=layer3_channels,
|
|
||||||
kernel_size=3,
|
|
||||||
stride=(1, 2), # (time, freq)
|
|
||||||
),
|
|
||||||
Balancer(layer3_channels, channel_dim=1, max_abs=4.0),
|
|
||||||
SwooshR(),
|
|
||||||
)
|
|
||||||
|
|
||||||
# just one convnext layer
|
|
||||||
self.convnext = ConvNeXt(layer3_channels, kernel_size=(7, 7))
|
|
||||||
|
|
||||||
# (in_channels-3)//4
|
|
||||||
self.out_width = (((in_channels - 1) // 2) - 1) // 2
|
|
||||||
self.layer3_channels = layer3_channels
|
|
||||||
|
|
||||||
self.out = nn.Linear(self.out_width * layer3_channels, out_channels)
|
|
||||||
# use a larger than normal grad_scale on this whitening module; there is
|
|
||||||
# only one such module, so there is not a concern about adding together
|
|
||||||
# many copies of this extra gradient term.
|
|
||||||
self.out_whiten = Whiten(
|
|
||||||
num_groups=1,
|
|
||||||
whitening_limit=ScheduledFloat((0.0, 4.0), (20000.0, 8.0), default=4.0),
|
|
||||||
prob=(0.025, 0.25),
|
|
||||||
grad_scale=0.02,
|
|
||||||
)
|
|
||||||
|
|
||||||
# max_log_eps=0.0 is to prevent both eps and the output of self.out from
|
|
||||||
# getting large, there is an unnecessary degree of freedom.
|
|
||||||
self.out_norm = BiasNorm(out_channels)
|
|
||||||
self.dropout = Dropout3(dropout, shared_dim=1)
|
|
||||||
|
|
||||||
def forward(
|
|
||||||
self, x: torch.Tensor, x_lens: torch.Tensor
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor]:
|
|
||||||
"""Subsample x.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
Its shape is (N, T, idim).
|
|
||||||
x_lens:
|
|
||||||
A tensor of shape (batch_size,) containing the number of frames in
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- a tensor of shape (N, (T-7)//2, odim)
|
|
||||||
- output lengths, of shape (batch_size,)
|
|
||||||
"""
|
|
||||||
# On entry, x is (N, T, idim)
|
|
||||||
x = x.unsqueeze(1) # (N, T, idim) -> (N, 1, T, idim) i.e., (N, C, H, W)
|
|
||||||
# scaling x by 0.1 allows us to use a larger grad-scale in fp16 "amp" (automatic mixed precision)
|
|
||||||
# training, since the weights in the first convolution are otherwise the limiting factor for getting infinite
|
|
||||||
# gradients.
|
|
||||||
x = self.conv(x)
|
|
||||||
x = self.convnext(x)
|
|
||||||
|
|
||||||
# Now x is of shape (N, odim, (T-7)//2, (idim-3)//4)
|
|
||||||
b, c, t, f = x.size()
|
|
||||||
|
|
||||||
x = x.transpose(1, 2).reshape(b, t, c * f)
|
|
||||||
# now x: (N, (T-7)//2, out_width * layer3_channels))
|
|
||||||
|
|
||||||
x = self.out(x)
|
|
||||||
# Now x is of shape (N, (T-7)//2, odim)
|
|
||||||
x = self.out_whiten(x)
|
|
||||||
x = self.out_norm(x)
|
|
||||||
x = self.dropout(x)
|
|
||||||
|
|
||||||
if torch.jit.is_scripting() or torch.jit.is_tracing():
|
|
||||||
x_lens = (x_lens - 7) // 2
|
|
||||||
else:
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
warnings.simplefilter("ignore")
|
|
||||||
x_lens = (x_lens - 7) // 2
|
|
||||||
assert x.size(1) == x_lens.max().item(), (x.size(1), x_lens.max())
|
|
||||||
|
|
||||||
return x, x_lens
|
|
||||||
|
|
||||||
def streaming_forward(
|
|
||||||
self,
|
|
||||||
x: torch.Tensor,
|
|
||||||
x_lens: torch.Tensor,
|
|
||||||
cached_left_pad: Tensor,
|
|
||||||
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
|
||||||
"""Subsample x.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
x:
|
|
||||||
Its shape is (N, T, idim).
|
|
||||||
x_lens:
|
|
||||||
A tensor of shape (batch_size,) containing the number of frames in
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
- a tensor of shape (N, (T-7)//2, odim)
|
|
||||||
- output lengths, of shape (batch_size,)
|
|
||||||
- updated cache
|
|
||||||
"""
|
|
||||||
# On entry, x is (N, T, idim)
|
|
||||||
x = x.unsqueeze(1) # (N, T, idim) -> (N, 1, T, idim) i.e., (N, C, H, W)
|
|
||||||
|
|
||||||
# T' = (T-7)//2
|
|
||||||
x = self.conv(x)
|
|
||||||
|
|
||||||
# T' = (T-7)//2-3
|
|
||||||
x, cached_left_pad = self.convnext.streaming_forward(
|
|
||||||
x, cached_left_pad=cached_left_pad
|
|
||||||
)
|
|
||||||
|
|
||||||
# Now x is of shape (N, odim, T', ((idim-1)//2 - 1)//2)
|
|
||||||
b, c, t, f = x.size()
|
|
||||||
|
|
||||||
x = x.transpose(1, 2).reshape(b, t, c * f)
|
|
||||||
# now x: (N, T', out_width * layer3_channels))
|
|
||||||
|
|
||||||
x = self.out(x)
|
|
||||||
# Now x is of shape (N, T', odim)
|
|
||||||
x = self.out_norm(x)
|
|
||||||
|
|
||||||
if torch.jit.is_scripting() or torch.jit.is_tracing():
|
|
||||||
assert self.convnext.padding[0] == 3
|
|
||||||
# The ConvNeXt module needs 3 frames of right padding after subsampling
|
|
||||||
x_lens = (x_lens - 7) // 2 - 3
|
|
||||||
else:
|
|
||||||
with warnings.catch_warnings():
|
|
||||||
warnings.simplefilter("ignore")
|
|
||||||
# The ConvNeXt module needs 3 frames of right padding after subsampling
|
|
||||||
assert self.convnext.padding[0] == 3
|
|
||||||
x_lens = (x_lens - 7) // 2 - 3
|
|
||||||
|
|
||||||
assert x.size(1) == x_lens.max().item(), (x.shape, x_lens.max())
|
|
||||||
|
|
||||||
return x, x_lens, cached_left_pad
|
|
||||||
|
|
||||||
@torch.jit.export
|
|
||||||
def get_init_states(
|
|
||||||
self,
|
|
||||||
batch_size: int = 1,
|
|
||||||
device: torch.device = torch.device("cpu"),
|
|
||||||
) -> Tensor:
|
|
||||||
"""Get initial states for Conv2dSubsampling module.
|
|
||||||
It is the cached left padding for ConvNeXt module,
|
|
||||||
of shape (batch_size, num_channels, left_pad, num_freqs)
|
|
||||||
"""
|
|
||||||
left_pad = self.convnext.padding[0]
|
|
||||||
freq = self.out_width
|
|
||||||
channels = self.layer3_channels
|
|
||||||
cached_embed_left_pad = torch.zeros(batch_size, channels, left_pad, freq).to(
|
|
||||||
device
|
|
||||||
)
|
|
||||||
|
|
||||||
return cached_embed_left_pad
|
|
1
egs/mls_english/ASR/zipformer/subsampling.py
Symbolic link
1
egs/mls_english/ASR/zipformer/subsampling.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/subsampling.py
|
@ -1,82 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
import torch
|
|
||||||
from scaling import PiecewiseLinear, ScheduledFloat, SwooshL, SwooshR
|
|
||||||
|
|
||||||
|
|
||||||
def test_piecewise_linear():
|
|
||||||
# An identity map in the range [0, 1].
|
|
||||||
# 1 - identity map in the range [1, 2]
|
|
||||||
# x1=0, y1=0
|
|
||||||
# x2=1, y2=1
|
|
||||||
# x3=2, y3=0
|
|
||||||
pl = PiecewiseLinear((0, 0), (1, 1), (2, 0))
|
|
||||||
assert pl(0.25) == 0.25, pl(0.25)
|
|
||||||
assert pl(0.625) == 0.625, pl(0.625)
|
|
||||||
assert pl(1.25) == 0.75, pl(1.25)
|
|
||||||
|
|
||||||
assert pl(-10) == pl(0), pl(-10) # out of range
|
|
||||||
assert pl(10) == pl(2), pl(10) # out of range
|
|
||||||
|
|
||||||
# multiplication
|
|
||||||
pl10 = pl * 10
|
|
||||||
assert pl10(1) == 10 * pl(1)
|
|
||||||
assert pl10(0.5) == 10 * pl(0.5)
|
|
||||||
|
|
||||||
|
|
||||||
def test_scheduled_float():
|
|
||||||
# Initial value is 0.2 and it decreases linearly towards 0 at 4000
|
|
||||||
dropout = ScheduledFloat((0, 0.2), (4000, 0.0), default=0.0)
|
|
||||||
dropout.batch_count = 0
|
|
||||||
assert float(dropout) == 0.2, (float(dropout), dropout.batch_count)
|
|
||||||
|
|
||||||
dropout.batch_count = 1000
|
|
||||||
assert abs(float(dropout) - 0.15) < 1e-5, (float(dropout), dropout.batch_count)
|
|
||||||
|
|
||||||
dropout.batch_count = 2000
|
|
||||||
assert float(dropout) == 0.1, (float(dropout), dropout.batch_count)
|
|
||||||
|
|
||||||
dropout.batch_count = 3000
|
|
||||||
assert abs(float(dropout) - 0.05) < 1e-5, (float(dropout), dropout.batch_count)
|
|
||||||
|
|
||||||
dropout.batch_count = 4000
|
|
||||||
assert float(dropout) == 0.0, (float(dropout), dropout.batch_count)
|
|
||||||
|
|
||||||
dropout.batch_count = 5000 # out of range
|
|
||||||
assert float(dropout) == 0.0, (float(dropout), dropout.batch_count)
|
|
||||||
|
|
||||||
|
|
||||||
def test_swoosh():
|
|
||||||
x1 = torch.linspace(start=-10, end=0, steps=100, dtype=torch.float32)
|
|
||||||
x2 = torch.linspace(start=0, end=10, steps=100, dtype=torch.float32)
|
|
||||||
x = torch.cat([x1, x2[1:]])
|
|
||||||
|
|
||||||
left = SwooshL()(x)
|
|
||||||
r = SwooshR()(x)
|
|
||||||
|
|
||||||
relu = torch.nn.functional.relu(x)
|
|
||||||
print(left[x == 0], r[x == 0])
|
|
||||||
plt.plot(x, left, "k")
|
|
||||||
plt.plot(x, r, "r")
|
|
||||||
plt.plot(x, relu, "b")
|
|
||||||
plt.axis([-10, 10, -1, 10]) # [xmin, xmax, ymin, ymax]
|
|
||||||
plt.legend(
|
|
||||||
[
|
|
||||||
"SwooshL(x) = log(1 + exp(x-4)) - 0.08x - 0.035 ",
|
|
||||||
"SwooshR(x) = log(1 + exp(x-1)) - 0.08x - 0.313261687",
|
|
||||||
"ReLU(x) = max(0, x)",
|
|
||||||
]
|
|
||||||
)
|
|
||||||
plt.grid()
|
|
||||||
plt.savefig("swoosh.pdf")
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
test_piecewise_linear()
|
|
||||||
test_scheduled_float()
|
|
||||||
test_swoosh()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/test_scaling.py
Symbolic link
1
egs/mls_english/ASR/zipformer/test_scaling.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/test_scaling.py
|
@ -1,152 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
import torch
|
|
||||||
from scaling import ScheduledFloat
|
|
||||||
from subsampling import Conv2dSubsampling
|
|
||||||
|
|
||||||
|
|
||||||
def test_conv2d_subsampling():
|
|
||||||
layer1_channels = 8
|
|
||||||
layer2_channels = 32
|
|
||||||
layer3_channels = 128
|
|
||||||
|
|
||||||
out_channels = 192
|
|
||||||
encoder_embed = Conv2dSubsampling(
|
|
||||||
in_channels=80,
|
|
||||||
out_channels=out_channels,
|
|
||||||
layer1_channels=layer1_channels,
|
|
||||||
layer2_channels=layer2_channels,
|
|
||||||
layer3_channels=layer3_channels,
|
|
||||||
dropout=ScheduledFloat((0.0, 0.3), (20000.0, 0.1)),
|
|
||||||
)
|
|
||||||
N = 2
|
|
||||||
T = 200
|
|
||||||
num_features = 80
|
|
||||||
x = torch.rand(N, T, num_features)
|
|
||||||
x_copy = x.clone()
|
|
||||||
|
|
||||||
x = x.unsqueeze(1) # (N, 1, T, num_features)
|
|
||||||
|
|
||||||
x = encoder_embed.conv[0](x) # conv2d, in 1, out 8, kernel 3, padding (0,1)
|
|
||||||
assert x.shape == (N, layer1_channels, T - 2, num_features)
|
|
||||||
# (2, 8, 198, 80)
|
|
||||||
|
|
||||||
x = encoder_embed.conv[1](x) # scale grad
|
|
||||||
x = encoder_embed.conv[2](x) # balancer
|
|
||||||
x = encoder_embed.conv[3](x) # swooshR
|
|
||||||
|
|
||||||
x = encoder_embed.conv[4](x) # conv2d, in 8, out 32, kernel 3, stride 2
|
|
||||||
assert x.shape == (
|
|
||||||
N,
|
|
||||||
layer2_channels,
|
|
||||||
((T - 2) - 3) // 2 + 1,
|
|
||||||
(num_features - 3) // 2 + 1,
|
|
||||||
)
|
|
||||||
# (2, 32, 98, 39)
|
|
||||||
|
|
||||||
x = encoder_embed.conv[5](x) # balancer
|
|
||||||
x = encoder_embed.conv[6](x) # swooshR
|
|
||||||
|
|
||||||
# conv2d:
|
|
||||||
# in 32, out 128, kernel 3, stride (1, 2)
|
|
||||||
x = encoder_embed.conv[7](x)
|
|
||||||
assert x.shape == (
|
|
||||||
N,
|
|
||||||
layer3_channels,
|
|
||||||
(((T - 2) - 3) // 2 + 1) - 2,
|
|
||||||
(((num_features - 3) // 2 + 1) - 3) // 2 + 1,
|
|
||||||
)
|
|
||||||
# (2, 128, 96, 19)
|
|
||||||
|
|
||||||
x = encoder_embed.conv[8](x) # balancer
|
|
||||||
x = encoder_embed.conv[9](x) # swooshR
|
|
||||||
|
|
||||||
# (((T - 2) - 3) // 2 + 1) - 2
|
|
||||||
# = (T - 2) - 3) // 2 + 1 - 2
|
|
||||||
# = ((T - 2) - 3) // 2 - 1
|
|
||||||
# = (T - 2 - 3) // 2 - 1
|
|
||||||
# = (T - 5) // 2 - 1
|
|
||||||
# = (T - 7) // 2
|
|
||||||
assert x.shape[2] == (x_copy.shape[1] - 7) // 2
|
|
||||||
|
|
||||||
# (((num_features - 3) // 2 + 1) - 3) // 2 + 1,
|
|
||||||
# = ((num_features - 3) // 2 + 1 - 3) // 2 + 1,
|
|
||||||
# = ((num_features - 3) // 2 - 2) // 2 + 1,
|
|
||||||
# = (num_features - 3 - 4) // 2 // 2 + 1,
|
|
||||||
# = (num_features - 7) // 2 // 2 + 1,
|
|
||||||
# = (num_features - 7) // 4 + 1,
|
|
||||||
# = (num_features - 3) // 4
|
|
||||||
assert x.shape[3] == (x_copy.shape[2] - 3) // 4
|
|
||||||
|
|
||||||
assert x.shape == (N, layer3_channels, (T - 7) // 2, (num_features - 3) // 4)
|
|
||||||
|
|
||||||
# Input shape to convnext is
|
|
||||||
#
|
|
||||||
# (N, layer3_channels, (T-7)//2, (num_features - 3)//4)
|
|
||||||
|
|
||||||
# conv2d: in layer3_channels, out layer3_channels, groups layer3_channels
|
|
||||||
# kernel_size 7, padding 3
|
|
||||||
x = encoder_embed.convnext.depthwise_conv(x)
|
|
||||||
assert x.shape == (N, layer3_channels, (T - 7) // 2, (num_features - 3) // 4)
|
|
||||||
|
|
||||||
# conv2d: in layer3_channels, out hidden_ratio * layer3_channels, kernel_size 1
|
|
||||||
x = encoder_embed.convnext.pointwise_conv1(x)
|
|
||||||
assert x.shape == (N, layer3_channels * 3, (T - 7) // 2, (num_features - 3) // 4)
|
|
||||||
|
|
||||||
x = encoder_embed.convnext.hidden_balancer(x) # balancer
|
|
||||||
x = encoder_embed.convnext.activation(x) # swooshL
|
|
||||||
|
|
||||||
# conv2d: in hidden_ratio * layer3_channels, out layer3_channels, kernel 1
|
|
||||||
x = encoder_embed.convnext.pointwise_conv2(x)
|
|
||||||
assert x.shape == (N, layer3_channels, (T - 7) // 2, (num_features - 3) // 4)
|
|
||||||
|
|
||||||
# bypass and layer drop, omitted here.
|
|
||||||
x = encoder_embed.convnext.out_balancer(x)
|
|
||||||
|
|
||||||
# Note: the input and output shape of ConvNeXt are the same
|
|
||||||
|
|
||||||
x = x.transpose(1, 2).reshape(N, (T - 7) // 2, -1)
|
|
||||||
assert x.shape == (N, (T - 7) // 2, layer3_channels * ((num_features - 3) // 4))
|
|
||||||
|
|
||||||
x = encoder_embed.out(x)
|
|
||||||
assert x.shape == (N, (T - 7) // 2, out_channels)
|
|
||||||
|
|
||||||
x = encoder_embed.out_whiten(x)
|
|
||||||
x = encoder_embed.out_norm(x)
|
|
||||||
# final layer is dropout
|
|
||||||
|
|
||||||
# test streaming forward
|
|
||||||
|
|
||||||
subsampling_factor = 2
|
|
||||||
cached_left_padding = encoder_embed.get_init_states(batch_size=N)
|
|
||||||
depthwise_conv_kernel_size = 7
|
|
||||||
pad_size = (depthwise_conv_kernel_size - 1) // 2
|
|
||||||
|
|
||||||
assert cached_left_padding.shape == (
|
|
||||||
N,
|
|
||||||
layer3_channels,
|
|
||||||
pad_size,
|
|
||||||
(num_features - 3) // 4,
|
|
||||||
)
|
|
||||||
|
|
||||||
chunk_size = 16
|
|
||||||
right_padding = pad_size * subsampling_factor
|
|
||||||
T = chunk_size * subsampling_factor + 7 + right_padding
|
|
||||||
x = torch.rand(N, T, num_features)
|
|
||||||
x_lens = torch.tensor([T] * N)
|
|
||||||
y, y_lens, next_cached_left_padding = encoder_embed.streaming_forward(
|
|
||||||
x, x_lens, cached_left_padding
|
|
||||||
)
|
|
||||||
|
|
||||||
assert y.shape == (N, chunk_size, out_channels), y.shape
|
|
||||||
assert next_cached_left_padding.shape == cached_left_padding.shape
|
|
||||||
|
|
||||||
assert y.shape[1] == y_lens[0] == y_lens[1]
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
test_conv2d_subsampling()
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
1
egs/mls_english/ASR/zipformer/test_subsampling.py
Symbolic link
1
egs/mls_english/ASR/zipformer/test_subsampling.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/test_subsampling.py
|
File diff suppressed because it is too large
Load Diff
1
egs/mls_english/ASR/zipformer/zipformer.py
Symbolic link
1
egs/mls_english/ASR/zipformer/zipformer.py
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
../../../librispeech/ASR/zipformer/zipformer.py
|
Loading…
x
Reference in New Issue
Block a user