-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathdifferentiating_attr_parse.swift
100 lines (83 loc) · 2.95 KB
/
differentiating_attr_parse.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
// RUN: %target-swift-frontend -parse -verify %s
/// Good
@differentiating(sin) // ok
func jvpSin(x: @nondiff Float)
-> (value: Float, differential: (Float)-> (Float)) {
return (x, { $0 })
}
@differentiating(sin, wrt: x) // ok
func vjpSin(x: Float) -> (value: Float, pullback: (Float) -> Float) {
return (x, { $0 })
}
@differentiating(add, wrt: (x, y)) // ok
func vjpAdd(x: Float, y: Float)
-> (value: Float, pullback: (Float) -> (Float, Float)) {
return (x + y, { ($0, $0) })
}
extension AdditiveArithmetic where Self : Differentiable {
@differentiating(+) // ok
static func vjpPlus(x: Self, y: Self) -> (value: Self,
pullback: (Self.TangentVector) -> (Self.TangentVector, Self.TangentVector)) {
return (x + y, { v in (v, v) })
}
}
@differentiating(linear) // ok
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
@differentiating(linear, linear) // ok
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
@differentiating(foo, linear) // ok
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
@differentiating(foo, linear, wrt: x) // ok
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
/// Bad
// expected-error @+3 {{expected an original function name}}
// expected-error @+2 {{expected ')' in 'differentiating' attribute}}
// expected-error @+1 {{expected declaration}}
@differentiating(3)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
// expected-error @+2 {{expected either 'linear' or 'wrt:'}}
// expected-error @+1 {{expected declaration}}
@differentiating(linear, foo)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
// expected-error @+2 {{expected ')' in 'differentiating' attribute}}
// expected-error @+1 {{expected declaration}}
@differentiating(foo, wrt: x, linear)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
// expected-error @+2 {{unexpected ',' separator}}
// expected-error @+1 {{expected declaration}}
@differentiating(foo,)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
// expected-error @+2 {{expected ')' in 'differentiating' attribute}}
// expected-error @+1 {{expected declaration}}
@differentiating(foo, wrt: x,)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
// expected-error @+2 {{expected either 'linear' or 'wrt:'}}
// expected-error @+1 {{expected declaration}}
@differentiating(linear, foo,)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}
// expected-error @+2 {{unexpected ',' separator}}
// expected-error @+1 {{expected declaration}}
@differentiating(linear,)
func dfoo(x: Float) -> (value: Float, differential: (Float) -> (Float)) {
return (x, { $0 })
}