Skip to content

Commit 7c5de89

Browse files
Marc Rasirxwei
authored andcommitted
[AutoDiff] differentiation benchmarks
1 parent b13a8e9 commit 7c5de89

File tree

4 files changed

+74
-1
lines changed

4 files changed

+74
-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: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
import TestsUtils
14+
15+
import _Differentiation
16+
17+
public let Differentiation = [
18+
BenchmarkInfo(
19+
name: "DifferentiationIdentity",
20+
runFunction: run_DifferentiationIdentity,
21+
tags: [.regression, .differentiation]
22+
),
23+
BenchmarkInfo(
24+
name: "DifferentiationSquare",
25+
runFunction: run_DifferentiationSquare,
26+
tags: [.regression, .differentiation]
27+
),
28+
BenchmarkInfo(
29+
name: "DifferentiationArraySum",
30+
runFunction: run_DifferentiationArraySum,
31+
tags: [.regression, .differentiation],
32+
setUpFunction: { blackHole(onesArray) }
33+
),
34+
]
35+
36+
@inline(never)
37+
public func run_DifferentiationIdentity(N: Int) {
38+
func f(_ x: Float) -> Float {
39+
x
40+
}
41+
for _ in 0..<1000*N {
42+
blackHole(valueWithGradient(at: 1, in: f))
43+
}
44+
}
45+
46+
@inline(never)
47+
public func run_DifferentiationSquare(N: Int) {
48+
func f(_ x: Float) -> Float {
49+
x * x
50+
}
51+
for _ in 0..<1000*N {
52+
blackHole(valueWithGradient(at: 1, in: f))
53+
}
54+
}
55+
56+
let onesArray: [Float] = Array(repeating: 1, count: 50)
57+
58+
@inline(never)
59+
public func run_DifferentiationArraySum(N: Int) {
60+
func sum(_ array: [Float]) -> Float {
61+
var result: Float = 0
62+
for i in withoutDerivative(at: 0..<array.count) {
63+
result += array[i]
64+
}
65+
return result
66+
}
67+
for _ in 0..<N {
68+
blackHole(valueWithGradient(at: onesArray, in: sum))
69+
}
70+
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import DictionaryOfAnyHashableStrings
6969
import DictionaryRemove
7070
import DictionarySubscriptDefault
7171
import DictionarySwap
72+
import Differentiation
7273
import Diffing
7374
import DiffingMyers
7475
import DropFirst
@@ -258,6 +259,7 @@ registerBenchmark(DictionaryOfAnyHashableStrings)
258259
registerBenchmark(DictionaryRemove)
259260
registerBenchmark(DictionarySubscriptDefault)
260261
registerBenchmark(DictionarySwap)
262+
registerBenchmark(Differentiation)
261263
registerBenchmark(Diffing)
262264
registerBenchmark(DiffingMyers)
263265
registerBenchmark(DropFirst)

0 commit comments

Comments
 (0)