Optimize managed FastTree Sumup to native parity on arm64 - #7670
Optimize managed FastTree Sumup to native parity on arm64#7670vladimir-aubrecht wants to merge 3 commits into
Conversation
The FastTree histogram build (Sumup) uses a native SSE-free C++ library on x64/x86, but falls back to a generic managed path on arm64 (and any platform where the native library is unavailable). That fallback goes through the IIntArrayForwardIndexer interface with per-element bounds checks, making it ~1.8x slower than native and allocating per call. This adds optimized managed Sumup implementations that mirror the native templates (Sumup.h / SumupNibbles.h / SumupSegment.h) exactly, using fixed pointers and no bounds checks: - DenseIntArray: new SumupManagedDense covering 4/8/16/32-bit, weighted and unweighted, root (no doc indices) and leaf cases. Dense8/4/16/32 now dispatch the managed handler to it instead of the slow base.Sumup fallback. - SegmentIntArray: new SumupManaged mirroring SumupSegment / SumupSegment_noindices for the compressed segment format. Native remains the default on x64/x86 (UseFastTreeNative unchanged); only the managed fallback path is replaced, so arm64 picks up the fast path automatically. Because the loops iterate in the same order as native, the float accumulation is bit-identical and existing baselines are unchanged. Measured on Apple M5 (arm64): the new managed path reaches ~0.96x native throughput (parity), versus ~1.79x slower for the old fallback, with zero managed allocations per call (down from 20 B). Histogram outputs are bit-identical to the old path, and FastTree/FastForest baseline tests pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
@dotnet-policy-service agree company="Microsoft" |
There was a problem hiding this comment.
Pull request overview
This PR improves FastTree training performance on arm64 (and other platforms without the native FastTree library) by replacing the slow managed IntArray.Sumup fallback with optimized, bounds-check-free managed implementations that mirror the native scalar Sumup templates to preserve bit-identical histogram accumulation.
Changes:
- Added an optimized managed dense
Sumupimplementation (SumupManagedDense) and routed Dense 4/8/16/32-bit arrays to use it when native is unavailable. - Added an optimized managed segment
Sumupimplementation (SegmentIntArray.SumupManaged) mirroring native segment decoding for both sequential (root) and indexed (leaf) cases. - Updated handler selection so managed fallbacks use the new optimized implementations instead of
base.Sumup.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/Microsoft.ML.FastTree/Dataset/SegmentIntArray.cs | Switches managed fallback to a new pointer-based segment Sumup implementation matching native decoding/accumulation order. |
| src/Microsoft.ML.FastTree/Dataset/DenseIntArray.cs | Introduces pointer-based dense Sumup implementation and wires Dense 4/8/16/32-bit arrays to use it on non-native platforms. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…Managed The public Sumup override already wraps SumupHandler in Timer.Time(TimerEvent.SumupSegment), so timing the managed handler again double-counts. Timing is now done only by Sumup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Would it make sense to add these as a tests? I'm wondering if we have much coverage over the new implementation... I think ideally we should run both native and the new manage implementation on CI side by side to make sure they are ± equal. |
Addresses review feedback requesting coverage of the new managed Sumup implementations. Adds FastTreeSumupParityTests covering Dense 4/8/16/32-bit and Segment arrays, root and leaf cases, with and without weights: - ManagedSumupMatchesReference: managed histogram vs an independent brute-force reference (runs on all platforms, incl. arm64). - ManagedSumupMatchesNative: managed vs native, bit-identical, gated on the FastTreeNative library so native and managed run side by side on x64 CI. Makes the Dense SumupManaged handlers internal so tests can invoke them directly (SumupNative left unchanged to avoid an unrelated visibility change). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Good idea — added
To let the tests call the managed path directly, the Dense Note: the current red CI is an unrelated flaky |
Problem
FastTree builds feature histograms via
Sumup, the per-iteration hot loop of treetraining. On x64/x86 this uses the native FastTree library; on arm64 (and any
platform where the native library isn't available) it falls back to the generic
managed
IntArray.Sumup, which goes through theIIntArrayForwardIndexerinterfacewith per-element bounds checks. That fallback is ~1.8x slower than native and
allocates on every call.
Note: the native
Sumup(Sumup.h) is itself a plain scalar loop — the only realSIMD in FastTreeNative is in
segment.cpp(one-time segment compression), not in theper-iteration histogram path. So there is no algorithmic reason managed can't match it.
Change
Add optimized managed Sumup implementations that mirror the native templates
(
Sumup.h/SumupNibbles.h/SumupSegment.h) exactly, usingfixedpointers andno bounds checks:
SumupManagedDensecovering 4/8/16/32-bit × weighted/unweighted× root (no doc indices)/leaf.
Dense8/4/16/32BitIntArraynow point their managedhandler at it instead of the slow
base.Sumup.SumupManagedmirroringSumupSegment/SumupSegment_noindicesfor the compressed segment format.Native remains the default on x64/x86 (
UseFastTreeNativeis unchanged) — only themanaged fallback path is replaced, so arm64 picks up the fast path automatically.
Because the loops iterate in the same order as native, float accumulation is
bit-identical and existing baselines are unchanged.
Results (Apple M5, arm64)
Real in-repo types (
FeatureHistogram.SumupWeighted), Dense8, weighted, 256 bins:Native comparison (native
Sumup.hscalar loop built for arm64 vs equivalent managed,N=20M): native 1337 Melem/s, old fallback 746 Melem/s (1.79x slower), new managed
1386 Melem/s (0.96x = parity). Managed allocations per call: 20 B -> 0 B.
Testing
Microsoft.ML.Predictor.Testspass onarm64 (7 passed, 0 failed; the managed path is what runs there, compared against
native-generated baselines -> confirms numerical identity).
(2000 random trials, sequential + indexed) and the 4-bit nibble formula
(5000 trials) — 0 mismatches.
Notes for reviewers
UseFastTreeNativeis intentionally left unchanged. If we later want to drop thenative library entirely, this managed path is now fast enough to be the default on
all architectures — but that is a separate decision.
Fixes #