From 491b39b23f795a8dc52d6c073cb1c4494ac23352 Mon Sep 17 00:00:00 2001 From: ViperEkura <3081035982@qq.com> Date: Tue, 7 Oct 2025 12:11:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(utils):=20=E4=BC=98=E5=8C=96=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E6=89=93=E5=8C=85=E9=80=BB=E8=BE=91=E4=BB=A5=E6=8F=90?= =?UTF-8?q?=E9=AB=98=E6=95=88=E7=8E=87=E5=92=8C=E6=AD=A3=E7=A1=AE=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/utils.py | 41 ++++++++++++++--------------------------- 1 file changed, 14 insertions(+), 27 deletions(-) diff --git a/modules/utils.py b/modules/utils.py index 9e52c3c..0588b9a 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -39,39 +39,26 @@ def comprehensive_normalization(text): def pack_sequences(sequences: List[Tensor], pack_size: int, pad_value: int) -> List[Tensor]: packages = [] sequences.sort(key=lambda x: x.numel(), reverse=True) - current_pack = torch.tensor([], dtype=torch.int32) + current_pack = torch.full((pack_size,), pad_value, dtype=torch.int32) + current_pos = 0 for tensor in sequences: if tensor.numel() > pack_size: - packages.append(tensor[:pack_size]) - continue - - remaining = pack_size - current_pack.numel() + tensor = tensor[:pack_size] - if remaining == 0: + tensor_size = tensor.numel() + + if current_pos + tensor_size > pack_size: packages.append(current_pack) - current_pack = tensor - elif tensor.numel() <= remaining: - current_pack = torch.cat([current_pack, tensor]) - else: - padding = torch.full((remaining,), pad_value, dtype=torch.int32) - current_pack = torch.cat([current_pack, padding]) - packages.append(current_pack) - current_pack = tensor - - if current_pack.numel() > 0: - if current_pack.numel() < pack_size: - padding = torch.full( - (pack_size - current_pack.numel(),), - pad_value, - dtype=torch.int32 - ) - current_pack = torch.cat([current_pack, padding]) - else: - current_pack = current_pack[:pack_size] - + current_pack = torch.full((pack_size,), pad_value, dtype=torch.int32) + current_pos = 0 + + current_pack[current_pos: current_pos + tensor_size] = tensor + current_pos += tensor_size + + if current_pos > 0: packages.append(current_pack) - + return packages