Skip to content

[AutoDiff] differentiation benchmarks #31108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 73 additions & 0 deletions benchmark/single-source/Differentiation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//===--- 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
//
//===----------------------------------------------------------------------===//

#if canImport(_Differentiation)

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..<array.count) {
result += array[i]
}
return result
}
for _ in 0..<N {
blackHole(valueWithGradient(at: onesArray, in: sum))
}
}

#endif
2 changes: 1 addition & 1 deletion benchmark/utils/TestsUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum BenchmarkCategory : String {
case runtime, refcount, metadata
// Other general areas of compiled code validation.
case abstraction, safetychecks, exceptions, bridging, concurrency, existential
case exclusivity
case exclusivity, differentiation

// Algorithms are "micro" that test some well-known algorithm in isolation:
// sorting, searching, hashing, fibonaci, crypto, etc.
Expand Down
6 changes: 6 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import DictionaryOfAnyHashableStrings
import DictionaryRemove
import DictionarySubscriptDefault
import DictionarySwap
#if canImport(_Differentiation)
import Differentiation
#endif
import Diffing
import DiffingMyers
import DropFirst
Expand Down Expand Up @@ -251,6 +254,9 @@ registerBenchmark(DictionaryOfAnyHashableStrings)
registerBenchmark(DictionaryRemove)
registerBenchmark(DictionarySubscriptDefault)
registerBenchmark(DictionarySwap)
#if canImport(_Differentiation)
registerBenchmark(Differentiation)
#endif
registerBenchmark(Diffing)
registerBenchmark(DiffingMyers)
registerBenchmark(DropFirst)
Expand Down
1 change: 1 addition & 0 deletions stdlib/public/Differentiation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I think missing this caused my rpath errors as well!

INSTALL_IN_COMPONENT stdlib)