{"record":{"id":"3261d102171172fe","repo":"RVC-Boss/GPT-SoVITS","slug":"search-len-ref-len","errorCode":null,"errorMessage":"搜索音频长度 ({search_len}) 必须大于等于参考音频长度 ({ref_len})","messagePattern":"搜索音频长度 \\((.+?)\\) 必须大于等于参考音频长度 \\((.+?)\\)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"GPT_SoVITS/stream_v2pro.py","lineNumber":193,"sourceCode":"        return refer, sv_emb\n\n    def extract_latent(self, ssl_content):\n        codes = self.vq_model.extract_latent(ssl_content)\n        return codes[0]\n\n    def forward(self, pred_semantic, text_seq, refer, sv_emb=None):\n        return self.vq_model(\n            pred_semantic, text_seq, refer, speed=1.0, sv_emb=sv_emb\n        )[0, 0]\n\n\n@torch.jit.script\ndef find_best_audio_offset_fast(reference_audio: Tensor, search_audio: Tensor):\n    ref_len = len(reference_audio)\n    search_len = len(search_audio)\n\n    if search_len < ref_len:\n        raise ValueError(\n            f\"搜索音频长度 ({search_len}) 必须大于等于参考音频长度 ({ref_len})\"\n        )\n\n    # 使用F.conv1d计算原始互相关\n    reference_flipped = reference_audio.unsqueeze(0).unsqueeze(0)\n    search_padded = search_audio.unsqueeze(0).unsqueeze(0)\n\n    # 计算点积\n    dot_products = F.conv1d(search_padded, reference_flipped).squeeze()\n\n    if len(dot_products.shape) == 0:\n        dot_products = dot_products.unsqueeze(0)\n\n    # 计算参考音频的平方和\n    ref_squared_sum = torch.sum(reference_audio**2)\n\n    # 计算搜索音频每个位置的平方和（滑动窗口）\n    search_squared = search_audio**2","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/RVC-Boss/GPT-SoVITS/blob/d523079fc05d9a8028d6085bffe4a2757c32abb6/GPT_SoVITS/stream_v2pro.py#L175-L211","documentation":"ValueError from a torch.jit.script-ed helper find_best_audio_offset_fast used in the v2Pro streaming path: it computes the best alignment offset of a reference clip inside a longer search window via conv1d cross-correlation, which is only defined when search_audio is at least as long as reference_audio. The guard runs inside the scripted function, so shape violations fail deterministically instead of producing a confusing conv1d kernel-size error.","triggerScenarios":"Calling find_best_audio_offset_fast(reference_audio, search_audio) (directly or via the v2Pro streaming decode that hunts overlap positions) with len(search_audio) < len(reference_audio) — e.g. the SOLA overlap window handed in is shorter than the overlap template taken from the previous chunk.","commonSituations":"Custom chunk sizes/overlap_len that make the tail chunk smaller than the overlap template; final chunk of a stream (is_final) shorter than the reference overlap; callers swapping argument order; sample-rate mismatch making lengths inconsistent.","solutions":["Ensure every search window passed in is >= the reference/overlap length — pad the final chunk with zeros to at least len(reference_audio) before the call.","Skip cross-correlation for tail chunks shorter than the overlap and use them as-is (or truncate the reference to the chunk length).","Double-check argument order and that both tensors are at the same sample rate/dtype.","If you control overlap_len, reduce it so it never exceeds your minimum chunk size."],"exampleFix":"# before\nbest = find_best_audio_offset_fast(ref_chunk, search)  # ValueError when search shorter\n\n# after\nif len(search) < len(ref_chunk):\n    pad = torch.zeros(len(ref_chunk) - len(search), dtype=search.dtype)\n    search = torch.cat([search, pad])\nbest = find_best_audio_offset_fast(ref_chunk, search)","handlingStrategy":"validation","validationCode":"assert search_audio.shape[-1] >= reference_audio.shape[-1], (\n    f\"search {search_audio.shape[-1]} < reference {reference_audio.shape[-1]}\"\n)","typeGuard":"def valid_sola_pair(ref: torch.Tensor, search: torch.Tensor) -> bool:\n    return search.numel() >= ref.numel() and ref.dtype == search.dtype","tryCatchPattern":null,"preventionTips":["Pad final stream chunks with zeros to at least the overlap/reference length.","Choose overlap_len smaller than the minimum chunk size you ever emit.","Keep both tensors at the same sample rate and dtype before cross-correlation."],"tags":["streaming","audio-alignment","shape-validation","v2pro"],"backgroundTag":null,"analyzedSha":"d523079fc05d9a8028d6085bffe4a2757c32abb6","analyzedAt":"2026-08-15T01:06:46.402Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}