-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathoptional_property.swift
206 lines (176 loc) · 5.93 KB
/
optional_property.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// RUN: %target-run-simple-swift
// RUN: %target-swift-emit-sil -Xllvm -debug-only=differentiation -module-name null -o /dev/null 2>&1 %s | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: asserts
// Test differentiation of `Optional` properties.
import DifferentiationUnittest
import StdlibUnittest
var OptionalTests = TestSuite("OptionalPropertyDifferentiation")
// Test `Optional` struct stored properties.
struct Struct: Differentiable {
var stored: Float
var optional: Float?
@differentiable(reverse)
func method() -> Float {
let s: Struct
do {
let tmp = Struct(stored: stored, optional: optional)
let tuple = (tmp, tmp)
s = tuple.0
}
if let x = s.optional {
return x * s.stored
}
return s.stored
}
}
// Check active SIL instructions in representative original functions.
// This tests SIL instruction coverage of derivative function cloners (e.g. PullbackCloner).
// CHECK-LABEL: [AD] Activity info for ${{.*}}Struct{{.*}}method{{.*}} at parameter indices (0) and result indices (0)
// CHECK: [ACTIVE] {{.*}} struct_extract {{%.*}} : $Struct, #Struct.stored
// CHECK: [ACTIVE] {{.*}} struct_extract {{%.*}} : $Struct, #Struct.optional
// CHECK: [ACTIVE] {{.*}} tuple ({{%.*}} : $Struct, {{%.*}} : $Struct)
// CHECK: [ACTIVE] {{.*}} destructure_tuple {{%.*}} : $(Struct, Struct)
// CHECK: [ACTIVE] {{.*}} struct_element_addr {{%.*}} : $*Struct, #Struct.optional
// CHECK: [ACTIVE] {{.*}} struct_element_addr {{%.*}} : $*Struct, #Struct.stored
// CHECK-LABEL: [AD] Activity info for $s4null6StructV6stored8optionalACSf_SfSgtcfC at parameter indices (0, 1) and result indices (0)
// CHECK: [ACTIVE] {{%.*}} struct $Struct ({{%.*}} : $Float, {{%.*}} : $Optional<Float>)
struct StructTracked: Differentiable {
var stored: NonresilientTracked<Float>
var optional: NonresilientTracked<Float>?
@differentiable(reverse)
func method() -> NonresilientTracked<Float> {
let s: StructTracked
do {
let tmp = StructTracked(stored: stored, optional: optional)
let tuple = (tmp, tmp)
s = tuple.0
}
if let x = s.optional {
return x * s.stored
}
return s.stored
}
}
struct StructGeneric<T: Differentiable>: Differentiable {
var stored: T
var optional: T?
@differentiable(reverse)
func method() -> T {
let s: StructGeneric
do {
let tmp = StructGeneric(stored: stored, optional: optional)
let tuple = (tmp, tmp)
s = tuple.0
}
if let x = s.optional {
return x
}
return s.stored
}
}
OptionalTests.test("Optional struct stored properties") {
expectEqual(
valueWithGradient(at: Struct(stored: 3, optional: 4), of: { $0.method() }),
(12, .init(stored: 4, optional: .init(3))))
expectEqual(
valueWithGradient(at: Struct(stored: 3, optional: nil), of: { $0.method() }),
(3, .init(stored: 1, optional: .init(0))))
expectEqual(
valueWithGradient(at: StructTracked(stored: 3, optional: 4), of: { $0.method() }),
(12, .init(stored: 4, optional: .init(3))))
expectEqual(
valueWithGradient(at: StructTracked(stored: 3, optional: nil), of: { $0.method() }),
(3, .init(stored: 1, optional: .init(0))))
expectEqual(
valueWithGradient(at: StructGeneric<Float>(stored: 3, optional: 4), of: { $0.method() }),
(4, .init(stored: 0, optional: .init(1))))
expectEqual(
valueWithGradient(at: StructGeneric<Float>(stored: 3, optional: nil), of: { $0.method() }),
(3, .init(stored: 1, optional: .init(0))))
}
// Test `Optional` class stored properties.
struct Class: Differentiable {
var stored: Float
var optional: Float?
init(stored: Float, optional: Float?) {
self.stored = stored
self.optional = optional
}
@differentiable(reverse)
func method() -> Float {
let c: Class
do {
let tmp = Class(stored: stored, optional: optional)
let tuple = (tmp, tmp)
c = tuple.0
}
if let x = c.optional {
return x * c.stored
}
return c.stored
}
}
struct ClassTracked: Differentiable {
var stored: NonresilientTracked<Float>
var optional: NonresilientTracked<Float>?
init(stored: NonresilientTracked<Float>, optional: NonresilientTracked<Float>?) {
self.stored = stored
self.optional = optional
}
@differentiable(reverse)
func method() -> NonresilientTracked<Float> {
let c: ClassTracked
do {
let tmp = ClassTracked(stored: stored, optional: optional)
let tuple = (tmp, tmp)
c = tuple.0
}
if let x = c.optional {
return x * c.stored
}
return c.stored
}
}
struct ClassGeneric<T: Differentiable>: Differentiable {
var stored: T
var optional: T?
init(stored: T, optional: T?) {
self.stored = stored
self.optional = optional
}
@differentiable(reverse)
func method() -> T {
let c: ClassGeneric
do {
let tmp = ClassGeneric(stored: stored, optional: optional)
let tuple = (tmp, tmp)
c = tuple.0
}
if let x = c.optional {
return x
}
return c.stored
}
}
OptionalTests.test("Optional class stored properties") {
expectEqual(
valueWithGradient(at: Class(stored: 3, optional: 4), of: { $0.method() }),
(12, .init(stored: 4, optional: .init(3))))
expectEqual(
valueWithGradient(at: Class(stored: 3, optional: nil), of: { $0.method() }),
(3, .init(stored: 1, optional: .init(0))))
expectEqual(
valueWithGradient(at: ClassTracked(stored: 3, optional: 4), of: { $0.method() }),
(12, .init(stored: 4, optional: .init(3))))
expectEqual(
valueWithGradient(at: ClassTracked(stored: 3, optional: nil), of: { $0.method() }),
(3, .init(stored: 1, optional: .init(0))))
expectEqual(
valueWithGradient(at: ClassGeneric<Tracked<Float>>(stored: 3, optional: 4), of: { $0.method() }),
(4, .init(stored: 0, optional: .init(1))))
expectEqual(
valueWithGradient(at: ClassGeneric<Tracked<Float>>(stored: 3, optional: nil), of: { $0.method() }),
(3, .init(stored: 1, optional: .init(0))))
}
runAllTests()