From 068a4054efe60084790752b5f9251468dc5b162a Mon Sep 17 00:00:00 2001 From: zhifu gao Date: Mon, 24 Jun 2024 10:20:53 +0800 Subject: [PATCH] update (#1841) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * v1.0.28 * version checker * version checker * rollback cif_v1 for training bug * fixbug * fixbug for cif * fixbug --------- Co-authored-by: 维石 --- .../bicif_paraformer/finetune.sh | 2 +- .../contextual_paraformer/finetune.sh | 2 +- .../llm_asr/demo_train_or_finetune.sh | 2 +- .../llm_asr/demo_train_or_finetune2.sh | 2 +- .../paraformer/finetune.sh | 2 +- .../paraformer_streaming/finetune.sh | 2 +- .../seaco_paraformer/finetune.sh | 2 +- .../sense_voice/finetune.sh | 2 +- funasr/auto/auto_model.py | 7 +++++ funasr/metrics/wer.py | 4 +-- funasr/models/paraformer/cif_predictor.py | 31 ++++++++++++++----- funasr/utils/version_checker.py | 24 ++++++++++++++ 12 files changed, 65 insertions(+), 17 deletions(-) create mode 100644 funasr/utils/version_checker.py diff --git a/examples/industrial_data_pretraining/bicif_paraformer/finetune.sh b/examples/industrial_data_pretraining/bicif_paraformer/finetune.sh index dc3149a98..37d98f549 100644 --- a/examples/industrial_data_pretraining/bicif_paraformer/finetune.sh +++ b/examples/industrial_data_pretraining/bicif_paraformer/finetune.sh @@ -54,7 +54,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/contextual_paraformer/finetune.sh b/examples/industrial_data_pretraining/contextual_paraformer/finetune.sh index f82a67d93..fe31f2a5d 100644 --- a/examples/industrial_data_pretraining/contextual_paraformer/finetune.sh +++ b/examples/industrial_data_pretraining/contextual_paraformer/finetune.sh @@ -55,7 +55,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune.sh b/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune.sh index 3ab761563..38827625c 100644 --- a/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune.sh +++ b/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune.sh @@ -37,7 +37,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune2.sh b/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune2.sh index 1762f3919..bdfa8e9c2 100644 --- a/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune2.sh +++ b/examples/industrial_data_pretraining/llm_asr/demo_train_or_finetune2.sh @@ -37,7 +37,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/paraformer/finetune.sh b/examples/industrial_data_pretraining/paraformer/finetune.sh index 8d4491af9..24eb101e6 100644 --- a/examples/industrial_data_pretraining/paraformer/finetune.sh +++ b/examples/industrial_data_pretraining/paraformer/finetune.sh @@ -51,7 +51,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/paraformer_streaming/finetune.sh b/examples/industrial_data_pretraining/paraformer_streaming/finetune.sh index 96ac79fe2..c79e63862 100644 --- a/examples/industrial_data_pretraining/paraformer_streaming/finetune.sh +++ b/examples/industrial_data_pretraining/paraformer_streaming/finetune.sh @@ -52,7 +52,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/seaco_paraformer/finetune.sh b/examples/industrial_data_pretraining/seaco_paraformer/finetune.sh index 32fe4ca3e..b07d5137f 100644 --- a/examples/industrial_data_pretraining/seaco_paraformer/finetune.sh +++ b/examples/industrial_data_pretraining/seaco_paraformer/finetune.sh @@ -50,7 +50,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/examples/industrial_data_pretraining/sense_voice/finetune.sh b/examples/industrial_data_pretraining/sense_voice/finetune.sh index 1191657af..240919bfe 100644 --- a/examples/industrial_data_pretraining/sense_voice/finetune.sh +++ b/examples/industrial_data_pretraining/sense_voice/finetune.sh @@ -52,7 +52,7 @@ DISTRIBUTED_ARGS=" --nproc_per_node $gpu_num \ --node_rank ${RANK:-0} \ --master_addr ${MASTER_ADDR:-127.0.0.1} \ - --master_port ${MASTER_PORT: 26669} + --master_port ${MASTER_PORT:-26669} " echo $DISTRIBUTED_ARGS diff --git a/funasr/auto/auto_model.py b/funasr/auto/auto_model.py index 57a15dbf0..01e6aaf6f 100644 --- a/funasr/auto/auto_model.py +++ b/funasr/auto/auto_model.py @@ -111,6 +111,13 @@ class AutoModel: def __init__(self, **kwargs): + try: + from funasr.utils.version_checker import check_for_update + + check_for_update() + except: + pass + log_level = getattr(logging, kwargs.get("log_level", "INFO").upper()) logging.basicConfig(level=log_level) diff --git a/funasr/metrics/wer.py b/funasr/metrics/wer.py index ab07bc29f..6bac30324 100755 --- a/funasr/metrics/wer.py +++ b/funasr/metrics/wer.py @@ -33,8 +33,8 @@ def compute_wer( if cn_postprocess: value = " ".join(value) value = value.replace(" ", "") - if value[0] == "请": - value = value[1:] + # if value[0] == "请": + # value = value[1:] value = [x for x in value] hyp_dict[key] = value with open(ref_file, "r") as ref_reader: diff --git a/funasr/models/paraformer/cif_predictor.py b/funasr/models/paraformer/cif_predictor.py index 0856eede2..535131f39 100644 --- a/funasr/models/paraformer/cif_predictor.py +++ b/funasr/models/paraformer/cif_predictor.py @@ -80,7 +80,7 @@ class CifPredictor(torch.nn.Module): hidden, alphas, token_num, mask=mask ) - acoustic_embeds, cif_peak = cif_v1(hidden, alphas, self.threshold) + acoustic_embeds, cif_peak = cif(hidden, alphas, self.threshold) if target_length is None and self.tail_threshold > 0.0: token_num_int = torch.max(token_num).type(torch.int32).item() @@ -245,7 +245,7 @@ class CifPredictorV2(torch.nn.Module): hidden, alphas, token_num, mask=None ) - acoustic_embeds, cif_peak = cif_v1(hidden, alphas, self.threshold) + acoustic_embeds, cif_peak = cif(hidden, alphas, self.threshold) if target_length is None and self.tail_threshold > 0.0: token_num_int = torch.max(token_num).type(torch.int32).item() acoustic_embeds = acoustic_embeds[:, :token_num_int, :] @@ -506,7 +506,10 @@ def cif_v1_export(hidden, alphas, threshold: float): frames = torch.zeros(batch_size, len_time, hidden_size, dtype=dtype, device=device) fires = torch.zeros(batch_size, len_time, dtype=dtype, device=device) - prefix_sum = torch.cumsum(alphas, dim=1) + # prefix_sum = torch.cumsum(alphas, dim=1) + prefix_sum = torch.cumsum(alphas, dim=1, dtype=torch.float64).to( + torch.float32 + ) # cumsum precision degradation cause wrong result in extreme prefix_sum_floor = torch.floor(prefix_sum) dislocation_prefix_sum = torch.roll(prefix_sum, 1, dims=1) dislocation_prefix_sum_floor = torch.floor(dislocation_prefix_sum) @@ -518,8 +521,8 @@ def cif_v1_export(hidden, alphas, threshold: float): fires[fire_idxs] = 1 fires = fires + prefix_sum - prefix_sum_floor + # prefix_sum_hidden = torch.cumsum(alphas.unsqueeze(-1).tile((1, 1, hidden_size)) * hidden, dim=1) prefix_sum_hidden = torch.cumsum(alphas.unsqueeze(-1).tile((1, 1, hidden_size)) * hidden, dim=1) - frames = prefix_sum_hidden[fire_idxs] shift_frames = torch.roll(frames, 1, dims=0) @@ -530,6 +533,7 @@ def cif_v1_export(hidden, alphas, threshold: float): shift_frames[shift_batch_idxs] = 0 remains = fires - torch.floor(fires) + # remain_frames = remains[fire_idxs].unsqueeze(-1).tile((1, hidden_size)) * hidden[fire_idxs] remain_frames = remains[fire_idxs].unsqueeze(-1).tile((1, hidden_size)) * hidden[fire_idxs] shift_remain_frames = torch.roll(remain_frames, 1, dims=0) @@ -537,8 +541,11 @@ def cif_v1_export(hidden, alphas, threshold: float): frames = frames - shift_frames + shift_remain_frames - remain_frames - max_label_len = batch_len.max() + # max_label_len = batch_len.max() + max_label_len = alphas.sum(dim=-1) + max_label_len = torch.floor(max_label_len).max().to(dtype=torch.int64) + # frame_fires = torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device) frame_fires = torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device) indices = torch.arange(max_label_len, device=device).expand(batch_size, -1) frame_fires_idxs = indices < batch_len.unsqueeze(1) @@ -667,7 +674,10 @@ def cif_wo_hidden_v1(alphas, threshold, return_fire_idxs=False): fires = torch.zeros(batch_size, len_time, dtype=dtype, device=device) - prefix_sum = torch.cumsum(alphas, dim=1) + # prefix_sum = torch.cumsum(alphas, dim=1) + prefix_sum = torch.cumsum(alphas, dim=1, dtype=torch.float64).to( + torch.float32 + ) # cumsum precision degradation cause wrong result in extreme prefix_sum_floor = torch.floor(prefix_sum) dislocation_prefix_sum = torch.roll(prefix_sum, 1, dims=1) dislocation_prefix_sum_floor = torch.floor(dislocation_prefix_sum) @@ -689,6 +699,8 @@ def cif_v1(hidden, alphas, threshold): device = hidden.device dtype = hidden.dtype batch_size, len_time, hidden_size = hidden.size() + # frames = torch.zeros(batch_size, len_time, hidden_size, dtype=dtype, device=device) + # prefix_sum_hidden = torch.cumsum(alphas.unsqueeze(-1).tile((1, 1, hidden_size)) * hidden, dim=1) frames = torch.zeros(batch_size, len_time, hidden_size, dtype=dtype, device=device) prefix_sum_hidden = torch.cumsum(alphas.unsqueeze(-1).tile((1, 1, hidden_size)) * hidden, dim=1) @@ -702,6 +714,7 @@ def cif_v1(hidden, alphas, threshold): shift_frames[shift_batch_idxs] = 0 remains = fires - torch.floor(fires) + # remain_frames = remains[fire_idxs].unsqueeze(-1).tile((1, hidden_size)) * hidden[fire_idxs] remain_frames = remains[fire_idxs].unsqueeze(-1).tile((1, hidden_size)) * hidden[fire_idxs] shift_remain_frames = torch.roll(remain_frames, 1, dims=0) @@ -709,8 +722,12 @@ def cif_v1(hidden, alphas, threshold): frames = frames - shift_frames + shift_remain_frames - remain_frames - max_label_len = batch_len.max() + # max_label_len = batch_len.max() + max_label_len = ( + torch.round(alphas.sum(-1)).int().max() + ) # torch.round to calculate the max length + # frame_fires = torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device) frame_fires = torch.zeros(batch_size, max_label_len, hidden_size, dtype=dtype, device=device) indices = torch.arange(max_label_len, device=device).expand(batch_size, -1) frame_fires_idxs = indices < batch_len.unsqueeze(1) diff --git a/funasr/utils/version_checker.py b/funasr/utils/version_checker.py new file mode 100644 index 000000000..b89af4881 --- /dev/null +++ b/funasr/utils/version_checker.py @@ -0,0 +1,24 @@ +import requests +from packaging import version +from funasr import __version__ # Ensure that __version__ is defined in your package's __init__.py + + +def get_pypi_version(package_name): + url = f"https://pypi.org/pypi/{package_name}/json" + response = requests.get(url) + if response.status_code == 200: + data = response.json() + return version.parse(data["info"]["version"]) + else: + raise Exception("Failed to retrieve version information from PyPI.") + + +def check_for_update(): + current_version = version.parse(__version__) + pypi_version = get_pypi_version("funasr") + + if current_version < pypi_version: + print(f"New version available: {pypi_version}. Your current version is {current_version}.") + print('Please use the command "pip install -U funasr" to upgrade.') + else: + print(f"You are using the latest version of funasr-{current_version}")