Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Sources/FluidAudio/ASR/Paraformer/ParaformerCif.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ enum ParaformerCif {
/// - encRows: encoder output rows `[T][D]` (real frames only).
/// - alphas: per-frame weights `[T]` from the CifAlphas model.
/// - Returns: acoustic embeddings `[L][D]`.
static func integrateAndFire(encRows: [[Float]], alphas: [Float]) -> [[Float]] {
/// CIF integrate-and-fire that also records the encoder-frame index at which
/// each acoustic token "fires" (the CIF boundary). The returned `fireFrames`
/// is the Swift port of FunASR's `pre_peak_index` and is consumed by the
/// timestamp routine (`ts_prediction_lfr6_standard`) to map tokens to time.
static func integrateAndFireWithFireFrames(
encRows: [[Float]], alphas: [Float]
) -> (embeds: [[Float]], fireFrames: [Int]) {
let threshold = ParaformerConfig.cifThreshold
let tail = ParaformerConfig.cifTailThreshold
let T = encRows.count
let dim = encRows.first?.count ?? ParaformerConfig.encoderDim

var embeds: [[Float]] = []
var fireFrames: [Int] = []
var integrate: Float = 0
var frame = [Float](repeating: 0, count: dim)

Expand All @@ -33,11 +40,17 @@ enum ParaformerCif {
let used = alpha - (integrate - threshold) // portion to reach threshold
for d in 0..<dim { frame[d] += used * hidden[d] }
embeds.append(frame)
fireFrames.append(t)
integrate -= threshold
let leftover = alpha - used
frame = hidden.map { $0 * leftover } // leftover seeds next token
}
}
return embeds
return (embeds, fireFrames)
}

/// Original integrate-and-fire entry point (embeds only).
static func integrateAndFire(encRows: [[Float]], alphas: [Float]) -> [[Float]] {
integrateAndFireWithFireFrames(encRows: encRows, alphas: alphas).embeds
}
}
Loading
Loading