Skip to content

Commit a0d197e

Browse files
committed
Add negative test for TF-1175.
Class-typed arguments are not always marked active. This produces incorrect derivative results.
1 parent 91c4e26 commit a0d197e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

test/AutoDiff/downstream/class_differentiation.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,32 @@ ClassTests.test("AddressOnlyTangentVector") {
138138
}
139139
*/
140140

141+
// TF-1175: Test whether class-typed arguments are not marked active.
142+
ClassTests.test("ClassArgumentActivity") {
143+
class C: Differentiable {
144+
@differentiable
145+
var x: Float
146+
147+
init(_ x: Float) {
148+
self.x = x
149+
}
150+
151+
// Note: this method mutates `self`. However, since `C` is a class, the
152+
// method type does not involve `inout` arguments: `(C) -> () -> ()`.
153+
func square() {
154+
x *= x
155+
}
156+
}
157+
158+
// Returns `x * x`.
159+
func squared(_ x: Float) -> Float {
160+
var c = C(x)
161+
c.square() // FIXME(TF-1175): doesn't get differentiated!
162+
return c.x
163+
}
164+
// FIXME(TF-1175): Find a robust solution so that derivatives are correct.
165+
// expectEqual((100, 20), valueWithGradient(at: 10, in: squared))
166+
expectEqual((100, 1), valueWithGradient(at: 10, in: squared))
167+
}
168+
141169
runAllTests()

0 commit comments

Comments
 (0)