From 7c5de8949c2deb8d771a9ee074e14ff07fe11ea8 Mon Sep 17 00:00:00 2001 From: Marc Rasi Date: Fri, 17 Apr 2020 14:03:25 -0700 Subject: [PATCH 1/3] [AutoDiff] differentiation benchmarks --- benchmark/CMakeLists.txt | 1 + benchmark/single-source/Differentiation.swift | 70 +++++++++++++++++++ benchmark/utils/TestsUtils.swift | 2 +- benchmark/utils/main.swift | 2 + 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 benchmark/single-source/Differentiation.swift diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index 0db48967fad2a..08dded4dd11c4 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -80,6 +80,7 @@ set(SWIFT_BENCH_MODULES single-source/DictionaryRemove single-source/DictionarySubscriptDefault single-source/DictionarySwap + single-source/Differentiation single-source/Diffing single-source/DiffingMyers single-source/DropFirst diff --git a/benchmark/single-source/Differentiation.swift b/benchmark/single-source/Differentiation.swift new file mode 100644 index 0000000000000..f4f7da99f3bd4 --- /dev/null +++ b/benchmark/single-source/Differentiation.swift @@ -0,0 +1,70 @@ +//===--- Differentiation.swift -------------------------------------------===// +// +// This source file is part of the Swift.org open source project +// +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors +// Licensed under Apache License v2.0 with Runtime Library Exception +// +// See https://swift.org/LICENSE.txt for license information +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors +// +//===----------------------------------------------------------------------===// + +import TestsUtils + +import _Differentiation + +public let Differentiation = [ + BenchmarkInfo( + name: "DifferentiationIdentity", + runFunction: run_DifferentiationIdentity, + tags: [.regression, .differentiation] + ), + BenchmarkInfo( + name: "DifferentiationSquare", + runFunction: run_DifferentiationSquare, + tags: [.regression, .differentiation] + ), + BenchmarkInfo( + name: "DifferentiationArraySum", + runFunction: run_DifferentiationArraySum, + tags: [.regression, .differentiation], + setUpFunction: { blackHole(onesArray) } + ), +] + +@inline(never) +public func run_DifferentiationIdentity(N: Int) { + func f(_ x: Float) -> Float { + x + } + for _ in 0..<1000*N { + blackHole(valueWithGradient(at: 1, in: f)) + } +} + +@inline(never) +public func run_DifferentiationSquare(N: Int) { + func f(_ x: Float) -> Float { + x * x + } + for _ in 0..<1000*N { + blackHole(valueWithGradient(at: 1, in: f)) + } +} + +let onesArray: [Float] = Array(repeating: 1, count: 50) + +@inline(never) +public func run_DifferentiationArraySum(N: Int) { + func sum(_ array: [Float]) -> Float { + var result: Float = 0 + for i in withoutDerivative(at: 0.. Date: Fri, 17 Apr 2020 16:10:00 -0700 Subject: [PATCH 2/3] #ifdef differentiation benchmarks so they only exist when differentiation is enabled --- benchmark/single-source/Differentiation.swift | 5 ++++- benchmark/utils/main.swift | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/benchmark/single-source/Differentiation.swift b/benchmark/single-source/Differentiation.swift index f4f7da99f3bd4..f15f64348bec6 100644 --- a/benchmark/single-source/Differentiation.swift +++ b/benchmark/single-source/Differentiation.swift @@ -10,8 +10,9 @@ // //===----------------------------------------------------------------------===// -import TestsUtils +#if canImport(_Differentiation) +import TestsUtils import _Differentiation public let Differentiation = [ @@ -68,3 +69,5 @@ public func run_DifferentiationArraySum(N: Int) { blackHole(valueWithGradient(at: onesArray, in: sum)) } } + +#endif diff --git a/benchmark/utils/main.swift b/benchmark/utils/main.swift index f353c0d6374aa..402b6eb60133c 100644 --- a/benchmark/utils/main.swift +++ b/benchmark/utils/main.swift @@ -69,7 +69,9 @@ import DictionaryOfAnyHashableStrings import DictionaryRemove import DictionarySubscriptDefault import DictionarySwap +#if canImport(_Differentiation) import Differentiation +#endif import Diffing import DiffingMyers import DropFirst @@ -259,7 +261,9 @@ registerBenchmark(DictionaryOfAnyHashableStrings) registerBenchmark(DictionaryRemove) registerBenchmark(DictionarySubscriptDefault) registerBenchmark(DictionarySwap) +#if canImport(_Differentiation) registerBenchmark(Differentiation) +#endif registerBenchmark(Diffing) registerBenchmark(DiffingMyers) registerBenchmark(DropFirst) From 3e86dc9396a8177eadeed7d2bc8ebdf6f0dc94ea Mon Sep 17 00:00:00 2001 From: Richard Wei Date: Wed, 2 Dec 2020 03:06:26 -0800 Subject: [PATCH 3/3] Make install name of _Differentiation be @rpath/libswift_Differentiation.dylib. --- stdlib/public/Differentiation/CMakeLists.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/stdlib/public/Differentiation/CMakeLists.txt b/stdlib/public/Differentiation/CMakeLists.txt index c37ae23cf5339..ffdf32aec3dcc 100644 --- a/stdlib/public/Differentiation/CMakeLists.txt +++ b/stdlib/public/Differentiation/CMakeLists.txt @@ -18,6 +18,8 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE ArrayDifferentiation.swift OptionalDifferentiation.swift + "${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c" + GYB_SOURCES FloatingPointDifferentiation.swift.gyb TgmathDerivatives.swift.gyb