forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiff_e2e_basic.swift
57 lines (43 loc) · 1.52 KB
/
autodiff_e2e_basic.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// RUN: %target-swift-frontend -emit-sil %s | %FileCheck %s
@differentiable(reverse, adjoint: adjointId)
func id(_ x: Float) -> Float {
return x
}
func adjointId(_ x: Float, originalValue: Float, seed: Float) -> Float {
return seed
}
_ = #gradient(id)(2)
// CHECK: @{{.*}}id{{.*}}__grad_src_0_wrt_0
// CHECK-LABEL: @{{.*}}id{{.*}}__grad_src_0_wrt_0_s_p
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import func Darwin.exp
#else
import func Glibc.exp
#endif
@differentiable(reverse, primal: primalSigmoid, adjoint: adjointSigmoid)
func sigmoid(_ x: Double) -> Double {
return 1.0 / (1.0 + exp(-x))
}
func primalSigmoid(_ x: Double) -> (checkpoints: (Double, Double, Double), result: Double) {
let minusX = -x
let expon = exp(minusX)
let plus = 1.0 + expon
let div = 1.0 / plus
return (checkpoints: (minusX, expon, plus), result: div)
}
func adjointSigmoid(_ x: Double, checkpoints: (Double, Double, Double), result: Double, seed: Double) -> Double {
return result * (1 - result)
}
let x = #gradient(sigmoid)(3)
let (value: y, gradient: z) = #valueAndGradient(sigmoid)(4)
print(x * z)
// CHECK: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0
// CHECK: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0_s_p
// CHECK: @{{.*}}sigmoid{{.*}}__grad_src_0_wrt_0_p
public func publicFunc(_ x: Float) -> Float {
return x + x
}
_ = #gradient(publicFunc)
// CHECK: sil non_abi @{{.*}}publicFunc{{.*}}__grad_src_0_wrt_0
// CHECK: sil non_abi @{{.*}}publicFunc{{.*}}__primal_src_0_wrt_0
// CHECK: sil non_abi @{{.*}}publicFunc{{.*}}__adjoint_src_0_wrt_0