Skip to content
This repository was archived by the owner on Jan 10, 2023. It is now read-only.

Commit f175c1d

Browse files
author
Dave Abrahams
committed
Merge branch 'main' of github.com:apple/swift into tensorflow-stage
* 'main' of github.com:apple/swift: [AutoDiff] Add differentiation benchmarks (swiftlang#34901)
2 parents ecfb7a0 + b8581cb commit f175c1d

File tree

5 files changed

+82
-1
lines changed

5 files changed

+82
-1
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ set(SWIFT_BENCH_MODULES
8080
single-source/DictionaryRemove
8181
single-source/DictionarySubscriptDefault
8282
single-source/DictionarySwap
83+
single-source/Differentiation
8384
single-source/Diffing
8485
single-source/DiffingMyers
8586
single-source/DropFirst
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===--- Differentiation.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#if canImport(_Differentiation)
14+
15+
import TestsUtils
16+
import _Differentiation
17+
18+
public let Differentiation = [
19+
BenchmarkInfo(
20+
name: "DifferentiationIdentity",
21+
runFunction: run_DifferentiationIdentity,
22+
tags: [.regression, .differentiation]
23+
),
24+
BenchmarkInfo(
25+
name: "DifferentiationSquare",
26+
runFunction: run_DifferentiationSquare,
27+
tags: [.regression, .differentiation]
28+
),
29+
BenchmarkInfo(
30+
name: "DifferentiationArraySum",
31+
runFunction: run_DifferentiationArraySum,
32+
tags: [.regression, .differentiation],
33+
setUpFunction: { blackHole(onesArray) }
34+
),
35+
]
36+
37+
@inline(never)
38+
public func run_DifferentiationIdentity(N: Int) {
39+
func f(_ x: Float) -> Float {
40+
x
41+
}
42+
for _ in 0..<1000*N {
43+
blackHole(valueWithGradient(at: 1, in: f))
44+
}
45+
}
46+
47+
@inline(never)
48+
public func run_DifferentiationSquare(N: Int) {
49+
func f(_ x: Float) -> Float {
50+
x * x
51+
}
52+
for _ in 0..<1000*N {
53+
blackHole(valueWithGradient(at: 1, in: f))
54+
}
55+
}
56+
57+
let onesArray: [Float] = Array(repeating: 1, count: 50)
58+
59+
@inline(never)
60+
public func run_DifferentiationArraySum(N: Int) {
61+
func sum(_ array: [Float]) -> Float {
62+
var result: Float = 0
63+
for i in withoutDerivative(at: 0..<array.count) {
64+
result += array[i]
65+
}
66+
return result
67+
}
68+
for _ in 0..<N {
69+
blackHole(valueWithGradient(at: onesArray, in: sum))
70+
}
71+
}
72+
73+
#endif

benchmark/utils/TestsUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public enum BenchmarkCategory : String {
2828
case runtime, refcount, metadata
2929
// Other general areas of compiled code validation.
3030
case abstraction, safetychecks, exceptions, bridging, concurrency, existential
31-
case exclusivity
31+
case exclusivity, differentiation
3232

3333
// Algorithms are "micro" that test some well-known algorithm in isolation:
3434
// sorting, searching, hashing, fibonaci, crypto, etc.

benchmark/utils/main.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ import DictionaryOfAnyHashableStrings
6969
import DictionaryRemove
7070
import DictionarySubscriptDefault
7171
import DictionarySwap
72+
#if canImport(_Differentiation)
73+
import Differentiation
74+
#endif
7275
import Diffing
7376
import DiffingMyers
7477
import DropFirst
@@ -258,6 +261,9 @@ registerBenchmark(DictionaryOfAnyHashableStrings)
258261
registerBenchmark(DictionaryRemove)
259262
registerBenchmark(DictionarySubscriptDefault)
260263
registerBenchmark(DictionarySwap)
264+
#if canImport(_Differentiation)
265+
registerBenchmark(Differentiation)
266+
#endif
261267
registerBenchmark(Diffing)
262268
registerBenchmark(DiffingMyers)
263269
registerBenchmark(DropFirst)

stdlib/public/Differentiation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
add_swift_target_library(swift_Differentiation ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
2424
Empty.swift
25+
"${SWIFT_SOURCE_DIR}/stdlib/linker-support/magic-symbols-for-install-name.c"
2526

2627
SWIFT_MODULE_DEPENDS_OSX Darwin
2728
SWIFT_MODULE_DEPENDS_IOS Darwin

0 commit comments

Comments
 (0)