Skip to content

Commit 5ae1dd9

Browse files
r-barnesmeta-codesync[bot]
authored andcommitted
Reproducible floating-point summations
Summary: Introduce ReproducibleFloatingAccumulator. Header-only C++ library for reproducible (order-independent) floating-point summation using binned floating-point arithmetic, adapted from ReproBLAS v2.1.0. Key features of the accumulator: - Reproducible summation independent of summation order - Accuracy at least as good as conventional summation, and tunable via FOLD - Handles overflow, underflow, NaN, and infinity reproducibly - Single read-only pass over summands; minimal memory (2*FOLD floats - usually 6 since FOLD=3 is sufficient for most purposes) Public interface: - Default, copy, and implicit scalar construction - operator+=/-= for scalars and accumulators - Binary operator+/- (including scalar-accumulator mixed expressions) - operator== for bitwise equality - Explicit conversion to native float via operator ftype() and value() - Batch add() via iterator pairs, C++20 ranges, or single values - Manual unsafe_add/renorm path for performance-critical loops - Error bound computation - fmt::formatter specialization (always available via fmt/format.h) - static_assert(FOLD >= 2) to catch invalid instantiation # Example ``` ReproducibleAccumulator<double> rfa; for (const auto& x : kDoubleData) { rfa += x; } rfa.value(); // Same result irrespective of ordering of kDoubleData ``` # Benchmark ``` ============================================================================ [...]/ReproducibleAccumulatorBenchmark.cpp relative time/iter iters/s ============================================================================ SumDouble_OneAtATime_10 50.69ns 19.73M SumDouble_BatchAdd_10 55.744% 90.93ns 11.00M ---------------------------------------------------------------------------- SumDouble_OneAtATime_1000 4.11us 243.32K SumDouble_BatchAdd_1000 132.29% 3.11us 321.88K ---------------------------------------------------------------------------- SumDouble_OneAtATime_1M 4.11ms 243.12 SumDouble_BatchAdd_1M 137.09% 3.00ms 333.30 ---------------------------------------------------------------------------- SumFloat_OneAtATime_1M 4.14ms 241.69 SumFloat_BatchAdd_1M 137.35% 3.01ms 331.96 ---------------------------------------------------------------------------- SumDouble_NaiveBaseline_1M 1.03ms 975.29 SumFloat_NaiveBaseline_1M 1.02ms 975.89 ---------------------------------------------------------------------------- SumDouble_Kahan_1M 13.31ms 75.11 SumFloat_Kahan_1M 13.32ms 75.10 ---------------------------------------------------------------------------- SumDouble_LongDouble_1M 2.39ms 418.41 SumFloat_LongDouble_1M 2.39ms 418.72 ``` Reviewed By: yfeldblum Differential Revision: D97780214 fbshipit-source-id: 07399b5f905f3a021252560ad80df47fe53db21a
1 parent 3b8cab2 commit 5ae1dd9

File tree

7 files changed

+2019
-0
lines changed

7 files changed

+2019
-0
lines changed

folly/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,7 @@ add_subdirectory(io)
14191419
add_subdirectory(json)
14201420
add_subdirectory(lang)
14211421
add_subdirectory(logging)
1422+
add_subdirectory(math)
14221423
add_subdirectory(memory)
14231424
add_subdirectory(net)
14241425
add_subdirectory(observer)

folly/math/BUCK

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load("@fbsource//tools/build_defs/dirsync:fb_dirsync_cpp_library.bzl", "fb_dirsync_cpp_library")
2+
3+
oncall("fbcode_entropy_wardens_folly")
4+
5+
fb_dirsync_cpp_library(
6+
name = "reproducible_accumulator",
7+
headers = ["ReproducibleAccumulator.h"],
8+
use_raw_headers = True,
9+
exported_deps = [
10+
"fbsource//third-party/fmt:fmt",
11+
"//folly:constexpr_math",
12+
],
13+
)

folly/math/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# @generated by folly/facebook/generate_cmake.py
16+
17+
folly_add_library(
18+
NAME reproducible_accumulator
19+
HEADERS
20+
ReproducibleAccumulator.h
21+
EXPORTED_DEPS
22+
folly_constexpr_math
23+
)

0 commit comments

Comments
 (0)