From a7716c03978f66feed0350388f071354d4de0fc4 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 b62a7b2937bf1..3fabaeb5b8137 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 b6855472cbbdf..dd1643fc1710a 100644 --- a/benchmark/utils/main.swift +++ b/benchmark/utils/main.swift @@ -68,7 +68,9 @@ import DictionaryOfAnyHashableStrings import DictionaryRemove import DictionarySubscriptDefault import DictionarySwap +#if canImport(_Differentiation) import Differentiation +#endif import Diffing import DiffingMyers import DropFirst @@ -252,7 +254,9 @@ registerBenchmark(DictionaryOfAnyHashableStrings) registerBenchmark(DictionaryRemove) registerBenchmark(DictionarySubscriptDefault) registerBenchmark(DictionarySwap) +#if canImport(_Differentiation) registerBenchmark(Differentiation) +#endif registerBenchmark(Diffing) registerBenchmark(DiffingMyers) registerBenchmark(DropFirst) From f199feafe7680414abe8ad6aab45b131d1d3b859 Mon Sep 17 00:00:00 2001 From: Marc Rasi Date: Mon, 20 Apr 2020 17:31:14 -0700 Subject: [PATCH 3/3] fix _Differentiation rpath --- stdlib/public/Differentiation/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/public/Differentiation/CMakeLists.txt b/stdlib/public/Differentiation/CMakeLists.txt index 4c5419a398b80..5613c4b298d39 100644 --- a/stdlib/public/Differentiation/CMakeLists.txt +++ b/stdlib/public/Differentiation/CMakeLists.txt @@ -36,4 +36,5 @@ add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPE ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS} -parse-stdlib LINK_FLAGS "${SWIFT_RUNTIME_SWIFT_LINK_FLAGS}" + DARWIN_INSTALL_NAME_DIR "@rpath" INSTALL_IN_COMPONENT stdlib)