Skip to content

fix(mps): make bisection_timestamp_search return the truly closest index - #354

Closed
Ace3Z wants to merge 2 commits into
facebookresearch:mainfrom
Ace3Z:fix/bisection-timestamp-closest-neighbor
Closed

fix(mps): make bisection_timestamp_search return the truly closest index#354
Ace3Z wants to merge 2 commits into
facebookresearch:mainfrom
Ace3Z:fix/bisection-timestamp-closest-neighbor

Conversation

@Ace3Z

@Ace3Z Ace3Z commented May 24, 2026

Copy link
Copy Markdown
Contributor

Closes #100.

bisection_timestamp_search was supposed to return the closest timestamp but it returned whichever side the binary search happened to land on. With [1000, 3000, ..., 19000] and query 14900, it returned index 6 (distance 1900) instead of 7 (distance 100). All four get_nearest_* wrappers advertise "closest or equal" in their docstrings, so they were silently returning the wrong neighbor for any query that fell between samples.

Credit to @georgegu1997 for the analysis in the issue thread.

Structured as two commits in case the second one feels out of scope.

Commit 1 keeps the existing border guards and binary search loop untouched. After the loop converges, the new code compares the three candidate indices (start - 1, start, start + 1) by absolute distance and returns the closest one. Ties break toward the lower index.

Commit 2 is a separate but related bug that fell out of the same review. The original border guards used <= and >=, so querying the exact first or last timestamp returned None instead of index 0 or len-1. Tightened to < and >. Happy to drop this commit if you'd rather see it as its own PR.

Added 3 unittests in core/python/test/mpsUtilsTest.py (same naming and style as mpsPyBindTest.py), wired into build-and-test.yml. On main all three fail (the #100 regression, the tie-break, and the endpoint case). On commit 1 alone the endpoint case still fails. On the full PR they all pass. The tests use SimpleNamespace to stub the tracking_timestamp attribute so they run without pip install .; CI still does the full install so the real import path is exercised.

All four internal callsites of bisection_timestamp_search just do data[bisection_index] after a None check; none depend on the old "always less or equal" behavior.

@meta-cla meta-cla Bot added the cla signed label May 24, 2026
@Ace3Z
Ace3Z force-pushed the fix/bisection-timestamp-closest-neighbor branch 2 times, most recently from 6bbc782 to 0805945 Compare May 25, 2026 08:51
Ace3Z added 2 commits May 25, 2026 12:20
The binary search converged to whichever side it happened to land on,
not the index with the smallest absolute distance to the query. For
example with timestamps [1000, 3000, ..., 19000] and query 14900 it
returned index 6 (distance 1900) instead of 7 (distance 100). The
four get_nearest_* wrappers all advertise "closest or equal" in their
docstrings, so they were silently returning the wrong neighbor for any
query that fell between samples.

After the existing loop, compare the converged index with its
neighbors (start - 1, start, start + 1) and return whichever has the
smallest absolute distance; ties resolve to the lower index.

Closes facebookresearch#100.
Credit @georgegu1997 for the analysis in the issue thread.
… timestamp exactly

The border guards used <= and >=, so querying the exact first or last
timestamp returned None instead of index 0 or len - 1, violating the
"closest or equal" wording in every get_nearest_* wrapper docstring.
Tightened to < and >.

Separable from the issue facebookresearch#100 fix; can be dropped if you'd rather see
it as its own PR.
@Ace3Z
Ace3Z force-pushed the fix/bisection-timestamp-closest-neighbor branch from 0805945 to 1d7e6b3 Compare May 25, 2026 10:20
@Ace3Z

Ace3Z commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping @SeaOtocinclus. Following up on #100 where you suggested opening a PR for integration. This implements the fix with unit tests wired into CI. Happy to adjust anything if needed.

@Ace3Z

Ace3Z commented May 29, 2026

Copy link
Copy Markdown
Contributor Author

Friendly ping. @YLouWashU, would you have a moment to take a look? Small one line fix to make bisection_timestamp_search actually return the closest index. Happy to address any feedback.

@meta-codesync

meta-codesync Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

@SeaOtocinclus has imported this pull request. If you are a Meta employee, you can view this in D107104515.

@meta-codesync

meta-codesync Bot commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

@SeaOtocinclus merged this pull request in 9b4e915.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bisection_timestamp_search returns the index closest and SMALLER than the query

1 participant