Optimize managed FastTree Sumup to native parity on arm64 - #7670
Open
vladimir-aubrecht wants to merge 2 commits into
Open
Optimize managed FastTree Sumup to native parity on arm64#7670vladimir-aubrecht wants to merge 2 commits into
vladimir-aubrecht wants to merge 2 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>
Author
|
@dotnet-policy-service agree company="Microsoft" |
Contributor
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 #