Skip to content

Consolidate the cosineSimilarity parity helpers - #320

Merged
stikves merged 6 commits into
apple:mainfrom
stikves:sukru/ws4-shared-cosine
Oct 9, 2026
Merged

stikves merged 6 commits into
apple:mainfrom
stikves:sukru/ws4-shared-cosine

Conversation

@stikves

@stikves stikves commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

The image-segmenter, diffusion-runner, and speech-recognizer parity harnesses each defined their own cosineSimilarity. This adds a shared cosineSimilarity to CoreAIShared/Runtime/VectorMath.swift (Double accumulation) and routes all three through it.

One zero-norm convention: empty, length-mismatched, and zero-norm inputs return 0. This changes the speech-recognizer harness's both-norms-zero case from 1 to 0; no parity fixture passes a zero-vector reference. The two Float-returning call sites wrap the result in Float(...) to keep their printed format.

Adds cosineSimilarity cases to VectorMathTests.

Part of the shared-utility cleanup. Stacked on #317 (shared sigmoid, same file): this PR's diff includes that change until #317 lands, then narrows to just cosineSimilarity.

DetectionDecoder and SegmentationPostprocessor each defined their own
scalar `sigmoid(_:)`. The video decoder's branches on the sign so the
exponent argument stays non-positive (overflow-safe); the segmenter's
naive `1/(1+exp(-x))` underflows large-negative logits to exactly 0.

Add one shared `sigmoid` in CoreAIShared/Runtime/VectorMath.swift (the
sign-branched form) and route both subsystems and their tests through it,
deleting the two duplicates. VectorMathTests characterizes both extremes.

Intended behavior change: the segmenter path now returns a tiny positive
value instead of exactly 0 for large-negative logits — negligible for any
threshold > 0.

WS4 PR2.
The image-segmenter, diffusion-runner, and speech-recognizer parity
harnesses each defined their own `cosineSimilarity`. Add a shared one to
CoreAIShared/Runtime/VectorMath.swift (Double accumulation) and route all
three through it, deleting the copies.

One zero-norm convention: empty, length-mismatched, and zero-norm inputs
return 0. This changes the speech-recognizer harness's both-norms-zero
case from 1 to 0; no parity fixture passes a zero-vector reference. The
two `Float`-returning call sites wrap the result in `Float(...)` to keep
their printed format.

Adds cosineSimilarity cases to VectorMathTests.
Comment thread swift/Sources/CoreAIShared/Runtime/VectorMath.swift Outdated
Comment thread swift/Tests/ImageSegmenterTests/ImageSegmentationTests.swift Outdated
Comment thread swift/Tests/VideoSegmenterTests/TrackingPrimitiveTests.swift Outdated
Comment thread swift/Sources/CoreAIShared/Runtime/VectorMath.swift Outdated
Comment thread swift/Sources/CoreAIVideoSegmenter/Tracking/DetectionDecoder.swift Outdated
Accumulate in Double, return Float. The image-segmenter and diffusion
parity callers drop their `Float(...)` wrap; the speech parity harness
casts to Double for its Double-based reporting.
Comment thread swift/Sources/CoreAIShared/Runtime/VectorMath.swift Outdated

@carinapeng carinapeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for a scoped change, please take a look at whether the exact match on all-zero tensors is consistent with the speech parity check

stikves and others added 2 commits October 9, 2026 10:26
Two zero-norm (or empty) vectors are identical and now score 1; a single
zero-norm vector still scores 0; a length mismatch scores 0. This restores the
behavior the speech parity harness had before the helper was consolidated
(both-zero -> 1), so every harness sharing cosineSimilarity agrees on the
all-zero case. Addresses review feedback.
@stikves
stikves merged commit bebda58 into apple:main Oct 9, 2026
3 checks passed
@stikves
stikves deleted the sukru/ws4-shared-cosine branch October 9, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants