Skip to content

Consolidate scalar sigmoid onto a shared overflow-safe helper - #317

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

stikves merged 4 commits into
apple:mainfrom
stikves:sukru/ws4-shared-sigmoid

Conversation

@stikves

@stikves stikves commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Both DetectionDecoder (CoreAIVideoSegmenter) and SegmentationPostprocessor (CoreAIImageSegmenter) defined their own scalar sigmoid(_:). The video decoder branches on the sign of x so the exponent argument stays non-positive (overflow-safe); the segmenter's naive 1 / (1 + exp(-x)) underflows large-negative logits to exactly 0. This folds both onto one shared sigmoid in CoreAIShared/Runtime/VectorMath.swift (the sign-branched form) and routes both subsystems and their tests through it.

Behavior change: the segmenter now returns a tiny positive value instead of 0 for large-negative logits, well below any threshold it applies.

Adds VectorMathTests for both extremes; the existing segmenter and tracker-score suites were migrated to the shared function and pass.

Part of the shared-utility cleanup.

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.
stikves and others added 2 commits October 2, 2026 11:38
- Condense the `sigmoid` doc comment and mark it `@inline(__always)` (it
  runs per-pixel on the detector/segmenter paths).
- Delete the `sigmoidKnownValues` (image-segmenter) and `sigmoidStability`
  (video-segmenter) tests: `VectorMathTests` already covers both the known
  values and the overflow-safe extremes.

@kevchengcodes kevchengcodes 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.

Looks like you addressed the comments in #320 here. Thanks for doing that, I'll leave it up to you to determine PR merge strategy

@stikves
stikves merged commit 1703448 into apple:main Oct 6, 2026
3 checks passed
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.

2 participants