|
| 1 | +// RUN: %target-swift-frontend -parse -verify %s |
| 2 | + |
| 3 | +/// Good |
| 4 | + |
| 5 | +@differentiating(sin) // ok |
| 6 | +func jvpSin(x: @nondiff Float) |
| 7 | +-> (value: Float, differential: (Float)-> (Float)) { |
| 8 | + return (x, { $0 }) |
| 9 | +} |
| 10 | + |
| 11 | +@differentiating(sin, wrt: x) // ok |
| 12 | +func vjpSin(x: Float) -> (value: Float, pullback: (Float) -> Float) { |
| 13 | + return (x, { $0 }) |
| 14 | +} |
| 15 | + |
| 16 | +@differentiating(add, wrt: (x, y)) // ok |
| 17 | +func vjpAdd(x: Float, y: Float) |
| 18 | +-> (value: Float, pullback: (Float) -> (Float, Float)) { |
| 19 | + return (x + y, { ($0, $0) }) |
| 20 | +} |
| 21 | + |
| 22 | +extension AdditiveArithmetic where Self : Differentiable { |
| 23 | + @differentiating(+) // ok |
| 24 | + static func vjpPlus(x: Self, y: Self) -> (value: Self, |
| 25 | + pullback: (Self.TangentVector) -> (Self.TangentVector, Self.TangentVector)) { |
| 26 | + return (x + y, { v in (v, v) }) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +@differentiating(linear) // ok |
| 31 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 32 | + return (x, { $0 }) |
| 33 | +} |
| 34 | + |
| 35 | +@differentiating(linear, linear) // ok |
| 36 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 37 | + return (x, { $0 }) |
| 38 | +} |
| 39 | + |
| 40 | +@differentiating(foo, linear) // ok |
| 41 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 42 | + return (x, { $0 }) |
| 43 | +} |
| 44 | + |
| 45 | +@differentiating(foo, linear, wrt: x) // ok |
| 46 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 47 | + return (x, { $0 }) |
| 48 | +} |
| 49 | + |
| 50 | +/// Bad |
| 51 | + |
| 52 | +// expected-error @+3 {{expected an original function name}} |
| 53 | +// expected-error @+2 {{expected ')' in 'differentiating' attribute}} |
| 54 | +// expected-error @+1 {{expected declaration}} |
| 55 | +@differentiating(3) |
| 56 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 57 | + return (x, { $0 }) |
| 58 | +} |
| 59 | + |
| 60 | +// expected-error @+2 {{expected either 'linear' or 'wrt:'}} |
| 61 | +// expected-error @+1 {{expected declaration}} |
| 62 | +@differentiating(linear, foo) |
| 63 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 64 | + return (x, { $0 }) |
| 65 | +} |
| 66 | + |
| 67 | +// expected-error @+2 {{expected ')' in 'differentiating' attribute}} |
| 68 | +// expected-error @+1 {{expected declaration}} |
| 69 | +@differentiating(foo, wrt: x, linear) |
| 70 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 71 | + return (x, { $0 }) |
| 72 | +} |
| 73 | + |
| 74 | +// expected-error @+2 {{unexpected ',' separator}} |
| 75 | +// expected-error @+1 {{expected declaration}} |
| 76 | +@differentiating(foo,) |
| 77 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 78 | + return (x, { $0 }) |
| 79 | +} |
| 80 | + |
| 81 | +// expected-error @+2 {{expected ')' in 'differentiating' attribute}} |
| 82 | +// expected-error @+1 {{expected declaration}} |
| 83 | +@differentiating(foo, wrt: x,) |
| 84 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 85 | + return (x, { $0 }) |
| 86 | +} |
| 87 | + |
| 88 | +// expected-error @+2 {{expected either 'linear' or 'wrt:'}} |
| 89 | +// expected-error @+1 {{expected declaration}} |
| 90 | +@differentiating(linear, foo,) |
| 91 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 92 | + return (x, { $0 }) |
| 93 | +} |
| 94 | + |
| 95 | +// expected-error @+2 {{unexpected ',' separator}} |
| 96 | +// expected-error @+1 {{expected declaration}} |
| 97 | +@differentiating(linear,) |
| 98 | +func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) { |
| 99 | + return (x, { $0 }) |
| 100 | +} |
0 commit comments