fix(ompi): correct integer Avg scaling in AllReduce and ReduceScatter#37
Open
GordonYang1 wants to merge 1 commit into
Open
fix(ompi): correct integer Avg scaling in AllReduce and ReduceScatter#37GordonYang1 wants to merge 1 commit into
Avg scaling in AllReduce and ReduceScatter#37GordonYang1 wants to merge 1 commit into
Conversation
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.
Summary
This PR fixes an integer
Avg(average) calculation error in the OpenMPI implementations of the reduce-family collectivesAllReduceandReduceScatter.The host-side averaging step scaled each element with
typed_buf[i] *= static_cast<T>(scale), wherescale = 1 / world_size. For anyworld_size > 1this reciprocal is a fraction in(0, 1); when the element typeTis an integer,static_cast<T>(scale)truncates it to0, so the entireAvgresult is zeroed out. The fix performs the scaling in floating point (throughdouble) and then casts back toTfor integer types, while leaving the floating-point path unchanged. This mirrors the integerAvgfix already applied toReducein #28, restoring consistency across the reduce family.Changes
Reduce-family
Avgcorrectness fixsrc/ompi/impl/all_reduce.h: guard the host-sideAvgscaling withif constexpr (std::is_integral_v<T>); for integer types scale throughdouble(static_cast<T>(static_cast<double>(typed_buf[i]) * scale)) before casting back toT, and keep the existing in-place multiply for floating-point types.src/ompi/impl/reduce_scatter.h: apply the identical integer-safeAvgscaling fix.Reduceimplementation fixed in feat: supportReducewith OpenMPI backend implementation #28, so all three reduce-family collectives now share the same correct averaging behavior.Includes
#include <type_traits>to both files forstd::is_integral_v.Platform and Backend Affected
Platform
Backend
Performance Impact
The averaging loop still runs once over the output buffer exactly as before; for integer types each element is now computed through
double, a negligible host-side per-element cost, and the floating-point path is byte-for-byte unchanged. For reference, the heterogeneous run (8 ranks, 4 MB per rank,Float32 + Sum) measuredAllReduceat 12.352 ms (0.55 GB/s bus BW),Reduceat 5.640 ms (1.21 GB/s bus BW), andReduceScatterat 77.529 ms (4 MB recv / 32 MB send per rank).Known Issues & Future Work
static_castto convert the floating-point scale result back toT. A unified host-sideCast(the existingTODO(lzm)) would be needed to support CPU custom types cleanly; this remains shared across the reduce family.100 / 16 → 6), consistent with the behavior already shipped inReduce. NCCL-exact rounding is not attempted.kFloat16/kBFloat16map toMPI_BYTE, so reducing them as raw bytes is incorrect. This is a pre-existing, codebase-wide limitation shared by all reduce-family collectives, pending a unifiedCast/ typed-reduction path.Test Results
Validated on a MetaX–NVIDIA heterogeneous cluster over the OpenMPI backend via
scripts/run_examples.py:server: NVIDIA, 4 GPUs, ranks 0–3 (built with Devices[cpu, nvidia], Backends[ompi]).test: MetaX, 4 GPUs, ranks 4–7 (built with Devices[cpu, metax], Backends[ompi]).float32(4 MB) per rank (ReduceScatter: 4 MB recv / 32 MB send per rank); 2 warm-up + 20 profiled iterations.Correct: YES.Note: the bundled examples all run
Float32 + Sum, which does not exercise the integerAvgpath that this PR fixes. The fix was therefore additionally verified with a dedicatedint32/int64+Avgcheck driving the realinfinicclAllReduce/infinicclReduceScatter: before the fix both ops returned0(the entire result zeroed), after the fix both return the correct average. The full example regression above confirms the unaffectedFloat32 + Sumpath is not broken.Test Involved Platform
Test Involved Backend
all_gather.log
all_reduce.log
all_to_all.log
broadcast.log
gather.log
reduce.log
reduce_scatter.log
scatter.log
send_recv.log
Checklist
Title, Branch, and Commits
feat: …,fix(nccl): …).<type>/xxx-yyyy-zzzzwhere<type>matches the PR title's Conventional Commits type and words are joined with hyphens (seeCONTRIBUTING.md§Branches).CONTRIBUTING.md§Pull Requests).master— the branch is rebased cleanly on top of the currentmaster.fixup!/squash!/wipcommits remain.Scope and Design
CONTRIBUTING.md§Code/General).printf/std::cout/print(...)left behind, orTODOwithout an owner and issue link.General Code Hygiene
CONTRIBUTING.md§Code/General).CONTRIBUTING.md§Code/General).the `AllReduce` implementation) (CONTRIBUTING.md§Code/General).CONTRIBUTING.md§Code/General).CONTRIBUTING.md§Code/General; §Python).C++ Specific (if C++ files changed)
clang-format(version 16, per.github/workflows/clang-format.yml) has been run against all modified applicable files; the diff is clean.assertwith messages that include at least__FILE__,__LINE__, and__func__(CONTRIBUTING.md§C++).CONTRIBUTING.md§C++).CONTRIBUTING.md§C++).CONTRIBUTING.md§C++).CONTRIBUTING.md§C++).CONTRIBUTING.md§C++).Python Specific (if Python files changed)
ruff checkpasses cleanly on CI (see.github/workflows/ruff.yml).ruff format --checkpasses cleanly — if not, runruff formatand commit the result.CONTRIBUTING.md§Python).pytest.skipmessages without terminal period) are honored where applicable (CONTRIBUTING.md§Python).CONTRIBUTING.md§Python).if,for, and similar control-flow statements (CONTRIBUTING.md§Python).return, except when it directly follows a control-flow statement (CONTRIBUTING.md§Python).CONTRIBUTING.md§Python).Testing
Build, CI, and Tooling
CMakeLists.txtunderif(AUTO_DETECT_DEVICES)or toif(AUTO_DETECT_BACKENDS)if applicable.clang-format.yml,ruff.yml) are green locally (or expected to be green on CI).Documentation
README.md,CONTRIBUTING.md, or inline docs updated when behavior, build flags, or developer workflow changed.!orBREAKING CHANGE:footer.Security and Safety