Merge pull request #214 from stephentyrone/rilakkuma

Initial pass at "relaxed" multiply and add operations.
This commit is contained in:
Stephen Canon authored and GitHub committed 2023-04-26 09:14:35 -04:00
commit 93e5499e54
17 files changed
+396 -30

No files matched your search

+3 -1
View File
@@ -53,7 +53,9 @@ let package = Package(
.target(
name: "_NumericsShims",
exclude: excludedFilenames,
linkerSettings: [.linkedLibrary("m", .when(platforms: [.linux, .android]))]
linkerSettings: [
.linkedLibrary("m", .when(platforms: [.linux, .android]))
]
),
.target(
@@ -123,4 +123,17 @@ extension Complex: AlgebraicField {
}
return nil
}
@_transparent
public static func _relaxedAdd(_ a: Self, _ b: Self) -> Self {
Complex(Relaxed.sum(a.x, b.x), Relaxed.sum(a.y, b.y))
}
@_transparent
public static func _relaxedMul(_ a: Self, _ b: Self) -> Self {
Complex(
Relaxed.sum(Relaxed.product(a.x, b.x), -Relaxed.product(a.y, b.y)),
Relaxed.sum(Relaxed.product(a.x, b.y), Relaxed.product(a.y, b.x))
)
}
}
@@ -119,7 +119,7 @@ extension Complex: ElementaryFunctions {
}
// Special cases out of the way, evaluate as discussed above.
return Complex(
RealType._mulAdd(.cos(z.y), .expMinusOne(z.x), .cosMinusOne(z.y)),
Relaxed.multiplyAdd(.cos(z.y), .expMinusOne(z.x), .cosMinusOne(z.y)),
.exp(z.x) * .sin(z.y)
)
}
@@ -300,7 +300,7 @@ extension Complex: ElementaryFunctions {
// We are not trying for sub-ulp accuracy, just a good relative error
// bound, so for our purposes it suffices to have log u dominate the
// result:
if u >= 1 || u >= RealType._mulAdd(u,u,v*v) {
if u >= 1 || u >= Relaxed.multiplyAdd(u, u, v*v) {
let r = v / u
return Complex(.log(u) + .log(onePlus: r*r)/2, θ)
}
+26 -5
View File
@@ -89,6 +89,12 @@ public protocol AlgebraicField: SignedNumeric {
/// }
/// ```
var reciprocal: Self? { get }
/// a + b, with the optimizer licensed to reassociate and form FMAs.
static func _relaxedAdd(_ a: Self, _ b: Self) -> Self
/// a * b, with the optimizer licensed to reassociate and form FMAs.
static func _relaxedMul(_ a: Self, _ b: Self) -> Self
}
extension AlgebraicField {
@@ -99,12 +105,27 @@ extension AlgebraicField {
return result
}
/// Implementations should be *conservative* with the reciprocal property;
/// it is OK to return `nil` even in cases where a reciprocal could be
/// represented. For this reason, a default implementation that simply
/// always returns `nil` is correct, but conforming types should provide
/// a better implementation if possible.
// Implementations should be *conservative* with the reciprocal property;
// it is OK to return `nil` even in cases where a reciprocal could be
// represented. For this reason, a default implementation that simply
// always returns `nil` is correct, but conforming types should provide
// a better implementation if possible.
@_transparent
public var reciprocal: Self? {
return nil
}
// It's always OK to simply fall back on normal arithmetic, and for any
// field with exact arithmetic, this is the correct definition.
@_transparent
public static func _relaxedAdd(_ a: Self, _ b: Self) -> Self {
a + b
}
// It's always OK to simply fall back on normal arithmetic, and for any
// field with exact arithmetic, this is the correct definition.
@_transparent
public static func _relaxedMul(_ a: Self, _ b: Self) -> Self {
a * b
}
}
+2 -1
View File
@@ -17,7 +17,8 @@ add_library(RealModule
Float16+Real.swift
Float80+Real.swift
Real.swift
RealFunctions.swift)
RealFunctions.swift
RelaxedArithmetic.swift)
set_target_properties(RealModule PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
target_link_libraries(RealModule PUBLIC
+7 -2
View File
@@ -221,7 +221,12 @@ extension Double: Real {
#endif
@_transparent
public static func _mulAdd(_ a: Double, _ b: Double, _ c: Double) -> Double {
_numerics_muladd(a, b, c)
public static func _relaxedAdd(_ a: Double, _ b: Double) -> Double {
_numerics_relaxed_add(a, b)
}
@_transparent
public static func _relaxedMul(_ a: Double, _ b: Double) -> Double {
_numerics_relaxed_mul(a, b)
}
}
+7 -2
View File
@@ -194,7 +194,12 @@ extension Float: Real {
#endif
@_transparent
public static func _mulAdd(_ a: Float, _ b: Float, _ c: Float) -> Float {
_numerics_muladdf(a, b, c)
public static func _relaxedAdd(_ a: Float, _ b: Float) -> Float {
_numerics_relaxed_addf(a, b)
}
@_transparent
public static func _relaxedMul(_ a: Float, _ b: Float) -> Float {
_numerics_relaxed_mulf(a, b)
}
}
+14
View File
@@ -172,6 +172,20 @@ extension Float16: Real {
Float16(.logGamma(Float(x)))
}
#endif
// TODO: once clang stabilizes the calling conventions for _Float16 on Intel,
// we can re-enable these; presently the type is disabled on the target.
#if !(arch(i386) || arch(x86_64))
@_transparent
public static func _relaxedAdd(_ a: Float16, _ b: Float16) -> Float16 {
_numerics_relaxed_addf16(a, b)
}
@_transparent
public static func _relaxedMul(_ a: Float16, _ b: Float16) -> Float16 {
_numerics_relaxed_mulf16(a, b)
}
#endif
}
#endif
+10
View File
@@ -165,5 +165,15 @@ extension Float80: Real {
var dontCare: Int32 = 0
return libm_lgammal(x, &dontCare)
}
@_transparent
public static func _relaxedAdd(_ a: Float80, _ b: Float80) -> Float80 {
_numerics_relaxed_addl(a, b)
}
@_transparent
public static func _relaxedMul(_ a: Float80, _ b: Float80) -> Float80 {
_numerics_relaxed_mull(a, b)
}
}
#endif
-5
View File
@@ -76,9 +76,4 @@ public protocol RealFunctions: ElementaryFunctions {
/// See also `gamma()` and `logGamma()`.
static func signGamma(_ x: Self) -> FloatingPointSign
#endif
/// a*b + c, computed _either_ with an FMA or with separate multiply and add.
///
/// Whichever is faster should be chosen by the compiler statically.
static func _mulAdd(_ a: Self, _ b: Self, _ c: Self) -> Self
}
@@ -0,0 +1,82 @@
//===--- RelaxedArithmetic.swift ------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import _NumericsShims
/// A namespace for "relaxed arithmetic" operations for types conforming to
/// `AlgebraicField`.
public enum Relaxed { }
extension Relaxed {
/// a+b with the optimizer licensed to reassociate expressions and form FMAs.
///
/// Floating-point addition is not an associative operation, so the Swift
/// compiler does not have any flexibility in how it evaluates an expression
/// like:
/// ```
/// func sum(array: [Float]) -> Float {
/// array.reduce(0, +)
/// }
/// ```
/// Using `Relaxed.sum` instead of `+` permits the compiler to reorder the
/// terms in the summation, which unlocks loop unrolling and vectorization.
/// In a benchmark, simply using `Relaxed.sum` provides about an 8x speedup
/// for release builds, without any unsafe flags or other optimizations.
/// Further improvement should be possible by improving LLVM optimizations
/// or adding attributes to license more aggressive unrolling and taking
/// advantage of vector ISA extensions for swift.
///
/// If you want to compute `a-b` with relaxed semantics, use
/// `Relaxed.sum(a, -b)`.
///
/// If a type or toolchain does not support reassociation for optimization
/// purposes, this operation decays to a normal addition; it is a license
/// for the compiler to optimize, not a guarantee that any change occurs.
@_transparent
public static func sum<T: AlgebraicField>(_ a: T, _ b: T) -> T {
T._relaxedAdd(a, b)
}
/// a*b with the optimizer licensed to reassociate expressions and form FMAs.
///
/// Floating-point addition and multiplication are not associative operations,
/// so the Swift compiler does not have any flexibility in how it evaluates
/// an expression like:
/// ```
/// func sumOfSquares(array: [Float]) -> Float {
/// array.reduce(0) { $0 + $1*$1 }
/// }
/// ```
/// Using `Relaxed.sum` and `Relaxed.product` instead of `+` and `*` permits
/// the compiler to reorder the terms in the summation, which unlocks loop
/// unrolling and vectorization, and form fused multiply-adds, which allows
/// us to achieve twice the throughput on some hardware.
///
/// If a type or toolchain does not support reassociation for optimization
/// purposes, this operation decays to a normal multiplication; it is a
/// license for the compiler to optimize, not a guarantee that any change
/// occurs.
@_transparent
public static func product<T: AlgebraicField>(_ a: T, _ b: T) -> T {
T._relaxedMul(a, b)
}
}
extension Relaxed {
/// a*b + c, computed _either_ with an FMA or with separate multiply and add,
/// whichever is fastest according to the optimizer's heuristics.
@_transparent
public static func multiplyAdd<T: AlgebraicField>(
_ a: T, _ b: T, _ c: T
) -> T {
T._relaxedAdd(c, T._relaxedMul(a, b))
}
}
+64 -11
View File
@@ -382,18 +382,71 @@ HEADER_SHIM long double libm_lgammal(long double x, int *signp) {
}
#endif
// MARK: - fast mul-add inlines
/// a*b + c evaluated _either_ as two operations or fma, whichever is faster.
HEADER_SHIM float _numerics_muladdf(float a, float b, float c) {
#pragma STDC FP_CONTRACT ON
return a*b + c;
// MARK: - math inlines with relaxed semantics to support optimization.
#define CLANG_RELAX_FP _Pragma("clang fp reassociate(on) contract(fast)")
#if !(__i386__ || __x86_64__)
/// a + b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM _Float16 _numerics_relaxed_addf16(_Float16 a, _Float16 b) {
CLANG_RELAX_FP
return a + b;
}
/// a*b + c evaluated _either_ as two operations or fma, whichever is faster.
HEADER_SHIM double _numerics_muladd(double a, double b, double c) {
#pragma STDC FP_CONTRACT ON
return a*b + c;
/// a * b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM _Float16 _numerics_relaxed_mulf16(_Float16 a, _Float16 b) {
CLANG_RELAX_FP
return a * b;
}
#endif
/// a + b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM float _numerics_relaxed_addf(float a, float b) {
CLANG_RELAX_FP
return a + b;
}
// No long-double muladd operation, because no one has built an FMA for it
// (except for Itanium, which Swift doesn't support).
/// a * b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM float _numerics_relaxed_mulf(float a, float b) {
CLANG_RELAX_FP
return a * b;
}
/// a + b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM double _numerics_relaxed_add(double a, double b) {
CLANG_RELAX_FP
return a + b;
}
/// a * b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM double _numerics_relaxed_mul(double a, double b) {
CLANG_RELAX_FP
return a * b;
}
#if !defined _WIN32 && (defined __i386__ || defined __x86_64__)
/// a + b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM long double _numerics_relaxed_addl(long double a, long double b) {
CLANG_RELAX_FP
return a + b;
}
/// a * b with the "allow reassociation" and "allow FMA formation" flags
/// set in the IR.
HEADER_SHIM long double _numerics_relaxed_mull(long double a, long double b) {
CLANG_RELAX_FP
return a * b;
}
#endif
HEADER_SHIM void _numerics_optimization_barrier(const void *pointer) {
__asm("": :"r" (pointer));
}
#undef CLANG_RELAX_FP
+19
View File
@@ -0,0 +1,19 @@
//===--- BlackHole.swift --------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import _NumericsShims
@_transparent
public func blackHole<T>(_ thing: T) {
withUnsafePointer(to: thing) {
_numerics_optimization_barrier($0)
}
}
+1
View File
@@ -8,6 +8,7 @@ See https://swift.org/LICENSE.txt for license information
#]]
add_library(_TestSupport
BlackHole.swift
DoubleWidth.swift
Error.swift
Interval.swift
+3 -1
View File
@@ -9,8 +9,10 @@ See https://swift.org/LICENSE.txt for license information
add_library(RealTests
ApproximateEqualityTests.swift
AugmentedArithmeticTests.swift
ElementaryFunctionChecks.swift
IntegerExponentTests.swift)
IntegerExponentTests.swift
RelaxedArithmeticTests.swift)
target_compile_options(RealTests PRIVATE
-enable-testing)
target_link_libraries(RealTests PUBLIC
@@ -0,0 +1,131 @@
//===--- RelaxedArithmeticTests.swift -------------------------*- swift -*-===//
//
// This source file is part of the Swift Numerics open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift Numerics project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import XCTest
import RealModule
import _TestSupport
#if canImport(Accelerate)
import Accelerate
#endif
func strictSum<T: Real>(_ array: [T]) -> T {
array.reduce(0, +)
}
func relaxedSum<T: Real>(_ array: [T]) -> T {
array.reduce(0, Relaxed.sum)
}
func strictSumOfSquares<T: Real>(_ array: [T]) -> T {
array.reduce(0) { $0 + $1*$1 }
}
func relaxedSumOfSquares<T: Real>(_ array: [T]) -> T {
array.reduce(0) { Relaxed.multiplyAdd($1, $1, $0) }
}
// TODO: not a great harness, but making it better bumps up against the
// limitations of what XCT measure { } lets us do easily. Good enough for now.
func benchmarkReduction(_ data: [Float], _ reduction: ([Float]) -> Float) {
var accum: Float = 0
#if DEBUG
let iters = 1_000
#else
let iters = 100_000
#endif
for _ in 0 ..< iters {
accum += reduction(data)
}
blackHole(accum)
}
final class RelaxedArithmeticTests: XCTestCase {
var floatData: [Float] = []
override func setUp() {
super.setUp()
floatData = (0 ..< 1024).map { _ in .random(in: .sqrt(1/2) ..< .sqrt(2)) }
}
func testStrictSumPerformance() {
measure { benchmarkReduction(floatData, strictSum) }
}
func testRelaxedSumPerformance() {
// Performance of this should be closer to vDSP.sum than to
// strict sum
measure { benchmarkReduction(floatData, relaxedSum) }
}
#if canImport(Accelerate)
func testvDSPSumPerformance() {
measure { benchmarkReduction(floatData, vDSP.sum) }
}
#endif
func testStrictDotPerformance() {
measure { benchmarkReduction(floatData, strictSumOfSquares) }
}
func testRelaxedDotPerformance() {
// Performance of this should be closer to vDSP.sumOfSquares than to
// strict sumOfSquares
measure { benchmarkReduction(floatData, relaxedSumOfSquares) }
}
#if canImport(Accelerate)
func testvDSPDotPerformance() {
measure { benchmarkReduction(floatData, vDSP.sumOfSquares) }
}
#endif
func testRelaxedArithmetic<T: FixedWidthFloatingPoint & Real>(_ type: T.Type) {
// Relaxed add is still an add; it's just permitted to reorder relative
// to other adds or form FMAs. So if we do one in isolation, it has to
// produce the same result as a normal addition.
let a = T.random(in: -1 ... 1)
let b = T.random(in: -1 ... 1)
XCTAssertEqual(a + b, Relaxed.sum(a, b))
// Same is true for mul.
XCTAssertEqual(a * b, Relaxed.product(a, b))
// add + mul must be either two operations or an FMA:
let unfused = a + 1.5 * b
let fused = a.addingProduct(1.5, b)
let relaxed = Relaxed.multiplyAdd(1.5, b, a)
XCTAssert(relaxed == unfused || relaxed == fused)
// Summing all values in an array can be associated however we want, but
// has to satisfy the usual error bound of 0.5 * sum.ulp * numberOfElements.
// We don't have a golden reference, but we can compare two sums with twice
// the bound for a sanity check.
let array = (0 ..< 128).map { _ in T.random(in: 1 ..< 2) }
var ref = strictSum(array)
var tst = relaxedSum(array)
var bound = max(ref, tst).ulp * T(array.count)
XCTAssertLessThanOrEqual(abs(ref - tst), bound)
// Similarly for sum of squares ...
ref = strictSumOfSquares(array)
tst = relaxedSumOfSquares(array)
bound = 2 * max(ref, tst).ulp * T(array.count)
XCTAssertLessThanOrEqual(abs(ref - tst), bound)
}
func testRelaxedArithmetic() {
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64))
testRelaxedArithmetic(Float16.self)
#endif
testRelaxedArithmetic(Float.self)
testRelaxedArithmetic(Double.self)
#if (arch(i386) || arch(x86_64)) && !os(Windows) && !os(Android)
testRelaxedArithmetic(Float80.self)
#endif
}
}
+12
View File
@@ -67,6 +67,18 @@ extension IntegerExponentTests {
}
#endif
extension RealTests.AugmentedArithmeticTests {
static var all = testCase([
("testTwoSum", RealTests.AugmentedArithmeticTests.testTwoSum),
])
}
extension RealTests.RelaxedArithmeticTests {
static var all = testCase([
("testRelaxedArithmetic", RealTests.RelaxedArithmeticTests.testRelaxedArithmetic),
])
}
extension ArithmeticTests {
static var all = testCase([
("testPolar", ArithmeticTests.testPolar),