Skip to content

Commit 0d0fa92

Browse files
ZhaoChaoqunclaude
andcommitted
Prefer FP32 over INT8 for Streaming Paraformer when available
If user downloads from GitHub (999MB full package), use FP32 version for better accuracy. Fall back to INT8 if only INT8 is available (e.g., from ModelScope's optimized 216MB package). Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8fa3c3e commit 0d0fa92

1 file changed

Lines changed: 24 additions & 7 deletions

File tree

Sources/SherpaOnnxManager.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,37 @@ class SherpaOnnxManager: NSObject {
133133

134134
// MARK: - Streaming Paraformer 模型路径
135135

136-
/// 获取 Streaming Paraformer 模型路径
136+
/// 获取 Streaming Paraformer 模型路径(优先使用 FP32,否则使用 INT8)
137137
func getStreamingParaformerPath() -> (encoderPath: String, decoderPath: String, tokensPath: String)? {
138138
let modelDir = modelsDirectory.appendingPathComponent(ASRModelType.streamingParaformer.folderName)
139-
let encoderPath = modelDir.appendingPathComponent("encoder.int8.onnx")
140-
let decoderPath = modelDir.appendingPathComponent("decoder.int8.onnx")
141139
let tokensPath = modelDir.appendingPathComponent("tokens.txt")
142140

143-
guard FileManager.default.fileExists(atPath: encoderPath.path),
144-
FileManager.default.fileExists(atPath: decoderPath.path),
145-
FileManager.default.fileExists(atPath: tokensPath.path) else {
141+
// 检查 tokens.txt 是否存在
142+
guard FileManager.default.fileExists(atPath: tokensPath.path) else {
146143
return nil
147144
}
148145

149-
return (encoderPath.path, decoderPath.path, tokensPath.path)
146+
// 优先使用 FP32 版本(精度更高)
147+
let encoderFP32 = modelDir.appendingPathComponent("encoder.onnx")
148+
let decoderFP32 = modelDir.appendingPathComponent("decoder.onnx")
149+
150+
if FileManager.default.fileExists(atPath: encoderFP32.path),
151+
FileManager.default.fileExists(atPath: decoderFP32.path) {
152+
print("[SherpaOnnx] 使用 FP32 版本 Streaming Paraformer(精度更高)")
153+
return (encoderFP32.path, decoderFP32.path, tokensPath.path)
154+
}
155+
156+
// 回退到 INT8 版本
157+
let encoderINT8 = modelDir.appendingPathComponent("encoder.int8.onnx")
158+
let decoderINT8 = modelDir.appendingPathComponent("decoder.int8.onnx")
159+
160+
if FileManager.default.fileExists(atPath: encoderINT8.path),
161+
FileManager.default.fileExists(atPath: decoderINT8.path) {
162+
print("[SherpaOnnx] 使用 INT8 版本 Streaming Paraformer")
163+
return (encoderINT8.path, decoderINT8.path, tokensPath.path)
164+
}
165+
166+
return nil
150167
}
151168

152169
/// 检查 Streaming Paraformer 模型是否已下载

0 commit comments

Comments
 (0)