Skip to content

Fix native D.UPM when exactly one degree is an integer - #200

Open
gitRasheed wants to merge 1 commit into
mainfrom
rasheed/d-upm-mixed-degrees
Open

gitRasheed wants to merge 1 commit into
mainfrom
rasheed/d-upm-mixed-degrees

Conversation

@gitRasheed

Copy link
Copy Markdown
Collaborator

With the native extension, d_upm is wrong whenever one degree is an integer and the other isn't. The 2.3.0 wheel on PyPI has it:

x = [0.1, -0.4, 0.8, -1.2, 0.3, 0.9, -0.7, 1.5, -0.2, 0.6]
y = [1.1, -0.3, 0.5, 0.2, -1.4, 0.7, 1.9, -0.8, 0.4, -0.6]
nns.d_upm(1, 1.5, x, y, 0, 0)  # 0.165, R 14.0 gives 0.19912061895106833
nns.d_upm(1.5, 1, x, y, 0, 0)  # 0.165, R gives 0.14114417505327964
nns.d_upm(2, 0.5, x, y, 0, 0)  # 0.213, R gives 0.13447041876537072

The pure-Python path (NNS_DISABLE_NATIVE=1) and d_lpm are correct.

The cause is in the vendored NNS-core at a0d3b93. Its d_upm has the two mixed-degree branches swapped, so the integer fast path runs on the wrong side and the fractional degree gets truncated to an integer. NNS-core #39 fixes it, but that PR isn't merged, so the fix isn't vendored here. The parity grid only used (3, 1), (0.5, 1.5) and (2, 0), and none has exactly one integer degree.

I added a hashed overlay, like the mreg_predict one. src/nns/_r14_partial_moments.cpp is the vendored file with only those two branches changed to match R's DUPM_C. CMake builds it in place of the vendor file, and I registered it in sync/nns_source.json. extern/NNS-core is unchanged.

The co-moment parity test now covers (1, 1.5), (1.5, 1), (2, 0.5), (0.5, 1) and (0, 0.5), for both native and fallback. It adds five cache entries I generated from the pinned NNS 14.0 archive under R 4.5.0. Before the fix, the five native cases fail on dupm. After it, they all pass.

The vendored NNS-core d_upm swaps its two mixed-degree branches: with one
integer and one fractional degree it applies repeated multiplication to
the wrong side and drops the fractional power, so D.UPM(1, 1.5, ...)
returned the D.UPM(1, 1, ...) value. Every public d_upm call goes through
d_upm_v, which uses that kernel.

Add src/nns/_r14_partial_moments.cpp, the vendored partial_moments.cpp
with those two branches matched to R's DUPM_C, swap it in for the vendor
translation unit in CMake, and register it with its hashes in
sync/nns_source.json. extern/NNS-core is unchanged.

The co-moment parity grid gains (1, 1.5), (1.5, 1), (2, 0.5), (0.5, 1)
and (0, 0.5), with five new pm14.co_moments cache entries generated by
the pinned NNS 14.0 archive under R 4.5.0, and checks the scalar native
d_lpm/d_upm kernels too.
@gitRasheed

Copy link
Copy Markdown
Collaborator Author

The failing checks here aren't from this change. The Ubuntu 26.04 runner's GCC enables fused multiply-add, which moves about 28 bitwise tests by 1 to 4 ulp on main too (#201 fails the same ones). #202 builds the native kernels with -ffp-contract=off and is green on all four Python versions. I'll rebase this onto main once #202 is in.

This branch has not been deployed

No deployments
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.

1 participant