|
| 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 |
0 commit comments